Class: GitWorkflow::Git::Repository

Inherits:
Object
  • Object
show all
Includes:
Execution, Logging, Singleton
Defined in:
lib/git_workflow/git.rb

Constant Summary collapse

RepositoryError =
Class.new(StandardError)
BranchError =
Class.new(RepositoryError)
CheckoutError =
Class.new(RepositoryError)
ConfigError =
Class.new(RepositoryError)

Instance Method Summary collapse

Methods included from Logging

default_logger, included, logger, logger=

Methods included from Execution

#execute_command, #execute_command_with_output_handling

Instance Method Details

#branchesObject



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

def branches
  execute_command('git branch').split("\n").map { |b| b.sub(/^(\*)?\s+/, '') }
end

#checkout(branch) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/git_workflow/git.rb', line 24

def checkout(branch)
  maintain_current_branch(branch) do
    begin
      info("Checking out branch '#{ branch }'") do
        execute_command("git checkout #{ branch }")
      end
    rescue Execution::CommandFailure => exception
      raise CheckoutError, "Unable to checkout branch '#{ branch }'"
    end
  end
end

#config_get(key) ⇒ Object



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

def config_get(key)
  command  = [ 'git config' ]
  command << '--file .git/config' if GitWorkflow::Configuration.instance.ignore_git_global?
  command << key
  value = execute_command(command.join(' ')).strip
  value.empty? ? nil : value
rescue Execution::CommandFailure => exception
  raise ConfigError, "Could not retrieve '#{ key }' configuration setting"
end

#config_set(key, value) ⇒ Object



74
75
76
77
78
79
# File 'lib/git_workflow/git.rb', line 74

def config_set(key, value)
  raise ConfigError, 'No key has been specified for configuration setting' if key.blank?
  execute_command(%Q{git config '#{ key }' '#{ value }'})
rescue Execution::CommandFailure => exception
  raise ConfigError, "Could not set '#{ key }' configuration setting (value: #{ value })"
end

#create(branch, source = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_workflow/git.rb', line 36

def create(branch, source = nil)
  maintain_current_branch(branch) do
    begin
      info("Creating branch '#{ branch }'") do
        command = "git checkout -b #{ branch }"
        command << " #{ source }" unless source.nil?
        execute_command(command)
      end
    rescue Execution::CommandFailure => exception
      raise CheckoutError, "Unable to create branch '#{ branch }'"
    end
  end
end

#current_branchObject

Raises:



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

def current_branch
  return @current_branch unless @current_branch.nil?

  match = execute_command('git branch').match(/\*\s+([^\s]+)/)
  raise BranchError, 'Could not determine current branch' if match.nil?
  @current_branch = match[ 1 ]
end

#does_branch_exist?(branch) ⇒ Boolean

Returns:

  • (Boolean)


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

def does_branch_exist?(branch)
  execute_command('git branch') =~ /\s+#{ branch }(\s+|$)/
end

#merge(branch) ⇒ Object



50
51
52
53
54
# File 'lib/git_workflow/git.rb', line 50

def merge(branch)
  execute_command("git merge #{ branch }")
rescue Execution::CommandFailure => exception
  raise BranchError, "Could not merge #{ branch } into current branch"
end

#push(branch) ⇒ Object



81
82
83
84
85
# File 'lib/git_workflow/git.rb', line 81

def push(branch)
  execute_command("git push origin #{ branch }")
rescue Execution::CommandFailure => exception
  raise BranchError, "Unable to push branch '#{ branch }'"
end