Skip to content

Commit bdfe278

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

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

tools/migrate/blame.py

Lines changed: 21 additions & 18 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,43 @@ 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']
2328
try:
24-
parser = getParser(leaf)
29+
parser = getParser(abspath)
2530
except UserWarning:
2631
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+
33+
fname = file_blame['path']
3234
parser.readFile(fname)
3335
entities, emap = parser.parse()
34-
self.blame[leaf] = {}
36+
self.blame[abspath] = {}
3537
for e in entities:
3638
if isinstance(e, Junk):
3739
continue
38-
blines = blames[
40+
entity_lines = file_blame['lines'][
3941
(e.value_position()[0] - 1):e.value_position(-1)[0]
4042
]
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
43+
# ignore timezone
44+
entity_lines.sort(key=lambda blame: -blame['date'][0])
45+
line_blame = entity_lines[0]
46+
user = line_blame['user']
47+
timestamp = line_blame['date'][0] # ignore timezone
4548
if user not in self.users:
4649
self.users.append(user)
4750
userid = self.users.index(user)
48-
self.blame[leaf][e.key] = [userid, timestamp]
51+
self.blame[abspath][e.key] = [userid, timestamp]
4952

5053
if __name__ == '__main__':
5154
parser = argparse.ArgumentParser()

0 commit comments

Comments
 (0)