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# This hook has been specifically developed for two-way file
10# synchronization with git repositories. For example, after having
11# enabled it on a remote repository, you can use a script similar to
12# the following one on every repository you want to keep in sync:
13#
14# ----->8------
15# #! /bin/sh
16# # Synchronize the local repository with origin
17#
18# git add --all
19# git commit --allow-empty -m Autocommit
20# git push origin master:`hostname`
21# git pull --no-edit origin HEAD
22# -----8<------
23#
24# Copyright (c) 2014 Nicola Fontana <ntd at entidi.it>
25#
26# This file is licensed under the GPL v2, or a later version
27# at the discretion of Nicola Fontana or any other official
28# post-receive.automerge maintainer.
29
30
31unset GIT_DIR
32cd ..
33
34
35# Autocommit the local modifications, if any
36git add --all
37git commit -qm 'Commit from post-receive.automerge'
38
39
40# Get the name of the recently updated ref
41read dummy dummy refname
42[ -z "$refname" ] && exit 0
43
44
45# Merge $refname into HEAD
46git merge --no-commit $refname
47topdir=`git rev-parse --show-toplevel`
48
49
50# For each conflicting file, create a backup for the HEAD version and
51# substitute the original with the $refname version
52for f in `git diff --name-only --diff-filter=U`; do
53 f2=`mktemp -p "$topdir" "$f-conflict-XXXX"`
54 git checkout --ours "$f"
55 mv "$f" "$f2"
56 git checkout --theirs "$f"
57 git add "$f" "$f2"
58done
59
60
61# Commit the new HEAD
62git commit -C MERGE_HEAD

Archive Download this file

Branches