-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadd-with-mergetool
executable file
·110 lines (95 loc) · 2.99 KB
/
add-with-mergetool
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
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
#
# add-with-mergetool
# Author: Stuart Berg (http://github.com/stuarteberg)
#
# This little script is like 'git add --patch', except that
# it launches a merge-tool to perform the merge.
# TODO: For now, this script hard-codes MERGETOOL and MERGECMD for kdiff3.
# Modify those variables for your own tool if you wish.
# In the future, it would be nice if we could somehow read
# MERGETOOL and MERGECMD from the user's git-config.
# Configure for kdiff3
# (and hide warnings on about modalSession, from kdiff3 on OSX)
MERGETOOL=${MERGETOOL-kdiff3}
MERGECMD=${MERGECMD-'"${MERGETOOL}" "${LOCAL}" "${REMOTE}" -o "${MERGED}"'\
2>&1 | grep -iv modalSession}
main() {
check_for_errors "$@"
process_all "$@"
}
check_for_errors() {
which "${MERGETOOL}" > /dev/null
if [[ $? == 1 ]]; then
echo "Error: Can't find mergetool: '${MERGETOOL}'" 1>&2
exit 1
fi
if [[ "$1" == "-h" ]]; then
echo "Usage: $(basename $0) [<pathspec>...]" 1>&2
exit 0
fi
# Exit early if we're not in a git repo
git status > /dev/null || exit $?
}
process_all() {
repo_toplevel=$(git rev-parse --show-toplevel)
# If no args given, add everything (like 'git add -p')
if [[ $# == 0 ]]; then
set -- "$repo_toplevel"
fi
# For each given file/directory...
args=( "$@" )
for arg in "${args[@]}"
do
# Find the modified file(s)
changed_files=( $(git diff --name-only -- "$arg") )
(
# Switch to toplevel, to easily handle 'git diff' output
cd "$repo_toplevel"
# For each modified file...
for f in "${changed_files[@]}"
do
if [[ $startmsg_shown != "yes" ]]; then
echo "Starting $(basename $0). Use Ctrl+C to stop early."
echo "To skip a file, quit ${MERGETOOL} without saving."
echo
startmsg_shown="yes"
fi
# This is where the magic happens.
patch_file_and_add "$f"
done
) || exit $? # exit early if loop body failed
done
}
# This helper function launches the mergetool for a single file,
# and then adds it to the git index (if the user saved the new file).
patch_file_and_add() {
f="$1"
git show :"$f" > "$f.from_index" # Copy from the index
(
set -e
trap "echo && exit 130" INT # Ctrl+C should trigger abnormal exit
# Execute 2-file merge
echo "Launching ${MERGETOOL} for '$f'."
LOCAL="$f.from_index"
REMOTE="$f"
MERGED="$f.to_add"
eval "${MERGECMD}"
if [[ -e "$f.to_add" ]]; then
mv "$f" "$f.from_working" # Backup original from working-tree
mv "$f.to_add" "$f" # Replace with patched version
git add "$f" # Add to the index
mv "$f.from_working" "$f" # Restore the working-tree version
fi
)
status=$?
rm "$f.from_index" # Discard the old index version
if [ $status == 130 ]; then
echo "User interrupted." 1>&2
exit $status
elif [ $status != 0 ]; then
echo "Error: Interactive add-patch stopped early!" 1>&2
exit $status
fi
}
main "$@"