git-cvsexportcommit.perlon commit Teach 'diff' about 'diff' attribute. (8c70124)
   1#!/usr/bin/perl -w
   2
   3# Known limitations:
   4# - does not propagate permissions
   5# - error handling has not been extensively tested
   6#
   7
   8use strict;
   9use Getopt::Std;
  10use File::Temp qw(tempdir);
  11use Data::Dumper;
  12use File::Basename qw(basename dirname);
  13
  14unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
  15    die "GIT_DIR is not defined or is unreadable";
  16}
  17
  18our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d);
  19
  20getopts('hPpvcfam:d:');
  21
  22$opt_h && usage();
  23
  24die "Need at least one commit identifier!" unless @ARGV;
  25
  26my @cvs;
  27if ($opt_d) {
  28        @cvs = ('cvs', '-d', $opt_d);
  29} else {
  30        @cvs = ('cvs');
  31}
  32
  33# setup a tempdir
  34our ($tmpdir, $tmpdirname) = tempdir('git-cvsapplycommit-XXXXXX',
  35                                     TMPDIR => 1,
  36                                     CLEANUP => 1);
  37
  38# resolve target commit
  39my $commit;
  40$commit = pop @ARGV;
  41$commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
  42chomp $commit;
  43if ($?) {
  44    die "The commit reference $commit did not resolve!";
  45}
  46
  47# resolve what parent we want
  48my $parent;
  49if (@ARGV) {
  50    $parent = pop @ARGV;
  51    $parent =  safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
  52    chomp $parent;
  53    if ($?) {
  54        die "The parent reference did not resolve!";
  55    }
  56}
  57
  58# find parents from the commit itself
  59my @commit  = safe_pipe_capture('git-cat-file', 'commit', $commit);
  60my @parents;
  61my $committer;
  62my $author;
  63my $stage = 'headers'; # headers, msg
  64my $title;
  65my $msg = '';
  66
  67foreach my $line (@commit) {
  68    chomp $line;
  69    if ($stage eq 'headers' && $line eq '') {
  70        $stage = 'msg';
  71        next;
  72    }
  73
  74    if ($stage eq 'headers') {
  75        if ($line =~ m/^parent (\w{40})$/) { # found a parent
  76            push @parents, $1;
  77        } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
  78            $author = $1;
  79        } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
  80            $committer = $1;
  81        }
  82    } else {
  83        $msg .= $line . "\n";
  84        unless ($title) {
  85            $title = $line;
  86        }
  87    }
  88}
  89
  90if ($parent) {
  91    my $found;
  92    # double check that it's a valid parent
  93    foreach my $p (@parents) {
  94        if ($p eq $parent) {
  95            $found = 1;
  96            last;
  97        }; # found it
  98    }
  99    die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
 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
 111open(MSG, ">.msg") or die "Cannot open .msg for writing";
 112if ($opt_m) {
 113    print MSG $opt_m;
 114}
 115print MSG $msg;
 116if ($opt_a) {
 117    print MSG "\n\nAuthor: $author\n";
 118    if ($author ne $committer) {
 119        print MSG "Committer: $committer\n";
 120    }
 121}
 122close MSG;
 123
 124`git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
 125
 126## apply non-binary changes
 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.
 132my $context = $opt_p ? '' : '-C1';
 133
 134print "Checking if patch will apply\n";
 135
 136my @stat;
 137open APPLY, "GIT_DIR= git-apply $context --binary --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
 138@stat=<APPLY>;
 139close APPLY || die "Cannot patch";
 140my (@bfiles,@files,@afiles,@dfiles);
 141chomp @stat;
 142foreach (@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}
 149map { s/^"(.*)"$/$1/g } @bfiles,@files;
 150map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
 151
 152# check that the files are clean and up to date according to cvs
 153my $dirty;
 154my @dirs;
 155foreach 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
 163foreach my $d (@dirs) {
 164    if (-e $d) {
 165        $dirty = 1;
 166        warn "$d exists and is not a directory!\n";
 167    }
 168}
 169foreach my $f (@afiles) {
 170    # This should return only one value
 171    if ($f =~ m,(.*)/[^/]*$,) {
 172        my $p = $1;
 173        next if (grep { $_ eq $p } @dirs);
 174    }
 175    my @status = grep(m/^File/,  safe_pipe_capture(@cvs, '-q', 'status' ,$f));
 176    if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
 177    if (-d dirname $f and $status[0] !~ m/Status: Unknown$/
 178        and $status[0] !~ m/^File: no file /) {
 179        $dirty = 1;
 180        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";
 181        warn "Status was: $status[0]\n";
 182    }
 183}
 184
 185foreach my $f (@files) {
 186    next if grep { $_ eq $f } @afiles;
 187    # TODO:we need to handle removed in cvs
 188    my @status = grep(m/^File/,  safe_pipe_capture(@cvs, '-q', 'status' ,$f));
 189    if (@status > 1) { warn 'Strange! cvs status returned more than one line?'};
 190    unless ($status[0] =~ m/Status: Up-to-date$/) {
 191        $dirty = 1;
 192        warn "File $f not up to date in your CVS checkout!\n";
 193    }
 194}
 195if ($dirty) {
 196    if ($opt_f) {       warn "The tree is not clean -- forced merge\n";
 197        $dirty = 0;
 198    } else {
 199        die "Exiting: your CVS tree is not clean for this merge.";
 200    }
 201}
 202
 203print "Applying\n";
 204`GIT_DIR= git-apply $context --binary --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
 205
 206print "Patch applied successfully. Adding new files and directories to CVS\n";
 207my $dirtypatch = 0;
 208foreach my $d (@dirs) {
 209    if (system(@cvs,'add',$d)) {
 210        $dirtypatch = 1;
 211        warn "Failed to cvs add directory $d -- you may need to do it manually";
 212    }
 213}
 214
 215foreach my $f (@afiles) {
 216    if (grep { $_ eq $f } @bfiles) {
 217      system(@cvs, 'add','-kb',$f);
 218    } else {
 219      system(@cvs, 'add', $f);
 220    }
 221    if ($?) {
 222        $dirtypatch = 1;
 223        warn "Failed to cvs add $f -- you may need to do it manually";
 224    }
 225}
 226
 227foreach my $f (@dfiles) {
 228    system(@cvs, 'rm', '-f', $f);
 229    if ($?) {
 230        $dirtypatch = 1;
 231        warn "Failed to cvs rm -f $f -- you may need to do it manually";
 232    }
 233}
 234
 235print "Commit to CVS\n";
 236print "Patch title (first comment line): $title\n";
 237my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
 238my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
 239
 240if ($dirtypatch) {
 241    print "NOTE: One or more hunks failed to apply cleanly.\n";
 242    print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
 243    print "using a patch program. After applying the patch and resolving the\n";
 244    print "problems you may commit using:";
 245    print "\n    $cmd\n\n";
 246    exit(1);
 247}
 248
 249if ($opt_c) {
 250    print "Autocommit\n  $cmd\n";
 251    print safe_pipe_capture(@cvs, 'commit', '-F', '.msg', @files);
 252    if ($?) {
 253        die "Exiting: The commit did not succeed";
 254    }
 255    print "Committed successfully to CVS\n";
 256    # clean up
 257    unlink(".msg");
 258} else {
 259    print "Ready for you to commit, just run:\n\n   $cmd\n";
 260}
 261
 262# clean up
 263unlink(".cvsexportcommit.diff");
 264
 265sub usage {
 266        print STDERR <<END;
 267Usage: GIT_DIR=/path/to/.git ${\basename $0} [-h] [-p] [-v] [-c] [-f] [-m msgprefix] [ parent ] commit
 268END
 269        exit(1);
 270}
 271
 272# An alternative to `command` that allows input to be passed as an array
 273# to work around shell problems with weird characters in arguments
 274# if the exec returns non-zero we die
 275sub safe_pipe_capture {
 276    my @output;
 277    if (my $pid = open my $child, '-|') {
 278        @output = (<$child>);
 279        close $child or die join(' ',@_).": $! $?";
 280    } else {
 281        exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
 282    }
 283    return wantarray ? @output : join('',@output);
 284}
 285
 286sub safe_pipe_capture_blob {
 287    my $output;
 288    if (my $pid = open my $child, '-|') {
 289        local $/;
 290        undef $/;
 291        $output = (<$child>);
 292        close $child or die join(' ',@_).": $! $?";
 293    } else {
 294        exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
 295    }
 296    return $output;
 297}