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 =~ /^git@heroku\.com:(.+)\.git$/}.map{|remote| remote.first}
end

Instance Method Details

#add_and_commit_all_assets_in(dir) ⇒ Object



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

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



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

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



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

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

#delete(branch_to_delete, branch_to_checkout) ⇒ Object



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

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)


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

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

#ensure_clean_gitObject



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

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

#has_untracked_files_in?(directory) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/kumade/git.rb', line 72

def has_untracked_files_in?(directory)
  relative_dir = directory.sub(Dir.pwd + '/', '')
  untracked_output = CommandLine.new("git status --porcelain --untracked-files").run
  untracked_output.split("\n").grep(/^\?{2} #{relative_dir}/).size > 0
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(/^git@heroku\..+:(.+)\.git$/)
end

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



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

def push(branch, remote = 'origin', force = false)
  return unless remote_exists?(remote)
  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)


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

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