git_rev 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7060bb3655f892aa3b0de369b5148938e9edb15d
4
+ data.tar.gz: e83618685c95c911d309b1230552dc21a54d811e
5
+ SHA512:
6
+ metadata.gz: 78a5c406b7d5fb6f9434bb98356ac0493dde51bd2c499fea94691f59b9a04faa652ccb729e0adeeca23d08a8e1d5c1a919737ada23aecf0aca03cdba2fdcfee5
7
+ data.tar.gz: 3330507fa6b64877ade06f337c53d29fd817318faf20e35c7a59681e84575e255f3bdbb1af58f5dd4f938a6e83c7361daa8f01724d3f29c0b784438fc7259e59
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in git_rev.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :minitest do
2
+ # with Minitest::Spec
3
+ watch(%r{^spec/(.*)_spec\.rb$})
4
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
5
+ watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 ServeBox
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # GitRev
2
+
3
+ Retrieve the SHA-1 hash of the current revision of a Git repository. GitRev
4
+ uses the contents of the `.git` directory therefore it does not require Git to
5
+ be installed.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'git_rev'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install git_rev
22
+
23
+ ## Usage
24
+
25
+ ### SHA-1
26
+
27
+ By default, `GitRev::Sha` uses the current directory as the Git repository and
28
+ caches the revision number:
29
+
30
+ ```ruby
31
+ require 'git_rev'
32
+
33
+ revision = GitRev::Sha.new
34
+
35
+ revision.full
36
+ #=> 1f7995f701d02f2f62b2e7faa91a1e16f3d68666
37
+
38
+ revision.short
39
+ #=> 1f7995f
40
+ ```
41
+
42
+ You may specify the location of the Git repository:
43
+
44
+ ```ruby
45
+ revision = GitRev::Sha.new(repository: '/path/to/repository')
46
+ ```
47
+
48
+ By default, the cache is enabled, meaning GitRev will only query the filesystem
49
+ once. If you need to ensure the revision info is reloaded each time:
50
+
51
+ ```ruby
52
+ revision = GitRev::Sha.new(repository: '/path/to/repository', cache: false)
53
+
54
+ revision.short
55
+ #=> 1f7995f
56
+
57
+ `git commit -m "A message"`
58
+
59
+ revision.short
60
+ # => 658f674
61
+
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/[my-github-username]/git_rev/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'spec'
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ end
9
+
10
+ task :default => [:test]
data/git_rev.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'git_rev/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "git_rev"
8
+ spec.version = GitRev::VERSION
9
+ spec.authors = ["Jef Mathiot"]
10
+ spec.email = ["jeff.mathiot@gmail.com"]
11
+ spec.summary = %q{Retrieve the git revision}
12
+ spec.description = %q{Get the git revision of a given repository. Does not require Git binaries}
13
+ spec.homepage = "http://github.com/servebox/git_rev"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'minitest', '~> 5.4', '>= 5.4.1'
24
+ spec.add_development_dependency 'minitest-implicit-subject', '~> 1.4', '>= 1.4.0'
25
+ spec.add_development_dependency 'rb-readline', '~> 0.5', '>= 0.5.0'
26
+ spec.add_development_dependency 'guard', '~> 2.11', '>= 2.11.1'
27
+ spec.add_development_dependency 'guard-minitest', '~> 2.3', '>= 2.3.2'
28
+
29
+ end
data/lib/git_rev.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "git_rev/version"
2
+ require "git_rev/sha"
3
+
4
+ module GitRev
5
+ end
@@ -0,0 +1,58 @@
1
+ module GitRev
2
+ class Sha
3
+
4
+ def initialize(options={})
5
+ repository = options[:repository] || Dir.pwd
6
+ cache = options.has_key?(:cache) ? options[:cache] : true
7
+ @repository=File.expand_path(repository)
8
+ @cache = cache
9
+ ensure_repository!
10
+ end
11
+
12
+ def full
13
+ cached? && @revision || load_revision
14
+ end
15
+
16
+ def short
17
+ full[0, 7]
18
+ end
19
+
20
+ private
21
+
22
+ def cached?
23
+ !!@cache
24
+ end
25
+
26
+ def git_repository?
27
+ File.directory?(git_directory)
28
+ end
29
+
30
+ def git_directory
31
+ File.join(@repository, '.git')
32
+ end
33
+
34
+ def ensure_repository!
35
+ raise "Not a git repository #{@repository}" unless git_repository?
36
+ end
37
+
38
+ def load_revision
39
+ head=File.read(File.join(git_directory, 'HEAD')).chomp
40
+ # Detached head
41
+ if sha1?(head)
42
+ @revision = head
43
+ else
44
+ @revision = ref(head)
45
+ end
46
+ end
47
+
48
+ def sha1?(subject)
49
+ /^[0-9a-f]{40}$/ =~ subject
50
+ end
51
+
52
+ def ref(head)
53
+ ref = head.scan(/^ref:\s+(.*)$/).first
54
+ File.read(File.join(git_directory, ref)).chomp
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module GitRev
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe GitRev::Sha do
5
+
6
+ before do
7
+ @tmpdir=Dir.mktmpdir
8
+ end
9
+
10
+ after do
11
+ FileUtils.rm_rf(@tmpdir)
12
+ end
13
+
14
+ let(:sha) do
15
+ subject.new( repository: @tmpdir)
16
+ end
17
+
18
+ let(:no_cache) do
19
+ subject.new( repository: @tmpdir, cache: false)
20
+ end
21
+
22
+ def self.assert_revisions(update_method)
23
+
24
+ def assert_and_update(subject, method)
25
+ subject.full.must_equal revision
26
+ send method, "b" * 40
27
+ end
28
+
29
+ it 'enables the cache by default' do
30
+ assert_and_update(sha, update_method)
31
+ sha.full.must_equal revision
32
+ end
33
+
34
+ it 'disables the cache' do
35
+ assert_and_update(no_cache, update_method)
36
+ no_cache.full.wont_equal revision
37
+ end
38
+
39
+ it 'provides the hash in short format' do
40
+ sha.short.must_equal "aaaaaaa"
41
+ end
42
+
43
+ end
44
+
45
+ it 'raises unless a git repository' do
46
+ ex = ->{ sha }.must_raise RuntimeError
47
+ ex.message.must_equal "Not a git repository #{@tmpdir}"
48
+ end
49
+
50
+ it 'defaults to the current directory' do
51
+ subject.new.short.must_equal `git describe --always`.chomp
52
+ end
53
+
54
+ describe 'with a fake git repository' do
55
+
56
+ before do
57
+ @gitdir=FileUtils.mkdir_p(File.join(@tmpdir, '.git'))
58
+ FileUtils.mkdir_p(File.join(@gitdir, 'refs/heads'))
59
+ end
60
+
61
+ let(:revision) do
62
+ "a" * 7 + "b" * 33
63
+ end
64
+
65
+ def overwrite(path, contents)
66
+ File.delete(path) if File.exists?(path)
67
+ File.open(path, 'wb') do |f|
68
+ f.write contents
69
+ end
70
+ end
71
+
72
+ def write_head(contents)
73
+ overwrite File.join(@gitdir, 'HEAD'), contents
74
+ end
75
+
76
+ def write_master_ref(revision)
77
+ overwrite File.join(@gitdir, 'refs/heads/master'), revision
78
+ end
79
+
80
+ describe 'on a branch' do
81
+
82
+ before do
83
+ write_head "ref: refs/heads/master"
84
+ write_master_ref revision
85
+ end
86
+
87
+ assert_revisions :write_master_ref
88
+
89
+ end
90
+
91
+ describe 'on a detached HEAD' do
92
+
93
+ before do
94
+ write_head revision
95
+ end
96
+
97
+ assert_revisions :write_head
98
+
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -0,0 +1,6 @@
1
+ require 'git_rev'
2
+ require 'minitest'
3
+ require 'minitest/spec'
4
+ require 'minitest/autorun'
5
+ require 'minitest/pride'
6
+ require 'minitest-implicit-subject'
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git_rev
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jef Mathiot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.4'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 5.4.1
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '5.4'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 5.4.1
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest-implicit-subject
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.4'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.4.0
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.4'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 1.4.0
81
+ - !ruby/object:Gem::Dependency
82
+ name: rb-readline
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '0.5'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 0.5.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '0.5'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 0.5.0
101
+ - !ruby/object:Gem::Dependency
102
+ name: guard
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '2.11'
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 2.11.1
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.11'
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 2.11.1
121
+ - !ruby/object:Gem::Dependency
122
+ name: guard-minitest
123
+ requirement: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - "~>"
126
+ - !ruby/object:Gem::Version
127
+ version: '2.3'
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 2.3.2
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '2.3'
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 2.3.2
141
+ description: Get the git revision of a given repository. Does not require Git binaries
142
+ email:
143
+ - jeff.mathiot@gmail.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files: []
147
+ files:
148
+ - ".gitignore"
149
+ - Gemfile
150
+ - Guardfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - git_rev.gemspec
155
+ - lib/git_rev.rb
156
+ - lib/git_rev/sha.rb
157
+ - lib/git_rev/version.rb
158
+ - spec/git_rev/sha_spec.rb
159
+ - spec/spec_helper.rb
160
+ homepage: http://github.com/servebox/git_rev
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.2.2
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Retrieve the git revision
184
+ test_files:
185
+ - spec/git_rev/sha_spec.rb
186
+ - spec/spec_helper.rb
187
+ has_rdoc: