【问题标题】:Python sh, set user.email to commit (Git)Python sh,将 user.email 设置为提交 (Git)
【发布时间】:2015-10-03 15:38:42
【问题描述】:

我正在与 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 正确设置当前用户提交?

【问题讨论】:

标签: python django git sh


【解决方案1】:

这是感兴趣的部分:

    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 &lt;email@example.com&gt;

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.

【讨论】:

  • 对,这就是问题所在,所以,我使用自定义用户,有 kwargs['author'] = author ,现在我使用 from django.contrib.auth import get_user_model User = get_user_model() 但得到 django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
  • 因为这是一个不同的错误,我建议打开另一个问题,提供更多详细信息。太具体或有太多部分的问题对其他用户没有用处;一个人能够完整回答整个多部分问题的可能性较小。
猜你喜欢
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 2022-12-06
  • 2011-04-14
相关资源
最近更新 更多