pre-upload: allow different --rerun-since argument interpretations

When passing the --rerun-since command line option, date is not the
easiest to specify time limit when trying to determine how many
patches should be checked.

Let's allow specifying different arguments to this command line
option, adding sha1 of an existing patch and number of patches to
check.

The following heuristics are used: if the value is shorter than 3
symbols, it must be the number of patches to check. If the value has
dashes in it, it must be a date. Otherwise it must be a SHA1.

BUG=none
TEST=verified that pre-uplad.py --rerun-since properly checks patches
     with any of all three formats.

Change-Id: I4a9383e820fa1c74fb7d26241d82495233d68969
Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/860905
Reviewed-by: Mike Frysinger <vapier@chromium.org>
(cherry picked from commit 75447b92ba2cf224a52e13a98a18b77ab011d885)
Reviewed-on: https://chromium-review.googlesource.com/895903
diff --git a/pre-upload.py b/pre-upload.py
index c6d8a74..c476b63 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -1646,10 +1646,12 @@
                       'specified, the repo tool will be used to figure this '
                       'out based on the dir.')
   parser.add_argument('--rerun-since', default=None,
-                      help='Rerun hooks on old commits since the given date.  '
-                      'The date should match git log\'s concept of a date.  '
-                      'e.g. 2012-06-20. This option is mutually exclusive '
-                      'with --pre-submit.')
+                      help='Rerun hooks on old commits since some point '
+                      'in the past.  The argument could be a date (should '
+                      'match git log\'s concept of a date, e.g. 2012-06-20), '
+                      'or a SHA1, or just a number of commits to check (from 1 '
+                      'to 99).  This option is mutually exclusive with '
+                      '--pre-submit.')
   parser.add_argument('--pre-submit', action="store_true",
                       help='Run the check against the pending commit.  '
                       'This option should be used at the \'git commit\' '
@@ -1664,7 +1666,16 @@
       raise BadInvocation('Can\'t pass commits and use rerun-since: %s' %
                           ' '.join(opts.commits))
 
-    cmd = ['git', 'log', '--since="%s"' % opts.rerun_since, '--pretty=%H']
+    if len(opts.rerun_since) < 3 and opts.rerun_since.isdigit():
+      # This must be the number of commits to check. We don't expect the user
+      # to want to check more than 99 commits.
+      limit = '-n%s' % opts.rerun_since
+    elif git.IsSHA1(opts.rerun_since, False):
+      limit = '%s..' %  opts.rerun_since
+    else:
+      # This better be a date.
+      limit = '--since=%s' % opts.rerun_since
+    cmd = ['git', 'log', limit, '--pretty=%H']
     all_commits = _run_command(cmd).splitlines()
     bot_commits = _run_command(cmd + ['--author=chrome-bot']).splitlines()