Sublime Forum

[Sublime Merge]Hope for multi-user feature

#1

it will be very useful to switch different git account by a simple combo box, no editing again and again.

0 Likes

#2

The easiest way to do this currently is by using this method outlined by @OdatNurd: Execute external tool from SublimeMerge. I actually do something like this to swtich between my personal and work .gitconfig. Thought you could make one more complex if needed.

Basically, I have a script that scans the current .gitconfig and looks at the user.name to know what config is loaded. I have two backed up configs: .gitconfig-personal, .gitconfig-work. In this simple case, the script scans the current .gitconfig for the user name and copies the current info to it’s respective backup location and copies content from the other backed up profile to the current .gitconfig.

Then I simply create an alias as @OdatNurd outlined in his post and create a command in Default.sublime-commands. Then I can just run the command from the command palette and then press ctrl + r to reload/refresh the profile in Merge.

Maybe this will give some ideas or help.

1 Like

#3

Here is the script I use for my simple case:

#!/usr/bin/python3
"""Switch git profiles."""
import os
import re

RE_USER = re.compile(r'^\s*name\s*=\s*(.*)$', re.M)
WORK_NAME = "Name"

path = os.path.expanduser('~/.gitconfig')
if os.path.exists(path):
    with open(path, 'r') as f:
        buf = f.read()

    m = RE_USER.search(buf)
    if m:
        print('USER: ', m.group(1))
        user = m.group(1).strip()
        if user == WORK_NAME:
            msg = 'Switching to personal...'
            switch = path + '-personal'
        else:
            msg = 'Switching to work...'
            switch = path + '-work'
        if os.path.exists(switch):
            print(msg)
            with open(switch, 'r') as f:
                buf = f.read()
            with open(path, 'w') as f:
                f.write(buf)

Then I create a simple git alias in all the configs I have saved away:

[alias]
	switchProfile = "!f() { /usr/local/bin/git-switch; }; f"

Then I create a simple command in Default.sublime-commands in Merge’s Packages/User folder:

[
	{
		"caption": "Switch Profile",
		"command": "git", "args": {
			"argv": ["switchProfile"]
		}
	}
]

For me this takes the pain out of switching users. I guess you could allow the script to take a user name and create multiple commands that switches to a specific user, but I only need to switch between two.

5 Likes