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

Class Method Details

.branch_exists?(branch_name) ⇒ Bool

Checks if a branch exists locally.

Parameters:

  • branch_name (String)

    The name of the branch to check for

Returns:

  • (Bool)

    True if the branch exists in the local working copy, false otherwise.



202
203
204
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 202

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.

Parameters:

  • branch (String, Hash)

    Name of the branch to pull. If you provide a Hash with a single key=>value pair, it will build the branch name as ‘“#key/#value”`, i.e. `checkout_and_pull(release: version)` is equivalent to `checkout_and_pull(“release/#version”)`.

Returns:

  • (Bool)

    True if it succeeded switching and pulling, false if there was an error during the switch or pull.



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')
  return true
rescue
  return false
end

.commit(message:, files: nil) ⇒ Bool

‘git add` the specified files (if any provided) then commit them using the provided message.

Parameters:

  • message (String)

    The commit message to use

  • files (String|Array<String>) (defaults to: nil)

    A file or array of files to git-add before creating the commit. Use ‘nil` or `[]` if you already added the files in a separate step and don’t wan’t this method to add any new file before commit. Also accepts the special symbol ‘:all` to add all the files (`git commit -a -m …`).

Returns:

  • (Bool)

    True if commit was successful, false if there was an issue (most likely being “nothing to commit”).



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 104

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', message)
    return true
  rescue
    return 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.

Parameters:

  • branch_name (String)

    The full name of the new branch to create, e.g “release/1.2”

  • from (String?) (defaults to: nil)

    The branch or tag from which to cut the branch from. If ‘nil`, will cut the new branch from the current commit. Otherwise, will checkout that commit/branch/tag before cutting the branch.



84
85
86
87
88
89
90
91
92
93
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 84

def self.create_branch(branch_name, from: nil)
  if branch_exists?(branch_name)
    UI.message("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.

Parameters:

  • version (String)

    The name of the tag to push, e.g. “1.2”

  • push (Bool) (defaults to: true)

    If true (the default), the tag will also be pushed to ‘origin`



134
135
136
137
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 134

def self.create_tag(version, push: true)
  Action.sh('git', 'tag', version)
  Action.sh('git', 'push', 'origin', version) if push
end

.current_git_branchString

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.

See docs.fastlane.tools/actions/git_branch/#git_branch

Returns:

  • (String)

    The current git branch, or “HEAD” if it’s not checked out to any branch



191
192
193
194
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 191

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.

Parameters:

  • tags_to_delete (Array<String>)

    The list of tags to delete

  • delete_on_remote (Bool) (defaults to: false)

    If true, will also delete the tag from the remote. Otherwise, it will only be deleted locally.



163
164
165
166
167
168
169
170
171
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 163

def self.delete_tags(tags_to_delete, delete_on_remote: false)
  g = Git.open(Dir.pwd)
  local_tag_names = g.tags.map(&:name)

  Array(tags_to_delete).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

Ensure that we are on the expected branch, and abort if not.

Parameters:

  • branch_name (String)

    The name of the branch we expect to be on

Raises:

  • (UserError)

    Raises a user_error! and interrupts the lane if we are not on the expected branch.



212
213
214
215
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 212

def self.ensure_on_branch!(branch_name)
  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_tagsObject

Fetch all the tags from the remote.



175
176
177
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 175

def self.fetch_all_tags
  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.

Parameters:

  • path (String)

    The path to inspect

Returns:

  • (Pathname)

    The first existing ancestor, or ‘path` itself if it exists



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).expand_path
  p = p.parent until p.exist? || p.root?
  return 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.

Parameters:

  • ref (String) (defaults to: 'HEAD')

    The git ref (commit, branch name, ‘HEAD’, …) to resolve as a SHA

Returns:

  • (String)

    The commit SHA of the ref



125
126
127
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 125

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

Returns:

  • (Bool)

    True if the current directory is a git working copy and 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

Parameters:

  • path (String) (defaults to: Dir.pwd)

    An optional path where to check if a Git repo exists.

Returns:

  • (Bool)

    True if the current directory is the root of a git repo (i.e. a local working copy) or a subdirectory of one.



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)
  return 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.

Parameters:

  • path (String)

    The path to check against ‘.gitignore`

Returns:

  • (Bool)

    True if the given path is ignored or outside a Git repository, false otherwise.



222
223
224
225
226
227
228
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 222

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

Parameters:

  • matching (String) (defaults to: '*')

    The pattern of the tag(s) to match and filter on; use “*” for wildcards. For example, ‘“1.2.*”` will match every tag starting with `“1.2.”`. Defaults to ’*‘ which lists all tags.

Returns:

  • (Array<String>)

    The list of local tags matching the pattern



154
155
156
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 154

def self.list_local_tags(matching: '*')
  Action.sh('git', 'tag', '--list', matching).split("\n")
end

.list_tags_on_current_commitArray<String>

Returns the list of tags that are pointing to the current commit (HEAD)

Returns:

  • (Array<String>)

    List of tags associated with the HEAD commit



143
144
145
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 143

def self.list_tags_on_current_commit
  Action.sh('git', 'tag', '--points-at', 'HEAD').split("\n")
end

.update_submodulesObject

Update every submodule in the current git repository



72
73
74
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/git_helper.rb', line 72

def self.update_submodules
  Action.sh('git', 'submodule', 'update', '--init', '--recursive')
end