Class: Git::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/tag_changelog/git/git.rb

Constant Summary collapse

@@tags =
nil

Class Method Summary collapse

Class Method Details

.current_branchString

Retrieves the name of the current branch.

Returns:

  • (String)

    Current branch.



54
55
56
# File 'lib/tag_changelog/git/git.rb', line 54

def self.current_branch
  `git name-rev --name-only HEAD`.strip
end

.get_filtered_message(commit, filter) ⇒ Object

Retrieves one commit message and filters it Todo: Armor this against code injection!



132
133
134
# File 'lib/tag_changelog/git/git.rb', line 132

def self.get_filtered_message(commit, filter)
  `git log #{commit} -E --grep='#{filter}' --format=%b`
end

.get_filtered_messages(from_commit, to_commit, filter = nil) ⇒ Object

Retrieves commit messages and filters them Todo: Armor this against code injection!



122
123
124
125
126
127
128
# File 'lib/tag_changelog/git/git.rb', line 122

def self.get_filtered_messages(from_commit, to_commit, filter = nil)
  if filter
    `git log #{from_commit}..#{to_commit} -E --grep='#{filter}' --format=%b`
  else
    `git log #{from_commit}..#{to_commit} --oneline`
  end
end

.get_tag_annotation(tag) ⇒ Object

Retrieves the first 99 lines of the annotation of a tag.



108
109
110
111
# File 'lib/tag_changelog/git/git.rb', line 108

def self.get_tag_annotation(tag)
  test_tag tag
  `git tag --sort refname -l -n99 #{tag}`.rstrip
end

.get_tag_date(tag) ⇒ Object

Retrieves the author date of a tag



115
116
117
118
# File 'lib/tag_changelog/git/git.rb', line 115

def self.get_tag_date(tag)
  test_tag tag
  `git log -1 --format=format:%ai #{tag}`
end

.is_empty_repository?Boolean

Determines if the repository in the current directory is empty.

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/tag_changelog/git/git.rb', line 45

def self.is_empty_repository?
  `git show HEAD > /dev/null 2>&1`
  $? != 0
end

.is_git_repository?(dir = nil) ⇒ bool

Determines whether the (current) directory is a git repository

Parameters:

  • dir (String) (defaults to: nil)

    Directory to check; if nil, uses the current directory.

Returns:

  • (bool)

    True if the directory is a Git repository, false if not.



37
38
39
40
41
# File 'lib/tag_changelog/git/git.rb', line 37

def self.is_git_repository?(dir = nil)
  dir = Dir.pwd if dir.nil?
  system("git status > /dev/null 2>&1")
  $? == 0
end

.is_installed?bool

Determines whether Git is installed

Returns:

  • (bool)

    True if Git is installed, false if not.



25
26
27
28
# File 'lib/tag_changelog/git/git.rb', line 25

def self.is_installed?
  `git --version`
  $? == 0
end

.launch_editor(file) ⇒ int

Launches the text editor that Git uses for commit messages, and passes file as a command line argument to it.

Parameters:

  • file (String)

    Filename to pass to the editor.

Returns:

  • (int)

    Exit code of the editor process, or false if no editor found.

See Also:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tag_changelog/git/git.rb', line 70

def self.launch_editor(file)
  # const char *editor = getenv("GIT_EDITOR");
  editor = ENV['GIT_EDITOR']

  # const char *terminal = getenv("TERM");
  terminal = ENV['TERM'];

  # int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
  terminal_is_dumb = !terminal || terminal == 'dumb'

  # if (!editor && editor_program)
  editor = `git config --get core.editor`.rstrip if editor.nil? || editor.empty?

  # if (!editor && !terminal_is_dumb)
  #   editor = getenv("VISUAL");
  editor = ENV['VISUAL'] if (editor.nil? || editor.empty?) && !terminal_is_dumb

  # if (!editor)
  #   editor = getenv("EDITOR");
  editor = ENV['EDITOR'] if (editor.nil? || editor.empty?)

  # if (!editor && terminal_is_dumb)
  #   return NULL;
  # if (!editor)
  #   editor = DEFAULT_EDITOR;
  # Use vi, Git's hard-coded default
  editor = 'vi' if (editor.nil? || editor.empty?) && !terminal_is_dumb

  if editor && !editor.empty?
    system "#{editor} '#{file}'"
    $?
  else
    false
  end
end