Class: D2::Helpers::Git::LocalRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/d2/helpers/git/local_repo.rb

Constant Summary collapse

UndeterminedError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(fragment) ⇒ LocalRepo

Returns a new instance of LocalRepo



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

def initialize(fragment)
  @fragment = fragment.gsub(/\.git$/, '')
end

Instance Method Details

#org_or_userString

The organization or user that owns the repo Defaults to DEFAULT_ORG_OR_USER in the case none is specified

Returns:

  • (String)

    the organization or user



43
44
45
46
47
48
49
50
51
# File 'lib/d2/helpers/git/local_repo.rb', line 43

def org_or_user
  split_path = path.split('/', 2)
  case split_path.size
  when 1
    D2::Config.get('git', 'default_owner')
  when 2
    split_path.first
  end
end

#path_on_diskObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/d2/helpers/git/local_repo.rb', line 78

def path_on_disk
  File.expand_path(
    File.join(
      D2::Config.get('src_path', 'default'),
      provider,
      org_or_user,
      repo_name
    )
  )
end

#providerString

The provider for the url, aka the git server This will usually be one of: github.com, bitbucket.org, or gitlab.com Defaults to the default provider set in the config in the case none is specified

Returns:

  • (String)

    the provider (host) for the repo



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

def provider
  case fragement_type
  when TYPES[:ssh], TYPES[:https]
    parsed_uri.host
  when TYPES[:org_repo], TYPES[:repo]
    D2::Config.get('git', 'default_provider')
  when TYPES[:undetermined]
    raise UndeterminedError
  end
end

#repo_nameString

The repository name

Returns:

  • (String)

    the repo name



57
58
59
# File 'lib/d2/helpers/git/local_repo.rb', line 57

def repo_name
  path.split('/', 2).last
end

#url(type:) ⇒ String

Provides a full git URL in SSH or HTTPS format

Parameters:

  • type (String)

    (ssh or https) The type of the url required

Returns:

  • (String)

    url representing the repo in the requested format

Raises:

  • (ArgumentError)

    when type is not supported



67
68
69
70
71
72
73
74
75
76
# File 'lib/d2/helpers/git/local_repo.rb', line 67

def url(type:)
  case type
  when 'ssh'
    "git@#{provider}:#{org_or_user}/#{repo_name}.git"
  when 'https'
    "https://#{provider}/#{org_or_user}/#{repo_name}.git"
  else
    raise ArgumentError, "#{type} is not a supported type"
  end
end