Class: Gerrit::Repo
- Inherits:
-
Object
- Object
- Gerrit::Repo
- Defined in:
- lib/gerrit/repo.rb
Overview
Exposes information about the current git repository.
Instance Method Summary collapse
-
#branch(ref = 'HEAD') ⇒ String?
Returns the name of the currently checked-out branch or the branch the specified ref is on.
-
#git_dir ⇒ String
Returns an absolute path to the .git directory for a repo.
-
#initialize(config) ⇒ Repo
constructor
A new instance of Repo.
-
#project ⇒ String
Returns the project name for this repo.
-
#remote_url ⇒ Object
Returns the Gerrit remote URL for this repo.
-
#remotes ⇒ Hash
Returns all remotes this repository has configured.
-
#root ⇒ String
Returns the absolute path to the root of the current repository the current working directory resides within.
Constructor Details
#initialize(config) ⇒ Repo
Returns a new instance of Repo.
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.
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_dir ⇒ String
Returns an absolute path to the .git directory for a repo.
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.('.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.(git_dir, repo_dir) end end git_dir end end |
#project ⇒ String
Returns the project name for this repo.
Uses the project name specified by the configuration, otherwise just uses the repo root directory.
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_url ⇒ Object
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 |
#remotes ⇒ Hash
Returns all remotes this repository has configured.
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 |
#root ⇒ String
Returns the absolute path to the root of the current repository the current working directory resides within.
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.('.')) .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 |