From f7e02d76ec22bda2a0dfcd24f44a4df8544fae02 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Thu, 13 Sep 2012 13:25:44 +0100 Subject: morph merge: Don't make a merge commit if only the refs changed 'git commit' fails if there are no changes in any case. --- morphlib/git.py | 11 +++++++++++ morphlib/plugins/branch_and_merge_plugin.py | 13 ++++++++++--- tests.branching/merge.script | 2 ++ tests.branching/merge.stdout | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/morphlib/git.py b/morphlib/git.py index 9ab98ad0..5862ef9b 100644 --- a/morphlib/git.py +++ b/morphlib/git.py @@ -196,6 +196,17 @@ def checkout_ref(runcmd, gitdir, ref): '''Checks out a specific ref/SHA1 in a git working tree.''' runcmd(['git', 'checkout', ref], cwd=gitdir) + +def index_has_changes(runcmd, gitdir): + '''Returns True if there are no staged changes to commit''' + try: + runcmd(['git', 'diff-index', '--cached', '--quiet', + '--ignore-submodules', 'HEAD'], cwd=gitdir) + except cliapp.AppException: + return True + return False + + def reset_workdir(runcmd, gitdir): '''Removes any differences between the current commit ''' '''and the status of the working directory''' diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py index c81c95c8..75935b72 100644 --- a/morphlib/plugins/branch_and_merge_plugin.py +++ b/morphlib/plugins/branch_and_merge_plugin.py @@ -685,6 +685,8 @@ class BranchAndMergePlugin(cliapp.Plugin): merge_chunk(old_ci, ci) if changed: self.save_morphology(to_repo, si['morph'], stratum) + self.app.runcmd(['git', 'add', si['morph'] + '.morph'], + cwd=to_repo) from_root_dir = self.find_repository(from_branch_dir, root_repo) to_root_dir = self.find_repository(to_branch_dir, root_repo) @@ -718,11 +720,16 @@ class BranchAndMergePlugin(cliapp.Plugin): merge_stratum(old_si, si) if changed: self.save_morphology(to_root_dir, name, morphology) + self.app.runcmd(['git', 'add', f], cwd=to_root_dir) for repo_dir in dirty_repos: - msg = "Merge system branch '%s'" % from_branch - self.app.runcmd(['git', 'commit', '--all', '--message=%s' % msg], - cwd=repo_dir) + # Repo will often turn out to not be dirty: if the changes we + # merged only updated refs to the system branch, we will have + # changed them back again so that the index will now be empty. + if morphlib.git.index_has_changes(self.app.runcmd, repo_dir): + msg = "Merge system branch '%s'" % from_branch + self.app.runcmd(['git', 'commit', '--all', '-m%s' % msg], + cwd=repo_dir) def build(self, args): if len(args) != 1: diff --git a/tests.branching/merge.script b/tests.branching/merge.script index d8a28cb3..21a30116 100755 --- a/tests.branching/merge.script +++ b/tests.branching/merge.script @@ -49,6 +49,8 @@ git status --short # make sure all changes are committed cd ../baserock:morphs ! grep "\"ref\": \"baserock/newbranch\"" *.morph +# The only change here was the branch refs, which have now been +# changed back - so there should not be any new commits. echo "Commit message for baserock:morphs" git cat-file commit HEAD | tail -n 1 diff --git a/tests.branching/merge.stdout b/tests.branching/merge.stdout index e5127734..6806f75a 100644 --- a/tests.branching/merge.stdout +++ b/tests.branching/merge.stdout @@ -1,5 +1,5 @@ Commit message for baserock:morphs -Merge system branch 'baserock/newbranch' +initial Commit message for baserock:hello Merge system branch 'baserock/newbranch' -- cgit v1.2.1