Calculate new version based on latest tag when pushing.
authormachenbach <machenbach@chromium.org>
Fri, 23 Jan 2015 10:30:32 +0000 (02:30 -0800)
committerCommit bot <commit-bot@chromium.org>
Fri, 23 Jan 2015 10:30:48 +0000 (10:30 +0000)
BUG=chromium:451357
TBR=tandrii@chromium.org
LOG=n
NOTRY=true

Review URL: https://codereview.chromium.org/868693002

Cr-Commit-Position: refs/heads/master@{#26243}

tools/push-to-trunk/common_includes.py
tools/push-to-trunk/push_to_trunk.py
tools/push-to-trunk/test_scripts.py

index ac78ef8..84468b1 100644 (file)
@@ -606,6 +606,16 @@ class Step(GitRecipesMixin):
                      self[prefix + "build"],
                      self[prefix + "patch"]])
 
+  def StoreVersion(self, version, prefix):
+    version_parts = version.split(".")
+    if len(version_parts) == 3:
+      version_parts.append("0")
+    major, minor, build, patch = version_parts
+    self[prefix + "major"] = major
+    self[prefix + "minor"] = minor
+    self[prefix + "build"] = build
+    self[prefix + "patch"] = patch
+
   def SetVersion(self, version_file, prefix):
     output = ""
     for line in FileToText(version_file).splitlines():
index 6e821f2..a8c131f 100755 (executable)
@@ -36,6 +36,7 @@ from common_includes import *
 
 PUSH_MSG_GIT_SUFFIX = " (based on %s)"
 PUSH_MSG_GIT_RE = re.compile(r".* \(based on (?P<git_rev>[a-fA-F0-9]+)\)$")
+VERSION_RE = re.compile(r"^\d+\.\d+\.\d+(?:\.\d+)?$")
 
 class Preparation(Step):
   MESSAGE = "Preparation."
@@ -44,6 +45,9 @@ class Preparation(Step):
     self.InitialEnvironmentChecks(self.default_cwd)
     self.CommonPrepare()
 
+    # Make sure tags are fetched.
+    self.Git("fetch origin +refs/tags/*:refs/tags/*")
+
     if(self["current_branch"] == self.Config("TRUNKBRANCH")
        or self["current_branch"] == self.Config("BRANCHNAME")):
       print "Warning: Script started on branch %s" % self["current_branch"]
@@ -109,55 +113,37 @@ class DetectLastPush(Step):
     self["last_push_bleeding_edge"] = last_push_bleeding_edge
 
 
-# TODO(machenbach): Code similarities with bump_up_version.py. Merge after
-# turning this script into a pure git script.
-class GetCurrentBleedingEdgeVersion(Step):
-  MESSAGE = "Get latest bleeding edge version."
+class GetLatestVersion(Step):
+  MESSAGE = "Get latest version from tags."
 
   def RunStep(self):
+    versions = sorted(filter(VERSION_RE.match, self.vc.GetTags()),
+                      key=SortingKey, reverse=True)
+    self.StoreVersion(versions[0], "latest_")
+    self["latest_version"] = self.ArrayToVersion("latest_")
+
+    # The version file on master can be used to bump up major/minor at
+    # branch time.
     self.GitCheckoutFile(VERSION_FILE, self.vc.RemoteMasterBranch())
+    self.ReadAndPersistVersion("master_")
+    self["master_version"] = self.ArrayToVersion("master_")
 
-    # Store latest version.
-    self.ReadAndPersistVersion("latest_")
-    self["latest_version"] = self.ArrayToVersion("latest_")
-    print "Bleeding edge version: %s" % self["latest_version"]
+    if SortingKey(self["master_version"]) > SortingKey(self["latest_version"]):
+      self["latest_version"] = self["master_version"]
+      self.StoreVersion(self["latest_version"], "latest_")
+
+    print "Determined latest version %s" % self["latest_version"]
 
 
 class IncrementVersion(Step):
   MESSAGE = "Increment version number."
 
   def RunStep(self):
-    # Retrieve current version from last trunk push.
-    self.GitCheckoutFile(VERSION_FILE, self["last_push_trunk"])
-    self.ReadAndPersistVersion()
-    self["trunk_version"] = self.ArrayToVersion("")
-
-    if self["latest_build"] == "9999":  # pragma: no cover
-      # If version control on bleeding edge was switched off, just use the last
-      # trunk version.
-      self["latest_version"] = self["trunk_version"]
-
-    if SortingKey(self["trunk_version"]) < SortingKey(self["latest_version"]):
-      # If the version on bleeding_edge is newer than on trunk, use it.
-      self.GitCheckoutFile(VERSION_FILE, self.vc.RemoteMasterBranch())
-      self.ReadAndPersistVersion()
-
-    if self.Confirm(("Automatically increment BUILD_NUMBER? (Saying 'n' will "
-                     "fire up your EDITOR on %s so you can make arbitrary "
-                     "changes. When you're done, save the file and exit your "
-                     "EDITOR.)" % VERSION_FILE)):
-
-      text = FileToText(os.path.join(self.default_cwd, VERSION_FILE))
-      text = MSub(r"(?<=#define BUILD_NUMBER)(?P<space>\s+)\d*$",
-                  r"\g<space>%s" % str(int(self["build"]) + 1),
-                  text)
-      TextToFile(text, os.path.join(self.default_cwd, VERSION_FILE))
-    else:
-      self.Editor(os.path.join(self.default_cwd, VERSION_FILE))
-
     # Variables prefixed with 'new_' contain the new version numbers for the
     # ongoing trunk push.
-    self.ReadAndPersistVersion("new_")
+    self["new_major"] = self["latest_major"]
+    self["new_minor"] = self["latest_minor"]
+    self["new_build"] = str(int(self["latest_build"]) + 1)
 
     # Make sure patch level is 0 in a new push.
     self["new_patch"] = "0"
@@ -419,7 +405,7 @@ class PushToTrunk(ScriptsBase):
       FreshBranch,
       PreparePushRevision,
       DetectLastPush,
-      GetCurrentBleedingEdgeVersion,
+      GetLatestVersion,
       IncrementVersion,
       PrepareChangeLog,
       EditChangeLog,
index db702a3..1fde75e 100644 (file)
@@ -616,27 +616,29 @@ class ScriptTest(unittest.TestCase):
     self.assertEquals("New\n        Lines",
                       FileToText(TEST_CONFIG["CHANGELOG_ENTRY_FILE"]))
 
-  # Version on trunk: 3.22.4.0. Version on master (bleeding_edge): 3.22.6.
-  # Make sure that the increment is 3.22.7.0.
-  def testIncrementVersion(self):
-    self.WriteFakeVersionFile()
-    self._state["last_push_trunk"] = "hash1"
-    self._state["latest_build"] = "6"
-    self._state["latest_version"] = "3.22.6.0"
+  TAGS = """
+4425.0
+0.0.0.0
+3.9.6
+3.22.4
+test_tag
+"""
 
+  # Version as tag: 3.22.4.0. Version on master: 3.22.6.
+  # Make sure that the latest version is 3.22.6.0.
+  def testGetLatestVersion(self):
     self.Expect([
-      Cmd("git checkout -f hash1 -- src/version.cc", ""),
+      Cmd("git tag", self.TAGS),
       Cmd("git checkout -f origin/master -- src/version.cc",
           "", cb=lambda: self.WriteFakeVersionFile(22, 6)),
-      RL("Y"),  # Increment build number.
     ])
 
-    self.RunStep(PushToTrunk, IncrementVersion)
+    self.RunStep(PushToTrunk, GetLatestVersion)
 
-    self.assertEquals("3", self._state["new_major"])
-    self.assertEquals("22", self._state["new_minor"])
-    self.assertEquals("7", self._state["new_build"])
-    self.assertEquals("0", self._state["new_patch"])
+    self.assertEquals("3", self._state["latest_major"])
+    self.assertEquals("22", self._state["latest_minor"])
+    self.assertEquals("6", self._state["latest_build"])
+    self.assertEquals("0", self._state["latest_patch"])
 
   def _TestSquashCommits(self, change_log, expected_msg):
     TEST_CONFIG["CHANGELOG_ENTRY_FILE"] = self.MakeEmptyTempFile()
@@ -770,6 +772,7 @@ Performance and stability improvements on all platforms.""", commit)
       Cmd("git status -s -uno", ""),
       Cmd("git status -s -b -uno", "## some_branch\n"),
       Cmd("git fetch", ""),
+      Cmd("git fetch origin +refs/tags/*:refs/tags/*", ""),
       Cmd("git branch", "  branch1\n* branch2\n"),
       Cmd("git branch", "  branch1\n* branch2\n"),
       Cmd(("git new-branch %s --upstream origin/master" %
@@ -785,14 +788,9 @@ Performance and stability improvements on all platforms.""", commit)
     expectations += [
       Cmd("git log -1 --format=%s hash2",
        "Version 3.4.5 (based on abc3)\n"),
+      Cmd("git tag", self.TAGS),
       Cmd("git checkout -f origin/master -- src/version.cc",
           "", cb=self.WriteFakeVersionFile),
-      Cmd("git checkout -f hash2 -- src/version.cc", "",
-          cb=self.WriteFakeVersionFile),
-    ]
-    if manual:
-      expectations.append(RL(""))  # Increment build number.
-    expectations += [
       Cmd("git log --format=%H abc3..push_hash", "rev1\n"),
       Cmd("git log -1 --format=%s rev1", "Log text 1.\n"),
       Cmd("git log -1 --format=%B rev1", "Text\nLOG=YES\nBUG=v8:321\nText\n"),