def _configuration(repo=None): repo = get_repo(repo) return Configuration( repo, 'github', { 'login': (NOT_SET, 'Github login'), 'access-token': (NOT_SET, 'Github access token') })
def current(): repo = get_repo() if gifi.feature.is_on_feature_branch(repo): f = gifi.feature.current(repo) return (f.target_remote, f.target_branch) else: return ('origin', 'master')
def configuration(repo=None): repo = get_repo(repo) return Configuration(repo, 'feature', { 'finish-with-rebase-interactive': (False, 'Should do a rebase interactive during feature finishing'), 'publish-with-pull-request': (False, 'Should create a pull request during feature publishing'), 'working-remote': ('origin', 'On which remote you are working at') })
def _start(feature=None, e=None): repo = get_repo() if e is None: e = gifi.epic.select() else: e = gifi.epic.Epic.parse(e) numbered_epic_features = list(map( lambda head: head.name.replace(e.to_string() + '/', ''), [head for head in repo.heads if re.match(r'%s/[0-9].*' % e.to_string(), head.name)])) feature_id = 1 if len(numbered_epic_features) > 0: feature_id = 1 + max(map( lambda epic_feature: int('0' + re.sub('_.*', '', epic_feature)), numbered_epic_features)) feature_branch = '%s/%03d' % (e.to_string(), feature_id) if feature: feature_branch = '%s_%s' % (feature_branch, feature) git_utils.check_repo_is_clean(repo) print('Starting ', feature_branch) _fetch(repo, e.remote) repo.create_head(feature_branch, e.to_string()) repo.heads[feature_branch].set_tracking_branch(repo.remotes[e.remote].refs[e.branch]) repo.heads[feature_branch].checkout()
def _publish(message=None): repo = get_repo() check_repo_is_clean(repo) config = configuration(repo) _push_working_branch(config, repo) if config.publish_with_pull_request: gifi.git_hub.request(repo, message)
def _rm(): repo = get_repo() config = configuration(repo) _print_list(repo, config) answer = ask('Which epic would you like to remove', 1) epics = _list_all(config) del epics[answer - 1] config.set('all', ','.join(epics))
def _add(epic): if epic is None: raise CommandException('No epic remote/branch given') repo = get_repo() config = configuration(repo) if epic in _list_all(config): raise CommandException('Epic %s is already added to the list' % epic) config.set('all', '%s,%s' % (config.all, epic))
def select(): repo = get_repo() config = configuration(repo) all = _list_all(config) if len(all) == 1: return Epic.parse(all[0]) _print_list(repo, config) answer = ask('Which epic would you like to select', 1) return Epic.parse(all[answer - 1])
def _print_list(repo=None, config=None): repo = get_repo(repo) if config is None: config = configuration(repo) print('List of epics:') i = 0 for epic in _list_all(config): i = i + 1 print(i, " - ", epic)
def _finish(): repo = get_repo() check_repo_is_clean(repo) config = configuration(repo) _current_feature_branch(repo) _rebase(repo, config) feature = current(repo) _push_working_branch(config, repo) repo.git.push(feature.target_remote, 'HEAD:%s' % feature.target_branch) _discard()
def _push(): repo = get_repo() check_repo_is_clean(repo) (target_remote, target_branch) = gifi.epic.current() base = '%s/%s' % (target_remote, target_branch) if repo.head.commit == repo.commit(base): raise CommandException( 'You are currently at %s, there is nothing to push' % base) commit_message = repo.head.commit.message repo.git.reset('--soft', 'HEAD^') repo.git.stash('save', _escape_new_lines(commit_message))
def __call__(self, config_level='global'): repo = git_utils.get_repo() config_writer = repo.config_writer(config_level) # it does not have to be recursive as there are only two levels for command in self.main.nested_commands(): if len(command.nested_commands()) != 0: for subcommand in command.nested_commands(): alias = '%s-%s' % (command.name, subcommand.name) value = '"!%s %s %s"' % (sys.argv[0], command.name, subcommand.name) config_writer.set_value('alias', alias, value) config_writer.release()
def _rebase(repo=None, config=None): repo = get_repo(repo) if config is None: config = configuration(repo) feature = current(repo) _fetch(repo, feature.target_remote) interactive = '-i' if config.finish_with_rebase_interactive else '' rebase_cmd = 'git rebase %s/%s %s' % (feature.target_remote, feature.target_branch, interactive) rebase_status = subprocess.call(rebase_cmd, shell=True) if rebase_status != 0: message = 'Rebase finished with an error, please fix it manually and then use "git rebase --continue"' raise CommandException(message)
def _discard(): repo = get_repo() config = configuration(repo) feature = current(repo) if repo.is_dirty(): if ask("There are uncommitted changes, would you like to remove them"): repo.git.reset('--hard', 'HEAD') else: return repo.git.checkout(feature.target_branch) try: repo.git.push(config.working_remote, ':%s' % feature.to_branch_name()) except GitCommandError as e: logging.warn('Unable to drop remote feature branch: %s' % e) print('WARNING: Unable to remove remote feature branch. Maybe it was not yet created?') repo.git.branch('-D', feature.to_branch_name()) repo.git.rebase('%s/%s' % (feature.target_remote, feature.target_branch)) repo.git.fetch('%s' % config.working_remote, '--prune')
def _pop(): repo = get_repo() check_repo_is_clean(repo) if repo.git.stash('list') == '': raise CommandException('There is nothing in the queue to pop from.') try: repo.git.stash('apply', '--index') except GitCommandError as e: if 'Try without --index' in e.stderr: try: repo.git.stash('apply') except GitCommandError: raise CommandException( 'Unable to pop automatically. Resolve conflicts then run queue-pop-finish.' ) else: raise CommandException( 'Unable to pop automatically. Resolve conflicts then run queue-pop-finish.' ) _pop_finish()
def _pop_finish(repo=None): repo = get_repo(repo) commit_message = repo.git.stash('list', '--max-count=1', '--oneline') commit_message = ': '.join(commit_message.split(': ')[2:]) repo.git.commit('-am', _unescape_new_lines(commit_message)) repo.git.stash('drop')
def request(repo=None, message=None): repo = get_repo(repo) try: _create_pull_request(repo, message) except GithubException as e: _handle_github_exception(e, 'create a pull request')
def configuration(repo=None): repo = get_repo(repo) return Configuration(repo, 'epic', { 'all': ('origin/master', 'A list of coma separated epic remote/branch`es') })
def _list(): repo = get_repo() return repo.git.stash('list')