Class: Gerrit::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/gerrit/repo.rb

Overview

Exposes information about the current git repository.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Repo

Returns a new instance of Repo.

Parameters:



7
8
9
# File 'lib/gerrit/repo.rb', line 7

def initialize(config)
  @config = config
end

Instance Method Details

#branch(ref = 'HEAD') ⇒ String?

Returns the name of the currently checked-out branch or the branch the specified ref is on.

Returns nil if it is detached.

Returns:

  • (String, nil)


17
18
19
20
21
# File 'lib/gerrit/repo.rb', line 17

def branch(ref = 'HEAD')
  name = `git branch`.split("\n").grep(/^\* /).first[/\w+/]
  # Check if detached head
  name.start_with?('(') ? nil : name
end

#git_dirString

Returns an absolute path to the .git directory for a repo.

Returns:

  • (String)


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

def git_dir
  @git_dir ||=
    begin
      git_dir = File.expand_path('.git', root)

      # .git could also be a file that contains the location of the git directory
      unless File.directory?(git_dir)
        git_dir = File.read(git_dir)[/^gitdir: (.*)$/, 1]

        # Resolve relative paths
        unless git_dir.start_with?('/')
          git_dir = File.expand_path(git_dir, repo_dir)
        end
      end

      git_dir
    end
end

#projectString

Returns the project name for this repo.

Uses the project name specified by the configuration, otherwise just uses the repo root directory.

Returns:

  • (String)


74
75
76
77
78
79
80
81
82
# File 'lib/gerrit/repo.rb', line 74

def project
  if url = remote_url
    File.basename(url[/\/[^\/]+$/], '.git')
  else
    # Otherwise just use the name of this repository
    File.basename(root)
  end
  #
end

#remote_urlObject

Returns the Gerrit remote URL for this repo.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gerrit/repo.rb', line 97

def remote_url
  unless push_remote = @config[:push_remote]
    raise Errors::ConfigurationInvalidError,
          'You must specify the `push_remote` option in your configuration.'
  end

  unless url = remotes[push_remote]
    raise Errors::ConfigurationInvalidError,
          "The '#{push_remote}' `push_remote` specified in your " \
          'configuration is not a remote in this repository. ' \
          'Have you run `gerrit setup`?'
  end

  url
end

#remotesHash

Returns all remotes this repository has configured.

Returns:

  • (Hash)

    hash of remote names mapping to their URLs



87
88
89
90
91
92
93
94
# File 'lib/gerrit/repo.rb', line 87

def remotes
  Hash[
    `git config --get-regexp '^remote\..+\.url$'`.split("\n").map do |line|
      match = line.match(/^remote\.(?<name>\S+)\.url\s+(?<url>.*)/)
      [match[:name], match[:url]]
    end
  ]
end

#rootString

Returns the absolute path to the root of the current repository the current working directory resides within.

Returns:

  • (String)

Raises:

  • (Gerrit::Errors::InvalidGitRepoError)

    if the current directory doesn’t reside within a git repository



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gerrit/repo.rb', line 29

def root
  @root ||=
    begin
      git_dir = Pathname.new(File.expand_path('.'))
                        .enum_for(:ascend)
                        .find do |path|
        (path + '.git').exist?
      end

      unless git_dir
        raise Errors::InvalidGitRepoError, 'no .git directory found'
      end

      git_dir.to_s
    end
end