aboutsummaryrefslogtreecommitdiff
path: root/shellrc.d/git
blob: 15b949a57d2a701b2db7b8998e10d7ca91fd9772 (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
# vim: ft=sh:

# Get path to worktree for git branch
_gitbworktree() {
	git worktree list --porcelain | \
		awk -v ref="refs/heads/$1" \
			'/^worktree / { worktree = $2 }; /^branch / { if ($2 == ref) { print worktree }}'
}

# Automatic branch merger (merge branch, push it to server and remove branch)
# Expects name of the branch as argument
# It fails if it's not fast forward merge and if there is fixup! commit.
gitbmerge() (
		local branch="$1"
		set -e
		if ! git rev-parse --quiet --verify "$branch" >/dev/null; then
			echo "No such branch: $branch" >&2
			return 1
		fi
		local upstream
		upstream="$(git rev-parse "$branch@{u}")" || true
		if [ -n "$upstream" ] && [ "$upstream" != "$(git rev-parse "$branch")" ]; then
			echo "First push your changes to the server!" >&2
			return 1
		fi
		[ -z "$(git log --grep="^fixup\!" HEAD.."$branch")" ] || {
			echo "First squash fixups!" >&2
			return 1
		}
		[ -z "$(git log --grep="^Apply .* suggestion(s) to " HEAD.."$branch")" ] || {
			echo "First squash suggestions!" >&2
			return 1
		}
		[ "$(git rev-parse HEAD)" != "$(git rev-parse "$branch")" ] || {
			echo "Nothing to merge! 🤷" >&2
			return 1
		}
		local wt
		wt="$(_gitbworktree "$branch")"
		if [ -n "$wt" ]; then
			if [ -d "$wd/.git" ]; then
				echo "Branch is checked out in root repository!"
				return 1
			fi
			rm -r "$wt"
			git worktree prune
		fi
		git merge --ff-only "$branch" \
			&& git push \
			&& git branch -d "$branch" \
			&& { if [ -n "$upstream" ]; then git push origin :"$branch"; fi; }
)

# Checkout branch to new work tree
gitbcheckout() {
	local nw
	nw="$(git rev-parse --show-toplevel)-$1"
	git worktree add "$nw" "$1"
	cd "$nw"
	git submodule update --no-fetch --init --recursive
}
alias gitbco='gitbcheckout'

# Create new branch from HEAD
gitbnew() {
	git branch "$1" HEAD
	gitbcheckout "$1"
}

# Git that uses SOCKS on my favorite port
sgit() {
	ALL_PROXY=socks5h://localhost:8123 git "$@"
}