Class: Gitrb::Repository
- Inherits:
-
Object
- Object
- Gitrb::Repository
- Defined in:
- lib/gitrb/repository.rb
Constant Summary collapse
- SHA_PATTERN =
/^[A-Fa-f0-9]{5,40}$/- REVISION_PATTERN =
/^[\w\-\.]+([\^~](\d+)?)*$/- DEFAULT_ENCODING =
'utf-8'- MIN_GIT_VERSION =
'1.6.0'
Instance Attribute Summary collapse
-
#branch ⇒ Object
Returns the value of attribute branch.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#bare? ⇒ Boolean
Bare repository?.
-
#changed? ⇒ Boolean
Has our repository been changed on disk?.
-
#clear ⇒ Object
Clear cached objects.
-
#commit(message = '', author = nil, committer = nil) ⇒ Object
Write a commit object to disk and set the head of the current branch.
- #default_user ⇒ Object
-
#diff(opts) ⇒ Object
Difference between versions Options: :to - Required target commit :from - Optional source commit (otherwise comparision with empty tree) :path - Restrict to path :detect_renames - Detect renames O(n^2) :detect_copies - Detect copies O(n^2), very slow.
-
#get(id) ⇒ Object
Get an object by its id.
- #get_blob(id) ⇒ Object
- #get_commit(id) ⇒ Object
- #get_tree(id) ⇒ Object
-
#in_transaction? ⇒ Boolean
Is there any transaction going on?.
-
#initialize(options = {}) ⇒ Repository
constructor
Initialize a repository.
-
#log(opts = {}) ⇒ Object
Returns a list of commits starting from head commit.
- #method_missing(name, *args, &block) ⇒ Object
-
#put(object) ⇒ Object
Write a raw object to the repository.
-
#refresh ⇒ Object
Load the repository, if it has been changed on disk.
- #set_encoding(s) ⇒ Object
-
#transaction(message = '', author = nil, committer = nil) ⇒ Object
All changes made inside a transaction are atomic.
Constructor Details
#initialize(options = {}) ⇒ Repository
Initialize a repository.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gitrb/repository.rb', line 32 def initialize( = {}) @bare = [:bare] || false @branch = [:branch] || 'master' @logger = [:logger] || Logger.new(nil) @encoding = [:encoding] || DEFAULT_ENCODING @lock = {} @transaction = Mutex.new @path = [:path] @path.chomp!('/') @path += '/.git' if !@bare check_git_version if ![:ignore_version] open_repository([:create]) load_packs load end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/gitrb/repository.rb', line 276 def method_missing(name, *args, &block) cmd = name.to_s if cmd[0..3] == 'git_' ENV['GIT_DIR'] = path cmd = cmd[4..-1].tr('_', '-') args = args.flatten.compact.map {|s| "'" + s.to_s.gsub("'", "'\\\\''") + "'" }.join(' ') cmdline = "git #{cmd} #{args} 2>&1" @logger.debug "gitrb: #{cmdline}" # Read in binary mode (ascii-8bit) and convert afterwards out = if block_given? IO.popen(cmdline, 'rb', &block) else set_encoding IO.popen(cmdline, 'rb') {|io| io.read } end if $?.exitstatus > 0 return '' if $?.exitstatus == 1 && out == '' raise CommandError.new("git #{cmd}", args, out) end out else super end end |
Instance Attribute Details
#branch ⇒ Object
Returns the value of attribute branch.
18 19 20 |
# File 'lib/gitrb/repository.rb', line 18 def branch @branch end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
18 19 20 |
# File 'lib/gitrb/repository.rb', line 18 def encoding @encoding end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
18 19 20 |
# File 'lib/gitrb/repository.rb', line 18 def head @head end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
18 19 20 |
# File 'lib/gitrb/repository.rb', line 18 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
18 19 20 |
# File 'lib/gitrb/repository.rb', line 18 def root @root end |
Instance Method Details
#bare? ⇒ Boolean
Bare repository?
52 53 54 |
# File 'lib/gitrb/repository.rb', line 52 def @bare end |
#changed? ⇒ Boolean
Has our repository been changed on disk?
65 66 67 |
# File 'lib/gitrb/repository.rb', line 65 def changed? !head || head.id != read_head_id end |
#clear ⇒ Object
Clear cached objects
75 76 77 78 79 80 |
# File 'lib/gitrb/repository.rb', line 75 def clear @transaction.synchronize do @objects.clear load end end |
#commit(message = '', author = nil, committer = nil) ⇒ Object
Write a commit object to disk and set the head of the current branch.
Returns the commit object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/gitrb/repository.rb', line 135 def commit( = '', = nil, committer = nil) return if !root.modified? ||= default_user committer ||= root.save commit = Commit.new(:repository => self, :tree => root, :parents => head, :author => , :committer => committer, :message => ) commit.save write_head_id(commit.id) load commit end |
#default_user ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/gitrb/repository.rb', line 304 def default_user name = git_config('user.name').chomp email = git_config('user.email').chomp if name.empty? require 'etc' user = Etc.getpwnam(Etc.getlogin) name = user.gecos end if email.empty? require 'etc' email = Etc.getlogin + '@' + `hostname -f`.chomp end User.new(name, email) end |
#diff(opts) ⇒ Object
Difference between versions Options:
:to - Required target commit
:from - Optional source commit (otherwise comparision with empty tree)
:path - Restrict to path
:detect_renames - Detect renames O(n^2)
:detect_copies - Detect copies O(n^2), very slow
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gitrb/repository.rb', line 94 def diff(opts) from, to = opts[:from], opts[:to] if from && !(Commit === from) raise ArgumentError, "Invalid sha: #{from}" if from !~ SHA_PATTERN from = Reference.new(:repository => self, :id => from) end if !(Commit === to) raise ArgumentError, "Invalid sha: #{to}" if to !~ SHA_PATTERN to = Reference.new(:repository => self, :id => to) end Diff.new(from, to, git_diff_tree('--root', '--full-index', '-u', opts[:detect_renames] ? '-M' : nil, opts[:detect_copies] ? '-C' : nil, from ? from.id : nil, to.id, '--', opts[:path])) end |
#get(id) ⇒ Object
Get an object by its id.
Returns a tree, blob, commit or tag object.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/gitrb/repository.rb', line 187 def get(id) raise ArgumentError, 'Invalid id given' if !(String === id) if id =~ SHA_PATTERN raise ArgumentError, "Sha too short: #{id}" if id.length < 5 trie = @objects.find(id) raise NotFound, "Sha is ambiguous: #{id}" if trie.size > 1 return trie.value if !trie.empty? elsif id =~ REVISION_PATTERN list = git_rev_parse(id).split("\n") rescue nil raise NotFound, "Revision not found: #{id}" if !list || list.empty? raise NotFound, "Revision is ambiguous: #{id}" if list.size > 1 id = list.first trie = @objects.find(id) raise NotFound, "Sha is ambiguous: #{id}" if trie.size > 1 return trie.value if !trie.empty? else raise ArgumentError, "Invalid id given: #{id}" end @logger.debug "gitrb: Loading #{id}" path = object_path(id) if File.exists?(path) || (glob = Dir.glob(path + '*')).size >= 1 if glob raise NotFound, "Sha is ambiguous: #{id}" if glob.size > 1 path = glob.first id = path[-41..-40] + path[-38..-1] end buf = File.open(path, 'rb') { |f| f.read } raise NotFound, "Not a loose object: #{id}" if !legacy_loose_object?(buf) header, content = Zlib::Inflate.inflate(buf).split("\0", 2) type, size = header.split(' ', 2) raise NotFound, "Bad object: #{id}" if content.length != size.to_i else trie = @packs.find(id) raise NotFound, "Object not found: #{id}" if trie.empty? raise NotFound, "Sha is ambiguous: #{id}" if trie.size > 1 id = trie.key pack, offset = trie.value content, type = pack.get_object(offset) end @logger.debug "gitrb: Loaded #{id}" set_encoding(id) object = GitObject.factory(type, :repository => self, :id => id, :data => content) @objects.insert(id, object) object end |
#get_blob(id) ⇒ Object
245 |
# File 'lib/gitrb/repository.rb', line 245 def get_blob(id) get_type(id, :blob) end |
#get_commit(id) ⇒ Object
246 |
# File 'lib/gitrb/repository.rb', line 246 def get_commit(id) get_type(id, :commit) end |
#get_tree(id) ⇒ Object
244 |
# File 'lib/gitrb/repository.rb', line 244 def get_tree(id) get_type(id, :tree) end |
#in_transaction? ⇒ Boolean
Is there any transaction going on?
83 84 85 |
# File 'lib/gitrb/repository.rb', line 83 def in_transaction? !!@lock[Thread.current.object_id] end |
#log(opts = {}) ⇒ Object
Returns a list of commits starting from head commit.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/gitrb/repository.rb', line 157 def log(opts = {}) max_count = opts[:max_count] skip = opts[:skip] start = opts[:start] path = opts[:path] raise ArgumentError, "Invalid commit: #{start}" if start.to_s =~ /^\-/ log = git_log('--pretty=tformat:%H%n%P%n%T%n%an%n%ae%n%at%n%cn%n%ce%n%ct%n%x00%s%n%b%x00', skip ? "--skip=#{skip.to_i}" : nil, max_count ? "--max-count=#{max_count.to_i}" : nil, start, '--', path).split(/\n*\x00\n*/) commits = [] log.each_slice(2) do |data, | data = data.split("\n") parents = data[1].empty? ? nil : data[1].split(' ').map {|id| Reference.new(:repository => self, :id => id) } commits << Commit.new(:repository => self, :id => data[0], :parents => parents, :tree => Reference.new(:repository => self, :id => data[2]), :author => User.new(data[3], data[4], Time.at(data[5].to_i)), :committer => User.new(data[6], data[7], Time.at(data[8].to_i)), :message => .strip) end commits rescue => ex return [] if ex. =~ /bad default revision 'HEAD'/i raise end |
#put(object) ⇒ Object
Write a raw object to the repository.
Returns the object.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/gitrb/repository.rb', line 251 def put(object) content = object.dump data = "#{object.type} #{content.bytesize rescue content.length}\0#{content}" id = sha(data) path = object_path(id) @logger.debug "gitrb: Storing #{id}" if !File.exists?(path) FileUtils.mkpath(File.dirname(path)) File.open(path, 'wb') do |f| f.write Zlib::Deflate.deflate(data) end end @logger.debug "gitrb: Stored #{id}" set_encoding(id) object.repository = self object.id = id @objects.insert(id, object) object end |
#refresh ⇒ Object
Load the repository, if it has been changed on disk.
70 71 72 |
# File 'lib/gitrb/repository.rb', line 70 def refresh load if changed? end |
#set_encoding(s) ⇒ Object
26 |
# File 'lib/gitrb/repository.rb', line 26 def set_encoding(s); s.force_encoding(@encoding); end |
#transaction(message = '', author = nil, committer = nil) ⇒ Object
All changes made inside a transaction are atomic. If some exception occurs the transaction will be rolled back.
Example:
repository.transaction { repository['a'] = 'b' }
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gitrb/repository.rb', line 116 def transaction( = '', = nil, committer = nil) @transaction.synchronize do begin start_transaction result = yield commit(, , committer) result rescue rollback_transaction raise ensure finish_transaction end end end |