Class: Coaster::Git::Repository
- Includes:
- Coaster::Git
- Defined in:
- lib/coaster/git/repository.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #add(*paths, **options) ⇒ Object
- #branch(*args, **options) ⇒ Object
- #checkout(*args, **options) ⇒ Object
- #commit(*args, **options) ⇒ Object
- #current_sha ⇒ Object
- #deep_merge(pointer) ⇒ Object
- #fetch(*args, **options) ⇒ Object
-
#initialize(path) ⇒ Repository
constructor
A new instance of Repository.
- #merge(pointer, *args, **options) ⇒ Object
- #merge_without_submodules ⇒ Object
- #pointers(sha) ⇒ Object
- #remove ⇒ Object
- #run_cmd(command, path: nil) ⇒ Object
- #run_git_cmd(command, *args, **options) ⇒ Object
- #status(*args, **options) ⇒ Object
- #submodule_add!(repo, path, *args, **options) ⇒ Object
- #submodule_init!(*paths) ⇒ Object
- #submodule_paths ⇒ Object
- #submodule_sha(path, pointer: nil) ⇒ Object
- #submodule_update!(*paths, **options) ⇒ Object
- #submodules ⇒ Object
- #with_git_options(*args, **options, &block) ⇒ Object
Methods included from Coaster::Git
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
#path ⇒ Object (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, **) opts = Options.new('add', **) 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, **) run_git_cmd("branch", *args, **) end |
#checkout(*args, **options) ⇒ Object
43 44 45 |
# File 'lib/coaster/git/repository.rb', line 43 def checkout(*args, **) run_git_cmd("checkout", *args, **) end |
#commit(*args, **options) ⇒ Object
33 34 35 36 37 |
# File 'lib/coaster/git/repository.rb', line 33 def commit(*args, **) opts = Options.new('commit', *args, **) opts['--message'] ||= "no message" run_git_cmd("commit", opts) end |
#current_sha ⇒ Object
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, **) run_git_cmd("fetch", *args, **) 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, **) opts = Options.new('merge', *args, **) pointers = pointers(pointer).join(',') puts "[MERGE] #{path} #{pointers} #{}" opts['--message'] ||= "Merge #{pointers}" run_git_cmd("merge #{pointer} #{opts}") end |
#merge_without_submodules ⇒ Object
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 |
#remove ⇒ Object
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, **) opts = Options.new(command, *args, **) 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, **) run_git_cmd("status", *args, **) 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, **) run_git_cmd(["submodule", 'add'], repo, path, *args, **) 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_paths ⇒ Object
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, **) run_git_cmd(["submodule", "update"], *paths, **) end |
#submodules ⇒ Object
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 |