| |   |
| 0 | | module Grit |
| 1 | | HEAD_PREFIX = 'refs/heads' |
| 2 | | |
| 3 | | # A Head is a named reference to a Commit. Every Head instance contains a name |
| 4 | | # and a Commit object. |
| 5 | | # |
| 6 | | # r = Grit::Repo.new("/path/to/repo") |
| 7 | | # h = r.heads.first |
| 8 | | # h.name # => "master" |
| 9 | | # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455"> |
| 10 | | # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455" |
| 11 | | class Head |
| 12 | | attr_reader :name |
| 13 | | attr_reader :commit |
| 14 | | |
| 15 | | # Instantiate a new Head |
| 16 | | # +name+ is the name of the head |
| 17 | | # +commit+ is the Commit that the head points to |
| 18 | | # |
| 19 | | # Returns Grit::Head (baked) |
| 20 | | def initialize(name, commit) |
| 21 | | @name = name |
| 22 | | @commit = commit |
| 23 | | end |
| 24 | | |
| 25 | | # Find all Heads |
| 26 | | # +repo+ is the Repo |
| 27 | | # +options+ is a Hash of options |
| 28 | | # |
| 29 | | # Returns Grit::Head[] (baked) |
| 30 | | def self.find_all(repo, options = {}) |
| 31 | | default_options = {:sort => "committerdate", |
| 32 | | :format => "%(refname)%00%(objectname)"} |
| 33 | | |
| 34 | | actual_options = default_options.merge(options) |
| 35 | | |
| 36 | | output = repo.git.for_each_ref(actual_options, HEAD_PREFIX) |
| 37 | | |
| 38 | | self.list_from_string(repo, output) |
| 39 | | end |
| 40 | | |
| 41 | | # Get the HEAD revision of the repo. |
| 42 | | # +repo+ is the Repo |
| 43 | | # +options+ is a Hash of options |
| 44 | | # |
| 45 | | # Returns Grit::Head (baked) |
| 46 | | def self.current(repo, options = {}) |
| 47 | | head = File.open(File.join(repo.path, 'HEAD')).read.chomp |
| 48 | | if /ref: refs\/heads\/(.*)/.match(head) |
| 49 | | self.new($1, repo.git.rev_parse(options, 'HEAD')) |
| 50 | | end |
| 51 | | end |
| 52 | | |
| 53 | | # Parse out head information into an array of baked head objects |
| 54 | | # +repo+ is the Repo |
| 55 | | # +text+ is the text output from the git command |
| 56 | | # |
| 57 | | # Returns Grit::Head[] (baked) |
| 58 | | def self.list_from_string(repo, text) |
| 59 | | heads = [] |
| 60 | | |
| 61 | | text.split("\n").each do |line| |
| 62 | | heads << self.from_string(repo, line) |
| 63 | | end |
| 64 | | |
| 65 | | heads |
| 66 | | end |
| 67 | | |
| 68 | | # Create a new Head instance from the given string. |
| 69 | | # +repo+ is the Repo |
| 70 | | # +line+ is the formatted head information |
| 71 | | # |
| 72 | | # Format |
| 73 | | # name: [a-zA-Z_/]+ |
| 74 | | # <null byte> |
| 75 | | # id: [0-9A-Fa-f]{40} |
| 76 | | # |
| 77 | | # Returns Grit::Head (baked) |
| 78 | | def self.from_string(repo, line) |
| 79 | | full_name, id = line.split("\0") |
| 80 | | name = full_name.sub("#{HEAD_PREFIX}/", '') |
| 81 | | commit = Commit.create(repo, :id => id) |
| 82 | | self.new(name, commit) |
| 83 | | end |
| 84 | | |
| 85 | | # Pretty object inspection |
| 86 | | def inspect |
| 87 | | %Q{#<Grit::Head "#{@name}">} |
| 88 | | end |
| 89 | | end # Head |
| 90 | | |
| 91 | | end # Grit |
| toggle raw diff |
--- a/lib/grit/head.rb
+++ /dev/null
@@ -1,92 +0,0 @@
-module Grit
- HEAD_PREFIX = 'refs/heads'
-
- # A Head is a named reference to a Commit. Every Head instance contains a name
- # and a Commit object.
- #
- # r = Grit::Repo.new("/path/to/repo")
- # h = r.heads.first
- # h.name # => "master"
- # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455">
- # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455"
- class Head
- attr_reader :name
- attr_reader :commit
-
- # Instantiate a new Head
- # +name+ is the name of the head
- # +commit+ is the Commit that the head points to
- #
- # Returns Grit::Head (baked)
- def initialize(name, commit)
- @name = name
- @commit = commit
- end
-
- # Find all Heads
- # +repo+ is the Repo
- # +options+ is a Hash of options
- #
- # Returns Grit::Head[] (baked)
- def self.find_all(repo, options = {})
- default_options = {:sort => "committerdate",
- :format => "%(refname)%00%(objectname)"}
-
- actual_options = default_options.merge(options)
-
- output = repo.git.for_each_ref(actual_options, HEAD_PREFIX)
-
- self.list_from_string(repo, output)
- end
-
- # Get the HEAD revision of the repo.
- # +repo+ is the Repo
- # +options+ is a Hash of options
- #
- # Returns Grit::Head (baked)
- def self.current(repo, options = {})
- head = File.open(File.join(repo.path, 'HEAD')).read.chomp
- if /ref: refs\/heads\/(.*)/.match(head)
- self.new($1, repo.git.rev_parse(options, 'HEAD'))
- end
- end
-
- # Parse out head information into an array of baked head objects
- # +repo+ is the Repo
- # +text+ is the text output from the git command
- #
- # Returns Grit::Head[] (baked)
- def self.list_from_string(repo, text)
- heads = []
-
- text.split("\n").each do |line|
- heads << self.from_string(repo, line)
- end
-
- heads
- end
-
- # Create a new Head instance from the given string.
- # +repo+ is the Repo
- # +line+ is the formatted head information
- #
- # Format
- # name: [a-zA-Z_/]+
- # <null byte>
- # id: [0-9A-Fa-f]{40}
- #
- # Returns Grit::Head (baked)
- def self.from_string(repo, line)
- full_name, id = line.split("\0")
- name = full_name.sub("#{HEAD_PREFIX}/", '')
- commit = Commit.create(repo, :id => id)
- self.new(name, commit)
- end
-
- # Pretty object inspection
- def inspect
- %Q{#<Grit::Head "#{@name}">}
- end
- end # Head
-
-end # Grit |
| |   |
| 1 | module Grit |
| 2 | |
| 3 | class Ref |
| 4 | |
| 5 | class << self |
| 6 | |
| 7 | # Find all Refs |
| 8 | # +repo+ is the Repo |
| 9 | # +options+ is a Hash of options |
| 10 | # |
| 11 | # Returns Grit::Ref[] (baked) |
| 12 | def find_all(repo, options = {}) |
| 13 | default_options = {:sort => "committerdate", |
| 14 | :format => "%(refname)%00%(objectname)"} |
| 15 | |
| 16 | actual_options = default_options.merge(options) |
| 17 | |
| 18 | output = repo.git.for_each_ref(actual_options, prefix) |
| 19 | |
| 20 | self.list_from_string(repo, output) |
| 21 | end |
| 22 | |
| 23 | # Parse out ref information into an array of baked refs objects |
| 24 | # +repo+ is the Repo |
| 25 | # +text+ is the text output from the git command |
| 26 | # |
| 27 | # Returns Grit::Ref[] (baked) |
| 28 | def list_from_string(repo, text) |
| 29 | refs = [] |
| 30 | |
| 31 | text.split("\n").each do |line| |
| 32 | refs << self.from_string(repo, line) |
| 33 | end |
| 34 | |
| 35 | refs |
| 36 | end |
| 37 | |
| 38 | # Create a new Ref instance from the given string. |
| 39 | # +repo+ is the Repo |
| 40 | # +line+ is the formatted head information |
| 41 | # |
| 42 | # Format |
| 43 | # name: [a-zA-Z_/]+ |
| 44 | # <null byte> |
| 45 | # id: [0-9A-Fa-f]{40} |
| 46 | # |
| 47 | # Returns Grit::Ref (baked) |
| 48 | def from_string(repo, line) |
| 49 | full_name, id = line.split("\0") |
| 50 | name = full_name.sub("#{prefix}/", '') |
| 51 | commit = Commit.create(repo, :id => id) |
| 52 | self.new(name, commit) |
| 53 | end |
| 54 | |
| 55 | protected |
| 56 | |
| 57 | def prefix |
| 58 | "refs/#{name.to_s.gsub(/^.*::/, '').downcase}s" |
| 59 | end |
| 60 | |
| 61 | end |
| 62 | |
| 63 | attr_reader :name |
| 64 | attr_reader :commit |
| 65 | |
| 66 | # Instantiate a new Head |
| 67 | # +name+ is the name of the head |
| 68 | # +commit+ is the Commit that the head points to |
| 69 | # |
| 70 | # Returns Grit::Head (baked) |
| 71 | def initialize(name, commit) |
| 72 | @name = name |
| 73 | @commit = commit |
| 74 | end |
| 75 | |
| 76 | # Pretty object inspection |
| 77 | def inspect |
| 78 | %Q{#<#{self.class.name} "#{@name}">} |
| 79 | end |
| 80 | end # Ref |
| 81 | |
| 82 | # A Head is a named reference to a Commit. Every Head instance contains a name |
| 83 | # and a Commit object. |
| 84 | # |
| 85 | # r = Grit::Repo.new("/path/to/repo") |
| 86 | # h = r.heads.first |
| 87 | # h.name # => "master" |
| 88 | # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455"> |
| 89 | # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455" |
| 90 | class Head < Ref |
| 91 | |
| 92 | # Get the HEAD revision of the repo. |
| 93 | # +repo+ is the Repo |
| 94 | # +options+ is a Hash of options |
| 95 | # |
| 96 | # Returns Grit::Head (baked) |
| 97 | def self.current(repo, options = {}) |
| 98 | head = File.open(File.join(repo.path, 'HEAD')).read.chomp |
| 99 | if /ref: refs\/heads\/(.*)/.match(head) |
| 100 | self.new($1, repo.git.rev_parse(options, 'HEAD')) |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | end # Head |
| 105 | |
| 106 | class Tag < Ref ; end |
| 107 | |
| 108 | class Remote < Ref ; end |
| 109 | |
| 110 | end # Grit |
| toggle raw diff |
--- /dev/null
+++ b/lib/grit/ref.rb
@@ -0,0 +1,110 @@
+module Grit
+
+ class Ref
+
+ class << self
+
+ # Find all Refs
+ # +repo+ is the Repo
+ # +options+ is a Hash of options
+ #
+ # Returns Grit::Ref[] (baked)
+ def find_all(repo, options = {})
+ default_options = {:sort => "committerdate",
+ :format => "%(refname)%00%(objectname)"}
+
+ actual_options = default_options.merge(options)
+
+ output = repo.git.for_each_ref(actual_options, prefix)
+
+ self.list_from_string(repo, output)
+ end
+
+ # Parse out ref information into an array of baked refs objects
+ # +repo+ is the Repo
+ # +text+ is the text output from the git command
+ #
+ # Returns Grit::Ref[] (baked)
+ def list_from_string(repo, text)
+ refs = []
+
+ text.split("\n").each do |line|
+ refs << self.from_string(repo, line)
+ end
+
+ refs
+ end
+
+ # Create a new Ref instance from the given string.
+ # +repo+ is the Repo
+ # +line+ is the formatted head information
+ #
+ # Format
+ # name: [a-zA-Z_/]+
+ # <null byte>
+ # id: [0-9A-Fa-f]{40}
+ #
+ # Returns Grit::Ref (baked)
+ def from_string(repo, line)
+ full_name, id = line.split("\0")
+ name = full_name.sub("#{prefix}/", '')
+ commit = Commit.create(repo, :id => id)
+ self.new(name, commit)
+ end
+
+ protected
+
+ def prefix
+ "refs/#{name.to_s.gsub(/^.*::/, '').downcase}s"
+ end
+
+ end
+
+ attr_reader :name
+ attr_reader :commit
+
+ # Instantiate a new Head
+ # +name+ is the name of the head
+ # +commit+ is the Commit that the head points to
+ #
+ # Returns Grit::Head (baked)
+ def initialize(name, commit)
+ @name = name
+ @commit = commit
+ end
+
+ # Pretty object inspection
+ def inspect
+ %Q{#<#{self.class.name} "#{@name}">}
+ end
+ end # Ref
+
+ # A Head is a named reference to a Commit. Every Head instance contains a name
+ # and a Commit object.
+ #
+ # r = Grit::Repo.new("/path/to/repo")
+ # h = r.heads.first
+ # h.name # => "master"
+ # h.commit # => #<Grit::Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455">
+ # h.commit.id # => "1c09f116cbc2cb4100fb6935bb162daa4723f455"
+ class Head < Ref
+
+ # Get the HEAD revision of the repo.
+ # +repo+ is the Repo
+ # +options+ is a Hash of options
+ #
+ # Returns Grit::Head (baked)
+ def self.current(repo, options = {})
+ head = File.open(File.join(repo.path, 'HEAD')).read.chomp
+ if /ref: refs\/heads\/(.*)/.match(head)
+ self.new($1, repo.git.rev_parse(options, 'HEAD'))
+ end
+ end
+
+ end # Head
+
+ class Tag < Ref ; end
+
+ class Remote < Ref ; end
+
+end # Grit |
| |   |
| 1 | 1 | module Grit |
| 2 | | |
| 2 | |
| 3 | 3 | class Repo |
| 4 | 4 | DAEMON_EXPORT_FILE = 'git-daemon-export-ok' |
| 5 | | |
| 5 | |
| 6 | 6 | # The path of the git repo as a String |
| 7 | 7 | attr_accessor :path |
| 8 | 8 | attr_reader :bare |
| 9 | | |
| 9 | |
| 10 | 10 | # The git command line interface object |
| 11 | 11 | attr_accessor :git |
| 12 | | |
| 12 | |
| 13 | 13 | # Create a new Repo instance |
| 14 | 14 | # +path+ is the path to either the root git directory or the bare git repo |
| 15 | 15 | # |
| … | … | |
| 20 | 20 | # Returns Grit::Repo |
| 21 | 21 | def initialize(path) |
| 22 | 22 | epath = File.expand_path(path) |
| 23 | | |
| 23 | |
| 24 | 24 | if File.exist?(File.join(epath, '.git')) |
| 25 | 25 | self.path = File.join(epath, '.git') |
| 26 | 26 | @bare = false |
| … | … | |
| 32 | 32 | else |
| 33 | 33 | raise NoSuchPathError.new(epath) |
| 34 | 34 | end |
| 35 | | |
| 35 | |
| 36 | 36 | self.git = Git.new(self.path) |
| 37 | 37 | end |
| 38 | | |
| 38 | |
| 39 | 39 | # The project's description. Taken verbatim from GIT_REPO/description |
| 40 | 40 | # |
| 41 | 41 | # Returns String |
| 42 | 42 | def description |
| 43 | 43 | File.open(File.join(self.path, 'description')).read.chomp |
| 44 | 44 | end |
| 45 | | |
| 45 | |
| 46 | # An array of Ref objects representing the refs in |
| 47 | # this repo |
| 48 | # |
| 49 | # Returns Grit::Ref[] (baked) |
| 50 | def refs |
| 51 | [ Head.find_all(self), Tag.find_all(self), Remote.find_all(self) ].flatten |
| 52 | end |
| 53 | |
| 46 | 54 | # An array of Head objects representing the branch heads in |
| 47 | 55 | # this repo |
| 48 | 56 | # |
| … | … | |
| 58 | 58 | def heads |
| 59 | 59 | Head.find_all(self) |
| 60 | 60 | end |
| 61 | | |
| 61 | |
| 62 | 62 | alias_method :branches, :heads |
| 63 | 63 | |
| 64 | 64 | # Object reprsenting the current repo head. |
| … | … | |
| 74 | 74 | def tags |
| 75 | 75 | Tag.find_all(self) |
| 76 | 76 | end |
| 77 | | |
| 77 | |
| 78 | # An array of Remote objects representing the remote branches in |
| 79 | # this repo |
| 80 | # |
| 81 | # Returns Grit::Remote[] (baked) |
| 82 | def remotes |
| 83 | Remote.find_all(self) |
| 84 | end |
| 85 | |
| 78 | 86 | # An array of Commit objects representing the history of a given ref/commit |
| 79 | 87 | # +start+ is the branch/commit name (default 'master') |
| 80 | 88 | # +max_count+ is the maximum number of commits to return (default 10) |
| … | … | |
| 92 | 92 | def commits(start = 'master', max_count = 10, skip = 0) |
| 93 | 93 | options = {:max_count => max_count, |
| 94 | 94 | :skip => skip} |
| 95 | | |
| 95 | |
| 96 | 96 | Commit.find_all(self, start, options) |
| 97 | 97 | end |
| 98 | | |
| 98 | |
| 99 | 99 | # The Commits objects that are reachable via +to+ but not via +from+ |
| 100 | 100 | # Commits are returned in chronological order. |
| 101 | 101 | # +from+ is the branch/commit name of the younger item |
| … | … | |
| 105 | 105 | def commits_between(from, to) |
| 106 | 106 | Commit.find_all(self, "#{from}..#{to}").reverse |
| 107 | 107 | end |
| 108 | | |
| 108 | |
| 109 | 109 | # The Commits objects that are newer than the specified date. |
| 110 | 110 | # Commits are returned in chronological order. |
| 111 | 111 | # +start+ is the branch/commit name (default 'master') |
| … | … | |
| 115 | 115 | # Returns Grit::Commit[] (baked) |
| 116 | 116 | def commits_since(start = 'master', since = '1970-01-01', extra_options = {}) |
| 117 | 117 | options = {:since => since}.merge(extra_options) |
| 118 | | |
| 118 | |
| 119 | 119 | Commit.find_all(self, start, options) |
| 120 | 120 | end |
| 121 | | |
| 121 | |
| 122 | 122 | # The number of commits reachable by the given branch/commit |
| 123 | 123 | # +start+ is the branch/commit name (default 'master') |
| 124 | 124 | # |
| … | … | |
| 126 | 126 | def commit_count(start = 'master') |
| 127 | 127 | Commit.count(self, start) |
| 128 | 128 | end |
| 129 | | |
| 129 | |
| 130 | 130 | # The Commit object for the specified id |
| 131 | 131 | # +id+ is the SHA1 identifier of the commit |
| 132 | 132 | # |
| 133 | 133 | # Returns Grit::Commit (baked) |
| 134 | 134 | def commit(id) |
| 135 | 135 | options = {:max_count => 1} |
| 136 | | |
| 136 | |
| 137 | 137 | Commit.find_all(self, id, options).first |
| 138 | 138 | end |
| 139 | | |
| 139 | |
| 140 | 140 | # The Tree object for the given treeish reference |
| 141 | 141 | # +treeish+ is the reference (default 'master') |
| 142 | 142 | # +paths+ is an optional Array of directory paths to restrict the tree (deafult []) |
| … | … | |
| 148 | 148 | def tree(treeish = 'master', paths = []) |
| 149 | 149 | Tree.construct(self, treeish, paths) |
| 150 | 150 | end |
| 151 | | |
| 151 | |
| 152 | 152 | # The Blob object for the given id |
| 153 | 153 | # +id+ is the SHA1 id of the blob |
| 154 | 154 | # |
| … | … | |
| 167 | 167 | commits = self.git.log(actual_options, *arg) |
| 168 | 168 | Commit.list_from_string(self, commits) |
| 169 | 169 | end |
| 170 | | |
| 170 | |
| 171 | 171 | # The diff from commit +a+ to commit +b+, optionally restricted to the given file(s) |
| 172 | 172 | # +a+ is the base commit |
| 173 | 173 | # +b+ is the other commit |
| … | … | |
| 175 | 175 | def diff(a, b, *paths) |
| 176 | 176 | self.git.diff({}, a, b, '--', *paths) |
| 177 | 177 | end |
| 178 | | |
| 178 | |
| 179 | 179 | # The commit diff for the given commit |
| 180 | 180 | # +commit+ is the commit name/id |
| 181 | 181 | # |
| … | … | |
| 183 | 183 | def commit_diff(commit) |
| 184 | 184 | Commit.diff(self, commit) |
| 185 | 185 | end |
| 186 | | |
| 186 | |
| 187 | 187 | # Initialize a bare git repository at the given path |
| 188 | 188 | # +path+ is the full path to the repo (traditionally ends with /<name>.git) |
| 189 | 189 | # +options+ is any additional options to the git init command |
| … | … | |
| 197 | 197 | git.init(options) |
| 198 | 198 | self.new(path) |
| 199 | 199 | end |
| 200 | | |
| 200 | |
| 201 | 201 | # Fork a bare git repository from this repo |
| 202 | 202 | # +path+ is the full path of the new repo (traditionally ends with /<name>.git) |
| 203 | 203 | # +options+ is any additional options to the git clone command |
| … | … | |
| 209 | 209 | self.git.clone(real_options, self.path, path) |
| 210 | 210 | Repo.new(path) |
| 211 | 211 | end |
| 212 | | |
| 212 | |
| 213 | 213 | # Archive the given treeish |
| 214 | 214 | # +treeish+ is the treeish name/id (default 'master') |
| 215 | 215 | # +prefix+ is the optional prefix |
| … | … | |
| 230 | 230 | options[:prefix] = prefix if prefix |
| 231 | 231 | self.git.archive(options, treeish) |
| 232 | 232 | end |
| 233 | | |
| 233 | |
| 234 | 234 | # Archive and gzip the given treeish |
| 235 | 235 | # +treeish+ is the treeish name/id (default 'master') |
| 236 | 236 | # +prefix+ is the optional prefix |
| … | … | |
| 251 | 251 | options[:prefix] = prefix if prefix |
| 252 | 252 | self.git.archive(options, treeish, "| gzip") |
| 253 | 253 | end |
| 254 | | |
| 254 | |
| 255 | 255 | # Enable git-daemon serving of this repository by writing the |
| 256 | 256 | # git-daemon-export-ok file to its git directory |
| 257 | 257 | # |
| … | … | |
| 259 | 259 | def enable_daemon_serve |
| 260 | 260 | FileUtils.touch(File.join(self.path, DAEMON_EXPORT_FILE)) |
| 261 | 261 | end |
| 262 | | |
| 262 | |
| 263 | 263 | # Disable git-daemon serving of this repository by ensuring there is no |
| 264 | 264 | # git-daemon-export-ok file in its git directory |
| 265 | 265 | # |
| … | … | |
| 267 | 267 | def disable_daemon_serve |
| 268 | 268 | FileUtils.rm_f(File.join(self.path, DAEMON_EXPORT_FILE)) |
| 269 | 269 | end |
| 270 | | |
| 270 | |
| 271 | 271 | # The list of alternates for this repo |
| 272 | 272 | # |
| 273 | 273 | # Returns Array[String] (pathnames of alternates) |
| 274 | 274 | def alternates |
| 275 | 275 | alternates_path = File.join(self.path, *%w{objects info alternates}) |
| 276 | | |
| 276 | |
| 277 | 277 | if File.exist?(alternates_path) |
| 278 | 278 | File.read(alternates_path).strip.split("\n") |
| 279 | 279 | else |
| 280 | 280 | [] |
| 281 | 281 | end |
| 282 | 282 | end |
| 283 | | |
| 283 | |
| 284 | 284 | # Sets the alternates |
| 285 | 285 | # +alts+ is the Array of String paths representing the alternates |
| 286 | 286 | # |
| … | … | |
| 291 | 291 | raise "Could not set alternates. Alternate path #{alt} must exist" |
| 292 | 292 | end |
| 293 | 293 | end |
| 294 | | |
| 294 | |
| 295 | 295 | if alts.empty? |
| 296 | 296 | File.delete(File.join(self.path, *%w{objects info alternates})) |
| 297 | 297 | else |
| … | … | |
| 300 | 300 | end |
| 301 | 301 | end |
| 302 | 302 | end |
| 303 | | |
| 303 | |
| 304 | 304 | def config |
| 305 | 305 | @config ||= Config.new(self) |
| 306 | 306 | end |
| 307 | | |
| 307 | |
| 308 | 308 | # Pretty object inspection |
| 309 | 309 | def inspect |
| 310 | 310 | %Q{#<Grit::Repo "#{@path}">} |
| 311 | 311 | end |
| 312 | 312 | end # Repo |
| 313 | | |
| 313 | |
| 314 | 314 | end # Grit |
| toggle raw diff |
--- a/lib/grit/repo.rb
+++ b/lib/grit/repo.rb
@@ -1,15 +1,15 @@
module Grit
-
+
class Repo
DAEMON_EXPORT_FILE = 'git-daemon-export-ok'
-
+
# The path of the git repo as a String
attr_accessor :path
attr_reader :bare
-
+
# The git command line interface object
attr_accessor :git
-
+
# Create a new Repo instance
# +path+ is the path to either the root git directory or the bare git repo
#
@@ -20,7 +20,7 @@ module Grit
# Returns Grit::Repo
def initialize(path)
epath = File.expand_path(path)
-
+
if File.exist?(File.join(epath, '.git'))
self.path = File.join(epath, '.git')
@bare = false
@@ -32,17 +32,25 @@ module Grit
else
raise NoSuchPathError.new(epath)
end
-
+
self.git = Git.new(self.path)
end
-
+
# The project's description. Taken verbatim from GIT_REPO/description
#
# Returns String
def description
File.open(File.join(self.path, 'description')).read.chomp
end
-
+
+ # An array of Ref objects representing the refs in
+ # this repo
+ #
+ # Returns Grit::Ref[] (baked)
+ def refs
+ [ Head.find_all(self), Tag.find_all(self), Remote.find_all(self) ].flatten
+ end
+
# An array of Head objects representing the branch heads in
# this repo
#
@@ -50,7 +58,7 @@ module Grit
def heads
Head.find_all(self)
end
-
+
alias_method :branches, :heads
# Object reprsenting the current repo head.
@@ -66,7 +74,15 @@ module Grit
def tags
Tag.find_all(self)
end
-
+
+ # An array of Remote objects representing the remote branches in
+ # this repo
+ #
+ # Returns Grit::Remote[] (baked)
+ def remotes
+ Remote.find_all(self)
+ end
+
# 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)
@@ -76,10 +92,10 @@ module Grit
def commits(start = 'master', max_count = 10, skip = 0)
options = {:max_count => max_count,
:skip => skip}
-
+
Commit.find_all(self, start, options)
end
-
+
# 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
@@ -89,7 +105,7 @@ module Grit
def commits_between(from, to)
Commit.find_all(self, "#{from}..#{to}").reverse
end
-
+
# The Commits objects that are newer than the specified date.
# Commits are returned in chronological order.
# +start+ is the branch/commit name (default 'master')
@@ -99,10 +115,10 @@ module Grit
# Returns Grit::Commit[] (baked)
def commits_since(start = 'master', since = '1970-01-01', extra_options = {})
options = {:since => since}.merge(extra_options)
-
+
Commit.find_all(self, start, options)
end
-
+
# The number of commits reachable by the given branch/commit
# +start+ is the branch/commit name (default 'master')
#
@@ -110,17 +126,17 @@ module Grit
def commit_count(start = 'master')
Commit.count(self, start)
end
-
+
# The Commit object for the specified id
# +id+ is the SHA1 identifier of the commit
#
# Returns Grit::Commit (baked)
def commit(id)
options = {:max_count => 1}
-
+
Commit.find_all(self, id, options).first
end
-
+
# 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 [])
@@ -132,7 +148,7 @@ module Grit
def tree(treeish = 'master', paths = [])
Tree.construct(self, treeish, paths)
end
-
+
# The Blob object for the given id
# +id+ is the SHA1 id of the blob
#
@@ -151,7 +167,7 @@ module Grit
commits = self.git.log(actual_options, *arg)
Commit.list_from_string(self, commits)
end
-
+
# 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
@@ -159,7 +175,7 @@ module Grit
def diff(a, b, *paths)
self.git.diff({}, a, b, '--', *paths)
end
-
+
# The commit diff for the given commit
# +commit+ is the commit name/id
#
@@ -167,7 +183,7 @@ module Grit
def commit_diff(commit)
Commit.diff(self, commit)
end
-
+
# 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
@@ -181,7 +197,7 @@ module Grit
git.init(options)
self.new(path)
end
-
+
# 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
@@ -193,7 +209,7 @@ module Grit
self.git.clone(real_options, self.path, path)
Repo.new(path)
end
-
+
# Archive the given treeish
# +treeish+ is the treeish name/id (default 'master')
# +prefix+ is the optional prefix
@@ -214,7 +230,7 @@ module Grit
options[:prefix] = prefix if prefix
self.git.archive(options, treeish)
end
-
+
# Archive and gzip the given treeish
# +treeish+ is the treeish name/id (default 'master')
# +prefix+ is the optional prefix
@@ -235,7 +251,7 @@ module Grit
options[:prefix] = prefix if prefix
self.git.archive(options, treeish, "| gzip")
end
-
+
# Enable git-daemon serving of this repository by writing the
# git-daemon-export-ok file to its git directory
#
@@ -243,7 +259,7 @@ module Grit
def enable_daemon_serve
FileUtils.touch(File.join(self.path, DAEMON_EXPORT_FILE))
end
-
+
# Disable git-daemon serving of this repository by ensuring there is no
# git-daemon-export-ok file in its git directory
#
@@ -251,20 +267,20 @@ module Grit
def disable_daemon_serve
FileUtils.rm_f(File.join(self.path, DAEMON_EXPORT_FILE))
end
-
+
# The list of alternates for this repo
#
# Returns Array[String] (pathnames of alternates)
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
-
+
# Sets the alternates
# +alts+ is the Array of String paths representing the alternates
#
@@ -275,7 +291,7 @@ module Grit
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
@@ -284,15 +300,15 @@ module Grit
end
end
end
-
+
def config
@config ||= Config.new(self)
end
-
+
# Pretty object inspection
def inspect
%Q{#<Grit::Repo "#{@path}">}
end
end # Repo
-
+
end # Grit |
| |   |
| 0 | | module Grit |
| 1 | | |
| 2 | | class Tag |
| 3 | | attr_reader :name |
| 4 | | attr_reader :commit |
| 5 | | |
| 6 | | # Instantiate a new Tag |
| 7 | | # +name+ is the name of the head |
| 8 | | # +commit+ is the Commit that the head points to |
| 9 | | # |
| 10 | | # Returns Grit::Tag (baked) |
| 11 | | def initialize(name, commit) |
| 12 | | @name = name |
| 13 | | @commit = commit |
| 14 | | end |
| 15 | | |
| 16 | | # Find all Tags |
| 17 | | # +repo+ is the Repo |
| 18 | | # +options+ is a Hash of options |
| 19 | | # |
| 20 | | # Returns Grit::Tag[] (baked) |
| 21 | | def self.find_all(repo, options = {}) |
| 22 | | default_options = {:sort => "committerdate", |
| 23 | | :format => "%(refname)%00%(objectname)"} |
| 24 | | |
| 25 | | actual_options = default_options.merge(options) |
| 26 | | |
| 27 | | output = repo.git.for_each_ref(actual_options, "refs/tags") |
| 28 | | |
| 29 | | self.list_from_string(repo, output) |
| 30 | | end |
| 31 | | |
| 32 | | # Parse out tag information into an array of baked Tag objects |
| 33 | | # +repo+ is the Repo |
| 34 | | # +text+ is the text output from the git command |
| 35 | | # |
| 36 | | # Returns Grit::Tag[] (baked) |
| 37 | | def self.list_from_string(repo, text) |
| 38 | | tags = [] |
| 39 | | |
| 40 | | text.split("\n").each do |line| |
| 41 | | tags << self.from_string(repo, line) |
| 42 | | end |
| 43 | | |
| 44 | | tags |
| 45 | | end |
| 46 | | |
| 47 | | # Create a new Tag instance from the given string. |
| 48 | | # +repo+ is the Repo |
| 49 | | # +line+ is the formatted tag information |
| 50 | | # |
| 51 | | # Format |
| 52 | | # name: [a-zA-Z_/]+ |
| 53 | | # <null byte> |
| 54 | | # id: [0-9A-Fa-f]{40} |
| 55 | | # |
| 56 | | # Returns Grit::Tag (baked) |
| 57 | | def self.from_string(repo, line) |
| 58 | | full_name, id = line.split("\0") |
| 59 | | name = full_name.split("/").last |
| 60 | | commit = Commit.create(repo, :id => id) |
| 61 | | self.new(name, commit) |
| 62 | | end |
| 63 | | |
| 64 | | # Pretty object inspection |
| 65 | | def inspect |
| 66 | | %Q{#<Grit::Tag "#{@name}">} |
| 67 | | end |
| 68 | | end # Tag |
| 69 | | |
| 70 | | end # Grit |
| toggle raw diff |
--- a/lib/grit/tag.rb
+++ /dev/null
@@ -1,71 +0,0 @@
-module Grit
-
- class Tag
- attr_reader :name
- attr_reader :commit
-
- # Instantiate a new Tag
- # +name+ is the name of the head
- # +commit+ is the Commit that the head points to
- #
- # Returns Grit::Tag (baked)
- def initialize(name, commit)
- @name = name
- @commit = commit
- end
-
- # Find all Tags
- # +repo+ is the Repo
- # +options+ is a Hash of options
- #
- # Returns Grit::Tag[] (baked)
- def self.find_all(repo, options = {})
- default_options = {:sort => "committerdate",
- :format => "%(refname)%00%(objectname)"}
-
- actual_options = default_options.merge(options)
-
- output = repo.git.for_each_ref(actual_options, "refs/tags")
-
- self.list_from_string(repo, output)
- end
-
- # Parse out tag information into an array of baked Tag objects
- # +repo+ is the Repo
- # +text+ is the text output from the git command
- #
- # Returns Grit::Tag[] (baked)
- def self.list_from_string(repo, text)
- tags = []
-
- text.split("\n").each do |line|
- tags << self.from_string(repo, line)
- end
-
- tags
- end
-
- # Create a new Tag instance from the given string.
- # +repo+ is the Repo
- # +line+ is the formatted tag information
- #
- # Format
- # name: [a-zA-Z_/]+
- # <null byte>
- # id: [0-9A-Fa-f]{40}
- #
- # Returns Grit::Tag (baked)
- def self.from_string(repo, line)
- full_name, id = line.split("\0")
- name = full_name.split("/").last
- commit = Commit.create(repo, :id => id)
- self.new(name, commit)
- end
-
- # Pretty object inspection
- def inspect
- %Q{#<Grit::Tag "#{@name}">}
- end
- end # Tag
-
-end # Grit
\ No newline at end of file |