-
Notifications
You must be signed in to change notification settings - Fork 1
/
easy-ws-git-integration.sh
139 lines (125 loc) · 4.01 KB
/
easy-ws-git-integration.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
#!/bin/sh
################################################################################
# EasyWorkspace git integration
#
# Sublime's EasyWorkspace can be integrated with git to easily maintain
# a workspace for each branch in the repo.
#
# Included here are some helpful functions and examples of how one can
# integrate EasyWorkspace with git!
#
# *NOTE: These features only work if sublime is already running. Sublime can
# only execute window commands and plugin commands if the application is
# already running (otherwise, windows and plugins are not
# loaded/created!)
#
# One could modify the below functions to manage this issue by adding
# a check and delay, but we leave this exercise up to the user.
#
# Usage
# ==============================================================================
# To use these EasyWorkspace/git integration examples in your own project,
# follow the following steps:
#
# 1. Save this file to your local machine
# 2. Add the following aliases to your .gitconfig file
# '~/.easy-ws-git-integration.sh' should be the path to your local copy in #1.
#
# [alias]
# edit = !. ~/.easy-ws-git-integration.sh && easy_ws_git_edit
# save = !. ~/.easy-ws-git-integration.sh && easy_ws_git_save
#
# 3. Now you can use 'git edit' or 'git save' to easily integrate git with
# sublime EasyWorkspaces!
#
# 4. Lastly, for autocomplete on these git aliases, add the following functions
# to your .bashrc file (or equivalent)
#
# _git_edit() {
# _git_checkout
# }
# _git_save() {
# _git_checkout
# }
#
# 5. Profit! Now you can autocomplete which branch to edit or save
#
################################################################################
##
# When run inside a git repo, outputs to stdout the EasyWorkspace filename
# associated with the repo's current branch.
#
# If a branchname is provided as the first argument, we output this branchname's
# associated EasyWorkspace filename
#
# Arguments:
# branch-name (optional) -- identifies the branch to get associated workspace file
##
easy_ws_git_workspace_name() {
# check for valid repo root
local gitRepoRoot
local gitRepoName
gitRepoRoot=$(git rev-parse --show-toplevel)
# check valid repo root
if [ $? -ne 0 ]; then
return 1
fi
gitRepoName=$(basename "$gitRepoRoot")
# use optional or current branch name
if [ ! -z "$1" ]; then
gitBranchName="$1"
else
gitBranchName=$(git rev-parse --abbrev-ref HEAD)
fi
# write to stdout the result
echo "$gitRepoName"/"$gitBranchName"
}
##
# When run inside a git repo, opens in a new sublime window the easyworkspace
# associated with the current git repo and branch
#
# If no corresponding workspace exists, it will open an empty workspace
# and create the association.
#
# Arguments:
# branch-name (optional) - identifies the branch workspace to edit
##
easy_ws_git_edit() {
local workspaceName
# try to determine the current repo/branch workspace name
workspaceName=$(easy_ws_git_workspace_name "$1")
if [ $? -ne 0 ]; then
return 1
fi
# open the easy workspace in sublime
subl --command "open_easy_workspace { \"filename\": \"$workspaceName\"}"
}
##
# When run inside a git repo, forces the most recently active sublime window
# to try to save its current workspace as an EasyWorkspace with the provided
# branch name.
#
# If no branch name is provided, the branch defaults to the current repo HEAD
#
# Arguments:
# branch-name (optional) - identifies the branch workspace to save as
##
easy_ws_git_save() {
local workspaceName
# try to determine the current repo/branch workspace name
workspaceName=$(easy_ws_git_workspace_name "$1")
if [ $? -ne 0 ]; then
return 1
fi
# open the easy workspace in sublime
subl --command "save_as_easy_workspace { \"filename\": \"$workspaceName\"}"
}
##
# Add git autocompletion
##
_git_edit() {
_git_checkout
}
_git_save() {
_git_checkout
}