lib/match/git_helper.rb in match-0.5.0 vs lib/match/git_helper.rb in match-0.6.0
- old
+ new
@@ -1,8 +1,8 @@
module Match
class GitHelper
- def self.clone(git_url, shallow_clone, manual_password: nil, skip_docs: false)
+ def self.clone(git_url, shallow_clone, manual_password: nil, skip_docs: false, branch: "master")
return @dir if @dir
@dir = Dir.mktmpdir
command = "git clone '#{git_url}' '#{@dir}'"
command << " --depth 1" if shallow_clone
@@ -12,10 +12,12 @@
print_all: $verbose,
print_command: $verbose)
UI.user_error!("Error cloning repo, make sure you have access to it '#{git_url}'") unless File.directory?(@dir)
+ checkout_branch(branch) unless branch == "master"
+
if !Helper.test? and GitHelper.match_version(@dir).nil? and manual_password.nil? and File.exist?(File.join(@dir, "README.md"))
UI.important "Migrating to new match..."
ChangePassword.update(params: { git_url: git_url,
shallow_clone: shallow_clone },
from: "",
@@ -45,21 +47,21 @@
if File.exist?(path)
Gem::Version.new(File.read(path))
end
end
- def self.commit_changes(path, message, git_url)
+ def self.commit_changes(path, message, git_url, branch = "master")
Dir.chdir(path) do
return if `git status`.include?("nothing to commit")
Encrypt.new.encrypt_repo(path: path, git_url: git_url)
File.write("match_version.txt", Match::VERSION) # unencrypted
commands = []
commands << "git add -A"
- commands << "git commit -m '#{message}'"
- commands << "git push origin master"
+ commands << "git commit -m #{message.shellescape}"
+ commands << "git push origin #{branch.shellescape}"
UI.message "Pushing changes to remote git repo..."
commands.each do |command|
FastlaneCore::CommandExecutor.execute(command: command,
@@ -75,9 +77,47 @@
return unless @dir
FileUtils.rm_rf(@dir)
UI.success "🔒 Successfully encrypted certificates repo" # so the user is happy
@dir = nil
+ end
+
+ # Create and checkout an specific branch in the git repo
+ def self.checkout_branch(branch)
+ return unless @dir
+
+ commands = []
+ if branch_exists?(branch)
+ # Checkout the branch if it already exists
+ commands << "git checkout #{branch.shellescape}"
+ else
+ # If a new branch is being created, we create it as an 'orphan' to not inherit changes from the master branch.
+ commands << "git checkout --orphan #{branch.shellescape}"
+ # We also need to reset the working directory to not transfer any uncommitted changes to the new branch.
+ commands << "git reset --hard"
+ end
+
+ UI.message "Checking out branch #{branch}..."
+
+ Dir.chdir(@dir) do
+ commands.each do |command|
+ FastlaneCore::CommandExecutor.execute(command: command,
+ print_all: $verbose,
+ print_command: $verbose)
+ end
+ end
+ end
+
+ # Checks if a specific branch exists in the git repo
+ def self.branch_exists?(branch)
+ return unless @dir
+
+ result = Dir.chdir(@dir) do
+ FastlaneCore::CommandExecutor.execute(command: "git branch --list origin/#{branch.shellescape} --no-color -r",
+ print_all: $verbose,
+ print_command: $verbose)
+ end
+ return !result.empty?
end
# Copies the README.md into the git repo
def self.copy_readme(directory)
template = File.read("#{Helper.gem_path('match')}/lib/assets/READMETemplate.md")