This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-3way-merge
executable file
·232 lines (184 loc) · 4.85 KB
/
git-3way-merge
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
function parseArgs
{
mergeAll=false
revision="HEAD~1"
verbose=false
extra_verbose=false
finalSummary=""
needsManualMerge=()
while [[ $# > 0 ]]; do
key="$1"
case $key in
-a|--all)
mergeAll=true
;;
-h|--help)
printHelp
exit 0
;;
-r|--revision)
revision="$2"
shift
;;
-v|--verbose)
verbose=true
;;
-vv|--extra-verbose)
verbose=true
extra_verbose=true
;;
*)
echo -e "\n Invalid argument was supplied!\n"
printHelp
exit 1
;;
esac
shift
done
}
function merge_directory
{
if [ -d "$2" ]; then
local localDir=$1
local localDirLength=${#localDir}
local coreDir=$2
if $extra_verbose; then
echo -e "INFO:\t ------------------"
echo -e "INFO:\t Merging directory $2"
echo -e "INFO:\t into directory $1"
echo -e "INFO:\t ------------------"
fi
for current in $localDir/* ; do
local coreEquivalent=$coreDir${current:$localDirLength}
if [ -d "$current" ]; then
merge_directory "$current" "$coreEquivalent"
else
local oldFile="/tmp/tmp-merge-old-`basename \"$coreEquivalent\"`"
local newFile="/tmp/tmp-merge-new-`basename \"$coreEquivalent\"`"
if [ -f "$coreEquivalent" ]; then
git show $revision:$coreEquivalent > $oldFile
diff3 -m "$current" "$oldFile" "$coreEquivalent" > "$newFile"
if [ $? == 1 ]; then
if $verbose; then
echo -e "CONFLICT - needs manual merge!: $current"
fi
needsManualMerge+=("$current")
elif $verbose; then
echo -e "SUCCESS: Successfully merged '$current' with '$coreEquivalent'"
fi
cp $newFile $current
elif $extra_verbose; then
echo -e "INFO:\t Skipping file $current; no equivalent in core code."
fi
fi
done
elif $extra_verbose; then
echo -e "INFO:\t Skipping directory $2; no equivalent in core code."
fi
}
function summarizeConflicts
{
needsManualMergeSize=${#needsManualMerge[@]}
# Assume there will be no error
toReturn=0
echo -e "---------------------------------------------\n"
echo -e " There has been recurrently merged these dirs:"
echo -e "$finalSummary\n"
echo -e "---------------------------------------------\n"
if [[ $needsManualMergeSize > 0 ]]; then
toReturn=1
echo " There is a need for manual merge :))"
echo -e "\t Files needed to be merged are:\n"
for i in "${needsManualMerge[@]}"; do
echo -e "Conflict which has to be merged manually: '$i' "
done
echo
inquire "Do you wish to resolve all the conflicts with meld?" "y" "N"
case $answer in
y)
for i in "${needsManualMerge[@]}"; do
args+="--diff $i "
done
echo -e " Running meld with cmd:\nmeld $args&\n"
meld $args&
;;
*)
echo -e " Okay, not opening meld - but don't forget to merge these conflicts ;)"
;;
esac
else
echo " Everything went OK ! :)"
fi
echo ""
echo "Performed 3way diff against revision '$revision'"
echo ""
}
function printHelp
{
echo -e "Usage: git-3way-merge [options]\n"
echo -e " All options are optional:\n"
echo -e " -a, --all Does include directories specified in skippable array"
echo -e " -h, --help Prints this help"
echo -e " -r, --revision 'HEAD~n' This can be any string which specifies the git revision to perform the git show against"
echo -e " - for example passing 'HEAD~5' will do 'git show HEAD~5:fileToHasAsTheSecondOne'"
echo -e " -v, --verbose Include also SUCCESS outputs"
echo -e " -vv, --extra-verbose Include all the output possible (INFO & SUCCESS)"
echo
}
function callMerges
{
if $mergeAll; then
for key in "${!skippableMergeMappings[@]}"; do
local mergeMappingName="$key[@]"
local mergeMapping=( "${!mergeMappingName}" )
local dirToMergeWith=${skippableMergeMappings[$key]}
for dirToBeMerged in "${mergeMapping[@]}"; do
finalSummary+="\n $dirToBeMerged"
merge_directory $dirToBeMerged $dirToMergeWith
done
finalSummary+="\n"
done
fi
for key in "${!mergeMappings[@]}"; do
local mergeMappingName="$key[@]"
local mergeMapping=( "${!mergeMappingName}" )
local dirToMergeWith=${mergeMappings[$key]}
for dirToBeMerged in "${mergeMapping[@]}"; do
finalSummary+="\n $dirToBeMerged"
merge_directory $dirToBeMerged $dirToMergeWith
done
done
}
inquire () {
echo -e -n "$1 [$2/$3]? "
read answer
finish="-1"
while [ "$finish" = '-1' ]
do
finish="1"
if [ "$answer" = '' ];
then
answer=""
else
case $answer in
y | Y | yes | YES ) answer="y";;
n | N | no | NO ) answer="n";;
*) finish="-1";
echo -n 'Invalid response -- please reenter:';
read answer;;
esac
fi
done
}
function initialize
{
parseArgs $@
# Load the config file
source git-3way-merge.cfg
callMerges
summarizeConflicts
}
# Initialize with the incoming arguments
initialize $@
exit $toReturn