Skip to content

Commit

Permalink
Merge pull request #142 from asedge/honor_repo_default_branch
Browse files Browse the repository at this point in the history
Honor repo default branch
  • Loading branch information
ekohl authored Feb 25, 2018
2 parents eeeffb7 + 2fe4137 commit cc935f9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
23 changes: 23 additions & 0 deletions features/step_definitions/git_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@
git_base: file://#{expand_path('.')}/
CONFIG
end

Given /a remote module repository with "(.+?)" as the default branch/ do |branch| # rubocop:disable Lint/AmbiguousRegexpLiteral
steps %(
Given a directory named "sources"
And I run `git clone --mirror https://github.com/maestrodev/puppet-test sources/puppet-test`
And a file named "managed_modules.yml" with:
"""
---
- puppet-test
"""
)
write_file('modulesync.yml', <<-CONFIG)
---
namespace: sources
git_base: file://#{expand_path('.')}/
CONFIG
cd('sources/puppet-test') do
steps %(
And I run `git branch -M master #{branch}`
And I run `git symbolic-ref HEAD refs/heads/#{branch}`
)
end
end
18 changes: 18 additions & 0 deletions features/update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -885,3 +885,21 @@ Feature: update
Then the exit status should be 0
Then the output should not contain "error"
Then the output should not contain "rejected"

Scenario: Repository with a default branch other than master
Given a mocked git configuration
And a remote module repository with "develop" as the default branch
And a file named "config_defaults.yml" with:
"""
---
Gemfile:
gem_source: https://somehost.com
"""
And a directory named "moduleroot"
And a file named "moduleroot/Gemfile.erb" with:
"""
source '<%= @configs['gem_source'] %>'
"""
When I run `msync update -m "Update Gemfile"`
Then the exit status should be 0
Then the output should contain "Using repository's default branch: develop"
4 changes: 2 additions & 2 deletions lib/modulesync/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class Base < Thor
:desc => 'A regular expression to skip repositories.'
class_option :branch,
:aliases => '-b',
:desc => 'Branch name to make the changes in. Defaults to master.',
:default => CLI.defaults[:branch] || 'master'
:desc => 'Branch name to make the changes in. Defaults to the default branch of the upstream repository, but falls back to "master".',
:default => CLI.defaults[:branch]

desc 'update', 'Update the modules in managed_modules.yml'
option :message,
Expand Down
29 changes: 23 additions & 6 deletions lib/modulesync/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'puppet_blacksmith'

module ModuleSync
module Git
module Git # rubocop:disable Metrics/ModuleLength
include Constants

def self.remote_branch_exists?(repo, branch)
Expand All @@ -18,7 +18,17 @@ def self.remote_branch_differ?(repo, local_branch, remote_branch)
repo.diff("#{local_branch}..origin/#{remote_branch}").any?
end

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

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)
Expand Down Expand Up @@ -95,12 +105,19 @@ def self.tag(repo, version, tag_pattern)
repo.push('origin', tag)
end

def self.checkout_branch(repo, branch)
selected_branch = branch || repo.current_branch || 'master'
repo.branch(selected_branch).checkout
selected_branch
end
private_class_method :checkout_branch

# Git add/rm, git commit, git push
def self.update(name, files, options)
module_root = "#{options[:project_root]}/#{name}"
message = options[:message]
repo = ::Git.open(module_root)
repo.branch(options[:branch]).checkout
branch = checkout_branch(repo, options[:branch])
files.each do |file|
if repo.status.deleted.include?(file)
repo.remove(file)
Expand All @@ -119,11 +136,11 @@ def self.update(name, files, options)
end
repo.commit(message, opts_commit)
if options[:remote_branch]
if remote_branch_differ?(repo, options[:branch], options[:remote_branch])
repo.push('origin', "#{options[:branch]}:#{options[:remote_branch]}", opts_push)
if remote_branch_differ?(repo, branch, options[:remote_branch])
repo.push('origin', "#{branch}:#{options[:remote_branch]}", opts_push)
end
else
repo.push('origin', options[:branch], opts_push)
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")
Expand Down Expand Up @@ -154,7 +171,7 @@ 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}")
repo.branch(options[:branch]).checkout
checkout_branch(repo, options[:branch])

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

0 comments on commit cc935f9

Please sign in to comment.