Class: Argus::Git

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(org, repo, branch = 'master', sha = nil) ⇒ Git

Returns a new instance of Git.



5
6
7
8
9
10
# File 'lib/argus/git.rb', line 5

def initialize(org, repo, branch = 'master', sha = nil)
  @org    = org
  @repo   = repo
  @branch = branch
  @sha    = sha
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



3
4
5
# File 'lib/argus/git.rb', line 3

def branch
  @branch
end

#orgObject (readonly)

Returns the value of attribute org.



3
4
5
# File 'lib/argus/git.rb', line 3

def org
  @org
end

#repoObject (readonly)

Returns the value of attribute repo.



3
4
5
# File 'lib/argus/git.rb', line 3

def repo
  @repo
end

#shaObject (readonly)

Returns the value of attribute sha.



3
4
5
# File 'lib/argus/git.rb', line 3

def sha
  @sha
end

Instance Method Details

#checkoutObject

checkout branch of an existing repo

Raises:



42
43
44
45
46
# File 'lib/argus/git.rb', line 42

def checkout
  puts "repo exists, pulling #{self}"
  %x[git fetch && git checkout -f #{branch} && git reset --hard origin/#{branch}]
  raise ArgusError, "git checkout failed for #{self}" unless $? == 0
end

#cloneObject

clone a new repo

Raises:



49
50
51
52
53
# File 'lib/argus/git.rb', line 49

def clone
  puts "new repo, cloning #{self}"
  %x[git clone -b #{branch} #{url} .] # not found: clone it
  raise ArgusError, "git clone failed for #{self}" unless $? == 0
end

#is_inside_work_tree?Boolean

is this dir a git repo?

Returns:

  • (Boolean)


26
27
28
# File 'lib/argus/git.rb', line 26

def is_inside_work_tree?
  %x[git rev-parse --is-inside-work-tree 2> /dev/null].chomp == 'true'
end

#pullObject

pull existing, or new git repo, and return sha



31
32
33
34
35
36
37
38
39
# File 'lib/argus/git.rb', line 31

def pull
  if is_inside_work_tree?
    checkout
  else
    clone
  end
  reset if sha              # specific sha was requested
  @sha = rev_parse          # return sha
end

#resetObject

get a specific commit



56
57
58
59
# File 'lib/argus/git.rb', line 56

def reset
  puts "specific sha requested, resetting to #{sha}"
  %x[git fetch && git reset --hard #{sha}]
end

#rev_parseObject

get current sha



62
63
64
# File 'lib/argus/git.rb', line 62

def rev_parse
  %x[git rev-parse #{branch} ].chomp
end

#to_sObject



12
13
14
# File 'lib/argus/git.rb', line 12

def to_s
  "#{org}/#{repo}:#{branch}"
end

#urlObject

if we have a token, use https, else depend on ssh being set up



17
18
19
20
21
22
23
# File 'lib/argus/git.rb', line 17

def url
  if ENV['GITHUB_TOKEN']
    "https://#{ENV['GITHUB_TOKEN']}@github.com/#{org}/#{repo}.git"
  else
    "[email protected]:#{org}/#{repo}.git"
  end
end