Merge lp:~abentley/launchpad/diffstat-count into lp:launchpad

Proposed by Aaron Bentley
Status: Merged
Merged at revision: not available
Proposed branch: lp:~abentley/launchpad/diffstat-count
Merge into: lp:launchpad
Diff against target: 123 lines
3 files modified
lib/lp/code/model/diff.py (+14/-2)
lib/lp/code/model/tests/test_diff.py (+33/-2)
lib/lp/code/stories/webservice/xx-branchmergeproposal.txt (+2/-2)
To merge this branch: bzr merge lp:~abentley/launchpad/diffstat-count
Reviewer Review Type Date Requested Status
Paul Hummer (community) Approve
Review via email: mp+12720@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Aaron Bentley (abentley) wrote :

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

= Summary =
This allows Launchpad to automatically know the overall number of lines
added or removed by a patch.

== Proposed fix ==
Calculate totals using the diffstat array, and store them in the
database. In the event that a diffstat cannot be calculated, store None
in these fields.

== Pre-implementation notes ==
Pre-implementation was with thumper

== Implementation details ==
As a drive-by, if a diffstat cannot be calculated, Diff.diffstat is set
to None rather than {}. None is already its default value.

== Tests ==
bin/test test_diff -t TestDiff

== Demo and Q/A ==
There is no user-visible change.

= Launchpad lint =

Checking for conflicts. and issues in doctests and templates.
Running jslint, xmllint, pyflakes, and pylint.
Using normal rules.

Linting changed files:
  lib/lp/code/model/diff.py
  lib/lp/code/model/tests/test_diff.py
  lib/lp/code/stories/webservice/xx-branchmergeproposal.txt

== Pylint notices ==

lib/lp/code/model/diff.py
    17: [F0401] Unable to import 'lazr.delegates' (No module named
delegates)

^^^ bogus
    166: [W0703, Diff.fromFile] Catch "Exception"
^^^ The block does generic exception handling.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkrEt4kACgkQ0F+nu1YWqI0UegCfWdcLy0KKD2rhXIAV0J4IdvO4
71AAn1gxQWugD8zeDYmUVoiTZ03/QTwd
=VU6P
-----END PGP SIGNATURE-----

Revision history for this message
Paul Hummer (rockstar) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/model/diff.py'
2--- lib/lp/code/model/diff.py 2009-09-25 03:42:50 +0000
3+++ lib/lp/code/model/diff.py 2009-10-01 14:15:26 +0000
4@@ -50,6 +50,9 @@
5 in simplejson.loads(self._diffstat).items())
6
7 def _set_diffstat(self, diffstat):
8+ if diffstat is None:
9+ self._diffstat = None
10+ return
11 # diffstats should be mappings of path to line counts.
12 assert isinstance(diffstat, dict)
13 self._diffstat = simplejson.dumps(diffstat)
14@@ -163,9 +166,18 @@
15 except Exception:
16 getUtility(IErrorReportingUtility).raising(sys.exc_info())
17 # Set the diffstat to be empty.
18- diffstat = {}
19+ diffstat = None
20+ added_lines_count = None
21+ removed_lines_count = None
22+ else:
23+ added_lines_count = 0
24+ removed_lines_count = 0
25+ for path, (added, removed) in diffstat.items():
26+ added_lines_count += added
27+ removed_lines_count += removed
28 return cls(diff_text=diff_text, diff_lines_count=diff_lines_count,
29- diffstat=diffstat)
30+ diffstat=diffstat, added_lines_count=added_lines_count,
31+ removed_lines_count=removed_lines_count)
32
33 @staticmethod
34 def generateDiffstat(diff_bytes):
35
36=== modified file 'lib/lp/code/model/tests/test_diff.py'
37--- lib/lp/code/model/tests/test_diff.py 2009-09-25 03:42:50 +0000
38+++ lib/lp/code/model/tests/test_diff.py 2009-10-01 14:15:26 +0000
39@@ -59,7 +59,7 @@
40 # There's a conflict because the source branch added a line "d", but
41 # the target branch added the line "c" in the same place.
42 self.assertIn(
43- '+<<<<<<< TREE\n c\n+=======\n+d\n+>>>>>>> MERGE-SOURCE\n',
44+ '+<<<''<<<< TREE\n c\n+=======\n+d\n+>>>>>''>> MERGE-SOURCE\n',
45 diff_text)
46
47
48@@ -140,6 +140,28 @@
49 "+d\n"
50 "+e\n")
51
52+ diff_bytes_2 = (
53+ "--- bar 2009-08-26 15:53:34.000000000 -0400\n"
54+ "+++ bar 1969-12-31 19:00:00.000000000 -0500\n"
55+ "@@ -1,3 +0,0 @@\n"
56+ "-a\n"
57+ "-b\n"
58+ "-c\n"
59+ "--- baz 1969-12-31 19:00:00.000000000 -0500\n"
60+ "+++ baz 2009-08-26 15:53:57.000000000 -0400\n"
61+ "@@ -0,0 +1,2 @@\n"
62+ "+a\n"
63+ "+b\n"
64+ "--- foo 2009-08-26 15:53:23.000000000 -0400\n"
65+ "+++ foo 2009-08-26 15:56:43.000000000 -0400\n"
66+ "@@ -1,3 +1,5 @@\n"
67+ " a\n"
68+ "-b\n"
69+ " c\n"
70+ "+d\n"
71+ "+e\n"
72+ "+f\n")
73+
74 def test_generateDiffstat(self):
75 self.assertEqual(
76 {'foo': (2, 1), 'bar': (0, 3), 'baz': (2, 0)},
77@@ -150,6 +172,13 @@
78 self.assertEqual({'bar': (0, 3), 'baz': (2, 0), 'foo': (2, 1)},
79 diff.diffstat)
80
81+ def test_fromFileSets_added_removed(self):
82+ """fromFile sets added_lines_count, removed_lines_count."""
83+ diff = Diff.fromFile(
84+ StringIO(self.diff_bytes_2), len(self.diff_bytes_2))
85+ self.assertEqual(5, diff.added_lines_count)
86+ self.assertEqual(4, diff.removed_lines_count)
87+
88 def test_fromFile_withError(self):
89 # If the diff is formatted such that generating the diffstat fails, we
90 # want to record an oops but continue.
91@@ -157,7 +186,9 @@
92 diff_bytes = "not a real diff"
93 diff = Diff.fromFile(StringIO(diff_bytes), len(diff_bytes))
94 self.assertNotEqual(last_oops_id, errorlog.globalErrorUtility.lastid)
95- self.assertEqual({}, diff.diffstat)
96+ self.assertIs(None, diff.diffstat)
97+ self.assertIs(None, diff.added_lines_count)
98+ self.assertIs(None, diff.removed_lines_count)
99
100
101 class TestStaticDiff(TestCaseWithFactory):
102
103=== modified file 'lib/lp/code/stories/webservice/xx-branchmergeproposal.txt'
104--- lib/lp/code/stories/webservice/xx-branchmergeproposal.txt 2009-09-15 20:45:30 +0000
105+++ lib/lp/code/stories/webservice/xx-branchmergeproposal.txt 2009-10-01 14:15:26 +0000
106@@ -222,7 +222,7 @@
107 >>> preview_diff = webservice.get(
108 ... merge_proposal['preview_diff_link']).jsonBody()
109 >>> pprint_entry(preview_diff)
110- added_lines_count: None
111+ added_lines_count: 0
112 branch_merge_proposal_link:
113 u'http://.../~source/fooix/fix-it/+merge/1'
114 conflicts: u'oh, no conflicts'
115@@ -231,7 +231,7 @@
116 diff_text_link:
117 u'http://.../~source/fooix/fix-it/+merge/1/+preview-diff/diff_text'
118 diffstat: {u'fooix.txt': [0, 0]}
119- removed_lines_count: None
120+ removed_lines_count: 0
121 resource_type_link: u'http://.../#preview_diff'
122 self_link:
123 u'http://.../~source/fooix/fix-it/+merge/1/+preview-diff'