##// END OF EJS Templates
hg resolve: merge a given node into the working directory...
mpm@selenic.com -
r232:fc4a6e5b default
parent child Browse files
Show More
@@ -153,6 +153,14 def parents(ui, repo, node = None):
153 if n != hg.nullid:
153 if n != hg.nullid:
154 ui.write("%d:%s\n" % (repo.changelog.rev(n), hg.hex(n)))
154 ui.write("%d:%s\n" % (repo.changelog.rev(n), hg.hex(n)))
155
155
156 def resolve(ui, repo, node = None):
157 '''merge a given node or the current tip into the working dir'''
158 if not node:
159 node = repo.changelog.tip()
160 else:
161 node = repo.lookup(node)
162 repo.resolve(node)
163
156 def status(ui, repo):
164 def status(ui, repo):
157 '''show changed files in the working directory
165 '''show changed files in the working directory
158
166
@@ -184,6 +192,7 table = {
184 ('c', 'changeset', None, 'show changeset')],
192 ('c', 'changeset', None, 'show changeset')],
185 'hg annotate [-u] [-c] [-n] [-r id] [files]'),
193 'hg annotate [-u] [-c] [-n] [-r id] [files]'),
186 "parents": (parents, [], 'hg parents [node]'),
194 "parents": (parents, [], 'hg parents [node]'),
195 "resolve": (resolve, [], 'hg resolve [node]'),
187 "status": (status, [], 'hg status'),
196 "status": (status, [], 'hg status'),
188 "undo": (undo, [], 'hg undo'),
197 "undo": (undo, [], 'hg undo'),
189 }
198 }
@@ -858,174 +858,110 class localrepository:
858 tr.close()
858 tr.close()
859 return
859 return
860
860
861 def merge(self, generator):
861 def resolve(self, node):
862 changesets = files = revisions = 0
862 pl = self.dirstate.parents()
863
863 if pl[1] != nullid:
864 self.lock()
864 self.ui.warn("last merge not committed")
865 class genread:
865 return
866 def __init__(self, generator):
867 self.g = generator
868 self.buf = ""
869 def read(self, l):
870 while l > len(self.buf):
871 try:
872 self.buf += self.g.next()
873 except StopIteration:
874 break
875 d, self.buf = self.buf[:l], self.buf[l:]
876 return d
877
878 if not generator: return
879 source = genread(generator)
880
881 def getchunk():
882 d = source.read(4)
883 if not d: return ""
884 l = struct.unpack(">l", d)[0]
885 if l <= 4: return ""
886 return source.read(l - 4)
887
866
888 def getgroup():
867 p1, p2 = pl[0], node
889 while 1:
868 m1n = self.changelog.read(p1)[0]
890 c = getchunk()
869 m2n = self.changelog.read(p2)[0]
891 if not c: break
870 man = self.manifest.ancestor(m1n, m2n)
892 yield c
871 m1 = self.manifest.read(m1n)
893
872 m2 = self.manifest.read(m2n)
894 tr = self.transaction()
873 ma = self.manifest.read(man)
895 simple = True
896 need = {}
897
898 self.ui.status("adding changesets\n")
899 # pull off the changeset group
900 def report(x):
901 self.ui.debug("add changeset %s\n" % short(x))
902 return self.changelog.count()
903
874
904 co = self.changelog.tip()
875 (c, a, d, u) = self.diffdir(self.root)
905 cn = self.changelog.addgroup(getgroup(), report, tr)
906
907 changesets = self.changelog.rev(cn) - self.changelog.rev(co)
908
909 self.ui.status("adding manifests\n")
910 # pull off the manifest group
911 mm = self.manifest.tip()
912 mo = self.manifest.addgroup(getgroup(),
913 lambda x: self.changelog.rev(x), tr)
914
915 # do we need a resolve?
916 if self.changelog.ancestor(co, cn) != co:
917 simple = False
918 resolverev = self.changelog.count()
919
876
920 # resolve the manifest to determine which files
877 # resolve the manifest to determine which files
921 # we care about merging
878 # we care about merging
922 self.ui.status("resolving manifests\n")
879 self.ui.status("resolving manifests\n")
923 ma = self.manifest.ancestor(mm, mo)
880 self.ui.debug(" ancestor %s local %s remote %s\n" %
924 omap = self.manifest.read(mo) # other
881 (short(man), short(m1n), short(m2n)))
925 amap = self.manifest.read(ma) # ancestor
926 mmap = self.manifest.read(mm) # mine
927 nmap = {}
928
882
929 self.ui.debug(" ancestor %s local %s remote %s\n" %
883 merge = {}
930 (short(ma), short(mm), short(mo)))
884 get = {}
885 remove = []
931
886
932 for f, mid in mmap.iteritems():
887 # construct a working dir manifest
933 if f in omap:
888 mw = m1.copy()
934 if mid != omap[f]:
889 for f in a + c:
890 mw[f] = nullid
891 for f in d:
892 del mw[f]
893
894 for f, n in mw.iteritems():
895 if f in m2:
896 if n != m2[f]:
935 self.ui.debug(" %s versions differ, do resolve\n" % f)
897 self.ui.debug(" %s versions differ, do resolve\n" % f)
936 need[f] = mid # use merged version or local version
898 merge[f] = (m1.get(f, nullid), m2[f])
937 else:
899 del m2[f]
938 nmap[f] = mid # keep ours
900 elif f in ma:
939 del omap[f]
901 if n != ma[f]:
940 elif f in amap:
941 if mid != amap[f]:
942 r = self.ui.prompt(
902 r = self.ui.prompt(
943 (" local changed %s which remote deleted\n" % f) +
903 (" local changed %s which remote deleted\n" % f) +
944 "(k)eep or (d)elete?", "[kd]", "k")
904 "(k)eep or (d)elete?", "[kd]", "k")
945 if r == "k": nmap[f] = mid
905 if r == "d":
906 remove.append(f)
946 else:
907 else:
947 self.ui.debug("other deleted %s\n" % f)
908 self.ui.debug("other deleted %s\n" % f)
948 pass # other deleted it
909 pass # other deleted it
949 else:
910 else:
950 self.ui.debug("local created %s\n" %f)
911 self.ui.debug("local created %s\n" %f)
951 nmap[f] = mid # we created it
952
912
953 del mmap
913 for f, n in m2.iteritems():
954
914 if f in ma:
955 for f, oid in omap.iteritems():
915 if n != ma[f]:
956 if f in amap:
957 if oid != amap[f]:
958 r = self.ui.prompt(
916 r = self.ui.prompt(
959 ("remote changed %s which local deleted\n" % f) +
917 ("remote changed %s which local deleted\n" % f) +
960 "(k)eep or (d)elete?", "[kd]", "k")
918 "(k)eep or (d)elete?", "[kd]", "k")
961 if r == "k": nmap[f] = oid
919 if r == "d": remove.append(f)
962 else:
920 else:
963 pass # probably safe
921 pass # probably safe
964 else:
922 else:
965 self.ui.debug("remote created %s, do resolve\n" % f)
923 self.ui.debug("remote created %s, do resolve\n" % f)
966 need[f] = oid
924 get[f] = n
967
925
968 del omap
926 del mw, m1, m2, ma
969 del amap
970
927
971 new = need.keys()
928 self.dirstate.setparents(p1, p2)
972 new.sort()
973
929
974 # process the files
930 # get the files we don't need to change
975 self.ui.status("adding files\n")
931 files = get.keys()
976 while 1:
932 files.sort()
977 f = getchunk()
933 for f in files:
978 if not f: break
934 if f[0] == "/": continue
979 self.ui.debug("adding %s revisions\n" % f)
935 self.ui.note(f, "\n")
980 fl = self.file(f)
936 t = self.file(f).revision(get[f])
981 o = fl.tip()
937 try:
982 n = fl.addgroup(getgroup(), lambda x: self.changelog.rev(x), tr)
938 file(f, "w").write(t)
983 revisions += fl.rev(n) - fl.rev(o)
939 except IOError:
984 files += 1
940 os.makedirs(os.path.dirname(f))
985 if f in need:
941 file(f, "w").write(t)
986 del need[f]
987 # manifest resolve determined we need to merge the tips
988 nmap[f] = self.merge3(fl, f, o, n, tr, resolverev)
989
990 if need:
991 # we need to do trivial merges on local files
992 for f in new:
993 if f not in need: continue
994 fl = self.file(f)
995 nmap[f] = self.merge3(fl, f, need[f], fl.tip(), tr, resolverev)
996 revisions += 1
997
942
998 # For simple merges, we don't need to resolve manifests or changesets
943 # we have to remember what files we needed to get/change
999 if simple:
944 # because any file that's different from either one of its
1000 self.ui.debug("simple merge, skipping resolve\n")
945 # parents must be in the changeset
1001 self.ui.status(("modified %d files, added %d changesets" +
946 self.dirstate.update(files, 'm')
1002 " and %d new revisions\n")
1003 % (files, changesets, revisions))
1004 tr.close()
1005 return
1006
1007 node = self.manifest.add(nmap, tr, resolverev, mm, mo)
1008 revisions += 1
1009
947
1010 # Now all files and manifests are merged, we add the changed files
948 # merge the tricky bits
1011 # and manifest id to the changelog
949 files = merge.keys()
1012 self.ui.status("committing merge changeset\n")
950 files.sort()
1013 if co == cn: cn = -1
951 for f in files:
952 m, o = merge[f]
953 self.merge3(f, m, o)
1014
954
1015 edittext = "\nHG: merge resolve\n" + \
955 # same here
1016 "HG: manifest hash %s\n" % hex(node) + \
956 self.dirstate.update(files, 'm')
1017 "".join(["HG: changed %s\n" % f for f in new])
1018 edittext = self.ui.edit(edittext)
1019 n = self.changelog.add(node, new, edittext, tr, co, cn)
1020 revisions += 1
1021
957
1022 self.ui.status("added %d changesets, %d files, and %d new revisions\n"
958 for f in remove:
1023 % (changesets, files, revisions))
959 self.ui.note("removing %s\n" % f)
960 #os.unlink(f)
961 self.dirstate.update(remove, 'r')
1024
962
1025 tr.close()
963 def merge3(self, fn, my, other):
1026
964 """perform a 3-way merge in the working directory"""
1027 def merge3(self, fl, fn, my, other, transaction, link):
1028 """perform a 3-way merge and append the result"""
1029
965
1030 def temp(prefix, node):
966 def temp(prefix, node):
1031 pre = "%s~%s." % (os.path.basename(fn), prefix)
967 pre = "%s~%s." % (os.path.basename(fn), prefix)
@@ -1035,31 +971,24 class localrepository:
1035 f.close()
971 f.close()
1036 return name
972 return name
1037
973
974 fl = self.file(fn)
1038 base = fl.ancestor(my, other)
975 base = fl.ancestor(my, other)
1039 self.ui.note("resolving %s\n" % fn)
976 a = fn
1040 self.ui.debug("local %s remote %s ancestor %s\n" %
977 b = temp("other", other)
1041 (short(my), short(other), short(base)))
978 c = temp("base", base)
1042
979
1043 if my == base:
980 self.ui.note("resolving %s\n" % fn)
1044 text = fl.revision(other)
981 self.ui.debug("file %s: other %s ancestor %s\n" %
1045 else:
982 (fn, short(other), short(base)))
1046 a = temp("local", my)
1047 b = temp("remote", other)
1048 c = temp("parent", base)
1049
983
1050 cmd = os.environ["HGMERGE"]
984 cmd = os.environ["HGMERGE"]
1051 self.ui.debug("invoking merge with %s\n" % cmd)
1052 r = os.system("%s %s %s %s %s" % (cmd, a, b, c, fn))
985 r = os.system("%s %s %s %s %s" % (cmd, a, b, c, fn))
1053 if r:
986 if r:
1054 raise "Merge failed!"
987 self.ui.warn("merging %s failed!\n" % f)
1055
988
1056 text = open(a).read()
1057 os.unlink(a)
1058 os.unlink(b)
989 os.unlink(b)
1059 os.unlink(c)
990 os.unlink(c)
1060
991
1061 return fl.add(text, transaction, link, my, other)
1062
1063 class remoterepository:
992 class remoterepository:
1064 def __init__(self, ui, path):
993 def __init__(self, ui, path):
1065 self.url = path
994 self.url = path
General Comments 0
You need to be logged in to leave comments. Login now