git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Infamous 'octopus merge'

From
Junio C Hamano <junkio@cox.net>
Date
Aug 20, 2005, 18:25 UTC
Message-ID
<7vbr3smpnr.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<7vvf20o4sp.fsf@assigned-by-dhcp.cox.net>

This script uses the list of heads and their origin multi-head "git fetch" left in the $GIT_DIR/FETCH_HEAD file, and makes an octopus merge on top of the current HEAD using them.

The implementation tries to be strict for the sake of safety. It insists that your working tree is clean (no local changes) and matches the HEAD, and when any of the merged heads does not automerge, the whole process is aborted and tries to rewind your working tree is to the original state.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 Makefile           |    1 +
 git-octopus-script |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+), 0 deletions(-)
 create mode 100755 git-octopus-script
325918bdc88517826fb7f14fba87a34b92ea2b52
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -72,6 +72,7 @@ SCRIPTS += git-count-objects-script
 # SCRIPTS += git-send-email-script
 SCRIPTS += git-revert-script
 SCRIPTS += git-show-branches-script
+SCRIPTS += git-octopus-script
 
 PROG=   git-update-cache git-diff-files git-init-db git-write-tree \
 	git-read-tree git-commit-tree git-cat-file git-fsck-cache \
diff --git a/git-octopus-script b/git-octopus-script
new file mode 100755
--- /dev/null
+++ b/git-octopus-script
@@ -0,0 +1,103 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+# Resolve two or more trees recorded in $GIT_DIR/FETCH_HEAD.
+#
+. git-sh-setup-script || die "Not a git archive"
+
+usage () {
+    die "usage: git octopus"
+}
+
+# Sanity check the heads early.
+while read SHA1 REPO
+do
+	test $(git-cat-file -t $SHA1) = "commit" ||
+		die "$REPO given to octopus is not a commit"
+done <"$GIT_DIR/FETCH_HEAD"
+
+head=$(git-rev-parse --verify HEAD) || exit
+
+git-update-cache --refresh ||
+	die "Your working tree is dirty."
+test "$(git-diff-cache --cached "$head")" = "" ||
+	die "Your working tree does not match HEAD."
+
+# MRC is the current "merge reference commit"
+# MRT is the current "merge result tree"
+
+MRC=$head MSG= PARENT="-p $head"
+MRT=$(git-write-tree)
+CNT=1 ;# counting our head
+NON_FF_MERGE=0
+while read SHA1 REPO
+do
+	common=$(git-merge-base $MRC $SHA1) ||
+		die "Unable to find common commit with $SHA1 from $REPO"
+
+	if test "$common" = $SHA1
+	then
+		echo "Already up-to-date: $REPO"
+		continue
+	fi
+
+	CNT=`expr $CNT + 1`
+	PARENT="$PARENT -p $SHA1"
+	MSG="$MSG
+	$REPO"
+
+	if test "$common,$NON_FF_MERGE" = "$MRC,0"
+	then
+		# The first head being merged was a fast-forward.
+		# Advance MRC to the head being merged, and use that
+		# tree as the intermediate result of the merge.
+		# We still need to count this as part of the parent set.
+
+		echo "Fast forwarding to: $REPO"
+		git-read-tree -u -m $head $SHA1 || exit
+		MRC=$SHA1 MRT=$(git-write-tree)
+		continue
+	fi
+
+	NON_FF_MERGE=1
+
+	echo "Trying simple merge with $REPO"
+	git-read-tree -u -m $common $MRT $SHA1 || exit
+	next=$(git-write-tree 2>/dev/null)
+	if test $? -ne 0
+	then
+		echo "Simple merge did not work, trying automatic merge."
+		git-merge-cache -o git-merge-one-file-script -a || {
+		git-read-tree --reset "$head"
+		git-checkout-tree -f -q -u -a
+		die "Automatic merge failed; should not be doing Octopus"
+		}
+		next=$(git-write-tree 2>/dev/null)
+	fi
+	MRC=$common
+	MRT=$next
+done <"$GIT_DIR/FETCH_HEAD"
+
+# Just to be careful in case the user feeds nonsense to us.
+case "$CNT" in
+1)
+	echo "No changes."
+	exit 0 ;;
+2)
+	echo "Not an Octopus; making an ordinary commit."
+	MSG="Merge "`expr "$MSG" : '.	\(.*\)'` ; # remove LF and TAB
+	;;
+*)
+	# In an octopus, the original head is just one of the equals,
+	# so we should list it as such.
+	HEAD_LINK=`readlink "$GIT_DIR/HEAD"`
+	MSG="Octopus merge of the following:
+
+	$HEAD_LINK from .$MSG"
+	;;
+esac
+result_commit=$(echo "$MSG" | git-commit-tree $MRT $PARENT)
+echo "Committed merge $result_commit"
+echo $result_commit >"$GIT_DIR"/HEAD
+git-diff-tree -p $head $result_commit | git-apply --stat
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 7 in “Updated multi-head downloads and $GIT_DIR/remotes/ support”
  1. Junio C HamanoAug 20, 2005
  2. Start adding the $GIT_DIR/remotes/ support.Junio C Hamano, Aug 20, 2005
  3. Multi-head fetch.Junio C Hamano, Aug 20, 2005
  4. Retire git-parse-remote.Junio C Hamano, Aug 20, 2005
  5. Infamous 'octopus merge'Junio C Hamano, Aug 20, 2005
  6. Make "git pull" and "git fetch" default to originJunio C Hamano, Aug 20, 2005
  7. Use .git/remote/origin, not .git/branches/origin.Junio C Hamano, Aug 20, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.