# HG changeset patch # User Idan Kamara # Date 1313673823 -10800 # Node ID c52383a550fbf0ab08224ae07723d56bf57d76d7 # Parent bd7dfd94b0d9be7dca312c354b56d2026a5f1e28 client: add summary command diff -r bd7dfd94b0d9 -r c52383a550fb hglib/client.py --- a/hglib/client.py Thu Aug 18 16:20:23 2011 +0300 +++ b/hglib/client.py Thu Aug 18 16:23:43 2011 +0300 @@ -640,6 +640,86 @@ t.append((name.rstrip(), int(rev), node, taglocal)) return t + def summary(self, remote=False): + """ + Return a dictionary with a brief summary of the working directory state, + including parents, branch, commit status, and available updates. + + 'parent' : a list of (rev, node, tags, message) + 'branch' : the current branch + 'commit' : True if the working directory is clean, False otherwise + 'update' : number of available updates, + ['remote' : (in, in bookmarks, out, out bookmarks),] + ['mq': (applied, unapplied) mq patches,] + + unparsed entries will be of them form key : value + """ + args = cmdbuilder('summary', remote=remote) + + out = self.rawcommand(args).splitlines() + + d = {} + while out: + line = out.pop(0) + name, value = line.split(': ', 1) + + if name == 'parent': + parent, tags = value.split(' ', 1) + rev, node = parent.split(':') + + if tags: + tags = tags.replace(' (empty repository)', '') + else: + tags = None + + value = d.get(name, []) + + if rev == '-1': + value.append((int(rev), node, tags, None)) + else: + message = out.pop(0)[1:] + value.append((int(rev), node, tags, message)) + elif name == 'branch': + pass + elif name == 'commit': + value = value == '(clean)' + elif name == 'update': + if value == '(current)': + value = 0 + else: + value = int(value.split(' ', 1)[0]) + elif remote and name == 'remote': + if value == '(synced)': + value = 0, 0, 0, 0 + else: + inc = incb = out_ = outb = 0 + + for v in value.split(', '): + count, v = v.split(' ', 1) + if v == 'outgoing': + out_ = int(count) + elif v.endswith('incoming'): + inc = int(count) + elif v == 'incoming bookmarks': + incb = int(count) + elif v == 'outgoing bookmarks': + outb = int(count) + + value = inc, incb, out_, outb + elif name == 'mq': + applied = unapplied = 0 + for v in value.split(', '): + count, v = v.split(' ', 1) + if v == 'applied': + applied = int(count) + elif v == 'unapplied': + unapplied = int(count) + value = applied, unapplied + + d[name] = value + + return d + def tip(self): args = cmdbuilder('tip', template=templates.changeset) out = self.rawcommand(args) diff -r bd7dfd94b0d9 -r c52383a550fb tests/test-summary.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-summary.py Thu Aug 18 16:23:43 2011 +0300 @@ -0,0 +1,107 @@ +import common +import hglib + +class test_summary(common.basetest): + def test_empty(self): + d = {'parent' : [(-1, '000000000000', 'tip', None)], + 'branch' : 'default', + 'commit' : True, + 'update' : 0} + + self.assertEquals(self.client.summary(), d) + + def test_basic(self): + self.append('a', 'a') + rev, node = self.client.commit('first', addremove=True) + + d = {'parent' : [(0, node[:12], 'tip', 'first')], + 'branch' : 'default', + 'commit' : True, + 'update' : 0} + + self.assertEquals(self.client.summary(), d) + + def test_commit_dirty(self): + self.append('a', 'a') + rev, node = self.client.commit('first', addremove=True) + self.append('a', 'a') + + d = {'parent' : [(0, node[:12], 'tip', 'first')], + 'branch' : 'default', + 'commit' : False, + 'update' : 0} + + self.assertEquals(self.client.summary(), d) + + def test_update(self): + self.append('a', 'a') + rev, node = self.client.commit('first', addremove=True) + self.append('a', 'a') + self.client.commit('second') + self.client.update(0) + + d = {'parent' : [(0, node[:12], None, 'first')], + 'branch' : 'default', + 'commit' : True, + 'update' : 1} + + self.assertEquals(self.client.summary(), d) + + def test_remote(self): + self.append('a', 'a') + rev, node = self.client.commit('first', addremove=True) + + self.client.clone(dest='other') + other = hglib.open('other') + + d = {'parent' : [(0, node[:12], 'tip', 'first')], + 'branch' : 'default', + 'commit' : True, + 'update' : 0, + 'remote' : (0, 0, 0, 0)} + + self.assertEquals(other.summary(remote=True), d) + + self.append('a', 'a') + self.client.commit('second') + + d['remote'] = (1, 0, 0, 0) + self.assertEquals(other.summary(remote=True), d) + + self.client.bookmark('bm') + d['remote'] = (1, 1, 0, 0) + self.assertEquals(other.summary(remote=True), d) + + other.bookmark('bmother') + d['remote'] = (1, 1, 0, 1) + d['parent'] = [(0, node[:12], 'tip bmother', 'first')] + self.assertEquals(other.summary(remote=True), d) + + self.append('other/a', 'a') + rev, node = other.commit('second in other') + + d['remote'] = (1, 1, 1, 1) + d['parent'] = [(1, node[:12], 'tip bmother', 'second in other')] + + self.assertEquals(other.summary(remote=True), d) + + def test_two_parents(self): + self.append('a', 'a') + rev0, node = self.client.commit('first', addremove=True) + + self.append('a', 'a') + rev1, node1 = self.client.commit('second') + + self.client.update(rev0) + self.append('b', 'a') + rev2, node2 = self.client.commit('third', addremove=True) + + self.client.merge(rev1) + + d = {'parent' : [(2, node2[:12], 'tip', 'third'), + (1, node1[:12], None, 'second')], + 'branch' : 'default', + 'commit' : False, + 'update' : 0} + + self.assertEquals(self.client.summary(), d)