Skip to content

Commit ec71937

Browse files
authored
Bug 1411943 - Fix Blame for Mercurial 4.3+ (#23)
Mercurial 4.3 changed the format of the "annotate" command. See https://www.mercurial-scm.org/wiki/Release4.3
1 parent 48b0006 commit ec71937

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

tools/migrate/blame.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
import os
32
import json
43
import hglib
54
from hglib.util import b, cmdbuilder
@@ -13,39 +12,44 @@ def __init__(self, repopath):
1312
self.blame = {}
1413

1514
def main(self):
16-
for manifestline in self.client.manifest():
17-
leaf = manifestline[-1]
18-
self.handleFile(leaf)
15+
args = cmdbuilder(
16+
b('annotate'), self.client.root(), d=True, u=True, T='json')
17+
blame_json = ''.join(self.client.rawcommand(args))
18+
file_blames = json.loads(blame_json)
19+
20+
for file_blame in file_blames:
21+
self.handleFile(file_blame)
22+
1923
return {'authors': self.users,
2024
'blame': self.blame}
2125

22-
def handleFile(self, leaf):
26+
def handleFile(self, file_blame):
27+
abspath = file_blame['abspath']
28+
2329
try:
24-
parser = getParser(leaf)
30+
parser = getParser(abspath)
2531
except UserWarning:
2632
return
27-
args = cmdbuilder(b('annotate'), d=True, u=True, T='json',
28-
*['path:' + leaf])
29-
blame_json = ''.join(self.client.rawcommand(args))
30-
blames = json.loads(blame_json)
31-
fname = os.path.join(self.client.root(), leaf)
32-
parser.readFile(fname)
33+
34+
self.blame[abspath] = {}
35+
36+
parser.readFile(file_blame['path'])
3337
entities, emap = parser.parse()
34-
self.blame[leaf] = {}
3538
for e in entities:
3639
if isinstance(e, Junk):
3740
continue
38-
blines = blames[
41+
entity_lines = file_blame['lines'][
3942
(e.value_position()[0] - 1):e.value_position(-1)[0]
4043
]
41-
blines.sort(key=lambda blame: -blame['date'][0]) # ignore timezone
42-
blame = blines[0]
43-
user = blame['user']
44-
timestamp = blame['date'][0] # ignore timezone
44+
# ignore timezone
45+
entity_lines.sort(key=lambda blame: -blame['date'][0])
46+
line_blame = entity_lines[0]
47+
user = line_blame['user']
48+
timestamp = line_blame['date'][0] # ignore timezone
4549
if user not in self.users:
4650
self.users.append(user)
4751
userid = self.users.index(user)
48-
self.blame[leaf][e.key] = [userid, timestamp]
52+
self.blame[abspath][e.key] = [userid, timestamp]
4953

5054
if __name__ == '__main__':
5155
parser = argparse.ArgumentParser()

0 commit comments

Comments
 (0)