From a7facb708a23dc27e37af86fe0ac0f6e85a4e4ce Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Mon, 13 Oct 2014 19:04:28 +0100 Subject: [PATCH] Let read-tree update index and tree Remove the unnecessary additional call to 'git checkout' to update the working tree to the current index when finishing the import merge. Add the necessary options to the read-tree call to update both. Additionally add some basic tests to ensure no behavioural change. Change-Id: I4bd77aaf0b6c9b71e717d74db92af0d897d9c7b3 --- git_upstream/commands/import.py | 10 +-- git_upstream/tests/test_import.py | 104 ++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 git_upstream/tests/test_import.py diff --git a/git_upstream/commands/import.py b/git_upstream/commands/import.py index 7a2c92b..ece1b55 100644 --- a/git_upstream/commands/import.py +++ b/git_upstream/commands/import.py @@ -383,21 +383,15 @@ class ImportUpstream(LogDedentMixin, GitMixin): self.log.info( """\ Replacing tree contents with those from the import branch: - git read-tree %s + git read-tree -u --reset %s """, self.import_branch) - self.git.read_tree(self.import_branch) + self.git.read_tree(self.import_branch, u=True, reset=True) self.log.info( """\ Committing merge commit: git commit --no-edit """) self.git.commit(no_edit=True) - self.log.info( - """\ - Checking out updated index: - git checkout -- . - """) - self.git.checkout("--", ".") # finally test that everything worked correctly by comparing if # the tree object id's match if self.git.rev_parse("HEAD^{tree}") != \ diff --git a/git_upstream/tests/test_import.py b/git_upstream/tests/test_import.py new file mode 100644 index 0000000..565e87b --- /dev/null +++ b/git_upstream/tests/test_import.py @@ -0,0 +1,104 @@ +# Copyright (c) 2012, 2013, 2014 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests for the 'import' module""" + +from base import BaseTestCase + +import_command = __import__("git_upstream.commands.import", globals(), + locals(), ['ImportUpstream'], -1) +ImportUpstream = import_command.ImportUpstream + + +class TestImport(BaseTestCase): + + def test_import_finish_merge_clean(self): + """Test that after finishing the import merge that the users working + tree is correctly updated to avoid it looking like there are + uncommitted changes + + Repository layout being checked (assumed already replayed) + + B---C local/master + / + / C1 import + / / + A---D---E upstream/master + + Test that ImportUpstream.finish() results in a clean working tree and + index + + """ + + tree = [ + ('A', []), + ('B', ['A']), + ('C', ['B']), + ('D', ['A']), + ('E', ['D']), + ('C1', ['E']) + ] + + branches = { + 'head': ('master', 'C'), + 'upstream': ('upstream/master', 'E'), + 'import': ('import', 'C1') + } + + self._build_git_tree(tree, branches.values()) + iu = ImportUpstream("master", "upstream/master", "import") + iu.finish() + self.assertEquals("", self.git.status(porcelain=True), + "ImportUpstream.finish() failed to result in a " + "clean working tree and index") + + def test_import_finish_merge_extra_files(self): + """Test that after finishing the import merge when the users working + tree is updated that any additional files not being managed by git are + left untouched + + Repository layout being checked (assumed already replayed) + + B---C local/master + / + / C1 import + / / + A---D---E upstream/master + + """ + + tree = [ + ('A', []), + ('B', ['A']), + ('C', ['B']), + ('D', ['A']), + ('E', ['D']), + ('C1', ['E']) + ] + + branches = { + 'head': ('master', 'C'), + 'upstream': ('upstream/master', 'E'), + 'import': ('import', 'C1') + } + + self._build_git_tree(tree, branches.values()) + iu = ImportUpstream("master", "upstream/master", "import") + # create a dummy file + open('dummy-file', 'a').close() + iu.finish() + self.assertEquals("?? dummy-file", self.git.status(porcelain=True), + "ImportUpstream.finish() failed to leave user " + "files not managed untouched.")