[英]mercurial multiple usernames in hgrc
I am accessing multiple mercurial repositories and based on the host name, I want to configure with what name and email address I appear on each of them.我正在访问多个 mercurial 存储库,并根据主机名,我想配置我在每个存储库上显示的名称和电子邮件地址。
The obvious solution would be adding the 'username' to the ui section of each repo's hgrc file but I do not want to rely on this as these sandboxes get deleted every now and then.显而易见的解决方案是将“用户名”添加到每个 repo 的 hgrc 文件的 ui 部分,但我不想依赖于此,因为这些沙箱不时被删除。
Therefore, I need a central place where I can keep all this together.因此,我需要一个可以将所有这些放在一起的中心位置。 I'd ideally like a solution where I can map host names to usernames in the user specific hgrc file (~/.hgrc).理想情况下,我想要一个解决方案,我可以将主机名映射到用户特定的 hgrc 文件 (~/.hgrc) 中的用户名。
Is this possible?这可能吗?
Regards,问候,
[edit] Yes, @cyon's answer does the job. [编辑] 是的,@cyon 的回答可以完成这项工作。 I've just updated it to handle 'ssh://user@' type urls and also cope when there is no target folder in the clone command.我刚刚更新了它以处理 'ssh://user@' 类型的 url,并且还可以在克隆命令中没有目标文件夹时进行处理。
def merc_host_to_username_mapper(**kwargs):
host_to_username_map={'bitbucket.org' : 'your name <name@mail.com>'}
hg_pats = kwargs['pats']
merc_url = hg_pats[0]
merc_path_list = merc_url.split('://', 1)
if len(merc_path_list) == 1:
#print('ret1')
return
merc_sub_path = merc_path_list[-1].split('@',1)[-1]
while True:
#print('sub_path: ', merc_sub_path)
if merc_sub_path in host_to_username_map:
#print('found path, breaking')
break
else:
if len(merc_sub_path.rsplit('/', 1)) == 1:
#print('ret2')
return
else:
merc_sub_path = merc_sub_path.rsplit('/', 1)[0]
if len(hg_pats) is 1:
for folder in reversed(hg_pats[0].split('/')):
if folder:
hg_pats.append(folder)
#print('breaking ',folder)
break
if len(hg_pats) is 1:
#print('ret3')
return
#print('hg_pats: ', hg_pats)
with open(hg_pats[1] + "/.hg/hgrc", "a") as hgrc:
print("adding username \'" + host_to_username_map[merc_sub_path] + '\' to hgrc');
hgrc.write("[ui]\n");
hgrc.write("username=" + host_to_username_map[merc_sub_path] + "\n");
You could use a post-clone
hook to automate the adding of the 'username' to the ui seciton of each repo's hgrc.您可以使用post-clone
挂钩自动将“用户名”添加到每个 repo 的 hgrc 的 ui 部分。
This hook would then give you a place where to keep the centralized mapping from repo to username.然后这个钩子会给你一个地方来保存从 repo 到用户名的集中映射。
The code could look like this:代码可能如下所示:
~/.hgrc: ~/.hgrc:
[hooks]
post-clone=python:/path/to/script/name_chooser.py:chooser
name_chooser.py: name_chooser.py:
def chooser(**kwargs):
map={'https://bitbucket.org/yourrepo' : 'your_user'}
hg_pats = kwargs['pats']
if hg_pats[0] not in map:
return
with open(hg_pats[1] + "/.hg/hgrc", "a") as hgrc:
hgrc.write("[ui]\n");
hgrc.write("username=" + map[hg_pats[0]] + "\n");
The kwargs['pats']
is a list of the arguments to the hg clone
command. kwargs['pats']
是hg clone
命令的参数列表。 In this code I assume that you invoke clone like this:在这段代码中,我假设您像这样调用克隆:
hg clone https://bitbucket.org/yourrepo local_repo_path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.