示例#1
0
文件: stack.py 项目: hborchardt/stgit
    def initialise(cls, repository, name=None, switch_to=False):
        """Initialise a Git branch to handle patch series.

        @param repository: The L{Repository} where the L{Stack} will be created
        @param name: The name of the L{Stack}
        """
        if not name:
            name = repository.current_branch_name
        # make sure that the corresponding Git branch exists
        branch = Branch(repository, name)

        dir = os.path.join(repository.directory, cls._repo_subdir, name)
        if os.path.exists(dir):
            raise StackException('%s: branch already initialized' % name)

        if switch_to:
            branch.switch_to()

        # create the stack directory and files
        utils.create_dirs(dir)
        compat_dir = os.path.join(dir, 'patches')
        utils.create_dirs(compat_dir)
        PatchOrder.create(dir)
        config.set(stackupgrade.format_version_key(name),
                   str(stackupgrade.FORMAT_VERSION))

        return repository.get_stack(name)
    def initialise(cls,
                   repository,
                   name=None,
                   msg='initialise',
                   switch_to=False):
        """Initialise a Git branch to handle patch stack.

        :param repository: :class:`Repository` where the :class:`Stack` will be created
        :param name: the name of the :class:`Stack`

        """
        if not name:
            name = repository.current_branch_name
        # make sure that the corresponding Git branch exists
        branch = Branch(repository, name)

        stack_state_ref = _stack_state_ref(name)
        if repository.refs.exists(stack_state_ref):
            raise StackException('%s: stack already initialized' % name)

        if switch_to:
            branch.switch_to()

        stack_state = log.StackState.new_empty(branch.head)
        state_commit = stack_state.commit_state(repository, msg)
        repository.refs.set(stack_state_ref, state_commit, msg)

        return repository.get_stack(name)
示例#3
0
def __print_branch(branch_name, length):
    branch = Branch(directory.repository, branch_name)
    current = '>' if __is_current_branch(branch_name) else ' '
    try:
        stack = directory.repository.get_stack(branch_name)
    except StackException:
        initialised = protected = ' '
    else:
        initialised = 's'
        protected = 'p' if stack.protected else ' '

    out.stdout(current + ' ' + initialised + protected + '\t' +
               branch_name.ljust(length) + '  | ' +
               (branch.get_description() or ''))
示例#4
0
文件: stack.py 项目: hborchardt/stgit
    def create(
        cls,
        repository,
        name,
        create_at=None,
        parent_remote=None,
        parent_branch=None,
        switch_to=False,
    ):
        """Create and initialise a Git branch returning the L{Stack} object.

        @param repository: The L{Repository} where the L{Stack} will be created
        @param name: The name of the L{Stack}
        @param create_at: The Git id used as the base for the newly created
            Git branch
        @param parent_remote: The name of the remote Git branch
        @param parent_branch: The name of the parent Git branch
        """
        branch = Branch.create(repository, name, create_at=create_at)
        try:
            stack = cls.initialise(repository, name, switch_to=switch_to)
        except (BranchException, StackException):
            branch.delete()
            raise
        stack.set_parents(parent_remote, parent_branch)
        return stack
    def create(
        cls,
        repository,
        name,
        msg,
        create_at=None,
        parent_remote=None,
        parent_branch=None,
        switch_to=False,
    ):
        """Create and initialise a Git branch returning the :class:`Stack` object.

        :param repository: :class:`Repository` where the :class:`Stack` will be created
        :param name: name of the :class:`Stack`
        :param msg: message to use in newly created log
        :param create_at: Git id used as the base for the newly created Git branch
        :param parent_remote: name of the parent remote Git branch
        :param parent_branch: name of the parent Git branch

        """
        branch = Branch.create(repository, name, create_at=create_at)
        try:
            stack = cls.initialise(repository, name, msg, switch_to=switch_to)
        except (BranchException, StackException):
            branch.delete()
            raise
        stack.set_parents(parent_remote, parent_branch)
        return stack
示例#6
0
    def initialise(cls, repository, name=None, msg='initialise', switch_to=False):
        """Initialise a Git branch to handle patch series.

        @param repository: The L{Repository} where the L{Stack} will be created
        @param name: The name of the L{Stack}
        """
        if not name:
            name = repository.current_branch_name
        # make sure that the corresponding Git branch exists
        branch = Branch(repository, name)

        stack_state_ref = _stack_state_ref(name)
        if repository.refs.exists(stack_state_ref):
            raise StackException('%s: stack already initialized' % name)

        dir = os.path.join(repository.directory, cls._repo_subdir, name)
        if os.path.exists(dir):
            raise StackException('%s: branch already initialized' % name)

        if switch_to:
            branch.switch_to()

        # create the stack directory and files
        utils.create_dirs(dir)
        compat_dir = os.path.join(dir, 'patches')
        utils.create_dirs(compat_dir)
        PatchOrder.create(dir)
        config.set(
            stackupgrade.format_version_key(name), str(stackupgrade.FORMAT_VERSION)
        )

        state_commit = log.StackState(
            repository,
            prev=None,
            head=branch.head,
            applied=[],
            unapplied=[],
            hidden=[],
            patches={},
            message=msg,
        ).commit_state()
        repository.refs.set(stack_state_ref, state_commit, msg)

        return repository.get_stack(name)
示例#7
0
def __delete_branch(doomed_name, force=False):
    if __is_current_branch(doomed_name):
        raise CmdException('Cannot delete the current branch')

    branch = Branch(directory.repository, doomed_name)
    try:
        stack = directory.repository.get_stack(doomed_name)
    except StackException:
        stack = None

    if stack:
        if stack.protected:
            raise CmdException('This branch is protected. Delete is not permitted')
        if not force and stack.patchorder.all:
            raise CmdException('Cannot delete: the series still contains patches')

    out.start('Deleting branch "%s"' % doomed_name)
    if stack:
        stack.cleanup()
    branch.delete()
    out.done()
示例#8
0
文件: stack.py 项目: hborchardt/stgit
 def __init__(self, repository, name):
     Branch.__init__(self, repository, name)
     self.patchorder = PatchOrder(self)
     self.patches = Patches(self)
     if not stackupgrade.update_to_current_format_version(repository, name):
         raise StackException('%s: branch not initialized' % name)
示例#9
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)