Coder Social home page Coder Social logo

samrocketman / git-identity-manager Goto Github PK

View Code? Open in Web Editor NEW
91.0 6.0 10.0 53 KB

Git identity manager. It allows you to switch between git identities for user, name, and SSH private key used for authoring and publishing git commits.

License: MIT License

Shell 100.00%
git-alias git identity-management idm bash git-subcommand

git-identity-manager's Introduction

Git Identity Manager

Ever find that managing multiple git identities is a pain? This git transport attempts to make it less painful.

Knowing who you are can be painful

Image credit PBS.org

Requirements

bash, awk, sed, and openssh are available by default on Mac OS X, BSD, and most flavors of GNU/Linux. Git likely needs to be installed. On Mac OS X, installing Git through homebrew is recommended.

Installation

Add git-idm script to your $PATH and make it executable. Then, you can access the script via git idm. See git idm help for usage.

Uninstall

Remove all data stored in $HOME/.gitconfig related to git idm. This will not affect settings used by git.

git idm uninstall

Quick start

Add your first identity.

git idm add jcool --name "Joe Cool" --email [email protected] --key ~/.ssh/id_rsa

Activate your identity.

git idm use jcool

Show which identity is active.

git idm active

List all known identities.

git idm list

For more commands see git idm help.

Autotracked identities

You can configure your Git identities to automatically switch depending on what directory you have cloned. For example, let's say you have personal projects and work projects on the same laptop. Assuming you have a work identity and a personal identity configured, the following commands would help you auto-switch identites for repositories under designated paths.

git idm track work --directory ~/git/work
git idm track personal --directory ~/git/github

You can list what directories are tracked by a given identity.

git idm list work --tracked

Which will return output like the following.

work identity will automatically apply to the following directories:
    /home/user/git/work/

Verify the identity has switched with git config user.email.

cd ~/git/work
mkdir example
cd example/
git init

# the email identity should show your work email
git config user.email

License

MIT License

git-identity-manager's People

Contributors

0xnero avatar samrocketman avatar vvdaal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

git-identity-manager's Issues

git idm active doesn't show the tracked ID

// Track ~/GitRepos/personal to use "personal" ID
~/GitRepos/personal
$ git idm track personal --directory $(pwd)
git config --file /Users/calvin/.gitconfig_idm_personal user.name Calvin Sangbin Park
git config --file /Users/calvin/.gitconfig_idm_personal user.email [email protected]
git config --file /Users/calvin/.gitconfig_idm_personal core.sshCommand ssh -i /Users/calvin/.ssh/id_rsa.github-personal.NVMBP -o IdentitiesOnly=yes -F /dev/null
git config --global includeIf.gitdir:/Users/calvin/GitRepos/personal/.idm personal
git config --global includeIf.gitdir:/Users/calvin/GitRepos/personal/.path /Users/calvin/.gitconfig_idm_personal

// Go to a root of all repos dir
~/GitRepos
$ cd ..

// Check that "work" ID is active
~/GitRepos
$ git idm active
work
    name=Calvin Park
    [email protected]
    sshkey=/Users/calvin/.ssh/id_rsa
    sshcommand=ssh -i /Users/calvin/.ssh/id_rsa -o IdentitiesOnly=yes -F /dev/null
WARNING: /Users/calvin/.ssh/id_rsa has not been added to ssh-agent.  To fix run: ssh-add '/Users/calvin/.ssh/id_rsa'

// Go into ~/GitRepos/personal/book/jenkins-shared-library/ where git idm tracks to use "personal" ID
~/GitRepos
$ cd personal/book/jenkins-shared-library/

// git idm active reports that it's using "work" ID
~/GitRepos/personal/book/jenkins-shared-library
$ git idm active
work
    name=Calvin Park
    [email protected]
    sshkey=/Users/calvin/.ssh/id_rsa
    sshcommand=ssh -i /Users/calvin/.ssh/id_rsa -o IdentitiesOnly=yes -F /dev/null
WARNING: /Users/calvin/.ssh/id_rsa has not been added to ssh-agent.  To fix run: ssh-add '/Users/calvin/.ssh/id_rsa'

// when in reality it's using "personal" ID
~/GitRepos/personal/book/jenkins-shared-library
$ git config user.email
[email protected]

A following git commit showed that "personal" ID is indeed is active, which means git track is working.

Bug: Tracking directories before activating an identity for the first time causes them not to track.

I ran into an issue when trying to automate setup for new machines, where .gitconfig is empty. If git-idm track is invoked before git-idm use is invoked for the first time (and user.name and include.path aren't already set), then use will create the sections [user] and [include] after [includeif ...], and those sections will always take precedence and remain active even when in an auto-tracked directory.

It's possible to workaround this by activating an identity first before tracking any directories, but the behavior surprised and confused me.

To reproduce

# create a dummy repository
mkdir -p ~/Work/bizcorp-repo
git init ~/Work/bizcorp-repo

# configure git identities
git-idm add personal --name "JaNe DoPe" --email "[email protected]" --ssh-command "..."
git-idm add work     --name "Jane Doe"  --email "[email protected]"   --ssh-command "..."
git-idm track work   --directory ~/Work
git-idm use personal

# check that it worked
cd ~/Work/bizcorp-repo
git config user.name # -> JaNe DoPe

# oh no, it didn't! how unprofessional.

Explanation

The commands above create ~/.gitconfig like this:

[gitidm "personal"]
	name = JaNe DoPe
	email = [email protected]
	sshCommand = ...
[gitidm "work"]
	name = Jane Doe
	email = [email protected]
	sshCommand = ...
[includeIf "gitdir:~/Work"]
	idm = work
	path = ~/.gitconfig_idm_work
[user]
	name = JaNe DoPe
	email = [email protected]
	activeidm = personal
[include]
	path = ~/.gitconfig_idm_personal

Because the [user] and [include] sections exist below [includeIf], they end up overwriting the tracking configs.

Solution

I don't think git-config has any commands for re-ordering sections, so I think invoking git-idm track should check first if an active identity is set (i.e. if key user.name and include.path already exist), and if not, try to activate one of them in order to populate those sections with dummy values prior to the creation of include rules which should overwrite them.

Kind of rusty with Bash scripting right now, but I might try to implement the fix myself.

Change git idm automatically based on project directory location

https://dev.to/maxlmator/maintaining-different-git-identities

It would be nice for identities to auto-select based on a local directory (optionally given).

[includeIf "gitdir:~/git/github/"]
    path = /home/sam/.gitconfig_idm_github

And the contents of /home/sam/.gitconfig_idm_github would be...

[user]
    name = Sam Gleske
    email = [email protected]
[core]
    sshCommand = ssh -i /home/sam/.ssh/id_rsa -o IdentitiesOnly=yes -F /dev/null

Assuming that the git idm identity is...

[user]
    name = Sam Gleske
    email = [email protected]
    activeidm = github
[gitidm "github"]
    name = Sam Gleske
    email = [email protected]
    sshKey = /home/sam/.ssh/id_rsa
    sshCommand = ssh -i /home/sam/.ssh/id_rsa -o IdentitiesOnly=yes -F /dev/null

This will require a minimum version of git 2.13.0 for this.

Brainstorming feature

I think it makes sense to add this as a sub-command git idm directory which associates the identity name with the directory.

# personal projects
git idm directory add github ~/git/github/

# work projects
git idm directory add work ~/git/work/

# generically
git idm directory [add|remove] [git idm identity] [project path to apply identity]

We could tie the identity with an idm subkey in .gitconfig like:

[includeIf "gitdir:~/git/github/"]
    idm = github
    path = /home/sam/.gitconfig_idm_github

When removing an identity we'll want it to search for all includeIf settings where idm key is equal to the identity being removed. We'll also want to delete the file.

Using the prefix .gitconfig_idm_[git idm identity] for the file seems like the best way to avoid clashing with potential user configuration files.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.