Class: CIQuantum::Git

Inherits:
Struct
  • Object
show all
Defined in:
lib/ciquantum/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



2
3
4
# File 'lib/ciquantum/git.rb', line 2

def path
  @path
end

Instance Method Details

#branchObject



28
29
30
31
32
33
# File 'lib/ciquantum/git.rb', line 28

def branch
  if !@branch && exec("cd #{path} && git branch 2>/dev/null | grep -e '\*' ") =~ /^\*\s(\w+)/
    @branch = $1.strip
  end
  @branch
end

#branchesObject



20
21
22
# File 'lib/ciquantum/git.rb', line 20

def branches
  branches_to_list `cd #{path} && git branch -r`
end

#branches_with_commit(commit_sha) ⇒ Object



48
49
50
51
# File 'lib/ciquantum/git.rb', line 48

def branches_with_commit commit_sha
  fetch
  branches_to_list exec("cd #{path} && git branch -r --contains #{commit_sha}")
end

#checkout(other_branch) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/ciquantum/git.rb', line 39

def checkout other_branch
  unless is_current_branch?(other_branch)
    fetch
    exec("cd #{path} && git checkout #{other_branch}") if branches.include?(other_branch)
    @branch = nil
  end
  update
end

#current_branch_contains_commit?(commit_sha) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/ciquantum/git.rb', line 53

def current_branch_contains_commit? commit_sha
  return false if commit_sha.nil? || commit_sha.empty?
  branches_with_commit(commit_sha).include?(branch)
end

#exec(command) ⇒ Object



4
5
6
7
8
9
# File 'lib/ciquantum/git.rb', line 4

def exec command
  puts "Executing git command: #{command}"
  res = `#{command}`
  puts "Result for git command:\n#{res}"
  res
end

#fetchObject



24
25
26
# File 'lib/ciquantum/git.rb', line 24

def fetch
  exec("cd #{path} && git fetch origin")
end

#is_current_branch?(other_branch) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/ciquantum/git.rb', line 35

def is_current_branch? other_branch
  return branch.eql? other_branch
end

#shaObject



11
12
13
# File 'lib/ciquantum/git.rb', line 11

def sha
  exec("cd #{path} && git rev-parse origin/#{branch}").chomp
end

#updateObject



15
16
17
18
# File 'lib/ciquantum/git.rb', line 15

def update
  fetch
  exec("cd #{path} && git reset --hard origin/#{branch} && git submodule update --init")
end