class GitReference
Public Class Methods
new(args)
click to toggle source
Calls superclass method
HasProperties::new
# File lib/gpack/core/GitReference.rb, line 46 def initialize args # Non-Required defaults @readonly = true super end
Public Instance Methods
archive()
click to toggle source
# 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)
click to toggle source
# 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_writable()
click to toggle source
# 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
checkout()
click to toggle source
# 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
clone()
click to toggle source
# 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_branch()
click to toggle source
# 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_branch()
click to toggle source
# 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_clean()
click to toggle source
# 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_exists()
click to toggle source
# File lib/gpack/core/GitReference.rb, line 354 def local_exists() if Dir.exists?(@localdir) return true else return false end end
local_rev()
click to toggle source
# File lib/gpack/core/GitReference.rb, line 344 def local_rev() revname = localcmd("git rev-parse --short HEAD") return revname end
local_url()
click to toggle source
# 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)
click to toggle source
# File lib/gpack/core/GitReference.rb, line 235 def localcmd(cmd_str) return `cd #{@localdir} && #{cmd_str}`.chomp end
print()
click to toggle source
# File lib/gpack/core/GitReference.rb, line 362 def print() puts "Reference #{@url}\n\tlocaldir-#{@localdir}\n\tbranch-#{@branch}\n\treadonly-#{@readonly}" end
remove()
click to toggle source
# 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)
click to toggle source
# 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
rinse()
click to toggle source
# 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)
click to toggle source
# 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
status()
click to toggle source
# 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)
click to toggle source
# 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
update()
click to toggle source
# 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