Class: CapsuleCD::GitUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/capsulecd/base/common/git_utils.rb

Class Method Summary collapse

Class Method Details

.checkout(repo_path, branch) ⇒ Object



25
26
27
28
# File 'lib/capsulecd/base/common/git_utils.rb', line 25

def self.checkout(repo_path, branch)
  repo = Git.open(repo_path)
  repo.checkout(branch)
end

.clone(parent_path, repository_name, git_remote) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/capsulecd/base/common/git_utils.rb', line 6

def self.clone(parent_path, repository_name, git_remote)
  repo_path = File.expand_path("#{parent_path}/#{repository_name}/")
  if !File.directory?(repo_path)
    FileUtils.mkdir_p(repo_path)
  else
    fail 'the repository path already exists, this should never happen'
  end

  repo = Git.clone(git_remote, '', path: repo_path)
  repo.config('user.name', 'CapsuleCD')
  repo.config('user.email', '[email protected]')
  repo.dir.to_s
end

.commit(repo_path, message, _all = true) ⇒ Object



30
31
32
33
34
# File 'lib/capsulecd/base/common/git_utils.rb', line 30

def self.commit(repo_path, message, _all = true)
  repo = Git.open(repo_path)
  repo.add(all: true)
  repo.commit_all(message)
end

.create_gitignore(repo_path, ignore_types) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/capsulecd/base/common/git_utils.rb', line 73

def self.create_gitignore(repo_path, ignore_types)
  #store current working directory, and change to the cookbook
  wd = Dir.getwd
  Dir.chdir(repo_path)

  begin
    #download gitignore templates and write them
    templates = Mkgitignore::searchForTemplatesWithNames(ignore_types)
    gitignore = String.new
    templates.each { |t| gitignore += Mkgitignore::downloadFromURL(t["url"], t["name"]) }
    Mkgitignore::writeGitignore(gitignore, true)
  ensure
    #restore previous working dir
    Dir.chdir(wd)
  end
end

.fetch(repo_path, remote_ref, local_branch) ⇒ Object



20
21
22
23
# File 'lib/capsulecd/base/common/git_utils.rb', line 20

def self.fetch(repo_path, remote_ref, local_branch)
  repo = Git.open(repo_path)
  repo.fetch(['origin', "#{remote_ref}:#{local_branch}"])
end

.generate_changelog(repo_path, base_sha, head_sha, full_name) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/capsulecd/base/common/git_utils.rb', line 63

def self.generate_changelog(repo_path, base_sha, head_sha, full_name)
  repo = Git.open(repo_path)
  markdown = "Timestamp |  SHA | Message | Author \n"
  markdown += "------------- | ------------- | ------------- | ------------- \n"
  repo.log.between(base_sha, head_sha).each do |commit|
    markdown += "#{commit.date.strftime('%Y-%m-%dT%H:%M:%SZ')} | [`#{commit.sha.slice 0..8}`](https://github.com/#{full_name}/commit/#{commit.sha}) | #{commit.message.gsub('|', '!').gsub(/[\n]+/,' ') || '--'} | #{commit.author.name} \n"
  end
  markdown
end

.get_latest_tag_commit(repo_path) ⇒ Object

gets the commit of the latest tag on current branch



52
53
54
55
56
57
58
59
60
61
# File 'lib/capsulecd/base/common/git_utils.rb', line 52

def self.get_latest_tag_commit(repo_path)
  repo = Git.open(repo_path)
  tag_name = repo.describe(nil,:abbrev => 0, :exact_match => true)

  #we're dealing with annotated tags, which have thier own git commit, which causes issues with the github release api
  # so we're manually creating a Git Object with the data we need (.sha and .name)
  tag_sha = repo.tag(tag_name).log.first.sha

  Git::Object::Tag.new(repo,tag_sha, tag_name)
end

.pull(repo_path) ⇒ Object



46
47
48
49
# File 'lib/capsulecd/base/common/git_utils.rb', line 46

def self.pull(repo_path)
  repo = Git.open(repo_path)
  repo.pull
end

.push(repo_path, local_branch, remote_branch) ⇒ Object



41
42
43
44
# File 'lib/capsulecd/base/common/git_utils.rb', line 41

def self.push(repo_path, local_branch, remote_branch)
  repo = Git.open(repo_path)
  repo.push('origin', "#{local_branch}:#{remote_branch}", tags: true)
end

.tag(repo_path, version) ⇒ Object



36
37
38
39
# File 'lib/capsulecd/base/common/git_utils.rb', line 36

def self.tag(repo_path, version)
  repo = Git.open(repo_path)
  repo.add_tag(version)
end