tg-remote.sh: handle new bases location
[topgit/pro.git] / tg--index-merge-one-file.sh
blob7bf3de60d3fd82bfbb0e7cdc80e00649eb0cd9d2
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 if [ "$1" = "-h" ] && [ $# -eq 1 ]; then
18 echo "\
19 usage: ${tgname:-tg} index-merge-one-file <s1_hash> <s2_hash> <s3_hash> <path> <s1_mode> <s2_mode> <s3_mode>"
20 exit 0
23 [ $# -eq 7 ] || exit 1
25 # We only handle auto merging existing files with the same mode
27 case "${1:-:}${2:-:}${3:-:}${4:-:}${5:-:}${6:-:}${7:-:}" in *":"*) exit 1; esac
28 [ "$5" = "$6" ] && [ "$6" = "$7" ] || exit 1
30 # Check for "just-in-case" things that shouldn't get in here:
32 # a) all three hashes are the same (handled same as next case)
33 # b) $2 and $3 are the same
34 # c) $1 and $2 are the same
35 # d) $1 and $3 are the same
37 # For the "just-in-case"s it doesn't actually matter what the mode is
38 newhash=
39 if [ "$2" = "$3" ]; then
40 newhash="$2"
41 elif [ "$1" = "$2" ]; then
42 newhash="$3"
43 elif [ "$1" = "$3" ]; then
44 newhash="$2"
47 if [ -z "$newhash" ]; then
48 # mode must match 100\o\o\o
49 case "$6" in 100[0-7][0-7][0-7]);;*) exit 1; esac
50 if [ "$4" = ".topdeps" ] || [ "$4" = ".topmsg" ]; then
51 # resolution for these two is always silently "ours" never a merge
52 newhash="$2"
53 else
54 tg_tmp_dir="${TG_TMP_DIR:-/tmp}"
55 basef="$tg_tmp_dir/tgmerge_$$_base"
56 oursf="$tg_tmp_dir/tgmerge_$$_ours"
57 thrsf="$tg_tmp_dir/tgmerge_$$_thrs"
58 trap 'rm -f "$basef" "$oursf" "$thrsf"' EXIT
59 trap 'exit 129' HUP
60 trap 'exit 130' INT
61 trap 'exit 131' QUIT
62 trap 'exit 134' ABRT
63 trap 'exit 141' PIPE
64 trap 'exit 143' TERM
65 git cat-file blob "$1" >"$basef" || exit 1
66 git cat-file blob "$2" >"$oursf" || exit 1
67 git cat-file blob "$3" >"$thrsf" || exit 1
68 git merge-file --quiet "$oursf" "$basef" "$thrsf" >/dev/null 2>&1 || exit 1
69 printf '%s\n' "Auto-merging $4"
70 newhash="$(git hash-object -w --stdin <"$oursf" 2>/dev/null)"
74 [ -n "$newhash" ] || exit 1
75 git update-index --cacheinfo "$6" "$newhash" "$4"