Module: GitOperations

Defined in:
lib/spm_version_updates/git_operations.rb

Overview

Git operations for SPM version checking (migrated from git.rb)

Defined Under Namespace

Classes: LsRemoteError

Constant Summary collapse

ALLOWED_PROTOCOLS =
"https:ssh:git"
LS_REMOTE_RETRY_DELAYS =
[0.25, 0.5].freeze
NON_INTERACTIVE_ENV =
{
  "GIT_ALLOW_PROTOCOL" => ALLOWED_PROTOCOLS,
  "GIT_TERMINAL_PROMPT" => "0"
}.freeze
TAG_REF_PATTERNS =
["[0-9]*.[0-9]*", "v[0-9]*.[0-9]*"].freeze

Class Method Summary collapse

Class Method Details

.branch_last_commit(repo_url, branch_name) ⇒ String?

Call git to find the last commit on a branch

Parameters:

  • repo_url (String)

    The URL of the dependency’s repository

  • branch_name (String)

    The name of the branch on which to find the last commit

Returns:

  • (String, nil)


72
73
74
75
76
77
78
79
80
# File 'lib/spm_version_updates/git_operations.rb', line 72

def self.branch_last_commit(repo_url, branch_name)
  branch_ref = "refs/heads/#{branch_name}"
  output = ls_remote(repo_url, options: ["--branches"], patterns: [branch_ref])

  line = output
    .split("\n")
    .find { |remote_ref| remote_ref.split("\t")[1] == branch_ref }
  line&.split("\t")&.first
end

.host(repo_url) ⇒ String?

Extracts the hostname from common git remote URL forms.

Returns:

  • (String, nil)


44
45
46
# File 'lib/spm_version_updates/git_operations.rb', line 44

def self.host(repo_url)
  GitHostNormalizer.host(repo_url)
end

.repo_name(repo_url) ⇒ String

Extract a readable name for the repo given the url, generally org/repo

Returns:

  • (String)


33
34
35
36
37
38
39
40
# File 'lib/spm_version_updates/git_operations.rb', line 33

def self.repo_name(repo_url)
  match = repo_url.match(%r{([\w-]+/[\w-]+)(.git)?$})
  if match
    match[1] || match[0]
  else
    repo_url
  end
end

.trim_repo_url(repo_url) ⇒ String

Removes protocol and trailing .git from a repo URL

Parameters:

  • repo_url (String)

    The URL of the repository

Returns:

  • (String)


24
25
26
27
28
29
# File 'lib/spm_version_updates/git_operations.rb', line 24

def self.trim_repo_url(repo_url)
  url = repo_url.to_s.strip
  return "" if url.empty?

  url.split("://").last.gsub(/\.git$/, "")
end

.version_tags(repo_url) ⇒ Array<SpmVersionUpdates::Semver>

Call git to list tags

Parameters:

  • repo_url (String)

    The URL of the dependency’s repository

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spm_version_updates/git_operations.rb', line 51

def self.version_tags(repo_url)
  output = ls_remote(repo_url, options: ["--tags", "--refs"], patterns: TAG_REF_PATTERNS)

  versions = output
    .split("\n")
    .filter_map { |line| tag_name(line) }
    .filter_map { |line|
      begin
        SpmVersionUpdates::Semver.new(line)
      rescue ArgumentError
        nil
      end
    }
  versions.sort!.reverse!
  versions
end