summaryrefslogtreecommitdiff
path: root/bin/common/gt-cmd
blob: 56b8df69fc24c634ab7e29a0c19e999b93d1e651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh

# path to repo
repo="$1"
shift 2>/dev/null || exit 1
# git command
command="$1"
shift 2>/dev/null || exit 1
args="$*"

repo_pretty="$(printf '%s' "$repo" | sed "s@^$HOME@~@")"

if [ ! -d "$repo" ]; then
	printf '%s: missing\n' "$repo_pretty"
	exit 1
fi

# Check if repo's remote's key is in ssh-agent
# If key is not registered and command is push/pull we exit with error
# Note:
# path to key: ~/.ssh/<hostname>.pub
# where <hostname> is the same in ssh config
r="$(grep "$repo" "$REPOS" | cut -f 2 -d ' ' | cut -f 2 -d '@' | cut -f 1 -d ':')"
if { [ "$command" = "push" ] ||
     [ "$command" = "pull" ] ||
     [ "$command" = "fetch" ]; }
then
	Keyfile="$HOME/.ssh/$r.pub"
	if [ ! -f "$Keyfile" ]
	then
		printf '%s: no identity file '\''%s'\''\n' "$repo_pretty" "$Keyfile"
		exit 1
	fi

	if ! ssh-add -L | grep "$(cat "$Keyfile")" > /dev/null
	then
		printf '%s: '\''%s'\'' not in ssh-agent\n' "$repo_pretty" "$r" 
		exit 1
	fi
fi


git -C "$repo" "$command" $args >/dev/null 2>&1
[ $? -gt 0 ] && s="x" || s="o"
printf '%s: %s\n' "$repo_pretty" "$s"