Skip to content

Commit 839a7a0

Browse files
author
Linus Torvalds
committed
Add the simple scripts I used to do a merge with content conflicts.
They sure as hell aren't perfect, but they allow you to do: ./git-pull-script {other-git-directory} to do the initial merge, and if that had content clashes, you do merge-cache ./git-merge-one-file-script -a which tries to auto-merge. When/if the auto-merge fails, it will leave the last file in your working directory, and you can edit it and then when you're happy you can do "update-cache filename" on it. Re-do the merge-cache thing until there are no files left to be merged, and now you can write the tree and commit: write-tree commit-tree .... -p $(cat .git/HEAD) -p $(cat .git/MERGE_HEAD) and you're done.
1 parent b51ad43 commit 839a7a0

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

git-merge-one-file-script

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
#
3+
# This is the git merge script, called with
4+
#
5+
# $1 - original file (or empty string)
6+
# $2 - file in branch1 (or empty string)
7+
# $3 - file in branch2 (or empty string)
8+
# $4 - pathname in repository
9+
#
10+
#
11+
# Case 1: file removed in both
12+
#
13+
if [ -z "$2$3" ]; then
14+
rm -- "$4"
15+
update-cache --remove -- "$4"
16+
exit 0
17+
fi
18+
#
19+
# Case 2: file exists in just one
20+
#
21+
if [ "$2$3" == "$3$2" ]; then
22+
cat "$2$3" > "$4"
23+
update-cache --add -- "$4"
24+
exit 0
25+
fi
26+
#
27+
# Case 3: file exists in both
28+
#
29+
src="$1"
30+
if [ -z "$1" ]; then
31+
src=/dev/null
32+
fi
33+
echo "Auto-merging $4"
34+
cp "$3" "$4"
35+
merge "$4" "$src" "$2" && update-cache --add -- "$4"

git-prune-script

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
fsck-cache --unreachable $(cat .git/HEAD ) | grep unreachable | cut -d' ' -f3 | sed 's:^\(..\):.git/objects/\1/:' | xargs rm

git-pull-script

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
#
3+
# use "$1" or something in a real script, this
4+
# just hard-codes it.
5+
#
6+
merge_repo=$1
7+
8+
echo "Getting object database"
9+
rsync -avz --ignore-existing $merge_repo/ .git/
10+
11+
echo "Getting remote head"
12+
rsync -avz $merge_repo/HEAD .git/MERGE_HEAD
13+
14+
head=$(cat .git/HEAD)
15+
merge_head=$(cat .git/MERGE_HEAD)
16+
common=$(merge-base $head $merge_head)
17+
if [ -z "$common" ]; then
18+
echo "Unable to find common commit between" $merge_head $head
19+
exit 1
20+
fi
21+
22+
# Get the trees associated with those commits
23+
common_tree=$(cat-file commit $common | sed 's/tree //;q')
24+
head_tree=$(cat-file commit $head | sed 's/tree //;q')
25+
merge_tree=$(cat-file commit $merge_head | sed 's/tree //;q')
26+
27+
if [ "$common" == "$merge_head" ]; then
28+
echo "Already up-to-date. Yeeah!"
29+
exit 0
30+
fi
31+
if [ "$common" == "$head" ]; then
32+
echo "Updating from $head to $merge_head."
33+
echo "Destroying all noncommitted data!"
34+
echo "Kill me within 3 seconds.."
35+
sleep 3
36+
read-tree $merge_tree && checkout-cache -f -a
37+
echo $merge_head > .git/HEAD
38+
exit 0
39+
fi
40+
echo "Trying to merge $merge_head into $head"
41+
read-tree -m $common_tree $head_tree $merge_tree
42+
result_tree=$(write-tree) || exit 1
43+
result_commit=$(echo "Merge $merge_repo" | commit-tree $result_tree -p $head -p $merge_head)
44+
echo "Committed merge $result_commit"
45+
echo $result_commit > .git/HEAD
46+
read-tree $result_tree && checkout-cache -f -a

0 commit comments

Comments
 (0)