comparison hgmerge @ 1701:4ba8fe499df2

hgmerge: various cleanups Details: - put temporary file names into variables - make all temporary file names end with random part - cleanup FileMerge handling - do not use hardlinks in FileMerge change test (breaks on FAT) - try harder to keep file mtime unchanged in case of failed merge
author Radoslaw Szkodzinski <astralstorm@gorzow.mm.pl>
date Mon, 06 Feb 2006 17:32:10 -0600
parents e2f91e0acbb8
children 4eea6a747c27
comparison
equal deleted inserted replaced
1700:e2f91e0acbb8 1701:4ba8fe499df2
35 35
36 type $FILEMERGE >/dev/null 2>&1 || FILEMERGE= 36 type $FILEMERGE >/dev/null 2>&1 || FILEMERGE=
37 type $KDIFF3 >/dev/null 2>&1 || KDIFF3= 37 type $KDIFF3 >/dev/null 2>&1 || KDIFF3=
38 type $TKDIFF >/dev/null 2>&1 || TKDIFF= 38 type $TKDIFF >/dev/null 2>&1 || TKDIFF=
39 39
40 # random part of names
41 RAND="$RANDOM.$RANDOM.$RANDOM.$$"
42
40 # temporary directory for diff+patch merge 43 # temporary directory for diff+patch merge
41 HGTMP="${TMPDIR-/tmp}/hgmerge.$RANDOM.$RANDOM.$RANDOM.$$" 44 HGTMP="${TMPDIR-/tmp}/hgmerge.$RAND"
45
46 # backup file
47 BACKUP="$LOCAL.orig.$RAND"
48
49 # file used to test for file change
50 CHGTEST="$LOCAL.chg.$RAND"
42 51
43 # put all your required cleanup here 52 # put all your required cleanup here
44 cleanup() { 53 cleanup() {
45 rm -f "$LOCAL.orig" 54 rm -f "$BACKUP" "$CHGTEST"
46 rm -rf "$HGTMP" 55 rm -rf "$HGTMP"
47 } 56 }
48 57
49 # functions concerning program exit 58 # functions concerning program exit
50 success() { 59 success() {
52 exit 0 61 exit 0
53 } 62 }
54 63
55 failure() { 64 failure() {
56 echo "merge failed" 1>&2 65 echo "merge failed" 1>&2
57 cp "$LOCAL.orig" "$LOCAL" 66 mv "$BACKUP" "$LOCAL"
58 cleanup 67 cleanup
59 exit 1 68 exit 1
60 } 69 }
61 70
62 # Clean up when interrupted 71 # Clean up when interrupted
63 trap "failure" 1 2 3 6 15 # HUP INT QUIT ABRT TERM 72 trap "failure" 1 2 3 6 15 # HUP INT QUIT ABRT TERM
64 73
65 # Back up our file 74 # Back up our file (and try hard to keep the mtime unchanged)
66 cp "$LOCAL" "$LOCAL.orig" 75 mv "$LOCAL" "$BACKUP"
76 cp "$BACKUP" "$LOCAL"
67 77
68 # Attempt to do a non-interactive merge 78 # Attempt to do a non-interactive merge
69 if [ -n "$MERGE" ]; then 79 if [ -n "$MERGE" ]; then
70 $MERGE "$LOCAL" "$BASE" "$OTHER" 2> /dev/null && success 80 $MERGE "$LOCAL" "$BASE" "$OTHER" 2> /dev/null && success
71 cp "$LOCAL.orig" "$LOCAL" 81 cp "$BACKUP" "$LOCAL"
72 elif [ -n "$DIFF3" ]; then 82 elif [ -n "$DIFF3" ]; then
73 echo $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" 83 echo $DIFF3 -m "$BACKUP" "$BASE" "$OTHER"
74 $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" && success 84 $DIFF3 -m "$BACKUP" "$BASE" "$OTHER" > "$LOCAL" && success
75 if [ $? -eq 2 ]; then 85 if [ $? -eq 2 ]; then
76 echo "$DIFF3 failed! Exiting." 1>&2 86 echo "$DIFF3 failed! Exiting." 1>&2
77 cp "$LOCAL.orig" "$LOCAL" 87 cp "$BACKUP" "$LOCAL"
78 failure 88 failure
79 fi 89 fi
80 cp "$LOCAL.orig" "$LOCAL" 90 cp "$BACKUP" "$LOCAL"
81 fi 91 fi
82 92
83 # on MacOS X try FileMerge.app, shipped with Apple's developer tools 93 # on MacOS X try FileMerge.app, shipped with Apple's developer tools
84 # TODO: make proper temp files. foo.orig and foo.link are dangerous
85
86 if [ -n "$FILEMERGE" ]; then 94 if [ -n "$FILEMERGE" ]; then
87 cp "$LOCAL.orig" "$LOCAL" 95 cp "$BACKUP" "$LOCAL"
88 ln "$LOCAL" "$LOCAL.link" 96 cp "$BACKUP" "$CHGTEST"
89 # filemerge prefers the right by default 97 # filemerge prefers the right by default
90 if ! "$FILEMERGE" -left "$OTHER" -right "$LOCAL" -ancestor "$BASE" -merge "$LOCAL" 98 $FILEMERGE -left "$OTHER" -right "$LOCAL" -ancestor "$BASE" -merge "$LOCAL"
99 [ $? -ne 0 ] && echo "FileMerge failed to launch" && failure
100 if test "$LOCAL" -nt "$CHGTEST"
91 then 101 then
92 echo "FileMerge failed to launch"
93 failure
94 fi
95 if ! test "$LOCAL" -ef "$LOCAL.link"
96 then
97 rm "$LOCAL.orig" "$LOCAL.link"
98 success 102 success
99 else 103 else
100 rm "$LOCAL.link" 104 echo "$LOCAL seems unchanged. Was the merge successful?"
101 echo "$LOCAL is unchanged. Was the merge successful?"
102 select answer in yes no 105 select answer in yes no
103 do 106 do
104 if test "$answer" == "yes" 107 test "$answer" == "yes" && success || failure
105 then
106 rm "$LOCAL.orig"
107 success
108 else
109 failure
110 fi
111 done 108 done
112 failure
113 fi 109 fi
110 failure
114 fi 111 fi
115 112
116 if [ -n "$DISPLAY" ]; then 113 if [ -n "$DISPLAY" ]; then
117 # try using kdiff3, which is fairly nice 114 # try using kdiff3, which is fairly nice
118 if [ -n "$KDIFF3" ]; then 115 if [ -n "$KDIFF3" ]; then
134 success 131 success
135 fi 132 fi
136 133
137 if [ -n "$DIFF3" ]; then 134 if [ -n "$DIFF3" ]; then
138 echo "conflicts detected in $LOCAL" 135 echo "conflicts detected in $LOCAL"
139 $DIFF3 -m "$LOCAL.orig" "$BASE" "$OTHER" > "$LOCAL" || { 136 $DIFF3 -m "$BACKUP" "$BASE" "$OTHER" > "$LOCAL" || {
140 case $? in 137 case $? in
141 1) 138 1)
142 $EDITOR "$LOCAL" ;; 139 $EDITOR "$LOCAL" ;;
143 2) echo "$DIFF3 failed! Exiting." 1>&2 140 2) echo "$DIFF3 failed! Exiting." 1>&2
144 cp "$LOCAL.orig" "$LOCAL" 141 cp "$BACKUP" "$LOCAL"
145 failure ;; 142 failure ;;
146 esac 143 esac
147 success 144 success
148 } 145 }
149 fi 146 fi