Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <junkio@cox.net>
To: git@vger.kernel.org
Subject: [PATCH] Redo "revert" using three-way merge machinery.
Date: Fri, 26 Aug 2005 01:04:42 -0700	[thread overview]
Message-ID: <7v8xypgm3p.fsf@assigned-by-dhcp.cox.net> (raw)

The reverse patch application using "git apply" sometimes is too
rigid.  Since the user would get used to resolving conflicting merges
by hand during the normal merge experience, using the same machinery
would be more helpful rather than just giving up.

Signed-off-by: Junio C Hamano <junkio@cox.net>

---

 git-revert-script |   75 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 42 insertions(+), 33 deletions(-)

186bb385d806bf08b5a81888230ad7a96aec50c7
diff --git a/git-revert-script b/git-revert-script
--- a/git-revert-script
+++ b/git-revert-script
@@ -2,38 +2,47 @@
 . git-sh-setup-script || die "Not a git archive"
 
 # We want a clean tree and clean index to be able to revert.
-dirty=`git-diff-cache --name-only --cached HEAD`
-case "$dirty" in '') dirty=`git-diff-cache --name-only HEAD` ;; esac
-case "$dirty" in
-'') ;;
-*)
-	echo >&2 "Modified:"
-	echo "$dirty" | sed >&2 -e 's/^/	/'
-	die "Your working tree is dirty; cannot revert a previous patch." ;;
-esac
+check_clean_tree || die "Cannot run revert from a dirty tree."
 
 rev=$(git-rev-parse --verify "$@") &&
-commit=$(git-rev-parse --verify "$rev^0") || exit
-if git-diff-tree -R -M -p $commit | git-apply --index &&
-   msg=$(git-rev-list --pretty=oneline --max-count=1 $commit)
-then
-        {
-                echo "$msg" | sed -e '
-			s/^[^ ]* /Revert "/
-			s/$/"/'
-                echo
-                echo "This reverts $commit commit."
-                test "$rev" = "$commit" ||
-                echo "(original 'git revert' arguments: $@)"
-        } | git commit -F -
-else
-        # Now why did it fail?
-        parents=`git-cat-file commit "$commit" 2>/dev/null |
-                sed -ne '/^$/q;/^parent /p' |
-                wc -l`
-        case $parents in
-        0) die "Cannot revert the root commit nor non commit-ish." ;;
-        1) die "The patch does not apply." ;;
-        *) die "Cannot revert a merge commit." ;;
-        esac
-fi
+commit=$(git-rev-parse --verify "$rev^0") ||
+	die "Not a single commit $@"
+head=$(git-rev-parse --verify HEAD) ||
+	die "You do not have a valid HEAD"
+prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
+	die "Cannot revert a root commit"
+git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
+	die "Cannot revert a multi-parent commit."
+
+# "commit" is an existing commit.  We would want to apply
+# the difference it introduces since its first parent "prev"
+# on top of the current HEAD.
+
+{
+	git-rev-list --pretty=oneline --max-count=1 $commit |
+	sed -e '
+		s/^[^ ]* /Revert "/
+		s/$/"/'
+	echo
+	echo "This reverts $commit commit."
+	test "$rev" = "$commit" ||
+	echo "(original 'git revert' arguments: $@)"
+} >.revertmsg
+
+# This three way merge is an interesting one.  We have come from
+# $commit to $head, and would want to apply the change between $commit
+# and $prev to us.
+git-read-tree -m -u $commit $head $prev &&
+result=$(git-write-tree 2>/dev/null) || {
+    echo >&2 "Simple revert fails; trying automated revert."
+    git-merge-cache -o git-merge-one-file-script -a || {
+	    echo >&2 "Automated revert failed.  After fixing it up,"
+	    echo >&2 "you can use \"git commit -F .revertmsg\""
+	    exit 1
+    }
+    result=$(git-write-tree) || exit
+}
+
+git commit -F .revertmsg
+rm -f .revertmsg
+

             reply	other threads:[~2005-08-26  8:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-26  8:04 Junio C Hamano [this message]
  -- strict thread matches above, loose matches on Subject: below --
2005-08-27  1:18 [PATCH] Redo "revert" using three-way merge machinery Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7v8xypgm3p.fsf@assigned-by-dhcp.cox.net \
    --to=junkio@cox.net \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox