Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1 | =head1 NAME |
| 2 | |
| 3 | Git - Perl interface to the Git version control system |
| 4 | |
| 5 | =cut |
| 6 | |
| 7 | |
| 8 | package Git; |
| 9 | |
| 10 | use strict; |
| 11 | |
| 12 | |
| 13 | BEGIN { |
| 14 | |
| 15 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK); |
| 16 | |
| 17 | # Totally unstable API. |
| 18 | $VERSION = '0.01'; |
| 19 | |
| 20 | |
| 21 | =head1 SYNOPSIS |
| 22 | |
| 23 | use Git; |
| 24 | |
| 25 | my $version = Git::command_oneline('version'); |
| 26 | |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 27 | git_cmd_try { Git::command_noisy('update-server-info') } |
| 28 | '%s failed w/ code %d'; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 29 | |
| 30 | my $repo = Git->repository (Directory => '/srv/git/cogito.git'); |
| 31 | |
| 32 | |
| 33 | my @revs = $repo->command('rev-list', '--since=last monday', '--all'); |
| 34 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 35 | my ($fh, $c) = $repo->command_output_pipe('rev-list', '--since=last monday', '--all'); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 36 | my $lastrev = <$fh>; chomp $lastrev; |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 37 | $repo->command_close_pipe($fh, $c); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 38 | |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 39 | my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ], |
| 40 | STDERR => 0 ); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 41 | |
Adam Roben | 7182530 | 2008-05-23 16:19:40 +0200 | [diff] [blame] | 42 | my $sha1 = $repo->hash_and_insert_object('file.txt'); |
| 43 | my $tempfile = tempfile(); |
| 44 | my $size = $repo->cat_blob($sha1, $tempfile); |
| 45 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 46 | =cut |
| 47 | |
| 48 | |
| 49 | require Exporter; |
| 50 | |
| 51 | @ISA = qw(Exporter); |
| 52 | |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 53 | @EXPORT = qw(git_cmd_try); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 54 | |
| 55 | # Methods which can be called as standalone functions as well: |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 56 | @EXPORT_OK = qw(command command_oneline command_noisy |
| 57 | command_output_pipe command_input_pipe command_close_pipe |
Adam Roben | d1a29af | 2008-05-23 16:19:39 +0200 | [diff] [blame] | 58 | command_bidi_pipe command_close_bidi_pipe |
Petr Baudis | 31a92f6 | 2008-07-08 19:48:04 +0200 | [diff] [blame] | 59 | version exec_path hash_object git_cmd_try |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 60 | remote_refs |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 61 | temp_acquire temp_release temp_reset temp_path); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 62 | |
| 63 | |
| 64 | =head1 DESCRIPTION |
| 65 | |
| 66 | This module provides Perl scripts easy way to interface the Git version control |
| 67 | system. The modules have an easy and well-tested way to call arbitrary Git |
| 68 | commands; in the future, the interface will also provide specialized methods |
| 69 | for doing easily operations which are not totally trivial to do over |
| 70 | the generic command interface. |
| 71 | |
| 72 | While some commands can be executed outside of any context (e.g. 'version' |
Nicolas Pitre | 5c94f87 | 2007-01-12 16:01:46 -0500 | [diff] [blame] | 73 | or 'init'), most operations require a repository context, which in practice |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 74 | means getting an instance of the Git object using the repository() constructor. |
| 75 | (In the future, we will also get a new_repository() constructor.) All commands |
| 76 | called as methods of the object are then executed in the context of the |
| 77 | repository. |
| 78 | |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 79 | Part of the "repository state" is also information about path to the attached |
| 80 | working copy (unless you work with a bare repository). You can also navigate |
| 81 | inside of the working copy using the C<wc_chdir()> method. (Note that |
| 82 | the repository object is self-contained and will not change working directory |
| 83 | of your process.) |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 84 | |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 85 | TODO: In the future, we might also do |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 86 | |
| 87 | my $remoterepo = $repo->remote_repository (Name => 'cogito', Branch => 'master'); |
| 88 | $remoterepo ||= Git->remote_repository ('http://git.or.cz/cogito.git/'); |
| 89 | my @refs = $remoterepo->refs(); |
| 90 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 91 | Currently, the module merely wraps calls to external Git tools. In the future, |
| 92 | it will provide a much faster way to interact with Git by linking directly |
| 93 | to libgit. This should be completely opaque to the user, though (performance |
Abhijit Menon-Sen | 9751a32 | 2008-08-05 07:36:16 +0530 | [diff] [blame] | 94 | increase notwithstanding). |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 95 | |
| 96 | =cut |
| 97 | |
| 98 | |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 99 | use Carp qw(carp croak); # but croak is bad - throw instead |
Petr Baudis | 97b16c0 | 2006-06-24 04:34:42 +0200 | [diff] [blame] | 100 | use Error qw(:try); |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 101 | use Cwd qw(abs_path); |
Adam Roben | d1a29af | 2008-05-23 16:19:39 +0200 | [diff] [blame] | 102 | use IPC::Open2 qw(open2); |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 103 | use Fcntl qw(SEEK_SET SEEK_CUR); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | =head1 CONSTRUCTORS |
| 108 | |
| 109 | =over 4 |
| 110 | |
| 111 | =item repository ( OPTIONS ) |
| 112 | |
| 113 | =item repository ( DIRECTORY ) |
| 114 | |
| 115 | =item repository () |
| 116 | |
| 117 | Construct a new repository object. |
| 118 | C<OPTIONS> are passed in a hash like fashion, using key and value pairs. |
| 119 | Possible options are: |
| 120 | |
| 121 | B<Repository> - Path to the Git repository. |
| 122 | |
| 123 | B<WorkingCopy> - Path to the associated working copy; not strictly required |
| 124 | as many commands will happily crunch on a bare repository. |
| 125 | |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 126 | B<WorkingSubdir> - Subdirectory in the working copy to work inside. |
| 127 | Just left undefined if you do not want to limit the scope of operations. |
| 128 | |
| 129 | B<Directory> - Path to the Git working directory in its usual setup. |
| 130 | The C<.git> directory is searched in the directory and all the parent |
| 131 | directories; if found, C<WorkingCopy> is set to the directory containing |
| 132 | it and C<Repository> to the C<.git> directory itself. If no C<.git> |
| 133 | directory was found, the C<Directory> is assumed to be a bare repository, |
| 134 | C<Repository> is set to point at it and C<WorkingCopy> is left undefined. |
| 135 | If the C<$GIT_DIR> environment variable is set, things behave as expected |
| 136 | as well. |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 137 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 138 | You should not use both C<Directory> and either of C<Repository> and |
| 139 | C<WorkingCopy> - the results of that are undefined. |
| 140 | |
| 141 | Alternatively, a directory path may be passed as a single scalar argument |
| 142 | to the constructor; it is equivalent to setting only the C<Directory> option |
| 143 | field. |
| 144 | |
| 145 | Calling the constructor with no options whatsoever is equivalent to |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 146 | calling it with C<< Directory => '.' >>. In general, if you are building |
| 147 | a standard porcelain command, simply doing C<< Git->repository() >> should |
| 148 | do the right thing and setup the object to reflect exactly where the user |
| 149 | is right now. |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 150 | |
| 151 | =cut |
| 152 | |
| 153 | sub repository { |
| 154 | my $class = shift; |
| 155 | my @args = @_; |
| 156 | my %opts = (); |
| 157 | my $self; |
| 158 | |
| 159 | if (defined $args[0]) { |
| 160 | if ($#args % 2 != 1) { |
| 161 | # Not a hash. |
Petr Baudis | 97b16c0 | 2006-06-24 04:34:42 +0200 | [diff] [blame] | 162 | $#args == 0 or throw Error::Simple("bad usage"); |
| 163 | %opts = ( Directory => $args[0] ); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 164 | } else { |
| 165 | %opts = @args; |
| 166 | } |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 167 | } |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 168 | |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 169 | if (not defined $opts{Repository} and not defined $opts{WorkingCopy}) { |
| 170 | $opts{Directory} ||= '.'; |
| 171 | } |
| 172 | |
| 173 | if ($opts{Directory}) { |
| 174 | -d $opts{Directory} or throw Error::Simple("Directory not found: $!"); |
| 175 | |
| 176 | my $search = Git->repository(WorkingCopy => $opts{Directory}); |
| 177 | my $dir; |
| 178 | try { |
| 179 | $dir = $search->command_oneline(['rev-parse', '--git-dir'], |
| 180 | STDERR => 0); |
| 181 | } catch Git::Error::Command with { |
| 182 | $dir = undef; |
| 183 | }; |
| 184 | |
| 185 | if ($dir) { |
Petr Baudis | 71efe0c | 2006-06-25 03:54:28 +0200 | [diff] [blame] | 186 | $dir =~ m#^/# or $dir = $opts{Directory} . '/' . $dir; |
| 187 | $opts{Repository} = $dir; |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 188 | |
| 189 | # If --git-dir went ok, this shouldn't die either. |
| 190 | my $prefix = $search->command_oneline('rev-parse', '--show-prefix'); |
| 191 | $dir = abs_path($opts{Directory}) . '/'; |
| 192 | if ($prefix) { |
| 193 | if (substr($dir, -length($prefix)) ne $prefix) { |
| 194 | throw Error::Simple("rev-parse confused me - $dir does not have trailing $prefix"); |
| 195 | } |
| 196 | substr($dir, -length($prefix)) = ''; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 197 | } |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 198 | $opts{WorkingCopy} = $dir; |
| 199 | $opts{WorkingSubdir} = $prefix; |
| 200 | |
| 201 | } else { |
| 202 | # A bare repository? Let's see... |
| 203 | $dir = $opts{Directory}; |
| 204 | |
| 205 | unless (-d "$dir/refs" and -d "$dir/objects" and -e "$dir/HEAD") { |
| 206 | # Mimick git-rev-parse --git-dir error message: |
Richard Hartmann | f66bc5f | 2008-12-22 00:17:32 +0100 | [diff] [blame] | 207 | throw Error::Simple("fatal: Not a git repository: $dir"); |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 208 | } |
| 209 | my $search = Git->repository(Repository => $dir); |
| 210 | try { |
| 211 | $search->command('symbolic-ref', 'HEAD'); |
| 212 | } catch Git::Error::Command with { |
| 213 | # Mimick git-rev-parse --git-dir error message: |
Richard Hartmann | f66bc5f | 2008-12-22 00:17:32 +0100 | [diff] [blame] | 214 | throw Error::Simple("fatal: Not a git repository: $dir"); |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | $opts{Repository} = abs_path($dir); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 218 | } |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 219 | |
| 220 | delete $opts{Directory}; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Junio C Hamano | 81a7173 | 2006-09-02 22:58:48 -0700 | [diff] [blame] | 223 | $self = { opts => \%opts }; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 224 | bless $self, $class; |
| 225 | } |
| 226 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 227 | =back |
| 228 | |
| 229 | =head1 METHODS |
| 230 | |
| 231 | =over 4 |
| 232 | |
| 233 | =item command ( COMMAND [, ARGUMENTS... ] ) |
| 234 | |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 235 | =item command ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) |
| 236 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 237 | Execute the given Git C<COMMAND> (specify it without the 'git-' |
| 238 | prefix), optionally with the specified extra C<ARGUMENTS>. |
| 239 | |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 240 | The second more elaborate form can be used if you want to further adjust |
| 241 | the command execution. Currently, only one option is supported: |
| 242 | |
| 243 | B<STDERR> - How to deal with the command's error output. By default (C<undef>) |
| 244 | it is delivered to the caller's C<STDERR>. A false value (0 or '') will cause |
| 245 | it to be thrown away. If you want to process it, you can get it in a filehandle |
| 246 | you specify, but you must be extremely careful; if the error output is not |
| 247 | very short and you want to read it in the same process as where you called |
| 248 | C<command()>, you are set up for a nice deadlock! |
| 249 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 250 | The method can be called without any instance or on a specified Git repository |
| 251 | (in that case the command will be run in the repository context). |
| 252 | |
| 253 | In scalar context, it returns all the command output in a single string |
| 254 | (verbatim). |
| 255 | |
| 256 | In array context, it returns an array containing lines printed to the |
| 257 | command's stdout (without trailing newlines). |
| 258 | |
| 259 | In both cases, the command's stdin and stderr are the same as the caller's. |
| 260 | |
| 261 | =cut |
| 262 | |
| 263 | sub command { |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 264 | my ($fh, $ctx) = command_output_pipe(@_); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 265 | |
| 266 | if (not defined wantarray) { |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 267 | # Nothing to pepper the possible exception with. |
| 268 | _cmd_close($fh, $ctx); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 269 | |
| 270 | } elsif (not wantarray) { |
| 271 | local $/; |
| 272 | my $text = <$fh>; |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 273 | try { |
| 274 | _cmd_close($fh, $ctx); |
| 275 | } catch Git::Error::Command with { |
| 276 | # Pepper with the output: |
| 277 | my $E = shift; |
| 278 | $E->{'-outputref'} = \$text; |
| 279 | throw $E; |
| 280 | }; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 281 | return $text; |
| 282 | |
| 283 | } else { |
| 284 | my @lines = <$fh>; |
Alex Riesen | 67e4baf | 2007-01-22 15:58:03 +0100 | [diff] [blame] | 285 | defined and chomp for @lines; |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 286 | try { |
| 287 | _cmd_close($fh, $ctx); |
| 288 | } catch Git::Error::Command with { |
| 289 | my $E = shift; |
| 290 | $E->{'-outputref'} = \@lines; |
| 291 | throw $E; |
| 292 | }; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 293 | return @lines; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | =item command_oneline ( COMMAND [, ARGUMENTS... ] ) |
| 299 | |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 300 | =item command_oneline ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) |
| 301 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 302 | Execute the given C<COMMAND> in the same way as command() |
| 303 | does but always return a scalar string containing the first line |
| 304 | of the command's standard output. |
| 305 | |
| 306 | =cut |
| 307 | |
| 308 | sub command_oneline { |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 309 | my ($fh, $ctx) = command_output_pipe(@_); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 310 | |
| 311 | my $line = <$fh>; |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 312 | defined $line and chomp $line; |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 313 | try { |
| 314 | _cmd_close($fh, $ctx); |
| 315 | } catch Git::Error::Command with { |
| 316 | # Pepper with the output: |
| 317 | my $E = shift; |
| 318 | $E->{'-outputref'} = \$line; |
| 319 | throw $E; |
| 320 | }; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 321 | return $line; |
| 322 | } |
| 323 | |
| 324 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 325 | =item command_output_pipe ( COMMAND [, ARGUMENTS... ] ) |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 326 | |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 327 | =item command_output_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) |
| 328 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 329 | Execute the given C<COMMAND> in the same way as command() |
| 330 | does but return a pipe filehandle from which the command output can be |
| 331 | read. |
| 332 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 333 | The function can return C<($pipe, $ctx)> in array context. |
| 334 | See C<command_close_pipe()> for details. |
| 335 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 336 | =cut |
| 337 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 338 | sub command_output_pipe { |
| 339 | _command_common_pipe('-|', @_); |
| 340 | } |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 341 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 342 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 343 | =item command_input_pipe ( COMMAND [, ARGUMENTS... ] ) |
| 344 | |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 345 | =item command_input_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) |
| 346 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 347 | Execute the given C<COMMAND> in the same way as command_output_pipe() |
| 348 | does but return an input pipe filehandle instead; the command output |
| 349 | is not captured. |
| 350 | |
| 351 | The function can return C<($pipe, $ctx)> in array context. |
| 352 | See C<command_close_pipe()> for details. |
| 353 | |
| 354 | =cut |
| 355 | |
| 356 | sub command_input_pipe { |
| 357 | _command_common_pipe('|-', @_); |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | |
| 361 | =item command_close_pipe ( PIPE [, CTX ] ) |
| 362 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 363 | Close the C<PIPE> as returned from C<command_*_pipe()>, checking |
Pavel Roskin | 3dff537 | 2007-02-03 23:49:16 -0500 | [diff] [blame] | 364 | whether the command finished successfully. The optional C<CTX> argument |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 365 | is required if you want to see the command name in the error message, |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 366 | and it is the second value returned by C<command_*_pipe()> when |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 367 | called in array context. The call idiom is: |
| 368 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 369 | my ($fh, $ctx) = $r->command_output_pipe('status'); |
| 370 | while (<$fh>) { ... } |
| 371 | $r->command_close_pipe($fh, $ctx); |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 372 | |
| 373 | Note that you should not rely on whatever actually is in C<CTX>; |
| 374 | currently it is simply the command name but in future the context might |
| 375 | have more complicated structure. |
| 376 | |
| 377 | =cut |
| 378 | |
| 379 | sub command_close_pipe { |
| 380 | my ($self, $fh, $ctx) = _maybe_self(@_); |
| 381 | $ctx ||= '<unknown>'; |
| 382 | _cmd_close($fh, $ctx); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Adam Roben | d1a29af | 2008-05-23 16:19:39 +0200 | [diff] [blame] | 385 | =item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] ) |
| 386 | |
| 387 | Execute the given C<COMMAND> in the same way as command_output_pipe() |
| 388 | does but return both an input pipe filehandle and an output pipe filehandle. |
| 389 | |
| 390 | The function will return return C<($pid, $pipe_in, $pipe_out, $ctx)>. |
| 391 | See C<command_close_bidi_pipe()> for details. |
| 392 | |
| 393 | =cut |
| 394 | |
| 395 | sub command_bidi_pipe { |
| 396 | my ($pid, $in, $out); |
| 397 | $pid = open2($in, $out, 'git', @_); |
| 398 | return ($pid, $in, $out, join(' ', @_)); |
| 399 | } |
| 400 | |
| 401 | =item command_close_bidi_pipe ( PID, PIPE_IN, PIPE_OUT [, CTX] ) |
| 402 | |
| 403 | Close the C<PIPE_IN> and C<PIPE_OUT> as returned from C<command_bidi_pipe()>, |
| 404 | checking whether the command finished successfully. The optional C<CTX> |
| 405 | argument is required if you want to see the command name in the error message, |
| 406 | and it is the fourth value returned by C<command_bidi_pipe()>. The call idiom |
| 407 | is: |
| 408 | |
| 409 | my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --batch-check'); |
| 410 | print "000000000\n" $out; |
| 411 | while (<$in>) { ... } |
| 412 | $r->command_close_bidi_pipe($pid, $in, $out, $ctx); |
| 413 | |
| 414 | Note that you should not rely on whatever actually is in C<CTX>; |
| 415 | currently it is simply the command name but in future the context might |
| 416 | have more complicated structure. |
| 417 | |
| 418 | =cut |
| 419 | |
| 420 | sub command_close_bidi_pipe { |
Abhijit Menon-Sen | 108c2aa | 2008-08-04 17:08:27 +0530 | [diff] [blame] | 421 | local $?; |
Adam Roben | d1a29af | 2008-05-23 16:19:39 +0200 | [diff] [blame] | 422 | my ($pid, $in, $out, $ctx) = @_; |
| 423 | foreach my $fh ($in, $out) { |
| 424 | unless (close $fh) { |
| 425 | if ($!) { |
| 426 | carp "error closing pipe: $!"; |
| 427 | } elsif ($? >> 8) { |
| 428 | throw Git::Error::Command($ctx, $? >>8); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | waitpid $pid, 0; |
| 434 | |
| 435 | if ($? >> 8) { |
| 436 | throw Git::Error::Command($ctx, $? >>8); |
| 437 | } |
| 438 | } |
| 439 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 440 | |
| 441 | =item command_noisy ( COMMAND [, ARGUMENTS... ] ) |
| 442 | |
| 443 | Execute the given C<COMMAND> in the same way as command() does but do not |
| 444 | capture the command output - the standard output is not redirected and goes |
| 445 | to the standard output of the caller application. |
| 446 | |
| 447 | While the method is called command_noisy(), you might want to as well use |
| 448 | it for the most silent Git commands which you know will never pollute your |
| 449 | stdout but you want to avoid the overhead of the pipe setup when calling them. |
| 450 | |
| 451 | The function returns only after the command has finished running. |
| 452 | |
| 453 | =cut |
| 454 | |
| 455 | sub command_noisy { |
| 456 | my ($self, $cmd, @args) = _maybe_self(@_); |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 457 | _check_valid_cmd($cmd); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 458 | |
| 459 | my $pid = fork; |
| 460 | if (not defined $pid) { |
Petr Baudis | 97b16c0 | 2006-06-24 04:34:42 +0200 | [diff] [blame] | 461 | throw Error::Simple("fork failed: $!"); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 462 | } elsif ($pid == 0) { |
| 463 | _cmd_exec($self, $cmd, @args); |
| 464 | } |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 465 | if (waitpid($pid, 0) > 0 and $?>>8 != 0) { |
| 466 | throw Git::Error::Command(join(' ', $cmd, @args), $? >> 8); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | |
Petr Baudis | 63df97a | 2006-06-24 04:34:36 +0200 | [diff] [blame] | 471 | =item version () |
| 472 | |
| 473 | Return the Git version in use. |
| 474 | |
Petr Baudis | 63df97a | 2006-06-24 04:34:36 +0200 | [diff] [blame] | 475 | =cut |
| 476 | |
Petr Baudis | 18b0fc1 | 2006-09-23 20:20:47 +0200 | [diff] [blame] | 477 | sub version { |
| 478 | my $verstr = command_oneline('--version'); |
| 479 | $verstr =~ s/^git version //; |
| 480 | $verstr; |
| 481 | } |
Petr Baudis | 63df97a | 2006-06-24 04:34:36 +0200 | [diff] [blame] | 482 | |
| 483 | |
Petr Baudis | eca1f6f | 2006-06-24 04:34:31 +0200 | [diff] [blame] | 484 | =item exec_path () |
| 485 | |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 486 | Return path to the Git sub-command executables (the same as |
Petr Baudis | eca1f6f | 2006-06-24 04:34:31 +0200 | [diff] [blame] | 487 | C<git --exec-path>). Useful mostly only internally. |
| 488 | |
Petr Baudis | eca1f6f | 2006-06-24 04:34:31 +0200 | [diff] [blame] | 489 | =cut |
| 490 | |
Petr Baudis | 18b0fc1 | 2006-09-23 20:20:47 +0200 | [diff] [blame] | 491 | sub exec_path { command_oneline('--exec-path') } |
Petr Baudis | eca1f6f | 2006-06-24 04:34:31 +0200 | [diff] [blame] | 492 | |
| 493 | |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 494 | =item repo_path () |
| 495 | |
| 496 | Return path to the git repository. Must be called on a repository instance. |
| 497 | |
| 498 | =cut |
| 499 | |
| 500 | sub repo_path { $_[0]->{opts}->{Repository} } |
| 501 | |
| 502 | |
| 503 | =item wc_path () |
| 504 | |
| 505 | Return path to the working copy. Must be called on a repository instance. |
| 506 | |
| 507 | =cut |
| 508 | |
| 509 | sub wc_path { $_[0]->{opts}->{WorkingCopy} } |
| 510 | |
| 511 | |
| 512 | =item wc_subdir () |
| 513 | |
| 514 | Return path to the subdirectory inside of a working copy. Must be called |
| 515 | on a repository instance. |
| 516 | |
| 517 | =cut |
| 518 | |
| 519 | sub wc_subdir { $_[0]->{opts}->{WorkingSubdir} ||= '' } |
| 520 | |
| 521 | |
| 522 | =item wc_chdir ( SUBDIR ) |
| 523 | |
| 524 | Change the working copy subdirectory to work within. The C<SUBDIR> is |
| 525 | relative to the working copy root directory (not the current subdirectory). |
| 526 | Must be called on a repository instance attached to a working copy |
| 527 | and the directory must exist. |
| 528 | |
| 529 | =cut |
| 530 | |
| 531 | sub wc_chdir { |
| 532 | my ($self, $subdir) = @_; |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 533 | $self->wc_path() |
| 534 | or throw Error::Simple("bare repository"); |
| 535 | |
| 536 | -d $self->wc_path().'/'.$subdir |
| 537 | or throw Error::Simple("subdir not found: $!"); |
| 538 | # Of course we will not "hold" the subdirectory so anyone |
| 539 | # can delete it now and we will never know. But at least we tried. |
| 540 | |
| 541 | $self->{opts}->{WorkingSubdir} = $subdir; |
| 542 | } |
| 543 | |
| 544 | |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 545 | =item config ( VARIABLE ) |
| 546 | |
Tom Prince | e0d10e1 | 2007-01-28 16:16:53 -0800 | [diff] [blame] | 547 | Retrieve the configuration C<VARIABLE> in the same manner as C<config> |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 548 | does. In scalar context requires the variable to be set only one time |
| 549 | (exception is thrown otherwise), in array context returns allows the |
| 550 | variable to be set multiple times and returns all the values. |
| 551 | |
Tom Prince | e0d10e1 | 2007-01-28 16:16:53 -0800 | [diff] [blame] | 552 | This currently wraps command('config') so it is not so fast. |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 553 | |
| 554 | =cut |
| 555 | |
| 556 | sub config { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 557 | my ($self, $var) = _maybe_self(@_); |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 558 | |
| 559 | try { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 560 | my @cmd = ('config'); |
| 561 | unshift @cmd, $self if $self; |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 562 | if (wantarray) { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 563 | return command(@cmd, '--get-all', $var); |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 564 | } else { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 565 | return command_oneline(@cmd, '--get', $var); |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 566 | } |
| 567 | } catch Git::Error::Command with { |
| 568 | my $E = shift; |
| 569 | if ($E->value() == 1) { |
| 570 | # Key not found. |
Lea Wiemann | 32d8050 | 2008-06-01 22:34:47 +0200 | [diff] [blame] | 571 | return; |
Petr Baudis | dc2613d | 2006-07-03 22:47:55 +0200 | [diff] [blame] | 572 | } else { |
| 573 | throw $E; |
| 574 | } |
| 575 | }; |
| 576 | } |
| 577 | |
| 578 | |
Petr Baudis | 35c49ee | 2007-05-09 12:49:41 +0200 | [diff] [blame] | 579 | =item config_bool ( VARIABLE ) |
Theodore Ts'o | 7b9a13e | 2007-02-20 15:13:42 -0500 | [diff] [blame] | 580 | |
Petr Baudis | 35c49ee | 2007-05-09 12:49:41 +0200 | [diff] [blame] | 581 | Retrieve the bool configuration C<VARIABLE>. The return value |
| 582 | is usable as a boolean in perl (and C<undef> if it's not defined, |
| 583 | of course). |
Theodore Ts'o | 7b9a13e | 2007-02-20 15:13:42 -0500 | [diff] [blame] | 584 | |
Theodore Ts'o | 7b9a13e | 2007-02-20 15:13:42 -0500 | [diff] [blame] | 585 | This currently wraps command('config') so it is not so fast. |
| 586 | |
| 587 | =cut |
| 588 | |
Petr Baudis | 35c49ee | 2007-05-09 12:49:41 +0200 | [diff] [blame] | 589 | sub config_bool { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 590 | my ($self, $var) = _maybe_self(@_); |
Theodore Ts'o | 7b9a13e | 2007-02-20 15:13:42 -0500 | [diff] [blame] | 591 | |
| 592 | try { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 593 | my @cmd = ('config', '--bool', '--get', $var); |
| 594 | unshift @cmd, $self if $self; |
| 595 | my $val = command_oneline(@cmd); |
Petr Baudis | 35c49ee | 2007-05-09 12:49:41 +0200 | [diff] [blame] | 596 | return undef unless defined $val; |
| 597 | return $val eq 'true'; |
Theodore Ts'o | 7b9a13e | 2007-02-20 15:13:42 -0500 | [diff] [blame] | 598 | } catch Git::Error::Command with { |
| 599 | my $E = shift; |
| 600 | if ($E->value() == 1) { |
| 601 | # Key not found. |
| 602 | return undef; |
| 603 | } else { |
| 604 | throw $E; |
| 605 | } |
| 606 | }; |
| 607 | } |
| 608 | |
Jakub Narebski | 346d203 | 2007-11-23 19:04:52 +0100 | [diff] [blame] | 609 | =item config_int ( VARIABLE ) |
| 610 | |
| 611 | Retrieve the integer configuration C<VARIABLE>. The return value |
| 612 | is simple decimal number. An optional value suffix of 'k', 'm', |
| 613 | or 'g' in the config file will cause the value to be multiplied |
| 614 | by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output. |
| 615 | It would return C<undef> if configuration variable is not defined, |
| 616 | |
Jakub Narebski | 346d203 | 2007-11-23 19:04:52 +0100 | [diff] [blame] | 617 | This currently wraps command('config') so it is not so fast. |
| 618 | |
| 619 | =cut |
| 620 | |
| 621 | sub config_int { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 622 | my ($self, $var) = _maybe_self(@_); |
Jakub Narebski | 346d203 | 2007-11-23 19:04:52 +0100 | [diff] [blame] | 623 | |
| 624 | try { |
Frank Lichtenheld | c2e357c | 2008-03-14 18:29:28 +0100 | [diff] [blame] | 625 | my @cmd = ('config', '--int', '--get', $var); |
| 626 | unshift @cmd, $self if $self; |
| 627 | return command_oneline(@cmd); |
Jakub Narebski | 346d203 | 2007-11-23 19:04:52 +0100 | [diff] [blame] | 628 | } catch Git::Error::Command with { |
| 629 | my $E = shift; |
| 630 | if ($E->value() == 1) { |
| 631 | # Key not found. |
| 632 | return undef; |
| 633 | } else { |
| 634 | throw $E; |
| 635 | } |
| 636 | }; |
| 637 | } |
Theodore Ts'o | 7b9a13e | 2007-02-20 15:13:42 -0500 | [diff] [blame] | 638 | |
Junio C Hamano | b4c61ed | 2007-12-05 00:50:23 -0800 | [diff] [blame] | 639 | =item get_colorbool ( NAME ) |
| 640 | |
| 641 | Finds if color should be used for NAMEd operation from the configuration, |
| 642 | and returns boolean (true for "use color", false for "do not use color"). |
| 643 | |
| 644 | =cut |
| 645 | |
| 646 | sub get_colorbool { |
| 647 | my ($self, $var) = @_; |
| 648 | my $stdout_to_tty = (-t STDOUT) ? "true" : "false"; |
| 649 | my $use_color = $self->command_oneline('config', '--get-colorbool', |
| 650 | $var, $stdout_to_tty); |
| 651 | return ($use_color eq 'true'); |
| 652 | } |
| 653 | |
| 654 | =item get_color ( SLOT, COLOR ) |
| 655 | |
| 656 | Finds color for SLOT from the configuration, while defaulting to COLOR, |
| 657 | and returns the ANSI color escape sequence: |
| 658 | |
| 659 | print $repo->get_color("color.interactive.prompt", "underline blue white"); |
| 660 | print "some text"; |
| 661 | print $repo->get_color("", "normal"); |
| 662 | |
| 663 | =cut |
| 664 | |
| 665 | sub get_color { |
| 666 | my ($self, $slot, $default) = @_; |
| 667 | my $color = $self->command_oneline('config', '--get-color', $slot, $default); |
| 668 | if (!defined $color) { |
| 669 | $color = ""; |
| 670 | } |
| 671 | return $color; |
| 672 | } |
| 673 | |
Petr Baudis | 31a92f6 | 2008-07-08 19:48:04 +0200 | [diff] [blame] | 674 | =item remote_refs ( REPOSITORY [, GROUPS [, REFGLOBS ] ] ) |
| 675 | |
| 676 | This function returns a hashref of refs stored in a given remote repository. |
| 677 | The hash is in the format C<refname =\> hash>. For tags, the C<refname> entry |
| 678 | contains the tag object while a C<refname^{}> entry gives the tagged objects. |
| 679 | |
| 680 | C<REPOSITORY> has the same meaning as the appropriate C<git-ls-remote> |
| 681 | argument; either an URL or a remote name (if called on a repository instance). |
| 682 | C<GROUPS> is an optional arrayref that can contain 'tags' to return all the |
| 683 | tags and/or 'heads' to return all the heads. C<REFGLOB> is an optional array |
| 684 | of strings containing a shell-like glob to further limit the refs returned in |
| 685 | the hash; the meaning is again the same as the appropriate C<git-ls-remote> |
| 686 | argument. |
| 687 | |
| 688 | This function may or may not be called on a repository instance. In the former |
| 689 | case, remote names as defined in the repository are recognized as repository |
| 690 | specifiers. |
| 691 | |
| 692 | =cut |
| 693 | |
| 694 | sub remote_refs { |
| 695 | my ($self, $repo, $groups, $refglobs) = _maybe_self(@_); |
| 696 | my @args; |
| 697 | if (ref $groups eq 'ARRAY') { |
| 698 | foreach (@$groups) { |
| 699 | if ($_ eq 'heads') { |
| 700 | push (@args, '--heads'); |
| 701 | } elsif ($_ eq 'tags') { |
| 702 | push (@args, '--tags'); |
| 703 | } else { |
| 704 | # Ignore unknown groups for future |
| 705 | # compatibility |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | push (@args, $repo); |
| 710 | if (ref $refglobs eq 'ARRAY') { |
| 711 | push (@args, @$refglobs); |
| 712 | } |
| 713 | |
| 714 | my @self = $self ? ($self) : (); # Ultra trickery |
| 715 | my ($fh, $ctx) = Git::command_output_pipe(@self, 'ls-remote', @args); |
| 716 | my %refs; |
| 717 | while (<$fh>) { |
| 718 | chomp; |
| 719 | my ($hash, $ref) = split(/\t/, $_, 2); |
| 720 | $refs{$ref} = $hash; |
| 721 | } |
| 722 | Git::command_close_pipe(@self, $fh, $ctx); |
| 723 | return \%refs; |
| 724 | } |
| 725 | |
| 726 | |
Petr Baudis | c7a30e5 | 2006-07-03 22:48:01 +0200 | [diff] [blame] | 727 | =item ident ( TYPE | IDENTSTR ) |
| 728 | |
| 729 | =item ident_person ( TYPE | IDENTSTR | IDENTARRAY ) |
| 730 | |
| 731 | This suite of functions retrieves and parses ident information, as stored |
| 732 | in the commit and tag objects or produced by C<var GIT_type_IDENT> (thus |
| 733 | C<TYPE> can be either I<author> or I<committer>; case is insignificant). |
| 734 | |
Todd Zullinger | 5354a56 | 2008-07-30 13:48:33 -0400 | [diff] [blame] | 735 | The C<ident> method retrieves the ident information from C<git var> |
Petr Baudis | c7a30e5 | 2006-07-03 22:48:01 +0200 | [diff] [blame] | 736 | and either returns it as a scalar string or as an array with the fields parsed. |
| 737 | Alternatively, it can take a prepared ident string (e.g. from the commit |
| 738 | object) and just parse it. |
| 739 | |
| 740 | C<ident_person> returns the person part of the ident - name and email; |
| 741 | it can take the same arguments as C<ident> or the array returned by C<ident>. |
| 742 | |
| 743 | The synopsis is like: |
| 744 | |
| 745 | my ($name, $email, $time_tz) = ident('author'); |
| 746 | "$name <$email>" eq ident_person('author'); |
| 747 | "$name <$email>" eq ident_person($name); |
| 748 | $time_tz =~ /^\d+ [+-]\d{4}$/; |
| 749 | |
Petr Baudis | c7a30e5 | 2006-07-03 22:48:01 +0200 | [diff] [blame] | 750 | =cut |
| 751 | |
| 752 | sub ident { |
Frank Lichtenheld | 4461792 | 2008-03-14 18:29:29 +0100 | [diff] [blame] | 753 | my ($self, $type) = _maybe_self(@_); |
Petr Baudis | c7a30e5 | 2006-07-03 22:48:01 +0200 | [diff] [blame] | 754 | my $identstr; |
| 755 | if (lc $type eq lc 'committer' or lc $type eq lc 'author') { |
Frank Lichtenheld | 4461792 | 2008-03-14 18:29:29 +0100 | [diff] [blame] | 756 | my @cmd = ('var', 'GIT_'.uc($type).'_IDENT'); |
| 757 | unshift @cmd, $self if $self; |
| 758 | $identstr = command_oneline(@cmd); |
Petr Baudis | c7a30e5 | 2006-07-03 22:48:01 +0200 | [diff] [blame] | 759 | } else { |
| 760 | $identstr = $type; |
| 761 | } |
| 762 | if (wantarray) { |
| 763 | return $identstr =~ /^(.*) <(.*)> (\d+ [+-]\d{4})$/; |
| 764 | } else { |
| 765 | return $identstr; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | sub ident_person { |
Frank Lichtenheld | 4461792 | 2008-03-14 18:29:29 +0100 | [diff] [blame] | 770 | my ($self, @ident) = _maybe_self(@_); |
| 771 | $#ident == 0 and @ident = $self ? $self->ident($ident[0]) : ident($ident[0]); |
Petr Baudis | c7a30e5 | 2006-07-03 22:48:01 +0200 | [diff] [blame] | 772 | return "$ident[0] <$ident[1]>"; |
| 773 | } |
| 774 | |
| 775 | |
Petr Baudis | 24c4b71 | 2006-06-25 03:54:26 +0200 | [diff] [blame] | 776 | =item hash_object ( TYPE, FILENAME ) |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 777 | |
Lea Wiemann | 58c8dd2 | 2008-06-01 22:26:25 +0200 | [diff] [blame] | 778 | Compute the SHA1 object id of the given C<FILENAME> considering it is |
| 779 | of the C<TYPE> object type (C<blob>, C<commit>, C<tree>). |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 780 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 781 | The method can be called without any instance or on a specified Git repository, |
| 782 | it makes zero difference. |
| 783 | |
| 784 | The function returns the SHA1 hash. |
| 785 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 786 | =cut |
| 787 | |
Petr Baudis | 18b0fc1 | 2006-09-23 20:20:47 +0200 | [diff] [blame] | 788 | # TODO: Support for passing FILEHANDLE instead of FILENAME |
Petr Baudis | e6634ac | 2006-07-02 01:38:56 +0200 | [diff] [blame] | 789 | sub hash_object { |
| 790 | my ($self, $type, $file) = _maybe_self(@_); |
Petr Baudis | 18b0fc1 | 2006-09-23 20:20:47 +0200 | [diff] [blame] | 791 | command_oneline('hash-object', '-t', $type, $file); |
Petr Baudis | e6634ac | 2006-07-02 01:38:56 +0200 | [diff] [blame] | 792 | } |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 793 | |
| 794 | |
Adam Roben | 7182530 | 2008-05-23 16:19:40 +0200 | [diff] [blame] | 795 | =item hash_and_insert_object ( FILENAME ) |
| 796 | |
| 797 | Compute the SHA1 object id of the given C<FILENAME> and add the object to the |
| 798 | object database. |
| 799 | |
| 800 | The function returns the SHA1 hash. |
| 801 | |
| 802 | =cut |
| 803 | |
| 804 | # TODO: Support for passing FILEHANDLE instead of FILENAME |
| 805 | sub hash_and_insert_object { |
| 806 | my ($self, $filename) = @_; |
| 807 | |
| 808 | carp "Bad filename \"$filename\"" if $filename =~ /[\r\n]/; |
| 809 | |
| 810 | $self->_open_hash_and_insert_object_if_needed(); |
| 811 | my ($in, $out) = ($self->{hash_object_in}, $self->{hash_object_out}); |
| 812 | |
| 813 | unless (print $out $filename, "\n") { |
| 814 | $self->_close_hash_and_insert_object(); |
| 815 | throw Error::Simple("out pipe went bad"); |
| 816 | } |
| 817 | |
| 818 | chomp(my $hash = <$in>); |
| 819 | unless (defined($hash)) { |
| 820 | $self->_close_hash_and_insert_object(); |
| 821 | throw Error::Simple("in pipe went bad"); |
| 822 | } |
| 823 | |
| 824 | return $hash; |
| 825 | } |
| 826 | |
| 827 | sub _open_hash_and_insert_object_if_needed { |
| 828 | my ($self) = @_; |
| 829 | |
| 830 | return if defined($self->{hash_object_pid}); |
| 831 | |
| 832 | ($self->{hash_object_pid}, $self->{hash_object_in}, |
| 833 | $self->{hash_object_out}, $self->{hash_object_ctx}) = |
| 834 | command_bidi_pipe(qw(hash-object -w --stdin-paths)); |
| 835 | } |
| 836 | |
| 837 | sub _close_hash_and_insert_object { |
| 838 | my ($self) = @_; |
| 839 | |
| 840 | return unless defined($self->{hash_object_pid}); |
| 841 | |
| 842 | my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx); |
| 843 | |
Abhijit Menon-Sen | 452d36b | 2008-08-04 10:32:47 +0530 | [diff] [blame] | 844 | command_close_bidi_pipe(@$self{@vars}); |
| 845 | delete @$self{@vars}; |
Adam Roben | 7182530 | 2008-05-23 16:19:40 +0200 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | =item cat_blob ( SHA1, FILEHANDLE ) |
| 849 | |
| 850 | Prints the contents of the blob identified by C<SHA1> to C<FILEHANDLE> and |
| 851 | returns the number of bytes printed. |
| 852 | |
| 853 | =cut |
| 854 | |
| 855 | sub cat_blob { |
| 856 | my ($self, $sha1, $fh) = @_; |
| 857 | |
| 858 | $self->_open_cat_blob_if_needed(); |
| 859 | my ($in, $out) = ($self->{cat_blob_in}, $self->{cat_blob_out}); |
| 860 | |
| 861 | unless (print $out $sha1, "\n") { |
| 862 | $self->_close_cat_blob(); |
| 863 | throw Error::Simple("out pipe went bad"); |
| 864 | } |
| 865 | |
| 866 | my $description = <$in>; |
| 867 | if ($description =~ / missing$/) { |
| 868 | carp "$sha1 doesn't exist in the repository"; |
Junio C Hamano | d683a0e | 2008-05-27 23:33:22 -0700 | [diff] [blame] | 869 | return -1; |
Adam Roben | 7182530 | 2008-05-23 16:19:40 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | if ($description !~ /^[0-9a-fA-F]{40} \S+ (\d+)$/) { |
| 873 | carp "Unexpected result returned from git cat-file"; |
Junio C Hamano | d683a0e | 2008-05-27 23:33:22 -0700 | [diff] [blame] | 874 | return -1; |
Adam Roben | 7182530 | 2008-05-23 16:19:40 +0200 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | my $size = $1; |
| 878 | |
| 879 | my $blob; |
| 880 | my $bytesRead = 0; |
| 881 | |
| 882 | while (1) { |
| 883 | my $bytesLeft = $size - $bytesRead; |
| 884 | last unless $bytesLeft; |
| 885 | |
| 886 | my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024; |
| 887 | my $read = read($in, $blob, $bytesToRead, $bytesRead); |
| 888 | unless (defined($read)) { |
| 889 | $self->_close_cat_blob(); |
| 890 | throw Error::Simple("in pipe went bad"); |
| 891 | } |
| 892 | |
| 893 | $bytesRead += $read; |
| 894 | } |
| 895 | |
| 896 | # Skip past the trailing newline. |
| 897 | my $newline; |
| 898 | my $read = read($in, $newline, 1); |
| 899 | unless (defined($read)) { |
| 900 | $self->_close_cat_blob(); |
| 901 | throw Error::Simple("in pipe went bad"); |
| 902 | } |
| 903 | unless ($read == 1 && $newline eq "\n") { |
| 904 | $self->_close_cat_blob(); |
| 905 | throw Error::Simple("didn't find newline after blob"); |
| 906 | } |
| 907 | |
| 908 | unless (print $fh $blob) { |
| 909 | $self->_close_cat_blob(); |
| 910 | throw Error::Simple("couldn't write to passed in filehandle"); |
| 911 | } |
| 912 | |
| 913 | return $size; |
| 914 | } |
| 915 | |
| 916 | sub _open_cat_blob_if_needed { |
| 917 | my ($self) = @_; |
| 918 | |
| 919 | return if defined($self->{cat_blob_pid}); |
| 920 | |
| 921 | ($self->{cat_blob_pid}, $self->{cat_blob_in}, |
| 922 | $self->{cat_blob_out}, $self->{cat_blob_ctx}) = |
| 923 | command_bidi_pipe(qw(cat-file --batch)); |
| 924 | } |
| 925 | |
| 926 | sub _close_cat_blob { |
| 927 | my ($self) = @_; |
| 928 | |
| 929 | return unless defined($self->{cat_blob_pid}); |
| 930 | |
| 931 | my @vars = map { 'cat_blob_' . $_ } qw(pid in out ctx); |
| 932 | |
Abhijit Menon-Sen | 452d36b | 2008-08-04 10:32:47 +0530 | [diff] [blame] | 933 | command_close_bidi_pipe(@$self{@vars}); |
| 934 | delete @$self{@vars}; |
Adam Roben | 7182530 | 2008-05-23 16:19:40 +0200 | [diff] [blame] | 935 | } |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 936 | |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 937 | |
| 938 | { # %TEMP_* Lexical Context |
| 939 | |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 940 | my (%TEMP_FILEMAP, %TEMP_FILES); |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 941 | |
| 942 | =item temp_acquire ( NAME ) |
| 943 | |
| 944 | Attempts to retreive the temporary file mapped to the string C<NAME>. If an |
| 945 | associated temp file has not been created this session or was closed, it is |
| 946 | created, cached, and set for autoflush and binmode. |
| 947 | |
| 948 | Internally locks the file mapped to C<NAME>. This lock must be released with |
| 949 | C<temp_release()> when the temp file is no longer needed. Subsequent attempts |
| 950 | to retrieve temporary files mapped to the same C<NAME> while still locked will |
| 951 | cause an error. This locking mechanism provides a weak guarantee and is not |
| 952 | threadsafe. It does provide some error checking to help prevent temp file refs |
| 953 | writing over one another. |
| 954 | |
| 955 | In general, the L<File::Handle> returned should not be closed by consumers as |
| 956 | it defeats the purpose of this caching mechanism. If you need to close the temp |
| 957 | file handle, then you should use L<File::Temp> or another temp file faculty |
| 958 | directly. If a handle is closed and then requested again, then a warning will |
| 959 | issue. |
| 960 | |
| 961 | =cut |
| 962 | |
| 963 | sub temp_acquire { |
Marten Svanfeldt (dev) | bcdd1b4 | 2008-11-13 20:04:09 +0800 | [diff] [blame] | 964 | my $temp_fd = _temp_cache(@_); |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 965 | |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 966 | $TEMP_FILES{$temp_fd}{locked} = 1; |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 967 | $temp_fd; |
| 968 | } |
| 969 | |
| 970 | =item temp_release ( NAME ) |
| 971 | |
| 972 | =item temp_release ( FILEHANDLE ) |
| 973 | |
| 974 | Releases a lock acquired through C<temp_acquire()>. Can be called either with |
| 975 | the C<NAME> mapping used when acquiring the temp file or with the C<FILEHANDLE> |
| 976 | referencing a locked temp file. |
| 977 | |
| 978 | Warns if an attempt is made to release a file that is not locked. |
| 979 | |
| 980 | The temp file will be truncated before being released. This can help to reduce |
| 981 | disk I/O where the system is smart enough to detect the truncation while data |
| 982 | is in the output buffers. Beware that after the temp file is released and |
| 983 | truncated, any operations on that file may fail miserably until it is |
| 984 | re-acquired. All contents are lost between each release and acquire mapped to |
| 985 | the same string. |
| 986 | |
| 987 | =cut |
| 988 | |
| 989 | sub temp_release { |
| 990 | my ($self, $temp_fd, $trunc) = _maybe_self(@_); |
| 991 | |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 992 | if (exists $TEMP_FILEMAP{$temp_fd}) { |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 993 | $temp_fd = $TEMP_FILES{$temp_fd}; |
| 994 | } |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 995 | unless ($TEMP_FILES{$temp_fd}{locked}) { |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 996 | carp "Attempt to release temp file '", |
| 997 | $temp_fd, "' that has not been locked"; |
| 998 | } |
| 999 | temp_reset($temp_fd) if $trunc and $temp_fd->opened; |
| 1000 | |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1001 | $TEMP_FILES{$temp_fd}{locked} = 0; |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1002 | undef; |
| 1003 | } |
| 1004 | |
| 1005 | sub _temp_cache { |
Marten Svanfeldt (dev) | bcdd1b4 | 2008-11-13 20:04:09 +0800 | [diff] [blame] | 1006 | my ($self, $name) = _maybe_self(@_); |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1007 | |
Marcus Griep | c14c8ce | 2008-08-15 15:53:59 -0400 | [diff] [blame] | 1008 | _verify_require(); |
| 1009 | |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1010 | my $temp_fd = \$TEMP_FILEMAP{$name}; |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1011 | if (defined $$temp_fd and $$temp_fd->opened) { |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1012 | if ($TEMP_FILES{$$temp_fd}{locked}) { |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1013 | throw Error::Simple("Temp file with moniker '", |
| 1014 | $name, "' already in use"); |
| 1015 | } |
| 1016 | } else { |
| 1017 | if (defined $$temp_fd) { |
| 1018 | # then we're here because of a closed handle. |
| 1019 | carp "Temp file '", $name, |
| 1020 | "' was closed. Opening replacement."; |
| 1021 | } |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1022 | my $fname; |
Marten Svanfeldt (dev) | bcdd1b4 | 2008-11-13 20:04:09 +0800 | [diff] [blame] | 1023 | |
| 1024 | my $tmpdir; |
| 1025 | if (defined $self) { |
| 1026 | $tmpdir = $self->repo_path(); |
| 1027 | } |
| 1028 | |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1029 | ($$temp_fd, $fname) = File::Temp->tempfile( |
Marten Svanfeldt (dev) | bcdd1b4 | 2008-11-13 20:04:09 +0800 | [diff] [blame] | 1030 | 'Git_XXXXXX', UNLINK => 1, DIR => $tmpdir, |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1031 | ) or throw Error::Simple("couldn't open new temp file"); |
Marten Svanfeldt (dev) | bcdd1b4 | 2008-11-13 20:04:09 +0800 | [diff] [blame] | 1032 | |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1033 | $$temp_fd->autoflush; |
| 1034 | binmode $$temp_fd; |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1035 | $TEMP_FILES{$$temp_fd}{fname} = $fname; |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1036 | } |
| 1037 | $$temp_fd; |
| 1038 | } |
| 1039 | |
Marcus Griep | c14c8ce | 2008-08-15 15:53:59 -0400 | [diff] [blame] | 1040 | sub _verify_require { |
| 1041 | eval { require File::Temp; require File::Spec; }; |
| 1042 | $@ and throw Error::Simple($@); |
| 1043 | } |
| 1044 | |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1045 | =item temp_reset ( FILEHANDLE ) |
| 1046 | |
| 1047 | Truncates and resets the position of the C<FILEHANDLE>. |
| 1048 | |
| 1049 | =cut |
| 1050 | |
| 1051 | sub temp_reset { |
| 1052 | my ($self, $temp_fd) = _maybe_self(@_); |
| 1053 | |
| 1054 | truncate $temp_fd, 0 |
| 1055 | or throw Error::Simple("couldn't truncate file"); |
| 1056 | sysseek($temp_fd, 0, SEEK_SET) and seek($temp_fd, 0, SEEK_SET) |
| 1057 | or throw Error::Simple("couldn't seek to beginning of file"); |
| 1058 | sysseek($temp_fd, 0, SEEK_CUR) == 0 and tell($temp_fd) == 0 |
| 1059 | or throw Error::Simple("expected file position to be reset"); |
| 1060 | } |
| 1061 | |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1062 | =item temp_path ( NAME ) |
| 1063 | |
| 1064 | =item temp_path ( FILEHANDLE ) |
| 1065 | |
| 1066 | Returns the filename associated with the given tempfile. |
| 1067 | |
| 1068 | =cut |
| 1069 | |
| 1070 | sub temp_path { |
| 1071 | my ($self, $temp_fd) = _maybe_self(@_); |
| 1072 | |
| 1073 | if (exists $TEMP_FILEMAP{$temp_fd}) { |
| 1074 | $temp_fd = $TEMP_FILEMAP{$temp_fd}; |
| 1075 | } |
| 1076 | $TEMP_FILES{$temp_fd}{fname}; |
| 1077 | } |
| 1078 | |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1079 | sub END { |
Marcus Griep | 836ff95 | 2008-09-08 12:53:01 -0400 | [diff] [blame] | 1080 | unlink values %TEMP_FILEMAP if %TEMP_FILEMAP; |
Marcus Griep | e41352b | 2008-08-12 12:00:18 -0400 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | } # %TEMP_* Lexical Context |
| 1084 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1085 | =back |
| 1086 | |
Petr Baudis | 97b16c0 | 2006-06-24 04:34:42 +0200 | [diff] [blame] | 1087 | =head1 ERROR HANDLING |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1088 | |
Petr Baudis | 97b16c0 | 2006-06-24 04:34:42 +0200 | [diff] [blame] | 1089 | All functions are supposed to throw Perl exceptions in case of errors. |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 1090 | See the L<Error> module on how to catch those. Most exceptions are mere |
| 1091 | L<Error::Simple> instances. |
| 1092 | |
| 1093 | However, the C<command()>, C<command_oneline()> and C<command_noisy()> |
| 1094 | functions suite can throw C<Git::Error::Command> exceptions as well: those are |
| 1095 | thrown when the external command returns an error code and contain the error |
| 1096 | code as well as access to the captured command's output. The exception class |
| 1097 | provides the usual C<stringify> and C<value> (command's exit code) methods and |
| 1098 | in addition also a C<cmd_output> method that returns either an array or a |
| 1099 | string with the captured command output (depending on the original function |
| 1100 | call context; C<command_noisy()> returns C<undef>) and $<cmdline> which |
| 1101 | returns the command and its arguments (but without proper quoting). |
| 1102 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 1103 | Note that the C<command_*_pipe()> functions cannot throw this exception since |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 1104 | it has no idea whether the command failed or not. You will only find out |
| 1105 | at the time you C<close> the pipe; if you want to have that automated, |
| 1106 | use C<command_close_pipe()>, which can throw the exception. |
| 1107 | |
| 1108 | =cut |
| 1109 | |
| 1110 | { |
| 1111 | package Git::Error::Command; |
| 1112 | |
| 1113 | @Git::Error::Command::ISA = qw(Error); |
| 1114 | |
| 1115 | sub new { |
| 1116 | my $self = shift; |
| 1117 | my $cmdline = '' . shift; |
| 1118 | my $value = 0 + shift; |
| 1119 | my $outputref = shift; |
| 1120 | my(@args) = (); |
| 1121 | |
| 1122 | local $Error::Depth = $Error::Depth + 1; |
| 1123 | |
| 1124 | push(@args, '-cmdline', $cmdline); |
| 1125 | push(@args, '-value', $value); |
| 1126 | push(@args, '-outputref', $outputref); |
| 1127 | |
| 1128 | $self->SUPER::new(-text => 'command returned error', @args); |
| 1129 | } |
| 1130 | |
| 1131 | sub stringify { |
| 1132 | my $self = shift; |
| 1133 | my $text = $self->SUPER::stringify; |
| 1134 | $self->cmdline() . ': ' . $text . ': ' . $self->value() . "\n"; |
| 1135 | } |
| 1136 | |
| 1137 | sub cmdline { |
| 1138 | my $self = shift; |
| 1139 | $self->{'-cmdline'}; |
| 1140 | } |
| 1141 | |
| 1142 | sub cmd_output { |
| 1143 | my $self = shift; |
| 1144 | my $ref = $self->{'-outputref'}; |
| 1145 | defined $ref or undef; |
| 1146 | if (ref $ref eq 'ARRAY') { |
| 1147 | return @$ref; |
| 1148 | } else { # SCALAR |
| 1149 | return $$ref; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | =over 4 |
| 1155 | |
| 1156 | =item git_cmd_try { CODE } ERRMSG |
| 1157 | |
| 1158 | This magical statement will automatically catch any C<Git::Error::Command> |
| 1159 | exceptions thrown by C<CODE> and make your program die with C<ERRMSG> |
| 1160 | on its lips; the message will have %s substituted for the command line |
| 1161 | and %d for the exit status. This statement is useful mostly for producing |
| 1162 | more user-friendly error messages. |
| 1163 | |
| 1164 | In case of no exception caught the statement returns C<CODE>'s return value. |
| 1165 | |
| 1166 | Note that this is the only auto-exported function. |
| 1167 | |
| 1168 | =cut |
| 1169 | |
| 1170 | sub git_cmd_try(&$) { |
| 1171 | my ($code, $errmsg) = @_; |
| 1172 | my @result; |
| 1173 | my $err; |
| 1174 | my $array = wantarray; |
| 1175 | try { |
| 1176 | if ($array) { |
| 1177 | @result = &$code; |
| 1178 | } else { |
| 1179 | $result[0] = &$code; |
| 1180 | } |
| 1181 | } catch Git::Error::Command with { |
| 1182 | my $E = shift; |
| 1183 | $err = $errmsg; |
| 1184 | $err =~ s/\%s/$E->cmdline()/ge; |
| 1185 | $err =~ s/\%d/$E->value()/ge; |
| 1186 | # We can't croak here since Error.pm would mangle |
| 1187 | # that to Error::Simple. |
| 1188 | }; |
| 1189 | $err and croak $err; |
| 1190 | return $array ? @result : $result[0]; |
| 1191 | } |
| 1192 | |
| 1193 | |
| 1194 | =back |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1195 | |
| 1196 | =head1 COPYRIGHT |
| 1197 | |
| 1198 | Copyright 2006 by Petr Baudis E<lt>pasky@suse.czE<gt>. |
| 1199 | |
| 1200 | This module is free software; it may be used, copied, modified |
| 1201 | and distributed under the terms of the GNU General Public Licence, |
| 1202 | either version 2, or (at your option) any later version. |
| 1203 | |
| 1204 | =cut |
| 1205 | |
| 1206 | |
| 1207 | # Take raw method argument list and return ($obj, @args) in case |
| 1208 | # the method was called upon an instance and (undef, @args) if |
| 1209 | # it was called directly. |
| 1210 | sub _maybe_self { |
Christian Jaeger | d8b24b9 | 2008-10-18 20:25:12 +0200 | [diff] [blame] | 1211 | UNIVERSAL::isa($_[0], 'Git') ? @_ : (undef, @_); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 1214 | # Check if the command id is something reasonable. |
| 1215 | sub _check_valid_cmd { |
| 1216 | my ($cmd) = @_; |
| 1217 | $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd"); |
| 1218 | } |
| 1219 | |
| 1220 | # Common backend for the pipe creators. |
| 1221 | sub _command_common_pipe { |
| 1222 | my $direction = shift; |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 1223 | my ($self, @p) = _maybe_self(@_); |
| 1224 | my (%opts, $cmd, @args); |
| 1225 | if (ref $p[0]) { |
| 1226 | ($cmd, @args) = @{shift @p}; |
| 1227 | %opts = ref $p[0] ? %{$p[0]} : @p; |
| 1228 | } else { |
| 1229 | ($cmd, @args) = @p; |
| 1230 | } |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 1231 | _check_valid_cmd($cmd); |
| 1232 | |
Petr Baudis | a6065b5 | 2006-06-25 03:54:23 +0200 | [diff] [blame] | 1233 | my $fh; |
Alex Riesen | d3b1785 | 2007-01-22 17:14:56 +0100 | [diff] [blame] | 1234 | if ($^O eq 'MSWin32') { |
Petr Baudis | a6065b5 | 2006-06-25 03:54:23 +0200 | [diff] [blame] | 1235 | # ActiveState Perl |
| 1236 | #defined $opts{STDERR} and |
| 1237 | # warn 'ignoring STDERR option - running w/ ActiveState'; |
| 1238 | $direction eq '-|' or |
| 1239 | die 'input pipe for ActiveState not implemented'; |
Alex Riesen | bed118d | 2007-01-22 17:16:05 +0100 | [diff] [blame] | 1240 | # the strange construction with *ACPIPE is just to |
| 1241 | # explain the tie below that we want to bind to |
| 1242 | # a handle class, not scalar. It is not known if |
| 1243 | # it is something specific to ActiveState Perl or |
| 1244 | # just a Perl quirk. |
| 1245 | tie (*ACPIPE, 'Git::activestate_pipe', $cmd, @args); |
| 1246 | $fh = *ACPIPE; |
Petr Baudis | a6065b5 | 2006-06-25 03:54:23 +0200 | [diff] [blame] | 1247 | |
| 1248 | } else { |
| 1249 | my $pid = open($fh, $direction); |
| 1250 | if (not defined $pid) { |
| 1251 | throw Error::Simple("open failed: $!"); |
| 1252 | } elsif ($pid == 0) { |
| 1253 | if (defined $opts{STDERR}) { |
| 1254 | close STDERR; |
| 1255 | } |
| 1256 | if ($opts{STDERR}) { |
| 1257 | open (STDERR, '>&', $opts{STDERR}) |
| 1258 | or die "dup failed: $!"; |
| 1259 | } |
| 1260 | _cmd_exec($self, $cmd, @args); |
Petr Baudis | d43ba46 | 2006-06-24 04:34:49 +0200 | [diff] [blame] | 1261 | } |
Petr Baudis | d79850e | 2006-06-24 04:34:47 +0200 | [diff] [blame] | 1262 | } |
| 1263 | return wantarray ? ($fh, join(' ', $cmd, @args)) : $fh; |
| 1264 | } |
| 1265 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1266 | # When already in the subprocess, set up the appropriate state |
| 1267 | # for the given repository and execute the git command. |
| 1268 | sub _cmd_exec { |
| 1269 | my ($self, @args) = @_; |
| 1270 | if ($self) { |
Petr Baudis | d5c7721 | 2006-06-24 04:34:51 +0200 | [diff] [blame] | 1271 | $self->repo_path() and $ENV{'GIT_DIR'} = $self->repo_path(); |
| 1272 | $self->wc_path() and chdir($self->wc_path()); |
| 1273 | $self->wc_subdir() and chdir($self->wc_subdir()); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1274 | } |
Petr Baudis | 97b16c0 | 2006-06-24 04:34:42 +0200 | [diff] [blame] | 1275 | _execv_git_cmd(@args); |
Ask Bjørn Hansen | 6aaa65d | 2007-11-06 02:54:01 -0800 | [diff] [blame] | 1276 | die qq[exec "@args" failed: $!]; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Petr Baudis | 8062f81 | 2006-06-24 04:34:34 +0200 | [diff] [blame] | 1279 | # Execute the given Git command ($_[0]) with arguments ($_[1..]) |
| 1280 | # by searching for it at proper places. |
Petr Baudis | 18b0fc1 | 2006-09-23 20:20:47 +0200 | [diff] [blame] | 1281 | sub _execv_git_cmd { exec('git', @_); } |
Petr Baudis | 8062f81 | 2006-06-24 04:34:34 +0200 | [diff] [blame] | 1282 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1283 | # Close pipe to a subprocess. |
| 1284 | sub _cmd_close { |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 1285 | my ($fh, $ctx) = @_; |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1286 | if (not close $fh) { |
| 1287 | if ($!) { |
| 1288 | # It's just close, no point in fatalities |
| 1289 | carp "error closing pipe: $!"; |
| 1290 | } elsif ($? >> 8) { |
Petr Baudis | 8b9150e | 2006-06-24 04:34:44 +0200 | [diff] [blame] | 1291 | # The caller should pepper this. |
| 1292 | throw Git::Error::Command($ctx, $? >> 8); |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1293 | } |
| 1294 | # else we might e.g. closed a live stream; the command |
| 1295 | # dying of SIGPIPE would drive us here. |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | |
Adam Roben | 7182530 | 2008-05-23 16:19:40 +0200 | [diff] [blame] | 1300 | sub DESTROY { |
| 1301 | my ($self) = @_; |
| 1302 | $self->_close_hash_and_insert_object(); |
| 1303 | $self->_close_cat_blob(); |
| 1304 | } |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1305 | |
| 1306 | |
Petr Baudis | a6065b5 | 2006-06-25 03:54:23 +0200 | [diff] [blame] | 1307 | # Pipe implementation for ActiveState Perl. |
| 1308 | |
| 1309 | package Git::activestate_pipe; |
| 1310 | use strict; |
| 1311 | |
| 1312 | sub TIEHANDLE { |
| 1313 | my ($class, @params) = @_; |
| 1314 | # FIXME: This is probably horrible idea and the thing will explode |
| 1315 | # at the moment you give it arguments that require some quoting, |
| 1316 | # but I have no ActiveState clue... --pasky |
Alex Riesen | d3b1785 | 2007-01-22 17:14:56 +0100 | [diff] [blame] | 1317 | # Let's just hope ActiveState Perl does at least the quoting |
| 1318 | # correctly. |
| 1319 | my @data = qx{git @params}; |
Petr Baudis | a6065b5 | 2006-06-25 03:54:23 +0200 | [diff] [blame] | 1320 | bless { i => 0, data => \@data }, $class; |
| 1321 | } |
| 1322 | |
| 1323 | sub READLINE { |
| 1324 | my $self = shift; |
| 1325 | if ($self->{i} >= scalar @{$self->{data}}) { |
| 1326 | return undef; |
| 1327 | } |
Alex Riesen | 2f5b398 | 2007-08-22 18:13:07 +0200 | [diff] [blame] | 1328 | my $i = $self->{i}; |
| 1329 | if (wantarray) { |
| 1330 | $self->{i} = $#{$self->{'data'}} + 1; |
| 1331 | return splice(@{$self->{'data'}}, $i); |
| 1332 | } |
| 1333 | $self->{i} = $i + 1; |
| 1334 | return $self->{'data'}->[ $i ]; |
Petr Baudis | a6065b5 | 2006-06-25 03:54:23 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | sub CLOSE { |
| 1338 | my $self = shift; |
| 1339 | delete $self->{data}; |
| 1340 | delete $self->{i}; |
| 1341 | } |
| 1342 | |
| 1343 | sub EOF { |
| 1344 | my $self = shift; |
| 1345 | return ($self->{i} >= scalar @{$self->{data}}); |
| 1346 | } |
| 1347 | |
| 1348 | |
Petr Baudis | b1edc53 | 2006-06-24 04:34:29 +0200 | [diff] [blame] | 1349 | 1; # Famous last words |