tg.sh: handle remote bases in new location
[topgit/pro.git] / tg--index-merge-one-file.sh
blob0a7c1bb90aa017cc032d690a05b893a2a0a4bb1f
1 #!/bin/sh
3 # tg--index-merge-one-file -- auto merge a file without touching working tree
4 # Copyright (C) 2017 Kyle J. McKay
5 # All rights reserved.
6 # License GPLv2+
8 # $TG_TMP_DIR => location to store temporary files
9 # $1 => stage 1 hash or empty
10 # $2 => stage 2 hash or empty
11 # $3 => stage 3 hash or empty
12 # $4 => full pathname in index
13 # $5 => stage 1 mode (6 octal digits) or empty
14 # $6 => stage 2 mode
15 # $7 => stage 3 mode
17 [ $# -eq 7 ] || exit 1
19 # We only handle auto merging existing files with the same mode
21 case "${1:-:}${2:-:}${3:-:}${4:-:}${5:-:}${6:-:}${7:-:}" in *":"*) exit 1; esac
22 [ "$5" = "$6" ] && [ "$6" = "$7" ] || exit 1
24 # Check for "just-in-case" things that shouldn't get in here:
26 # a) all three hashes are the same (handled same as next case)
27 # b) $2 and $3 are the same
28 # c) $1 and $2 are the same
29 # d) $1 and $3 are the same
31 # For the "just-in-case"s it doesn't actually matter what the mode is
32 newhash=
33 if [ "$2" = "$3" ]; then
34 newhash="$2"
35 elif [ "$1" = "$2" ]; then
36 newhash="$3"
37 elif [ "$1" = "$3" ]; then
38 newhash="$2"
41 if [ -z "$newhash" ]; then
42 # mode must match 100\o\o\o
43 case "$6" in 100[0-7][0-7][0-7]);;*) exit 1; esac
44 if [ "$4" = ".topdeps" ] || [ "$4" = ".topmsg" ]; then
45 # resolution for these two is always silently "ours" never a merge
46 newhash="$2"
47 else
48 tg_tmp_dir="${TG_TMP_DIR:-/tmp}"
49 basef="$tg_tmp_dir/tgmerge_$$_base"
50 oursf="$tg_tmp_dir/tgmerge_$$_ours"
51 thrsf="$tg_tmp_dir/tgmerge_$$_thrs"
52 trap 'rm -f "$basef" "$oursf" "$thrsf"' EXIT
53 trap 'exit 129' HUP
54 trap 'exit 130' INT
55 trap 'exit 131' QUIT
56 trap 'exit 134' ABRT
57 trap 'exit 141' PIPE
58 trap 'exit 143' TERM
59 git cat-file blob "$1" >"$basef" || exit 1
60 git cat-file blob "$2" >"$oursf" || exit 1
61 git cat-file blob "$3" >"$thrsf" || exit 1
62 git merge-file --quiet "$oursf" "$basef" "$thrsf" >/dev/null 2>&1 || exit 1
63 printf '%s\n' "Auto-merging $4"
64 newhash="$(git hash-object -w --stdin <"$oursf" 2>/dev/null)"
68 [ -n "$newhash" ] || exit 1
69 git update-index --cacheinfo "$6" "$newhash" "$4"