asgit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ module Asgit
2
+ class Config
3
+ attr_accessor :organization, :project
4
+
5
+ def service
6
+ @_service
7
+ end
8
+
9
+ def service= name
10
+ @_service = Asgit::Services.send(name.to_sym)
11
+ end
12
+
13
+ end
14
+
15
+ class << self
16
+ def configure &block
17
+ yield config
18
+ end
19
+
20
+ def config
21
+ @_config ||= Config.new
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,31 @@
1
+ module Asgit
2
+ module Services
3
+
4
+ DATA = {
5
+ github: {
6
+ base_url: 'https://github.com',
7
+ base_structure: "%{base_url}/%{organization}/%{project}",
8
+ commit_uri: "commit/%{commit}",
9
+ branch_uri: "tree/%{branch}"
10
+ }
11
+ }
12
+
13
+ DATA.each_key do |service|
14
+ define_singleton_method(service) do
15
+ instance_variable_get("@_#{service}") || instance_variable_set("@_#{service}", Service.new( DATA[service] ))
16
+ end
17
+ end
18
+
19
+ class Service
20
+
21
+ def initialize data
22
+ data.each do |attr, val|
23
+ self.class.send(:define_method, attr) { val }
24
+ end
25
+ return self.freeze
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,28 @@
1
+ require 'open3'
2
+
3
+ module Asgit
4
+ module Shell
5
+
6
+ class << self
7
+
8
+ def run command, &block
9
+ stdout, stderr, status = Open3.capture3(command)
10
+
11
+ if status.success?
12
+ yield stdout if block_given?
13
+ [ true, stdout, stderr ]
14
+ else
15
+ $stderr.puts "Problem running #{command}"
16
+ $stderr.puts stderr
17
+ [ false, stdout, stderr ]
18
+ end
19
+ rescue StandardError => e
20
+ $stderr.puts "Problem running '#{command}'"
21
+ $stderr.puts "#{e.message}"
22
+ $stderr.puts "#{e.backtrace}"
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
data/lib/asgit/url.rb ADDED
@@ -0,0 +1,25 @@
1
+ module Asgit
2
+ module Url
3
+
4
+ class << self
5
+
6
+ def project
7
+ Asgit.config.service.base_structure % {
8
+ base_url: Asgit.config.service.base_url,
9
+ organization: Asgit.config.organization,
10
+ project: Asgit.config.project
11
+ }
12
+ end
13
+
14
+ def commit sha
15
+ File.join( project, Asgit.config.service.commit_uri % { commit: sha } )
16
+ end
17
+
18
+ def branch name
19
+ File.join( project, Asgit.config.service.branch_uri % { branch: name } )
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Asgit
2
+ VERSION = "0.0.1"
3
+ end
data/lib/asgit.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'asgit/shell'
2
+ require 'asgit/url'
3
+ require 'asgit/config'
4
+ require 'asgit/services'
5
+
6
+ module Asgit
7
+ class << self
8
+
9
+ # Check if working tree is clean
10
+ # @return [Boolean] true if branch is clean
11
+ def working_tree_clean?
12
+ Shell.run "git status" do |output|
13
+ return output.include? "nothing to commit"
14
+ end
15
+ end
16
+
17
+ # Get current git branch based on exec directory
18
+ # @return [String] the current checked out branch
19
+ def current_branch
20
+ Shell.run "git symbolic-ref HEAD" do |output|
21
+ return output.strip.gsub(/^refs\/heads\//, '')
22
+ end
23
+ end
24
+
25
+ # Get current git commit based on exec directory
26
+ # @return [String] the current commit level
27
+ def current_commit
28
+ Shell.run "git rev-parse HEAD" do |output|
29
+ return output.strip
30
+ end
31
+ end
32
+
33
+ # Check if branch is in sync with remote
34
+ # @return [Boolean]
35
+ def remote_up_to_date?
36
+ status, stdout, stderr = Shell.run "git push --dry-run"
37
+ return status && stderr.include?( "Everything up-to-date" )
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Asgit::Config do
4
+
5
+ describe "::config" do
6
+ it "returns a config" do
7
+ expect( Asgit.config.class ).to eq Asgit::Config
8
+ end
9
+ end
10
+
11
+ describe "::configure" do
12
+ it "yields the config" do
13
+ Asgit.configure do |config|
14
+ expect( config.class ).to eq Asgit::Config
15
+ end
16
+ end
17
+
18
+ it "sets attributes on Config" do
19
+ Asgit.configure do |config|
20
+ config.project = "woot"
21
+ end
22
+
23
+ expect( Asgit.config.project ).to eq "woot"
24
+ end
25
+
26
+ describe "#service" do
27
+ it "sets as an Asgit::Services::Service" do
28
+ Asgit.configure do |c|
29
+ c.service = :github
30
+ end
31
+
32
+ expect( Asgit.config.service.class ).to eq Asgit::Services::Service
33
+ end
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Asgit::Services do
4
+
5
+ it "returns a Service for services that exist" do
6
+ expect( Asgit::Services.github.class ).to eq Asgit::Services::Service
7
+ end
8
+
9
+ it "doesn't redefine a service if it is set" do
10
+ Asgit::Services.instance_variable_set("@_github", "foo")
11
+ expect( Asgit::Services.github ).to eq "foo"
12
+
13
+ # teardown, just to keep from poluting the rest of the tests
14
+ Asgit::Services.send(:remove_instance_variable, "@_github")
15
+ end
16
+
17
+ describe Asgit::Services::Service do
18
+
19
+ let(:github) { Asgit::Services.github }
20
+
21
+ it "returns data for each key" do
22
+ expect( github.base_url ).to eq "https://github.com"
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Asgit::Url do
4
+
5
+ before :all do
6
+ Asgit.configure do |c|
7
+ c.organization = "wu"
8
+ c.project = "tang"
9
+ c.service = :github
10
+ end
11
+ end
12
+
13
+ after :all do
14
+ Asgit::Services.send(:remove_instance_variable, "@_github")
15
+ end
16
+
17
+ describe "::commit" do
18
+ it "returns the correct url for the provided commit" do
19
+ expect( Asgit::Url.commit "woot" ).to eq "https://github.com/wu/tang/commit/woot"
20
+ end
21
+ end
22
+
23
+ describe "::branch" do
24
+ it "returns the correct url for the provided branch" do
25
+ expect( Asgit::Url.branch "woot" ).to eq "https://github.com/wu/tang/tree/woot"
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Asgit do
4
+
5
+ describe "::working_tree_clean?" do
6
+ it "is true when nothing to commit" do
7
+ Asgit::Shell.fake_stdout "# On branch master\n" +
8
+ "nothing to commit, working directory clean" do
9
+ Asgit.working_tree_clean?.should be_true
10
+ end
11
+ end
12
+ it "is false when tree isn't clean" do
13
+ Asgit::Shell.fake_stdout "# On branch master\n" +
14
+ "# Untracked files:\n" +
15
+ "# (use \"git add <file>...\" to include in what will be committed)\n" +
16
+ "#\n" +
17
+ "# foo\n" +
18
+ "nothing added to commit but untracked files present (use \"git add\" to track)\n" do
19
+ Asgit.working_tree_clean?.should be_false
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "::current_branch" do
25
+ it "returns master when on master" do
26
+ Asgit::Shell.fake_stdout "refs/heads/master" do
27
+ Asgit.current_branch.should == "master"
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "::current_commit" do
33
+ it "returns current commit" do
34
+ Asgit::Shell.fake_stdout "12345" do
35
+ Asgit.current_commit.should == "12345"
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "::remote_up_to_date?" do
41
+ it "returns true if remote is current" do
42
+ Asgit::Shell.fake_stderr "Everything up-to-date" do
43
+ Asgit.remote_up_to_date?.should be_true
44
+ end
45
+ end
46
+ it "returns false if remote is out of sync" do
47
+ Asgit::Shell.fake_stderr "To git@github.com:mailchimp/statistrano.git\n" +
48
+ "fbe7c67..3cf2934 HEAD -> master" do
49
+ Asgit.remote_up_to_date?.should be_false
50
+ end
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,24 @@
1
+ require 'rspec'
2
+ require 'pry-debugger'
3
+
4
+ require 'asgit'
5
+
6
+ require 'support/fake_stdout'
7
+ require 'support/eat_stdout'
8
+
9
+ describe "spec_helper" do
10
+ describe "Asgit::Shell.fake_stdout" do
11
+ it "overrides the actual stdout for Asgit::Shell" do
12
+ Asgit::Shell.fake_stdout "woo" do
13
+ expect( Asgit::Shell.run("foo")[1] ).to eq "woo"
14
+ end
15
+ end
16
+ end
17
+ describe "Asgit::Shell.fake_stderr" do
18
+ it "overrides the actual stderr for Asgit::Shell" do
19
+ Asgit::Shell.fake_stderr "woo" do
20
+ expect( Asgit::Shell.run("foo")[2] ).to eq "woo"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ def eat_stdout!
2
+ output = StringIO.open('','w+')
3
+ $stdout = output
4
+ end
@@ -0,0 +1,48 @@
1
+ module Asgit
2
+ module Shell
3
+ class << self
4
+
5
+ def fake_stdout out
6
+ set_stdout out
7
+ yield
8
+ ensure
9
+ unset_stdout
10
+ end
11
+
12
+ def fake_stderr err
13
+ set_stderr err
14
+ yield
15
+ ensure
16
+ unset_stderr
17
+ end
18
+
19
+ def patched_run command, &block
20
+ if @shell_out || @shell_err
21
+ yield @shell_out if block_given?
22
+ [ true, @shell_out, @shell_err ]
23
+ else
24
+ system_run command, &block
25
+ end
26
+ end
27
+
28
+ def set_stdout out
29
+ @shell_out = out
30
+ end
31
+
32
+ def unset_stdout
33
+ @shell_out = nil
34
+ end
35
+
36
+ def set_stderr err
37
+ @shell_err = err
38
+ end
39
+
40
+ def unset_stderr
41
+ @shell_err = nil
42
+ end
43
+
44
+ alias_method :system_run, :run
45
+ alias_method :run, :patched_run
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asgit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Steven Sloan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! ' A simple query interface for git. '
15
+ email:
16
+ - stevenosloan@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/asgit/config.rb
22
+ - lib/asgit/services.rb
23
+ - lib/asgit/shell.rb
24
+ - lib/asgit/url.rb
25
+ - lib/asgit/version.rb
26
+ - lib/asgit.rb
27
+ - spec/asgit/config_spec.rb
28
+ - spec/asgit/services_spec.rb
29
+ - spec/asgit/url_spec.rb
30
+ - spec/asgit_spec.rb
31
+ - spec/spec_helper.rb
32
+ - spec/support/eat_stdout.rb
33
+ - spec/support/fake_stdout.rb
34
+ homepage: http://github.com/stevenosloan/asgit
35
+ licenses:
36
+ - MIT
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.25
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A simple query interface for git
59
+ test_files:
60
+ - spec/asgit/config_spec.rb
61
+ - spec/asgit/services_spec.rb
62
+ - spec/asgit/url_spec.rb
63
+ - spec/asgit_spec.rb
64
+ - spec/spec_helper.rb
65
+ - spec/support/eat_stdout.rb
66
+ - spec/support/fake_stdout.rb
67
+ has_rdoc: