aboutsummaryrefslogtreecommitdiff
path: root/reposync-functions.sh
blob: 4874ad1369a9d3ae02b546822bf469e07e2f59ce (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
set -e

# handle base args
for arg in "$@"; do
	case "$arg" in
		-v|--verbose) VERBOSE=1 ;;
		-d|--dry-run) DRY_RUN=1 ;;
	esac
done

[ -n "$VERBOSE" ] && VERBOSE_ARG=--verbose
run() { [ -n "$DRY_RUN" ] && echo "DRY RUN:" $@ || $@; }

# define base functions
clonemissing() {
	url="$1"
	target="$2"
	flags="$3"
	#branch="$3"

	# return if already existing
	if [ -d "${target}/.git" ]; then
		return
	fi

	# clone url to target
	echo "cloning $url to $target"
	run mkdir -p $VERBOSE_ARG "$target"
	run git clone $VERBOSE_ARG $flags "$url" "$target"
}

pull() {
	target="$1"
	flags="$2"

	cd "$target"

	# check if local changes exist before running pull
	if ! (git diff --quiet && git diff --cached --quiet); then
		echo "$target: local changes exist. won't attempt to pull"
		return
	fi

	# pull changes
	echo "$target: pulling latest changes"
	run git pull $VERBOSE_ARG $flags
}

remoteadd() {
	target="$1"
	remote="$2"
	url="$3"

	cd "$target"

	# check if remote exists
	remoteurl="$(git $VERBOSE_ARG remote get-url "$remote" 2>/dev/null)"
	if [ $? = 0 ]; then
		# verify remote url
		if [ "$remoteurl" = "$url" ]; then
			return
		fi
		echo "$target: the url of remote $remote ($remoteurl) does not match the expected value ($url)"
		return
	fi

	# add remote
	echo "$target: adding remote $remote ($remoteurl)"
	run git remote $VERBOSE_ARG add "$remote" "$url"
}

stowrepo() {
	dir="$1"
	target="$2"
	packages="$3"

	cd "$dir"

	echo "$target: stowing packages ($packages) to $target"
	run mkdir $VERBOSE_ARG -p "$target"
	run stow $VERBOSE_ARG --restow --target="$target" $packages
}

# define repo specific sync funcs