Andrew Bogott has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/386331 )

Change subject: git-sync-upstream: perform rebase in a sepaarate, temporary 
workdir
......................................................................

git-sync-upstream: perform rebase in a sepaarate, temporary workdir

This madness allows us to do the rebase away from the puppet
checkout that puppet is actually using.  This prevents puppet
from compiling catalogs out of a repo mid-rebase.

Bug: 386318
Change-Id: I4f56f4339c5440281fd7e9821d026ebc5265507a
---
M modules/puppetmaster/files/git-sync-upstream
1 file changed, 41 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/31/386331/1

diff --git a/modules/puppetmaster/files/git-sync-upstream 
b/modules/puppetmaster/files/git-sync-upstream
index 6723c89..66cfab2 100755
--- a/modules/puppetmaster/files/git-sync-upstream
+++ b/modules/puppetmaster/files/git-sync-upstream
@@ -3,6 +3,7 @@
 import datetime
 import logging
 import os
+import shutil
 import sys
 
 # Send all git output to stdout
@@ -14,6 +15,9 @@
 
 
 def rebase_repo(repo_path, track_branch):
+    tagname = "snapshot-%s" % datetime.datetime.now().strftime('%Y%m%d%H%M')
+    tempdir = "/tmp/%s" % tagname
+
     repo = git.Repo(repo_path)
     assert not repo.bare
 
@@ -26,6 +30,7 @@
 
     repo.remotes.origin.fetch()
 
+    latest_commit = repo.git.rev_parse(track_branch)
     latest_upstream_commit = repo.git.show_ref("-s", upstream_branch)
     latest_merged_commit = repo.git.merge_base(upstream_branch, "HEAD")
 
@@ -34,19 +39,47 @@
         return
 
     try:
-        repo.git.rebase("--preserve-merges",
-                        "--stat",
-                        "--strategy=recursive",
-                        "--strategy-option=patience",
-                        upstream_branch)
+        # Rebase in a tempdir to avoid changing the state of the current 
workdir.
+        #  (Rebasing in place causes an occasional puppet race)
+        #
+        # This next bit is largely cribbed from
+        #  https://github.com/encukou/bin/blob/master/oot-rebase
+        #
+        os.makedirs(tempdir)
+
+        tmprepo = git.Repo.init(tempdir)
+
+        with open(os.path.join(tempdir,".git/objects/info/alternates"), "w") 
as alternates:
+            alternates.write("%s/.git/objects" % repo_path)
+
+        tmprepo.git.fetch("-n", repo_path,
+                          "%s:oot-rebase/%s" % (track_branch, track_branch),
+                          "%s:oot-rebase/%s" % (upstream_branch, 
upstream_branch))
+        tmprepo.git.checkout("oot-rebase/%s" % track_branch)
+        tmprepo.git.rebase("--preserve-merges",
+                           "--stat",
+                           "--strategy=recursive",
+                           "--strategy-option=patience",
+                           "oot-rebase/%s" % upstream_branch)
+
+        # Now that we have a rebase, push to branch 'tagname' in the original 
repo
+        tmprepo.git.push("--force-with-lease=%s:%s" % (track_branch, 
latest_commit),
+                         repo_path,
+                         "oot-rebase/%s:%s" % (track_branch,tagname))
+
+        # And reset our original repo to this new branch and discard the 
'tagname' branch
+        repo.git.reset("--hard", tagname)
+        repo.git.branch("-D", tagname)
+
     except git.exc.GitCommandError:
-        print("Rebase failed! Reverting rebase attempt.", file=sys.stderr)
-        repo.git.rebase("--abort")
+        print("Rebase failed!")
+        shutil.rmtree(tempdir)
         return(2)
+
+    shutil.rmtree(tempdir)
 
     repo.git.submodule("update", "--init", "--recursive")
 
-    tagname = "snapshot-%s" % datetime.datetime.now().strftime('%Y%m%d%H%M')
     repo.create_tag(tagname)
     print("Tagged as %s" % tagname)
 

-- 
To view, visit https://gerrit.wikimedia.org/r/386331
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4f56f4339c5440281fd7e9821d026ebc5265507a
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Andrew Bogott <abog...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to