summaryrefslogtreecommitdiffstats
path: root/scripts/git-ptx-refresh-tags-editor
blob: fff5d0d3ee1ce1a530d2a59c041eaf78576e085b (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
#!/bin/bash

find_tags() {
	local rev taglistfd commitish
	rev="$(git rev-parse "${1}")"
	exec {taglistfd}< <(git show-ref --tags)
	while read commitish tag <&${taglistfd}; do
		tag="${tag#refs/tags/}"
		if [[ "${tag}" =~ -ptx-old$ ]]; then
			continue
		fi
		if [ "${commitish}" = "${rev}" ]; then
			tags="${tags} ${tag}"
		fi
	done
}

push_tags() {
	local tag
	for tag in ${tags}; do
		echo "exec git tag -f ${tag}-ptx-old $(git rev-parse "${tag}")" >> "${tmpfile}"
		echo "exec git tag -f ${tag}" >> "${tmpfile}"
	done
	tags=
}

filter_rebase() {
	file="${1}"
	tmpfile="$(tempfile -d "$(dirname "${file}")" -p "$(basename ${file}).")"
	tags=
	cat "${file}" | while read cmd commitish subject; do
		case "${cmd}" in
		"#"|"")
			;;
		pick|p|reword|r|edit|e)
			push_tags
			find_tags "${commitish}"
			;;
		squash|s|fixup|f)
			find_tags "${commitish}"
			;;
		*)
			push_tags
			;;
		esac
		echo "${cmd}${commitish:+ }${commitish}${subject:+ }${subject}" >> "${tmpfile}"
	done
	push_tags

	mv "${tmpfile}" "${file}"
}

# only do something when rebasing
if [ "${GIT_REFLOG_ACTION}" = "rebase" ]; then
	filter_rebase "${1}"
fi

# find the editor outside the local repository
editor="$(cd / && git var GIT_EDITOR)"
exec ${editor} "${@}"