{"thread":{"id":"19575","subject":"[PATCH] git-svn: let 'dcommit $rev' work on $rev instead of HEAD","startedAt":"2009-05-29T14:38:23Z","lastAt":"2009-05-29T14:38:23Z","messageCount":1,"participants":["Thomas Rast"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"115014","messageId":"3126ac02cedeae2dd70fb6559d8d7fdf192d7a57.1243607811.git.trast@student.ethz.ch","threadId":"19575","inReplyTo":null,"subject":"[PATCH] git-svn: let 'dcommit $rev' work on $rev instead of HEAD","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2009-05-29T14:38:23Z","receivedAt":"2009-05-29T14:38:23Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"'git svn dcommit' takes an optional revision argument, but the meaning\nof it was rather scary.  It completely ignored the current state of\nthe HEAD, only looking at the revisions between SVN and $rev.  If HEAD\nwas attached to $branch, the branch lost all commits $rev..$branch in\nthe process.\n\nConsidering that 'git svn dcommit HEAD^' has the intuitive meaning\n\"dcommit all changes on my branch except the last one\", we change the\nmeaning of the revision argument.  git-svn temporarily checks out $rev\nfor its work, meaning that\n\n* if a branch is specified, that branch (_not_ the HEAD) is rebased as\n  part of the dcommit,\n\n* if some other revision is specified, as in the example, all work\n  happens on a detached HEAD and no branch is affected.\n\nSigned-off-by: Thomas Rast <trast@student.ethz.ch>\n---\n\nI hope this can still be changed, as I can see no use-case in which\nthe old variant would have been useful.  The manual accurately states\n\n\tCommit each diff from a specified head directly to the SVN\n\trepository, and then rebase or reset (depending on whether or\n\tnot there is a diff between SVN and head).\n\nmeaning that the \"loss\" of commits is actually a reset --mixed in the\ncommon case where the branch was already rebased with git-svn.  So the\nchanges remain in the tree, but the commits vanish.  This happened to\nan unsuspecting user yesterday on IRC.\n\nThere is a slight catch of course: the user will have to rebase\nmanually.  This may be slightly surprising in the HEAD^ case, but\nspecial-casing would have become too confusing (do we rebase only if\nthe $rev is an ancestor?  Why do we rebase for $branch^0, but not for\n$branch?).\n\nBy analogy with 'git rebase foo bar', we could let the run end on\n'bar' instead of returning to the previous HEAD.  I'm somewhat torn on\nthis, as it would probably reduce confusion as to what happened in the\nbranch case, but require more work from the user in the HEAD^ case.\n\nIf it is too late to change this behaviour, I'll make a patch with a\nbig warning in the docs and at the end of the dcommit run that HEAD\nprobably doesn't look like what the user expected.\n\n\n Documentation/git-svn.txt |    5 +++--\n git-svn.perl              |   32 +++++++++++++++++++++++++++++++-\n t/t9100-git-svn-basic.sh  |   17 +++++++++++++++++\n 3 files changed, 51 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-svn.txt b/Documentation/git-svn.txt\nindex ca3fc3d..5aa9beb 100644\n--- a/Documentation/git-svn.txt\n+++ b/Documentation/git-svn.txt\n@@ -170,8 +170,9 @@ and have no uncommitted changes.\n \tIt is recommended that you run 'git-svn' fetch and rebase (not\n \tpull or merge) your commits against the latest changes in the\n \tSVN repository.\n-\tAn optional command-line argument may be specified as an\n-\talternative to HEAD.\n+\tAn optional revision or branch argument may be specified, and\n+\tcauses 'git-svn' to do all work on that revision/branch\n+\tinstead of HEAD.\n \tThis is advantageous over 'set-tree' (below) because it produces\n \tcleaner, more linear history.\n +\ndiff --git a/git-svn.perl b/git-svn.perl\nindex a70c7d7..0f94c37 100755\n--- a/git-svn.perl\n+++ b/git-svn.perl\n@@ -447,13 +447,27 @@\n \tprint \"Done committing \",scalar @revs,\" revisions to SVN\\n\";\n \tunlink $gs->{index};\n }\n-\n+#\"\n sub cmd_dcommit {\n \tmy $head = shift;\n \tgit_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }\n \t\t'Cannot dcommit with a dirty index.  Commit your changes first, '\n \t\t. \"or stash them with `git stash'.\\n\";\n \t$head ||= 'HEAD';\n+\n+\tmy $old_head;\n+\tif ($head ne 'HEAD') {\n+\t\t$old_head = eval {\n+\t\t\tcommand_oneline([qw/symbolic-ref -q HEAD/])\n+\t\t};\n+\t\tif ($old_head) {\n+\t\t\t$old_head =~ s{^refs/heads/}{};\n+\t\t} else {\n+\t\t\t$old_head = eval { command_oneline(qw/rev-parse HEAD/) };\n+\t\t}\n+\t\tcommand(['checkout', $head], STDERR => 0);\n+\t}\n+\n \tmy @refs;\n \tmy ($url, $rev, $uuid, $gs) = working_head_info($head, \\@refs);\n \tunless ($gs) {\n@@ -579,6 +593,22 @@\n \t\t\t}\n \t\t}\n \t}\n+\n+\tif ($old_head) {\n+\t\tmy $new_head = command_oneline(qw/rev-parse HEAD/);\n+\t\tmy $new_is_symbolic = eval {\n+\t\t\tcommand_oneline(qw/symbolic-ref -q HEAD/);\n+\t\t};\n+\t\tif ($new_is_symbolic) {\n+\t\t\tprint \"dcommitted the branch \", $head, \"\\n\";\n+\t\t} else {\n+\t\t\tprint \"dcommitted on a detached HEAD because you gave \",\n+\t\t\t      \"a revision argument.\\n\",\n+\t\t\t      \"The rewritten commit is: \", $new_head, \"\\n\";\n+\t\t}\n+\t\tcommand(['checkout', $old_head], STDERR => 0);\n+\t}\n+\n \tunlink $gs->{index};\n }\n \ndiff --git a/t/t9100-git-svn-basic.sh b/t/t9100-git-svn-basic.sh\nindex 64aa7e2..476c3da 100755\n--- a/t/t9100-git-svn-basic.sh\n+++ b/t/t9100-git-svn-basic.sh\n@@ -231,6 +231,23 @@ test_expect_success \\\n                               \"^:refs/${remotes_git_svn}$\"\n         '\n \n+test_expect_success 'dcommit $rev does not clobber current branch' '\n+\tgit svn fetch -i bar &&\n+\tgit checkout -b my-bar refs/remotes/bar &&\n+\techo 1 > foo &&\n+\tgit add foo &&\n+\tgit commit -m 1 &&\n+\techo 2 > foo &&\n+\tgit add foo &&\n+\tgit commit -m 2 &&\n+\told_head=$(git rev-parse HEAD) &&\n+\tgit svn dcommit -i bar HEAD^ &&\n+\ttest $old_head = $(git rev-parse HEAD) &&\n+\ttest refs/heads/my-bar = $(git symbolic-ref HEAD) &&\n+\tgit checkout master &&\n+\tgit branch -D my-bar\n+\t'\n+\n test_expect_success 'able to dcommit to a subdirectory' \"\n \tgit svn fetch -i bar &&\n \tgit checkout -b my-bar refs/remotes/bar &&\n-- \n1.6.3.1.286.gcb89e\n"}]}