main: add git identity via "origin" remote

This commit is contained in:
Roman Zeyde 2016-02-19 20:48:16 +02:00
parent 5b0e56697f
commit 41b30b42b5

View File

@ -2,7 +2,9 @@
import argparse import argparse
import functools import functools
import logging import logging
import re
import os import os
import subprocess
import sys import sys
import time import time
@ -61,6 +63,18 @@ def setup_logging(verbosity):
logging.basicConfig(format=fmt, level=level) logging.basicConfig(format=fmt, level=level)
def git_host(remote_name):
output = subprocess.check_output('git config --local --list'.split())
pattern = r'remote\.{}\.url=(.*)'.format(remote_name)
matches = re.findall(pattern, output)
log.debug('git remote "%r": %r', remote_name, matches)
if len(matches) != 1:
raise ValueError('{:d} git remotes found: %s', matches)
url = matches[0].strip()
user, url = url.split('@', 1)
host, path = url.split(':', 1)
return 'ssh://{}@{}/{}'.format(user, host, path)
def ssh_sign(conn, label, blob): def ssh_sign(conn, label, blob):
"""Perform SSH signature using given hardware device connection.""" """Perform SSH signature using given hardware device connection."""
now = time.strftime('%Y-%m-%d %H:%M:%S') now = time.strftime('%Y-%m-%d %H:%M:%S')
@ -76,6 +90,13 @@ def run_agent(client_factory):
label = args.identity label = args.identity
command = args.command command = args.command
if label == 'git':
label = git_host('origin')
log.debug('Git identity: %r', label)
if args.command:
command = ['git'] + args.command
log.debug('Git command: %r', command)
public_key = conn.get_public_key(label=label) public_key = conn.get_public_key(label=label)
if args.connect: if args.connect: