summaryrefslogtreecommitdiff
path: root/git-smh
blob: 631f05aaba207604e9de408dd0c876ce2650909c (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env bash

unset CDPATH
IFS=$' \t\n'

if type gettext &>/dev/null; then
	_() { gettext "$@"; }
else
	_() { echo "$@"; }
fi

print() {
	printf "$(_ "$1")\n" "${@:2}"
}

# low-ish level smh commands

smh-root() (
	local gitdir
	while true; do
		gitdir="$(git rev-parse --git-dir)" || return $?
		cd "$(git rev-parse --cdup)" || return $?
		if [[ "$gitdir" != */.git/modules/* ]]; then
			break
		fi
		cd .. || return $?
	done
	pwd
)

smh-split() {
	local root
	root="$(smh-root)" || return $?

	local cwd files
	cwd_files=("$@")

	declare -i i=0
	local smh_file cwd_file
	local gittop
	while read -r -d '' smh_file; do
		cwd_file="${cwd_files[$i]}"

		if [[ "$smh_file" = ../* ]] || ! [[ -e "$smh_file" ]]; then
			# let git print the error message
			git add "$cwd_file"
			return $?
		else
			if [[ -d "$cwd_file" ]]; then
				gittop="$((cd "$cwd_file"; git --show-toplevel))"
			else
				gittop="$((cd "$(dirname -- "$cwd_file")"; git --show-toplevel))"
			fi
			printf '%s\0' \
			       "$(realpath --no-symlinks --relative-to . "$gittop")" \
			       "$(realpath --no-symlinks --relative-to "$gittop" "$cwd_file")"
		fi
		i+=1
	done < <(realpath -z --canonicalize-missing --no-symlinks --relative-to "$root" -- "${cwd_files[@]}")
}
cmd_split() {
	local args
	args=$(getopt -a "$0" -o Hz -- "$@") || return $?
	eval set -- "$args"
	local mode
	if [[ -t 1 ]]; then
		mode=human
	else
		mode=machine
	fi
	while true; do
		case "$1" in
			-H) shift; mode=human;;
			-z) shift; mode=machine;;
			--) shift; break;;
		esac
	done
	case "$mode" in
		machine)
			smh-split "$@"
			;;
		human)
			set -o pipefail
			smh-split "$@" | xargs -0r printf '%q %q\n'
			;;
	esac
}

smh-foreach-
smh-foreach() {
	local cmd="$1"
	
	exec 3<&0
	(
		cd "$(smh-root)" || return $?
		export LC_ALL=C
		git submodule foreach --quiet --recursive pwd | sort --reverse
		pwd
	) | xargs -0 realpath -zs --relative-to=. | while read -r -d '' path; do
		(
			cd "$path" &&
			sh -c "$cmd"
		) <&3 3<&- || {
			print "Stopping at '%s'; script returned non-zero status." "$path"
			return 1
		}
	done
}


# smh add

smh-add--helper() {
	local file extra
	file="$(git rev-parse --git-path SMH_ADD)"
	read -a extra -r -d '' < "$file"
	rm -f "$file"
	exec git add "$@" "${extra[@]}"
}

smh-add() {
	local mode=batch
	local gitflags=()
	local args
	args=$(getopt -a "$0" -o vfipeuAN -l verbose,force,interactive,patch,edit,update,all,no-ignore-removal,no-all,ignore-removal,intent-to-add,refresh,ignore-errors -- "$@") || return $?
	eval set -- "$args"
	while true; do
		case "$1" in
			--verbose|-v) gitflags+=("$1");;
			--force|-f) gitflags+=("$1");;
			--interactive|-i) mode=interactive;;
			--patch|-p) mode=patch;;
			--edit|-e) mode=edit;;
			--update|-u) gitflags+=("$1");;
			-A|--all|-no-ignore-removal) gitflags+=("$1");;
			--no-all|--ignore-removal) gitflags+=("$1");;
			--intent-to-add|-N) gitflags+=("$1");;
			--refresh) gitflags+=("$1");;
			--ignore-errors) gitflags+=("$1");;
			--) shift; break;;
		esac
		shift
	done
	while read -r -d '' dir && read -r -d '' file; do
		printf '%s\0' "$file" >> "$(cd "$dir" && git rev-parse --git-path 'SMH_ADD')"
	done < <(smh-split "$@")

	local cmd
	case "$mode" in
		batch)
			printf -v cmd '%q ' git smh add--helper "${gitflags[@]}" --
			smh-foreach "$cmd"
			;;
		interactive)
			gitflags=(-i "${gitflags[@]}")
			printf -v cmd '%q ' git smh add--helper "${gitflags[@]}" --
			printf -v cmd '%q ' git smh foreach "$cmd"
			sed -r '/^(7|q|qu|qui|quit)$/q' | script --return --quiet -c "$cmd" /dev/null
			;;	
		patch)
			gitflags=(-p "${gitflags[@]}")
			printf -v cmd '%q ' git smh add--helper "${gitflags[@]}" --
			printf -v cmd '%q ' git smh foreach "$cmd"
			sed '/^q$/q' | script --return --quiet -c "$cmd" /dev/null
			;;
		edit)
			gitflags=(-e "${gitflags[@]}")
			print 'not implemented: smh add --edit' >&2
			return 4
			;;
	esac
}

# smh commit

smh-commit--helper() {
	git commit "$@" &&
	cd .. &&
	git add "$OLDPWD"
}

smh-commit() {
	local cmd
	printf -v '%q ' git smh commit--helper "$@"
	smh-foreach "$cmd"
}	

# smh push

smh-push() {
	local cmd
	printf -v '%q ' git push "$@"
	smh-foreach "$cmd"
}

main() {
	local cmd="$1"; shift
	case "$cmd" in
		root|add|add--helper|commit|commit--helper|push)
			smh-"$cmd" "$@"
			;;
		split)
			cmd_"$cmd" "$@"
			;;
		*)
			print 'error: Unknown subcommand: %s' "$cmd" >&2
			return 129
			;;
	esac
}			

main "$@"