Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | |
Yann Dirson | 0d71b31 | 2006-05-27 18:39:33 +0200 | [diff] [blame] | 3 | # Known limitations: |
Yann Dirson | 0d71b31 | 2006-05-27 18:39:33 +0200 | [diff] [blame] | 4 | # - does not propagate permissions |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 5 | # - error handling has not been extensively tested |
| 6 | # |
Yann Dirson | 0d71b31 | 2006-05-27 18:39:33 +0200 | [diff] [blame] | 7 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 8 | use strict; |
| 9 | use Getopt::Std; |
| 10 | use File::Temp qw(tempdir); |
| 11 | use Data::Dumper; |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 12 | use File::Basename qw(basename dirname); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 13 | |
| 14 | unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){ |
| 15 | die "GIT_DIR is not defined or is unreadable"; |
| 16 | } |
| 17 | |
Robin Rosenberg | e5d8064 | 2007-05-24 17:06:55 +0200 | [diff] [blame] | 18 | our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 19 | |
Robin Rosenberg | e5d8064 | 2007-05-24 17:06:55 +0200 | [diff] [blame] | 20 | getopts('uhPpvcfam:d:'); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 21 | |
| 22 | $opt_h && usage(); |
| 23 | |
| 24 | die "Need at least one commit identifier!" unless @ARGV; |
| 25 | |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 26 | my @cvs; |
| 27 | if ($opt_d) { |
| 28 | @cvs = ('cvs', '-d', $opt_d); |
| 29 | } else { |
| 30 | @cvs = ('cvs'); |
| 31 | } |
| 32 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 33 | # setup a tempdir |
| 34 | our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX', |
| 35 | TMPDIR => 1, |
| 36 | CLEANUP => 1); |
| 37 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 38 | # resolve target commit |
| 39 | my $commit; |
| 40 | $commit = pop @ARGV; |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 41 | $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0"); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 42 | chomp $commit; |
| 43 | if ($?) { |
| 44 | die "The commit reference $commit did not resolve!"; |
| 45 | } |
| 46 | |
| 47 | # resolve what parent we want |
| 48 | my $parent; |
| 49 | if (@ARGV) { |
| 50 | $parent = pop @ARGV; |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 51 | $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0"); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 52 | chomp $parent; |
| 53 | if ($?) { |
| 54 | die "The parent reference did not resolve!"; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | # find parents from the commit itself |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 59 | my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 60 | my @parents; |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 61 | my $committer; |
| 62 | my $author; |
| 63 | my $stage = 'headers'; # headers, msg |
| 64 | my $title; |
| 65 | my $msg = ''; |
| 66 | |
| 67 | foreach my $line (@commit) { |
| 68 | chomp $line; |
| 69 | if ($stage eq 'headers' && $line eq '') { |
| 70 | $stage = 'msg'; |
| 71 | next; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 72 | } |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 73 | |
| 74 | if ($stage eq 'headers') { |
| 75 | if ($line =~ m/^parent (\w{40})$/) { # found a parent |
| 76 | push @parents, $1; |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 77 | } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) { |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 78 | $author = $1; |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 79 | } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) { |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 80 | $committer = $1; |
| 81 | } |
| 82 | } else { |
| 83 | $msg .= $line . "\n"; |
| 84 | unless ($title) { |
| 85 | $title = $line; |
| 86 | } |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | if ($parent) { |
Peter Baumann | 135a522 | 2006-07-07 12:55:41 +0200 | [diff] [blame] | 91 | my $found; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 92 | # double check that it's a valid parent |
| 93 | foreach my $p (@parents) { |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 94 | if ($p eq $parent) { |
| 95 | $found = 1; |
| 96 | last; |
| 97 | }; # found it |
Alexander Litvinov | e09f5d7 | 2005-11-09 13:02:58 +0600 | [diff] [blame] | 98 | } |
Simon 'corecode' Schubert | ca28370 | 2007-02-01 11:43:39 +0100 | [diff] [blame] | 99 | die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 100 | } else { # we don't have a parent from the cmdline... |
| 101 | if (@parents == 1) { # it's safe to get it from the commit |
| 102 | $parent = $parents[0]; |
| 103 | } else { # or perhaps not! |
| 104 | die "This commit has more than one parent -- please name the parent you want to use explicitly"; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | $opt_v && print "Applying to CVS commit $commit from parent $parent\n"; |
| 109 | |
| 110 | # grab the commit message |
Martin Langhoff | 992793c | 2006-04-26 12:26:16 +1200 | [diff] [blame] | 111 | open(MSG, ">.msg") or die "Cannot open .msg for writing"; |
Martin Langhoff | 1b91abe | 2006-07-18 14:22:49 +1200 | [diff] [blame] | 112 | if ($opt_m) { |
| 113 | print MSG $opt_m; |
| 114 | } |
| 115 | print MSG $msg; |
| 116 | if ($opt_a) { |
| 117 | print MSG "\n\nAuthor: $author\n"; |
| 118 | if ($author ne $committer) { |
| 119 | print MSG "Committer: $committer\n"; |
| 120 | } |
| 121 | } |
Martin Langhoff | 992793c | 2006-04-26 12:26:16 +1200 | [diff] [blame] | 122 | close MSG; |
| 123 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 124 | `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff"; |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 125 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 126 | ## apply non-binary changes |
Tomash Brechko | fc1f458 | 2007-04-09 15:24:02 +0400 | [diff] [blame] | 127 | |
| 128 | # In pedantic mode require all lines of context to match. In normal |
| 129 | # mode, be compatible with diff/patch: assume 3 lines of context and |
| 130 | # require at least one line match, i.e. ignore at most 2 lines of |
| 131 | # context, like diff/patch do by default. |
| 132 | my $context = $opt_p ? '' : '-C1'; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 133 | |
| 134 | print "Checking if patch will apply\n"; |
| 135 | |
| 136 | my @stat; |
Tomash Brechko | fc1f458 | 2007-04-09 15:24:02 +0400 | [diff] [blame] | 137 | open APPLY, "GIT_DIR= git-apply $context --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch"; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 138 | @stat=<APPLY>; |
| 139 | close APPLY || die "Cannot patch"; |
| 140 | my (@bfiles,@files,@afiles,@dfiles); |
| 141 | chomp @stat; |
| 142 | foreach (@stat) { |
| 143 | push (@bfiles,$1) if m/^-\t-\t(.*)$/; |
| 144 | push (@files, $1) if m/^-\t-\t(.*)$/; |
| 145 | push (@files, $1) if m/^\d+\t\d+\t(.*)$/; |
| 146 | push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/; |
| 147 | push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/; |
| 148 | } |
| 149 | map { s/^"(.*)"$/$1/g } @bfiles,@files; |
| 150 | map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 151 | |
| 152 | # check that the files are clean and up to date according to cvs |
| 153 | my $dirty; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 154 | my @dirs; |
| 155 | foreach my $p (@afiles) { |
| 156 | my $path = dirname $p; |
| 157 | while (!-d $path and ! grep { $_ eq $path } @dirs) { |
| 158 | unshift @dirs, $path; |
| 159 | $path = dirname $path; |
| 160 | } |
| 161 | } |
| 162 | |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 163 | # ... check dirs, |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 164 | foreach my $d (@dirs) { |
| 165 | if (-e $d) { |
| 166 | $dirty = 1; |
| 167 | warn "$d exists and is not a directory!\n"; |
| 168 | } |
| 169 | } |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 170 | |
| 171 | # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat. |
| 172 | my @canstatusfiles; |
| 173 | foreach my $f (@files) { |
| 174 | my $path = dirname $f; |
| 175 | next if (grep { $_ eq $path } @dirs); |
| 176 | push @canstatusfiles, $f; |
| 177 | } |
| 178 | |
| 179 | my %cvsstat; |
| 180 | if (@canstatusfiles) { |
Robin Rosenberg | e5d8064 | 2007-05-24 17:06:55 +0200 | [diff] [blame] | 181 | if ($opt_u) { |
| 182 | my @updated = safe_pipe_capture(@cvs, 'update', @canstatusfiles); |
| 183 | print @updated; |
| 184 | } |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 185 | my @cvsoutput; |
| 186 | @cvsoutput= safe_pipe_capture(@cvs, 'status', @canstatusfiles); |
| 187 | my $matchcount = 0; |
| 188 | foreach my $l (@cvsoutput) { |
| 189 | chomp $l; |
| 190 | if ( $l =~ /^File:/ and $l =~ /Status: (.*)$/ ) { |
| 191 | $cvsstat{$canstatusfiles[$matchcount]} = $1; |
| 192 | $matchcount++; |
| 193 | } |
Yann Dirson | 576cfc8 | 2006-01-06 21:54:41 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 196 | |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 197 | # ... validate new files, |
| 198 | foreach my $f (@afiles) { |
| 199 | if (defined ($cvsstat{$f}) and $cvsstat{$f} ne "Unknown") { |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 200 | $dirty = 1; |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 201 | warn "File $f is already known in your CVS checkout -- perhaps it has been added by another user. Or this may indicate that it exists on a different branch. If this is the case, use -f to force the merge.\n"; |
| 202 | warn "Status was: $cvsstat{$f}\n"; |
| 203 | } |
| 204 | } |
| 205 | # ... validate known files. |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 206 | foreach my $f (@files) { |
| 207 | next if grep { $_ eq $f } @afiles; |
Yann Dirson | 576cfc8 | 2006-01-06 21:54:41 +0100 | [diff] [blame] | 208 | # TODO:we need to handle removed in cvs |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 209 | unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") { |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 210 | $dirty = 1; |
Steffen Prohaska | c56f0d9 | 2007-05-10 01:06:36 +0200 | [diff] [blame] | 211 | warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | if ($dirty) { |
Martin Langhoff | 992793c | 2006-04-26 12:26:16 +1200 | [diff] [blame] | 215 | if ($opt_f) { warn "The tree is not clean -- forced merge\n"; |
| 216 | $dirty = 0; |
| 217 | } else { |
| 218 | die "Exiting: your CVS tree is not clean for this merge."; |
| 219 | } |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 220 | } |
| 221 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 222 | print "Applying\n"; |
Tomash Brechko | fc1f458 | 2007-04-09 15:24:02 +0400 | [diff] [blame] | 223 | `GIT_DIR= git-apply $context --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 224 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 225 | print "Patch applied successfully. Adding new files and directories to CVS\n"; |
| 226 | my $dirtypatch = 0; |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 227 | foreach my $d (@dirs) { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 228 | if (system(@cvs,'add',$d)) { |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 229 | $dirtypatch = 1; |
Yann Dirson | 3f0f756 | 2006-05-27 18:39:35 +0200 | [diff] [blame] | 230 | warn "Failed to cvs add directory $d -- you may need to do it manually"; |
| 231 | } |
| 232 | } |
| 233 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 234 | foreach my $f (@afiles) { |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 235 | if (grep { $_ eq $f } @bfiles) { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 236 | system(@cvs, 'add','-kb',$f); |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 237 | } else { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 238 | system(@cvs, 'add', $f); |
Robin Rosenberg | fe142b3 | 2006-11-12 16:29:42 +0100 | [diff] [blame] | 239 | } |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 240 | if ($?) { |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 241 | $dirtypatch = 1; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 242 | warn "Failed to cvs add $f -- you may need to do it manually"; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | foreach my $f (@dfiles) { |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 247 | system(@cvs, 'rm', '-f', $f); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 248 | if ($?) { |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 249 | $dirtypatch = 1; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 250 | warn "Failed to cvs rm -f $f -- you may need to do it manually"; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | print "Commit to CVS\n"; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 255 | print "Patch title (first comment line): $title\n"; |
| 256 | my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files); |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 257 | my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 258 | |
| 259 | if ($dirtypatch) { |
| 260 | print "NOTE: One or more hunks failed to apply cleanly.\n"; |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 261 | print "You'll need to apply the patch in .cvsexportcommit.diff manually\n"; |
| 262 | print "using a patch program. After applying the patch and resolving the\n"; |
| 263 | print "problems you may commit using:"; |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 264 | print "\n $cmd\n\n"; |
Junio C Hamano | 27dedf0 | 2005-11-16 21:32:44 -0800 | [diff] [blame] | 265 | exit(1); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 266 | } |
| 267 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 268 | if ($opt_c) { |
| 269 | print "Autocommit\n $cmd\n"; |
Simon 'corecode' Schubert | 4a6b9bb | 2007-02-18 18:17:08 +0100 | [diff] [blame] | 270 | print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 271 | if ($?) { |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 272 | die "Exiting: The commit did not succeed"; |
| 273 | } |
| 274 | print "Committed successfully to CVS\n"; |
Gerrit Pape | cf70c16 | 2007-02-28 12:35:39 +0000 | [diff] [blame] | 275 | # clean up |
| 276 | unlink(".msg"); |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 277 | } else { |
| 278 | print "Ready for you to commit, just run:\n\n $cmd\n"; |
| 279 | } |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 280 | |
| 281 | # clean up |
| 282 | unlink(".cvsexportcommit.diff"); |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 283 | |
Robin Rosenberg | f836f1a | 2007-07-25 00:56:20 +0200 | [diff] [blame] | 284 | # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp |
| 285 | # used by CVS and the one set by subsequence file modifications are different. |
| 286 | # If they are not different CVS will not detect changes. |
| 287 | sleep(1); |
| 288 | |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 289 | sub usage { |
| 290 | print STDERR <<END; |
Martin Langhoff | 992793c | 2006-04-26 12:26:16 +1200 | [diff] [blame] | 291 | Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit |
Martin Langhoff | 5e0306a | 2005-11-07 17:57:08 +1300 | [diff] [blame] | 292 | END |
| 293 | exit(1); |
| 294 | } |
| 295 | |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 296 | # An alternative to `command` that allows input to be passed as an array |
Martin Langhoff | d41df15 | 2006-01-30 19:12:12 +1300 | [diff] [blame] | 297 | # to work around shell problems with weird characters in arguments |
| 298 | # if the exec returns non-zero we die |
| 299 | sub safe_pipe_capture { |
| 300 | my @output; |
| 301 | if (my $pid = open my $child, '-|') { |
| 302 | @output = (<$child>); |
| 303 | close $child or die join(' ',@_).": $! $?"; |
| 304 | } else { |
| 305 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found |
| 306 | } |
| 307 | return wantarray ? @output : join('',@output); |
| 308 | } |
Jim Meyering | 7c0f702 | 2006-12-04 08:44:08 +0100 | [diff] [blame] | 309 | |
Robin Rosenberg | e86ad71 | 2006-12-11 00:30:06 +0100 | [diff] [blame] | 310 | sub safe_pipe_capture_blob { |
| 311 | my $output; |
| 312 | if (my $pid = open my $child, '-|') { |
| 313 | local $/; |
| 314 | undef $/; |
| 315 | $output = (<$child>); |
| 316 | close $child or die join(' ',@_).": $! $?"; |
| 317 | } else { |
| 318 | exec(@_) or die "$! $?"; # exec() can fail the executable can't be found |
| 319 | } |
| 320 | return $output; |
Jim Meyering | 7c0f702 | 2006-12-04 08:44:08 +0100 | [diff] [blame] | 321 | } |