-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-setupb
executable file
·62 lines (50 loc) · 1.92 KB
/
git-setupb
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
#!/bin/bash
# Copyright (c) 2019-2020 Benjamin Holt -- MIT License
if [ "$1" == "-h" -o "$1" == "--help" ]; then
cat <<USAGE
usage: git setupb [SLUG [ALIAS]] Pushes a local branch (ALIAS) to a
remote branch named prefix/SLUG; use
once to set the upstream, then just
push normally
git setupb -h|--help Print this message and exit
USAGE
exit 0
fi
Bold=`tput bold`
Off=`tput sgr0`
Alias="${2:-${ALLGIT_BRANCH:-`git rev-parse --abbrev-ref HEAD`}}"
Slug="${1:-$Alias}"
if [ -z "$Alias" ]; then
echo "Must specify a branch directly, using allgit, or the current branch" >&2
exit 1
fi
Branch=`git branch --list "$Alias"`
if [ -z "$Branch" ]; then
echo "$Alias: branch not found" >&2
exit 1
fi
ConfigRemote=`git config bettergit.defaultremote`
: ${DefaultRemote:=${ConfigRemote:-"origin"}} # REM: maybe use the first remote by default? `git remote | head -n1`
Upstream=`git rev-parse --abbrev-ref "$Alias@{upstream}" 2> /dev/null | sed -e "s:$DefaultRemote/::"`
if [ "$Upstream" ]; then
echo "$Alias: already pointing to $Upstream" >&2
exit 1
fi
if `echo "$Slug" | grep "/" - > /dev/null`; then
git push -u "$DefaultRemote" "$Alias:$Slug" # If slug is already prefixed, use as-is
exit 0
fi
: ${BranchPrefix:=`git config bettergit.branchprefix`}
if [ -z "$BranchPrefix" ]; then
BranchPrefix=`git config user.shortname`
if [ -z "$BranchPrefix" ]; then
BranchPrefix=`git config user.name | tr 'A-Z ' 'a-z_'`
fi
if [ -z "$BranchPrefix" ]; then
BranchPrefix="$USER"
fi
echo "${Bold}Using prefix $BranchPrefix/${Off} set it with: git config --global bettergit.branchprefix 'users/abc'"
fi
BranchPrefix=`echo "$BranchPrefix" | sed -e's:/$::'` # Trim any trailing slash to ensure isn't doubled
git push -u "$DefaultRemote" "$Alias:$BranchPrefix/$Slug"
###