Example #1
0
    def run(self,
            iw=None,
            set_head=True,
            allow_bad_head=False,
            print_current_patch=True):
        """Execute the transaction. Will either succeed, or fail (with an
        exception) and do nothing."""
        self._check_consistency()
        log_external_mods(self.stack)
        new_head = self.head

        # Set branch head.
        if set_head:
            if iw:
                try:
                    self._checkout(new_head.data.tree, iw, allow_bad_head)
                except CheckoutException:
                    # We have to abort the transaction.
                    self.abort(iw)
                    self._abort()
            self.stack.set_head(new_head, self._msg)

        if self._error:
            if self._conflicts:
                out.error(*([self._error] + self._conflicts))
            else:
                out.error(self._error)

        old_applied = self.stack.patchorder.applied
        msg = self._msg + (' (CONFLICT)' if self._conflicts else '')

        # Write patches.
        for pn, commit in self.patches.items():
            if self.stack.patches.exists(pn):
                p = self.stack.patches.get(pn)
                if commit is None:
                    p.delete()
                else:
                    p.set_commit(commit, msg)
            else:
                self.stack.patches.new(pn, commit, msg)
        self.stack.patchorder.set_order(self._applied, self._unapplied,
                                        self._hidden)
        log_entry(self.stack, msg)

        if print_current_patch:
            _print_current_patch(old_applied, self._applied)

        if self._error:
            return utils.STGIT_CONFLICT
        else:
            return utils.STGIT_SUCCESS
Example #2
0
 def write(msg):
     for pn, commit in self.__patches.items():
         if self.__stack.patches.exists(pn):
             p = self.__stack.patches.get(pn)
             if commit is None:
                 p.delete()
             else:
                 p.set_commit(commit, msg)
         else:
             self.__stack.patches.new(pn, commit, msg)
     self.__stack.patchorder.applied = self.__applied
     self.__stack.patchorder.unapplied = self.__unapplied
     self.__stack.patchorder.hidden = self.__hidden
     log.log_entry(self.__stack, msg)
Example #3
0
 def write(msg):
     for pn, commit in self.__patches.iteritems():
         if self.__stack.patches.exists(pn):
             p = self.__stack.patches.get(pn)
             if commit == None:
                 p.delete()
             else:
                 p.set_commit(commit, msg)
         else:
             self.__stack.patches.new(pn, commit, msg)
     self.__stack.patchorder.applied = self.__applied
     self.__stack.patchorder.unapplied = self.__unapplied
     self.__stack.patchorder.hidden = self.__hidden
     log.log_entry(self.__stack, msg)
Example #4
0
def func(parser, options, args):
    """Rename a patch in the series"""
    stack = directory.repository.get_stack(options.branch)

    if len(args) == 2:
        old, new = args
    elif len(args) == 1:
        if not stack.patchorder.applied:
            raise CmdException("No applied top patch to rename exists.")
        old = stack.patchorder.applied[-1]
        new = args[0]
    else:
        parser.error('incorrect number of arguments')

    out.start('Renaming patch "%s" to "%s"' % (old, new))
    stack.rename_patch(old, new)
    log_entry(stack, 'rename %s to %s' % (old, new))
    out.done()
Example #5
0
def func(parser, options, args):
    repository = directory.repository

    if options.create:
        if len(args) == 0 or len(args) > 2:
            parser.error('incorrect number of arguments')

        branch_name = args[0]
        committish = None if len(args) < 2 else args[1]

        if committish:
            check_local_changes(repository)
        check_conflicts(repository.default_iw)
        try:
            stack = repository.get_stack()
        except (DetachedHeadException, StackException):
            pass
        else:
            check_head_top_equal(stack)

        stack = __create_branch(branch_name, committish)

        out.info('Branch "%s" created' % branch_name)
        log.log_entry(stack, 'branch --create %s' % stack.name)
        return

    elif options.clone:

        cur_branch = Branch(repository, repository.current_branch_name)
        if len(args) == 0:
            clone_name = cur_branch.name + time.strftime('-%C%y%m%d-%H%M%S')
        elif len(args) == 1:
            clone_name = args[0]
        else:
            parser.error('incorrect number of arguments')

        check_local_changes(repository)
        check_conflicts(repository.default_iw)
        try:
            stack = repository.current_stack
        except StackException:
            stack = None
            base = repository.refs.get(repository.head_ref)
        else:
            check_head_top_equal(stack)
            base = stack.base

        out.start('Cloning current branch to "%s"' % clone_name)
        clone = Stack.create(
            repository,
            name=clone_name,
            create_at=base,
            parent_remote=cur_branch.parent_remote,
            parent_branch=cur_branch.name,
        )
        if stack:
            for pn in stack.patchorder.all_visible:
                patch = stack.patches.get(pn)
                clone.patches.new(pn, patch.commit, 'clone %s' % stack.name)
            clone.patchorder.set_order(applied=[],
                                       unapplied=stack.patchorder.all_visible,
                                       hidden=[])
            trans = StackTransaction(clone, 'clone')
            try:
                for pn in stack.patchorder.applied:
                    trans.push_patch(pn)
            except TransactionHalted:
                pass
            trans.run()
        prefix = 'branch.%s.' % cur_branch.name
        new_prefix = 'branch.%s.' % clone.name
        for n, v in list(config.getstartswith(prefix)):
            config.set(n.replace(prefix, new_prefix, 1), v)
        clone.set_description('clone of "%s"' % cur_branch.name)
        clone.switch_to()
        out.done()

        log.copy_log(log.default_repo(), cur_branch.name, clone.name,
                     'branch --clone')
        return

    elif options.delete:

        if len(args) != 1:
            parser.error('incorrect number of arguments')
        __delete_branch(args[0], options.force)
        log.delete_log(log.default_repo(), args[0])
        return

    elif options.cleanup:

        if not args:
            name = repository.current_branch_name
        elif len(args) == 1:
            name = args[0]
        else:
            parser.error('incorrect number of arguments')
        __cleanup_branch(name, options.force)
        log.delete_log(log.default_repo(), name)
        return

    elif options.list:

        if len(args) != 0:
            parser.error('incorrect number of arguments')

        branch_names = sorted(
            ref.replace('refs/heads/', '', 1) for ref in repository.refs
            if ref.startswith('refs/heads/') and not ref.endswith('.stgit'))

        if branch_names:
            out.info('Available branches:')
            max_len = max(len(name) for name in branch_names)
            for branch_name in branch_names:
                __print_branch(branch_name, max_len)
        else:
            out.info('No branches')
        return

    elif options.protect:

        if len(args) == 0:
            branch_name = repository.current_branch_name
        elif len(args) == 1:
            branch_name = args[0]
        else:
            parser.error('incorrect number of arguments')

        try:
            stack = repository.get_stack(branch_name)
        except StackException:
            raise CmdException('Branch "%s" is not controlled by StGIT' %
                               branch_name)

        out.start('Protecting branch "%s"' % branch_name)
        stack.protected = True
        out.done()

        return

    elif options.rename:

        if len(args) == 1:
            stack = repository.current_stack
            new_name = args[0]
        elif len(args) == 2:
            stack = repository.get_stack(args[0])
            new_name = args[1]
        else:
            parser.error('incorrect number of arguments')

        old_name = stack.name
        stack.rename(new_name)

        out.info('Renamed branch "%s" to "%s"' % (old_name, new_name))
        log.rename_log(repository, old_name, new_name, 'branch --rename')
        return

    elif options.unprotect:

        if len(args) == 0:
            branch_name = repository.current_branch_name
        elif len(args) == 1:
            branch_name = args[0]
        else:
            parser.error('incorrect number of arguments')

        try:
            stack = repository.get_stack(branch_name)
        except StackException:
            raise CmdException('Branch "%s" is not controlled by StGIT' %
                               branch_name)

        out.info('Unprotecting branch "%s"' % branch_name)
        stack.protected = False
        out.done()

        return

    elif options.description is not None:

        if len(args) == 0:
            branch_name = repository.current_branch_name
        elif len(args) == 1:
            branch_name = args[0]
        else:
            parser.error('incorrect number of arguments')

        Branch(repository, branch_name).set_description(options.description)
        return

    elif len(args) == 1:
        branch_name = args[0]
        if branch_name == repository.current_branch_name:
            raise CmdException('Branch "%s" is already the current branch' %
                               branch_name)

        if not options.merge:
            check_local_changes(repository)
        check_conflicts(repository.default_iw)
        try:
            stack = repository.get_stack()
        except StackException:
            pass
        else:
            check_head_top_equal(stack)

        out.start('Switching to branch "%s"' % branch_name)
        Branch(repository, branch_name).switch_to()
        out.done()
        return

    # default action: print the current branch
    if len(args) != 0:
        parser.error('incorrect number of arguments')

    out.stdout(directory.repository.current_branch_name)