git_cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,147 @@
1
+ # Copyright (C) 2020 Chris Liaw <chrisliaw@antrapol.com>
2
+ # Author: Chris Liaw <chrisliaw@antrapol.com>
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+
18
+ module GitCli
19
+ module AddCommit
20
+
21
+ def add_to_staging(paths)
22
+ check_vcs
23
+
24
+ raise_if_empty(paths, "Given path to add is empty", GitCliException)
25
+
26
+ paths = [paths] if not paths.is_a?(Array)
27
+
28
+ cmd = []
29
+ cmd << "cd"
30
+ cmd << @wsPath
31
+ cmd << "&&"
32
+ cmd << @vcs.exe_path
33
+ cmd << "add"
34
+ cmd.append(paths)
35
+ cmdln = cmd.join " "
36
+
37
+ log_debug "Add : #{cmdln}"
38
+
39
+ os_exec(cmdln) do |st, res|
40
+ [st.success?, res.strip]
41
+ end
42
+ end # add_to_staging
43
+ alias :add :add_to_staging
44
+ alias :add_staging :add_to_staging
45
+
46
+ def remove_from_staging(paths)
47
+ check_vcs
48
+
49
+ raise_if_empty(paths, "Given path to reset is empty", GitCliException)
50
+
51
+ paths = [paths] if not paths.is_a?(Array)
52
+
53
+ cmd = []
54
+ cmd << "cd"
55
+ cmd << @wsPath
56
+ cmd << "&&"
57
+ cmd << @vcs.exe_path
58
+ cmd << "reset"
59
+ cmd.append(paths)
60
+ cmdln = cmd.join " "
61
+
62
+ log_debug "reset : #{cmdln}"
63
+
64
+ os_exec(cmdln) do |st, res|
65
+ [st.success?, res.strip]
66
+ end
67
+
68
+ end # remove_from_staging
69
+ alias :remove_staging :remove_from_staging
70
+
71
+ def remove_from_vcs(paths)
72
+
73
+ check_vcs
74
+
75
+ raise_if_empty(paths, "Given path to remove from VCS is empty", GitCliException)
76
+
77
+ paths = [paths] if not paths.is_a?(Array)
78
+
79
+ cmd = []
80
+ cmd << "cd"
81
+ cmd << @wsPath
82
+ cmd << "&&"
83
+ cmd << @vcs.exe_path
84
+ cmd << "rm --cached"
85
+ cmd.append(paths)
86
+ cmdln = cmd.join " "
87
+
88
+ log_debug "Remove from git version control : #{cmdln}"
89
+
90
+ os_exec(cmdln) do |st, res|
91
+ [st.success?, res.strip]
92
+ end
93
+
94
+ end # remove_from_vcs
95
+
96
+ def commit(message)
97
+ check_vcs
98
+
99
+ # have to escape the message for command line purposes
100
+ msg = message.gsub("\"","\\\"").gsub("\\","\\\\")
101
+
102
+ cmd = []
103
+ cmd << "cd"
104
+ cmd << @wsPath
105
+ cmd << "&&"
106
+ cmd << @vcs.exe_path
107
+ cmd << "commit"
108
+ cmd << "-m"
109
+ cmd << "\"#{msg}\""
110
+
111
+ cmdln = cmd.join " "
112
+
113
+ log_debug "Commit : #{cmdln}"
114
+
115
+ os_exec(cmdln) do |st, res|
116
+ [st.success?, res.strip]
117
+ end
118
+
119
+ end
120
+
121
+ def commit_all(msg)
122
+ check_vcs
123
+
124
+ # have to escape the message for command line purposes
125
+ msg = message.gsub("\"","\\\"").gsub("\\","\\\\")
126
+
127
+ cmd = []
128
+ cmd << "cd"
129
+ cmd << @wsPath
130
+ cmd << "&&"
131
+ cmd << @vcs.exe_path
132
+ cmd << "commit"
133
+ cmd << "-am"
134
+ cmd << msg
135
+
136
+ cmdln = cmd.join " "
137
+
138
+ log_debug "Commit All : #{cmdln}"
139
+
140
+ os_exec(cmdln) do |st, res|
141
+ [st.success?, res.strip]
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+ end
@@ -0,0 +1,273 @@
1
+ # Copyright (C) 2020 Chris Liaw <chrisliaw@antrapol.com>
2
+ # Author: Chris Liaw <chrisliaw@antrapol.com>
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+
18
+
19
+ module GitCli
20
+ module Branch
21
+
22
+ def current_branch
23
+
24
+ check_vcs
25
+
26
+ cmd = []
27
+ cmd << "cd"
28
+ cmd << @wsPath
29
+ cmd << "&&"
30
+ cmd << @vcs.exe_path
31
+ # only works from git version 2.22 onwards
32
+ # Shall return just the name of the branch
33
+ cmd << "branch --show-current"
34
+ # another way but shall return like refs/heads/master
35
+ #cmd << "symbolic-ref HEAD"
36
+
37
+ cmdln = cmd.join(" ")
38
+ log_debug "Current branch : #{cmdln}"
39
+ res = os_exec(cmdln) do |st, res|
40
+
41
+ if st.success?
42
+
43
+ # this is for command 'git branch'
44
+ # But for newly created workspace, git branch will give empty string
45
+ #res.each_line do |l|
46
+ # tok = l.split " "
47
+ # if tok[0] == "*"
48
+ # @currBranch = tok[1].strip
49
+ # break
50
+ # end
51
+ #end
52
+
53
+ [true, res.strip!]
54
+ else
55
+ [false, res]
56
+ end
57
+ end
58
+
59
+ end # current_branch
60
+
61
+ def all_branches
62
+ br = []
63
+ st, lb = local_branches
64
+ if st
65
+ br.concat(lb)
66
+ end
67
+
68
+ st, rb = remote_branches
69
+ if st
70
+ br.concat(rb)
71
+ end
72
+
73
+ [true, br]
74
+
75
+ end # all_branches
76
+
77
+ def local_branches
78
+
79
+ check_vcs
80
+
81
+ cmd = []
82
+ cmd << "cd"
83
+ cmd << @wsPath
84
+ cmd << "&&"
85
+ cmd << @vcs.exe_path
86
+ cmd << "branch"
87
+
88
+ cmdln = cmd.join(" ")
89
+ log_debug "Local branch : #{cmdln}"
90
+ res = os_exec(cmdln) do |st, res|
91
+
92
+ if st.success?
93
+ b = []
94
+ res.strip!
95
+ res.each_line do |l|
96
+ b << l.strip
97
+ end
98
+ [true, b]
99
+ else
100
+ [false, res]
101
+ end
102
+ end
103
+
104
+ end # local_branches
105
+
106
+ def remote_branches
107
+
108
+ check_vcs
109
+
110
+ cmd = []
111
+ cmd << "cd"
112
+ cmd << @wsPath
113
+ cmd << "&&"
114
+ cmd << @vcs.exe_path
115
+ cmd << "branch -r"
116
+
117
+ cmdln = cmd.join(" ")
118
+ log_debug "Remote branch : #{cmdln}"
119
+ res = os_exec(cmdln) do |st, res|
120
+
121
+ if st.success?
122
+ b = []
123
+ res.strip!
124
+ res.each_line do |l|
125
+ b << l.strip
126
+ end
127
+
128
+ [true, b]
129
+ else
130
+ [false, res]
131
+ end
132
+ end
133
+
134
+ end # remote_branches
135
+
136
+ def switch_branch(branch)
137
+
138
+ raise_if_empty(branch, "Branch name cannot be empty", GitCliException)
139
+
140
+ check_vcs
141
+
142
+ cmd = []
143
+ cmd << "cd"
144
+ cmd << @wsPath
145
+ cmd << "&&"
146
+ cmd << @vcs.exe_path
147
+ cmd << "checkout"
148
+ cmd << branch
149
+
150
+ cmdln = cmd.join(" ")
151
+ log_debug "Switch branch : #{cmdln}"
152
+ res = os_exec(cmdln) do |st, res|
153
+ if st.success?
154
+ [true, res.strip]
155
+ else
156
+ [false, res]
157
+ end
158
+ end
159
+
160
+ end # switch_branch
161
+
162
+ def create_branch(branch)
163
+
164
+ raise_if_empty(branch, "Branch name cannot be empty", GitCliException)
165
+
166
+ check_vcs
167
+
168
+ branch = branch.gsub(" ","_")
169
+
170
+ cmd = []
171
+ cmd << "cd"
172
+ cmd << @wsPath
173
+ cmd << "&&"
174
+ cmd << @vcs.exe_path
175
+ cmd << "branch"
176
+ cmd << branch
177
+
178
+ cmdln = cmd.join(" ")
179
+ log_debug "Create branch : #{cmdln}"
180
+ res = os_exec(cmdln) do |st, res|
181
+
182
+ if st.success?
183
+ [true, res.strip]
184
+ else
185
+ [false, res]
186
+ end
187
+ end
188
+
189
+ end # create_branch
190
+
191
+ def download_all_remote_branches_name
192
+
193
+ check_vcs
194
+ check_repos
195
+
196
+ cmd = []
197
+ cmd << "cd"
198
+ cmd << @wsPath
199
+ cmd << "&&"
200
+ cmd << @vcs.exe_path
201
+ cmd << "fetch -all"
202
+
203
+ cmdln = cmd.join(" ")
204
+ log_debug "Download remote branches name : #{cmdln}"
205
+ res = os_exec(cmdln) do |st, res|
206
+
207
+ if st.success?
208
+ [true, res.strip]
209
+ else
210
+ [false, res]
211
+ end
212
+ end
213
+
214
+ end # download_all_remote_branches_name
215
+ alias :sync_all_remote_branches_name :download_all_remote_branches_name
216
+
217
+ def merge_branch(branch)
218
+ raise_if_empty(branch, "Branch name cannot be empty", GitCliException)
219
+
220
+ check_vcs
221
+
222
+ cmd = []
223
+ cmd << "cd"
224
+ cmd << @wsPath
225
+ cmd << "&&"
226
+ cmd << @vcs.exe_path
227
+ cmd << "merge"
228
+ cmd << branch
229
+
230
+ cmdln = cmd.join(" ")
231
+ log_debug "Merge current branch with branch '#{branch}' : #{cmdln}"
232
+ res = os_exec(cmdln) do |st, res|
233
+
234
+ if st.success?
235
+ [true, res.strip]
236
+ else
237
+ [false, res]
238
+ end
239
+ end
240
+
241
+ end # merge_branch
242
+
243
+ def delete_branch(branch)
244
+
245
+ raise_if_empty(branch, "Branch name cannot be empty", GitCliException)
246
+
247
+ check_vcs
248
+
249
+ cmd = []
250
+ cmd << "cd"
251
+ cmd << @wsPath
252
+ cmd << "&&"
253
+ cmd << @vcs.exe_path
254
+ cmd << "branch"
255
+ cmd << "-d"
256
+ cmd << branch
257
+
258
+ cmdln = cmd.join(" ")
259
+ log_debug "Delete branch '#{branch}' : #{cmdln}"
260
+ res = os_exec(cmdln) do |st, res|
261
+
262
+ if st.success?
263
+ [true, res.strip]
264
+ else
265
+ [false, res]
266
+ end
267
+
268
+ end
269
+
270
+ end # delete_branch
271
+
272
+ end
273
+ end
@@ -0,0 +1,42 @@
1
+ # Copyright (C) 2020 Chris Liaw <chrisliaw@antrapol.com>
2
+ # Author: Chris Liaw <chrisliaw@antrapol.com>
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+
18
+ module GitCli
19
+ module Clone
20
+
21
+ include Antrapol::ToolRack::ExceptionUtils
22
+
23
+ def clone(src, dest, opts = { }, &block)
24
+ raise_if_empty(src, "Source to clone cannot be empty", GitCliException)
25
+ raise_if_empty(dest, "Destination to clone cannot be empty", GitCliException)
26
+
27
+ cmd = []
28
+ cmd << exe_path
29
+ cmd << "clone"
30
+ cmd << src
31
+ cmd << dest
32
+ cmdln = cmd.join(" ")
33
+
34
+ log_debug "Clone : #{cmdln}"
35
+
36
+ os_exec(cmdln) do |st, res|
37
+ [st.success?, res.strip]
38
+ end
39
+ end
40
+
41
+ end
42
+ end