git-hacks

git-hacks Commit Details

Date:2014-02-20 10:04:22 (4 years 5 months ago)
Author:Nicola Fontana
Branch:master
Commit:4dbfe17b10e3f31e6148533bcb11453be4bc276d
Parents: ddee25a8d573e6a13830970bbf02a5e9a0988912
Message:post-receive.merge: allow multiple branch merging

Do not use the same branch for pushing and merging. Instead allow to
push on different branches and merge the current working tree with the
last updated ref.

This allows pushing from different repository: just remember to pull
from HEAD for two-file synchronization purposes.
Changes:
Mpost-receive.automerge (2 diffs)

File differences

post-receive.automerge
11
22
3
4
5
6
3
4
5
6
7
78
8
9
10
11
9
10
11
1212
1313
1414
15
15
1616
1717
1818
19
20
19
20
2121
2222
2323
......
3232
3333
3434
35
36
37
35
36
37
3838
3939
40
41
42
43
44
45
46
47
48
49
40
41
42
5043
5144
52
53
54
45
46
5547
5648
57
58
5949
50
51
6052
6153
54
55
56
6257
#! /bin/sh
#
# This is intended to be run inside a non-bare repository. It will
# merge the local working tree with the latest pushed changes after
# every git push, # automatically resolving conflicts by creating a
# copy of each conflicting file.
# This is intended to be run inside a remote (non-bare) repository.
# It merges the latest pushed ref into HEAD (that is the local working
# tree), automatically resolving conflicts by creating a copy of each
# conflicting file. To be able to do this, HEAD must not be in a
# detached state.
#
# I am using it for two-way file synchronization between different
# git repositories: on this side I have this hook enabled and on the
# other side (possibly more than one) I have to do the following in
# order to synchronize the repositories:
# It can be used in a two-way file synchronization process on multiple
# repositories. For example, on every local repository you can use a
# script similar to the following one:
#
# ----->8------
# #! /bin/sh
# # Synchronize this repository with the default remote
# # Synchronize the local repository with origin
#
# git add --all
# git commit --allow-empty -m Autocommit
# git push
# git pull
# git push origin master:`hostname`
# git pull origin HEAD
# -----8<------
#
# Copyright (c) 2014 Nicola Fontana <ntd at entidi.it>
cd ..
# Generate the local tree object to be merged with HEAD
GIT_INDEX_FILE=.git/tmpindex git add --all
tree=`GIT_INDEX_FILE=.git/tmpindex git write-tree`
# Get the name of the recently updated ref
read dummy dummy refname
[ -z "$refname" ] && exit 0
# Reset to the latest push state
rm -f .git/tmpindex
git reset -q --hard
git clean -qfxd
# Check if there is something to merge in the tree
[ -z "$tree" ] && exit 0
commit=`git commit-tree -p HEAD^ -m "$message" $tree`
[ -z "$commit" ] && exit 0
# Merge $refname into HEAD
git merge --no-commit $refname
topdir=`git rev-parse --show-toplevel`
# Commit local changes
git cherry-pick -rn $commit
topdir=`git rev-parse --show-toplevel`
# For each conflicting file, create a backup for the HEAD version and
# substitute the original with the $refname version
for f in `git diff --name-only --diff-filter=U`; do
f2=`mktemp -p "$topdir" "$f-conflict-XXXX"`
git checkout --theirs "$f"
mv "$f" "$f2"
git checkout --ours "$f"
mv "$f" "$f2"
git checkout --theirs "$f"
git add "$f" "$f2"
done
# Commit the new HEAD
git commit -m "$message"

Archive Download the corresponding diff file

Branches