Module: Gitchefsync::Git
- Defined in:
- lib/gitchefsync/git_util.rb
Class Method Summary collapse
-
.branchTags(path, branch) ⇒ Object
Return all git tags (which map to a SHA-1 hash) that only exist on monitoring branch (@rel_branch) Solves MAND-602 “ChefSync - Tagged Cookbooks on non-targeted git branches get synced”.
-
.cmd(x) ⇒ Object
executes a command line process raises and exception on stderr returns the sys output.
- .cmdNoError(x) ⇒ Object
-
.gitInit(path) ⇒ Object
check that path exists and git is intializated.
- .hasGit ⇒ Object
-
.remoteExists(path, remote) ⇒ Object
a check to determine if a repository exists this is inherintly dangerous operation, as network issues could prevent the real issue will provide a 2 stage check, one a pull (if successful) return true second, if that failts.
Class Method Details
.branchTags(path, branch) ⇒ Object
Return all git tags (which map to a SHA-1 hash) that only exist on monitoring branch (@rel_branch) Solves MAND-602 “ChefSync - Tagged Cookbooks on non-targeted git branches get synced”
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gitchefsync/git_util.rb', line 66 def self.branchTags(path, branch) self.cmd "cd #{path} && git clean -xdf" self.cmd "cd #{path} && git checkout #{branch}" git_graph = "git log --graph --oneline --branches=master --pretty='%H'" = "#{git_graph} | grep '^*' |" + " tr -d '|' | awk '{ print $2 }' |" + " xargs -n1 git describe --tags --exact-match 2>/dev/null" status = `cd #{path} && git status`.split(/\n/) graph = `cd #{path} && #{git_graph}`.split(/\n/) Gitchefsync.logger.debug "event_id=branchTags: path=#{path}, status=#{status}" Gitchefsync.logger.debug "event_id=branchTags: path=#{path}, graph=#{graph}" = self.cmd("cd #{path} && #{branch_tags}").split(/\n/) Gitchefsync.logger.info "event_id=branchTags: path=#{path}, tags=#{tags}" end |
.cmd(x) ⇒ Object
executes a command line process raises and exception on stderr returns the sys output
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/gitchefsync/git_util.rb', line 88 def self.cmd(x) ret = nil err = nil Open3.popen3(x) do |stdin, stdout, stderr, wait_thr| ret = stdout.read err = stderr.read end ret << err if ret.start_with?"error:" raise GitError, "stderr=#{ret}:cmd=#{x}" end if ret.start_with?"fatal:" raise GitError, "stderr=#{ret}:cmd=#{x}" end ret end |
.cmdNoError(x) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/gitchefsync/git_util.rb', line 106 def self.cmdNoError(x) ret = nil err = nil Open3.popen3(x) do |stdin, stdout, stderr, wait_thr| ret = stdout.read err = stderr.read end ret << err ret end |
.gitInit(path) ⇒ Object
check that path exists and git is intializated
34 35 36 |
# File 'lib/gitchefsync/git_util.rb', line 34 def self.gitInit (path) return File.directory?(path + "/.git") end |
.hasGit ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/gitchefsync/git_util.rb', line 23 def self.hasGit() begin git_ver = `git --version` return git_ver.match('git version .*') rescue #TODO raise NoGit, "Git must be installed" end end |
.remoteExists(path, remote) ⇒ Object
a check to determine if a repository exists this is inherintly dangerous operation, as network issues could prevent the real issue will provide a 2 stage check, one a pull (if successful) return true second, if that failts
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gitchefsync/git_util.rb', line 43 def self.remoteExists(path, remote) Gitchefsync.logger.debug "event_id=checkRemoteExists:path=#{path}" begin self.cmd("cd #{path} && git ls-remote") return true rescue GitError => e Gitchefsync.logger.warn "event_id=git_pull_err:msg=#{e.message}" end # we've passed the first begin #arbitrary gitlab command Gitlab.users() #successfully called Gitlab and not been able to pull remotely: give up return false rescue Exception => e #in the face of not being able to contact Gitlab (for whatever reason) assume repository alive return true end return true end |