Class: ModuleSync::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/modulesync/repository.rb

Overview

Wrapper for Git in ModuleSync context

Instance Method Summary collapse

Constructor Details

#initialize(directory:, remote:) ⇒ Repository

Returns a new instance of Repository.



8
9
10
11
# File 'lib/modulesync/repository.rb', line 8

def initialize(directory:, remote:)
  @directory = directory
  @remote = remote
end

Instance Method Details

#branch_behind?(branch, target_branch) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/modulesync/repository.rb', line 45

def branch_behind?(branch, target_branch)
  log = repo.log(1).between(branch, "origin/#{target_branch}")
  commits = log.respond_to?(:execute) ? log.execute : log
  commits.any?
end

#checkout_branch(branch) ⇒ Object



175
176
177
178
179
# File 'lib/modulesync/repository.rb', line 175

def checkout_branch(branch)
  selected_branch = branch || repo.current_branch || 'master'
  repo.branch(selected_branch).checkout
  selected_branch
end

#cloneObject



97
98
99
100
# File 'lib/modulesync/repository.rb', line 97

def clone
  puts "Cloning from '#{@remote}'"
  @git = Git.clone(@remote, @directory)
end

#cloned?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/modulesync/repository.rb', line 93

def cloned?
  Dir.exist? File.join(@directory, '.git')
end

#default_branchObject



51
52
53
54
55
56
57
58
59
# File 'lib/modulesync/repository.rb', line 51

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



145
146
147
# File 'lib/modulesync/repository.rb', line 145

def default_reset_branch(branch)
  remote_branch_exists?(branch) ? branch : default_branch
end

#gitObject



13
14
15
# File 'lib/modulesync/repository.rb', line 13

def git
  @git ||= Git.open @directory
end

#local_branch_exists?(branch) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/modulesync/repository.rb', line 26

def local_branch_exists?(branch)
  repo.branches.local.collect(&:name).include?(branch)
end

#prepare_workspace(branch:, operate_offline:, rebase: false) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/modulesync/repository.rb', line 102

def prepare_workspace(branch:, operate_offline:, rebase: false)
  @rebased = false
  if cloned?
    puts "Overriding any local changes to repository in '#{@directory}'"
    git.fetch 'origin', prune: true unless operate_offline
    git.reset_hard
    rebase_target = remote_default_branch if rebase && !operate_offline
    switch(branch: branch)
    git.pull('origin', branch) if !operate_offline && remote_branch_exists?(branch)
    rebase_onto(rebase_target) if rebase_target
  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

Raises:



221
222
223
224
225
226
227
228
229
# File 'lib/modulesync/repository.rb', line 221

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

#push_changes(branch, remote_branch, options) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/modulesync/repository.rb', line 135

def push_changes(branch, remote_branch, options)
  refspec = remote_branch ? "#{branch}:#{remote_branch}" : branch
  if @rebased && !options[:force]
    repo.lib.send(:command, 'push', '--force-with-lease', 'origin', refspec)
  else
    opts_push = options[:force] ? { force: true } : {}
    repo.push('origin', refspec, opts_push)
  end
end

#putsObject



259
260
261
# File 'lib/modulesync/repository.rb', line 259

def puts(*)
  $stdout.puts(*) if ModuleSync.options[:verbose]
end

#rebase_onto(branch) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/modulesync/repository.rb', line 120

def rebase_onto(branch)
  return false if repo.current_branch == branch || !branch_behind?(repo.current_branch, branch)

  puts "Rebasing #{repo.current_branch} onto origin/#{branch}"
  repo.lib.send(:command, 'rebase', "origin/#{branch}")
  @rebased = true
rescue Git::Error => e
  begin
    repo.lib.send(:command, 'rebase', '--abort')
  rescue Git::Error
    # Preserve the original rebase error if Git had no rebase to abort.
  end
  raise ModuleSync::Error, "Rebase onto origin/#{branch} failed and was aborted: #{e.message}"
end

#remote_branch_ahead?(source_branch, target_branch) ⇒ Boolean

This method checks if the source branch is ahead of the target branch in the remote repository. It does this by checking if there are any commits in the source branch that are not in the target branch.

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/modulesync/repository.rb', line 37

def remote_branch_ahead?(source_branch, target_branch)
  return false unless remote_branch_exists?(source_branch) && remote_branch_exists?(target_branch)

  log = repo.log(1).between("origin/#{target_branch}", "origin/#{source_branch}")
  commits = log.respond_to?(:execute) ? log.execute : log
  commits.any?
end

#remote_branch_differ?(local_branch, remote_branch) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/modulesync/repository.rb', line 30

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/modulesync/repository.rb', line 22

def remote_branch_exists?(branch)
  repo.branches.remote.collect(&:name).include?(branch)
end

#remote_default_branchObject



61
62
63
64
65
# File 'lib/modulesync/repository.rb', line 61

def remote_default_branch
  return Git.default_branch(@remote) if Git.respond_to? :default_branch

  default_branch
end

#repoObject

This is an alias to minimize code alteration



18
19
20
# File 'lib/modulesync/repository.rb', line 18

def repo
  git
end

#reset_workspace(branch:, operate_offline:, source_branch: nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/modulesync/repository.rb', line 149

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/modulesync/repository.rb', line 240

def show_changes(options)
  checkout_branch(options[: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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/modulesync/repository.rb', line 182

def submit_changes(files, options)
  message = options[:message]
  branch = checkout_branch(options[: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_commit = { amend: true } if options[:amend]
    if options[:pre_commit_script]
      script = "#{File.dirname(__FILE__, 3)}/contrib/#{options[:pre_commit_script]}"
      system(script, @directory)
    end
    if repo.status.changed.empty? && repo.status.added.empty? && repo.status.deleted.empty?
      puts "There were no changes in '#{@directory}'. Not committing."
      return false unless @rebased
    else
      repo.commit(message, opts_commit)
    end
    if options[:remote_branch]
      if @rebased || remote_branch_differ?(branch, options[:remote_branch])
        push_changes(branch, options[:remote_branch], options)
        puts "Changes have been pushed to: '#{branch}:#{options[:remote_branch]}'"
      end
    else
      push_changes(branch, nil, options)
      puts "Changes have been pushed to: '#{branch}'"
    end
  rescue Git::Error => e
    raise e.message
  end

  true
end

#switch(branch:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/modulesync/repository.rb', line 67

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



168
169
170
171
172
173
# File 'lib/modulesync/repository.rb', line 168

def tag(version, tag_pattern)
  tag = tag_pattern % version
  puts "Tagging with #{tag}"
  repo.add_tag(tag)
  repo.push('origin', tag)
end

#untracked_unignored_filesObject

Needed because of a bug in the git gem that lists ignored files as untracked under some circumstances https://github.com/schacon/ruby-git/issues/130



234
235
236
237
238
# File 'lib/modulesync/repository.rb', line 234

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