@@ -156,15 +156,27 @@ To have multiple identities, all I have to do is:
156156 (r"opt/private", "secret"),
157157 ]
158158
159- # If any of the ssh arguments have 'cweb' in it, the 'personal' identity
160- # has to be used. For example: "ssh myhost.cweb.com" will have cweb in
161- # argv, and the "personal" identity will be used.
162- # This is optional - don't include any MATCH_ARGV if you don't
163- # need it.
159+ # Specifies which identity to use depending on the arguments I'm running
160+ # ssh with. At first each element is separately tested against all patterns,
161+ # then a string of all elements is tested against all patterns.
162+ # This is optional - don't include any MATCH_ARGV if you don't need it.
163+ # For example: If any of the ssh arguments have 'cweb' in it, the 'personal'
164+ # identity has to be used. "ssh myhost.cweb.com" will have cweb in argv, and
165+ # the "personal" identity will be used.
164166 MATCH_ARGV = [
165167 (r"cweb", "personal"),
166168 (r"corp", "work"),
167169 ]
170+ # Another example: Choose an identity for git depending on the accessed
171+ # repository on GitHub (similar for GitLab, Bitbucket, etc.).
172+ # Recognize the elements with the host and the repository path to switch
173+ # between 'personal' and 'work' identity.
174+ MATCH_ARGV = [
175+ (r"\s(git@)?github\.com\s.*'company\/.+\.git'", "work"),
176+ (r"\s(git@)?github\.com\s.*'ccontavalli\/.+\.git'", "personal"),
177+ (r"\s(git@)?gist\.github\.com\s.*'abcdef01234567890fedcba912345678\.git'", "work"),
178+ (r"^(git@)?(gist\.)?github\.com$", "personal"),
179+ ]
168180
169181 # Note that if no match is found, the DEFAULT_IDENTITY is used. This is
170182 # generally your loginname, no need to change it.
@@ -506,7 +518,7 @@ class Config(object):
506518 """Sets configuration option parameter to value."""
507519 self .values [parameter ] = value
508520
509- def FindIdentityInList (elements , identities ):
521+ def FindIdentityInList (elements , identities , all_elements ):
510522 """Matches a list of identities to a list of elements.
511523
512524 Args:
@@ -518,9 +530,22 @@ def FindIdentityInList(elements, identities):
518530 The identity specified in identities for the first regular expression
519531 matching the first element in elements.
520532 """
533+ # Test against each element separately
521534 for element in elements :
522535 for regex , identity in identities :
523536 if re .search (regex , element ):
537+ print ("Matching: {0}" .format (element ),
538+ file = sys .stderr ,
539+ loglevel = LOG_DEBUG )
540+ return identity
541+ # Test against all elements in a single string
542+ if all_elements and len (elements ) > 1 :
543+ element = " " .join (elements )
544+ for regex , identity in identities :
545+ if re .search (regex , element ):
546+ print ("Matching: {0}" .format (element ),
547+ file = sys .stderr ,
548+ loglevel = LOG_DEBUG )
524549 return identity
525550 return None
526551
@@ -537,8 +562,8 @@ def FindIdentity(argv, config):
537562 """
538563 paths = set ([os .getcwd (), os .path .abspath (os .getcwd ()), os .path .normpath (os .getcwd ())])
539564 return (
540- FindIdentityInList (argv , config .Get ("MATCH_ARGV" )) or
541- FindIdentityInList (paths , config .Get ("MATCH_PATH" )) or
565+ FindIdentityInList (argv , config .Get ("MATCH_ARGV" ), True ) or
566+ FindIdentityInList (paths , config .Get ("MATCH_PATH" ), False ) or
542567 config .Get ("DEFAULT_IDENTITY" ))
543568
544569def FindKeys (identity , config ):
0 commit comments