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.
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.
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).
Is this possible?
Regards,
[edit] Yes, @cyon's answer does the job. I've just updated it to handle 'ssh://user@' type urls and also cope when there is no target folder in the clone command.
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");