Class: Kumade::Git

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.environmentsObject


9
10
11
# File 'lib/kumade/git.rb', line 9

def self.environments
  url_remotes = `git remote`.strip.split("\n").map{|remote| [remote, `git config --get remote.#{remote}.url`.strip] }.select{|remote| remote.last =~ /^[email protected]\.com:(.+)\.git$/}.map{|remote| remote.first}
end

Instance Method Details

#add_and_commit_all_assets_in(dir) ⇒ Object


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

def add_and_commit_all_assets_in(dir)
  command = ["git checkout -b #{Kumade::Heroku::DEPLOY_BRANCH} 2>/dev/null",
             "git add -f #{dir}",
             "git commit -m 'Compiled assets.'"].join(' && ')
  command_line = CommandLine.new(command)
  command_line.run_or_error("Cannot deploy: couldn't commit assets")
  Kumade.configuration.outputter.success("Added and committed all assets")
end

#create(branch) ⇒ Object


25
26
27
28
29
# File 'lib/kumade/git.rb', line 25

def create(branch)
  unless has_branch?(branch)
    CommandLine.new("git branch #{branch} >/dev/null").run_or_error("Failed to create #{branch}")
  end
end

#current_branchObject


47
48
49
# File 'lib/kumade/git.rb', line 47

def current_branch
  `git symbolic-ref HEAD`.sub("refs/heads/", "").strip
end

#delete(branch_to_delete, branch_to_checkout) ⇒ Object


31
32
33
34
35
36
# File 'lib/kumade/git.rb', line 31

def delete(branch_to_delete, branch_to_checkout)
  if has_branch?(branch_to_delete)
    command_line = CommandLine.new("git checkout #{branch_to_checkout} 2>/dev/null && git branch -D #{branch_to_delete}")
    command_line.run_or_error("Failed to clean up #{branch_to_delete} branch")
  end
end

#dirty?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'lib/kumade/git.rb', line 59

def dirty?
  ! CommandLine.new("git diff --exit-code").run
end

#ensure_clean_gitObject


63
64
65
66
67
68
69
# File 'lib/kumade/git.rb', line 63

def ensure_clean_git
  if ! Kumade.configuration.pretending? && dirty?
    Kumade.configuration.outputter.error("Cannot deploy: repo is not clean.")
  else
    Kumade.configuration.outputter.success("Git repo is clean")
  end
end

#heroku_remote?Boolean

Returns:

  • (Boolean)

4
5
6
7
# File 'lib/kumade/git.rb', line 4

def heroku_remote?
  remote_url = `git config --get remote.#{Kumade.configuration.environment}.url`.strip
  !! remote_url.strip.match(/^[email protected]\..+:(.+)\.git$/)
end

#push(branch, remote = 'origin', force = false) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kumade/git.rb', line 13

def push(branch, remote = 'origin', force = false)
  command = ["git push"]
  command << "-f" if force
  command << remote
  command << branch
  command = command.join(" ")

  command_line = CommandLine.new(command)
  command_line.run_or_error("Failed to push #{branch} -> #{remote}")
  Kumade.configuration.outputter.success("Pushed #{branch} -> #{remote}")
end

#remote_exists?(remote_name) ⇒ Boolean

Returns:

  • (Boolean)

51
52
53
54
55
56
57
# File 'lib/kumade/git.rb', line 51

def remote_exists?(remote_name)
  if Kumade.configuration.pretending?
    true
  else
    `git remote` =~ /^#{remote_name}$/
  end
end