Module: Git

Defined in:
lib/spm_version_updates/git.rb

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)


50
51
52
53
54
55
# File 'lib/spm_version_updates/git.rb', line 50

def self.branch_last_commit(repo_url, branch_name)
  `git ls-remote -h #{repo_url}`
    .split("\n")
    .find { |line| line.split("\trefs/heads/")[1] == branch_name }
    .split("\trefs/heads/")[0]
end

.repo_name(repo_url) ⇒ String

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

Returns:

  • (String)


14
15
16
17
18
19
20
21
22
# File 'lib/spm_version_updates/git.rb', line 14

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)


8
9
10
# File 'lib/spm_version_updates/git.rb', line 8

def self.trim_repo_url(repo_url)
  repo_url.split("://").last.gsub(/\.git$/, "")
end

.version_tags(repo_url) ⇒ Array<Semantic::Version>

Call git to list tags

Parameters:

  • repo_url (String)

    The URL of the dependency’s repository

Returns:

  • (Array<Semantic::Version>)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spm_version_updates/git.rb', line 28

def self.version_tags(repo_url)
  versions = `git ls-remote -t #{repo_url}`
    .split("\n")
    .map { |line| line.split("/tags/").last }
    .filter_map { |line|
      begin
        Semantic::Version.new(line)
      rescue ArgumentError
        nil
      end
    }
  versions.sort!
  versions.reverse!
  versions
end