Python sh,将user.email设置为commit(Git)

时间:2015-07-14 19:58:01

标签: python django git sh

我正在使用Waliki,特别是使用waliki.git,

Waliki.git使用sh来管理Git,所以在执行提交时,用户被设置为settings.WALIKI_COMMITTER_EMAIL, 寻找来源(如下),user .git/config永远不会改变。

class Git(object):
    __shared_state = {}     # it's a Borg

    def __init__(self):
        self.__dict__ = self.__shared_state
        from waliki.settings import WALIKI_DATA_DIR
        self.content_dir = WALIKI_DATA_DIR
        os.chdir(self.content_dir)
        if not os.path.isdir(os.path.join(self.content_dir, '.git')):
            git.init()
            git.config("user.email", settings.WALIKI_COMMITTER_EMAIL)
            git.config("user.name", settings.WALIKI_COMMITTER_NAME)    
        self.git = git

    def commit(self, page, message='', author=None, parent=None, extra_path=None):
        path = page.path
        paths_to_commit = [path]
        if extra_path:
            paths_to_commit.append(extra_path)
        kwargs = {}
        if isinstance(author, User) and author.is_authenticated():
            kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email)
        elif isinstance(author, six.string_types):
            kwargs['author'] = author

        try:
            there_were_changes = parent and parent != self.last_version(page)
            status = git.status('--porcelain', path).stdout.decode('utf8')[:2]
            if parent and status != "UU":
                git.stash()
                git.checkout('--detach', parent)
                try:
                    git.stash('pop')
                except:
                    git.checkout('--theirs', path)

            if status == 'UU':
                # See http://stackoverflow.com/a/8062976/811740
                kwargs['i'] = True

            git.add(path)
            git_commit_cmd = git.commit.bake(allow_empty=True, allow_empty_message=True, m=message, **kwargs)
            git_commit_cmd('--', *paths_to_commit)
            last = self.last_version(page)
            if parent and status != "UU":
                git.checkout('master')
                git.merge(last)
        except ErrorReturnCode as e:
            # TODO: make this more robust!
            error = e.stdout.decode('utf8')
            if 'CONFLICT' in error:
                # For '-i' attribute see http://stackoverflow.com/q/5827944/811740
                git_commit_cmd = git.commit.bake(allow_empty=True, allow_empty_message=True, m=_('Merged with conflict'), i=True, **kwargs)
                git_commit_cmd('--', *paths_to_commit)
                raise Page.EditionConflict(_('Automatic merge failed. Please, fix the conflict and save the page.'))
            else:
                raise
        return there_were_changes

我添加了一些代码,所以:

类Git(对象):         __shared_state = {}#这是博格

    def __init__(self, author):
        self.__dict__ = self.__shared_state
        from waliki.settings import WALIKI_DATA_DIR
        self.content_dir = WALIKI_DATA_DIR
        os.chdir(self.content_dir)
        if not os.path.isdir(os.path.join(self.content_dir, '.git')):
            git.init()
            git.config("user.email", settings.WALIKI_COMMITTER_EMAIL)
            git.config("user.name", settings.WALIKI_COMMITTER_NAME)    
        else:
            git.config("user.email", author)
            git.config("user.name", author)
        self.git = git

但得到了:

  RAN: '/usr/bin/git --no-pager commit -m Page created --allow-empty --allow-empty-message --author=rizotastack@gmail.com -- content.md'

  STDOUT:


  STDERR:
fatal: No existing author found with 'rizotastack@gmail.com'

我如何才能让Git正确设置当前用户?

1 个答案:

答案 0 :(得分:0)

这是感兴趣的部分:

    if isinstance(author, User) and author.is_authenticated():
        kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email)
    elif isinstance(author, six.string_types):
        kwargs['author'] = author

您需要使用格式Full Name <email@example.com>指定作者。

From the docs

--author=<author>

Override the commit author. Specify an explicit author using the standard
"A U Thor <author@example.com>" format. Otherwise <author> is assumed to be a
pattern and is used to search for an existing commit by that author (i.e.
rev-list --all -i --author=<author>); the commit author is then copied from the
first such commit found.