FIXUP: FROMGIT: patman: Use the Change-Id, version, and prefix in the Message-Id

===
This is a squash of a revert of the FROMLIST patch we landed earlier
with a pick of the FROMGIT patch.
===

As per the centithread on ksummit-discuss [1], there are folks who
feel that if a Change-Id is present in a developer's local commit that
said Change-Id could be interesting to include in upstream posts.
Specifically if two commits are posted with the same Change-Id there's
a reasonable chance that they are either the same commit or a newer
version of the same commit.  Specifically this is because that's how
gerrit has trained people to work.

There is much angst about Change-Id in upstream Linux, but one thing
that seems safe and non-controversial is to include the Change-Id as
part of the string of crud that makes up a Message-Id.

Let's give that a try.

In theory (if there is enough adoption) this could help a tool more
reliably find various versions of a commit.  This actually might work
pretty well for U-Boot where (I believe) quite a number of developers
use patman, so there could be critical mass (assuming that enough of
these people also use a git hook that adds Change-Id to their
commits).  I was able to find this git hook by searching for "gerrit
change id git hook" in my favorite search engine.

In theory one could imagine something like this could be integrated
into other tools, possibly even git-send-email.  Getting it into
patman seems like a sane first step, though.

NOTE: this patch is being posted using a patman containing this patch,
so you should be able to see the Message-Id of this patch and see that
it contains my local Change-Id, which ends in 2b9 if you want to
check.

[1] https://lists.linuxfoundation.org/pipermail/ksummit-discuss/2019-August/006739.html

Signed-off-by: Douglas Anderson <dianders@chromium.org>
(cherry picked from commit 858a7f2484992667fe9bcf959123bea201c59904
 https://gitlab.denx.de/u-boot/custodians/u-boot-dm.git next)

BUG=None
TEST=Use patman to send this patch

Change-Id: I55f8465574089894a732fa2f0075984925976e83
Signed-off-by: Douglas Anderson <dianders@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1832143
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/patman/patchstream.py b/tools/patman/patchstream.py
index 2dd6a77..ef06606 100644
--- a/tools/patman/patchstream.py
+++ b/tools/patman/patchstream.py
@@ -288,6 +288,7 @@
                     raise ValueError("%s: Two Change-Ids: '%s' vs. '%s'" %
                         (self.commit.hash, self.commit.change_id, value))
                 self.commit.change_id = value
+            self.skip_blank = True
 
         # Detect Commit-xxx tags
         elif commit_tag_match:
@@ -371,6 +372,12 @@
         if not self.commit.change_id:
             return
 
+        # If the count is -1 we're testing, so use a fixed time
+        if self.commit.count == -1:
+            time_now = datetime.datetime(1999, 12, 31, 23, 59, 59)
+        else:
+            time_now = datetime.datetime.now()
+
         # In theory there is email.utils.make_msgid() which would be nice
         # to use, but it already produces something way too long and thus
         # will produce ugly commit lines if someone throws this into
@@ -378,7 +385,7 @@
 
         # Start with the time; presumably we wouldn't send the same series
         # with the same Change-Id at the exact same second.
-        parts = [datetime.datetime.now().strftime("%Y%m%d%H%M%S")]
+        parts = [time_now.strftime("%Y%m%d%H%M%S")]
 
         # These seem like they would be nice to include.
         if 'prefix' in self.series:
diff --git a/tools/patman/test.py b/tools/patman/test.py
index e1b94bd..cc61c20 100644
--- a/tools/patman/test.py
+++ b/tools/patman/test.py
@@ -12,6 +12,7 @@
 import gitutil
 import patchstream
 import series
+import commit
 
 
 class TestPatch(unittest.TestCase):
@@ -48,7 +49,8 @@
  arch/arm/cpu/armv7/tegra2/ap20.c           |   57 ++----
  arch/arm/cpu/armv7/tegra2/clock.c          |  163 +++++++++++++++++
 '''
-        expected='''
+        expected='''Message-Id: <19991231235959.0.I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413@changeid>
+
 
 From 656c9a8c31fa65859d924cd21da920d6ba537fad Mon Sep 17 00:00:00 2001
 From: Simon Glass <sjg@chromium.org>
@@ -79,7 +81,16 @@
         expfd.write(expected)
         expfd.close()
 
-        patchstream.FixPatch(None, inname, series.Series(), None)
+        # Normally by the time we call FixPatch we've already collected
+        # metadata.  Here, we haven't, but at least fake up something.
+        # Set the "count" to -1 which tells FixPatch to use a bogus/fixed
+        # time for generating the Message-Id.
+        com = commit.Commit('')
+        com.change_id = 'I80fe1d0c0b7dd10aa58ce5bb1d9290b6664d5413'
+        com.count = -1
+
+        patchstream.FixPatch(None, inname, series.Series(), com)
+
         rc = os.system('diff -u %s %s' % (inname, expname))
         self.assertEqual(rc, 0)