Class: GitReference

Inherits:
Object
  • Object
show all
Includes:
HasProperties
Defined in:
lib/gpack/core/GitReference.rb

Instance Attribute Summary

Attributes included from HasProperties

#props, #require_attrs

Instance Method Summary collapse

Methods included from HasProperties

#has_properties, #has_required, included

Constructor Details

#initialize(args) ⇒ GitReference

Returns a new instance of GitReference.



46
47
48
49
50
51
# File 'lib/gpack/core/GitReference.rb', line 46

def initialize args
   # Non-Required defaults
   @readonly   = true

   super
end

Instance Method Details

#archiveObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/gpack/core/GitReference.rb', line 279

def archive()
   #Archive the Git Repository
   
   checks_failed = false
   
   #check if directory already exists
   if !self.check()
      command_failed = true
   else
      git_ref = local_rev
      dirname = @localdir.match( /\/([^\/]*)\s*$/)[1].chomp
      tarname = "#{dirname}_#{git_ref}.tar.gz"
      tarcmd = "tar -zcvf #{tarname} #{@localdir} > /dev/null"
      syscmd(tarcmd)
   end
   
   return command_failed
end

#check(skip_branch = false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gpack/core/GitReference.rb', line 141

def check(skip_branch=false)
   #Check integrety
   #  Check that URL matches
   #  Check that branch matches
   #  Check that there are no local changes "clean" state
   check_git_writable()
   
   puts "\nRunning checks on local repository #{@localdir}"
   checks_failed = false
   if local_exists
      if !skip_branch
         if is_branch()
            bname = @branch
         else
            bname = rev_parse(@branch)
         end
         branch_valid = local_branch() == bname
         if !branch_valid
            puts "\tFAIL - Check branch matches #{@branch} rev #{bname}".color(Colors::RED)
            puts "\t\tLocal  Branch abbrev    : '#{rev_parse("HEAD",true)}'"
            puts "\t\tLocal  Branch SHA       : '#{rev_parse("HEAD")}'"
            puts "\t\tSpecified Branch        :    '#{@branch}'"
            puts "\t\tSpecified Branch abbrev : '#{rev_parse(@branch)}'"
            puts "\t\tSpecified Branch SHA    : '#{rev_parse(@branch,true)}'"
            checks_failed = true
         end
      end

      if local_url() == @url
         #puts "\tPASS - Check remote url matches #{@url}"
      else
         puts "\tFAIL - Check remote url matches #{@url}".color(Colors::RED)
         puts "\t\tLocal URL #{local_url()}'"
         puts "\t\tRemote URL #{@url}'"
         checks_failed = true
      end

      if local_clean()
         #puts "\tPASS - Check local repository clean"
      else
         puts "\tFAIL - Check local repository clean".color(Colors::RED)
         checks_failed = true
      end
      
      if !checks_failed
         puts "PASS - All checks on local repository #{@localdir}".color(Colors::GREEN)
      else
         puts "CHECK FAILURE on local repository #{@localdir}. See previous log for info on which check failed".color(Colors::RED)
      end
   else
      puts "\tFAIL - Check local repository exists".color(Colors::RED)
      checks_failed = true
   end
   return checks_failed
end

#check_git_writableObject



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/gpack/core/GitReference.rb', line 298

def check_git_writable()
   # Make sure .git folder is writable
   
   gitdirs = `find #{localdir} -type d -name ".git"`
   gitdirs.each_line do |dir|
      dir.chomp!
      if !File.writable?(dir)
         puts "Warning, .git folder #{dir} was found read-only. Automatically setting it to writable"
         syscmd("chmod ug+w -R #{dir}")
      end
   end
end

#checkoutObject



101
102
103
104
105
106
107
108
# File 'lib/gpack/core/GitReference.rb', line 101

def checkout()
   if is_branch()
      checkout_cmd = "checkout -B #{@branch} origin/#{@branch}" # Create a local branch
   else
      checkout_cmd = "checkout #{@branch}" # Direct checkout the tag/comit
   end
   syscmd("git #{checkout_cmd} && git submodule update --init --recursive")  
end

#cloneObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gpack/core/GitReference.rb', line 52

def clone()
 
   #Clone the Git Repository
   checks_failed = false
   
   #check if directory already exists
   if local_exists
      puts "Cloning Warning - Directory #{localdir} already exists! Running checks instead"
      checks_failed = self.check()
   else
      status = syscmd("git clone -b #{branch} #{url} #{localdir} --recursive",true,false)
      self.checkout
      self.set_writeable(false) if @readonly
      
      if status != 0
         checks_failed = true
      end
      
   end
   
   return checks_failed
end

#is_branchObject



316
317
318
319
# File 'lib/gpack/core/GitReference.rb', line 316

def is_branch()
   #check if branch ID is a branch or a tag/commit
   return system("cd #{localdir} && git show-ref -q --verify refs/remotes/origin/#{@branch}")
end

#local_branchObject



321
322
323
324
325
326
327
328
# File 'lib/gpack/core/GitReference.rb', line 321

def local_branch()
   if is_branch()
      bname = rev_parse("HEAD",true)
   else
      bname = rev_parse("HEAD")
   end
   return bname
end

#local_cleanObject



349
350
351
352
# File 'lib/gpack/core/GitReference.rb', line 349

def local_clean()
   clean = localcmd("git status --porcelain")
   return clean == "" # Empty string means it's clean
end

#local_existsObject



354
355
356
357
358
359
360
# File 'lib/gpack/core/GitReference.rb', line 354

def local_exists()
   if Dir.exists?(@localdir)
      return true
   else
      return false
   end
end

#local_revObject



344
345
346
347
# File 'lib/gpack/core/GitReference.rb', line 344

def local_rev()
   revname = localcmd("git rev-parse --short HEAD")
   return revname
end

#local_urlObject



339
340
341
342
# File 'lib/gpack/core/GitReference.rb', line 339

def local_url()
   urlname = localcmd("git config --get remote.origin.url")
   return urlname
end

#localcmd(cmd_str) ⇒ Object



235
236
237
# File 'lib/gpack/core/GitReference.rb', line 235

def localcmd(cmd_str)
   return `cd #{@localdir} && #{cmd_str}`.chomp
end


362
363
364
# File 'lib/gpack/core/GitReference.rb', line 362

def print()
   puts "Reference #{@url}\n\tlocaldir-#{@localdir}\n\tbranch-#{@branch}\n\treadonly-#{@readonly}"
end

#removeObject



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/gpack/core/GitReference.rb', line 239

def remove()
   force = $SETTINGS["core"]["force"]
   command_failed = false
   if force || !self.check
      puts "Removing local repository #{@localdir}"
      self.set_writeable(true) if @readonly || force
      syscmd("rm -rf #{@localdir}",false,false)
      command_failed = false
   else
      command_failed = true
   end
   return command_failed
end

#rev_parse(rev, abbrev = false) ⇒ Object



330
331
332
333
334
335
336
337
# File 'lib/gpack/core/GitReference.rb', line 330

def rev_parse(rev,abbrev=false)
   if abbrev
      rp = localcmd("git rev-parse --abbrev-ref #{rev}")
   else
      rp = localcmd("git rev-parse #{rev}")
   end
   return rp
end

#rinseObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/gpack/core/GitReference.rb', line 253

def rinse()
   force = $SETTINGS["core"]["force"]
   if !@readonly && !force
      puts "Error with repository #{@localdir}\n\t Repositories can only be rinsed when in readonly mode"
      command_failed = true
   else
      self.set_writeable(true) if @readonly
      status = syscmd( \
      "git fetch origin && " \
      "git clean -xdff && "  \
      "git reset --hard && " \
      "git submodule foreach --recursive git clean -xdff && " \
      "git submodule foreach --recursive git reset --hard && " \
      "git submodule update --init --recursive")
      self.checkout
      self.set_writeable(false) if @readonly
      if !status
         command_failed = true
         puts "Rinse command failed for repo #{@localdir}, check log"
      end
   end
   
   
   return command_failed
end

#set_writeable(tf) ⇒ Object



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
# File 'lib/gpack/core/GitReference.rb', line 110

def set_writeable(tf)

   if tf
      puts "Setting #{@localdir} to writable"
      perms = "u+w" 
   else
      puts "Setting #{@localdir} to read only"
      perms = "a-w" 
   end
   
   file_paths = []
   ignore_paths = []
   if local_exists()
      Find.find(@localdir) do |path|
         # Ignore .git folder
         if path.match(/.*\/.git$/) || path.match(/.*\/.git\/.*/)
            ignore_paths << path
         else
            file_paths << path
            #FileUtils.chmod 'a-w', path
            FileUtils.chmod(perms,path) if File.exist?(path)
         end
      end
   end
   
   # Useful for debug
   #puts "IGNORED PATHS\n"+ignore_paths.to_s
   #puts "FOUND_PATHS\n"+file_paths.to_s
   
end

#statusObject



310
311
312
313
314
# File 'lib/gpack/core/GitReference.rb', line 310

def status
   self.print()
   syscmd("git status && echo 'Git Branch' && git branch && echo 'Git SHA' && git rev-parse HEAD")
   return false
end

#syscmd(cmd, open_xterm = false, cd_first = true) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/gpack/core/GitReference.rb', line 197

def syscmd(cmd,open_xterm=false,cd_first=true)
   if cd_first
      cmd = "cd #{@localdir} && #{cmd}"
   end
   
   #Pass env var to Open3
   ssh_cmd = $SETTINGS["ssh"]["cmd"]
   if ssh_cmd
      args = {"GIT_SSH_COMMAND" => ssh_cmd}
      puts "custom ssh"
   else
      args = {}
   end
   
   if open_xterm && $SETTINGS["gui"]["show"]
      if $SETTINGS["gui"]["persist"]
         hold_opt = "-hold"
      end
      if ssh_cmd
         cmd = "echo 'GIT_SSH_COMMAND $GIT_SSH_COMMAND' ; #{cmd}"
      end
      cmd = "xterm #{hold_opt} -geometry 90x30 -e \"#{cmd} || echo 'Command Failed, see log above. Press CTRL+C to close window' && sleep infinity\""
   end
   cmd_id = Digest::SHA1.hexdigest(cmd).to_s[0..4]
   
   stdout_str,stderr_str,status = Open3.capture3(args,cmd)
   
   puts "="*30+"COMMAND ID #{cmd_id}"+"="*28+"\n"
   puts ("#{cmd}").color(Colors::YELLOW)
   if stdout_str != "" || stderr_str != ""
      puts "="*30+"COMMAND #{cmd_id} LOG START"+"="*28+"\n"
      puts stderr_str
      puts stdout_str
      puts "="*30+"COMMAND #{cmd_id} LOG END"+"="*30+"\n"
   end
   status
end

#updateObject



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/gpack/core/GitReference.rb', line 75

def update()
   force_clone = $SETTINGS["core"]["force"] || $SETTINGS["core"]["install"]
   command_failed = false
   # Returns true if falure
   if local_exists
      checks_failed = self.check(true) # TODO, should this fail if branch is wrong?
      if !checks_failed
         puts "Updating local repository #{@localdir}"
         self.set_writeable(true) if @readonly
         syscmd("git fetch origin",true)
         self.checkout
         syscmd("git submodule update --init --recursive")
         self.set_writeable(false) if @readonly
         command_failed = false
      else
         command_failed = true
      end
   elsif force_clone
      self.clone
      command_failed = false
   else
      command_failed = true
   end
   return command_failed
end