Class: Grit::Repo
- Inherits:
-
Object
- Object
- Grit::Repo
- Defined in:
- lib/grit/repo.rb
Constant Summary collapse
- DAEMON_EXPORT_FILE =
'git-daemon-export-ok'
Instance Attribute Summary collapse
-
#bare ⇒ Object
readonly
Returns the value of attribute bare.
-
#git ⇒ Object
The git command line interface object.
-
#path ⇒ Object
The path of the git repo as a String.
-
#working_dir ⇒ Object
Returns the value of attribute working_dir.
Class Method Summary collapse
-
.init(path) ⇒ Object
Does nothing yet…
-
.init_bare(path, git_options = {}, repo_options = {}) ⇒ Object
Initialize a bare git repository at the given path
pathis the full path to the repo (traditionally ends with /<name>.git)optionsis any additional options to the git init command. - .init_bare_or_open(path, git_options = {}, repo_options = {}) ⇒ Object
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
altsis the Array of String paths representing the alternates. -
#archive_tar(treeish = 'master', prefix = nil) ⇒ Object
Archive the given treeish
treeishis the treeish name/id (default ‘master’)prefixis the optional prefix. -
#archive_tar_gz(treeish = 'master', prefix = nil) ⇒ Object
Archive and gzip the given treeish
treeishis the treeish name/id (default ‘master’)prefixis 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
treeishis the treeish name/id (default ‘master’)prefixis the optional prefix (default nil)filenameis the name of the file (default ‘archive.tar.gz’)formatis the optional format (default nil)pipeis the command to run the output through (default ‘gzip’). - #blame(file, commit = nil) ⇒ Object
- #blame_tree(commit, path = nil) ⇒ Object
-
#blob(id) ⇒ Object
The Blob object for the given id
idis the SHA1 id of the blob. -
#commit(id) ⇒ Object
The Commit object for the specified id
idis 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
startis 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_repobut not in self. -
#commit_diff(commit) ⇒ Object
The commit diff for the given commit
commitis the commit name/id. -
#commit_index(message) ⇒ Object
Commits current index.
- #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
startis the branch/commit name (default ‘master’)max_countis the maximum number of commits to return (default 10, usefalsefor all)skipis the number of commits to skip (default 0). -
#commits_between(from, to) ⇒ Object
The Commits objects that are reachable via
tobut not viafromCommits 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
-
#description ⇒ Object
The project’s description.
-
#diff(a, b, *paths) ⇒ Object
The diff from commit
ato commitb, optionally restricted to the given file(s)ais the base commitbis the other commitpathsis 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.
-
#fork_bare(path, options = {}) ⇒ Object
Fork a bare git repository from this repo
pathis the full path of the new repo (traditionally ends with /<name>.git)optionsis any additional options to the git clone command (:bare and :shared are true by default). - #gc_auto ⇒ Object
- #get_head(head_name) ⇒ Object
-
#head ⇒ Object
Object reprsenting the current repo head.
-
#heads ⇒ Object
(also: #branches)
An array of Head objects representing the branch heads in this repo.
- #index ⇒ Object
-
#initialize(path, options = {}) ⇒ Repo
constructor
Create a new Repo instance
pathis the path to either the root git directory or the bare git repooptions:is_bare force to load a bare repo. -
#inspect ⇒ Object
Pretty object inspection.
- #is_head?(head_name) ⇒ Boolean
-
#log(commit = 'master', path = nil, options = {}) ⇒ Object
The commit log for a treeish.
- #objects(refs) ⇒ Object
- #objects_between(ref1, ref2 = nil) ⇒ Object
-
#refs ⇒ Object
An array of Ref objects representing the refs in this repo.
- #remote_add(name, url) ⇒ 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
-
#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
treeishis the reference (default ‘master’)pathsis an optional Array of directory paths to restrict the tree (deafult []). - #update_ref(head, commit_sha) ⇒ Object
Constructor Details
#initialize(path, options = {}) ⇒ Repo
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/grit/repo.rb', line 23 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)
Returns the value of attribute bare.
9 10 11 |
# File 'lib/grit/repo.rb', line 9 def @bare end |
#git ⇒ Object
The git command line interface object
12 13 14 |
# File 'lib/grit/repo.rb', line 12 def git @git end |
#path ⇒ Object
The path of the git repo as a String
7 8 9 |
# File 'lib/grit/repo.rb', line 7 def path @path end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
8 9 10 |
# File 'lib/grit/repo.rb', line 8 def working_dir @working_dir end |
Class Method Details
.init(path) ⇒ Object
Does nothing yet…
43 44 45 46 47 48 |
# File 'lib/grit/repo.rb', line 43 def self.init(path) # !! TODO !! # create directory # generate initial git directory # create new Grit::Repo on that dir, return it end |
Instance Method Details
#add(*files) ⇒ Object
Adds files to the index
103 104 105 |
# File 'lib/grit/repo.rb', line 103 def add(*files) self.git.add({}, *files.flatten) end |
#alternates ⇒ Object
The list of alternates for this repo
Returns Array (pathnames of alternates)
451 452 453 454 455 456 457 458 |
# File 'lib/grit/repo.rb', line 451 def alternates alternates_path = "objects/info/alternates" if self.git.fs_exist?(alternates_path) self.git.fs_read(alternates_path).strip.split("\n") else [] end end |
#alternates=(alts) ⇒ Object
Sets the alternates
+alts+ is the Array of String paths representing the alternates
Returns nothing
464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/grit/repo.rb', line 464 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)
386 387 388 389 390 |
# File 'lib/grit/repo.rb', line 386 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)
407 408 409 410 411 |
# File 'lib/grit/repo.rb', line 407 def archive_tar_gz(treeish = 'master', prefix = nil) = {} [:prefix] = prefix if prefix 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
421 422 423 424 425 426 |
# File 'lib/grit/repo.rb', line 421 def archive_to_file(treeish = 'master', prefix = nil, filename = 'archive.tar.gz', format = nil, pipe = "gzip") = {} [:prefix] = prefix if prefix [:format] = format if format self.git.archive(, treeish, "| #{pipe} > #{filename}") end |
#blame(file, commit = nil) ⇒ Object
57 58 59 |
# File 'lib/grit/repo.rb', line 57 def blame(file, commit = nil) Blame.new(self, file, commit) end |
#blame_tree(commit, path = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/grit/repo.rb', line 113 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)
303 304 305 |
# File 'lib/grit/repo.rb', line 303 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)
234 235 236 237 238 |
# File 'lib/grit/repo.rb', line 234 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
98 99 100 |
# File 'lib/grit/repo.rb', line 98 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
226 227 228 |
# File 'lib/grit/repo.rb', line 226 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[]
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/grit/repo.rb', line 243 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 |ref| Commit.find_all(other_repo, ref, {: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[]
330 331 332 |
# File 'lib/grit/repo.rb', line 330 def commit_diff(commit) Commit.diff(self, commit) end |
#commit_index(message) ⇒ Object
Commits current index
Returns true/false if commit worked
91 92 93 |
# File 'lib/grit/repo.rb', line 91 def commit_index() self.git.commit({}, '-m', ) end |
#commit_stats(start = 'master', max_count = 10, skip = 0) ⇒ Object
179 180 181 182 183 184 |
# File 'lib/grit/repo.rb', line 179 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)
192 193 194 195 196 197 |
# File 'lib/grit/repo.rb', line 192 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)
205 206 207 |
# File 'lib/grit/repo.rb', line 205 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 represeting a date/time
+extra_options+ is a hash of extra options
Returns Grit::Commit[] (baked)
216 217 218 219 220 |
# File 'lib/grit/repo.rb', line 216 def commits_since(start = 'master', since = '1970-01-01', = {}) = {:since => since}.merge() Commit.find_all(self, start, ) end |
#config ⇒ Object
478 479 480 |
# File 'lib/grit/repo.rb', line 478 def config @config ||= Config.new(self) end |
#description ⇒ Object
The project’s description. Taken verbatim from GIT_REPO/description
Returns String
53 54 55 |
# File 'lib/grit/repo.rb', line 53 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
322 323 324 |
# File 'lib/grit/repo.rb', line 322 def diff(a, b, *paths) self.git.diff({}, a, b, '--', *paths) end |
#diff_objects(commit_sha, parents = true) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/grit/repo.rb', line 270 def diff_objects(commit_sha, parents = true) revs = [] Grit.no_quote = true if parents # PARENTS: cmd = "-r -t -m #{commit_sha}" revs = self.git.diff_tree({}, cmd).strip.split("\n").map{ |a| r = a.split(' '); r[3] if r[1] != '160000' } else # NO PARENTS: cmd = "-r -t #{commit_sha}" revs = self.git.method_missing('ls-tree', {}, "-r -t #{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
440 441 442 |
# File 'lib/grit/repo.rb', line 440 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
432 433 434 |
# File 'lib/grit/repo.rb', line 432 def enable_daemon_serve self.git.fs_write(DAEMON_EXPORT_FILE, '') end |
#fork_bare(path, options = {}) ⇒ Object
Fork a bare git repository from this repo
+path+ is the full path of the new repo (traditionally ends with /<name>.git)
+options+ is any additional options to the git clone command (:bare and :shared are true by default)
Returns Grit::Repo (the newly forked repo)
363 364 365 366 367 368 369 |
# File 'lib/grit/repo.rb', line 363 def (path, = {}) = {:bare => true, :shared => true} = .merge() Git.new(path).fs_mkdir('..') self.git.clone(, self.path, path) self.new(path) end |
#gc_auto ⇒ Object
444 445 446 |
# File 'lib/grit/repo.rb', line 444 def gc_auto self.git.gc({:auto => true}) end |
#get_head(head_name) ⇒ Object
72 73 74 |
# File 'lib/grit/repo.rb', line 72 def get_head(head_name) heads.find { |h| h.name == head_name } end |
#head ⇒ Object
Object reprsenting the current repo head.
Returns Grit::Head (baked)
83 84 85 |
# File 'lib/grit/repo.rb', line 83 def head Head.current(self) end |
#heads ⇒ Object Also known as: branches
An array of Head objects representing the branch heads in this repo
Returns Grit::Head[] (baked)
66 67 68 |
# File 'lib/grit/repo.rb', line 66 def heads Head.find_all(self) end |
#inspect ⇒ Object
Pretty object inspection
505 506 507 |
# File 'lib/grit/repo.rb', line 505 def inspect %Q{#<Grit::Repo "#{@path}">} end |
#is_head?(head_name) ⇒ Boolean
76 77 78 |
# File 'lib/grit/repo.rb', line 76 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[]
310 311 312 313 314 315 316 |
# File 'lib/grit/repo.rb', line 310 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 |
#objects(refs) ⇒ Object
254 255 256 257 258 259 |
# File 'lib/grit/repo.rb', line 254 def objects(refs) Grit.no_quote = true obj = self.git.rev_list({:objects => true}, refs).split("\n").map { |a| a[0, 40] } Grit.no_quote = false obj end |
#objects_between(ref1, ref2 = nil) ⇒ Object
261 262 263 264 265 266 267 268 |
# File 'lib/grit/repo.rb', line 261 def objects_between(ref1, ref2 = nil) if ref2 refs = "#{ref2}..#{ref1}" else refs = ref1 end self.objects(refs) end |
#refs ⇒ Object
An array of Ref objects representing the refs in this repo
Returns Grit::Ref[] (baked)
175 176 177 |
# File 'lib/grit/repo.rb', line 175 def refs [ Head.find_all(self), Tag.find_all(self), Remote.find_all(self) ].flatten end |
#remote_add(name, url) ⇒ Object
147 148 149 |
# File 'lib/grit/repo.rb', line 147 def remote_add(name, url) self.git.remote({}, 'add', name, url) end |
#remote_fetch(name) ⇒ Object
151 152 153 |
# File 'lib/grit/repo.rb', line 151 def remote_fetch(name) self.git.fetch({}, name) end |
#remote_list ⇒ Object
143 144 145 |
# File 'lib/grit/repo.rb', line 143 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)
139 140 141 |
# File 'lib/grit/repo.rb', line 139 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,
}
163 164 165 166 167 168 |
# File 'lib/grit/repo.rb', line 163 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
108 109 110 |
# File 'lib/grit/repo.rb', line 108 def remove(*files) self.git.rm({}, *files.flatten) end |
#rename(name) ⇒ Object
Rename the current repository directory.
+name+ is the new name
Returns nothing
496 497 498 499 500 501 502 |
# File 'lib/grit/repo.rb', line 496 def rename(name) if @bare self.git.fs_move('/', "../#{name}") else self.git.fs_move('/', "../../#{name}") end end |
#tags ⇒ Object
An array of Tag objects that are available in this repo
Returns Grit::Tag[] (baked)
131 132 133 |
# File 'lib/grit/repo.rb', line 131 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 (deafult [])
Examples
repo.tree('master', ['lib/'])
Returns Grit::Tree (baked)
295 296 297 |
# File 'lib/grit/repo.rb', line 295 def tree(treeish = 'master', paths = []) Tree.construct(self, treeish, paths) end |
#update_ref(head, commit_sha) ⇒ Object
486 487 488 489 490 |
# File 'lib/grit/repo.rb', line 486 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 |