Module: GitCli::Stash

Included in:
Workspace
Defined in:
lib/git_cli/stash.rb

Instance Method Summary collapse

Instance Method Details

#stash_all_changes(msg, includes_ignore_file = false) ⇒ Object

Save all the temporary changes so the workspace will be clean for other operation

This operation only stash: modified files (already in git workspace) and new files (not in git workspace yet)

If flag includes_ignore_file is true, include ignored file too



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
# File 'lib/git_cli/stash.rb', line 65

def stash_all_changes(msg, includes_ignore_file = false)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash"
  cmd << "save"

  if not_empty?(msg)
    # have to escape the message for command line purposes
    msg2 = msg.gsub("\"","\\\"").gsub("\\","\\\\")
    cmd << msg2
  end

  cmd << "--include-untracked"

  if includes_ignore_file == true
    cmd << "--all"
  end

  cmdln = cmd.join(" ")
  if includes_ignore_file
    log_debug "Stash all changes (including untracked and ignored files) : #{cmdln}"
  else
    log_debug "Stash all changes (including untracked files) : #{cmdln}"
  end
  res = os_exec(cmdln) do |st, res|
    [st.success?, res]
  end

end

#stash_changes(msg) ⇒ Object

Save all changes so branch switch is possible Excluding untracked files



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
# File 'lib/git_cli/stash.rb', line 30

def stash_changes(msg)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash"
  cmd << "save"

  if not_empty?(msg)
    # have to escape the message for command line purposes
    msg2 = msg.gsub("\"","\\\"").gsub("\\","\\\\")
    cmd << msg2
  end

  cmdln = cmd.join(" ")
  log_debug "Stash changes : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    [st.success?, res]
  end

end

#stash_clearObject

stash_to_branch



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/git_cli/stash.rb', line 222

def stash_clear

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash clear"

  cmdln = cmd.join(" ")
  log_debug "Clear the stash list (all uncommitted changes lost permanently) : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    [st.success?, res]
  end
  
end

#stash_listObject

List all saved temporary changes



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
# File 'lib/git_cli/stash.rb', line 105

def stash_list

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash list"

  cmdln = cmd.join(" ")
  log_debug "Stash list : #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    if st.success?
      list = { }
      res.each_line do |l|
        ll = l.split(": ")

        # first element is the stash id
        # 2nd element is the info on branch
        branch = ll[1].split(" ")[-1]


        list[ll[0].strip] = [branch, ll[1..-1].join(": ")] 
      end

      [true, list]
    else
      [false, res]
    end
  end

end

#stash_remove(id = nil) ⇒ Object

stash clear



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/git_cli/stash.rb', line 241

def stash_remove(id = nil)
 
  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash drop"
  if not is_empty?(id)
    cmd << id
  end

  cmdln = cmd.join(" ")
  log_debug "Stash #{is_empty?(id) ? "remove" : "'#{id}' is being removed"} from list (all uncommitted changes lost permanantly): #{cmdln}"
  res = os_exec(cmdln) do |st, res|
    [st.success?, res]
  end
 
end

#stash_restore(id = nil) ⇒ Object

Restore the temporary changes Regardless which branch are you in



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/git_cli/stash.rb', line 144

def stash_restore(id = nil)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash apply"

  if not is_empty?(id)
    cmd << id
  end

  cmdln = cmd.join(" ")
  log_debug "Stash apply (restore the saved state) : #{cmdln}"
  res = os_exec(cmdln) do |st, res|

    [st.success?, res]

  end

end

#stash_restore_and_remove(id = nil) ⇒ Object

stash restore



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/git_cli/stash.rb', line 169

def stash_restore_and_remove(id = nil)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash pop"

  if not is_empty?(id)
    cmd << id
  end

  cmdln = cmd.join(" ")
  log_debug "Stash pop (restore the saved state and delete from list) : #{cmdln}"
  res = os_exec(cmdln) do |st, res|

    [st.success?, res]

  end

end

#stash_to_new_branch(branch, id = nil) ⇒ Object

stash restore



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/git_cli/stash.rb', line 194

def stash_to_new_branch(branch, id = nil)

  raise_if_empty(branch, "Branch name must not be empty for stash to branch operation", GitCliException)

  check_vcs

  cmd = []
  cmd << "cd"
  cmd << @wsPath
  cmd << "&&"
  cmd << @vcs.exe_path
  cmd << "stash"
  cmd << branch

  if not is_empty?(id)
    cmd << id
  end

  cmdln = cmd.join(" ")
  log_debug "Stash to new branch and delete from list : #{cmdln}"
  res = os_exec(cmdln) do |st, res|

    [st.success?, res]

  end
  
end