summaryrefslogtreecommitdiffstats
path: root/src/subgit-save
blob: f2594e891d46ac4d26bf324e479327758b1219bd (plain) (blame)
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
#!/bin/bash

[ "$1" = "-h" ] && die $(cat <<-EOF
	Usage: subgit save [PATH..]

	  Updates .subgitrc with current remote, branch, and commit from
	  the bare repositories. Captures the current state of specified
	  subrepositories (or all if none specified).
	EOF
	)

root=$(subgit-sub root .)
source "$root/.subgitrc"

if [ $# -eq 0 ]; then
	subrepos=${!subgit[@]}
else
	subrepos=()
	for a in "$@"; do
		parent=$(dirname "$(realpath "$a")")
		[ "$root" != "$(subgit-sub root "$parent")" ] \
			&& die "Subgits do not have same parent"
		subrepos+=($(subgit-sub relpath "$root" "$a"))
	done
fi

for subrepo in $subrepos; do
	if [ ! -d "$root/.subgit/$subrepo" -o ! -d "$root/$subrepo" ]; then
		die "Subrepo $subrepo uninitialized"
	fi
	subgit[$subrepo]=1
	subgitinfo[$subrepo/remote]=$(git -C "$root/.subgit/$subrepo" remote get-url origin)
	subgitinfo[$subrepo/branch]=$(git -C "$root/.subgit/$subrepo" branch --show-current)
	subgitinfo[$subrepo/commit]=$(git -C "$root/.subgit/$subrepo" rev-parse --verify HEAD)
done

source subgit-write