Module: ModuleSync::Git

Includes:
Constants
Defined in:
lib/modulesync/git.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary

Constants included from Constants

Constants::CONF_FILE, Constants::GLOBAL_DEFAULTS_KEY, Constants::HOOK_FILE, Constants::MODULESYNC_CONF_FILE, Constants::MODULE_CONF_FILE, Constants::MODULE_FILES_DIR

Class Method Summary collapse

Class Method Details

.bump(repo, m, message, module_root, changelog = false) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/modulesync/git.rb', line 91

def self.bump(repo, m, message, module_root, changelog = false)
  new = m.bump!
  puts "Bumped to version #{new}"
  repo.add('metadata.json')
  update_changelog(repo, new, message, module_root) if changelog
  repo.commit("Release version #{new}")
  repo.push
  new
end

.default_branch(repo) ⇒ Object



21
22
23
24
25
# File 'lib/modulesync/git.rb', line 21

def self.default_branch(repo)
  symbolic_ref = repo.branches.find { |b| b.full =~ %r{remotes/origin/HEAD} }
  return unless symbolic_ref
  %r{remotes/origin/HEAD\s+->\s+origin/(?<branch>.+?)$}.match(symbolic_ref.full)[:branch]
end

.local_branch_exists?(repo, branch) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.pull(git_base, name, branch, project_root, opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/modulesync/git.rb', line 48

def self.pull(git_base, name, branch, project_root, opts)
  Dir.mkdir(project_root) unless Dir.exist?(project_root)

  # Repo needs to be cloned in the cwd
  if !Dir.exist?("#{project_root}/#{name}") || !Dir.exist?("#{project_root}/#{name}/.git")
    puts 'Cloning repository fresh'
    remote = opts[:remote] || (git_base.start_with?('file://') ? "#{git_base}/#{name}" : "#{git_base}/#{name}.git")
    local = "#{project_root}/#{name}"
    puts "Cloning from #{remote}"
    repo = ::Git.clone(remote, local)
    switch_branch(repo, branch)
  # Repo already cloned, check out master and override local changes
  else
    # Some versions of git can't properly handle managing a repo from outside the repo directory
    Dir.chdir("#{project_root}/#{name}") do
      puts "Overriding any local changes to repositories in #{project_root}"
      repo = ::Git.open('.')
      repo.fetch
      repo.reset_hard
      switch_branch(repo, branch)
      repo.pull('origin', branch) if remote_branch_exists?(repo, branch)
    end
  end
end

.remote_branch_differ?(repo, local_branch, remote_branch) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/modulesync/git.rb', line 16

def self.remote_branch_differ?(repo, local_branch, remote_branch)
  !remote_branch_exists?(repo, remote_branch) ||
    repo.diff("#{local_branch}..origin/#{remote_branch}").any?
end

.remote_branch_exists?(repo, branch) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.switch_branch(repo, branch) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/modulesync/git.rb', line 27

def self.switch_branch(repo, branch)
  unless branch
    branch = default_branch(repo)
    puts "Using repository's default branch: #{branch}"
  end
  return if repo.current_branch == branch

  if local_branch_exists?(repo, branch)
    puts "Switching to branch #{branch}"
    repo.checkout(branch)
  elsif remote_branch_exists?(repo, branch)
    puts "Creating local branch #{branch} from origin/#{branch}"
    repo.checkout("origin/#{branch}")
    repo.branch(branch).checkout
  else
    repo.checkout('origin/master')
    puts "Creating new branch #{branch}"
    repo.branch(branch).checkout
  end
end

.tag(repo, version, tag_pattern) ⇒ Object



101
102
103
104
105
106
# File 'lib/modulesync/git.rb', line 101

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

.untracked_unignored_files(repo) ⇒ 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



164
165
166
167
168
# File 'lib/modulesync/git.rb', line 164

def self.untracked_unignored_files(repo)
  ignore_path = "#{repo.dir.path}/.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

.update(name, files, options) ⇒ Object

Git add/rm, git commit, git push



116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
# File 'lib/modulesync/git.rb', line 116

def self.update(name, files, options)
  module_root = "#{options[:project_root]}/#{name}"
  message = options[:message]
  repo = ::Git.open(module_root)
  branch = checkout_branch(repo, options[:branch])
  files.each do |file|
    if repo.status.deleted.include?(file)
      repo.remove(file)
    elsif File.exist?("#{module_root}/#{file}")
      repo.add(file)
    end
  end
  begin
    opts_commit = {}
    opts_push = {}
    opts_commit = { :amend => true } if options[:amend]
    opts_push = { :force => true } if options[:force]
    if options[:pre_commit_script]
      script = "#{File.dirname(File.dirname(__FILE__))}/../contrib/#{options[:pre_commit_script]}"
      `#{script} #{module_root}`
    end
    repo.commit(message, opts_commit)
    if options[:remote_branch]
      if remote_branch_differ?(repo, branch, options[:remote_branch])
        repo.push('origin', "#{branch}:#{options[:remote_branch]}", opts_push)
      end
    else
      repo.push('origin', branch, opts_push)
    end
    # Only bump/tag if pushing didn't fail (i.e. there were changes)
    m = Blacksmith::Modulefile.new("#{module_root}/metadata.json")
    if options[:bump]
      new = bump(repo, m, message, module_root, options[:changelog])
      tag(repo, new, options[:tag_pattern]) if options[:tag]
    end
  rescue ::Git::GitExecuteError => git_error
    if git_error.message =~ /working (directory|tree) clean/
      puts "There were no files to update in #{name}. Not committing."
    else
      puts git_error
      raise
    end
  end
end

.update_changelog(repo, version, message, module_root) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/modulesync/git.rb', line 73

def self.update_changelog(repo, version, message, module_root)
  changelog = "#{module_root}/CHANGELOG.md"
  if File.exist?(changelog)
    puts "Updating #{changelog} for version #{version}"
    changes = File.readlines(changelog)
    File.open(changelog, 'w') do |f|
      date = Time.now.strftime('%Y-%m-%d')
      f.puts "## #{date} - Release #{version}\n\n"
      f.puts "#{message}\n\n"
      # Add old lines again
      f.puts changes
    end
    repo.add('CHANGELOG.md')
  else
    puts 'No CHANGELOG.md file found, not updating.'
  end
end

.update_noop(name, options) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/modulesync/git.rb', line 170

def self.update_noop(name, options)
  puts "Using no-op. Files in #{name} may be changed but will not be committed."

  repo = ::Git.open("#{options[:project_root]}/#{name}")
  checkout_branch(repo, options[:branch])

  puts 'Files changed:'
  repo.diff('HEAD', '--').each do |diff|
    puts diff.patch
  end

  puts 'Files added:'
  untracked_unignored_files(repo).each_key do |file|
    puts file
  end

  puts "\n\n"
  puts '--------------------------------'
end