def test_06_git_finish(self): """Finish branch1 with local remote branch removed.""" with chdir_tmp(self.dir_local.name): self.git.checkout('branch1') self.git.branch('-dr', 'origin/branch1') GitTrunkFinish(log_level=common.LOG_LEVEL).run() self._test_finish('branch1', 'master', 2)
def _setup_trunk(self): """Override to combine main repository with submodule one.""" # Repo that will be added as submodule. # NOTE. submodule dir_local will be used as combination from # main repo and submodule path inside it. _, self.dir_local_sub, self.dir_remote = self._setup_repo( self._build_repo) # Repo that will be used as submodule holder. self.git_main, self.dir_local_main, self.dir_remote_main = ( self._setup_repo(self._build_repo_simple)) # Path used inside main repo when adding submodule. self._add_submodule(self.git_main, self.dir_remote.name, PATH_SUBMODULE) # Set variables as it would look like normal repo used from # GitTrunkCommon. self.dir_local = MockedPath(self.dir_local_main.name, PATH_SUBMODULE) repo_sub = Repo(self.dir_local.name) self.git = repo_sub.git() # Make submodule look like main repo. self.git.fetch('origin') self.git.checkout('branch1') self.git.checkout('master') # Re-add branch2 locally, because it is not on remote (so won't # be pulled). with chdir_tmp(self.dir_local.name): self._add_branch_with_content(self.git, 'branch2', 'module2') self._setup_trunk_init()
def _create_dummy_commit(self, dir_name, body=None): with chdir_tmp(self.dir_local.name): self._create_dummy_dir_with_content(dir_name) self.git.add('.') msg = '[ADD] %s' % dir_name if body: msg += '\n%s' % body self.git.commit('-m', msg)
def test_05_git_finish(self): """Finish branch1 with --no-ff flag.""" with chdir_tmp(self.dir_local.name): self.git.checkout('branch1') trunk_finish = GitTrunkFinish(log_level=common.LOG_LEVEL) trunk_finish.config.section['ff'] = False trunk_finish.run() # One extra commit is merge commit. self._test_finish('branch1', 'master', 3)
def test_07_git_finish(self): """Try to finish trunk branch. Also try to finish with local trunk branch removed. """ with chdir_tmp(self.dir_local.name): with self.assertRaises(ValueError): GitTrunkFinish(log_level=common.LOG_LEVEL).run() self.git.checkout('branch1') self.git.branch('-D', 'master') with self.assertRaises(ValueError): GitTrunkFinish(log_level=common.LOG_LEVEL).run()
def _setup_repo(self, build): dir_remote = tempfile.TemporaryDirectory() # Remote. Repo.init(dir_remote.name, bare=True) dir_local = tempfile.TemporaryDirectory() # Initialize repo and get git command interface for it. git = Repo.init(dir_local.name).git() with chdir_tmp(dir_local.name): # When build is called, local and remote repos are already # initiated and active directory is at local dir. build(git, dir_local, dir_remote) return git, dir_local, dir_remote
def test_09_git_finish(self): """Try to Finish branch1 with extra commit on remote.""" with chdir_tmp(self.dir_local.name): self.git.checkout('branch1') self._create_dummy_dir_with_content('new_dir123') self.git.add('.') self.git.commit('-m "[ADD] new_dir123"') self.git.push() # Remove last commit, so it would not be up to date with # remote. self.git.reset('--hard', 'HEAD~1') with self.assertRaises(ValueError): GitTrunkFinish(log_level=common.LOG_LEVEL).run()
def test_08_git_finish(self): """Finish branch1 with remote removed.""" with chdir_tmp(self.dir_local.name): self.git.checkout('branch1') self.git.remote('rm', 'origin') trunk_finish = GitTrunkFinish(log_level=common.LOG_LEVEL) trunk_finish.config.section['ff'] = False trunk_finish.run() # One extra commit is merge commit. 128, because remote # can't be found (we removed it). self._test_finish('branch1', 'master', 3, check_trunk_branch_remote=False)
def run(self, count: int = None, include_squash_msg=True, custom_msg: str = '', **kwargs) -> None: """Squash active branch N commits. Args: count: number of commits to squash. If not specified, will squash all ahead commits leaving just the first one. include_squash_msg: whether to include squashed commits message. custom_msg: whether to use custom commit message after squash. These commands will be run: - git checkout TRUNK - git pull --rebase REMOTE TRUNK - git checkout ACTIVE - git rebase TRUNK - git reset --soft HEAD~COUNT - git commit --amend -m 'MSG' (message depends on options) - git push --force (if enabled in config) """ if not count: count = self.max_squash_commits_count super().run(count=count, **kwargs) # Making sure active branch has all latest changes from trunk. trunk_refresh = GitTrunkRefresh(repo_path=self.repo.working_dir, log_level=self.logger.level) trunk_refresh.run() message = self._get_message_for_squash( count, # type: ignore include_squash_msg=include_squash_msg, custom_msg=custom_msg) self._squash(count) # type: ignore self._amend_commit_for_squash(message=message) # To open commit message for editing. if self.config.section['edit_squash_message']: # Using subprocess to make sure editor is opened correctly. with chdir_tmp(self.repo.working_dir): gt_base._git_cmd(['commit', '--amend']) if self.config.section['force_push_squash']: tracking_data = self.active_tracking_branch_data if tracking_data: self.git_push(tracking_data.remote, tracking_data.head, '--force')
def _create_tag(self, new_version: str, ref: str): tag = self._attach_prefix_to_version(new_version) latest_ver = self.version_manager.get_latest_version() try: latest_tag = self.versions_tags_map[latest_ver] except KeyError: latest_tag = None msg = self._tag_msg_formatter(tag, ref, latest_tag=latest_tag) args = ['-a', tag, ref] # tag on specific reference. if self.config.section['edit_tag_message']: args.append('--edit') # NOTE. this is fake command, because real one takes '-m' arg, # but we hide it on purpose, to not pollute logs. self.logger.notice('git tag %s', ' '.join(args)) # Using subprocess, to make sure editor is opened correctly. with chdir_tmp(self.repo.working_dir): gt_base._git_cmd(['tag'] + args + ['-m', msg])
def test_04_git_finish(self): """Finish branch2 without upstream.""" with chdir_tmp(self.dir_local.name): self.git.checkout('branch2') GitTrunkFinish(log_level=common.LOG_LEVEL).run() self._test_finish('branch2', 'master', 2)
def test_10_git_finish(self): """Try to Finish branch when it is not ahead trunk.""" with chdir_tmp(self.dir_local.name): self.git.checkout('-b', 'new_branch') with self.assertRaises(ValueError): GitTrunkFinish(log_level=common.LOG_LEVEL).run()
def _update_dummy_file_in_dir(self, dir_name): with chdir_tmp(self.dir_local.name): _write_file(os.path.join(dir_name, DUMMY_FILE_NAME), str(uuid.uuid1()))
def test_02_chdir_tmp(self): """Change current working directory and then change it back.""" with path.chdir_tmp(self.cfd): self.assertTrue(os.getcwd().endswith('/footil/footil/tests')) self.assertTrue(os.getcwd().endswith('/footil'))