summaryrefslogtreecommitdiffstats
path: root/uglib.sh
blob: 7168e97ad023464ab51affda46a4b87086badfd0 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
## authorinfo ## {{{2
authorinfo() {
	# XXX: implement parsing .ubergit/author
	# more XXX: get author from $1
	echo "GIT_AUTHOR_NAME='Uwe Kleine-König'"
	echo "GIT_AUTHER_EMAIL='ukl@pengutronix.de'"
	echo "GIT_AUTHER_DATE='1283281597 +0200'"
}

## assert ## {{{2
assert() {
	"$@" || die "assertion failed: $*"
}

## base ## {{{2
base() {
	assert is_topic "$1"

	git cat-file blob "$1:.ubergit/base" \
	| sed -e 's/ *#.*//; /^$/d'
}

## commitenv ## {{{2
commitenv() {
	local commit
	commit="$1"; shift

	# XXX: apply the right dose of authorinfo(), committerinfo(), eval and sq()
	env GIT_AUTHOR_NAME='Uwe Kleine-König' GIT_AUTHOR_EMAIL='ukl@pengutronix.de' GIT_AUTHOR_DATE='1283281597 +0200' GIT_COMMITTER_NAME='Uwe Kleine-König' GIT_COMMITTER_EMAIL='ukl@pengutronix.de' GIT_COMMITTER_DATE='1283281597 +0200' "$@"
}

## commiterinfo ## {{{2
committerinfo() {
	assert is_topic "$1"

	# XXX: get commiter from $1
	echo "GIT_COMMITTER_NAME='Uwe Kleine-König'"
	echo "GIT_COMMITTER_EMAIL='ukl@pengutronix.de'"
	echo "GIT_COMMITTER_DATE='1283281597 +0200'"
}

## debug ## {{{2
debug() {
	echo -- "$*" > /dev/tty
}

## deps ## {{{2
deps() {
	assert test "$#" = "1"
	assert is_sha1 "$1"
	if is_topic "$1"; then
		local base deplines

		base="$(base "$1")"

		# this removes the final newline
		git cat-file blob "$base:.ubergit/deps" | sed -e 's/ *#.*//; /^$/d'
	fi
}

## die ## {{{2
die() {
	printf "fatal: %s\n" "$*" >&2
	exit 1
}

## export_rev_chk ## {{{2
export_rev_chk() {
	assert is_sha1 "$1"
	eval test -z "\$exported_$1"
}

## export_rev_cmd ## {{{2
export_rev_cmd() {
	local deps parent exported

	assert is_sha1 "$1"

	# XXX: is this portable?
	deps="$(printf "%s" "$(deps "$1")" | tr '\012' ' ')"

	case "$deps" in
	"")
		eval "exported_$1=$1"
		return
		;;
	*" "*)
		# >1 dependency
		parent="$(printf "exported base for $1\n" \
		| commitenv "$1" git commit-tree "$(pretty_tree "$(base "$1")")" $(set -f; for p in $deps; do eval printf "%s" "\" -p \$exported_$p\""; done))"
		;;
	*)
		# single dependency
		parent=$(eval printf "\$exported_$deps")
		;;
	esac

	exported="$(msg "$1" |
		git stripspace |
		commitenv "$1" git commit-tree "$(pretty_tree "$1")" -p "$parent"
	)"
	eval "exported_$1=$exported"
}

## export_rev ## {{{2
export_rev() {
	local ref
	assert is_sha1 "$1"

	ref="$1"

	recurse_deps export_rev_cmd export_rev_chk "$ref"

	eval echo "\$exported_$ref"
}

## is_sha1 ## {{{2
is_sha1() {
	assert test "$#" = 1
	git rev-parse -q --verify "$1" >/dev/null &&
	test "$(git rev-parse --verify "$1")" = "$1"
}

## is_ugbase ## {{{2
is_base() {
	rev_has_file "$1" ".ubergit/deps"
}

## is_topic ## {{{2
is_topic() {
	rev_has_file "$1" ".ubergit/base"
}

## is_ugish ## {{{2
is_ugish() {
	rev_has_file "$1" ".ubergit/"
}

## msg ## {{{2
msg() {
	assert test "$#" = 1
	assert is_topic "$1"

	git cat-file blob "$1:.ubergit/msg"
}

## pretty_tree ## {{{2
pretty_tree() {
	git ls-tree --full-tree "$1" \
	| awk -F '	' '$2 != ".ubergit"' \
	| git mktree
}

## recurse_deps ## {{{2
## recurse_deps <cmd> <recchk> <commitish>
recurse_deps() {
	local recdep_cmd recdep_recchk recdep
	recdep_cmd="$1"; shift
	recdep_recchk="$1"; shift

	if is_topic "$1" && "$recdep_recchk" "$@"; then
		for recdep in $(deps "$1"); do
			debug "recurse: patch=$1, dep=$recdep"
			recurse_deps "$recdep_cmd" "$recdep_recchk" "$recdep" "$@" || return "$?"
		done
	fi

	"$recdep_cmd" "$@"
}

## rev_has_file ## {{{2
rev_has_file() {
	git ls-tree --full-tree --name-only "$1" "$2" | grep . > /dev/null
}

## rev_verify ## {{{2
rev_verify() {
	git rev-parse --verify -q "$1" >/dev/null
}

## sq ## {{{2
sq() {
	# depends on git >= v1.6.4-rc0~24^2~22
	git rev-parse --sq-quote "$@"
}

## option parsing ## {{{1
if test -n "$UG_OPTIONS_SPEC"; then
	# depends on git >= v1.7.2-rc3~1^2~2, because before passing
	# --keep-dashdash made git ignore --stop-at-non-option
	#eval "$(printf "%s\n" "$UG_OPTIONS_SPEC" | git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- "$@" || exit "$?")"
	eval "$(printf "%s\n" "$UG_OPTIONS_SPEC" | git rev-parse --parseopt --stop-at-non-option -- "$@" || exit "$?")"
fi