加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

hgrc中的mercurial多个用户名

发布时间:2020-12-20 13:48:07 所属栏目:Python 来源:网络整理
导读:我正在访问多个mercurial存储库,并根据主机名,我想配置我出现在每个名称和电子邮件地址. 显而易见的解决方案是将“用户名”添加到每个repo的hgrc文件的ui部分,但我不想依赖于此,因为这些沙箱会不时被删除. 因此,我需要一个可以将所有这些保持在一起的中心位
我正在访问多个mercurial存储库,并根据主机名,我想配置我出现在每个名称和电子邮件地址.

显而易见的解决方案是将“用户名”添加到每个repo的hgrc文件的ui部分,但我不想依赖于此,因为这些沙箱会不时被删除.

因此,我需要一个可以将所有这些保持在一起的中心位置.我理想地喜欢一种解决方案,我可以将主机名映射到用户特定的hgrc文件(?/ .hgrc)中的用户名.

这可能吗?

问候,

[编辑]
是的,@ cyon的答案完成了这项工作.我刚刚更新它来处理’ssh:// user @’类型网址,并且当克隆命令中没有目标文件夹时也应对.

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");

解决方法

你可以使用一个后克隆钩子来自动添加’username’到每个repo的hgrc的ui secit.

然后,这个钩子将为您提供一个位置,用于保持从repo到用户名的集中映射.

代码可能如下所示:

?/ .hgrc:

[hooks]
post-clone=python:/path/to/script/name_chooser.py:chooser

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");

kwargs [‘pats’]是hg clone命令的参数列表.在这段代码中,我假设你像这样调用clone:

hg clone https://bitbucket.org/yourrepo local_repo_path

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读