-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
102657a
Honor a repository's default branch, which is automatically checked o…
asedge 1b2c947
Get the default branch (remotes/origin/HEAD) from the repo. Add some…
asedge ad29f4c
No matter what I tried, rubocop complained about this line. Disable …
asedge 038fb7c
Disable Metrics/ModuleLength on ModuleSync::Git.
asedge 88e8a28
Refactor to not modify the options hash. Extract the branch selection…
asedge 2fe4137
Make helper method private and improve documentation on CLI option.
asedge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -95,12 +105,19 @@ def self.tag(repo, version, tag_pattern) | |
repo.push('origin', tag) | ||
end | ||
|
||
def self.checkout_branch(repo, branch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name is quite similar to |
||
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) | ||
|
@@ -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") | ||
|
@@ -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| | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.