summaryrefslogtreecommitdiffstats
path: root/src/subgit-check
blob: d86dc3280b9f7372b61965ea5f792adcd5f09ea2 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash

# Checks whether the branch / commit tracked by subgit for the
# specified repos are up-to-date, and whether the worktree is dirty.

usage() {
	die $(cat <<-EOF
	Usage: subgit check [PATH..]

	  Checks whether tracked subrepositories have uncommitted changes,
	  or if their remote, branch, or commit differ from what's recorded
	  in .subgitrc. Reports any discrepancies or dirty working trees.
	EOF
	)
}

while [ "$1" ]; do
	case $1 in
	-h) usage;;
	--) shift; break;;
	-*) die "Invalid option $1";;
	*) break;;
	esac
done

subgit-sub subrepo "$@" | while read -r subrepo; do
	root=$(subgit-sub parent "$subrepo")
	relpath=$(realpath -m --relative-to="$root" "$subrepo")

	source "$root/.subgitrc"

	bare="$root/.subgit/$relpath"
	if [ ! -d "$bare" ]; then
		warn "Subrepo $relpath uninitialized"
		continue
	fi

	remote=$(git -C "$bare" remote get-url origin)
	if [ "${subgitinfo[$relpath/remote]}" != "$remote" ]; then
		echo "$relpath: new remote - '$remote'"
	fi

	branch=$(git -C "$bare" branch --show-current)
	if [ ! -z "${subgitinfo[$relpath/branch]}" ] && [ "${subgitinfo[$relpath/branch]}" != "$branch" ]; then
		echo "$relpath: new branch - '$branch'"
	fi

	commit=$(git -C "$bare" rev-parse --verify HEAD)
	if [ "${subgitinfo[$relpath/commit]}" != "$commit" ]; then
		echo "$relpath: new commit - '$commit'"
	fi

	dirty=$(git -C "$subrepo" diff --stat 2>/dev/null | head -n1)
	if [ ! -z "$dirty" ]; then
		echo "$relpath: dirty tree"
	fi
done