-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.sh
More file actions
executable file
·38 lines (27 loc) · 1.11 KB
/
hook.sh
File metadata and controls
executable file
·38 lines (27 loc) · 1.11 KB
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
#!/bin/bash
function main {
# Get the remote name (usually "origin")
local remote
remote=$(git remote)
# Use the first argument as the default branch, or find the default branch
local default_branch="$1"
if [[ -z "$default_branch" ]]; then
default_branch=$(git remote show "$(git remote -v | grep push | awk '{print $2}')" | grep 'HEAD branch' | awk '{print $3}')
fi
# Fallback to "main"
[[ -n "$default_branch" ]] || default_branch="main"
# Fetch the default branch for rebasing
git fetch --verbose --prune "$(git remote)" "$default_branch" || return $?
# Attempt to rebase onto the remote default automatically
local target_branch="$remote/$default_branch"
# Stash staged changes before rebasing
git stash push --staged
# Attempt to rebase onto the remote default automatically
git rebase --verbose --rerere-autoupdate --allow-empty "$target_branch"
local result=$?
[[ $result -eq 0 ]] || git rebase --verbose --abort
# Restore the stashed staged changes
git stash pop --index
[[ $result -eq 0 ]] || return $result
}
main "$@"