-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgit-switch
executable file
·41 lines (35 loc) · 990 Bytes
/
git-switch
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
#!/usr/bin/env python
#
# git-switch
#
# Switches between feature branches.
#
# Usage:
# git switch banana
# git switch main
#
# Is equivalent to calling:
# git checkout <prefix>/banana
# git checkout main
#
# If the prefixed version of the branch wasn't found, it also
# tries the name verbatim.
import sys
import os
import git
import util
if len(sys.argv) < 2:
util.fatal('Incorrect usage, missing branch name. Use: git switch [feature]')
try:
repo = git.Repo(os.getcwd(), search_parent_directories=True)
except git.exc.InvalidGitRepositoryError:
util.fatal('git start must be run from within a valid git repository.')
util.fatal_if_dirty(repo)
# Try prefixed name first, then verbatim.
branch_names = (util.get_branch_name(sys.argv[1]), sys.argv[1])
for name in branch_names:
if name in repo.heads:
repo.heads[name].checkout()
util.success('Switched to %s' % name)
exit(0)
util.fatal('Branch not found. Tried: %s' % ', '.join(branch_names))