class Git

A static wrapper class for git

Public Class Methods

current_branch() click to toggle source

Retrieves the name of the current branch.

@return [String]

Current branch.
# File lib/git.rb, line 57
def self.current_branch
        `git branch`.rstrip
end
get_filtered_message(commit, filter) click to toggle source

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

# File lib/git.rb, line 131
def self.get_filtered_message(commit, filter)
        `git log #{commit} -E --grep='#{filter}' --format=%b`
end
get_filtered_messages(from_commit, to_commit, filter) click to toggle source

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

# File lib/git.rb, line 125
def self.get_filtered_messages(from_commit, to_commit, filter)
        `git log #{from_commit}..#{to_commit} -E --grep='#{filter}' --format=%b`
end
get_tag_annotation(tag) click to toggle source

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

# File lib/git.rb, line 111
def self.get_tag_annotation(tag)
        test_tag tag
        `git tag --sort refname -l -n99 #{tag}`.rstrip
end
get_tag_date(tag) click to toggle source

Retrieves the author date of a tag

# File lib/git.rb, line 118
def self.get_tag_date(tag)
        test_tag tag
        `git log -1 --format=format:%ai #{tag}`
end
is_empty_repository?() click to toggle source

Determines if the repository in the current directory is empty.

# File lib/git.rb, line 48
def self.is_empty_repository?
        `git show HEAD > /dev/null 2>&1`
        $? != 0
end
is_git_repository?(dir = nil) click to toggle source

Determines whether the (current) directory is a git repository

@param [String] dir

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

@return [bool]

True if the directory is a Git repository, false if not.
# File lib/git.rb, line 40
def self.is_git_repository?(dir = nil)
        dir = Dir.pwd if dir.nil?
        system("git status > /dev/null 2>&1")
        $? == 0
end
is_installed?() click to toggle source

Determines whether Git is installed

@return [bool]

True if Git is installed, false if not.
# File lib/git.rb, line 24
def self.is_installed?
        begin
                `git --version`
                true
        rescue
                false
        end
end
launch_editor(file) click to toggle source

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

@see github.com/git/git/blob/master/editor.c

Git's editor.c on GitHub

@param [String] file

Filename to pass to the editor.

@return [int]

Exit code of the editor process, or false if no editor found.
# File lib/git.rb, line 73
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

Private Class Methods

tags() click to toggle source

Ensures lazy loading of the tag list to enable calling code to change the working directory first.

# File lib/git.rb, line 139
def self.tags
        @@tags = TagList.new unless @@tags
        @@tags
end
test_tag(tag) click to toggle source

Tests if the given tag exists and fails if it doesn't

# File lib/git.rb, line 145
def self.test_tag(tag)
        fail "Invalid tag: #{tag}" unless tags.list.include?(tag)
end