我正在访问多个汞存储库,并且基于主机名,我想配置我在每个存储库上显示的名称和电子邮件地址。
显而易见的解决方案是将“用户名”添加到每个repo的hgrc文件的ui部分,但我不想依赖这一点,因为这些沙箱不时会被删除。
因此,我需要一个中心的地方,我可以把所有这些放在一起。理想情况下,我希望有一个解决方案,可以将主机名映射到用户特定的hgrc文件(~/.hgrc)中的用户名。
这个是可能的吗?
致以敬意,
编辑是的,@cyon的回答完成了这项工作。我刚刚更新了它,以处理'ssh://user@‘类型的urls,并在克隆命令中没有目标文件夹时进行处理。
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");发布于 2015-03-18 23:30:44
您可以使用一个post-clone钩子来自动将“用户名”添加到每个回购的hgrc的ui部分。
然后,这个钩子将为您提供一个地方来保持从repo到username的集中式映射。
代码可以如下所示:
~/.hgrc:
[hooks]
post-clone=python:/path/to/script/name_chooser.py:choosername_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");kwargs['pats']是hg clone命令的参数列表。在这段代码中,我假设您像这样调用克隆:
hg clone https://bitbucket.org/yourrepo local_repo_path
https://stackoverflow.com/questions/29133722
复制相似问题