-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublime_checkout.sh
executable file
·71 lines (56 loc) · 1.38 KB
/
sublime_checkout.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
#!/bin/bash
# SETUP
# In ~/.gitconfig add these two lines
#
# [alias]
# scheckout = !sh /full/path/to/sublime_checkout.sh
project_home="$HOME/.config/sublime-text-3/Packages/User/Projects"
if [ ! -d "$project_home" ];then
project_home="$HOME/Library/Application Support/Sublime Text 3/Packages/User/Projects"
fi
branch="$1"
prev_branch="$(git rev-parse --abbrev-ref HEAD)"
if [ -z "$branch" ];then
branch="$prev_branch"
fi
# Get root directory of repository
pwd="$(git rev-parse --show-toplevel)"
project="$(basename "$pwd")"
SC_checkout() {
echo "Project Home: $project_home"
if git checkout "$branch" || git checkout -b "$branch";then
SC_create_project
workspace="$project_home/$project/$branch.sublime-project"
echo "Switching to $workspace"
if [ -f "$workspace" ];then
subl "$workspace" -a
sleep 0.1
subl "$workspace" -a
else
echo "Invalid workspace! $workspace"
fi
fi
}
SC_create_project() {
folders="$pwd"
cd "$project_home"
if [ ! -f "$project.sublime-project" ];then
cat <<EOF > "$project.sublime-project"
{
"folders":
[{
"path": "$folders"
}]
}
EOF
fi
mkdir -p "$(dirname "$project/$branch")" 2>/dev/null
if [ ! -L "$project/$branch.sublime-project" ];then
ln -s "$project_home/$project.sublime-project" "$project/$branch.sublime-project"
fi
cd -
}
_git_scheckout() {
_git_checkout
}
SC_checkout