From dc8d497a298165ec954a5ff550cd97c79dde1a8a Mon Sep 17 00:00:00 2001 From: VG Date: Thu, 11 May 2017 10:01:40 +0200 Subject: add more scripts --- scripts/git-sync | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 scripts/git-sync (limited to 'scripts/git-sync') diff --git a/scripts/git-sync b/scripts/git-sync new file mode 100755 index 0000000..deb19bc --- /dev/null +++ b/scripts/git-sync @@ -0,0 +1,74 @@ +#!/bin/bash +set -e + +hosthash=$(python3 -c "import uuid, hashlib +print(hashlib.new('sha1', str(uuid.getnode()).encode('utf8')).hexdigest())") + +if [ -d "$(git rev-parse --git-dir)/annex" ]; then + echo 'Sync: using git annex' + exec git annex sync "$@" +fi + +# determine current branch name +branch_name=$(git symbolic-ref -q --short HEAD) + +# remote name +remote_name=$(git config --get branch.${branch_name}.remote || true) + +# conflict commit message +conflict_message="Merge conflict autocommit on ${hosthash}" + +if [ -z "$remote_name" ]; then + echo "No remote for this repository" >&2 + exit 1 +fi + +if [ "$(git config --get --bool branch.${branch_name}.sync)" != "true" ]; then + echo "Branch is not configured to accept git-sync" >&2 + echo "To accept git-sync operation, set current branch sync to true:" >&2 + echo " git config --bool branch.${branch_name}.sync true" >&2 + exit 1 +fi + +echo 'Sync: git autocommit' +git add -A "./$(git rev-parse --show-cdup)" +git commit -m "Auto-commit on ${hosthash}" >/dev/null 2>&1 || true + +echo 'Sync: fetch and merge' + +git fetch + +# might fail one time if conflict detected +git merge -m "$conflict_message" || true + +echo 'Sync: conflict autocommit' + +git status --porcelain | while read -r filename; do + state="${filename:0:2}" + filename="${filename:3}" + ours="${filename}.${hosthash}.conflict" + theirs="${filename}.${remote_name}.conflict" + case "$state" in + *U*) + git checkout --ours -- "$filename" + mv -i -- "$filename" "$ours" + git add -- "$ours" + git checkout --theirs -- "$filename" + cp -ai -- "$filename" "$theirs" + git add -- "$filename" + git add -- "$theirs" + ;; + esac +done + +# no action is done if there is nothing to commit +git commit -m "$conflict_message" || true + +# should not fail again +git merge -m "$conflict_message" + +echo 'Sync: pushing changes' + +git push + +echo 'Sync: done' -- cgit v1.2.3