Module: Fastlane::Helper::GitHelper
- Defined in:
- lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb
Overview
Helper methods to execute git-related operations
Constant Summary collapse
- DEFAULT_GIT_BRANCH =
Fallback default branch of the client repository.
'trunk'.freeze
Class Method Summary collapse
-
.branch_exists?(branch_name) ⇒ Bool
Checks if a branch exists locally.
-
.checkout_and_pull(branch) ⇒ Bool
Switch to the given branch and pull its latest commits.
-
.commit(message:, files: nil) ⇒ Bool
‘git add` the specified files (if any provided) then commit them using the provided message.
-
.create_branch(branch_name, from: nil) ⇒ Object
Create a new branch named ‘branch_name`, cutting it from branch/commit/tag `from`.
-
.create_tag(version, push: true) ⇒ Object
Creates a tag for the given version, and optionally push it to the remote.
-
.current_git_branch ⇒ String
Returns the current git branch, or “HEAD” if it’s not checked out to any branch Can NOT be replaced using the environment variables such as ‘GIT_BRANCH` or `BUILDKITE_BRANCH`.
-
.delete_tags(tags_to_delete, delete_on_remote: false) ⇒ Object
Delete the mentioned local tags in the local working copy, and optionally delete them on the remote too.
-
.ensure_on_branch!(branch_name) ⇒ Object
deprecated
Deprecated.
This method is going to be removed soon. Fastlane has a built-in ‘ensure_git_branch` action that can be used instead. After updating to Fastlane version `2.217.0` or later, the `FL_GIT_BRANCH_DONT_USE_ENV_VARS` environment variable can be set to `true` to disable the use of environment variables for the `git_branch` action that `ensure_git_branch` relies on. This will make `ensure_git_branch` work as expected in CI environments. See github.com/fastlane/fastlane/pull/21597 for more details.
-
.fetch_all_tags ⇒ Object
Fetch all the tags from the remote.
-
.first_existing_ancestor_of(path:) ⇒ Pathname
Travels back the hierarchy of the given path until it finds an existing ancestor, or it reaches the root of the file system.
-
.get_commit_sha(ref: 'HEAD') ⇒ String
Get the SHA of a given git ref.
-
.has_git_lfs? ⇒ Bool
Check if the current directory has git-lfs enabled.
-
.is_git_repo?(path: Dir.pwd) ⇒ Bool
Checks if the given path, or current directory if no path is given, is inside a Git repository.
-
.is_ignored?(path:) ⇒ Bool
Checks whether a given path is ignored by Git, relying on Git’s ‘check-ignore` under the hood.
-
.list_local_tags(matching: '*') ⇒ Array<String>
List all the tags in the local working copy, optionally filtering the list using a pattern.
-
.list_tags_on_current_commit ⇒ Array<String>
Returns the list of tags that are pointing to the current commit (HEAD).
-
.update_submodules ⇒ Object
deprecated
Deprecated.
This method is going to be removed soon. Fastlane has a built-in ‘git_submodule_update` action that can be used instead.
Class Method Details
.branch_exists?(branch_name) ⇒ Bool
Checks if a branch exists locally.
205 206 207 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 205 def self.branch_exists?(branch_name) !Action.sh('git', 'branch', '--list', branch_name).empty? end |
.checkout_and_pull(branch) ⇒ Bool
Switch to the given branch and pull its latest commits.
61 62 63 64 65 66 67 68 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 61 def self.checkout_and_pull(branch) branch = branch.first.join('/') if branch.is_a?(Hash) Action.sh('git', 'checkout', branch) Action.sh('git', 'pull') true rescue StandardError false end |
.commit(message:, files: nil) ⇒ Bool
‘git add` the specified files (if any provided) then commit them using the provided message.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 107 def self.commit(message:, files: nil) files = [files] if files.is_a?(String) args = [] if files == :all args = ['-a'] elsif !files.nil? && !files.empty? Action.sh('git', 'add', *files) end begin Action.sh('git', 'commit', *args, '-m', ) true rescue StandardError false end end |
.create_branch(branch_name, from: nil) ⇒ Object
Create a new branch named ‘branch_name`, cutting it from branch/commit/tag `from`
If the branch with that name already exists, it will instead switch to it and pull new commits.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 87 def self.create_branch(branch_name, from: nil) if branch_exists?(branch_name) UI.("Branch #{branch_name} already exists. Skipping creation.") Action.sh('git', 'checkout', branch_name) Action.sh('git', 'pull', 'origin', branch_name) else Action.sh('git', 'checkout', from) unless from.nil? Action.sh('git', 'checkout', '-b', branch_name) end end |
.create_tag(version, push: true) ⇒ Object
Creates a tag for the given version, and optionally push it to the remote.
137 138 139 140 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 137 def self.create_tag(version, push: true) Action.sh('git', 'tag', version) Action.sh('git', 'push', 'origin', version) if push end |
.current_git_branch ⇒ String
Returns the current git branch, or “HEAD” if it’s not checked out to any branch Can NOT be replaced using the environment variables such as ‘GIT_BRANCH` or `BUILDKITE_BRANCH`
‘fastlane` already has a helper action for this called `git_branch`, however it’s modified by CI environment variables. We need to check which branch we are actually on and not the initial branch a CI build is started from, so we are using the ‘git_branch_name_using_HEAD` helper instead.
194 195 196 197 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 194 def self.current_git_branch # We can't use `other_action.git_branch`, because it is modified by environment variables in Buildkite. Fastlane::Actions.git_branch_name_using_HEAD end |
.delete_tags(tags_to_delete, delete_on_remote: false) ⇒ Object
Delete the mentioned local tags in the local working copy, and optionally delete them on the remote too.
166 167 168 169 170 171 172 173 174 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 166 def self.(, delete_on_remote: false) g = Git.open(Dir.pwd) local_tag_names = g..map(&:name) Array().each do |tag| g.delete_tag(tag) if local_tag_names.include?(tag) g.push('origin', ":refs/tags/#{tag}") if delete_on_remote end end |
.ensure_on_branch!(branch_name) ⇒ Object
This method is going to be removed soon. Fastlane has a built-in ‘ensure_git_branch` action that can be used instead. After updating to Fastlane version `2.217.0` or later, the `FL_GIT_BRANCH_DONT_USE_ENV_VARS` environment variable can be set to `true` to disable the use of environment variables for the `git_branch` action that `ensure_git_branch` relies on. This will make `ensure_git_branch` work as expected in CI environments. See github.com/fastlane/fastlane/pull/21597 for more details.
Ensure that we are on the expected branch, and abort if not.
221 222 223 224 225 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 221 def self.ensure_on_branch!(branch_name) UI.deprecated("The `ensure_on_branch!` method will soon be removed from `release-toolkit`. Please use fastlane's `ensure_git_branch` action (+ potentially set `FL_GIT_BRANCH_DONT_USE_ENV_VARS=true`) instead.") current_branch_name = Action.sh('git', 'symbolic-ref', '-q', 'HEAD') UI.user_error!("This command works only on #{branch_name} branch") unless current_branch_name.include?(branch_name) end |
.fetch_all_tags ⇒ Object
Fetch all the tags from the remote.
178 179 180 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 178 def self. Action.sh('git', 'fetch', '--tags') end |
.first_existing_ancestor_of(path:) ⇒ Pathname
Travels back the hierarchy of the given path until it finds an existing ancestor, or it reaches the root of the file system.
37 38 39 40 41 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 37 def self.first_existing_ancestor_of(path:) p = Pathname(path). p = p.parent until p.exist? || p.root? p end |
.get_commit_sha(ref: 'HEAD') ⇒ String
Get the SHA of a given git ref. Typically useful to get the SHA of the current HEAD commit.
128 129 130 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 128 def self.get_commit_sha(ref: 'HEAD') Git.open(Dir.pwd).revparse(ref) end |
.has_git_lfs? ⇒ Bool
Check if the current directory has git-lfs enabled
47 48 49 50 51 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 47 def self.has_git_lfs? return false unless is_git_repo? !`git config --get-regex lfs`.empty? end |
.is_git_repo?(path: Dir.pwd) ⇒ Bool
Checks if the given path, or current directory if no path is given, is inside a Git repository
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 17 def self.is_git_repo?(path: Dir.pwd) # If the path doesn't exist, find its first ancestor. path = first_existing_ancestor_of(path: path) # Get the path's directory, so we can look in it for the Git folder dir = path.directory? ? path : path.dirname # Recursively look for the Git folder until it's found or we read the the file system root dir = dir.parent until Dir.entries(dir).include?('.git') || dir.root? # If we reached the root, we haven't found a repo. # (Technically, there could be a repo in the root of the system, but that's a usecase that we don't need to support at this time) dir.root? == false end |
.is_ignored?(path:) ⇒ Bool
Checks whether a given path is ignored by Git, relying on Git’s ‘check-ignore` under the hood.
232 233 234 235 236 237 238 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 232 def self.is_ignored?(path:) return true unless is_git_repo?(path: path) Actions.sh('git', 'check-ignore', path) do |status, _, _| status.success? end end |
.list_local_tags(matching: '*') ⇒ Array<String>
List all the tags in the local working copy, optionally filtering the list using a pattern
157 158 159 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 157 def self.(matching: '*') Action.sh('git', 'tag', '--list', matching).split("\n") end |
.list_tags_on_current_commit ⇒ Array<String>
Returns the list of tags that are pointing to the current commit (HEAD)
146 147 148 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 146 def self. Action.sh('git', 'tag', '--points-at', 'HEAD').split("\n") end |
.update_submodules ⇒ Object
This method is going to be removed soon. Fastlane has a built-in ‘git_submodule_update` action that can be used instead.
Update every submodule in the current git repository
74 75 76 77 |
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 74 def self.update_submodules UI.deprecated("The `update_submodules` method will soon be removed from `release-toolkit`. Please use fastlane's `git_submodule_update` action instead.") Action.sh('git', 'submodule', 'update', '--init', '--recursive') end |