@matheusbsbs / matheus-bagliano / commits / 27e9fb8d41
Fork 0

git-svn: add the commit-diff command

This is intended for interoperability with git-svnimport. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>

Eric Wong committed Jun 27, 2006 at 19:39 UTC 27e9fb8d415a7bbc14001c14580f4c568e246381
2 files changed +112 -19
contrib/git-svn/git-svn.perl
+71 -19
@@ -46,6 +46,7 @@ my $sha1_short = qr/[a-f\d]{4,40}/;
46 my ($_revision,$_stdin,$_no_ignore_ext,$_no_stop_copy,$_help,$_rmdir,$_edit,
47 $_find_copies_harder, $_l, $_cp_similarity, $_cp_remote,
48 $_repack, $_repack_nr, $_repack_flags,
49 + $_message, $_file,
50 $_template, $_shared, $_no_default_regex, $_no_graft_copy,
51 $_limit, $_verbose, $_incremental, $_oneline, $_l_fmt, $_show_commit,
52 $_version, $_upgrade, $_authors, $_branch_all_refs, @_opt_m);
@@ -65,6 +66,12 @@ my %multi_opts = ( 'trunk|T=s' => \$_trunk,
66 'tags|t=s' => \$_tags,
67 'branches|b=s' => \$_branches );
68 my %init_opts = ( 'template=s' => \$_template, 'shared' => \$_shared );
69 +my %cmt_opts = ( 'edit|e' => \$_edit,
70 + 'rmdir' => \$_rmdir,
71 + 'find-copies-harder' => \$_find_copies_harder,
72 + 'l=i' => \$_l,
73 + 'copy-similarity|C=i'=> \$_cp_similarity
74 +);
75
76 # yes, 'native' sets "\n". Patches to fix this for non-*nix systems welcome:
77 my %EOL = ( CR => "\015", LF => "\012", CRLF => "\015\012", native => "\012" );
@@ -76,14 +83,7 @@ my %cmd = (
83 " (requires URL argument)",
84 \%init_opts ],
85 commit => [ \&commit, "Commit git revisions to SVN",
79 - { 'stdin|' => \$_stdin,
80 - 'edit|e' => \$_edit,
81 - 'rmdir' => \$_rmdir,
82 - 'find-copies-harder' => \$_find_copies_harder,
83 - 'l=i' => \$_l,
84 - 'copy-similarity|C=i'=> \$_cp_similarity,
85 - %fc_opts,
86 - } ],
86 + { 'stdin|' => \$_stdin, %cmt_opts, %fc_opts, } ],
87 'show-ignore' => [ \&show_ignore, "Show svn:ignore listings",
88 { 'revision|r=i' => \$_revision } ],
89 rebuild => [ \&rebuild, "Rebuild git-svn metadata (after git clone)",
@@ -112,6 +112,10 @@ my %cmd = (
112 'show-commit' => \$_show_commit,
113 'authors-file|A=s' => \$_authors,
114 } ],
115 + 'commit-diff' => [ \&commit_diff, 'Commit a diff between two trees',
116 + { 'message|m=s' => \$_message,
117 + 'file|F=s' => \$_file,
118 + %cmt_opts } ],
119 );
120
121 my $cmd;
@@ -484,11 +488,7 @@ sub commit_lib {
488 my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
489 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
490
487 - if (defined $LC_ALL) {
488 - $ENV{LC_ALL} = $LC_ALL;
489 - } else {
490 - delete $ENV{LC_ALL};
491 - }
491 + set_svn_commit_env();
492 foreach my $c (@revs) {
493 my $log_msg = get_commit_message($c, $commit_msg);
494
@@ -723,6 +723,55 @@ out:
723 print '-' x72,"\n" unless $_incremental || $_oneline;
724 }
725
726 +sub commit_diff_usage {
727 + print STDERR "Usage: $0 commit-diff <tree-ish> <tree-ish> [<URL>]\n";
728 + exit 1
729 +}
730 +
731 +sub commit_diff {
732 + if (!$_use_lib) {
733 + print STDERR "commit-diff must be used with SVN libraries\n";
734 + exit 1;
735 + }
736 + my $ta = shift or commit_diff_usage();
737 + my $tb = shift or commit_diff_usage();
738 + if (!eval { $SVN_URL = shift || file_to_s("$GIT_SVN_DIR/info/url") }) {
739 + print STDERR "Needed URL or usable git-svn id command-line\n";
740 + commit_diff_usage();
741 + }
742 + if (defined $_message && defined $_file) {
743 + print STDERR "Both --message/-m and --file/-F specified ",
744 + "for the commit message.\n",
745 + "I have no idea what you mean\n";
746 + exit 1;
747 + }
748 + if (defined $_file) {
749 + $_message = file_to_s($_message);
750 + } else {
751 + $_message ||= get_commit_message($tb,
752 + "$GIT_DIR/.svn-commit.tmp.$$")->{msg};
753 + }
754 + my $repo;
755 + ($repo, $SVN_PATH) = repo_path_split($SVN_URL);
756 + $SVN_LOG ||= libsvn_connect($repo);
757 + $SVN ||= libsvn_connect($repo);
758 + my @lock = $SVN::Core::VERSION ge '1.2.0' ? (undef, 0) : ();
759 + my $ed = SVN::Git::Editor->new({ r => $SVN->get_latest_revnum,
760 + ra => $SVN, c => $tb,
761 + svn_path => $SVN_PATH
762 + },
763 + $SVN->get_commit_editor($_message,
764 + sub {print "Committed $_[0]\n"},@lock)
765 + );
766 + my $mods = libsvn_checkout_tree($ta, $tb, $ed);
767 + if (@$mods == 0) {
768 + print "No changes\n$ta == $tb\n";
769 + $ed->abort_edit;
770 + } else {
771 + $ed->close_edit;
772 + }
773 +}
774 +
775 ########################### utility functions #########################
776
777 sub cmt_showable {
@@ -1470,7 +1519,6 @@ sub get_commit_message {
1519 my %log_msg = ( msg => '' );
1520 open my $msg, '>', $commit_msg or croak $!;
1521
1473 - print "commit: $commit\n";
1522 chomp(my $type = `git-cat-file -t $commit`);
1523 if ($type eq 'commit') {
1524 my $pid = open my $msg_fh, '-|';
@@ -1507,6 +1555,14 @@ sub get_commit_message {
1555 return \%log_msg;
1556 }
1557
1558 +sub set_svn_commit_env {
1559 + if (defined $LC_ALL) {
1560 + $ENV{LC_ALL} = $LC_ALL;
1561 + } else {
1562 + delete $ENV{LC_ALL};
1563 + }
1564 +}
1565 +
1566 sub svn_commit_tree {
1567 my ($last, $commit) = @_;
1568 my $commit_msg = "$GIT_SVN_DIR/.svn-commit.tmp.$$";
@@ -1514,11 +1570,7 @@ sub svn_commit_tree {
1570 my ($oneline) = ($log_msg->{msg} =~ /([^\n\r]+)/);
1571 print "Committing $commit: $oneline\n";
1572
1517 - if (defined $LC_ALL) {
1518 - $ENV{LC_ALL} = $LC_ALL;
1519 - } else {
1520 - delete $ENV{LC_ALL};
1521 - }
1573 + set_svn_commit_env();
1574 my @ci_output = safe_qx(qw(svn commit -F),$commit_msg);
1575 $ENV{LC_ALL} = 'C';
1576 unlink $commit_msg;
contrib/git-svn/t/t0005-commit-diff.sh new
+41
@@ -0,0 +1,41 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2006 Eric Wong
4 +test_description='git-svn commit-diff'
5 +. ./lib-git-svn.sh
6 +
7 +if test -n "$GIT_SVN_NO_LIB" && test "$GIT_SVN_NO_LIB" -ne 0
8 +then
9 + echo 'Skipping: commit-diff needs SVN libraries'
10 + test_done
11 + exit 0
12 +fi
13 +
14 +test_expect_success 'initialize repo' "
15 + mkdir import &&
16 + cd import &&
17 + echo hello > readme &&
18 + svn import -m 'initial' . $svnrepo &&
19 + cd .. &&
20 + echo hello > readme &&
21 + git update-index --add readme &&
22 + git commit -a -m 'initial' &&
23 + echo world >> readme &&
24 + git commit -a -m 'another'
25 + "
26 +
27 +head=`git rev-parse --verify HEAD^0`
28 +prev=`git rev-parse --verify HEAD^1`
29 +
30 +# the internals of the commit-diff command are the same as the regular
31 +# commit, so only a basic test of functionality is needed since we've
32 +# already tested commit extensively elsewhere
33 +
34 +test_expect_success 'test the commit-diff command' "
35 + test -n '$prev' && test -n '$head' &&
36 + git-svn commit-diff '$prev' '$head' '$svnrepo' &&
37 + svn co $svnrepo wc &&
38 + cmp readme wc/readme
39 + "
40 +
41 +test_done