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.
Class Method Summary collapse
-
.init_bare(path, options = {}) ⇒ Object
Initialize a bare git repository at the given path
path
is the full path to the repo (traditionally ends with /<name>.git)options
is any additional options to the git init command.
Instance Method Summary collapse
-
#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. -
#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_count(start = 'master') ⇒ Object
The number of commits reachable by the given branch/commit
start
is the branch/commit name (default ‘master’). -
#commit_diff(commit) ⇒ Object
The commit diff for the given commit
commit
is the commit name/id. -
#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)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
-
#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. -
#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
path
is the full path of the new repo (traditionally ends with /<name>.git)options
is any additional options to the git clone command. -
#has_branch?(branch) ⇒ Boolean
Boolean value if the branch specified is found in the repository.
-
#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) ⇒ Repo
constructor
Create a new Repo instance
path
is the path to either the root git directory or the bare git repo. -
#inspect ⇒ Object
Pretty object inspection.
-
#log(commit = 'master', path = nil, options = {}) ⇒ Object
The commit log for a treeish.
-
#refs ⇒ Object
An array of Ref objects representing the refs in this repo.
-
#remotes ⇒ Object
An array of Remote objects representing the remote branches in this repo.
-
#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 (deafult []).
Constructor Details
#initialize(path) ⇒ Repo
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/grit/repo.rb', line 21 def initialize(path) epath = File.(path) if File.exist?(File.join(epath, '.git')) self.path = File.join(epath, '.git') @bare = false elsif File.exist?(epath) && epath =~ /\.git$/ 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.
8 9 10 |
# File 'lib/grit/repo.rb', line 8 def @bare end |
#git ⇒ Object
The git command line interface object
11 12 13 |
# File 'lib/grit/repo.rb', line 11 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 |
Class Method Details
Instance Method Details
#alternates ⇒ Object
The list of alternates for this repo
Returns Array (pathnames of alternates)
281 282 283 284 285 286 287 288 289 |
# File 'lib/grit/repo.rb', line 281 def alternates alternates_path = File.join(self.path, *%w{objects info alternates}) if File.exist?(alternates_path) File.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
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/grit/repo.rb', line 295 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? File.delete(File.join(self.path, *%w{objects info alternates})) else File.open(File.join(self.path, *%w{objects info alternates}), 'w') do |f| f.write alts.join("\n") end 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)
235 236 237 238 239 |
# File 'lib/grit/repo.rb', line 235 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)
256 257 258 259 260 |
# File 'lib/grit/repo.rb', line 256 def archive_tar_gz(treeish = 'master', prefix = nil) = {} [:prefix] = prefix if prefix self.git.archive(, treeish, "| gzip") end |
#blob(id) ⇒ Object
The Blob object for the given id
+id+ is the SHA1 id of the blob
Returns Grit::Blob (unbaked)
163 164 165 |
# File 'lib/grit/repo.rb', line 163 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)
141 142 143 144 145 |
# File 'lib/grit/repo.rb', line 141 def commit(id) = {:max_count => 1} Commit.find_all(self, id, ).first 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
133 134 135 |
# File 'lib/grit/repo.rb', line 133 def commit_count(start = 'master') Commit.count(self, start) end |
#commit_diff(commit) ⇒ Object
The commit diff for the given commit
+commit+ is the commit name/id
Returns Grit::Diff[]
190 191 192 |
# File 'lib/grit/repo.rb', line 190 def commit_diff(commit) Commit.diff(self, commit) 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)
+skip+ is the number of commits to skip (default 0)
Returns Grit::Commit[] (baked)
99 100 101 102 103 104 |
# File 'lib/grit/repo.rb', line 99 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)
112 113 114 |
# File 'lib/grit/repo.rb', line 112 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)
123 124 125 126 127 |
# File 'lib/grit/repo.rb', line 123 def commits_since(start = 'master', since = '1970-01-01', = {}) = {:since => since}.merge() Commit.find_all(self, start, ) end |
#config ⇒ Object
311 312 313 |
# File 'lib/grit/repo.rb', line 311 def config @config ||= Config.new(self) end |
#description ⇒ Object
The project’s description. Taken verbatim from GIT_REPO/description
Returns String
42 43 44 |
# File 'lib/grit/repo.rb', line 42 def description File.open(File.join(self.path, 'description')).read.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
182 183 184 |
# File 'lib/grit/repo.rb', line 182 def diff(a, b, *paths) self.git.diff({}, a, b, '--', *paths) 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
274 275 276 |
# File 'lib/grit/repo.rb', line 274 def disable_daemon_serve FileUtils.rm_f(File.join(self.path, 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
266 267 268 |
# File 'lib/grit/repo.rb', line 266 def enable_daemon_serve FileUtils.touch(File.join(self.path, 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
Returns Grit::Repo (the newly forked repo)
213 214 215 216 217 218 |
# File 'lib/grit/repo.rb', line 213 def (path, = {}) = {:bare => true, :shared => true} = .merge() self.git.clone(, self.path, path) Repo.new(path) end |
#has_branch?(branch) ⇒ Boolean
Boolean value if the branch specified is found in the repository
Returns Boolean
89 90 91 |
# File 'lib/grit/repo.rb', line 89 def has_branch?(branch) self.branches.map(&:name).include?(branch) end |
#head ⇒ Object
Object reprsenting the current repo head.
Returns Grit::Head (baked)
59 60 61 |
# File 'lib/grit/repo.rb', line 59 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)
50 51 52 |
# File 'lib/grit/repo.rb', line 50 def heads Head.find_all(self) end |
#index ⇒ Object
315 316 317 |
# File 'lib/grit/repo.rb', line 315 def index Index.new(self) end |
#inspect ⇒ Object
Pretty object inspection
320 321 322 |
# File 'lib/grit/repo.rb', line 320 def inspect %Q{#<Grit::Repo "#{@path}">} end |
#log(commit = 'master', path = nil, options = {}) ⇒ Object
The commit log for a treeish
Returns Grit::Commit[]
170 171 172 173 174 175 176 |
# File 'lib/grit/repo.rb', line 170 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 |
#refs ⇒ Object
An array of Ref objects representing the refs in this repo
Returns Grit::Ref[] (baked)
82 83 84 |
# File 'lib/grit/repo.rb', line 82 def refs [ Head.find_all(self), Tag.find_all(self), Remote.find_all(self) ].flatten end |
#remotes ⇒ Object
An array of Remote objects representing the remote branches in this repo
Returns Grit::Remote[] (baked)
74 75 76 |
# File 'lib/grit/repo.rb', line 74 def remotes Remote.find_all(self) end |
#tags ⇒ Object
An array of Tag objects that are available in this repo
Returns Grit::Tag[] (baked)
66 67 68 |
# File 'lib/grit/repo.rb', line 66 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)
155 156 157 |
# File 'lib/grit/repo.rb', line 155 def tree(treeish = 'master', paths = []) Tree.construct(self, treeish, paths) end |