Mercurial > public > mercurial-scm > hg
comparison mercurial/context.py @ 30040:3e3f2201bbdf
annotate: calculate line count correctly
Before this patch, the "lines" function inside "annotate" returns 1 for
empty text (''). This patch makes it 0. Because the function should match
mdiff.splitnewlines (used by mdiff.allblocks), or s.splitlines (used at the
end of the "annotate" method). Both len(mdiff.splitnewlines('')) and
len(''.splitlines(True)) are 0.
This issue was discovered while testing fastannotate [1].
I could not find a test case to reveal this issue. However in theory this
could reduce memory usage a little bit, and avoids surprises when people
are touching this area in the future.
[1]: https://bitbucket.org/facebook/hg-experimental/commits/525b3b98e93a
author | Jun Wu <quark@fb.com> |
---|---|
date | Sat, 01 Oct 2016 14:18:58 +0100 |
parents | ff17dff99295 |
children | e25ce44f8447 |
comparison
equal
deleted
inserted
replaced
30039:ff7697b436ab | 30040:3e3f2201bbdf |
---|---|
928 ''' | 928 ''' |
929 | 929 |
930 def lines(text): | 930 def lines(text): |
931 if text.endswith("\n"): | 931 if text.endswith("\n"): |
932 return text.count("\n") | 932 return text.count("\n") |
933 return text.count("\n") + 1 | 933 return text.count("\n") + int(bool(text)) |
934 | 934 |
935 if linenumber: | 935 if linenumber: |
936 def decorate(text, rev): | 936 def decorate(text, rev): |
937 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text) | 937 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text) |
938 else: | 938 else: |