[PATCH] git-merge -s theirs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sometimes people wonder why "-s ours" strategy exists but "-s theirs" does
not.  The reason was explained on the mailing list recently:

  Quoting Junio C Hamano <gitster@xxxxxxxxx>:

  > One big problem "-s theirs" has, compared to the "reset to origin,
  > discarding or setting aside the failed history" is that your 'master'
  > history that your further development is based on will keep your failed
  > crap in it forever if you did "-s theirs".  Hopefully you will become a
  > better programmer over time, and you may eventually have something worth
  > sharing with the world near the tip of your master branch.  When that
  > happens, however, you _cannot_ offer your master branch to be pulled by
  > the upstream, as the wider world will not be interested in your earlier
  > mistakes at all.

This patch steals much code from "git-merge-resolve" to add "theirs"
strategy.  Its purpose is to always fail and suggest using the preferred
command "git reset --hard the-other-commit".

Signed-off-by: Nanako Shiraishi <nanako3@xxxxxxxxxxx>
---
 Makefile            |    3 ++-
 builtin-merge.c     |    1 +
 git-merge-theirs.sh |   34 ++++++++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 1 deletions(-)
 create mode 100755 git-merge-theirs.sh

diff --git a/Makefile b/Makefile
index 2b670d7..22f699a 100644
--- a/Makefile
+++ b/Makefile
@@ -240,6 +240,7 @@ SCRIPT_SH += git-lost-found.sh
 SCRIPT_SH += git-merge-octopus.sh
 SCRIPT_SH += git-merge-one-file.sh
 SCRIPT_SH += git-merge-resolve.sh
+SCRIPT_SH += git-merge-theirs.sh
 SCRIPT_SH += git-mergetool.sh
 SCRIPT_SH += git-parse-remote.sh
 SCRIPT_SH += git-pull.sh
@@ -1433,7 +1434,7 @@ check-docs::
 	do \
 		case "$$v" in \
 		git-merge-octopus | git-merge-ours | git-merge-recursive | \
-		git-merge-resolve | git-merge-subtree | \
+		git-merge-resolve | git-merge-subtree | git-merge-theirs | \
 		git-fsck-objects | git-init-db | \
 		git-?*--?* ) continue ;; \
 		esac ; \
diff --git a/builtin-merge.c b/builtin-merge.c
index edc6016..8ed815b 100644
--- a/builtin-merge.c
+++ b/builtin-merge.c
@@ -55,6 +55,7 @@ static struct strategy all_strategy[] = {
 	{ "octopus",    DEFAULT_OCTOPUS },
 	{ "resolve",    0 },
 	{ "ours",       NO_FAST_FORWARD | NO_TRIVIAL },
+	{ "theirs",     NO_FAST_FORWARD | NO_TRIVIAL },
 	{ "subtree",    NO_FAST_FORWARD | NO_TRIVIAL },
 };
 
diff --git a/git-merge-theirs.sh b/git-merge-theirs.sh
new file mode 100755
index 0000000..e228d2b
--- /dev/null
+++ b/git-merge-theirs.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+# The first parameters up to -- are merge bases; the rest are heads.
+bases= head= remotes= sep_seen=
+for arg
+do
+	case ",$sep_seen,$head,$arg," in
+	*,--,)
+		sep_seen=yes
+		;;
+	,yes,,*)
+		head=$arg
+		;;
+	,yes,*)
+		remotes="$remotes$arg "
+		;;
+	*)
+		bases="$bases$arg "
+		;;
+	esac
+done
+
+# Give up if we are given two or more remotes -- not handling octopus.
+case "$remotes" in
+?*' '?*)
+	exit 2 ;;
+esac
+
+echo "If you wanted to say the other history is better than your history," 
+echo "use 'git reset --hard $remotes' instead." 
+echo "If you want to keep a record of your failure, you can create a" 
+echo "new branch from the current HEAD before running the reset command." 
+
+exit 2
-- 
1.5.6.3

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux