git-hacks

git-hacks Git Source Tree

Root/post-receive.automerge

1#! /bin/sh
2#
3# This is intended to be run inside a remote (non-bare) repository.
4# It merges the latest pushed ref into HEAD (that is the local working
5# tree), automatically resolving conflicts by creating a copy of each
6# conflicting file. To be able to do this, HEAD must not be in a
7# detached state.
8#
9# It can be used in a two-way file synchronization process on multiple
10# repositories. For example, on every local repository you can use a
11# script similar to the following one:
12#
13# ----->8------
14# #! /bin/sh
15# # Synchronize the local repository with origin
16#
17# git add --all
18# git commit --allow-empty -m Autocommit
19# git push origin master:`hostname`
20# git pull origin HEAD
21# -----8<------
22#
23# Copyright (c) 2014 Nicola Fontana <ntd at entidi.it>
24#
25# This file is licensed under the GPL v2, or a later version
26# at the discretion of Nicola Fontana or any other official
27# post-receive.automerge maintainer.
28
29
30message='Commit from post-receive.automerge'
31unset GIT_DIR
32cd ..
33
34
35# Get the name of the recently updated ref
36read dummy dummy refname
37[ -z "$refname" ] && exit 0
38
39
40# Merge $refname into HEAD
41git merge --no-commit $refname
42topdir=`git rev-parse --show-toplevel`
43
44
45# For each conflicting file, create a backup for the HEAD version and
46# substitute the original with the $refname version
47for f in `git diff --name-only --diff-filter=U`; do
48 f2=`mktemp -p "$topdir" "$f-conflict-XXXX"`
49 git checkout --ours "$f"
50 mv "$f" "$f2"
51 git checkout --theirs "$f"
52 git add "$f" "$f2"
53done
54
55
56# Commit the new HEAD
57git commit -m "$message"

Archive Download this file

Branches