Module: Git

Defined in:
lib/git.rb,
lib/git/lib.rb,
lib/git/log.rb,
lib/git/base.rb,
lib/git/diff.rb,
lib/git/path.rb,
lib/git/index.rb,
lib/git/stash.rb,
lib/git/author.rb,
lib/git/branch.rb,
lib/git/config.rb,
lib/git/object.rb,
lib/git/remote.rb,
lib/git/status.rb,
lib/git/stashes.rb,
lib/git/version.rb,
lib/git/branches.rb,
lib/git/repository.rb,
lib/git/base/factory.rb,
lib/git/working_directory.rb

Overview

Git/Ruby Library

This provides bindings for working with git in complex interactions, including branching and merging, object inspection and manipulation, history, patch generation and more. You should be able to do most fundamental git operations with this library.

This module provides the basic functions to open a git reference to work with. You can open a working directory, open a bare repository, initialize a new repo or clone an existing remote repository.

Author

Scott Chacon ([email protected])

License

MIT License

Defined Under Namespace

Classes: Author, Base, Branch, Branches, Config, Diff, GitExecuteError, GitTagNameDoesNotExist, Index, Lib, Log, Object, Path, Remote, Repository, Stash, Stashes, Status, WorkingDirectory

Constant Summary collapse

VERSION =

The current gem version

Returns:

  • (String)

    the current gem version.

'1.2.9'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bare(git_dir, options = {}) ⇒ Object

open a bare repository

this takes the path to a bare git repo it expects not to be able to use a working directory so you can’t checkout stuff, commit things, etc. but you can do most read operations



82
83
84
# File 'lib/git.rb', line 82

def self.bare(git_dir, options = {})
  Base.bare(git_dir, options)
end

.clone(repository, name, options = {}) ⇒ Object

clones a remote repository

options

:bare => true (does a bare clone)
:repository => '/path/to/alt_git_dir'
:index => '/path/to/alt_index_file'

example

Git.clone('git://repo.or.cz/rubygit.git', 'clone.git', :bare => true)


96
97
98
# File 'lib/git.rb', line 96

def self.clone(repository, name, options = {})
  Base.clone(repository, name, options)
end

.configObject



68
69
70
# File 'lib/git.rb', line 68

def self.config
  return Base.config
end

.configure {|Base.config| ... } ⇒ Object

Yields:



64
65
66
# File 'lib/git.rb', line 64

def self.configure
  yield Base.config
end

.export(repository, name, options = {}) ⇒ Object

Export the current HEAD (or a branch, if options[:branch] is specified) into the name directory, then remove all traces of git from the directory.

See clone for options. Does not obey the :remote option, since the .git info will be deleted anyway; always uses the default remote, ‘origin.’



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

def self.export(repository, name, options = {})
  options.delete(:remote)
  repo = clone(repository, name, {:depth => 1}.merge(options))
  repo.checkout("origin/#{options[:branch]}") if options[:branch]
  Dir.chdir(repo.dir.to_s) { FileUtils.rm_r '.git' }
end

.global_config(name = nil, value = nil) ⇒ Object

Same as g.config, but forces it to be at the global level

g.config(‘user.name’, ‘Scott Chacon’) # sets value g.config(‘user.email’, ‘[email protected]’) # sets value g.config(‘user.name’) # returns ‘Scott Chacon’ g.config # returns whole config hash



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/git.rb', line 120

def self.global_config(name = nil, value = nil)
  lib = Git::Lib.new(nil, nil)
  if(name && value)
    # set value
    lib.global_config_set(name, value)
  elsif (name)
    # return value
    lib.global_config_get(name)
  else
    # return hash
    lib.global_config_list
  end
end

.init(working_dir = '.', options = {}) ⇒ Object

initialize a new git repository, defaults to the current working directory

options

:repository => '/path/to/alt_git_dir'
:index => '/path/to/alt_index_file'


139
140
141
# File 'lib/git.rb', line 139

def self.init(working_dir = '.', options = {})
  Base.init(working_dir, options)
end

.ls_remote(location = nil) ⇒ {String=>Hash}

returns a Hash containing information about the references of the target repository

Parameters:

  • location (String|NilClass) (defaults to: nil)

    the target repository location or nil for ‘.’

Returns:

  • ({String=>Hash})

    the available references of the target repo.



148
149
150
# File 'lib/git.rb', line 148

def self.ls_remote(location=nil)
  Git::Lib.new.ls_remote(location)
end

.open(working_dir, options = {}) ⇒ Object

open an existing git working directory

this will most likely be the most common way to create a git reference, referring to a working directory. if not provided in the options, the library will assume your git_dir and index are in the default place (.git/, .git/index)

options

:repository => '/path/to/alt_git_dir'
:index => '/path/to/alt_index_file'


162
163
164
# File 'lib/git.rb', line 162

def self.open(working_dir, options = {})
  Base.open(working_dir, options)
end

Instance Method Details

#config(name = nil, value = nil) ⇒ Object

g.config(‘user.name’, ‘Scott Chacon’) # sets value g.config(‘user.email’, ‘[email protected]’) # sets value g.config(‘user.name’) # returns ‘Scott Chacon’ g.config # returns whole config hash



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git.rb', line 50

def config(name = nil, value = nil)
  lib = Git::Lib.new
  if(name && value)
    # set value
    lib.config_set(name, value)
  elsif (name)
    # return value
    lib.config_get(name)
  else
    # return hash
    lib.config_list
  end
end

#global_config(name = nil, value = nil) ⇒ Object



72
73
74
# File 'lib/git.rb', line 72

def global_config(name = nil, value = nil)
  self.class.global_config(name, value)
end