annotate: Convert all -| calls to use a helper open_pipe().
When we settle on a solution for ActiveState's forking issues, all compatibility checks can be handled inside this one function. Also, fixed an abuse of global variables in the process of cleaning this up. Signed-off-by: Ryan Anderson <ryan@michonline.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Ryan Anderson committed
Feb 25, 2006 at 22:02 UTC
6b3e21d6031e1e8df8b01cba5ab7374c4b721257
1 file changed
+44
-29
git-annotate.perl
+44
-29
@@ -98,15 +98,15 @@ while (my $bound = pop @stack) {
98
push @revqueue, $head;
99
init_claim( defined $starting_rev ? $starting_rev : 'dirty');
100
unless (defined $starting_rev) {
101
- open(DIFF,"-|","git","diff","-R", "HEAD", "--",$filename)
101
+ my $diff = open_pipe("git","diff","-R", "HEAD", "--",$filename)
102
or die "Failed to call git diff to check for dirty state: $!";
103
104
- _git_diff_parse(*DIFF, $head, "dirty", (
104
+ _git_diff_parse($diff, $head, "dirty", (
105
'author' => gitvar_name("GIT_AUTHOR_IDENT"),
106
'author_date' => sprintf("%s +0000",time()),
107
)
108
);
109
- close(DIFF);
109
+ close($diff);
110
}
111
handle_rev();
112
@@ -172,20 +172,21 @@ sub handle_rev {
172
sub git_rev_list {
173
my ($rev, $file) = @_;
174
175
+ my $revlist;
176
if ($rev_file) {
176
- open(P, '<' . $rev_file);
177
+ open($revlist, '<' . $rev_file);
178
} else {
178
- open(P,"-|","git-rev-list","--parents","--remove-empty",$rev,"--",$file)
179
+ $revlist = open_pipe("git-rev-list","--parents","--remove-empty",$rev,"--",$file)
180
or die "Failed to exec git-rev-list: $!";
181
}
182
183
my @revs;
183
- while(my $line = <P>) {
184
+ while(my $line = <$revlist>) {
185
chomp $line;
186
my ($rev, @parents) = split /\s+/, $line;
187
push @revs, [ $rev, @parents ];
188
}
188
- close(P);
189
+ close($revlist);
190
191
printf("0 revs found for rev %s (%s)\n", $rev, $file) if (@revs == 0);
192
return @revs;
@@ -194,22 +195,22 @@ sub git_rev_list {
195
sub find_parent_renames {
196
my ($rev, $file) = @_;
197
197
- open(P,"-|","git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
198
+ my $patch = open_pipe("git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
199
or die "Failed to exec git-diff: $!";
200
201
local $/ = "\0";
202
my %bound;
202
- my $junk = <P>;
203
- while (my $change = <P>) {
203
+ my $junk = <$patch>;
204
+ while (my $change = <$patch>) {
205
chomp $change;
205
- my $filename = <P>;
206
+ my $filename = <$patch>;
207
chomp $filename;
208
209
if ($change =~ m/^[AMD]$/ ) {
210
next;
211
} elsif ($change =~ m/^R/ ) {
212
my $oldfilename = $filename;
212
- $filename = <P>;
213
+ $filename = <$patch>;
214
chomp $filename;
215
if ( $file eq $filename ) {
216
my $parent = git_find_parent($rev, $oldfilename);
@@ -218,7 +219,7 @@ sub find_parent_renames {
219
}
220
}
221
}
221
- close(P);
222
+ close($patch);
223
224
return \%bound;
225
}
@@ -227,14 +228,14 @@ sub find_parent_renames {
228
sub git_find_parent {
229
my ($rev, $filename) = @_;
230
230
- open(REVPARENT,"-|","git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
231
+ my $revparent = open_pipe("git-rev-list","--remove-empty", "--parents","--max-count=1","$rev","--",$filename)
232
or die "Failed to open git-rev-list to find a single parent: $!";
233
233
- my $parentline = <REVPARENT>;
234
+ my $parentline = <$revparent>;
235
chomp $parentline;
236
my ($revfound,$parent) = split m/\s+/, $parentline;
237
237
- close(REVPARENT);
238
+ close($revparent);
239
240
return $parent;
241
}
@@ -245,13 +246,13 @@ sub git_find_parent {
246
sub git_diff_parse {
247
my ($parent, $rev, %revinfo) = @_;
248
248
- open(DIFF,"-|","git-diff-tree","-M","-p",$rev,$parent,"--",
249
+ my $diff = open_pipe("git-diff-tree","-M","-p",$rev,$parent,"--",
250
$revs{$rev}{'filename'}, $revs{$parent}{'filename'})
251
or die "Failed to call git-diff for annotation: $!";
252
252
- _git_diff_parse(*DIFF, $parent, $rev, %revinfo);
253
+ _git_diff_parse($diff, $parent, $rev, %revinfo);
254
254
- close(DIFF);
255
+ close($diff);
256
}
257
258
sub _git_diff_parse {
@@ -264,7 +265,7 @@ sub _git_diff_parse {
265
my $gotheader = 0;
266
my ($remstart);
267
my ($hunk_start, $hunk_index);
267
- while(<DIFF>) {
268
+ while(<$diff>) {
269
chomp;
270
if (m/^@@ -(\d+),(\d+) \+(\d+),(\d+)/) {
271
$remstart = $1;
@@ -335,15 +336,15 @@ sub git_cat_file {
336
337
my $blob = git_ls_tree($rev, $filename);
338
338
- open(C,"-|","git","cat-file", "blob", $blob)
339
+ my $catfile = open_pipe("git","cat-file", "blob", $blob)
340
or die "Failed to git-cat-file blob $blob (rev $rev, file $filename): " . $!;
341
342
my @lines;
342
- while(<C>) {
343
+ while(<$catfile>) {
344
chomp;
345
push @lines, $_;
346
}
346
- close(C);
347
+ close($catfile);
348
349
return @lines;
350
}
@@ -351,15 +352,15 @@ sub git_cat_file {
352
sub git_ls_tree {
353
my ($rev, $filename) = @_;
354
354
- open(T,"-|","git","ls-tree",$rev,$filename)
355
+ my $lstree = open_pipe("git","ls-tree",$rev,$filename)
356
or die "Failed to call git ls-tree: $!";
357
358
my ($mode, $type, $blob, $tfilename);
358
- while(<T>) {
359
+ while(<$lstree>) {
360
($mode, $type, $blob, $tfilename) = split(/\s+/, $_, 4);
361
last if ($tfilename eq $filename);
362
}
362
- close(T);
363
+ close($lstree);
364
365
return $blob if $filename eq $filename;
366
die "git-ls-tree failed to find blob for $filename";
@@ -379,11 +380,11 @@ sub claim_line {
380
381
sub git_commit_info {
382
my ($rev) = @_;
382
- open(COMMIT, "-|","git-cat-file", "commit", $rev)
383
+ my $commit = open_pipe("git-cat-file", "commit", $rev)
384
or die "Failed to call git-cat-file: $!";
385
386
my %info;
386
- while(<COMMIT>) {
387
+ while(<$commit>) {
388
chomp;
389
last if (length $_ == 0);
390
@@ -397,7 +398,7 @@ sub git_commit_info {
398
$info{'committer_date'} = $3;
399
}
400
}
400
- close(COMMIT);
401
+ close($commit);
402
403
return %info;
404
}
@@ -430,3 +431,17 @@ sub gitvar_name {
431
return join(' ', @field[0...(@field-4)]);
432
}
433
434
+
435
+sub open_pipe {
436
+ my (@execlist) = @_;
437
+
438
+ my $pid = open my $kid, "-|";
439
+ defined $pid or die "Cannot fork: $!";
440
+
441
+ unless ($pid) {
442
+ exec @execlist;
443
+ die "Cannot exec @execlist: $!";
444
+ }
445
+
446
+ return $kid;
447
+}