Class: Giturl::GitDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/giturl/git_directory.rb

Overview

A directory on disk, asked about through git: whether git manages it, and what GitHub page shows it. Every git invocation in giturl happens here.

Constant Summary collapse

FAILURE =
'Git commands failed. Please check if the directory is a git repository.'

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GitDirectory

Returns a new instance of GitDirectory.



12
13
14
# File 'lib/giturl/git_directory.rb', line 12

def initialize(path)
  @path = path
end

Instance Method Details

#managed?Boolean

git prints to stderr when the path is not a repository, which is noise for a question that is allowed to answer "no", so stderr is silenced around it.

Returns:

  • whether the directory is inside a git work tree



20
21
22
23
24
25
26
27
# File 'lib/giturl/git_directory.rb', line 20

def managed?
  stderr_old = $stderr.dup
  $stderr.reopen(File::NULL)
  inside, = capture('rev-parse', '--is-inside-work-tree')
  $stderr.flush
  $stderr.reopen stderr_old
  inside == 'true'
end

#urlString

Returns the GitHub page URL for this directory, or '' (after a warning) when git could not answer.

Returns:

  • the GitHub page URL for this directory, or '' (after a warning) when git could not answer



31
32
33
34
35
36
37
38
39
# File 'lib/giturl/git_directory.rb', line 31

def url
  here = location
  unless here
    warn FAILURE
    return ''
  end

  "#{RemoteUrl.new(here.remote).https}/tree/#{here.encoded_branch}/#{here.prefix}"
end