Class: Coaster::Git::Repository

Inherits:
Object
  • Object
show all
Includes:
Coaster::Git
Defined in:
lib/coaster/git/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Coaster::Git

create

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



8
9
10
# File 'lib/coaster/git/repository.rb', line 8

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/coaster/git/repository.rb', line 6

def path
  @path
end

Instance Method Details

#add(*paths, **options) ⇒ Object



28
29
30
31
# File 'lib/coaster/git/repository.rb', line 28

def add(*paths, **options)
  opts = Options.new('add', **options)
  run_git_cmd("add", *paths, opts)
end

#branch(*args, **options) ⇒ Object



39
40
41
# File 'lib/coaster/git/repository.rb', line 39

def branch(*args, **options)
  run_git_cmd("branch", *args, **options)
end

#checkout(*args, **options) ⇒ Object



43
44
45
# File 'lib/coaster/git/repository.rb', line 43

def checkout(*args, **options)
  run_git_cmd("checkout", *args, **options)
end

#commit(*args, **options) ⇒ Object



33
34
35
36
37
# File 'lib/coaster/git/repository.rb', line 33

def commit(*args, **options)
  opts = Options.new('commit', *args, **options)
  opts['--message'] ||= "no message"
  run_git_cmd("commit", opts)
end

#current_shaObject



67
68
69
# File 'lib/coaster/git/repository.rb', line 67

def current_sha
  run_git_cmd('rev-parse HEAD').strip
end

#deep_merge(pointer) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/coaster/git/repository.rb', line 116

def deep_merge(pointer)
  puts "[DEEP_MERGE] #{path} #{pointer}"
  submodules.values.each do |submodule|
    sm_sha = submodule_sha(submodule.path, pointer: pointer)
    submodule.merge(sm_sha) if sm_sha.present?
  end
  merge_without_submodules do
    merge(pointer) if pointer.present?
  end
end

#fetch(*args, **options) ⇒ Object



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

def fetch(*args, **options)
  run_git_cmd("fetch", *args, **options)
end

#merge(pointer, *args, **options) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/coaster/git/repository.rb', line 83

def merge(pointer, *args, **options)
  opts = Options.new('merge', *args, **options)
  pointers = pointers(pointer).join(',')
  puts "[MERGE] #{path} #{pointers} #{options}"
  opts['--message'] ||= "Merge #{pointers}"
  run_git_cmd("merge #{pointer} #{opts}")
end

#merge_without_submodulesObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/coaster/git/repository.rb', line 96

def merge_without_submodules
  run_git_cmd('config merge.ours.name "Keep ours merge driver"')
  run_git_cmd('config merge.ours.driver true')
  ga_file = File.join(@path, '.gitattributes')
  run_cmd("touch #{ga_file}")
  ga_lines = File.read(ga_file).split("\n")
  ga_lines_appended = ga_lines + submodules.keys.map{|sb_path| "#{sb_path} merge=ours" }
  File.open(ga_file, 'w') do |f|
    f.puts ga_lines_appended.join("\n")
  end
  add('.')
  commit
  yield
  File.open(ga_file, 'w') do |f|
    f.puts ga_lines.join("\n")
  end
  add('.')
  commit
end

#pointers(sha) ⇒ Object



127
128
129
130
131
132
# File 'lib/coaster/git/repository.rb', line 127

def pointers(sha)
  run_git_cmd("branch --contains #{sha}").split("\n").map do |br|
    br = (br.start_with?('*') ? br[2..-1] : br).strip
    br.match?(/^\(.*\)$/) ? nil : br
  end.compact
end

#removeObject



134
135
136
# File 'lib/coaster/git/repository.rb', line 134

def remove
  run_cmd("rm -rf #{path}", path: path.split('/')[0..-2].join('/'))
end

#run_cmd(command, path: nil) ⇒ Object



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

def run_cmd(command, path: nil)
  super(path || @path, command)
end

#run_git_cmd(command, *args, **options) ⇒ Object



22
23
24
25
26
# File 'lib/coaster/git/repository.rb', line 22

def run_git_cmd(command, *args, **options)
  opts = Options.new(command, *args, **options)
  cmd = "git #{@git_options} #{Array.wrap(command).join(' ')} #{opts}"
  run_cmd(cmd)
end

#status(*args, **options) ⇒ Object



63
64
65
# File 'lib/coaster/git/repository.rb', line 63

def status(*args, **options)
  run_git_cmd("status", *args, **options)
end

#submodule_add!(repo, path, *args, **options) ⇒ Object



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

def submodule_add!(repo, path, *args, **options)
  run_git_cmd(["submodule", 'add'], repo, path, *args, **options)
end

#submodule_init!(*paths) ⇒ Object



51
52
53
# File 'lib/coaster/git/repository.rb', line 51

def submodule_init!(*paths)
  run_git_cmd(["submodule", "init"], *paths)
end

#submodule_pathsObject



71
72
73
74
75
# File 'lib/coaster/git/repository.rb', line 71

def submodule_paths
  @submodule_paths ||= run_git_cmd('submodule status --recursive').split("\n").map do |line|
    line.split(' ')[1]
  end
end

#submodule_sha(path, pointer: nil) ⇒ Object



91
92
93
94
# File 'lib/coaster/git/repository.rb', line 91

def submodule_sha(path, pointer: nil)
  pointer ||= current_sha
  run_git_cmd("ls-tree #{pointer} #{path}").split(' ')[2]
end

#submodule_update!(*paths, **options) ⇒ Object



55
56
57
# File 'lib/coaster/git/repository.rb', line 55

def submodule_update!(*paths, **options)
  run_git_cmd(["submodule", "update"], *paths, **options)
end

#submodulesObject



77
78
79
80
81
# File 'lib/coaster/git/repository.rb', line 77

def submodules
  @submodules ||= submodule_paths.map do |path|
    [path, Git::Repository.new(File.join(@path, path))]
  end.to_h
end

#with_git_options(*args, **options, &block) ⇒ Object



16
17
18
19
20
# File 'lib/coaster/git/repository.rb', line 16

def with_git_options(*args, **options, &block)
  @git_options = Options.new('git', *args, **options)
  yield
  @git_options = nil
end