-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-crypt-merge.sh
executable file
·99 lines (83 loc) · 2.13 KB
/
git-crypt-merge.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
#
# git-crypt-merge: helper script for merging changes in git-crypt encrypted
# files.
#
set -e
set -o nounset
PROGNAME=$(basename $0)
usage() {
cat <<EOF
usage: ${PROGNAME} -b BASE -c CURRENT -o OTHER -k KEY_NAME -l LOCATION
-h, --help Display this help
EOF
}
if [[ $# -lt 10 ]]; then
echo "${PROGNAME}: not enough arguments" >&2
usage >&2
exit 1
fi
BASE=""
CURRENT=""
OTHER=""
KEY_NAME=""
LOCATION=""
while [[ $# -gt 0 ]]; do
case $1 in
--help|-h)
usage
exit 0
;;
--base|-b)
BASE="$2"
shift
shift
;;
--current|-c)
CURRENT="$2"
shift
shift
;;
--other|-o)
OTHER="$2"
shift
shift
;;
--key_name|-k)
KEY_NAME="$2"
shift
shift
;;
--location|-l)
LOCATION="$2"
shift
shift
;;
*)
echo "${PROGNAME}: unknown option $1" >&2
usage >&2
exit 1
;;
esac
done
# Temporary file names to store decrypted files
base_decrypted="$(mktemp)"
current_decrypted="$(mktemp)"
other_decrypted="$(mktemp)"
trap "{ rm -f $base_decrypted; }" EXIT
trap "{ rm -f $current_decrypted; }" EXIT
trap "{ rm -f $other_decrypted; }" EXIT
echo "Starting with git-crypt-merge at ${LOCATION}...."
echo "DEBUG: Decrypting base...."
cat ${BASE} | git-crypt smudge --key-name=${KEY_NAME} > "${base_decrypted}"
echo "DEBUG: Decrypting current...."
cat ${CURRENT} | git-crypt smudge --key-name=${KEY_NAME} > "${current_decrypted}"
echo "DEBUG: Decrypting other...."
cat ${OTHER} | git-crypt smudge --key-name=${KEY_NAME} > "${other_decrypted}"
echo "Trying auto-merge...."
if ! git merge-file -L CURRENT -L BASE -L OTHER ${current_decrypted} ${base_decrypted} ${other_decrypted}; then
echo "Merge conflict; opening editor to resolve." >&2
${EDITOR:-vi} ${current_decrypted}
fi
# Re-encrypt the file
cat "${current_decrypted}" | git-crypt clean --key-name=${KEY_NAME} > $CURRENT