Module: Licensed::Git

Defined in:
lib/licensed/git.rb

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns whether git commands are available

Returns:

  • (Boolean)


5
6
7
# File 'lib/licensed/git.rb', line 5

def self.available?
  @git ||= Licensed::Shell.tool_available?("git")
end

.commit_date(sha) ⇒ Object

Returns the commit date for the provided SHA as a timestamp

sha - commit sha to retrieve date



35
36
37
38
# File 'lib/licensed/git.rb', line 35

def self.commit_date(sha)
  return unless git_repo? && sha
  Licensed::Shell.execute("git", "show", "-s", "-1", "--format=%ct", sha)
end

.filesObject

Returns the files in the git repository from ‘git ls-files –recurse-submodules`



41
42
43
44
45
# File 'lib/licensed/git.rb', line 41

def self.files
  return unless git_repo?
  output = Licensed::Shell.execute("git", "ls-files", "--full-name", "--recurse-submodules")
  output.lines.map(&:strip)
end

.git_repo?Boolean

Returns true if a git repository is found, false otherwise

Returns:

  • (Boolean)


19
20
21
# File 'lib/licensed/git.rb', line 19

def self.git_repo?
  !repository_root.to_s.empty?
end

.repository_rootObject

Returns the root of the current git repository or nil if not in a git repository.



11
12
13
14
15
16
# File 'lib/licensed/git.rb', line 11

def self.repository_root
  return unless available?
  root = Licensed::Shell.execute("git", "rev-parse", "--show-toplevel", allow_failure: true)
  return nil if root.empty?
  root
end

.version(descriptor) ⇒ Object

Returns the most recent git SHA for a file or directory or nil if SHA is not available

descriptor - file or directory to retrieve latest SHA for



27
28
29
30
# File 'lib/licensed/git.rb', line 27

def self.version(descriptor)
  return unless git_repo? && descriptor
  Licensed::Shell.execute("git", "rev-list", "-1", "HEAD", "--", descriptor, allow_failure: true)
end