summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ganarchy/git.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/ganarchy/git.py b/ganarchy/git.py
index a88a979..ccee0bf 100644
--- a/ganarchy/git.py
+++ b/ganarchy/git.py
@@ -358,15 +358,20 @@ class _WithWorkRepos:
             del work
             del branch
 
+            collected_branches = []
             for i, repo in enumerate(self.work_repos):
                 for branch in repo.pending_branches:
                     fetch_head = "{}-{}".format(branch, i)
                     # First collect the work branch into a fetch head
                     self.cache_repo._fetch_work(repo, fetch_head, branch)
                     # If that succeeds, delete the work branch to free up disk
+                    # (this doesn't really free up disk unless we run git gc)
                     repo._rm_branch(branch)
-                    # We have all the objects in the main repo and we probably
-                    # have enough disk, so just replace the fetch head into
-                    # the main branch and hope nothing errors.
-                    self.cache_repo._replace(fetch_head, branch)
+                    # We now have all the branch's objects in the main repo,
+                    # save it to update the refs later
+                    collected_branches.append((fetch_head, branch))
                 repo._delete()
+            for fetch_head, branch in collected_branches:
+                # Once every work branch is referenced in the main repo, update
+                # the original refs so objects can be safely GCed
+                self.cache_repo._replace(fetch_head, branch)