Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor repo default branch #142

Merged
merged 6 commits into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes you use origin as a remote. I always use upstream as a remote so I'd appreciate if that was overridable somehow but I do see that assumption is already present in a few places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it is hard coded in a number of places. Shall we handle it in this PR or a future one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's pre-existing code I'd handle it in a separate PR.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is quite similar to switch_branch. Maybe this should be a private method to clearly indicate it's a helper?

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