opens a bare Git Repository - no working directory options
# File lib/git/base.rb, line 6 6: def self.bare(git_dir, opts = {}) 7: self.new({:repository => git_dir}.merge(opts)) 8: end
clones a git repository locally
repository - http://repo.or.cz/w/sinatra.git name - sinatra
options:
:repository :bare or :working_directory :index_file
# File lib/git/base.rb, line 49 49: def self.clone(repository, name, opts = {}) 50: # run git-clone 51: self.new(Git::Lib.new.clone(repository, name, opts)) 52: end
initializes a git repository
options:
:repository :index_file
# File lib/git/base.rb, line 22 22: def self.init(working_dir, opts = {}) 23: opts = { 24: :working_directory => working_dir, 25: :repository => File.join(working_dir, '.git') 26: }.merge(opts) 27: 28: FileUtils.mkdir_p(opts[:working_directory]) if opts[:working_directory] && !File.directory?(opts[:working_directory]) 29: 30: # run git_init there 31: Git::Lib.new(opts).init 32: 33: self.new(opts) 34: end
# File lib/git/base.rb, line 54 54: def initialize(options = {}) 55: if working_dir = options[:working_directory] 56: options[:repository] ||= File.join(working_dir, '.git') 57: options[:index] ||= File.join(working_dir, '.git', 'index') 58: end 59: if options[:log] 60: @logger = options[:log] 61: @logger.info("Starting Git") 62: else 63: @logger = nil 64: end 65: 66: @working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil 67: @repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil 68: @index = options[:index] ? Git::Index.new(options[:index], false) : nil 69: end
opens a new Git Project from a working directory you can specify non-standard git_dir and index file in the options
# File lib/git/base.rb, line 12 12: def self.open(working_dir, opts={}) 13: self.new({:working_directory => working_dir}.merge(opts)) 14: end
adds files from the working directory to the git repository
# File lib/git/base.rb, line 247 247: def add(path = '.') 248: self.lib.add(path) 249: end
adds a new remote to this repository url can be a git url or a Git::Base object if it’s a local reference
@git.add_remote('scotts_git', 'git://repo.or.cz/rubygit.git') @git.fetch('scotts_git') @git.merge('scotts_git/master')
# File lib/git/base.rb, line 340 340: def add_remote(name, url, opts = {}) 341: url = url.repo.path if url.is_a?(Git::Base) 342: self.lib.remote_add(name, url, opts) 343: Git::Remote.new(self, name) 344: end
creates a new git tag (Git::Tag)
# File lib/git/base.rb, line 357 357: def add_tag(tag_name) 358: self.lib.tag(tag_name) 359: tag(tag_name) 360: end
# File lib/git/base.rb, line 376 376: def apply(file) 377: if File.exists?(file) 378: self.lib.apply(file) 379: end 380: end
# File lib/git/base.rb, line 382 382: def apply_mail(file) 383: self.lib.apply_mail(file) if File.exists?(file) 384: end
creates an archive file of the given tree-ish
# File lib/git/base.rb, line 363 363: def archive(treeish, file = nil, opts = {}) 364: self.object(treeish).archive(file, opts) 365: end
returns a Git::Branch object for branch_name
# File lib/git/base.rb, line 184 184: def branch(branch_name = 'master') 185: Git::Branch.new(self, branch_name) 186: end
returns a Git::Branches object of all the Git::Branch objects for this repo
# File lib/git/base.rb, line 179 179: def branches 180: Git::Branches.new(self) 181: end
# File lib/git/base.rb, line 467 467: def cat_file(objectish) 468: self.lib.object_contents(objectish) 469: end
changes current working directory for a block to the git working directory
example
@git.chdir do # write files @git.add @git.commit('message') end
# File lib/git/base.rb, line 110 110: def chdir # :yields: the Git::Path 111: Dir.chdir(dir.path) do 112: yield dir.path 113: end 114: end
checks out a branch as the new git working directory
# File lib/git/base.rb, line 286 286: def checkout(branch = 'master', opts = {}) 287: self.lib.checkout(branch, opts) 288: end
checks out an old version of a file
# File lib/git/base.rb, line 291 291: def checkout_file(version, file) 292: self.lib.checkout_file(version,file) 293: end
# File lib/git/base.rb, line 403 403: def checkout_index(opts = {}) 404: self.lib.checkout_index(opts) 405: end
commits all pending changes in the index file to the git repository
options:
:add_all :allow_empty :author
# File lib/git/base.rb, line 273 273: def commit(message, opts = {}) 274: self.lib.commit(message, opts) 275: end
commits all pending changes in the index file to the git repository, but automatically adds all modified files without having to explicitly calling @git.add() on them.
# File lib/git/base.rb, line 280 280: def commit_all(message, opts = {}) 281: opts = {:add_all => true}.merge(opts) 282: self.lib.commit(message, opts) 283: end
# File lib/git/base.rb, line 415 415: def commit_tree(tree = nil, opts = {}) 416: Git::Object::Commit.new(self, self.lib.commit_tree(tree, opts)) 417: end
g.config(‘user.name’, ‘Scott Chacon’) # sets value g.config(‘user.email’, ‘email@email.com’) # sets value g.config(‘user.name’) # returns ‘Scott Chacon’ g.config # returns whole config hash
# File lib/git/base.rb, line 129 129: def config(name = nil, value = nil) 130: if(name && value) 131: # set value 132: lib.config_set(name, value) 133: elsif (name) 134: # return value 135: lib.config_get(name) 136: else 137: # return hash 138: lib.config_list 139: end 140: end
returns the name of the branch the working directory is currently on
# File lib/git/base.rb, line 472 472: def current_branch 473: self.lib.branch_current 474: end
returns a Git::Diff object
# File lib/git/base.rb, line 242 242: def diff(objectish = 'HEAD', obj2 = nil) 243: Git::Diff.new(self, objectish, obj2) 244: end
returns a reference to the working directory
@git.dir.path @git.dir.writeable?
# File lib/git/base.rb, line 75 75: def dir 76: @working_directory 77: end
iterates over the files which are unmerged
# File lib/git/base.rb, line 318 318: def each_conflict(&block) # :yields: file, your_version, their_version 319: self.lib.conflicts(&block) 320: end
fetches changes from a remote branch - this does not modify the working directory, it just gets the changes from the remote if there are any
# File lib/git/base.rb, line 297 297: def fetch(remote = 'origin') 298: self.lib.fetch(remote) 299: end
# File lib/git/base.rb, line 164 164: def gblob(objectish) 165: Git::Object.new(self, objectish, 'blob') 166: end
# File lib/git/base.rb, line 160 160: def gcommit(objectish) 161: Git::Object.new(self, objectish, 'commit') 162: end
will run a grep for ‘string’ on the HEAD of the git repository
to be more surgical in your grep, you can call grep() off a specific git object. for example:
@git.object("v2.3").grep('TODO')
in any case, it returns a hash of arrays of the type:
hsh[tree-ish] = [[line_no, match], [line_no, match2]] hsh[tree-ish] = [[line_no, match], [line_no, match2]]
so you might use it like this:
@git.grep("TODO").each do |sha, arr| puts "in blob #{sha}:" arr.each do |match| puts "\t line #{match[0]}: '#{match[1]}'" end end
# File lib/git/base.rb, line 237 237: def grep(string, path_limiter = nil, opts = {}) 238: self.object('HEAD').grep(string, path_limiter, opts) 239: end
# File lib/git/base.rb, line 156 156: def gtree(objectish) 157: Git::Object.new(self, objectish, 'tree') 158: end
returns reference to the git index file
# File lib/git/base.rb, line 86 86: def index 87: @index 88: end
returns true if the branch exists
# File lib/git/base.rb, line 201 201: def is_branch?(branch) 202: branch_names = self.branches.map {|b| b.name} 203: branch_names.include?(branch) 204: end
returns true if the branch exists locally
# File lib/git/base.rb, line 189 189: def is_local_branch?(branch) 190: branch_names = self.branches.local.map {|b| b.name} 191: branch_names.include?(branch) 192: end
returns true if the branch exists remotely
# File lib/git/base.rb, line 195 195: def is_remote_branch?(branch) 196: branch_names = self.branches.local.map {|b| b.name} 197: branch_names.include?(branch) 198: end
this is a convenience method for accessing the class that wraps all the actual ‘git’ forked system calls. At some point I hope to replace the Git::Lib class with one that uses native methods or libgit C bindings
# File lib/git/base.rb, line 214 214: def lib 215: @lib ||= Git::Lib.new(self, @logger) 216: end
returns a Git::Log object with count commits
# File lib/git/base.rb, line 169 169: def log(count = 30) 170: Git::Log.new(self, count) 171: end
# File lib/git/base.rb, line 429 429: def ls_files(location=nil) 430: self.lib.ls_files(location) 431: end
# File lib/git/base.rb, line 463 463: def ls_tree(objectish) 464: self.lib.ls_tree(objectish) 465: end
merges one or more branches into the current working branch
you can specify more than one branch to merge by passing an array of branches
# File lib/git/base.rb, line 313 313: def merge(branch, message = 'merge') 314: self.lib.merge(branch, message) 315: end
returns a Git::Object of the appropriate type you can also call @git.gtree(‘tree’), but that’s just for readability. If you call @git.gtree(‘HEAD’) it will still return a Git::Object::Commit object.
@git.object calls a factory method that will run a rev-parse on the objectish and determine the type of the object and return an appropriate object for that type
# File lib/git/base.rb, line 152 152: def object(objectish) 153: Git::Object.new(self, objectish) 154: end
fetches a branch from a remote and merges it into the current working branch
# File lib/git/base.rb, line 323 323: def pull(remote = 'origin', branch = 'master', message = 'origin pull') 324: fetch(remote) 325: merge(branch, message) 326: end
pushes changes to a remote repository - easiest if this is a cloned repository, otherwise you may have to run something like this first to setup the push parameters:
@git.config('remote.remote-name.push', 'refs/heads/master:refs/heads/master')
# File lib/git/base.rb, line 306 306: def push(remote = 'origin', branch = 'master', tags = false) 307: self.lib.push(remote, branch, tags) 308: end
# File lib/git/base.rb, line 407 407: def read_tree(treeish, opts = {}) 408: self.lib.read_tree(treeish, opts) 409: end
returns a Git::Remote object
# File lib/git/base.rb, line 207 207: def remote(remote_name = 'origin') 208: Git::Remote.new(self, remote_name) 209: end
returns an array of Git:Remote objects
# File lib/git/base.rb, line 329 329: def remotes 330: self.lib.remotes.map { |r| Git::Remote.new(self, r) } 331: end
removes file(s) from the git repository
# File lib/git/base.rb, line 252 252: def remove(path = '.', opts = {}) 253: self.lib.remove(path, opts) 254: end
repacks the repository
# File lib/git/base.rb, line 368 368: def repack 369: self.lib.repack 370: end
returns reference to the git repository directory
@git.dir.path
# File lib/git/base.rb, line 81 81: def repo 82: @repository 83: end
returns the repository size in bytes
# File lib/git/base.rb, line 117 117: def repo_size 118: size = 0 119: Dir.chdir(repo.path) do 120: (size, dot) = `du -s`.chomp.split 121: end 122: size.to_i 123: end
resets the working directory to the provided commitish
# File lib/git/base.rb, line 257 257: def reset(commitish = nil, opts = {}) 258: self.lib.reset(commitish, opts) 259: end
resets the working directory to the commitish with ’—hard’
# File lib/git/base.rb, line 262 262: def reset_hard(commitish = nil, opts = {}) 263: opts = {:hard => true}.merge(opts) 264: self.lib.reset(commitish, opts) 265: end
runs git rev-parse to convert the objectish to a full sha
@git.revparse("HEAD^^") @git.revparse('v2.4^{tree}') @git.revparse('v2.4:/doc/index.html')
# File lib/git/base.rb, line 459 459: def revparse(objectish) 460: self.lib.revparse(objectish) 461: end
# File lib/git/base.rb, line 96 96: def set_index(index_file, check = true) 97: @lib = nil 98: @index = Git::Index.new(index_file.to_s, check) 99: end
# File lib/git/base.rb, line 91 91: def set_working(work_dir, check = true) 92: @lib = nil 93: @working_directory = Git::WorkingDirectory.new(work_dir.to_s, check) 94: end
returns a Git::Status object
# File lib/git/base.rb, line 174 174: def status 175: Git::Status.new(self) 176: end
returns a Git::Tag object
# File lib/git/base.rb, line 352 352: def tag(tag_name) 353: Git::Object.new(self, tag_name, 'tag', true) 354: end
# File lib/git/base.rb, line 424 424: def update_ref(branch, commit) 425: branch(branch).update_ref(commit) 426: end
LOWER LEVEL INDEX OPERATIONS ##
# File lib/git/base.rb, line 388 388: def with_index(new_index) # :yields: new_index 389: old_index = @index 390: set_index(new_index, false) 391: return_value = yield @index 392: set_index(old_index) 393: return_value 394: end
# File lib/git/base.rb, line 396 396: def with_temp_index &blk 397: tempfile = Tempfile.new('temp-index') 398: temp_path = tempfile.path 399: tempfile.unlink 400: with_index(temp_path, &blk) 401: end
# File lib/git/base.rb, line 444 444: def with_temp_working &blk 445: tempfile = Tempfile.new("temp-workdir") 446: temp_dir = tempfile.path 447: tempfile.unlink 448: Dir.mkdir(temp_dir, 0700) 449: with_working(temp_dir, &blk) 450: end
# File lib/git/base.rb, line 433 433: def with_working(work_dir) # :yields: the Git::WorkingDirectory 434: return_value = false 435: old_working = @working_directory 436: set_working(work_dir) 437: Dir.chdir work_dir do 438: return_value = yield @working_directory 439: end 440: set_working(old_working) 441: return_value 442: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.