graal 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Graal
1
+ Graal [![Build Status](https://travis-ci.org/aumgn/graal.png)](https://travis-ci.org/aumgn/graal.png)
2
2
  =============
3
3
 
4
4
  Simple, read-only (for now), abstraction layer to access git repository
@@ -4,7 +4,11 @@ require 'graal/repository'
4
4
 
5
5
  module Graal
6
6
 
7
- def self.Repository(repository_path)
8
- Repository.new repository_path
7
+ class << self
8
+
9
+ def Repository(repository_path)
10
+ Repository.new repository_path
11
+ end
12
+ alias Repo Repository
9
13
  end
10
14
  end
@@ -0,0 +1,16 @@
1
+ module Graal
2
+
3
+ class Actor
4
+
5
+ attr_reader :name, :email
6
+
7
+ def initialize(name, email)
8
+ @name = name
9
+ @email = email
10
+ end
11
+
12
+ def header_line
13
+ "#@name <#@email>"
14
+ end
15
+ end
16
+ end
@@ -5,7 +5,7 @@ module Graal
5
5
  class Blob < TreeBlob
6
6
 
7
7
  def contents
8
- @backend.blob_contents(hash)
8
+ @backend.blob_contents(gitid)
9
9
  end
10
10
  end
11
11
  end
@@ -1,5 +1,6 @@
1
1
  require 'graal/tree'
2
2
  require 'graal/blob'
3
+ require 'graal/actor'
3
4
 
4
5
  module Graal
5
6
 
@@ -8,5 +9,39 @@ module Graal
8
9
  def initialize(backend, revision)
9
10
  super(backend, revision, '')
10
11
  end
12
+
13
+ def parent
14
+ Commit.new(@backend, info[0])
15
+ end
16
+
17
+ def author
18
+ @author ||= Actor.new(info[1], info[2])
19
+ end
20
+
21
+ def author_date
22
+ @author_date ||= Time.at(info[3].to_i)
23
+ end
24
+
25
+ def committer
26
+ @committer ||= Actor.new(info[4], info[5])
27
+ end
28
+
29
+ def committer_date
30
+ @committer_date ||= Time.at(info[6].to_i)
31
+ end
32
+
33
+ def subject
34
+ @subject ||= info[7]
35
+ end
36
+
37
+ def body
38
+ @body ||= info.length > 8 ? info[8] : ''
39
+ end
40
+
41
+ private
42
+
43
+ def info
44
+ @info ||= @backend.commit_info(@revision)
45
+ end
11
46
  end
12
47
  end
@@ -5,3 +5,4 @@ module Graal::Backend
5
5
  end
6
6
 
7
7
  require_relative 'git'
8
+ require_relative 'git_ruby'
@@ -4,6 +4,13 @@ module Graal::Backend::Exe
4
4
 
5
5
  class Git
6
6
 
7
+ COMMIT_SHOW_FORMAT = '--pretty=format:' + [
8
+ '%P',
9
+ '%an', '%ae', '%at',
10
+ '%cn', '%ce', '%ct',
11
+ '%s', '%-b'
12
+ ] * '%n'
13
+
7
14
  class Result
8
15
 
9
16
  attr_reader :exit_code, :output, :error
@@ -31,7 +38,7 @@ module Graal::Backend::Exe
31
38
 
32
39
  def run_with_pathspec(cmd, *args, pathspec)
33
40
  command_line = ['git', cmd, *args]
34
- unless pathspec.nil?
41
+ unless pathspec.nil? or pathspec.empty?
35
42
  command_line << '--'
36
43
  command_line << pathspec
37
44
  end
@@ -48,7 +55,7 @@ module Graal::Backend::Exe
48
55
  result.output.split("\n")
49
56
  end
50
57
 
51
- def hash_for(revision, path)
58
+ def gitid_for(revision, path)
52
59
  if path.empty?
53
60
  result = run('rev-parse', revision).output.chomp
54
61
  else
@@ -57,27 +64,47 @@ module Graal::Backend::Exe
57
64
  end
58
65
  end
59
66
 
60
- def child(hash, fullpath)
61
- result = run('ls-tree', hash, fullpath)
67
+ def commit_info(revision)
68
+ result = run('show', '--quiet', COMMIT_SHOW_FORMAT, revision)
69
+ ary = result.output.split("\n")
70
+ ary[8] = ary[8..-1] * "\n" if ary.size > 9
71
+ ary
72
+ end
73
+
74
+ def log(revision, path, raw_opts = {})
75
+ opts = parse_opts(raw_opts)
76
+ result = run_with_pathspec('rev-list', *opts, revision, path)
77
+ result.output.split("\n")
78
+ end
79
+
80
+ def child(gitid, fullpath)
81
+ result = run('ls-tree', gitid, fullpath)
62
82
  return result.output.split(" ")[1..2]
63
83
  end
64
84
 
65
- def children(hash, path)
66
- gitpath = path
67
- gitpath += '/' unless path.empty?
68
- result = run('ls-tree', hash, gitpath)
69
- return result.output.split("\n").map do |line|
85
+ def children(gitid, path, filter = nil)
86
+ result = run('ls-tree', gitid, path)
87
+ children = []
88
+ result.output.split("\n").each do |line|
70
89
  splitted = line.split(" ")
71
- [splitted[1], splitted[3], splitted[2]]
90
+ next unless filter.nil? or splitted[1] == filter
91
+ children << [splitted[1], splitted[3], splitted[2]]
72
92
  end
93
+ children
73
94
  end
74
95
 
75
- def blob_contents(hash)
76
- run('cat-file', '-p', hash).output
96
+ def blob_contents(gitid)
97
+ run('cat-file', '-p', gitid).output
77
98
  end
78
99
 
79
100
  private
80
101
 
102
+ def parse_opts(opts)
103
+ opts.each_with_object([]) do |opt, array|
104
+ array << '--' + (opt[1] == true ? opt[0] : opt * '=')
105
+ end
106
+ end
107
+
81
108
  def do_run(command_line)
82
109
  process = ChildProcess.build(*command_line)
83
110
  process.cwd = @repository
@@ -0,0 +1,41 @@
1
+ module Graal::Backend::Exe
2
+
3
+ class Git
4
+
5
+ def heads
6
+ refs('heads')
7
+ end
8
+
9
+ def tags
10
+ refs('tags')
11
+ end
12
+
13
+ private
14
+
15
+ def git_directory
16
+ live = File.join(@repository, '.git')
17
+ return live if File.exists? live
18
+ return @repository
19
+ end
20
+
21
+ def refs(type)
22
+ refs = []
23
+ git_dir = git_directory
24
+
25
+ refs_dir = File.join(git_dir, 'refs', type)
26
+ if File.exists? refs_dir
27
+ Dir.chdir refs_dir do
28
+ Dir['*'].each { |ref| refs << ref if File.file?(ref) }
29
+ end
30
+ end
31
+
32
+ packed = File.join(git_dir, 'packed-refs')
33
+ if File.exists? packed
34
+ pattern = %r{ refs/#{type}/(.+)$}
35
+ File.read(packed).scan(pattern).each { |m| refs << m.first }
36
+ end
37
+
38
+ refs
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,60 @@
1
+ module Graal
2
+
3
+ class Log < Enumerator
4
+
5
+ def initialize(backend, revision, path, opts = {})
6
+ super() do |yielder|
7
+ commits_id = @backend.log(revision, path, opts)
8
+ commits_id.each { |cid| yielder << Commit.new(@backend, cid) }
9
+ end
10
+
11
+ @backend = backend
12
+ @revision = revision
13
+ @path = path
14
+ @opts = opts
15
+ end
16
+
17
+ def number(n)
18
+ push_opt('max-count', n)
19
+ end
20
+ alias n number
21
+ alias max_count number
22
+
23
+ def skip(n)
24
+ push_opt('skip', n)
25
+ end
26
+
27
+ def since(time)
28
+ push_opt('since', time_pattern_for(time))
29
+ end
30
+ alias after since
31
+
32
+ def until(time)
33
+ push_opt('until', time_pattern_for(time))
34
+ end
35
+ alias before until
36
+
37
+ def author(actor)
38
+ push_opt('author', actor_pattern_for(actor))
39
+ end
40
+
41
+ def committer(actor)
42
+ push_opt('committer', actor_pattern_for(actor))
43
+ end
44
+
45
+ private
46
+
47
+ def push_opt(name, value = true)
48
+ Log.new(@backend, @revision, @path, @opts.merge(name => value))
49
+ end
50
+
51
+ def time_pattern_for(time_or_pattern)
52
+ return time_or_pattern.to_i if time_or_pattern.is_a? Time
53
+ end
54
+
55
+ def actor_pattern_for(actor_or_pattern)
56
+ return actor_or_pattern.header_line if actor_or_pattern.is_a? Actor
57
+ actor_or_pattern
58
+ end
59
+ end
60
+ end
@@ -8,49 +8,44 @@ module Graal
8
8
  fullpath = File.join(@path, child_path)
9
9
  fullpath[ 0] = '' if fullpath[ 0] == ?/
10
10
  fullpath[-1] = '' if fullpath[-1] == ?/
11
- type, child_hash = @backend.child(hash, fullpath)
12
- child_for(type, fullpath, child_hash)
11
+ type, child_gitid = @backend.child(gitid, fullpath)
12
+ child_for(type, fullpath, child_gitid)
13
13
  end
14
14
  alias [] child
15
15
  alias / child
16
16
 
17
- def children
18
- return Enumerator.new(self, :children) unless block_given?
19
- @backend.children(hash, @path).each { |data| yield child_for(*data) }
20
- nil
17
+ def children(&block)
18
+ enum_children(:children, &block)
21
19
  end
22
20
  alias each_child children
23
21
  alias list children
24
22
  alias ls children
25
23
 
26
- def trees
27
- return Enumerator.new(self, :trees) unless block_given?
28
- children = @backend.children(hash, @path).each do |data|
29
- yield child_for(*data) if data[0] == 'tree'
30
- end
31
- nil
24
+ def trees(&block)
25
+ enum_children(:trees, 'tree', &block)
32
26
  end
33
27
  alias each_tree trees
34
28
  alias list_trees trees
35
29
 
36
- def blobs
37
- return Enumerator.new(self, :blobs) unless block_given?
38
- @backend.children(hash, @path).each do |data|
39
- yield child_for(*data) if data[0] == 'blob'
40
- end
41
- nil
30
+ def blobs(&block)
31
+ enum_children(:blobs, 'blob', &block)
42
32
  end
43
33
  alias each_blob blobs
44
34
  alias list_blobs blobs
45
35
 
46
36
  private
47
37
 
48
- def child_for(type, fullpath, hash)
49
- if type == 'tree'
50
- Tree.new(@backend, @revision, fullpath, hash)
51
- else
52
- Blob.new(@backend, @revision, fullpath, hash)
53
- end
38
+ def enum_children(method, filter = nil)
39
+ dirpath = @path.empty? ? path : path + '/'
40
+ @backend.children(gitid, dirpath, filter).each do |data|
41
+ yield child_for(*data)
42
+ end if block_given?
43
+ Enumerator.new(self, method)
44
+ end
45
+
46
+ def child_for(type, fullpath, gitid)
47
+ return Blob.new(@backend, @revision, fullpath, gitid) if type == 'blob'
48
+ Tree.new(@backend, @revision, fullpath, gitid)
54
49
  end
55
50
  end
56
51
  end
@@ -1,18 +1,25 @@
1
+ require 'graal/log'
2
+
1
3
  module Graal
2
4
 
3
5
  class TreeBlob
4
6
 
5
7
  attr_reader :revision, :path
6
8
 
7
- def initialize(backend, revision, path, hash = nil)
9
+ def initialize(backend, revision, path, gitid = nil)
8
10
  @backend = backend
9
11
  @revision = revision || 'master'
10
12
  @path = path || ''
11
- @hash = hash
13
+ @gitid = gitid
14
+ end
15
+
16
+ def gitid
17
+ @gitid ||= @backend.gitid_for(@revision, @path)
12
18
  end
19
+ alias git_hash gitid
13
20
 
14
- def hash
15
- @hash ||= @backend.hash_for(@revision, @path)
21
+ def abbrev(n = 8)
22
+ gitid[0...n]
16
23
  end
17
24
 
18
25
  def tree?
@@ -22,5 +29,16 @@ module Graal
22
29
  def blob?
23
30
  is_a? Blob
24
31
  end
32
+
33
+ def log
34
+ Log.new(@backend, @revision, @path)
35
+ end
36
+
37
+ def history(n = nil, skip = nil)
38
+ history = log
39
+ history = log.number(n) unless n.nil?
40
+ history = log.skip(skip) unless skip.nil?
41
+ history.to_a
42
+ end
25
43
  end
26
44
  end
@@ -1,4 +1,4 @@
1
1
  module Graal
2
2
 
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
12
+ date: 2012-11-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: childprocess
@@ -27,38 +27,6 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.3.6
30
- - !ruby/object:Gem::Dependency
31
- name: rake
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: 10.0.2
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 10.0.2
46
- - !ruby/object:Gem::Dependency
47
- name: pry
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: 0.9.10
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 0.9.10
62
30
  description: ! " Simple, read-only (for now), abstraction layer to access git
63
31
  repository\n aiming to support different platforms/backends.\n"
64
32
  email: contact@aumgn.fr
@@ -68,11 +36,14 @@ extra_rdoc_files: []
68
36
  files:
69
37
  - README.md
70
38
  - LICENSE
39
+ - lib/graal/actor.rb
71
40
  - lib/graal/backend.rb
72
41
  - lib/graal/blob.rb
73
42
  - lib/graal/commit.rb
74
43
  - lib/graal/exe/backend.rb
75
44
  - lib/graal/exe/git.rb
45
+ - lib/graal/exe/git_ruby.rb
46
+ - lib/graal/log.rb
76
47
  - lib/graal/repository.rb
77
48
  - lib/graal/tree.rb
78
49
  - lib/graal/tree_blob.rb