def test_push(wst, monkeypatch): push_repo = Mock() remove_branch = Mock() monkeypatch.setattr('workspace.commands.push.push_repo', push_repo) monkeypatch.setattr('workspace.commands.push.remove_branch', remove_branch) with temp_remote_git_repo(): wst('push') push_repo.assert_called_with(branch='master', force=False, remote='origin') with open('new_file', 'w') as fp: fp.write('Hello World') assert 'new_file' in stat_repo(return_output=True) wst('commit "Add new file"') wst('push') push_repo.assert_called_with(branch='add-new@master', force=False, remote='origin') assert ['add-new@master', 'master'] == all_branches() wst('push --merge') push_repo.assert_called_with(branch='master', force=False, remote='origin') remove_branch.assert_called_with('add-new@master', force=True, remote=True) # Remove local branch from workspace.scm import remove_branch as rb rb('add-new@master', force=True, remote=False) assert ['master'] == all_branches() assert "ahead of 'origin/master' by 1 commit." in stat_repo(return_output=True)
def run(self): if is_repo(): if len(self.target) == 1: if self.target[0] in all_branches(): checkout_branch(self.target[0]) click.echo('Switched to branch ' + self.target[0]) return else: possible_remotes = {upstream_remote()} if '/' in self.target[0]: possible_remotes.add(self.target[0].split('/')[0]) for pull_tags in [False, True]: if pull_tags: for remote in possible_remotes & set( all_remotes()): update_tags(remote) if '/' in self.target[0] and 'remotes/{}'.format( self.target[0]) in all_branches(remotes=True): checkout_branch(self.target[0]) click.echo('Switched to branch ' + self.target[0].split('/')[-1]) return if 'remotes/{}/{}'.format( upstream_remote(), self.target[0]) in all_branches(remotes=True): checkout_branch("{}/{}".format( upstream_remote(), self.target[0])) click.echo('Switched to branch ' + self.target[0]) return checkout_files(self.target) return product_urls = expand_product_groups(self.target) for product_url in product_urls: product_url = product_url.strip('/') product_path = product_checkout_path(product_url) if os.path.exists(product_path): click.echo('Updating ' + product_name(product_path)) else: click.echo('Checking out ' + product_url) checkout_product(product_url, product_path)
def run(self): try: scm_repos = repos() in_repo = is_repo(os.getcwd()) optional = len(scm_repos) == 1 pager = ProductPager(optional=optional) for repo in scm_repos: stat_path = os.getcwd() if in_repo else repo output = stat_repo(stat_path, return_output=True, with_color=True) nothing_to_commit = ('nothing to commit' in output and 'Your branch is ahead of' not in output and 'Your branch is behind' not in output) branches = all_branches(repo, verbose=True) child_branches = [b for b in branches if '@' in b] if len(child_branches) >= 1 or len(scm_repos) == 1: show_branches = branches if len(scm_repos) == 1 else child_branches remotes = all_remotes() if len(scm_repos) == 1 else [] remotes = '\n# Remotes: {}'.format(' '.join(remotes)) if len(remotes) > 1 else '' if nothing_to_commit: output = '# Branches: {}{}'.format(' '.join(show_branches), remotes) nothing_to_commit = False elif len(show_branches) > 1: output = '# Branches: {}{}\n#\n{}'.format(' '.join(show_branches), remotes, output) if output and not nothing_to_commit: pager.write(product_name(repo), output) finally: pager.close_and_wait()
def run(self): repo = repo_path() if repo: click.echo('Removing build/dist folders') silent_run("rm -rf build dist docs/_build */activate", cwd=repo, shell=True) click.echo('Removing *.pyc files') silent_run( "find . -type d \( -path '*/.tox' -o -path '*/mppy-*' \) -prune -o -name *.pyc -exec rm {} \;", cwd=repo, shell=True) if self.force: click.echo('Removing untracked/ignored files') silent_run('git clean -fdx') else: path = workspace_path() click.echo('Cleaning {}'.format(path)) if config.clean.remove_products_older_than_days or config.clean.remove_all_products_except: keep_time = 0 keep_products = [] if config.clean.remove_all_products_except: click.echo('Removing all products except: %s' % config.clean.remove_all_products_except) keep_products = expand_product_groups( config.clean.remove_all_products_except.split()) if config.clean.remove_products_older_than_days: click.echo('Removing products older than %s days' % config.clean.remove_products_older_than_days) keep_time = time( ) - config.clean.remove_products_older_than_days * 86400 removed_products = [] for repo in repos(path): name = product_name(repo) modified_time = os.stat(repo).st_mtime if keep_products and name not in keep_products or keep_time and modified_time < keep_time: status = stat_repo(repo, return_output=True) if (not status or 'nothing to commit' in status and ('working directory clean' in status or 'working tree clean' in status) and len(all_branches(repo)) <= 1): shutil.rmtree(repo) removed_products.append(name) else: click.echo( ' - Skipping "%s" as it has changes that may not be committed' % name) if removed_products: click.echo('Removed ' + ', '.join(removed_products))
def test_commit(wst): with temp_dir(): with pytest.raises(SystemExit): wst('commit') with temp_git_repo(): with pytest.raises(SystemExit): wst('commit "no files to commit"') with open('new_file', 'w') as fp: fp.write('Hello World') assert 'new_file' in stat_repo(return_output=True) wst('commit "Add new file" --branch master') assert 'working tree clean' in stat_repo(return_output=True) assert 'Hello World' == open('new_file').read() with open('new_file', 'w') as fp: fp.write('New World') wst('commit "Update file"') assert ['update-file@master', 'master'] == all_branches() wst('commit --move release') assert ['update-file@master', 'master', 'release'] == all_branches() wst('commit --discard') assert ['master', 'release'] == all_branches() wst('checkout release') wst('commit --discard') assert ['release', 'master'] == all_branches() logs = commit_logs() assert 'new file' in logs assert 1 == len(list(filter(None, logs.split('commit'))))
def run(self): repo = repo_path() if repo: click.echo('Removing build/dist folders') silent_run("rm -rf build dist docs/_build */activate", cwd=repo, shell=True) click.echo('Removing *.pyc files') silent_run("find . -type d \( -path '*/.tox' -o -path '*/mppy-*' \) -prune -o -name *.pyc -exec rm {} \;", cwd=repo, shell=True) if self.force: click.echo('Removing untracked/ignored files') silent_run('git clean -fdx') else: path = workspace_path() click.echo('Cleaning {}'.format(path)) if config.clean.remove_products_older_than_days or config.clean.remove_all_products_except: keep_time = 0 keep_products = [] if config.clean.remove_all_products_except: click.echo('Removing all products except: %s' % config.clean.remove_all_products_except) keep_products = expand_product_groups(config.clean.remove_all_products_except.split()) if config.clean.remove_products_older_than_days: click.echo('Removing products older than %s days' % config.clean.remove_products_older_than_days) keep_time = time() - config.clean.remove_products_older_than_days * 86400 removed_products = [] for repo in repos(path): name = product_name(repo) modified_time = os.stat(repo).st_mtime if keep_products and name not in keep_products or keep_time and modified_time < keep_time: status = stat_repo(repo, return_output=True) if (not status or 'nothing to commit' in status and ('working directory clean' in status or 'working tree clean' in status) and len(all_branches(repo)) <= 1): shutil.rmtree(repo) removed_products.append(name) else: click.echo(' - Skipping "%s" as it has changes that may not be committed' % name) if removed_products: click.echo('Removed ' + ', '.join(removed_products))
def run(self): if is_repo(): if len(self.target) == 1: if self.target[0] in all_branches(): checkout_branch(self.target[0]) click.echo('Switched to branch ' + self.target[0]) return else: possible_remotes = {upstream_remote()} if '/' in self.target[0]: possible_remotes.add(self.target[0].split('/')[0]) for pull_tags in [False, True]: if pull_tags: for remote in possible_remotes & set(all_remotes()): update_tags(remote) if '/' in self.target[0] and 'remotes/{}'.format(self.target[0]) in all_branches(remotes=True): checkout_branch(self.target[0]) click.echo('Switched to branch ' + self.target[0].split('/')[-1]) return if 'remotes/{}/{}'.format(upstream_remote(), self.target[0]) in all_branches(remotes=True): checkout_branch("{}/{}".format(upstream_remote(), self.target[0])) click.echo('Switched to branch ' + self.target[0]) return checkout_files(self.target) return product_urls = expand_product_groups(self.target) for product_url in product_urls: product_url = product_url.strip('/') product_path = product_checkout_path(product_url) if os.path.exists(product_path): click.echo('Updating ' + product_name(product_path)) else: click.echo('Checking out ' + product_url) checkout_product(product_url, product_path)
def run(self): try: scm_repos = repos() in_repo = is_repo(os.getcwd()) optional = len(scm_repos) == 1 pager = ProductPager(optional=optional) for repo in scm_repos: stat_path = os.getcwd() if in_repo else repo output = stat_repo(stat_path, return_output=True, with_color=True) nothing_to_commit = ('nothing to commit' in output and 'Your branch is ahead of' not in output and 'Your branch is behind' not in output) branches = all_branches(repo, verbose=True) child_branches = [b for b in branches if '@' in b] if len(child_branches) >= 1 or len(scm_repos) == 1: show_branches = branches if len( scm_repos) == 1 else child_branches remotes = all_remotes() if len(scm_repos) == 1 else [] remotes = '\n# Remotes: {}'.format( ' '.join(remotes)) if len(remotes) > 1 else '' if nothing_to_commit: output = '# Branches: {}{}'.format( ' '.join(show_branches), remotes) nothing_to_commit = False elif len(show_branches) > 1: output = '# Branches: {}{}\n#\n{}'.format( ' '.join(show_branches), remotes, output) if output and not nothing_to_commit: pager.write(product_name(repo), output) finally: pager.close_and_wait()
def run(self): if self.discard or self.move: if not self.branch: if self.discard: self.branch = current_branch() else: self.branch = self.move[0] is_child_branch = base_branch = parent_branch(self.branch) if self.discard: changes = commit_logs(self.discard) if not is_child_branch else diff_branch(self.branch, left_branch=base_branch) else: changes = commit_logs(1) changes = [_f for _f in changes.split('commit ') if _f] if self.discard and len(changes) <= self.discard and is_child_branch: checkout_branch(base_branch) remove_branch(self.branch, raises=True, force=True) else: match = re.match('([a-f0-9]+)(?: \(.*\))\n', changes[0]) if match: last_commit = match.group(1) if self.move: cur_branch = current_branch() create_branch(self.branch) checkout_branch(cur_branch) click.echo('Moved {} to {}'.format(last_commit[:7], self.branch)) hard_reset(last_commit + '~1') else: checkout_branch(self.branch) hard_reset(last_commit + '~' + str(self.discard)) else: log.error('Odd. No commit hash found in: %s', changes[0]) else: if not self.skip_style_check and (self.test or self.push) and self.commander.command('test').supports_style_check(): click.echo('Checking style') self.commander.run('test', env_or_file=['style'], silent=2) test_output = None if not (self.msg or self.amend): self.msg = prompt_with_editor('Please provide a commit message. Empty message will cancel the commit.') if not self.msg: sys.exit() if not self.amend and self.test: click.echo('Running tests') test_output = self.commander.run('test', return_output=False, test_dependents=self.test > 1) branches = all_branches() cur_branch = branches and branches[0] if (not (self.push or self.amend) and config.commit.commit_branch_indicator not in cur_branch and not self.branch and self.msg and config.commit.auto_branch_from_commit_words): self.branch = self._branch_for_msg(self.msg, words=config.commit.auto_branch_from_commit_words, branches=branches) if cur_branch: self.branch = '{}@{}'.format(self.branch, cur_branch) if self.branch: if branches: if self.branch in branches: if self.branch != cur_branch: checkout_branch(self.branch) else: create_branch(self.branch, cur_branch) else: # Empty repo without a commit has no branches create_branch(self.branch) add_files(files=self.files) local_commit(self.msg, self.amend) if self.amend and self.test: if self.commander.command('test').supports_style_check(): click.echo('Running style check') self.commander.run('test', env_or_file=['style'], silent=2) click.echo('Running tests') test_output = self.commander.run('test', return_output=False, test_dependents=self.test > 1) if self.push: self.commander.run('push', branch=self.branch, force=self.amend, skip_style_check=True, all_remotes=int(self.push) > 1) return test_output