From 2f001ed4aa40cc13455831cf39799215845f3842 Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Wed, 9 Jun 2021 11:30:07 -0700 Subject: [PATCH 1/9] Adds action for obtaining remote repo default branch --- .../actions/git_default_remote_branch.rb | 51 +++++++++++++++++++ fastlane/lib/fastlane/helper/git_helper.rb | 11 ++++ 2 files changed, 62 insertions(+) create mode 100644 fastlane/lib/fastlane/actions/git_default_remote_branch.rb diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb new file mode 100644 index 00000000000..f8298605a0f --- /dev/null +++ b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb @@ -0,0 +1,51 @@ +module Fastlane + module Actions + class GitDefaultRemoteBranchAction < Action + def self.run(params) + Actions.git_default_remote_branch_name || "" + end + + ##################################################### + # @!group Documentation + ##################################################### + + def self.description + "Returns the name of the current git remote default branch" + end + + def self.details + "If no default remote branch could be found, this action will return an empty string. This is a wrapper for the internal action Actions.git_default_remote_branch_name" + end + + def self.available_options + [] + end + + def self.output + [] + end + + def self.authors + ["SeanMcNeil"] + end + + def self.is_supported?(platform) + true + end + + def self.example_code + [ + 'git_default_remote_branch_name' + ] + end + + def self.return_type + :string + end + + def self.category + :source_control + end + end + end +end diff --git a/fastlane/lib/fastlane/helper/git_helper.rb b/fastlane/lib/fastlane/helper/git_helper.rb index 5dea94cc9e6..abf4b595581 100644 --- a/fastlane/lib/fastlane/helper/git_helper.rb +++ b/fastlane/lib/fastlane/helper/git_helper.rb @@ -139,6 +139,17 @@ def self.git_branch_name_using_HEAD end end + # Returns the default git remote branch name + def self.git_default_remote_branch_name + # Rescues if not a git repo or no remote repo + begin + Actions.sh("variable=$(git remote) && git remote show $variable | grep \"HEAD branch\" | sed 's/.*: //'", log: false).chomp + rescue => err + UI.verbose("Error getting git default remote branch: #{err.message}") + nil + end + end + private_class_method def self.git_log_merge_commit_filtering_option(merge_commit_filtering) case merge_commit_filtering From 4599320a2ff0df90855514c35d4e3f21b84e0df2 Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Wed, 9 Jun 2021 12:01:22 -0700 Subject: [PATCH 2/9] Correct example name --- fastlane/lib/fastlane/actions/git_default_remote_branch.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb index f8298605a0f..abfcd75b46e 100644 --- a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb +++ b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb @@ -35,7 +35,7 @@ def self.is_supported?(platform) def self.example_code [ - 'git_default_remote_branch_name' + 'git_default_remote_branch' ] end From 958f71c92b8fcfabbced742aa38afb2e1dfe95ee Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Thu, 10 Jun 2021 07:18:02 -0700 Subject: [PATCH 3/9] Adds unit test --- .../fastlane/actions/git_default_remote_branch.rb | 4 +--- .../spec/actions_specs/git_default_remote_branch.rb | 13 +++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 fastlane/spec/actions_specs/git_default_remote_branch.rb diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb index abfcd75b46e..5f0f4516107 100644 --- a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb +++ b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb @@ -34,9 +34,7 @@ def self.is_supported?(platform) end def self.example_code - [ - 'git_default_remote_branch' - ] + ['git_default_remote_branch'] end def self.return_type diff --git a/fastlane/spec/actions_specs/git_default_remote_branch.rb b/fastlane/spec/actions_specs/git_default_remote_branch.rb new file mode 100644 index 00000000000..24dedd87fa9 --- /dev/null +++ b/fastlane/spec/actions_specs/git_default_remote_branch.rb @@ -0,0 +1,13 @@ +describe Fastlane do + describe Fastlane::FastFile do + describe "Git Default Remote Branch Action" do + it "generates the correct git command for retrieving default branch from remote" do + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("variable=$(git remote) && git remote show $variable | grep \"HEAD branch\" | sed 's/.*: //'") + end + end + end +end From fed038fe232b1f1fc0250a0dc418360d0dae9e2d Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Thu, 10 Jun 2021 13:39:36 -0700 Subject: [PATCH 4/9] Completed tests --- .../actions/git_default_remote_branch.rb | 4 +- fastlane/lib/fastlane/helper/git_helper.rb | 2 +- .../git_default_remote_branch.rb | 90 ++++++++++++++++++- 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb index 5f0f4516107..dde26a6fe9e 100644 --- a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb +++ b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb @@ -2,7 +2,7 @@ module Fastlane module Actions class GitDefaultRemoteBranchAction < Action def self.run(params) - Actions.git_default_remote_branch_name || "" + Actions.git_default_remote_branch_name || "No remote default available" end ##################################################### @@ -14,7 +14,7 @@ def self.description end def self.details - "If no default remote branch could be found, this action will return an empty string. This is a wrapper for the internal action Actions.git_default_remote_branch_name" + "If no default remote branch could be found, this action will return a message indicating the lack of a default remote branch. This is a wrapper for the internal action Actions.git_default_remote_branch_name" end def self.available_options diff --git a/fastlane/lib/fastlane/helper/git_helper.rb b/fastlane/lib/fastlane/helper/git_helper.rb index abf4b595581..02a938dfd9b 100644 --- a/fastlane/lib/fastlane/helper/git_helper.rb +++ b/fastlane/lib/fastlane/helper/git_helper.rb @@ -143,7 +143,7 @@ def self.git_branch_name_using_HEAD def self.git_default_remote_branch_name # Rescues if not a git repo or no remote repo begin - Actions.sh("variable=$(git remote) && git remote show $variable | grep \"HEAD branch\" | sed 's/.*: //'", log: false).chomp + Actions.sh("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp rescue => err UI.verbose("Error getting git default remote branch: #{err.message}") nil diff --git a/fastlane/spec/actions_specs/git_default_remote_branch.rb b/fastlane/spec/actions_specs/git_default_remote_branch.rb index 24dedd87fa9..a796fbdcdef 100644 --- a/fastlane/spec/actions_specs/git_default_remote_branch.rb +++ b/fastlane/spec/actions_specs/git_default_remote_branch.rb @@ -1,13 +1,101 @@ describe Fastlane do describe Fastlane::FastFile do + directory = "fl_spec_default_remote_branch" + repository = "https://github.com/seanmcneil/Empty.git" + describe "Git Default Remote Branch Action" do it "generates the correct git command for retrieving default branch from remote" do result = Fastlane::FastFile.new.parse("lane :test do git_default_remote_branch end").runner.execute(:test) - expect(result).to eq("variable=$(git remote) && git remote show $variable | grep \"HEAD branch\" | sed 's/.*: //'") + expect(result).to eq("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'") + end + end + + context "runs the command in a directory with no git repo" do + it "Confirms that no default remote is found" do + test_directory_path = Dir.mktmpdir(directory) + + Dir.chdir(test_directory_path) do + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("No remote default available") + end + end + end + + context "runs the command in a directory with no remote git repo" do + it "Confirms that no default remote is found" do + test_directory_path = Dir.mktmpdir(directory) + + Dir.chdir(test_directory_path) do + `git init` + + File.write('test_file', <<-TESTFILE) + 'Hello' + TESTFILE + `git add .` + `git commit --message "Test file"` + + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("No remote default available") + end end end + + context "runs the command in a directory with a remote git repo" do + it "Confirms that a default remote is found" do + test_directory_path = Dir.mktmpdir(directory) + + `git clone #{repository} #{test_directory_path}` + + Dir.chdir(test_directory_path) do + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + .and_return("main") + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("main") + end + end + end + + context "runs the command in a directory with a remote git repo on non-default branch" do + it "Confirms that a default remote is found when on non-default branch" do + test_directory_path = Dir.mktmpdir(directory) + + `git clone #{repository} #{test_directory_path}` + + Dir.chdir(test_directory_path) do + `git checkout -b other` + + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + .and_return("main") + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("main") + end + end + end + end end From f0155c999c881a35e47074338745dab5c9bad753 Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Fri, 11 Jun 2021 11:48:31 -0700 Subject: [PATCH 5/9] Uses nil return and some cleanup for remote work --- .../lib/fastlane/actions/git_default_remote_branch.rb | 2 +- .../spec/actions_specs/git_default_remote_branch.rb | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb index dde26a6fe9e..cccce91a0aa 100644 --- a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb +++ b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb @@ -2,7 +2,7 @@ module Fastlane module Actions class GitDefaultRemoteBranchAction < Action def self.run(params) - Actions.git_default_remote_branch_name || "No remote default available" + Actions.git_default_remote_branch_name end ##################################################### diff --git a/fastlane/spec/actions_specs/git_default_remote_branch.rb b/fastlane/spec/actions_specs/git_default_remote_branch.rb index a796fbdcdef..dd9d3587bc1 100644 --- a/fastlane/spec/actions_specs/git_default_remote_branch.rb +++ b/fastlane/spec/actions_specs/git_default_remote_branch.rb @@ -1,7 +1,6 @@ describe Fastlane do describe Fastlane::FastFile do directory = "fl_spec_default_remote_branch" - repository = "https://github.com/seanmcneil/Empty.git" describe "Git Default Remote Branch Action" do it "generates the correct git command for retrieving default branch from remote" do @@ -25,7 +24,7 @@ git_default_remote_branch end").runner.execute(:test) - expect(result).to eq("No remote default available") + expect(result).to be_nil end end end @@ -50,7 +49,7 @@ git_default_remote_branch end").runner.execute(:test) - expect(result).to eq("No remote default available") + expect(result).to be_nil end end end @@ -59,8 +58,6 @@ it "Confirms that a default remote is found" do test_directory_path = Dir.mktmpdir(directory) - `git clone #{repository} #{test_directory_path}` - Dir.chdir(test_directory_path) do expect(Fastlane::Actions).to receive(:sh) .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) @@ -79,11 +76,7 @@ it "Confirms that a default remote is found when on non-default branch" do test_directory_path = Dir.mktmpdir(directory) - `git clone #{repository} #{test_directory_path}` - Dir.chdir(test_directory_path) do - `git checkout -b other` - expect(Fastlane::Actions).to receive(:sh) .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) .and_return("main") From 825651e1ed1ad11aff0cd9f7a86b1594304ebe81 Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Sat, 12 Jun 2021 07:33:24 -0700 Subject: [PATCH 6/9] Optional remote name --- .../actions/git_default_remote_branch.rb | 14 ++- fastlane/lib/fastlane/helper/git_helper.rb | 15 ++- .../git_default_remote_branch_spec.rb | 104 ++++++++++++++++++ 3 files changed, 125 insertions(+), 8 deletions(-) create mode 100644 fastlane/spec/actions_specs/git_default_remote_branch_spec.rb diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb index cccce91a0aa..07c377af486 100644 --- a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb +++ b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb @@ -2,7 +2,7 @@ module Fastlane module Actions class GitDefaultRemoteBranchAction < Action def self.run(params) - Actions.git_default_remote_branch_name + Actions.git_default_remote_branch_name(params[:remote_name]) end ##################################################### @@ -18,7 +18,12 @@ def self.details end def self.available_options - [] + [ + FastlaneCore::ConfigItem.new(key: :remote_name, + env_name: "FL_REMOTE_REPOSITORY", + description: "The remote repository to check", + optional: true) + ] end def self.output @@ -34,7 +39,10 @@ def self.is_supported?(platform) end def self.example_code - ['git_default_remote_branch'] + [ + 'git_default_remote_branch', + 'git_default_remote_branch(remote_name:"upstream") # Provide a remote name' + ] end def self.return_type diff --git a/fastlane/lib/fastlane/helper/git_helper.rb b/fastlane/lib/fastlane/helper/git_helper.rb index 02a938dfd9b..2a22f9710b8 100644 --- a/fastlane/lib/fastlane/helper/git_helper.rb +++ b/fastlane/lib/fastlane/helper/git_helper.rb @@ -140,13 +140,18 @@ def self.git_branch_name_using_HEAD end # Returns the default git remote branch name - def self.git_default_remote_branch_name + def self.git_default_remote_branch_name(remote_name) # Rescues if not a git repo or no remote repo begin - Actions.sh("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp - rescue => err - UI.verbose("Error getting git default remote branch: #{err.message}") - nil + if remote_name + Actions.sh("git remote show #{remote_name} | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp + else + # Query git for the current remote head + Actions.sh("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp + end + rescue => err + UI.verbose("Error getting git default remote branch: #{err.message}") + nil end end diff --git a/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb b/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb new file mode 100644 index 00000000000..365e5e6e6cf --- /dev/null +++ b/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb @@ -0,0 +1,104 @@ +describe Fastlane do + describe Fastlane::FastFile do + directory = "fl_spec_default_remote_branch" + + describe "Git Default Remote Branch Action" do + it "generates the correct git command for retrieving default branch from remote" do + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'") + end + end + + describe "Git Default Remote Branch Action with optional remote name" do + it "generates the correct git command for retrieving default branch from provided remote name" do + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch(remote_name:'upstream') + end").runner.execute(:test) + + expect(result).to eq("git remote show upstream | grep 'HEAD branch' | sed 's/.*: //'") + end + end + + context "runs the command in a directory with no git repo" do + it "Confirms that no default remote is found" do + test_directory_path = Dir.mktmpdir(directory) + + Dir.chdir(test_directory_path) do + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to be_nil + end + end + end + + context "runs the command in a directory with no remote git repo" do + it "Confirms that no default remote is found" do + test_directory_path = Dir.mktmpdir(directory) + + Dir.chdir(test_directory_path) do + `git init` + + File.write('test_file', <<-TESTFILE) + 'Hello' + TESTFILE + `git add .` + `git commit --message "Test file"` + + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to be_nil + end + end + end + + context "runs the command in a directory with a remote git repo" do + it "Confirms that a default remote is found" do + test_directory_path = Dir.mktmpdir(directory) + + Dir.chdir(test_directory_path) do + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + .and_return("main") + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("main") + end + end + end + + context "runs the command in a directory with a remote git repo on non-default branch" do + it "Confirms that a default remote is found when on non-default branch" do + test_directory_path = Dir.mktmpdir(directory) + + Dir.chdir(test_directory_path) do + expect(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + .and_return("main") + + result = Fastlane::FastFile.new.parse("lane :test do + git_default_remote_branch + end").runner.execute(:test) + + expect(result).to eq("main") + end + end + end + + end +end From 1ab708a1da9beb822a98f55d1052792a06e590ad Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Sun, 13 Jun 2021 09:57:41 -0700 Subject: [PATCH 7/9] Cleanup and test updates --- .../actions/git_default_remote_branch.rb | 4 +- .../git_default_remote_branch.rb | 94 ------------------- .../git_default_remote_branch_spec.rb | 37 ++------ 3 files changed, 10 insertions(+), 125 deletions(-) delete mode 100644 fastlane/spec/actions_specs/git_default_remote_branch.rb diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb index 07c377af486..4814122c3e4 100644 --- a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb +++ b/fastlane/lib/fastlane/actions/git_default_remote_branch.rb @@ -14,13 +14,13 @@ def self.description end def self.details - "If no default remote branch could be found, this action will return a message indicating the lack of a default remote branch. This is a wrapper for the internal action Actions.git_default_remote_branch_name" + "If no default remote branch could be found, this action will return nil. This is a wrapper for the internal action Actions.git_default_remote_branch_name" end def self.available_options [ FastlaneCore::ConfigItem.new(key: :remote_name, - env_name: "FL_REMOTE_REPOSITORY", + env_name: "FL_REMOTE_REPOSITORY_NAME", description: "The remote repository to check", optional: true) ] diff --git a/fastlane/spec/actions_specs/git_default_remote_branch.rb b/fastlane/spec/actions_specs/git_default_remote_branch.rb deleted file mode 100644 index dd9d3587bc1..00000000000 --- a/fastlane/spec/actions_specs/git_default_remote_branch.rb +++ /dev/null @@ -1,94 +0,0 @@ -describe Fastlane do - describe Fastlane::FastFile do - directory = "fl_spec_default_remote_branch" - - describe "Git Default Remote Branch Action" do - it "generates the correct git command for retrieving default branch from remote" do - result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch - end").runner.execute(:test) - - expect(result).to eq("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'") - end - end - - context "runs the command in a directory with no git repo" do - it "Confirms that no default remote is found" do - test_directory_path = Dir.mktmpdir(directory) - - Dir.chdir(test_directory_path) do - expect(Fastlane::Actions).to receive(:sh) - .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) - - result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch - end").runner.execute(:test) - - expect(result).to be_nil - end - end - end - - context "runs the command in a directory with no remote git repo" do - it "Confirms that no default remote is found" do - test_directory_path = Dir.mktmpdir(directory) - - Dir.chdir(test_directory_path) do - `git init` - - File.write('test_file', <<-TESTFILE) - 'Hello' - TESTFILE - `git add .` - `git commit --message "Test file"` - - expect(Fastlane::Actions).to receive(:sh) - .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) - - result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch - end").runner.execute(:test) - - expect(result).to be_nil - end - end - end - - context "runs the command in a directory with a remote git repo" do - it "Confirms that a default remote is found" do - test_directory_path = Dir.mktmpdir(directory) - - Dir.chdir(test_directory_path) do - expect(Fastlane::Actions).to receive(:sh) - .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) - .and_return("main") - - result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch - end").runner.execute(:test) - - expect(result).to eq("main") - end - end - end - - context "runs the command in a directory with a remote git repo on non-default branch" do - it "Confirms that a default remote is found when on non-default branch" do - test_directory_path = Dir.mktmpdir(directory) - - Dir.chdir(test_directory_path) do - expect(Fastlane::Actions).to receive(:sh) - .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) - .and_return("main") - - result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch - end").runner.execute(:test) - - expect(result).to eq("main") - end - end - end - - end -end diff --git a/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb b/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb index 365e5e6e6cf..617e9a51649 100644 --- a/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb +++ b/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb @@ -13,7 +13,7 @@ end describe "Git Default Remote Branch Action with optional remote name" do - it "generates the correct git command for retrieving default branch from provided remote name" do + it "generates the correct git command for retrieving default branch using provided remote name" do result = Fastlane::FastFile.new.parse("lane :test do git_default_remote_branch(remote_name:'upstream') end").runner.execute(:test) @@ -64,39 +64,18 @@ end end - context "runs the command in a directory with a remote git repo" do + context "runs the command with a remote git repo" do it "Confirms that a default remote is found" do - test_directory_path = Dir.mktmpdir(directory) - - Dir.chdir(test_directory_path) do - expect(Fastlane::Actions).to receive(:sh) - .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) - .and_return("main") - - result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch - end").runner.execute(:test) - - expect(result).to eq("main") - end - end - end - - context "runs the command in a directory with a remote git repo on non-default branch" do - it "Confirms that a default remote is found when on non-default branch" do - test_directory_path = Dir.mktmpdir(directory) + allow(Fastlane::Actions).to receive(:sh) + .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) + .and_return("main") + allow(Fastlane::Actions).to receive(:git_branch).and_return(nil) - Dir.chdir(test_directory_path) do - expect(Fastlane::Actions).to receive(:sh) - .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) - .and_return("main") - - result = Fastlane::FastFile.new.parse("lane :test do + result = Fastlane::FastFile.new.parse("lane :test do git_default_remote_branch end").runner.execute(:test) - expect(result).to eq("main") - end + expect(result).to eq("main") end end From 32b11c58e6a019fb6f61bcff53f0877836c7d6d1 Mon Sep 17 00:00:00 2001 From: Sean McNeil Date: Mon, 14 Jun 2021 13:55:45 -0700 Subject: [PATCH 8/9] Removed default from name --- ...fault_remote_branch.rb => git_remote_branch.rb} | 8 ++++---- fastlane/lib/fastlane/helper/git_helper.rb | 2 +- ...te_branch_spec.rb => git_remote_branch_spec.rb} | 14 +++++++------- 3 files changed, 12 insertions(+), 12 deletions(-) rename fastlane/lib/fastlane/actions/{git_default_remote_branch.rb => git_remote_branch.rb} (82%) rename fastlane/spec/actions_specs/{git_default_remote_branch_spec.rb => git_remote_branch_spec.rb} (88%) diff --git a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb b/fastlane/lib/fastlane/actions/git_remote_branch.rb similarity index 82% rename from fastlane/lib/fastlane/actions/git_default_remote_branch.rb rename to fastlane/lib/fastlane/actions/git_remote_branch.rb index 4814122c3e4..d79c3879a50 100644 --- a/fastlane/lib/fastlane/actions/git_default_remote_branch.rb +++ b/fastlane/lib/fastlane/actions/git_remote_branch.rb @@ -1,8 +1,8 @@ module Fastlane module Actions - class GitDefaultRemoteBranchAction < Action + class GitRemoteBranchAction < Action def self.run(params) - Actions.git_default_remote_branch_name(params[:remote_name]) + Actions.git_remote_branch_name(params[:remote_name]) end ##################################################### @@ -40,8 +40,8 @@ def self.is_supported?(platform) def self.example_code [ - 'git_default_remote_branch', - 'git_default_remote_branch(remote_name:"upstream") # Provide a remote name' + 'git_remote_branch # Query git for first available remote name', + 'git_remote_branch(remote_name:"upstream") # Provide a remote name' ] end diff --git a/fastlane/lib/fastlane/helper/git_helper.rb b/fastlane/lib/fastlane/helper/git_helper.rb index 2a22f9710b8..e96663fa5c1 100644 --- a/fastlane/lib/fastlane/helper/git_helper.rb +++ b/fastlane/lib/fastlane/helper/git_helper.rb @@ -140,7 +140,7 @@ def self.git_branch_name_using_HEAD end # Returns the default git remote branch name - def self.git_default_remote_branch_name(remote_name) + def self.git_remote_branch_name(remote_name) # Rescues if not a git repo or no remote repo begin if remote_name diff --git a/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb b/fastlane/spec/actions_specs/git_remote_branch_spec.rb similarity index 88% rename from fastlane/spec/actions_specs/git_default_remote_branch_spec.rb rename to fastlane/spec/actions_specs/git_remote_branch_spec.rb index 617e9a51649..21dd6e56ab2 100644 --- a/fastlane/spec/actions_specs/git_default_remote_branch_spec.rb +++ b/fastlane/spec/actions_specs/git_remote_branch_spec.rb @@ -2,20 +2,20 @@ describe Fastlane::FastFile do directory = "fl_spec_default_remote_branch" - describe "Git Default Remote Branch Action" do + describe "Git Remote Branch Action" do it "generates the correct git command for retrieving default branch from remote" do result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch + git_remote_branch end").runner.execute(:test) expect(result).to eq("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'") end end - describe "Git Default Remote Branch Action with optional remote name" do + describe "Git Remote Branch Action with optional remote name" do it "generates the correct git command for retrieving default branch using provided remote name" do result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch(remote_name:'upstream') + git_remote_branch(remote_name:'upstream') end").runner.execute(:test) expect(result).to eq("git remote show upstream | grep 'HEAD branch' | sed 's/.*: //'") @@ -31,7 +31,7 @@ .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch + git_remote_branch end").runner.execute(:test) expect(result).to be_nil @@ -56,7 +56,7 @@ .with("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false) result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch + git_remote_branch end").runner.execute(:test) expect(result).to be_nil @@ -72,7 +72,7 @@ allow(Fastlane::Actions).to receive(:git_branch).and_return(nil) result = Fastlane::FastFile.new.parse("lane :test do - git_default_remote_branch + git_remote_branch end").runner.execute(:test) expect(result).to eq("main") From c84ba11eddc4ff92f9d232d3e46870fbe4947e9d Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 22 Jun 2021 21:37:22 -0500 Subject: [PATCH 9/9] Small syntax cleanup --- fastlane/lib/fastlane/helper/git_helper.rb | 28 ++++++++++------------ 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/fastlane/lib/fastlane/helper/git_helper.rb b/fastlane/lib/fastlane/helper/git_helper.rb index e96663fa5c1..c8b181dd988 100644 --- a/fastlane/lib/fastlane/helper/git_helper.rb +++ b/fastlane/lib/fastlane/helper/git_helper.rb @@ -131,28 +131,24 @@ def self.git_branch # Returns the checked out git branch name or "HEAD" if you're in detached HEAD state def self.git_branch_name_using_HEAD # Rescues if not a git repo or no commits in a git repo - begin - Actions.sh("git rev-parse --abbrev-ref HEAD", log: false).chomp - rescue => err - UI.verbose("Error getting git branch: #{err.message}") - nil - end + Actions.sh("git rev-parse --abbrev-ref HEAD", log: false).chomp + rescue => err + UI.verbose("Error getting git branch: #{err.message}") + nil end # Returns the default git remote branch name def self.git_remote_branch_name(remote_name) # Rescues if not a git repo or no remote repo - begin - if remote_name - Actions.sh("git remote show #{remote_name} | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp - else - # Query git for the current remote head - Actions.sh("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp - end - rescue => err - UI.verbose("Error getting git default remote branch: #{err.message}") - nil + if remote_name + Actions.sh("git remote show #{remote_name} | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp + else + # Query git for the current remote head + Actions.sh("variable=$(git remote) && git remote show $variable | grep 'HEAD branch' | sed 's/.*: //'", log: false).chomp end + rescue => err + UI.verbose("Error getting git default remote branch: #{err.message}") + nil end private_class_method