Module: Git::Helpers::Utils

Defined in:
lib/git/helpers/utils.rb

Overview

Provides utility methods for other Git-Helper modules

Class Method Summary collapse

Class Method Details

.remote?(repo, remote) ⇒ Boolean

Checks if the given remote exists on the given repo

Parameters:

  • repo (Git::Base)

    The repository in question

  • remote (String)

    The name of the remote to check for

Returns:

  • (Boolean)

    true if the remote exists, false if not



10
11
12
# File 'lib/git/helpers/utils.rb', line 10

def self.remote?(repo, remote)
  repo.remotes.map(&:name).include?(remote)
end

.remotes_hash(repo) ⇒ Hash{String => Git::Remote}

Get a hash of remotes keyed to their name

Parameters:

  • repo (Git::Base)

    Repository to get the remotes for

Returns:

  • (Hash{String => Git::Remote})

    A hash of remotes



17
18
19
# File 'lib/git/helpers/utils.rb', line 17

def self.remotes_hash(repo)
  Hash[repo.remotes.map { |r| [r.name, r] }]
end

.split_remote_string(str) ⇒ Array<String>

Splits a remote/branch string into its component parts

Parameters:

  • str (String)

    String to split

Returns:

  • (Array<String>)

    remote, branch



40
41
42
43
44
45
46
47
# File 'lib/git/helpers/utils.rb', line 40

def self.split_remote_string(str)
  first_slash = str.index('/')
  if first_slash.nil?
    return str, nil
  else
    return str[0..first_slash-1], str[first_slash+1..-1]
  end
end

.transform_url(url, scheme = 'https') ⇒ String

Takes a url from a git remote and transforms it into an normal url with

the desired scheme

Parameters:

  • url (String)

    Url to transform

  • scheme (String) (defaults to: 'https')

    What scheme the transformed Url will use, https (default), or http

Returns:

  • (String)

    A transformed Url



26
27
28
29
30
31
32
33
34
35
# File 'lib/git/helpers/utils.rb', line 26

def self.transform_url(url, scheme = 'https')
  # if git ssh url
  if /^git@/ === url
    url.gsub(/^git@(.*):/, scheme + '://\1/')
  elsif %r{^git://} === url
    url.gsub(/^git/, scheme)
  else
    url
  end
end

.true_name(remote) ⇒ String

Gets the true name for a remote

Parameters:

  • remote (Git::Remote)

    The remote to get the true name for

Returns:

  • (String)

    The true name of the remote, pulled from the url



52
53
54
55
# File 'lib/git/helpers/utils.rb', line 52

def self.true_name(remote)
  transform_url(remote.url) =~ %r{^https://.*?/(.*?)/.*$}
  Regexp.last_match 1
end