exportcommit: replace backticks with safe_pipe_capture() or system() - initial pass
Replaced backticks with potentially troublesome unescaped input with safe_pipe_capture(). Signed-off-by: Martin Langhoff <martin@catalyst.net.nz> Signed-off-by: Junio C Hamano <junkio@cox.net>
Martin Langhoff committed
Jan 30, 2006 at 19:12 UTC
d41df15e6913e8f7e1b5d656bb51dc4268232c36
1 file changed
+33
-20
git-cvsexportcommit.perl
+33
-20
@@ -27,7 +27,7 @@ print Dumper(@ARGV);
27
# resolve target commit
28
my $commit;
29
$commit = pop @ARGV;
30
-$commit = `git-rev-parse --verify "$commit"^0`;
30
+$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
31
chomp $commit;
32
if ($?) {
33
die "The commit reference $commit did not resolve!";
@@ -37,7 +37,7 @@ if ($?) {
37
my $parent;
38
if (@ARGV) {
39
$parent = pop @ARGV;
40
- $parent = `git-rev-parse --verify "$parent"^0"`;
40
+ $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
41
chomp $parent;
42
if ($?) {
43
die "The parent reference did not resolve!";
@@ -45,7 +45,7 @@ if (@ARGV) {
45
}
46
47
# find parents from the commit itself
48
-my @commit = `git-cat-file commit $commit`;
48
+my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
49
my @parents;
50
foreach my $p (@commit) {
51
if ($p =~ m/^$/) { # end of commit headers, we're done
@@ -81,12 +81,12 @@ $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
81
$? && die "Error extracting the commit message";
82
83
my (@afiles, @dfiles, @mfiles);
84
-my @files = `git-diff-tree -r $parent $commit`;
84
+my @files = safe_pipe_capture('git-diff-tree', '-r', $parent, $commit);
85
print @files;
86
$? && die "Error in git-diff-tree";
87
foreach my $f (@files) {
88
chomp $f;
89
- my @fields = split(m/\s+/, $f);
89
+ my @fields = split(m!\s+!, $f);
90
if ($fields[4] eq 'A') {
91
push @afiles, $fields[5];
92
}
@@ -104,18 +104,19 @@ undef @files; # don't need it anymore
104
# check that the files are clean and up to date according to cvs
105
my $dirty;
106
foreach my $f (@afiles) {
107
- my $status = `cvs -q status "$f" | grep '^File: '`;
108
-
109
- unless ($status =~ m/Status: Unknown$/) {
110
- $dirty = 1;
107
+ # This should return only one value
108
+ my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
109
+ if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
110
+ unless ($status[0] =~ m/Status: Unknown$/) {
111
+ $dirty = 1;
112
warn "File $f is already known in your CVS checkout!\n";
113
}
114
}
115
foreach my $f (@mfiles, @dfiles) {
116
# TODO:we need to handle removed in cvs
116
- my $status = `cvs -q status "$f" | grep '^File: '`;
117
-
118
- unless ($status =~ m/Status: Up-to-date$/) {
117
+ my @status = grep(m/^File/, safe_pipe_capture('cvs', '-q', 'status' ,$f));
118
+ if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
119
+ unless ($status[0] =~ m/Status: Up-to-date$/) {
120
$dirty = 1;
121
warn "File $f not up to date in your CVS checkout!\n";
122
}
@@ -132,18 +133,17 @@ if ($dirty) {
133
134
print "'Patching' binary files\n";
135
135
-my @bfiles = `git-diff-tree -p $parent $commit | grep '^Binary'`;
136
+my @bfiles = grep(m/^Binary/, safe_pipe_capture('git-diff-tree', '-p', $parent, $commit));
137
@bfiles = map { chomp } @bfiles;
138
foreach my $f (@bfiles) {
139
# check that the file in cvs matches the "old" file
140
# extract the file to $tmpdir and comparre with cmp
140
- my $tree = `git-rev-parse $parent^{tree} `;
141
+ my $tree = safe_pipe_capture('git-rev-parse', "$parent^{tree}");
142
chomp $tree;
143
my $blob = `git-ls-tree $tree "$f" | cut -f 1 | cut -d ' ' -f 3`;
144
chomp $blob;
145
`git-cat-file blob $blob > $tmpdir/blob`;
145
- `cmp -q $f $tmpdir/blob`;
146
- if ($?) {
146
+ if (system('cmp', '-q', $f, "$tmpdir/blob")) {
147
warn "Binary file $f in CVS does not match parent.\n";
148
$dirty = 1;
149
next;
@@ -175,7 +175,7 @@ if (($? >> 8) == 2) {
175
}
176
177
foreach my $f (@afiles) {
178
- `cvs add $f`;
178
+ system('cvs', 'add', $f);
179
if ($?) {
180
$dirty = 1;
181
warn "Failed to cvs add $f -- you may need to do it manually";
@@ -183,7 +183,7 @@ foreach my $f (@afiles) {
183
}
184
185
foreach my $f (@dfiles) {
186
- `cvs rm -f $f`;
186
+ system('cvs', 'rm', '-f', $f);
187
if ($?) {
188
$dirty = 1;
189
warn "Failed to cvs rm -f $f -- you may need to do it manually";
@@ -204,7 +204,7 @@ if ($dirtypatch) {
204
205
if ($opt_c) {
206
print "Autocommit\n $cmd\n";
207
- print `cvs commit -F .msg $commitfiles 2>&1`;
207
+ print safe_pipe_capture('cvs', 'commit', '-F', '.msg', @afiles, @mfiles, @dfiles);
208
if ($?) {
209
cleanupcvs(@files);
210
die "Exiting: The commit did not succeed";
@@ -224,10 +224,23 @@ END
224
sub cleanupcvs {
225
my @files = @_;
226
foreach my $f (@files) {
227
- `cvs -q update -C "$f"`;
227
+ system('cvs', '-q', 'update', '-C', $f);
228
if ($?) {
229
warn "Warning! Failed to cleanup state of $f\n";
230
}
231
}
232
}
233
234
+# An alterative to `command` that allows input to be passed as an array
235
+# to work around shell problems with weird characters in arguments
236
+# if the exec returns non-zero we die
237
+sub safe_pipe_capture {
238
+ my @output;
239
+ if (my $pid = open my $child, '-|') {
240
+ @output = (<$child>);
241
+ close $child or die join(' ',@_).": $! $?";
242
+ } else {
243
+ exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
244
+ }
245
+ return wantarray ? @output : join('',@output);
246
+}