Browse Source

Don't parse git log header for topic

When looking for candidate topic in bug or blueprint ID, get the
git log  without header lines, e.g., commit hash, author, committer,
dates, so that accidental matches such as commit hash (which could look
like a bug ID) or user name (which could look like anything) don't
accidentally get taken as the topic.

Change-Id: Ie45ed7509e7f873e566f078b55cadd547da704dd
Closes-Bug: #2000296
Benjamin Pflanz 3 years ago
parent
commit
d80b6cb346
2 changed files with 74 additions and 3 deletions
  1. 8
    1
      git_review/cmd.py
  2. 66
    2
      git_review/tests/test_git_review.py

+ 8
- 1
git_review/cmd.py View File

@@ -900,7 +900,14 @@ def get_topic(target_branch):
900 900
                          "for the topic of the change submitted",
901 901
                          "/".join(branch_parts[2:]))
902 902
 
903
-    log_output = run_command("git log HEAD^1..HEAD")
903
+    preferred_log_format = "%B"
904
+    log_output = run_command("git log --pretty='" + preferred_log_format +
905
+                             "' HEAD^1..HEAD")
906
+    if log_output == preferred_log_format:
907
+        # The %B format specifier is supported starting at Git v1.7.2. If it's
908
+        # not supported, we'll just get back '%B', so we try something else.
909
+        # The downside of %s is that it removes newlines in the subject.
910
+        log_output = run_command("git log --pretty='%s%n%b' HEAD^1..HEAD")
904 911
     bug_re = r'''(?x)                # verbose regexp
905 912
                  \b([Bb]ug|[Ll][Pp]) # bug or lp
906 913
                  [ \t\f\v]*          # don't want to match newline

+ 66
- 2
git_review/tests/test_git_review.py View File

@@ -330,7 +330,7 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
330 330
 
331 331
     def test_bug_topic_newline(self):
332 332
         self._run_git_review('-s')
333
-        self._simple_change('a change', 'new change not for bug\n123')
333
+        self._simple_change('a change', 'new change not for bug\n\n123')
334 334
         self._assert_branch_would_be('master')
335 335
 
336 336
     def test_bp_topic(self):
@@ -340,9 +340,73 @@ class GitReviewTestCase(tests.BaseGitReviewTestCase):
340 340
 
341 341
     def test_bp_topic_newline(self):
342 342
         self._run_git_review('-s')
343
-        self._simple_change('a change', 'new change not for bluepring\nasdf')
343
+        self._simple_change('a change', 'new change not for blueprint\n\nasdf')
344 344
         self._assert_branch_would_be('master')
345 345
 
346
+    def test_author_name_topic_bp(self):
347
+        old_author = None
348
+        if 'GIT_AUTHOR_NAME' in os.environ:
349
+            old_author = os.environ['GIT_AUTHOR_NAME']
350
+        try:
351
+            os.environ['GIT_AUTHOR_NAME'] = 'BPNAME'
352
+            self._run_git_review('-s')
353
+            self._simple_change('a change',
354
+                                'new change 1 with name but no topic')
355
+            self._assert_branch_would_be('master')
356
+        finally:
357
+            if old_author:
358
+                os.environ['GIT_AUTHOR_NAME'] = old_author
359
+            else:
360
+                del os.environ['GIT_AUTHOR_NAME']
361
+
362
+    def test_author_email_topic_bp(self):
363
+        old_author = None
364
+        if 'GIT_AUTHOR_EMAIL' in os.environ:
365
+            old_author = os.environ['GIT_AUTHOR_EMAIL']
366
+        try:
367
+            os.environ['GIT_AUTHOR_EMAIL'] = 'bpemail@example.com'
368
+            self._run_git_review('-s')
369
+            self._simple_change('a change',
370
+                                'new change 1 with email but no topic')
371
+            self._assert_branch_would_be('master')
372
+        finally:
373
+            if old_author:
374
+                os.environ['GIT_AUTHOR_EMAIL'] = old_author
375
+            else:
376
+                del os.environ['GIT_AUTHOR_EMAIL']
377
+
378
+    def test_author_name_topic_bug(self):
379
+        old_author = None
380
+        if 'GIT_AUTHOR_NAME' in os.environ:
381
+            old_author = os.environ['GIT_AUTHOR_NAME']
382
+        try:
383
+            os.environ['GIT_AUTHOR_NAME'] = 'Bug: #1234'
384
+            self._run_git_review('-s')
385
+            self._simple_change('a change',
386
+                                'new change 2 with name but no topic')
387
+            self._assert_branch_would_be('master')
388
+        finally:
389
+            if old_author:
390
+                os.environ['GIT_AUTHOR_NAME'] = old_author
391
+            else:
392
+                del os.environ['GIT_AUTHOR_NAME']
393
+
394
+    def test_author_email_topic_bug(self):
395
+        old_author = None
396
+        if 'GIT_AUTHOR_EMAIL' in os.environ:
397
+            old_author = os.environ['GIT_AUTHOR_EMAIL']
398
+        try:
399
+            os.environ['GIT_AUTHOR_EMAIL'] = 'bug5678@example.com'
400
+            self._run_git_review('-s')
401
+            self._simple_change('a change',
402
+                                'new change 2 with email but no topic')
403
+            self._assert_branch_would_be('master')
404
+        finally:
405
+            if old_author:
406
+                os.environ['GIT_AUTHOR_EMAIL'] = old_author
407
+            else:
408
+                del os.environ['GIT_AUTHOR_EMAIL']
409
+
346 410
     def test_git_review_T(self):
347 411
         self._run_git_review('-s')
348 412
         self._simple_change('test file modified', 'commit message for bug 456')

Loading…
Cancel
Save