-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-checkout-interactive.sh
159 lines (146 loc) · 5.18 KB
/
git-checkout-interactive.sh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env bash
__gchk_script_dirname="$(dirname $0)"
gchk() {
__gchk_usage() {
echo "Git Checkout++ -- SimGus 2020"
echo "Usage: gchk [-i|--interactive] [<PARTIAL-BRANCH-NAME>]"
echo "\t<PARTIAL-BRANCH-NAME>\t\tThe name of a git branch (can be partial in interactive mode)"
echo "\t-i, --interactive\t\tRun the command in interactive mode"
echo "\t-r, --include-remote-branches\tIf the command runs interactively, only takes into account the remote branches"
echo "\t-a, --all\t\t\tIf the command runs interactively, take into account both the local and remote branches"
echo "\nFor information about Git's official checkout, please read 'man git checkout'."
}
if [ "$#" -eq 0 ]
then
echo "Too few arguments"
__gchk_usage
return 1
else
interactive=false
for arg in "$@"
do
shift
if [ "$arg" = "-i" ] || [ "$arg" = "--interactive" ]
then
interactive=true
continue
fi
set -- "$@" "$arg"
done
if [ "$interactive" = false ]
then
git checkout $@
else
git_checkout_interactive $@
fi
fi
}
git_checkout_interactive() (
source "$__gchk_script_dirname"/utils.sh
__gci_usage() {
echo "Git Checkout Interactive -- SimGus 2020"
echo "Usage: git_checkout_interactive [-r|-a] [<PARTIAL-BRANCH-NAME>]"
echo "\t<PARTIAL-BRANCH-NAME>\tThe name of a git branch (can be partial)"
echo "\t-r, --include-remote-branches\tIf the command runs interactively, only takes into account the remote branches"
echo "\t-a, --all\t\t\tIf the command runs interactively, take into account both the local and remote branches"
}
__gci_checkout() {
local include_remote_branches=false
for arg in "$@"
do
if [ "$arg" = "--include-remote-branches" ]
then
include_remote_branches=true
continue
else
local selected_branch="$arg"
fi
done
if [ "$include_remote_branches" = false ]
then
git checkout $selected_branch
else
local remote_name="$(echo $selected_branch | sed 's:remotes/::')"
local corresponding_local_branch="$(__gci_find_corresponding_local_branch $remote_name)"
if [ -z "$corresponding_local_branch" ]
then
git checkout -t $selected_branch
else
echo "Local branch '$corresponding_local_branch' already tracks '$remote_name'"
echo -n "Create a new local branch with another name? [y/n] "
local create_local_branch
read create_local_branch
if [[ $create_local_branch =~ [yY] ]]
then
echo -n "Please specify the name of the new branch: "
local new_branch_name
read new_branch_name
if [ -n "$new_branch_name" ]
then
git checkout -b $new_branch_name $remote_name
else
echo "No name provided"
return 1
fi
fi
fi
fi
}
__gci_find_corresponding_local_branch() {
if [ "$#" -ne 1 ]
then
return 0
else
echo "$(git branch -vv | eval $__gi_grep_command $remote_name | cut -d' ' -f2)"
fi
}
if [ "$(__gi_is_git_repo)" = false ]
then
echo "Fatal: not in a get repository"
return 1
else
__gi_check_dependencies
local branches=$(__gi_fetch_branches $@)
local branch_selection_return_value="$?"
local include_remote_branches_flag=""
for arg in "$@"
do
shift
if [ "$arg" = "-r" ] || [ "$arg" = "--include-remote-branches" ] || [ "$arg" = "-a" ] || [ "$arg" = "--all" ]
then
include_remote_branches_flag="--include-remote-branches"
continue
fi
set -- "$@" "$arg"
done
if [ "$#" -gt 1 ]
then
echo "Too many arguments."
__gci_usage
return 1
fi
if [ "$branch_selection_return_value" -ne 0 ] # Error when getting branches
then
echo "Error while fetching branches."
return 1
else
local nb_branches=$(echo $branches | wc -l)
if [ "$nb_branches" -lt 1 ]
then
echo "No branch to select"
return 0
elif [ "$nb_branches" -eq 1 ]
then
local selected_branch="$branches"
echo "A single branch corresponds: $selected_branches"
else
local selected_branch=$(echo "$branches" | fzf --cycle -q "$1")
if [ -z "$selected_branch" ]
then
return 0
fi
fi
__gci_checkout $include_remote_branches_flag $selected_branches
fi
fi
)