Module: Gitchefsync::Git

Defined in:
lib/gitchefsync/git_util.rb

Class Method Summary collapse

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”



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gitchefsync/git_util.rb', line 50

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'"
  branch_tags = "#{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}"

  tags = self.cmd("cd #{path} && #{branch_tags}").split(/\n/)
  Gitchefsync.logger.info "event_id=branchTags: path=#{path}, tags=#{tags}"
  tags
end

.cmd(x) ⇒ Object

executes a command line process raises and exception on stderr returns the sys output



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gitchefsync/git_util.rb', line 72

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



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitchefsync/git_util.rb', line 90

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



18
19
20
# File 'lib/gitchefsync/git_util.rb', line 18

def self.gitInit (path)
  return File.directory?(path + "/.git")
end

.hasGitObject



7
8
9
10
11
12
13
14
15
# File 'lib/gitchefsync/git_util.rb', line 7

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitchefsync/git_util.rb', line 27

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