hg.py
955 lines
| 28.5 KiB
| text/x-python
|
PythonLexer
/ mercurial / hg.py
|
r0 | # hg.py - repository classes for mercurial | ||
# | ||||
# Copyright 2005 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
# This software may be used and distributed according to the terms | ||||
# of the GNU General Public License, incorporated herein by reference. | ||||
|
r37 | import sys, struct, sha, socket, os, time, base64, re, urllib2 | ||
|
r15 | import urllib | ||
|
r0 | from mercurial import byterange | ||
from mercurial.transaction import * | ||||
from mercurial.revlog import * | ||||
|
r79 | from difflib import SequenceMatcher | ||
|
r0 | |||
class filelog(revlog): | ||||
def __init__(self, opener, path): | ||||
s = self.encodepath(path) | ||||
revlog.__init__(self, opener, os.path.join("data", s + "i"), | ||||
os.path.join("data", s)) | ||||
def encodepath(self, path): | ||||
s = sha.sha(path).digest() | ||||
s = base64.encodestring(s)[:-3] | ||||
s = re.sub("\+", "%", s) | ||||
s = re.sub("/", "_", s) | ||||
return s | ||||
def read(self, node): | ||||
return self.revision(node) | ||||
def add(self, text, transaction, link, p1=None, p2=None): | ||||
return self.addrevision(text, transaction, link, p1, p2) | ||||
def resolvedag(self, old, new, transaction, link): | ||||
"""resolve unmerged heads in our DAG""" | ||||
if old == new: return None | ||||
a = self.ancestor(old, new) | ||||
|
r48 | if old == a: return None | ||
|
r0 | return self.merge3(old, new, a, transaction, link) | ||
def merge3(self, my, other, base, transaction, link): | ||||
"""perform a 3-way merge and append the result""" | ||||
def temp(prefix, node): | ||||
(fd, name) = tempfile.mkstemp(prefix) | ||||
f = os.fdopen(fd, "w") | ||||
f.write(self.revision(node)) | ||||
f.close() | ||||
return name | ||||
a = temp("local", my) | ||||
b = temp("remote", other) | ||||
c = temp("parent", base) | ||||
cmd = os.environ["HGMERGE"] | ||||
r = os.system("%s %s %s %s" % (cmd, a, b, c)) | ||||
if r: | ||||
raise "Merge failed, implement rollback!" | ||||
t = open(a).read() | ||||
os.unlink(a) | ||||
os.unlink(b) | ||||
os.unlink(c) | ||||
return self.addrevision(t, transaction, link, my, other) | ||||
def merge(self, other, transaction, linkseq, link): | ||||
"""perform a merge and resolve resulting heads""" | ||||
(o, n) = self.mergedag(other, transaction, linkseq) | ||||
return self.resolvedag(o, n, transaction, link) | ||||
|
r79 | def annotate(self, node): | ||
revs = [] | ||||
while node != nullid: | ||||
revs.append(node) | ||||
node = self.parents(node)[0] | ||||
revs.reverse() | ||||
prev = [] | ||||
annotate = [] | ||||
for node in revs: | ||||
curr = self.read(node).splitlines(1) | ||||
linkrev = self.linkrev(node) | ||||
sm = SequenceMatcher(None, prev, curr) | ||||
offset = 0 | ||||
for o, m, n, s, t in sm.get_opcodes(): | ||||
if o in ('insert','replace'): | ||||
annotate[m+offset:n+offset] = \ | ||||
[ (linkrev, l) for l in curr[s:t]] | ||||
if o == 'insert': | ||||
offset += m-n | ||||
elif o == 'delete': | ||||
del annotate[m+offset:n+offset] | ||||
offset -= m-n | ||||
assert len(annotate) == len(curr) | ||||
prev = curr | ||||
return annotate | ||||
|
r0 | class manifest(revlog): | ||
def __init__(self, opener): | ||||
self.mapcache = None | ||||
self.listcache = None | ||||
self.addlist = None | ||||
revlog.__init__(self, opener, "00manifest.i", "00manifest.d") | ||||
def read(self, node): | ||||
if self.mapcache and self.mapcache[0] == node: | ||||
return self.mapcache[1] | ||||
text = self.revision(node) | ||||
map = {} | ||||
|
r25 | self.listcache = (text, text.splitlines(1)) | ||
for l in self.listcache[1]: | ||||
|
r0 | (f, n) = l.split('\0') | ||
map[f] = bin(n[:40]) | ||||
self.mapcache = (node, map) | ||||
return map | ||||
def diff(self, a, b): | ||||
# this is sneaky, as we're not actually using a and b | ||||
|
r25 | if self.listcache and len(self.listcache[0]) == len(a): | ||
return mdiff.diff(self.listcache[1], self.addlist, 1) | ||||
|
r0 | else: | ||
|
r44 | return mdiff.textdiff(a, b) | ||
|
r0 | |||
def add(self, map, transaction, link, p1=None, p2=None): | ||||
files = map.keys() | ||||
files.sort() | ||||
self.addlist = ["%s\000%s\n" % (f, hex(map[f])) for f in files] | ||||
text = "".join(self.addlist) | ||||
n = self.addrevision(text, transaction, link, p1, p2) | ||||
self.mapcache = (n, map) | ||||
|
r25 | self.listcache = (text, self.addlist) | ||
|
r0 | |||
return n | ||||
class changelog(revlog): | ||||
def __init__(self, opener): | ||||
revlog.__init__(self, opener, "00changelog.i", "00changelog.d") | ||||
def extract(self, text): | ||||
|
r37 | if not text: | ||
|
r40 | return (nullid, "", "0", [], "") | ||
|
r0 | last = text.index("\n\n") | ||
desc = text[last + 2:] | ||||
l = text[:last].splitlines() | ||||
manifest = bin(l[0]) | ||||
user = l[1] | ||||
date = l[2] | ||||
files = l[3:] | ||||
return (manifest, user, date, files, desc) | ||||
def read(self, node): | ||||
return self.extract(self.revision(node)) | ||||
def add(self, manifest, list, desc, transaction, p1=None, p2=None): | ||||
|
r55 | user = (os.environ.get("HGUSER") or | ||
os.environ.get("EMAIL") or | ||||
os.environ.get("LOGNAME", "unknown") + '@' + socket.getfqdn()) | ||||
|
r0 | date = "%d %d" % (time.time(), time.timezone) | ||
list.sort() | ||||
l = [hex(manifest), user, date] + list + ["", desc] | ||||
text = "\n".join(l) | ||||
return self.addrevision(text, transaction, self.count(), p1, p2) | ||||
def merge3(self, my, other, base): | ||||
pass | ||||
class dircache: | ||||
|
r20 | def __init__(self, opener, ui): | ||
|
r0 | self.opener = opener | ||
self.dirty = 0 | ||||
|
r20 | self.ui = ui | ||
|
r0 | self.map = None | ||
def __del__(self): | ||||
if self.dirty: self.write() | ||||
def __getitem__(self, key): | ||||
try: | ||||
return self.map[key] | ||||
except TypeError: | ||||
self.read() | ||||
return self[key] | ||||
def read(self): | ||||
if self.map is not None: return self.map | ||||
self.map = {} | ||||
try: | ||||
st = self.opener("dircache").read() | ||||
except: return | ||||
pos = 0 | ||||
while pos < len(st): | ||||
e = struct.unpack(">llll", st[pos:pos+16]) | ||||
l = e[3] | ||||
pos += 16 | ||||
f = st[pos:pos + l] | ||||
self.map[f] = e[:3] | ||||
pos += l | ||||
def update(self, files): | ||||
if not files: return | ||||
self.read() | ||||
self.dirty = 1 | ||||
for f in files: | ||||
try: | ||||
s = os.stat(f) | ||||
self.map[f] = (s.st_mode, s.st_size, s.st_mtime) | ||||
except IOError: | ||||
self.remove(f) | ||||
def taint(self, files): | ||||
if not files: return | ||||
self.read() | ||||
self.dirty = 1 | ||||
for f in files: | ||||
self.map[f] = (0, -1, 0) | ||||
def remove(self, files): | ||||
if not files: return | ||||
self.read() | ||||
self.dirty = 1 | ||||
for f in files: | ||||
|
r20 | try: | ||
del self.map[f] | ||||
except KeyError: | ||||
self.ui.warn("Not in dircache: %s\n" % f) | ||||
pass | ||||
|
r0 | |||
def clear(self): | ||||
self.map = {} | ||||
self.dirty = 1 | ||||
def write(self): | ||||
st = self.opener("dircache", "w") | ||||
for f, e in self.map.items(): | ||||
e = struct.pack(">llll", e[0], e[1], e[2], len(f)) | ||||
st.write(e + f) | ||||
self.dirty = 0 | ||||
def copy(self): | ||||
self.read() | ||||
return self.map.copy() | ||||
# used to avoid circular references so destructors work | ||||
def opener(base): | ||||
p = base | ||||
def o(path, mode="r"): | ||||
|
r15 | if p[:7] == "http://": | ||
f = os.path.join(p, urllib.quote(path)) | ||||
return httprangereader(f) | ||||
|
r0 | f = os.path.join(p, path) | ||
if mode != "r" and os.path.isfile(f): | ||||
s = os.stat(f) | ||||
if s.st_nlink > 1: | ||||
file(f + ".tmp", "w").write(file(f).read()) | ||||
os.rename(f+".tmp", f) | ||||
return file(f, mode) | ||||
return o | ||||
|
r60 | class localrepository: | ||
|
r0 | def __init__(self, ui, path=None, create=0): | ||
self.remote = 0 | ||||
if path and path[:7] == "http://": | ||||
self.remote = 1 | ||||
self.path = path | ||||
else: | ||||
if not path: | ||||
p = os.getcwd() | ||||
while not os.path.isdir(os.path.join(p, ".hg")): | ||||
p = os.path.dirname(p) | ||||
if p == "/": raise "No repo found" | ||||
path = p | ||||
self.path = os.path.join(path, ".hg") | ||||
self.root = path | ||||
self.ui = ui | ||||
if create: | ||||
os.mkdir(self.path) | ||||
os.mkdir(self.join("data")) | ||||
self.opener = opener(self.path) | ||||
self.manifest = manifest(self.opener) | ||||
self.changelog = changelog(self.opener) | ||||
self.ignorelist = None | ||||
|
r67 | self.tags = None | ||
|
r0 | |||
if not self.remote: | ||||
|
r20 | self.dircache = dircache(self.opener, ui) | ||
|
r0 | try: | ||
|
r4 | self.current = bin(self.opener("current").read()) | ||
except IOError: | ||||
|
r0 | self.current = None | ||
def setcurrent(self, node): | ||||
self.current = node | ||||
self.opener("current", "w").write(hex(node)) | ||||
def ignore(self, f): | ||||
if self.ignorelist is None: | ||||
self.ignorelist = [] | ||||
try: | ||||
|
r67 | l = open(os.path.join(self.root, ".hgignore")) | ||
|
r0 | for pat in l: | ||
|
r9 | if pat != "\n": | ||
self.ignorelist.append(re.compile(pat[:-1])) | ||||
|
r0 | except IOError: pass | ||
for pat in self.ignorelist: | ||||
if pat.search(f): return True | ||||
return False | ||||
|
r67 | def lookup(self, key): | ||
if self.tags is None: | ||||
self.tags = {} | ||||
try: | ||||
fl = self.file(".hgtags") | ||||
for l in fl.revision(fl.tip()).splitlines(): | ||||
if l: | ||||
n, k = l.split(" ") | ||||
self.tags[k] = bin(n) | ||||
except KeyError: pass | ||||
try: | ||||
return self.tags[key] | ||||
except KeyError: | ||||
return self.changelog.lookup(key) | ||||
|
r0 | def join(self, f): | ||
return os.path.join(self.path, f) | ||||
def file(self, f): | ||||
return filelog(self.opener, f) | ||||
def transaction(self): | ||||
return transaction(self.opener, self.join("journal")) | ||||
def merge(self, other): | ||||
tr = self.transaction() | ||||
changed = {} | ||||
new = {} | ||||
|
r10 | seqrev = self.changelog.count() | ||
# some magic to allow fiddling in nested scope | ||||
nextrev = [seqrev] | ||||
|
r0 | |||
# helpers for back-linking file revisions to local changeset | ||||
# revisions so we can immediately get to changeset from annotate | ||||
def accumulate(text): | ||||
# track which files are added in which changeset and the | ||||
# corresponding _local_ changeset revision | ||||
files = self.changelog.extract(text)[3] | ||||
for f in files: | ||||
|
r10 | changed.setdefault(f, []).append(nextrev[0]) | ||
nextrev[0] += 1 | ||||
|
r0 | |||
def seq(start): | ||||
while 1: | ||||
yield start | ||||
start += 1 | ||||
def lseq(l): | ||||
for r in l: | ||||
yield r | ||||
# begin the import/merge of changesets | ||||
self.ui.status("merging new changesets\n") | ||||
(co, cn) = self.changelog.mergedag(other.changelog, tr, | ||||
seq(seqrev), accumulate) | ||||
resolverev = self.changelog.count() | ||||
# is there anything to do? | ||||
if co == cn: | ||||
tr.close() | ||||
return | ||||
# do we need to resolve? | ||||
simple = (co == self.changelog.ancestor(co, cn)) | ||||
# merge all files changed by the changesets, | ||||
# keeping track of the new tips | ||||
changelist = changed.keys() | ||||
changelist.sort() | ||||
for f in changelist: | ||||
sys.stdout.write(".") | ||||
sys.stdout.flush() | ||||
r = self.file(f) | ||||
node = r.merge(other.file(f), tr, lseq(changed[f]), resolverev) | ||||
if node: | ||||
new[f] = node | ||||
sys.stdout.write("\n") | ||||
# begin the merge of the manifest | ||||
self.ui.status("merging manifests\n") | ||||
(mm, mo) = self.manifest.mergedag(other.manifest, tr, seq(seqrev)) | ||||
# For simple merges, we don't need to resolve manifests or changesets | ||||
if simple: | ||||
tr.close() | ||||
return | ||||
ma = self.manifest.ancestor(mm, mo) | ||||
# resolve the manifest to point to all the merged files | ||||
self.ui.status("resolving manifests\n") | ||||
mmap = self.manifest.read(mm) # mine | ||||
omap = self.manifest.read(mo) # other | ||||
amap = self.manifest.read(ma) # ancestor | ||||
nmap = {} | ||||
for f, mid in mmap.iteritems(): | ||||
if f in omap: | ||||
if mid != omap[f]: | ||||
nmap[f] = new.get(f, mid) # use merged version | ||||
else: | ||||
nmap[f] = new.get(f, mid) # they're the same | ||||
del omap[f] | ||||
elif f in amap: | ||||
if mid != amap[f]: | ||||
pass # we should prompt here | ||||
else: | ||||
pass # other deleted it | ||||
else: | ||||
nmap[f] = new.get(f, mid) # we created it | ||||
del mmap | ||||
for f, oid in omap.iteritems(): | ||||
if f in amap: | ||||
if oid != amap[f]: | ||||
pass # this is the nasty case, we should prompt | ||||
else: | ||||
pass # probably safe | ||||
else: | ||||
nmap[f] = new.get(f, oid) # remote created it | ||||
del omap | ||||
del amap | ||||
node = self.manifest.add(nmap, tr, resolverev, mm, mo) | ||||
# Now all files and manifests are merged, we add the changed files | ||||
# and manifest id to the changelog | ||||
self.ui.status("committing merge changeset\n") | ||||
new = new.keys() | ||||
new.sort() | ||||
if co == cn: cn = -1 | ||||
edittext = "\n"+"".join(["HG: changed %s\n" % f for f in new]) | ||||
edittext = self.ui.edit(edittext) | ||||
n = self.changelog.add(node, new, edittext, tr, co, cn) | ||||
tr.close() | ||||
|
r30 | def commit(self, parent, update = None, text = ""): | ||
|
r0 | tr = self.transaction() | ||
try: | ||||
remove = [ l[:-1] for l in self.opener("to-remove") ] | ||||
os.unlink(self.join("to-remove")) | ||||
except IOError: | ||||
remove = [] | ||||
if update == None: | ||||
|
r29 | update = self.diffdir(self.root, parent)[0] | ||
|
r0 | |||
# check in files | ||||
new = {} | ||||
linkrev = self.changelog.count() | ||||
for f in update: | ||||
|
r83 | self.ui.note(f + "\n") | ||
|
r0 | try: | ||
t = file(f).read() | ||||
except IOError: | ||||
remove.append(f) | ||||
continue | ||||
r = self.file(f) | ||||
new[f] = r.add(t, tr, linkrev) | ||||
# update manifest | ||||
mmap = self.manifest.read(self.manifest.tip()) | ||||
mmap.update(new) | ||||
for f in remove: | ||||
del mmap[f] | ||||
mnode = self.manifest.add(mmap, tr, linkrev) | ||||
# add changeset | ||||
new = new.keys() | ||||
new.sort() | ||||
edittext = text + "\n"+"".join(["HG: changed %s\n" % f for f in new]) | ||||
|
r25 | edittext += "".join(["HG: removed %s\n" % f for f in remove]) | ||
|
r0 | edittext = self.ui.edit(edittext) | ||
n = self.changelog.add(mnode, new, edittext, tr) | ||||
tr.close() | ||||
self.setcurrent(n) | ||||
self.dircache.update(new) | ||||
self.dircache.remove(remove) | ||||
def checkdir(self, path): | ||||
d = os.path.dirname(path) | ||||
if not d: return | ||||
if not os.path.isdir(d): | ||||
self.checkdir(d) | ||||
os.mkdir(d) | ||||
def checkout(self, node): | ||||
# checkout is really dumb at the moment | ||||
# it ought to basically merge | ||||
change = self.changelog.read(node) | ||||
mmap = self.manifest.read(change[0]) | ||||
l = mmap.keys() | ||||
l.sort() | ||||
stats = [] | ||||
for f in l: | ||||
|
r83 | self.ui.note(f + "\n") | ||
|
r0 | r = self.file(f) | ||
t = r.revision(mmap[f]) | ||||
try: | ||||
file(f, "w").write(t) | ||||
except: | ||||
self.checkdir(f) | ||||
file(f, "w").write(t) | ||||
self.setcurrent(node) | ||||
self.dircache.clear() | ||||
self.dircache.update(l) | ||||
|
r29 | def diffdir(self, path, changeset): | ||
|
r0 | changed = [] | ||
|
r4 | mf = {} | ||
|
r0 | added = [] | ||
|
r29 | if changeset: | ||
change = self.changelog.read(changeset) | ||||
|
r4 | mf = self.manifest.read(change[0]) | ||
|
r0 | |||
|
r29 | if changeset == self.current: | ||
dc = self.dircache.copy() | ||||
else: | ||||
dc = dict.fromkeys(mf) | ||||
def fcmp(fn): | ||||
|
r64 | t1 = file(os.path.join(self.root, fn)).read() | ||
|
r29 | t2 = self.file(fn).revision(mf[fn]) | ||
return cmp(t1, t2) | ||||
|
r0 | for dir, subdirs, files in os.walk(self.root): | ||
d = dir[len(self.root)+1:] | ||||
if ".hg" in subdirs: subdirs.remove(".hg") | ||||
for f in files: | ||||
fn = os.path.join(d, f) | ||||
|
r64 | try: s = os.stat(os.path.join(self.root, fn)) | ||
|
r0 | except: continue | ||
if fn in dc: | ||||
c = dc[fn] | ||||
del dc[fn] | ||||
|
r29 | if not c: | ||
if fcmp(fn): | ||||
changed.append(fn) | ||||
|
r33 | elif c[1] != s.st_size: | ||
|
r0 | changed.append(fn) | ||
elif c[0] != s.st_mode or c[2] != s.st_mtime: | ||||
|
r29 | if fcmp(fn): | ||
|
r0 | changed.append(fn) | ||
else: | ||||
if self.ignore(fn): continue | ||||
added.append(fn) | ||||
deleted = dc.keys() | ||||
deleted.sort() | ||||
return (changed, added, deleted) | ||||
|
r32 | def diffrevs(self, node1, node2): | ||
|
r33 | changed, added = [], [] | ||
|
r32 | |||
change = self.changelog.read(node1) | ||||
mf1 = self.manifest.read(change[0]) | ||||
|
r33 | change = self.changelog.read(node2) | ||
|
r32 | mf2 = self.manifest.read(change[0]) | ||
for fn in mf2: | ||||
if mf1.has_key(fn): | ||||
if mf1[fn] != mf2[fn]: | ||||
changed.append(fn) | ||||
del mf1[fn] | ||||
else: | ||||
added.append(fn) | ||||
deleted = mf1.keys() | ||||
deleted.sort() | ||||
return (changed, added, deleted) | ||||
|
r0 | def add(self, list): | ||
self.dircache.taint(list) | ||||
def remove(self, list): | ||||
dl = self.opener("to-remove", "a") | ||||
for f in list: | ||||
dl.write(f + "\n") | ||||
|
r56 | def branches(self, nodes): | ||
if not nodes: nodes = [self.changelog.tip()] | ||||
b = [] | ||||
for n in nodes: | ||||
t = n | ||||
while n: | ||||
p = self.changelog.parents(n) | ||||
if p[1] != nullid or p[0] == nullid: | ||||
b.append((t, n, p[0], p[1])) | ||||
break | ||||
n = p[0] | ||||
return b | ||||
def between(self, pairs): | ||||
r = [] | ||||
for top, bottom in pairs: | ||||
n, l, i = top, [], 0 | ||||
f = 1 | ||||
while n != bottom: | ||||
p = self.changelog.parents(n)[0] | ||||
if i == f: | ||||
l.append(n) | ||||
f = f * 2 | ||||
n = p | ||||
i += 1 | ||||
r.append(l) | ||||
return r | ||||
def newer(self, nodes): | ||||
m = {} | ||||
nl = [] | ||||
cl = self.changelog | ||||
t = l = cl.count() | ||||
for n in nodes: | ||||
l = min(l, cl.rev(n)) | ||||
for p in cl.parents(n): | ||||
m[p] = 1 | ||||
|
r46 | |||
|
r56 | for i in xrange(l, t): | ||
n = cl.node(i) | ||||
for p in cl.parents(n): | ||||
if p in m and n not in m: | ||||
m[n] = 1 | ||||
nl.append(n) | ||||
return nl | ||||
def getchangegroup(self, remote): | ||||
|
r60 | tip = remote.branches([])[0] | ||
|
r83 | self.ui.debug("remote tip branch is %s:%s\n" % | ||
(short(tip[0]), short(tip[1]))) | ||||
|
r65 | m = self.changelog.nodemap | ||
|
r60 | unknown = [tip] | ||
|
r56 | search = [] | ||
fetch = [] | ||||
|
r46 | |||
|
r65 | if tip[0] in m: | ||
|
r83 | self.ui.note("nothing to do!\n") | ||
|
r60 | return None | ||
|
r56 | while unknown: | ||
n = unknown.pop(0) | ||||
if n == nullid: break | ||||
|
r65 | if n[1] and n[1] in m: # do we know the base? | ||
|
r83 | self.ui.debug("found incomplete branch %s\n" % short(n[1])) | ||
|
r56 | search.append(n) # schedule branch range for scanning | ||
else: | ||||
for b in remote.branches([n[2], n[3]]): | ||||
|
r65 | if b[0] in m: | ||
if n[1] not in fetch: | ||||
|
r83 | self.ui.debug("found new changeset %s\n" % | ||
short(n[1])) | ||||
|
r65 | fetch.append(n[1]) # earliest unknown | ||
|
r56 | else: | ||
unknown.append(b) | ||||
while search: | ||||
n = search.pop(0) | ||||
l = remote.between([(n[0], n[1])])[0] | ||||
p = n[0] | ||||
f = 1 | ||||
for i in l + [n[1]]: | ||||
|
r65 | if i in m: | ||
|
r60 | if f <= 4: | ||
|
r83 | self.ui.debug("found new branch changeset %s\n" % | ||
short(p)) | ||||
|
r56 | fetch.append(p) | ||
else: | ||||
|
r83 | self.ui.debug("narrowed branch search to %s:%s\n" | ||
% (short(p), short(i))) | ||||
|
r56 | search.append((p, i)) | ||
|
r65 | break | ||
|
r56 | p, f = i, f * 2 | ||
|
r65 | for f in fetch: | ||
if f in m: | ||||
|
r83 | raise "already have", short(f[:4]) | ||
self.ui.note("merging new changesets starting at " + | ||||
" ".join([short(f) for f in fetch]) + "\n") | ||||
|
r65 | |||
|
r56 | return remote.changegroup(fetch) | ||
def changegroup(self, basenodes): | ||||
nodes = self.newer(basenodes) | ||||
|
r46 | # construct the link map | ||
linkmap = {} | ||||
for n in nodes: | ||||
linkmap[self.changelog.rev(n)] = n | ||||
# construct a list of all changed files | ||||
changed = {} | ||||
for n in nodes: | ||||
c = self.changelog.read(n) | ||||
for f in c[3]: | ||||
changed[f] = 1 | ||||
changed = changed.keys() | ||||
changed.sort() | ||||
# the changegroup is changesets + manifests + all file revs | ||||
revs = [ self.changelog.rev(n) for n in nodes ] | ||||
|
r60 | yield self.changelog.group(linkmap) | ||
yield self.manifest.group(linkmap) | ||||
|
r46 | |||
for f in changed: | ||||
g = self.file(f).group(linkmap) | ||||
if not g: raise "couldn't find change to %s" % f | ||||
l = struct.pack(">l", len(f)) | ||||
|
r60 | yield "".join([l, f, g]) | ||
|
r46 | |||
|
r65 | def addchangegroup(self, generator): | ||
class genread: | ||||
def __init__(self, generator): | ||||
self.g = generator | ||||
self.buf = "" | ||||
def read(self, l): | ||||
while l > len(self.buf): | ||||
try: | ||||
self.buf += self.g.next() | ||||
except StopIteration: | ||||
break | ||||
d, self.buf = self.buf[:l], self.buf[l:] | ||||
return d | ||||
if not generator: return | ||||
source = genread(generator) | ||||
|
r60 | |||
|
r65 | def getchunk(add = 0): | ||
d = source.read(4) | ||||
if not d: return "" | ||||
l = struct.unpack(">l", d)[0] | ||||
return source.read(l - 4 + add) | ||||
|
r46 | tr = self.transaction() | ||
simple = True | ||||
|
r83 | self.ui.status("merging changesets\n") | ||
|
r46 | # pull off the changeset group | ||
|
r65 | csg = getchunk() | ||
|
r46 | co = self.changelog.tip() | ||
cn = self.changelog.addgroup(csg, lambda x: self.changelog.count(), tr) | ||||
|
r83 | self.ui.status("merging manifests\n") | ||
|
r46 | # pull off the manifest group | ||
|
r65 | mfg = getchunk() | ||
|
r46 | mo = self.manifest.tip() | ||
|
r65 | mm = self.manifest.addgroup(mfg, lambda x: self.changelog.rev(x), tr) | ||
|
r46 | |||
# do we need a resolve? | ||||
if self.changelog.ancestor(co, cn) != co: | ||||
simple = False | ||||
resolverev = self.changelog.count() | ||||
# process the files | ||||
|
r83 | self.ui.status("merging files\n") | ||
|
r46 | new = {} | ||
|
r65 | while 1: | ||
f = getchunk(4) | ||||
if not f: break | ||||
fg = getchunk() | ||||
|
r46 | |||
fl = self.file(f) | ||||
o = fl.tip() | ||||
n = fl.addgroup(fg, lambda x: self.changelog.rev(x), tr) | ||||
if not simple: | ||||
|
r65 | nn = fl.resolvedag(o, n, tr, resolverev) | ||
if nn: new[f] = nn | ||||
|
r46 | |||
# For simple merges, we don't need to resolve manifests or changesets | ||||
if simple: | ||||
tr.close() | ||||
return | ||||
# resolve the manifest to point to all the merged files | ||||
self.ui.status("resolving manifests\n") | ||||
ma = self.manifest.ancestor(mm, mo) | ||||
mmap = self.manifest.read(mm) # mine | ||||
omap = self.manifest.read(mo) # other | ||||
amap = self.manifest.read(ma) # ancestor | ||||
nmap = {} | ||||
for f, mid in mmap.iteritems(): | ||||
if f in omap: | ||||
if mid != omap[f]: | ||||
nmap[f] = new.get(f, mid) # use merged version | ||||
else: | ||||
nmap[f] = new.get(f, mid) # they're the same | ||||
del omap[f] | ||||
elif f in amap: | ||||
if mid != amap[f]: | ||||
pass # we should prompt here | ||||
else: | ||||
pass # other deleted it | ||||
else: | ||||
nmap[f] = new.get(f, mid) # we created it | ||||
del mmap | ||||
for f, oid in omap.iteritems(): | ||||
if f in amap: | ||||
if oid != amap[f]: | ||||
pass # this is the nasty case, we should prompt | ||||
else: | ||||
pass # probably safe | ||||
else: | ||||
nmap[f] = new.get(f, oid) # remote created it | ||||
del omap | ||||
del amap | ||||
node = self.manifest.add(nmap, tr, resolverev, mm, mo) | ||||
# Now all files and manifests are merged, we add the changed files | ||||
# and manifest id to the changelog | ||||
self.ui.status("committing merge changeset\n") | ||||
new = new.keys() | ||||
new.sort() | ||||
if co == cn: cn = -1 | ||||
|
r78 | edittext = "\nHG: merge resolve\n" + \ | ||
"".join(["HG: changed %s\n" % f for f in new]) | ||||
|
r46 | edittext = self.ui.edit(edittext) | ||
n = self.changelog.add(node, new, edittext, tr, co, cn) | ||||
tr.close() | ||||
|
r60 | class remoterepository: | ||
def __init__(self, ui, path): | ||||
self.url = path.replace("hg://", "http://", 1) | ||||
self.ui = ui | ||||
def do_cmd(self, cmd, **args): | ||||
|
r83 | self.ui.debug("sending %s command\n" % cmd) | ||
|
r60 | q = {"cmd": cmd} | ||
q.update(args) | ||||
qs = urllib.urlencode(q) | ||||
cu = "%s?%s" % (self.url, qs) | ||||
|
r65 | return urllib.urlopen(cu) | ||
|
r60 | |||
def branches(self, nodes): | ||||
n = " ".join(map(hex, nodes)) | ||||
|
r65 | d = self.do_cmd("branches", nodes=n).read() | ||
|
r60 | br = [ map(bin, b.split(" ")) for b in d.splitlines() ] | ||
return br | ||||
def between(self, pairs): | ||||
n = "\n".join(["-".join(map(hex, p)) for p in pairs]) | ||||
|
r65 | d = self.do_cmd("between", pairs=n).read() | ||
|
r60 | p = [ map(bin, l.split(" ")) for l in d.splitlines() ] | ||
return p | ||||
def changegroup(self, nodes): | ||||
n = " ".join(map(hex, nodes)) | ||||
|
r65 | zd = zlib.decompressobj() | ||
f = self.do_cmd("changegroup", roots=n) | ||||
while 1: | ||||
d = f.read(4096) | ||||
if not d: | ||||
yield zd.flush() | ||||
break | ||||
yield zd.decompress(d) | ||||
|
r60 | |||
def repository(ui, path=None, create=0): | ||||
if path and path[:5] == "hg://": | ||||
return remoterepository(ui, path) | ||||
else: | ||||
return localrepository(ui, path, create) | ||||
|
r0 | class ui: | ||
|
r83 | def __init__(self, verbose=False, debug=False, quiet=False): | ||
self.quiet = quiet and not verbose and not debug | ||||
self.verbose = verbose or debug | ||||
self.debugflag = debug | ||||
|
r0 | def write(self, *args): | ||
for a in args: | ||||
sys.stdout.write(str(a)) | ||||
def prompt(self, msg, pat): | ||||
while 1: | ||||
sys.stdout.write(msg) | ||||
r = sys.stdin.readline()[:-1] | ||||
if re.match(pat, r): | ||||
return r | ||||
def status(self, *msg): | ||||
|
r83 | if not self.quiet: self.write(*msg) | ||
|
r0 | def warn(self, msg): | ||
self.write(*msg) | ||||
def note(self, msg): | ||||
if self.verbose: self.write(*msg) | ||||
def debug(self, msg): | ||||
|
r83 | if self.debugflag: self.write(*msg) | ||
|
r0 | def edit(self, text): | ||
(fd, name) = tempfile.mkstemp("hg") | ||||
f = os.fdopen(fd, "w") | ||||
f.write(text) | ||||
f.close() | ||||
editor = os.environ.get("EDITOR", "vi") | ||||
r = os.system("%s %s" % (editor, name)) | ||||
if r: | ||||
raise "Edit failed!" | ||||
t = open(name).read() | ||||
t = re.sub("(?m)^HG:.*\n", "", t) | ||||
return t | ||||
class httprangereader: | ||||
def __init__(self, url): | ||||
self.url = url | ||||
self.pos = 0 | ||||
def seek(self, pos): | ||||
self.pos = pos | ||||
def read(self, bytes=None): | ||||
opener = urllib2.build_opener(byterange.HTTPRangeHandler()) | ||||
urllib2.install_opener(opener) | ||||
req = urllib2.Request(self.url) | ||||
end = '' | ||||
if bytes: end = self.pos + bytes | ||||
req.add_header('Range', 'bytes=%d-%s' % (self.pos, end)) | ||||
f = urllib2.urlopen(req) | ||||
return f.read() | ||||