Class: Grit::Repo
- Inherits:
-
Object
- Object
- Grit::Repo
- Defined in:
- lib/grit/repo.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#bare ⇒ Object
readonly
Public: The Boolean of whether or not the repo is bare.
-
#git ⇒ Object
Public: The Grit::Git command line interface object.
-
#path ⇒ Object
Public: The String path of the Git repo.
-
#working_dir ⇒ Object
Public: The String path to the working directory of the repo, or nil if there is no working directory.
Class Method Summary collapse
-
.init(path, git_options = {}, repo_options = {}) ⇒ Object
Public: Initialize a git repository (create it on the filesystem).
-
.init_bare(path, git_options = {}, repo_options = {}) ⇒ Object
Public: Initialize a bare git repository (create it on the filesystem).
-
.init_bare_or_open(path, git_options = {}, repo_options = {}) ⇒ Object
Public: Initialize a bare Git repository (create it on the filesystem) or, if the repo already exists, simply return it.
Instance Method Summary collapse
-
#add(*files) ⇒ Object
Adds files to the index.
-
#alternates ⇒ Object
The list of alternates for this repo.
-
#alternates=(alts) ⇒ Object
Sets the alternates
alts
is the Array of String paths representing the alternates. -
#archive_tar(treeish = 'master', prefix = nil) ⇒ Object
Archive the given treeish
treeish
is the treeish name/id (default ‘master’)prefix
is the optional prefix. -
#archive_tar_gz(treeish = 'master', prefix = nil) ⇒ Object
Archive and gzip the given treeish
treeish
is the treeish name/id (default ‘master’)prefix
is the optional prefix. -
#archive_to_file(treeish = 'master', prefix = nil, filename = 'archive.tar.gz', format = nil, pipe = "gzip") ⇒ Object
Write an archive directly to a file
treeish
is the treeish name/id (default ‘master’)prefix
is the optional prefix (default nil)filename
is the name of the file (default ‘archive.tar.gz’)format
is the optional format (default nil)pipe
is the command to run the output through (default ‘gzip’). -
#batch(*shas) ⇒ Object
Public: Return the full Git objects from the given SHAs.
- #blame(file, commit = nil) ⇒ Object
- #blame_tree(commit, path = nil) ⇒ Object
-
#blob(id) ⇒ Object
The Blob object for the given id
id
is the SHA1 id of the blob. -
#commit(id) ⇒ Object
The Commit object for the specified id
id
is the SHA1 identifier of the commit. -
#commit_all(message) ⇒ Object
Commits all tracked and modified files.
-
#commit_count(start = 'master') ⇒ Object
The number of commits reachable by the given branch/commit
start
is the branch/commit name (default ‘master’). -
#commit_deltas_from(other_repo, ref = "master", other_ref = "master") ⇒ Object
Returns a list of commits that is in
other_repo
but not in self. -
#commit_diff(commit) ⇒ Object
The commit diff for the given commit
commit
is the commit name/id. -
#commit_index(message) ⇒ Object
Commits current index.
- #commit_objects(refs) ⇒ Object
- #commit_stats(start = 'master', max_count = 10, skip = 0) ⇒ Object
-
#commits(start = 'master', max_count = 10, skip = 0) ⇒ Object
An array of Commit objects representing the history of a given ref/commit
start
is the branch/commit name (default ‘master’)max_count
is the maximum number of commits to return (default 10, usefalse
for all)skip
is the number of commits to skip (default 0). -
#commits_between(from, to) ⇒ Object
The Commits objects that are reachable via
to
but not viafrom
Commits are returned in chronological order. -
#commits_since(start = 'master', since = '1970-01-01', extra_options = {}) ⇒ Object
The Commits objects that are newer than the specified date.
- #config ⇒ Object
- #delete_ref(ref) ⇒ Object
-
#description ⇒ Object
The project’s description.
-
#diff(a, b, *paths) ⇒ Object
The diff from commit
a
to commitb
, optionally restricted to the given file(s)a
is the base commitb
is the other commitpaths
is an optional list of file paths on which to restrict the diff. - #diff_objects(commit_sha, parents = true) ⇒ Object
-
#disable_daemon_serve ⇒ Object
Disable git-daemon serving of this repository by ensuring there is no git-daemon-export-ok file in its git directory.
-
#enable_daemon_serve ⇒ Object
Enable git-daemon serving of this repository by writing the git-daemon-export-ok file to its git directory.
- #fast_forwardable?(to, from) ⇒ Boolean
-
#fork_bare(path, options = {}) ⇒ Object
Public: Create a bare fork of this repository.
-
#fork_bare_from(path, options = {}) ⇒ Object
Public: Fork a bare git repository from another repo.
- #gc_auto ⇒ Object
- #get_head(head_name) ⇒ Object
- #grep(searchtext, contextlines = 3, branch = 'master') ⇒ Object
-
#head ⇒ Object
Object reprsenting the current repo head.
- #head_count ⇒ Object (also: #branch_count)
-
#heads ⇒ Object
(also: #branches)
An array of Head objects representing the branch heads in this repo.
- #index ⇒ Object
-
#initialize(path, options = {}) ⇒ Repo
constructor
Public: Create a new Repo instance.
-
#inspect ⇒ Object
Pretty object inspection.
- #is_head?(head_name) ⇒ Boolean
-
#log(commit = 'master', path = nil, options = {}) ⇒ Object
The commit log for a treeish.
-
#lstree(treeish = 'master', options = {}) ⇒ Object
quick way to get a simple array of hashes of the entries of a single tree or recursive tree listing from a given sha or reference
treeish
is the reference (default ‘master’)options
is a hash or options - currently only takes :recursive. - #object(sha) ⇒ Object
- #objects(refs) ⇒ Object
- #objects_between(ref1, ref2 = nil) ⇒ Object
-
#parse_batch(text) ⇒ Object
Parses ‘git cat-file –batch` output, returning an array of Grit objects.
-
#recent_tag_name(committish = nil, options = {}) ⇒ Object
Finds the most recent annotated tag name that is reachable from a commit.
-
#refs ⇒ Object
An array of Ref objects representing the refs in this repo.
-
#refs_list ⇒ Object
returns an array of hashes representing all references.
- #remote_add(name, url) ⇒ Object
- #remote_count ⇒ Object
- #remote_fetch(name) ⇒ Object
- #remote_list ⇒ Object
-
#remotes ⇒ Object
An array of Remote objects representing the remote branches in this repo.
-
#remotes_fetch_needed(remotes) ⇒ Object
takes an array of remote names and last pushed dates fetches from all of the remotes where the local fetch date is earlier than the passed date, then records the last fetched date.
-
#remove(*files) ⇒ Object
Remove files from the index.
-
#rename(name) ⇒ Object
Rename the current repository directory.
- #status ⇒ Object
- #tag_count ⇒ Object
-
#tags ⇒ Object
An array of Tag objects that are available in this repo.
-
#tree(treeish = 'master', paths = []) ⇒ Object
The Tree object for the given treeish reference
treeish
is the reference (default ‘master’)paths
is an optional Array of directory paths to restrict the tree (default []). - #update_ref(head, commit_sha) ⇒ Object
Constructor Details
#initialize(path, options = {}) ⇒ Repo
Public: Create a new Repo instance.
path - The String path to either the root git directory or the bare
git repo. Bare repos are expected to end with ".git".
options - A Hash of options (default: {}):
:is_bare - Boolean whether to consider the repo as bare even
if the repo name does not end with ".git".
Examples
r = Repo.new("/Users/tom/dev/normal")
r = Repo.new("/Users/tom/public/bare.git")
r = Repo.new("/Users/tom/public/bare", {:is_bare => true})
Returns a newly initialized Grit::Repo. Raises Grit::InvalidGitRepositoryError if the path exists but is not
a Git repository.
Raises Grit::NoSuchPathError if the path does not exist.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/grit/repo.rb', line 39 def initialize(path, = {}) epath = File.(path) if File.exist?(File.join(epath, '.git')) self.working_dir = epath self.path = File.join(epath, '.git') @bare = false elsif File.exist?(epath) && (epath =~ /\.git$/ || [:is_bare]) self.path = epath @bare = true elsif File.exist?(epath) raise InvalidGitRepositoryError.new(epath) else raise NoSuchPathError.new(epath) end self.git = Git.new(self.path) end |
Instance Attribute Details
#bare ⇒ Object (readonly)
Public: The Boolean of whether or not the repo is bare.
16 17 18 |
# File 'lib/grit/repo.rb', line 16 def @bare end |
#git ⇒ Object
Public: The Grit::Git command line interface object.
19 20 21 |
# File 'lib/grit/repo.rb', line 19 def git @git end |
#path ⇒ Object
Public: The String path of the Git repo.
9 10 11 |
# File 'lib/grit/repo.rb', line 9 def path @path end |
#working_dir ⇒ Object
Public: The String path to the working directory of the repo, or nil if there is no working directory.
13 14 15 |
# File 'lib/grit/repo.rb', line 13 def working_dir @working_dir end |
Class Method Details
.init(path, git_options = {}, repo_options = {}) ⇒ Object
Public: Initialize a git repository (create it on the filesystem). By default, the newly created repository will contain a working directory. If you would like to create a bare repo, use Grit::Repo.init_bare.
path - The String full path to the repo. Traditionally ends with
"/<name>.git".
git_options - A Hash of additional options to the git init command
(default: {}).
repo_options - A Hash of additional options to the Grit::Repo.new call
(default: {}).
Examples
Grit::Repo.init('/var/git/myrepo.git')
Returns the newly created Grit::Repo.
74 75 76 77 78 79 80 |
# File 'lib/grit/repo.rb', line 74 def self.init(path, = {}, = {}) = {:base => false}.merge() git = Git.new(path) git.fs_mkdir('..') git.init(, path) self.new(path, ) end |
.init_bare(path, git_options = {}, repo_options = {}) ⇒ Object
Public: Initialize a bare git repository (create it on the filesystem).
path - The String full path to the repo. Traditionally ends with
"/<name>.git".
git_options - A Hash of additional options to the git init command
(default: {}).
repo_options - A Hash of additional options to the Grit::Repo.new call
(default: {}).
Examples
Grit::Repo.('/var/git/myrepo.git')
Returns the newly created Grit::Repo.
96 97 98 99 100 101 102 103 |
# File 'lib/grit/repo.rb', line 96 def self.(path, = {}, = {}) = {:bare => true}.merge() git = Git.new(path) git.fs_mkdir('..') git.init() = {:is_bare => true}.merge() self.new(path, ) end |
.init_bare_or_open(path, git_options = {}, repo_options = {}) ⇒ Object
Public: Initialize a bare Git repository (create it on the filesystem) or, if the repo already exists, simply return it.
path - The String full path to the repo. Traditionally ends with
"/<name>.git".
git_options - A Hash of additional options to the git init command
(default: {}).
repo_options - A Hash of additional options to the Grit::Repo.new call
(default: {}).
Returns the new or existing Grit::Repo.
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/grit/repo.rb', line 116 def self.(path, = {}, = {}) git = Git.new(path) unless git.exist? git.fs_mkdir(path) git.init() end self.new(path, ) end |
Instance Method Details
#add(*files) ⇒ Object
Adds files to the index
253 254 255 |
# File 'lib/grit/repo.rb', line 253 def add(*files) self.git.add({}, *files.flatten) end |
#alternates ⇒ Object
The list of alternates for this repo
Returns Array (pathnames of alternates)
666 667 668 669 670 671 |
# File 'lib/grit/repo.rb', line 666 def alternates alternates_path = "objects/info/alternates" self.git.fs_read(alternates_path).strip.split("\n") rescue Errno::ENOENT [] end |
#alternates=(alts) ⇒ Object
Sets the alternates
+alts+ is the Array of String paths representing the alternates
Returns nothing
677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/grit/repo.rb', line 677 def alternates=(alts) alts.each do |alt| unless File.exist?(alt) raise "Could not set alternates. Alternate path #{alt} must exist" end end if alts.empty? self.git.fs_write('objects/info/alternates', '') else self.git.fs_write('objects/info/alternates', alts.join("\n")) end end |
#archive_tar(treeish = 'master', prefix = nil) ⇒ Object
Archive the given treeish
+treeish+ is the treeish name/id (default 'master')
+prefix+ is the optional prefix
Examples
repo.archive_tar
# => <String containing tar archive>
repo.archive_tar('a87ff14')
# => <String containing tar archive for commit a87ff14>
repo.archive_tar('master', 'myproject/')
# => <String containing tar archive and prefixed with 'myproject/'>
Returns String (containing tar archive)
599 600 601 602 603 |
# File 'lib/grit/repo.rb', line 599 def archive_tar(treeish = 'master', prefix = nil) = {} [:prefix] = prefix if prefix self.git.archive(, treeish) end |
#archive_tar_gz(treeish = 'master', prefix = nil) ⇒ Object
Archive and gzip the given treeish
+treeish+ is the treeish name/id (default 'master')
+prefix+ is the optional prefix
Examples
repo.archive_tar_gz
# => <String containing tar.gz archive>
repo.archive_tar_gz('a87ff14')
# => <String containing tar.gz archive for commit a87ff14>
repo.archive_tar_gz('master', 'myproject/')
# => <String containing tar.gz archive and prefixed with 'myproject/'>
Returns String (containing tar.gz archive)
620 621 622 623 624 625 |
# File 'lib/grit/repo.rb', line 620 def archive_tar_gz(treeish = 'master', prefix = nil) = {} [:prefix] = prefix if prefix [:pipeline] = true self.git.archive(, treeish, "| gzip -n") end |
#archive_to_file(treeish = 'master', prefix = nil, filename = 'archive.tar.gz', format = nil, pipe = "gzip") ⇒ Object
Write an archive directly to a file
+treeish+ is the treeish name/id (default 'master')
+prefix+ is the optional prefix (default nil)
+filename+ is the name of the file (default 'archive.tar.gz')
+format+ is the optional format (default nil)
+pipe+ is the command to run the output through (default 'gzip')
Returns nothing
635 636 637 638 639 640 641 |
# File 'lib/grit/repo.rb', line 635 def archive_to_file(treeish = 'master', prefix = nil, filename = 'archive.tar.gz', format = nil, pipe = "gzip") = {} [:prefix] = prefix if prefix [:format] = format if format [:pipeline] = true self.git.archive(, treeish, "| #{pipe} > #{filename}") end |
#batch(*shas) ⇒ Object
Public: Return the full Git objects from the given SHAs. Only Commit objects are parsed for now.
*shas - Array of String SHAs.
Returns an Array of Grit objects (Grit::Commit).
167 168 169 170 171 |
# File 'lib/grit/repo.rb', line 167 def batch(*shas) shas.flatten! text = git.native(:cat_file, {:batch => true, :input => (shas * "\n")}) parse_batch(text) end |
#blame(file, commit = nil) ⇒ Object
203 204 205 |
# File 'lib/grit/repo.rb', line 203 def blame(file, commit = nil) Blame.new(self, file, commit) end |
#blame_tree(commit, path = nil) ⇒ Object
263 264 265 266 267 268 269 270 271 |
# File 'lib/grit/repo.rb', line 263 def blame_tree(commit, path = nil) commit_array = self.git.blame_tree(commit, path) final_array = {} commit_array.each do |file, sha| final_array[file] = commit(sha) end final_array end |
#blob(id) ⇒ Object
The Blob object for the given id
+id+ is the SHA1 id of the blob
Returns Grit::Blob (unbaked)
546 547 548 |
# File 'lib/grit/repo.rb', line 546 def blob(id) Blob.create(self, :id => id) end |
#commit(id) ⇒ Object
The Commit object for the specified id
+id+ is the SHA1 identifier of the commit
Returns Grit::Commit (baked)
436 437 438 439 440 |
# File 'lib/grit/repo.rb', line 436 def commit(id) = {:max_count => 1} Commit.find_all(self, id, ).first end |
#commit_all(message) ⇒ Object
Commits all tracked and modified files
Returns true/false if commit worked
248 249 250 |
# File 'lib/grit/repo.rb', line 248 def commit_all() self.git.commit({}, '-a', '-m', ) end |
#commit_count(start = 'master') ⇒ Object
The number of commits reachable by the given branch/commit
+start+ is the branch/commit name (default 'master')
Returns Integer
428 429 430 |
# File 'lib/grit/repo.rb', line 428 def commit_count(start = 'master') Commit.count(self, start) end |
#commit_deltas_from(other_repo, ref = "master", other_ref = "master") ⇒ Object
Returns a list of commits that is in other_repo
but not in self
Returns Grit::Commit[]
445 446 447 448 449 450 451 452 453 454 |
# File 'lib/grit/repo.rb', line 445 def commit_deltas_from(other_repo, ref = "master", other_ref = "master") # TODO: we should be able to figure out the branch point, rather than # rev-list'ing the whole thing repo_refs = self.git.rev_list({}, ref).strip.split("\n") other_repo_refs = other_repo.git.rev_list({}, other_ref).strip.split("\n") (other_repo_refs - repo_refs).map do |refn| Commit.find_all(other_repo, refn, {:max_count => 1}).first end end |
#commit_diff(commit) ⇒ Object
The commit diff for the given commit
+commit+ is the commit name/id
Returns Grit::Diff[]
580 581 582 |
# File 'lib/grit/repo.rb', line 580 def commit_diff(commit) Commit.diff(self, commit) end |
#commit_index(message) ⇒ Object
Commits current index
Returns true/false if commit worked
241 242 243 |
# File 'lib/grit/repo.rb', line 241 def commit_index() self.git.commit({}, '-m', ) end |
#commit_objects(refs) ⇒ Object
462 463 464 465 |
# File 'lib/grit/repo.rb', line 462 def commit_objects(refs) refs = refs.split(/\s+/) if refs.respond_to?(:to_str) self.git.rev_list({:timeout => false}, *refs).split("\n").map { |a| a[0, 40] } end |
#commit_stats(start = 'master', max_count = 10, skip = 0) ⇒ Object
376 377 378 379 380 381 |
# File 'lib/grit/repo.rb', line 376 def commit_stats(start = 'master', max_count = 10, skip = 0) = {:max_count => max_count, :skip => skip} CommitStats.find_all(self, start, ) end |
#commits(start = 'master', max_count = 10, skip = 0) ⇒ Object
An array of Commit objects representing the history of a given ref/commit
+start+ is the branch/commit name (default 'master')
+max_count+ is the maximum number of commits to return (default 10, use +false+ for all)
+skip+ is the number of commits to skip (default 0)
Returns Grit::Commit[] (baked)
389 390 391 392 393 394 |
# File 'lib/grit/repo.rb', line 389 def commits(start = 'master', max_count = 10, skip = 0) = {:max_count => max_count, :skip => skip} Commit.find_all(self, start, ) end |
#commits_between(from, to) ⇒ Object
The Commits objects that are reachable via to
but not via from
Commits are returned in chronological order.
+from+ is the branch/commit name of the younger item
+to+ is the branch/commit name of the older item
Returns Grit::Commit[] (baked)
402 403 404 |
# File 'lib/grit/repo.rb', line 402 def commits_between(from, to) Commit.find_all(self, "#{from}..#{to}").reverse end |
#commits_since(start = 'master', since = '1970-01-01', extra_options = {}) ⇒ Object
The Commits objects that are newer than the specified date. Commits are returned in chronological order.
+start+ is the branch/commit name (default 'master')
+since+ is a string representing a date/time
+extra_options+ is a hash of extra options
Returns Grit::Commit[] (baked)
418 419 420 421 422 |
# File 'lib/grit/repo.rb', line 418 def commits_since(start = 'master', since = '1970-01-01', = {}) = {:since => since}.merge() Commit.find_all(self, start, ) end |
#config ⇒ Object
691 692 693 |
# File 'lib/grit/repo.rb', line 691 def config @config ||= Config.new(self) end |
#delete_ref(ref) ⇒ Object
372 373 374 |
# File 'lib/grit/repo.rb', line 372 def delete_ref(ref) self.git.native(:update_ref, {:d => true}, ref) end |
#description ⇒ Object
The project’s description. Taken verbatim from GIT_REPO/description
Returns String
199 200 201 |
# File 'lib/grit/repo.rb', line 199 def description self.git.fs_read('description').chomp end |
#diff(a, b, *paths) ⇒ Object
The diff from commit a
to commit b
, optionally restricted to the given file(s)
+a+ is the base commit
+b+ is the other commit
+paths+ is an optional list of file paths on which to restrict the diff
565 566 567 568 569 570 571 572 573 574 |
# File 'lib/grit/repo.rb', line 565 def diff(a, b, *paths) diff = self.git.native('diff', {}, a, b, '--', *paths) if diff =~ /diff --git a/ diff = diff.sub(/.*?(diff --git a)/m, '\1') else diff = '' end Diff.list_from_string(self, diff) end |
#diff_objects(commit_sha, parents = true) ⇒ Object
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/grit/repo.rb', line 476 def diff_objects(commit_sha, parents = true) revs = [] Grit.no_quote = true if parents # PARENTS: revs = self.git.diff_tree({:timeout => false, :r => true, :t => true, :m => true}, commit_sha). strip.split("\n").map{ |a| r = a.split(' '); r[3] if r[1] != '160000' } else # NO PARENTS: revs = self.git.native(:ls_tree, {:timeout => false, :r => true, :t => true}, commit_sha). split("\n").map{ |a| a.split("\t").first.split(' ')[2] } end revs << self.commit(commit_sha).tree.id Grit.no_quote = false return revs.uniq.compact end |
#disable_daemon_serve ⇒ Object
Disable git-daemon serving of this repository by ensuring there is no git-daemon-export-ok file in its git directory
Returns nothing
655 656 657 |
# File 'lib/grit/repo.rb', line 655 def disable_daemon_serve self.git.fs_delete(DAEMON_EXPORT_FILE) end |
#enable_daemon_serve ⇒ Object
Enable git-daemon serving of this repository by writing the git-daemon-export-ok file to its git directory
Returns nothing
647 648 649 |
# File 'lib/grit/repo.rb', line 647 def enable_daemon_serve self.git.fs_write(DAEMON_EXPORT_FILE, '') end |
#fast_forwardable?(to, from) ⇒ Boolean
406 407 408 409 |
# File 'lib/grit/repo.rb', line 406 def fast_forwardable?(to, from) mb = self.git.native(:merge_base, {}, [to, from]).strip mb == from end |
#fork_bare(path, options = {}) ⇒ Object
Public: Create a bare fork of this repository.
path - The String full path of where to create the new fork.
Traditionally ends with "/<name>.git".
options - The Hash of additional options to the git clone command.
These options will be merged on top of the default Hash:
{:bare => true, :shared => true}.
Returns the newly forked Grit::Repo.
136 137 138 139 140 141 142 |
# File 'lib/grit/repo.rb', line 136 def (path, = {}) = {:bare => true, :shared => true} = .merge() Git.new(path).fs_mkdir('..') self.git.clone(, self.path, path) Repo.new(path) end |
#fork_bare_from(path, options = {}) ⇒ Object
Public: Fork a bare git repository from another repo.
path - The String full path of the repo from which to fork..
Traditionally ends with "/<name>.git".
options - The Hash of additional options to the git clone command.
These options will be merged on top of the default Hash:
{:bare => true, :shared => true}.
Returns the newly forked Grit::Repo.
153 154 155 156 157 158 159 |
# File 'lib/grit/repo.rb', line 153 def (path, = {}) = {:bare => true, :shared => true} = .merge() Git.new(self.path).fs_mkdir('..') self.git.clone(, path, self.path) Repo.new(self.path) end |
#gc_auto ⇒ Object
659 660 661 |
# File 'lib/grit/repo.rb', line 659 def gc_auto self.git.gc({:auto => true}) end |
#get_head(head_name) ⇒ Object
222 223 224 |
# File 'lib/grit/repo.rb', line 222 def get_head(head_name) heads.find { |h| h.name == head_name } end |
#grep(searchtext, contextlines = 3, branch = 'master') ⇒ Object
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
# File 'lib/grit/repo.rb', line 717 def grep(searchtext, contextlines = 3, branch = 'master') context_arg = '-C ' + contextlines.to_s result = git.native(:grep, {pipeline: false}, '-n', '-E', '-i', '-z', '--heading', '--break', context_arg, searchtext, branch).force_encoding('UTF-8') greps = [] filematches = result.split("\n\n") filematches.each do |filematch| binary = false file = '' matches = filematch.split("--\n") matches.each_with_index do |match, i| content = [] startline = 0 lines = match.split("\n") if i == 0 text = lines.first lines.slice!(0) file = text[/^Binary file (.+) matches$/] if file binary = true else text.slice! /^#{branch}:/ file = text end end lines.each_with_index do |line, j| line.chomp! number, text = line.split("\0", 2) if j == 0 startline = number.to_i end content << text end greps << Grit::Grep.new(self, file, startline, content, binary) end end greps end |
#head ⇒ Object
Object reprsenting the current repo head.
Returns Grit::Head (baked)
233 234 235 |
# File 'lib/grit/repo.rb', line 233 def head Head.current(self) end |
#head_count ⇒ Object Also known as: branch_count
215 216 217 |
# File 'lib/grit/repo.rb', line 215 def head_count Head.count_all(self) end |
#heads ⇒ Object Also known as: branches
An array of Head objects representing the branch heads in this repo
Returns Grit::Head[] (baked)
211 212 213 |
# File 'lib/grit/repo.rb', line 211 def heads Head.find_all(self) end |
#inspect ⇒ Object
Pretty object inspection
756 757 758 |
# File 'lib/grit/repo.rb', line 756 def inspect %Q{#<Grit::Repo "#{@path}">} end |
#is_head?(head_name) ⇒ Boolean
226 227 228 |
# File 'lib/grit/repo.rb', line 226 def is_head?(head_name) get_head(head_name) end |
#log(commit = 'master', path = nil, options = {}) ⇒ Object
The commit log for a treeish
Returns Grit::Commit[]
553 554 555 556 557 558 559 |
# File 'lib/grit/repo.rb', line 553 def log(commit = 'master', path = nil, = {}) = {:pretty => "raw"} = .merge() arg = path ? [commit, '--', path] : [commit] commits = self.git.log(, *arg) Commit.list_from_string(self, commits) end |
#lstree(treeish = 'master', options = {}) ⇒ Object
quick way to get a simple array of hashes of the entries of a single tree or recursive tree listing from a given sha or reference
+treeish+ is the reference (default 'master')
+options+ is a hash or options - currently only takes :recursive
Examples
repo.lstree('master', :recursive => true)
Returns array of hashes - one per tree entry
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/grit/repo.rb', line 515 def lstree(treeish = 'master', = {}) # check recursive option opts = {:timeout => false, :l => true, :t => true} if [:recursive] opts[:r] = true end # mode, type, sha, size, path revs = self.git.native(:ls_tree, opts, treeish) lines = revs.split("\n") revs = lines.map do |a| stuff, path = a.split("\t") mode, type, sha, size = stuff.split(" ") entry = {:mode => mode, :type => type, :sha => sha, :path => path} entry[:size] = size.strip.to_i if size.strip != '-' entry end revs end |
#object(sha) ⇒ Object
534 535 536 537 538 539 540 |
# File 'lib/grit/repo.rb', line 534 def object(sha) obj = git.get_git_object(sha) raw = Grit::GitRuby::Internal::RawObject.new(obj[:type], obj[:content]) object = Grit::GitRuby::GitObject.from_raw(raw) object.sha = sha object end |
#objects(refs) ⇒ Object
456 457 458 459 460 |
# File 'lib/grit/repo.rb', line 456 def objects(refs) refs = refs.split(/\s+/) if refs.respond_to?(:to_str) self.git.rev_list({:objects => true, :timeout => false}, *refs). split("\n").map { |a| a[0, 40] } end |
#objects_between(ref1, ref2 = nil) ⇒ Object
467 468 469 470 471 472 473 474 |
# File 'lib/grit/repo.rb', line 467 def objects_between(ref1, ref2 = nil) if ref2 refs = "#{ref2}..#{ref1}" else refs = ref1 end self.objects(refs) end |
#parse_batch(text) ⇒ Object
Parses ‘git cat-file –batch` output, returning an array of Grit objects.
text - Raw String output.
Returns an Array of Grit objects (Grit::Commit).
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/grit/repo.rb', line 178 def parse_batch(text) io = StringIO.new(text) objects = [] while line = io.gets sha, type, size = line.split(" ", 3) parser = BATCH_PARSERS[type] if type == 'missing' || !parser io.seek(size.to_i + 1, IO::SEEK_CUR) objects << nil next end object = io.read(size.to_i + 1) objects << parser.parse_batch(self, sha, size, object) end objects end |
#recent_tag_name(committish = nil, options = {}) ⇒ Object
Finds the most recent annotated tag name that is reachable from a commit.
@repo.recent_tag_name('master')
# => "v1.0-0-abcdef"
committish - optional commit SHA, branch, or tag name. options - optional hash of options to pass to git.
Default: {:always => true}
:tags => true # use lightweight tags too.
:abbrev => Integer # number of hex digits to form the unique
name. Defaults to 7.
:long => true # always output tag + commit sha
# see `git describe` docs for more options.
Returns the String tag name, or just the commit if no tag is found. If there have been updates since the tag was made, a suffix is added with the number of commits since the tag, and the abbreviated object name of the most recent commit. Returns nil if the committish value is not found.
308 309 310 311 |
# File 'lib/grit/repo.rb', line 308 def recent_tag_name(committish = nil, = {}) value = git.describe({:always => true}.update(), committish.to_s).to_s.strip value.size.zero? ? nil : value end |
#refs ⇒ Object
An array of Ref objects representing the refs in this repo
Returns Grit::Ref[] (baked)
357 358 359 |
# File 'lib/grit/repo.rb', line 357 def refs [ Head.find_all(self), Tag.find_all(self), Remote.find_all(self) ].flatten end |
#refs_list ⇒ Object
returns an array of hashes representing all references
362 363 364 365 366 367 368 369 370 |
# File 'lib/grit/repo.rb', line 362 def refs_list refs = self.git.for_each_ref refarr = refs.split("\n").map do |line| shatype, ref = line.split("\t") sha, type = shatype.split(' ') [ref, sha, type] end refarr end |
#remote_add(name, url) ⇒ Object
329 330 331 |
# File 'lib/grit/repo.rb', line 329 def remote_add(name, url) self.git.remote({}, 'add', name, url) end |
#remote_count ⇒ Object
321 322 323 |
# File 'lib/grit/repo.rb', line 321 def remote_count Remote.count_all(self) end |
#remote_fetch(name) ⇒ Object
333 334 335 |
# File 'lib/grit/repo.rb', line 333 def remote_fetch(name) self.git.fetch({}, name) end |
#remote_list ⇒ Object
325 326 327 |
# File 'lib/grit/repo.rb', line 325 def remote_list self.git.list_remotes end |
#remotes ⇒ Object
An array of Remote objects representing the remote branches in this repo
Returns Grit::Remote[] (baked)
317 318 319 |
# File 'lib/grit/repo.rb', line 317 def remotes Remote.find_all(self) end |
#remotes_fetch_needed(remotes) ⇒ Object
takes an array of remote names and last pushed dates fetches from all of the remotes where the local fetch date is earlier than the passed date, then records the last fetched date
{ ‘origin’ => date,
'peter => date,
}
345 346 347 348 349 350 |
# File 'lib/grit/repo.rb', line 345 def remotes_fetch_needed(remotes) remotes.each do |remote, date| # TODO: check against date self.remote_fetch(remote) end end |
#remove(*files) ⇒ Object
Remove files from the index
258 259 260 |
# File 'lib/grit/repo.rb', line 258 def remove(*files) self.git.rm({}, *files.flatten) end |
#rename(name) ⇒ Object
Rename the current repository directory.
+name+ is the new name
Returns nothing
709 710 711 712 713 714 715 |
# File 'lib/grit/repo.rb', line 709 def rename(name) if @bare self.git.fs_move('/', "../#{name}") else self.git.fs_move('/', "../../#{name}") end end |
#tag_count ⇒ Object
285 286 287 |
# File 'lib/grit/repo.rb', line 285 def tag_count Tag.count_all(self) end |
#tags ⇒ Object
An array of Tag objects that are available in this repo
Returns Grit::Tag[] (baked)
281 282 283 |
# File 'lib/grit/repo.rb', line 281 def Tag.find_all(self) end |
#tree(treeish = 'master', paths = []) ⇒ Object
The Tree object for the given treeish reference
+treeish+ is the reference (default 'master')
+paths+ is an optional Array of directory paths to restrict the tree (default [])
Examples
repo.tree('master', ['lib/'])
Returns Grit::Tree (baked)
501 502 503 |
# File 'lib/grit/repo.rb', line 501 def tree(treeish = 'master', paths = []) Tree.construct(self, treeish, paths) end |
#update_ref(head, commit_sha) ⇒ Object
699 700 701 702 703 |
# File 'lib/grit/repo.rb', line 699 def update_ref(head, commit_sha) return nil if !commit_sha || (commit_sha.size != 40) self.git.fs_write("refs/heads/#{head}", commit_sha) commit_sha end |