diff --git a/git_trac/app.py b/git_trac/app.py index 6a51664..7549807 100644 --- a/git_trac/app.py +++ b/git_trac/app.py @@ -117,7 +117,29 @@ def checkout(self, ticket_or_branch, branch_name=None): else: branch = str(ticket_or_branch) self.repo.checkout_new_branch(branch, branch) - + + def old(self, ticket_or_branch, branch_name=None): + if ticket_or_branch.is_number(): + ticket_number = int(ticket_or_branch) + print('Loading ticket #{0}...'.format(ticket_number)) + ticket = self.trac.load(ticket_number) + if len(ticket.branch) == 0: + raise ValueError('No branch attached to #{0}'.format(ticket_number)) + remote = ticket.branch + local = self._validate_branch(branch_name, ticket_number, ticket) + else: + remote = local = str(ticket_or_branch) + self.repo.merge_into_develop(remote, local) + + def _validate_branch(self, branch_name, ticket_number, ticket): + if branch_name is None: + branch = self.suggest_local_branch(ticket_number, ticket.branch) + else: + branch = branch_name.strip() + if len(branch) == 0: + raise ValueError('no local branch specified') + return branch + def _checkout_ticket(self, ticket_number, branch_name=None): print('Loading ticket #{0}...'.format(ticket_number)) ticket = self.trac.load(ticket_number) @@ -133,12 +155,7 @@ def _checkout_ticket(self, ticket_number, branch_name=None): print('Newly created local branch: {0}'.format(local)) self.repo.create(local) return - if branch_name is None: - branch = self.suggest_local_branch(ticket_number, ticket.branch) - else: - branch = branch_name.strip() - if len(branch) == 0: - raise ValueError('no local branch specified') + branch = self._validate_branch(branch_name, ticket_number, ticket) print('Checking out Trac #{0} remote branch {1} -> local branch {2}...' .format(ticket_number, ticket.branch, branch)) self.repo.checkout_new_branch(ticket.branch, branch) diff --git a/git_trac/cmdline.py b/git_trac/cmdline.py index 037d876..121455c 100644 --- a/git_trac/cmdline.py +++ b/git_trac/cmdline.py @@ -125,6 +125,13 @@ def make_parser(): parser_checkout.add_argument('ticket_or_branch', type=TicketOrBranch, help='Ticket number or remote branch name') + parser_old = subparsers.add_parser('old', help='Download branch, merge into develop') + parser_old.add_argument('-b', '--branch', dest='branch_name', + help='Local branch name', + default=None) + parser_old.add_argument('ticket_or_branch', type=TicketOrBranch, + help='Ticket number or remote branch name') + parser_search = subparsers.add_parser('search', help='Search trac') parser_search.add_argument('--branch', dest='branch_name', help='Remote git branch name (default: local branch)', @@ -213,6 +220,8 @@ def launch(): app.create(args.summary, args.branch_name) elif args.subcommand == 'checkout': app.checkout(args.ticket_or_branch, args.branch_name) + elif args.subcommand == 'old': + app.old(args.ticket_or_branch, args.branch_name) elif args.subcommand == 'fetch': app.fetch(args.ticket_or_branch) elif args.subcommand == 'pull': diff --git a/git_trac/git_repository.py b/git_trac/git_repository.py index 5d5e370..7c77726 100644 --- a/git_trac/git_repository.py +++ b/git_trac/git_repository.py @@ -24,7 +24,7 @@ ############################################################################## -import re +import re, os import textwrap from .cached_property import cached_property @@ -53,7 +53,7 @@ def git(self): def master(self): head = self.git.show_ref('master', head=True) return GitCommit(self, head[0:40]) - + @property def head(self): head = self.git.show_ref('HEAD', head=True) @@ -138,10 +138,33 @@ def checkout_new_branch(self, remote, local): self.git.checkout(local) self.set_upstream(remote) + def merge_into_develop(self, remote, local): + self.git.fetch('trac', 'develop') + if self.git.exit_code.show_ref('refs/heads/' + local) == 0: + # local branch exists + # We don't use working_tree since there's no risk + # of checking out a very old branch + current_branch = self.git.show_ref('refs/heads/' + local, hash=True) + try: + self.git.branch(local,'FETCH_HEAD',f=True) + self.git.checkout(local) + self.git.merge(current_branch) + print('Merged local branch into develop.') + except GitError: + self.git.branch(local,current_branch,f=True) + print('Merge of local branch into develop failed.') + return + else: + self.git.branch(local, 'FETCH_HEAD') + self.git.checkout(local) + self.set_upstream(remote) + self.git.fetch('trac',remote) + self.git.merge('FETCH_HEAD') + def create(self, local, starting_branch='develop'): """ Create new branch. - """ + """ self.git.fetch('trac', starting_branch) self.git.branch(local, 'FETCH_HEAD') self.git.checkout(local)