Class: ModuleSync::Repository
- Inherits:
-
Object
- Object
- ModuleSync::Repository
- Defined in:
- lib/modulesync/repository.rb
Overview
Wrapper for Git in ModuleSync context
Instance Method Summary collapse
- #checkout_branch(branch) ⇒ Object
- #clone ⇒ Object
- #cloned? ⇒ Boolean
- #default_branch ⇒ Object
- #default_reset_branch(branch) ⇒ Object
- #git ⇒ Object
-
#initialize(directory:, remote:) ⇒ Repository
constructor
A new instance of Repository.
- #local_branch_exists?(branch) ⇒ Boolean
- #prepare_workspace(branch:, operate_offline:) ⇒ Object
- #push(branch:, remote_branch:, remote_name: 'origin') ⇒ Object
- #puts(*args) ⇒ Object
- #remote_branch_differ?(local_branch, remote_branch) ⇒ Boolean
- #remote_branch_exists?(branch) ⇒ Boolean
-
#repo ⇒ Object
This is an alias to minimize code alteration.
- #reset_workspace(branch:, operate_offline:, source_branch: nil) ⇒ Object
- #show_changes(options) ⇒ Object
-
#submit_changes(files, options) ⇒ Object
Git add/rm, git commit, git push.
- #switch(branch:) ⇒ Object
- #tag(version, tag_pattern) ⇒ Object
-
#untracked_unignored_files ⇒ Object
Needed because of a bug in the git gem that lists ignored files as untracked under some circumstances github.com/schacon/ruby-git/issues/130.
Constructor Details
#initialize(directory:, remote:) ⇒ Repository
Returns a new instance of Repository.
6 7 8 9 |
# File 'lib/modulesync/repository.rb', line 6 def initialize(directory:, remote:) @directory = directory @remote = remote end |
Instance Method Details
#checkout_branch(branch) ⇒ Object
123 124 125 126 127 |
# File 'lib/modulesync/repository.rb', line 123 def checkout_branch(branch) selected_branch = branch || repo.current_branch || 'master' repo.branch(selected_branch).checkout selected_branch end |
#clone ⇒ Object
73 74 75 76 |
# File 'lib/modulesync/repository.rb', line 73 def clone puts "Cloning from '#{@remote}'" @git = Git.clone(@remote, @directory) end |
#cloned? ⇒ Boolean
69 70 71 |
# File 'lib/modulesync/repository.rb', line 69 def cloned? Dir.exist? File.join(@directory, '.git') end |
#default_branch ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/modulesync/repository.rb', line 33 def default_branch # `Git.default_branch` requires ruby-git >= 1.17.0 return Git.default_branch(repo.dir) if Git.respond_to? :default_branch symbolic_ref = repo.branches.find { |b| b.full.include?('remotes/origin/HEAD') } return unless symbolic_ref %r{remotes/origin/HEAD\s+->\s+origin/(?<branch>.+?)$}.match(symbolic_ref.full)[:branch] end |
#default_reset_branch(branch) ⇒ Object
93 94 95 |
# File 'lib/modulesync/repository.rb', line 93 def default_reset_branch(branch) remote_branch_exists?(branch) ? branch : default_branch end |
#git ⇒ Object
11 12 13 |
# File 'lib/modulesync/repository.rb', line 11 def git @git ||= Git.open @directory end |
#local_branch_exists?(branch) ⇒ Boolean
24 25 26 |
# File 'lib/modulesync/repository.rb', line 24 def local_branch_exists?(branch) repo.branches.local.collect(&:name).include?(branch) end |
#prepare_workspace(branch:, operate_offline:) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/modulesync/repository.rb', line 78 def prepare_workspace(branch:, operate_offline:) if cloned? puts "Overriding any local changes to repository in '#{@directory}'" git.fetch 'origin', prune: true unless operate_offline git.reset_hard switch(branch: branch) git.pull('origin', branch) if !operate_offline && remote_branch_exists?(branch) else raise ModuleSync::Error, 'Unable to clone in offline mode.' if operate_offline clone switch(branch: branch) end end |
#push(branch:, remote_branch:, remote_name: 'origin') ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/modulesync/repository.rb', line 169 def push(branch:, remote_branch:, remote_name: 'origin') raise ModuleSync::Error, 'Repository must be locally available before trying to push' unless cloned? remote_url = git.remote(remote_name).url remote_branch ||= branch puts "Push branch '#{branch}' to '#{remote_url}' (#{remote_name}/#{remote_branch})" git.push(remote_name, "#{branch}:#{remote_branch}", force: true) end |
#puts(*args) ⇒ Object
207 208 209 |
# File 'lib/modulesync/repository.rb', line 207 def puts(*args) $stdout.puts(*args) if ModuleSync.[:verbose] end |
#remote_branch_differ?(local_branch, remote_branch) ⇒ Boolean
28 29 30 31 |
# File 'lib/modulesync/repository.rb', line 28 def remote_branch_differ?(local_branch, remote_branch) !remote_branch_exists?(remote_branch) || repo.diff("#{local_branch}..origin/#{remote_branch}").any? end |
#remote_branch_exists?(branch) ⇒ Boolean
20 21 22 |
# File 'lib/modulesync/repository.rb', line 20 def remote_branch_exists?(branch) repo.branches.remote.collect(&:name).include?(branch) end |
#repo ⇒ Object
This is an alias to minimize code alteration
16 17 18 |
# File 'lib/modulesync/repository.rb', line 16 def repo git end |
#reset_workspace(branch:, operate_offline:, source_branch: nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/modulesync/repository.rb', line 97 def reset_workspace(branch:, operate_offline:, source_branch: nil) raise if branch.nil? if cloned? source_branch ||= "origin/#{default_reset_branch branch}" puts "Hard-resetting any local changes to repository in '#{@directory}' from branch '#{source_branch}'" switch(branch: branch) git.fetch 'origin', prune: true unless operate_offline git.reset_hard source_branch git.clean(d: true, force: true) else raise ModuleSync::Error, 'Unable to clone in offline mode.' if operate_offline clone switch(branch: branch) end end |
#show_changes(options) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/modulesync/repository.rb', line 188 def show_changes() checkout_branch([:branch]) $stdout.puts 'Files changed:' repo.diff('HEAD', '--').each do |diff| $stdout.puts diff.patch end $stdout.puts 'Files added:' untracked_unignored_files.each_key do |file| $stdout.puts file end $stdout.puts "\n\n" $stdout.puts '--------------------------------' git.diff('HEAD', '--').any? || untracked_unignored_files.any? end |
#submit_changes(files, options) ⇒ Object
Git add/rm, git commit, git push
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/modulesync/repository.rb', line 130 def submit_changes(files, ) = [:message] branch = checkout_branch([:branch]) files.each do |file| if repo.status.deleted.include?(file) repo.remove(file) elsif File.exist? File.join(@directory, file) repo.add(file) end end begin opts_commit = {} opts_push = {} opts_commit = { amend: true } if [:amend] opts_push = { force: true } if [:force] if [:pre_commit_script] script = "#{File.dirname(File.dirname(__FILE__))}/../contrib/#{[:pre_commit_script]}" `#{script} #{@directory}` end repo.commit(, opts_commit) if [:remote_branch] if remote_branch_differ?(branch, [:remote_branch]) repo.push('origin', "#{branch}:#{[:remote_branch]}", opts_push) puts "Changes have been pushed to: '#{branch}:#{[:remote_branch]}'" end else repo.push('origin', branch, opts_push) puts "Changes have been pushed to: '#{branch}'" end rescue Git::GitExecuteError => e raise unless e..match?(/working (directory|tree) clean/) puts "There were no changes in '#{@directory}'. Not committing." return false end true end |
#switch(branch:) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/modulesync/repository.rb', line 43 def switch(branch:) unless branch branch = default_branch puts "Using repository's default branch: #{branch}" end return if repo.current_branch == branch if local_branch_exists?(branch) puts "Switching to branch #{branch}" repo.checkout(branch) elsif remote_branch_exists?(branch) puts "Creating local branch #{branch} from origin/#{branch}" repo.checkout("origin/#{branch}") repo.branch(branch).checkout else base_branch = default_branch unless base_branch puts "Couldn't detect default branch. Falling back to assuming 'master'" base_branch = 'master' end puts "Creating new branch #{branch} from #{base_branch}" repo.checkout("origin/#{base_branch}") repo.branch(branch).checkout end end |
#tag(version, tag_pattern) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/modulesync/repository.rb', line 116 def tag(version, tag_pattern) tag = tag_pattern % version puts "Tagging with #{tag}" repo.add_tag(tag) repo.push('origin', tag) end |
#untracked_unignored_files ⇒ Object
Needed because of a bug in the git gem that lists ignored files as untracked under some circumstances github.com/schacon/ruby-git/issues/130
182 183 184 185 186 |
# File 'lib/modulesync/repository.rb', line 182 def untracked_unignored_files ignore_path = File.join @directory, '.gitignore' ignored = File.exist?(ignore_path) ? File.read(ignore_path).split : [] repo.status.untracked.keep_if { |f, _| ignored.none? { |i| File.fnmatch(i, f) } } end |