1Git(3) User Contributed Perl Documentation Git(3)
2
3
4
6 Git - Perl interface to the Git version control system
7
9 use Git;
10
11 my $version = Git::command_oneline('version');
12
13 git_cmd_try { Git::command_noisy('update-server-info') }
14 '%s failed w/ code %d';
15
16 my $repo = Git->repository (Directory => '/srv/git/cogito.git');
17
18 my @revs = $repo->command('rev-list', '--since=last monday', '--all');
19
20 my ($fh, $c) = $repo->command_output_pipe('rev-list', '--since=last monday', '--all');
21 my $lastrev = <$fh>; chomp $lastrev;
22 $repo->command_close_pipe($fh, $c);
23
24 my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ],
25 STDERR => 0 );
26
28 This module provides Perl scripts easy way to interface the Git version
29 control system. The modules have an easy and well-tested way to call
30 arbitrary Git commands; in the future, the interface will also provide
31 specialized methods for doing easily operations which are not totally
32 trivial to do over the generic command interface.
33
34 While some commands can be executed outside of any context (e.g. 'ver‐
35 sion' or 'init'), most operations require a repository context, which
36 in practice means getting an instance of the Git object using the
37 repository() constructor. (In the future, we will also get a
38 new_repository() constructor.) All commands called as methods of the
39 object are then executed in the context of the repository.
40
41 Part of the "repository state" is also information about path to the
42 attached working copy (unless you work with a bare repository). You can
43 also navigate inside of the working copy using the "wc_chdir()" method.
44 (Note that the repository object is self-contained and will not change
45 working directory of your process.)
46
47 TODO: In the future, we might also do
48
49 my $remoterepo = $repo->remote_repository (Name => 'cogito', Branch => 'master');
50 $remoterepo ⎪⎪= Git->remote_repository ('http://git.or.cz/cogito.git/');
51 my @refs = $remoterepo->refs();
52
53 Currently, the module merely wraps calls to external Git tools. In the
54 future, it will provide a much faster way to interact with Git by link‐
55 ing directly to libgit. This should be completely opaque to the user,
56 though (performance increate nonwithstanding).
57
59 repository ( OPTIONS )
60 repository ( DIRECTORY )
61 repository ()
62 Construct a new repository object. "OPTIONS" are passed in a hash
63 like fashion, using key and value pairs. Possible options are:
64
65 Repository - Path to the Git repository.
66
67 WorkingCopy - Path to the associated working copy; not strictly
68 required as many commands will happily crunch on a bare repository.
69
70 WorkingSubdir - Subdirectory in the working copy to work inside.
71 Just left undefined if you do not want to limit the scope of opera‐
72 tions.
73
74 Directory - Path to the Git working directory in its usual setup.
75 The ".git" directory is searched in the directory and all the par‐
76 ent directories; if found, "WorkingCopy" is set to the directory
77 containing it and "Repository" to the ".git" directory itself. If
78 no ".git" directory was found, the "Directory" is assumed to be a
79 bare repository, "Repository" is set to point at it and "Working‐
80 Copy" is left undefined. If the $GIT_DIR environment variable is
81 set, things behave as expected as well.
82
83 You should not use both "Directory" and either of "Repository" and
84 "WorkingCopy" - the results of that are undefined.
85
86 Alternatively, a directory path may be passed as a single scalar
87 argument to the constructor; it is equivalent to setting only the
88 "Directory" option field.
89
90 Calling the constructor with no options whatsoever is equivalent to
91 calling it with "Directory => '.'". In general, if you are building
92 a standard porcelain command, simply doing "Git->repository()"
93 should do the right thing and setup the object to reflect exactly
94 where the user is right now.
95
97 command ( COMMAND [, ARGUMENTS... ] )
98 command ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
99 Execute the given Git "COMMAND" (specify it without the 'git-' pre‐
100 fix), optionally with the specified extra "ARGUMENTS".
101
102 The second more elaborate form can be used if you want to further
103 adjust the command execution. Currently, only one option is sup‐
104 ported:
105
106 STDERR - How to deal with the command's error output. By default
107 ("undef") it is delivered to the caller's "STDERR". A false value
108 (0 or '') will cause it to be thrown away. If you want to process
109 it, you can get it in a filehandle you specify, but you must be
110 extremely careful; if the error output is not very short and you
111 want to read it in the same process as where you called "com‐
112 mand()", you are set up for a nice deadlock!
113
114 The method can be called without any instance or on a specified Git
115 repository (in that case the command will be run in the repository
116 context).
117
118 In scalar context, it returns all the command output in a single
119 string (verbatim).
120
121 In array context, it returns an array containing lines printed to
122 the command's stdout (without trailing newlines).
123
124 In both cases, the command's stdin and stderr are the same as the
125 caller's.
126
127 command_oneline ( COMMAND [, ARGUMENTS... ] )
128 command_oneline ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
129 Execute the given "COMMAND" in the same way as command() does but
130 always return a scalar string containing the first line of the com‐
131 mand's standard output.
132
133 command_output_pipe ( COMMAND [, ARGUMENTS... ] )
134 command_output_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
135 Execute the given "COMMAND" in the same way as command() does but
136 return a pipe filehandle from which the command output can be read.
137
138 The function can return "($pipe, $ctx)" in array context. See
139 "command_close_pipe()" for details.
140
141 command_input_pipe ( COMMAND [, ARGUMENTS... ] )
142 command_input_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
143 Execute the given "COMMAND" in the same way as command_out‐
144 put_pipe() does but return an input pipe filehandle instead; the
145 command output is not captured.
146
147 The function can return "($pipe, $ctx)" in array context. See
148 "command_close_pipe()" for details.
149
150 command_close_pipe ( PIPE [, CTX ] )
151 Close the "PIPE" as returned from "command_*_pipe()", checking
152 whether the command finished successfully. The optional "CTX" argu‐
153 ment is required if you want to see the command name in the error
154 message, and it is the second value returned by "command_*_pipe()"
155 when called in array context. The call idiom is:
156
157 my ($fh, $ctx) = $r->command_output_pipe('status');
158 while (<$fh>) { ... }
159 $r->command_close_pipe($fh, $ctx);
160
161 Note that you should not rely on whatever actually is in "CTX";
162 currently it is simply the command name but in future the context
163 might have more complicated structure.
164
165 command_noisy ( COMMAND [, ARGUMENTS... ] )
166 Execute the given "COMMAND" in the same way as command() does but
167 do not capture the command output - the standard output is not
168 redirected and goes to the standard output of the caller applica‐
169 tion.
170
171 While the method is called command_noisy(), you might want to as
172 well use it for the most silent Git commands which you know will
173 never pollute your stdout but you want to avoid the overhead of the
174 pipe setup when calling them.
175
176 The function returns only after the command has finished running.
177
178 version ()
179 Return the Git version in use.
180
181 exec_path ()
182 Return path to the Git sub-command executables (the same as "git
183 --exec-path"). Useful mostly only internally.
184
185 repo_path ()
186 Return path to the git repository. Must be called on a repository
187 instance.
188
189 wc_path ()
190 Return path to the working copy. Must be called on a repository
191 instance.
192
193 wc_subdir ()
194 Return path to the subdirectory inside of a working copy. Must be
195 called on a repository instance.
196
197 wc_chdir ( SUBDIR )
198 Change the working copy subdirectory to work within. The "SUBDIR"
199 is relative to the working copy root directory (not the current
200 subdirectory). Must be called on a repository instance attached to
201 a working copy and the directory must exist.
202
203 config ( VARIABLE )
204 Retrieve the configuration "VARIABLE" in the same manner as "con‐
205 fig" does. In scalar context requires the variable to be set only
206 one time (exception is thrown otherwise), in array context returns
207 allows the variable to be set multiple times and returns all the
208 values.
209
210 Must be called on a repository instance.
211
212 This currently wraps command('config') so it is not so fast.
213
214 config_bool ( VARIABLE )
215 Retrieve the bool configuration "VARIABLE". The return value is
216 usable as a boolean in perl (and "undef" if it's not defined, of
217 course).
218
219 Must be called on a repository instance.
220
221 This currently wraps command('config') so it is not so fast.
222
223 ident ( TYPE ⎪ IDENTSTR )
224 ident_person ( TYPE ⎪ IDENTSTR ⎪ IDENTARRAY )
225 This suite of functions retrieves and parses ident information, as
226 stored in the commit and tag objects or produced by "var
227 GIT_type_IDENT" (thus "TYPE" can be either author or committer;
228 case is insignificant).
229
230 The "ident" method retrieves the ident information from "git-var"
231 and either returns it as a scalar string or as an array with the
232 fields parsed. Alternatively, it can take a prepared ident string
233 (e.g. from the commit object) and just parse it.
234
235 "ident_person" returns the person part of the ident - name and
236 email; it can take the same arguments as "ident" or the array
237 returned by "ident".
238
239 The synopsis is like:
240
241 my ($name, $email, $time_tz) = ident('author');
242 "$name <$email>" eq ident_person('author');
243 "$name <$email>" eq ident_person($name);
244 $time_tz =~ /^\d+ [+-]\d{4}$/;
245
246 Both methods must be called on a repository instance.
247
248 hash_object ( TYPE, FILENAME )
249 Compute the SHA1 object id of the given "FILENAME" (or data waiting
250 in "FILEHANDLE") considering it is of the "TYPE" object type
251 ("blob", "commit", "tree").
252
253 The method can be called without any instance or on a specified Git
254 repository, it makes zero difference.
255
256 The function returns the SHA1 hash.
257
259 All functions are supposed to throw Perl exceptions in case of errors.
260 See the Error module on how to catch those. Most exceptions are mere
261 Error::Simple instances.
262
263 However, the "command()", "command_oneline()" and "command_noisy()"
264 functions suite can throw "Git::Error::Command" exceptions as well:
265 those are thrown when the external command returns an error code and
266 contain the error code as well as access to the captured command's out‐
267 put. The exception class provides the usual "stringify" and "value"
268 (command's exit code) methods and in addition also a "cmd_output"
269 method that returns either an array or a string with the captured com‐
270 mand output (depending on the original function call context; "com‐
271 mand_noisy()" returns "undef") and $<cmdline> which returns the command
272 and its arguments (but without proper quoting).
273
274 Note that the "command_*_pipe()" functions cannot throw this exception
275 since it has no idea whether the command failed or not. You will only
276 find out at the time you "close" the pipe; if you want to have that
277 automated, use "command_close_pipe()", which can throw the exception.
278
279 git_cmd_try { CODE } ERRMSG
280 This magical statement will automatically catch any
281 "Git::Error::Command" exceptions thrown by "CODE" and make your
282 program die with "ERRMSG" on its lips; the message will have %s
283 substituted for the command line and %d for the exit status. This
284 statement is useful mostly for producing more user-friendly error
285 messages.
286
287 In case of no exception caught the statement returns "CODE"'s
288 return value.
289
290 Note that this is the only auto-exported function.
291
293 Copyright 2006 by Petr Baudis <pasky@suse.cz>.
294
295 This module is free software; it may be used, copied, modified and dis‐
296 tributed under the terms of the GNU General Public Licence, either ver‐
297 sion 2, or (at your option) any later version.
298
299
300
301perl v5.8.8 2007-09-29 Git(3)