THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

464 lines
13KB

  1. #!/usr/bin/perl
  2. use 5.008;
  3. use strict;
  4. use warnings;
  5. use Getopt::Std;
  6. use File::Temp qw(tempdir);
  7. use Data::Dumper;
  8. use File::Basename qw(basename dirname);
  9. use File::Spec;
  10. use Git;
  11. our ($opt_h, $opt_P, $opt_p, $opt_v, $opt_c, $opt_f, $opt_a, $opt_m, $opt_d, $opt_u, $opt_w, $opt_W, $opt_k);
  12. getopts('uhPpvcfkam:d:w:W');
  13. $opt_h && usage();
  14. die "Need at least one commit identifier!" unless @ARGV;
  15. # Get git-config settings
  16. my $repo = Git->repository();
  17. $opt_w = $repo->config('cvsexportcommit.cvsdir') unless defined $opt_w;
  18. if ($opt_w || $opt_W) {
  19. # Remember where GIT_DIR is before changing to CVS checkout
  20. unless ($ENV{GIT_DIR}) {
  21. # No GIT_DIR set. Figure it out for ourselves
  22. my $gd =`git-rev-parse --git-dir`;
  23. chomp($gd);
  24. $ENV{GIT_DIR} = $gd;
  25. }
  26. # On MSYS, convert a Windows-style path to an MSYS-style path
  27. # so that rel2abs() below works correctly.
  28. if ($^O eq 'msys') {
  29. $ENV{GIT_DIR} =~ s#^([[:alpha:]]):/#/$1/#;
  30. }
  31. # Make sure GIT_DIR is absolute
  32. $ENV{GIT_DIR} = File::Spec->rel2abs($ENV{GIT_DIR});
  33. }
  34. if ($opt_w) {
  35. if (! -d $opt_w."/CVS" ) {
  36. die "$opt_w is not a CVS checkout";
  37. }
  38. chdir $opt_w or die "Cannot change to CVS checkout at $opt_w";
  39. }
  40. unless ($ENV{GIT_DIR} && -r $ENV{GIT_DIR}){
  41. die "GIT_DIR is not defined or is unreadable";
  42. }
  43. my @cvs;
  44. if ($opt_d) {
  45. @cvs = ('cvs', '-d', $opt_d);
  46. } else {
  47. @cvs = ('cvs');
  48. }
  49. # resolve target commit
  50. my $commit;
  51. $commit = pop @ARGV;
  52. $commit = safe_pipe_capture('git-rev-parse', '--verify', "$commit^0");
  53. chomp $commit;
  54. if ($?) {
  55. die "The commit reference $commit did not resolve!";
  56. }
  57. # resolve what parent we want
  58. my $parent;
  59. if (@ARGV) {
  60. $parent = pop @ARGV;
  61. $parent = safe_pipe_capture('git-rev-parse', '--verify', "$parent^0");
  62. chomp $parent;
  63. if ($?) {
  64. die "The parent reference did not resolve!";
  65. }
  66. }
  67. # find parents from the commit itself
  68. my @commit = safe_pipe_capture('git-cat-file', 'commit', $commit);
  69. my @parents;
  70. my $committer;
  71. my $author;
  72. my $stage = 'headers'; # headers, msg
  73. my $title;
  74. my $msg = '';
  75. foreach my $line (@commit) {
  76. chomp $line;
  77. if ($stage eq 'headers' && $line eq '') {
  78. $stage = 'msg';
  79. next;
  80. }
  81. if ($stage eq 'headers') {
  82. if ($line =~ m/^parent (\w{40})$/) { # found a parent
  83. push @parents, $1;
  84. } elsif ($line =~ m/^author (.+) \d+ [-+]\d+$/) {
  85. $author = $1;
  86. } elsif ($line =~ m/^committer (.+) \d+ [-+]\d+$/) {
  87. $committer = $1;
  88. }
  89. } else {
  90. $msg .= $line . "\n";
  91. unless ($title) {
  92. $title = $line;
  93. }
  94. }
  95. }
  96. my $noparent = "0000000000000000000000000000000000000000";
  97. if ($parent) {
  98. my $found;
  99. # double check that it's a valid parent
  100. foreach my $p (@parents) {
  101. if ($p eq $parent) {
  102. $found = 1;
  103. last;
  104. }; # found it
  105. }
  106. die "Did not find $parent in the parents for this commit!" if !$found and !$opt_P;
  107. } else { # we don't have a parent from the cmdline...
  108. if (@parents == 1) { # it's safe to get it from the commit
  109. $parent = $parents[0];
  110. } elsif (@parents == 0) { # there is no parent
  111. $parent = $noparent;
  112. } else { # cannot choose automatically from multiple parents
  113. die "This commit has more than one parent -- please name the parent you want to use explicitly";
  114. }
  115. }
  116. my $go_back_to = 0;
  117. if ($opt_W) {
  118. $opt_v && print "Resetting to $parent\n";
  119. $go_back_to = `git symbolic-ref HEAD 2> /dev/null ||
  120. git rev-parse HEAD` || die "Could not determine current branch";
  121. system("git checkout -q $parent^0") && die "Could not check out $parent^0";
  122. }
  123. $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
  124. # grab the commit message
  125. open(MSG, ">.msg") or die "Cannot open .msg for writing";
  126. if ($opt_m) {
  127. print MSG $opt_m;
  128. }
  129. print MSG $msg;
  130. if ($opt_a) {
  131. print MSG "\n\nAuthor: $author\n";
  132. if ($author ne $committer) {
  133. print MSG "Committer: $committer\n";
  134. }
  135. }
  136. close MSG;
  137. if ($parent eq $noparent) {
  138. `git-diff-tree --binary -p --root $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
  139. } else {
  140. `git-diff-tree --binary -p $parent $commit >.cvsexportcommit.diff`;# || die "Cannot diff";
  141. }
  142. ## apply non-binary changes
  143. # In pedantic mode require all lines of context to match. In normal
  144. # mode, be compatible with diff/patch: assume 3 lines of context and
  145. # require at least one line match, i.e. ignore at most 2 lines of
  146. # context, like diff/patch do by default.
  147. my $context = $opt_p ? '' : '-C1';
  148. print "Checking if patch will apply\n";
  149. my @stat;
  150. open APPLY, "GIT_DIR= git-apply $context --summary --numstat<.cvsexportcommit.diff|" || die "cannot patch";
  151. @stat=<APPLY>;
  152. close APPLY || die "Cannot patch";
  153. my (@bfiles,@files,@afiles,@dfiles);
  154. chomp @stat;
  155. foreach (@stat) {
  156. push (@bfiles,$1) if m/^-\t-\t(.*)$/;
  157. push (@files, $1) if m/^-\t-\t(.*)$/;
  158. push (@files, $1) if m/^\d+\t\d+\t(.*)$/;
  159. push (@afiles,$1) if m/^ create mode [0-7]+ (.*)$/;
  160. push (@dfiles,$1) if m/^ delete mode [0-7]+ (.*)$/;
  161. }
  162. map { s/^"(.*)"$/$1/g } @bfiles,@files;
  163. map { s/\\([0-7]{3})/sprintf('%c',oct $1)/eg } @bfiles,@files;
  164. # check that the files are clean and up to date according to cvs
  165. my $dirty;
  166. my @dirs;
  167. foreach my $p (@afiles) {
  168. my $path = dirname $p;
  169. while (!-d $path and ! grep { $_ eq $path } @dirs) {
  170. unshift @dirs, $path;
  171. $path = dirname $path;
  172. }
  173. }
  174. # ... check dirs,
  175. foreach my $d (@dirs) {
  176. if (-e $d) {
  177. $dirty = 1;
  178. warn "$d exists and is not a directory!\n";
  179. }
  180. }
  181. # ... query status of all files that we have a directory for and parse output of 'cvs status' to %cvsstat.
  182. my @canstatusfiles;
  183. foreach my $f (@files) {
  184. my $path = dirname $f;
  185. next if (grep { $_ eq $path } @dirs);
  186. push @canstatusfiles, $f;
  187. }
  188. my %cvsstat;
  189. if (@canstatusfiles) {
  190. if ($opt_u) {
  191. my @updated = xargs_safe_pipe_capture([@cvs, 'update'], @canstatusfiles);
  192. print @updated;
  193. }
  194. # "cvs status" reorders the parameters, notably when there are multiple
  195. # arguments with the same basename. So be precise here.
  196. my %added = map { $_ => 1 } @afiles;
  197. my %todo = map { $_ => 1 } @canstatusfiles;
  198. while (%todo) {
  199. my @canstatusfiles2 = ();
  200. my %fullname = ();
  201. foreach my $name (keys %todo) {
  202. my $basename = basename($name);
  203. # CVS reports files that don't exist in the current revision as
  204. # "no file $basename" in its "status" output, so we should
  205. # anticipate that. Totally unknown files will have a status
  206. # "Unknown". However, if they exist in the Attic, their status
  207. # will be "Up-to-date" (this means they were added once but have
  208. # been removed).
  209. $basename = "no file $basename" if $added{$basename};
  210. $basename =~ s/^\s+//;
  211. $basename =~ s/\s+$//;
  212. if (!exists($fullname{$basename})) {
  213. $fullname{$basename} = $name;
  214. push (@canstatusfiles2, $name);
  215. delete($todo{$name});
  216. }
  217. }
  218. my @cvsoutput;
  219. @cvsoutput = xargs_safe_pipe_capture([@cvs, 'status'], @canstatusfiles2);
  220. foreach my $l (@cvsoutput) {
  221. chomp $l;
  222. next unless
  223. my ($file, $status) = $l =~ /^File:\s+(.*\S)\s+Status: (.*)$/;
  224. my $fullname = $fullname{$file};
  225. print STDERR "Huh? Status '$status' reported for unexpected file '$file'\n"
  226. unless defined $fullname;
  227. # This response means the file does not exist except in
  228. # CVS's attic, so set the status accordingly
  229. $status = "In-attic"
  230. if $file =~ /^no file /
  231. && $status eq 'Up-to-date';
  232. $cvsstat{$fullname{$file}} = $status
  233. if defined $fullname{$file};
  234. }
  235. }
  236. }
  237. # ... Validate that new files have the correct status
  238. foreach my $f (@afiles) {
  239. next unless defined(my $stat = $cvsstat{$f});
  240. # This means the file has never been seen before
  241. next if $stat eq 'Unknown';
  242. # This means the file has been seen before but was removed
  243. next if $stat eq 'In-attic';
  244. $dirty = 1;
  245. 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";
  246. warn "Status was: $cvsstat{$f}\n";
  247. }
  248. # ... validate known files.
  249. foreach my $f (@files) {
  250. next if grep { $_ eq $f } @afiles;
  251. # TODO:we need to handle removed in cvs
  252. unless (defined ($cvsstat{$f}) and $cvsstat{$f} eq "Up-to-date") {
  253. $dirty = 1;
  254. warn "File $f not up to date but has status '$cvsstat{$f}' in your CVS checkout!\n";
  255. }
  256. # Depending on how your GIT tree got imported from CVS you may
  257. # have a conflict between expanded keywords in your CVS tree and
  258. # unexpanded keywords in the patch about to be applied.
  259. if ($opt_k) {
  260. my $orig_file ="$f.orig";
  261. rename $f, $orig_file;
  262. open(FILTER_IN, "<$orig_file") or die "Cannot open $orig_file\n";
  263. open(FILTER_OUT, ">$f") or die "Cannot open $f\n";
  264. while (<FILTER_IN>)
  265. {
  266. my $line = $_;
  267. $line =~ s/\$([A-Z][a-z]+):[^\$]+\$/\$$1\$/g;
  268. print FILTER_OUT $line;
  269. }
  270. close FILTER_IN;
  271. close FILTER_OUT;
  272. }
  273. }
  274. if ($dirty) {
  275. if ($opt_f) { warn "The tree is not clean -- forced merge\n";
  276. $dirty = 0;
  277. } else {
  278. die "Exiting: your CVS tree is not clean for this merge.";
  279. }
  280. }
  281. print "Applying\n";
  282. if ($opt_W) {
  283. system("git checkout -q $commit^0") && die "cannot patch";
  284. } else {
  285. `GIT_DIR= git-apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
  286. }
  287. print "Patch applied successfully. Adding new files and directories to CVS\n";
  288. my $dirtypatch = 0;
  289. #
  290. # We have to add the directories in order otherwise we will have
  291. # problems when we try and add the sub-directory of a directory we
  292. # have not added yet.
  293. #
  294. # Luckily this is easy to deal with by sorting the directories and
  295. # dealing with the shortest ones first.
  296. #
  297. @dirs = sort { length $a <=> length $b} @dirs;
  298. foreach my $d (@dirs) {
  299. if (system(@cvs,'add',$d)) {
  300. $dirtypatch = 1;
  301. warn "Failed to cvs add directory $d -- you may need to do it manually";
  302. }
  303. }
  304. foreach my $f (@afiles) {
  305. if (grep { $_ eq $f } @bfiles) {
  306. system(@cvs, 'add','-kb',$f);
  307. } else {
  308. system(@cvs, 'add', $f);
  309. }
  310. if ($?) {
  311. $dirtypatch = 1;
  312. warn "Failed to cvs add $f -- you may need to do it manually";
  313. }
  314. }
  315. foreach my $f (@dfiles) {
  316. system(@cvs, 'rm', '-f', $f);
  317. if ($?) {
  318. $dirtypatch = 1;
  319. warn "Failed to cvs rm -f $f -- you may need to do it manually";
  320. }
  321. }
  322. print "Commit to CVS\n";
  323. print "Patch title (first comment line): $title\n";
  324. my @commitfiles = map { unless (m/\s/) { '\''.$_.'\''; } else { $_; }; } (@files);
  325. my $cmd = join(' ', @cvs)." commit -F .msg @commitfiles";
  326. if ($dirtypatch) {
  327. print "NOTE: One or more hunks failed to apply cleanly.\n";
  328. print "You'll need to apply the patch in .cvsexportcommit.diff manually\n";
  329. print "using a patch program. After applying the patch and resolving the\n";
  330. print "problems you may commit using:";
  331. print "\n cd \"$opt_w\"" if $opt_w;
  332. print "\n $cmd\n";
  333. print "\n git checkout $go_back_to\n" if $go_back_to;
  334. print "\n";
  335. exit(1);
  336. }
  337. if ($opt_c) {
  338. print "Autocommit\n $cmd\n";
  339. print xargs_safe_pipe_capture([@cvs, 'commit', '-F', '.msg'], @files);
  340. if ($?) {
  341. die "Exiting: The commit did not succeed";
  342. }
  343. print "Committed successfully to CVS\n";
  344. # clean up
  345. unlink(".msg");
  346. } else {
  347. print "Ready for you to commit, just run:\n\n $cmd\n";
  348. }
  349. # clean up
  350. unlink(".cvsexportcommit.diff");
  351. if ($opt_W) {
  352. system("git checkout $go_back_to") && die "cannot move back to $go_back_to";
  353. if (!($go_back_to =~ /^[0-9a-fA-F]{40}$/)) {
  354. system("git symbolic-ref HEAD $go_back_to") &&
  355. die "cannot move back to $go_back_to";
  356. }
  357. }
  358. # CVS version 1.11.x and 1.12.x sleeps the wrong way to ensure the timestamp
  359. # used by CVS and the one set by subsequence file modifications are different.
  360. # If they are not different CVS will not detect changes.
  361. sleep(1);
  362. sub usage {
  363. print STDERR <<END;
  364. usage: GIT_DIR=/path/to/.git git cvsexportcommit [-h] [-p] [-v] [-c] [-f] [-u] [-k] [-w cvsworkdir] [-m msgprefix] [ parent ] commit
  365. END
  366. exit(1);
  367. }
  368. # An alternative to `command` that allows input to be passed as an array
  369. # to work around shell problems with weird characters in arguments
  370. # if the exec returns non-zero we die
  371. sub safe_pipe_capture {
  372. my @output;
  373. if (my $pid = open my $child, '-|') {
  374. binmode($child, ":crlf");
  375. @output = (<$child>);
  376. close $child or die join(' ',@_).": $! $?";
  377. } else {
  378. exec(@_) or die "$! $?"; # exec() can fail the executable can't be found
  379. }
  380. return wantarray ? @output : join('',@output);
  381. }
  382. sub xargs_safe_pipe_capture {
  383. my $MAX_ARG_LENGTH = 65536;
  384. my $cmd = shift;
  385. my @output;
  386. my $output;
  387. while(@_) {
  388. my @args;
  389. my $length = 0;
  390. while(@_ && $length < $MAX_ARG_LENGTH) {
  391. push @args, shift;
  392. $length += length($args[$#args]);
  393. }
  394. if (wantarray) {
  395. push @output, safe_pipe_capture(@$cmd, @args);
  396. }
  397. else {
  398. $output .= safe_pipe_capture(@$cmd, @args);
  399. }
  400. }
  401. return wantarray ? @output : $output;
  402. }