-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.githelpers
52 lines (45 loc) · 1.52 KB
/
.githelpers
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
#!/bin/bash
HASH="%C(always,yellow)%h%C(always,reset)"
RELATIVE_TIME="%C(always,green)%ar%C(always,reset)"
AUTHOR="%C(always,bold blue)%an%C(always,reset)"
REFS="%C(always,red)%d%C(always,reset)"
SUBJECT="%s"
RESET="\033[0m"
YELLOW="\033[33m"
FORMAT="$HASH $RELATIVE_TIME{$AUTHOR{$REFS $SUBJECT"
pretty_git_log() {
git log --graph --pretty="tformat:${FORMAT}" $* |
# Replace (2 years ago) with (2 years)
sed -Ee 's/(^[^<]*) ago\)/\1)/' |
# Replace (2 years, 5 months) with (2 years)
sed -Ee 's/(^[^<]*), [[:digit:]]+ .*months?\)/\1)/' |
# Color merge commits specially
sed -Ee "s/(Merge (branch|remote-tracking branch|pull request) .*$)/${YELLOW}\1${RESET}/" |
# Page only if we need to
less -FXRS
}
# Checks out a remote branch (if it exists) and creates a local branch tracking the remote branch
pull_remote() {
if git rev-parse --quiet --verify "$1"; then
echo "Local branch $1 already exists.";
else
git fetch origin "$1" && git checkout -b "$1" --track origin/"$1";
fi
}
# Finds and switches to a local branch that matches the specified prefix
switch_prefix() {
branch=$(git branch --list "$1*" | head -n 1 | sed "s/^[* ]*//");
if [ -z "$branch" ]; then
echo "No branch found matching prefix $1";
else
git switch "$branch";
fi;
}
# Sets the upstream branch if remote branch exists
set_upstream() {
if git rev-parse --quiet --verify "origin/$1"; then
git branch --set-upstream-to=origin/"$1" "$1";
else
echo "Remote branch origin/$1 does not exist.";
fi
}