git-pcheckout 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MzYzOTQ2MmI0N2ZhZTBlZjdhYWYwY2U3NGRmNjk5OTU0MmJiMGI2OA==
5
- data.tar.gz: !binary |-
6
- OTA2NTE0NmRmMTVmNTU0NGNjNTkyMjIwZWQ4OTEwMTZjMWUwYTI4Yw==
2
+ SHA1:
3
+ metadata.gz: 19ebb7be03ae20a913488690729afd281e5989a4
4
+ data.tar.gz: 5df1feabe877b12315955ce0b0b8d480ad49cb9c
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- M2IxMGI3Nzg0M2ZmNDYwMGI4MzM2YjI2Yjc5MWFlNjZmYjU0ZThjNWRmNWVl
10
- NzNlZTE0NjZjY2ZiNDJkNjQzY2RkNzA2ZGJhYWUzZmI4NjQ4M2Q5ZmQ5ZDQ5
11
- YTBkZTE2Njk4Njg2ZmRkNjVhMjgwNjJmNWY1NjYyNGQwODM0ZWM=
12
- data.tar.gz: !binary |-
13
- NDE1YTI4ZjQyYjBhYmMxMDkyZjBjM2JkOGZmYTZhNmJhZTQzMjM2N2MyZTJh
14
- YjkzYThlOGNkNTRkYTE1N2ZmMWViN2Q3ZWYzNjk2NjkwZThjMzllNzllMmVl
15
- YzBiZTRhM2ZkOTY5ZjRkZTE3MmI3YjhiM2E4YzAzYzVlZDYxOWQ=
6
+ metadata.gz: e3064c7df5e48508363f0105cc20aec8faae792a18df9ebc2b70e7ea6dafe73f271bd67bf3ae98b807a7bc2adcb4f184fa45bcd18e89d3083b803a354025db16
7
+ data.tar.gz: 3f32fbaa2c8a1ac25bff5857a09238acce97de293e008b0e1c712e29e6d7a0a9c138b92278e9f5251d392b05db6b98cee20bbd1451e73919e9b6733c11d4ecec
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/README.md CHANGED
@@ -1,7 +1,11 @@
1
1
  # git-pcheckout
2
2
 
3
- Git command to evenly checkout local/remote branches and source/forks
4
- pull pequests by URL (with Hub)
3
+ [![Build Status](https://api.travis-ci.org/vrybas/git-pcheckout.svg)][travis]
4
+ [![Gem Version](http://img.shields.io/gem/v/git-pcheckout.svg)][gem]
5
+ [![Code Climate](http://img.shields.io/codeclimate/github/vrybas/git-pcheckout.svg)][codeclimate]
6
+
7
+ Git command to evenly checkout local/remote branches and source/fork
8
+ pull requests by URL (with Hub)
5
9
 
6
10
  ## Installation
7
11
 
@@ -17,20 +21,20 @@ pull pequests by URL (with Hub)
17
21
 
18
22
  ## What it does
19
23
 
20
- If specified _branch-name_ exists locally:
24
+ 1. If specified _branch-name_ exists locally:
21
25
 
22
26
  $ git checkout branch-name
23
27
  $ git pull origin branch-name
24
28
 
25
- If specified _branch-name_ not exist locally:
29
+ 2. If specified _branch-name_ not exist locally:
26
30
 
27
31
  $ git fetch
28
32
  $ git checkout --track origin/branch-name
29
33
 
30
- If pull request URL specified(from fork):
34
+ 3. If pull request URL specified(from fork):
31
35
  - pulls fork branch with Hub
32
36
 
33
- If pull request URL specified(from source repo):
37
+ 4. If pull request URL specified(from source repo):
34
38
  - treats it as if just a _branch_name_ was specified (goto 1)
35
39
 
36
40
  ## Contributing
@@ -42,3 +46,6 @@ If pull request URL specified(from source repo):
42
46
  5. Create new Pull Request
43
47
 
44
48
  [1]: http://hub.github.com/
49
+ [travis]: https://travis-ci.org/vrybas/git-pcheckout
50
+ [gem]: http://rubygems.org/gems/git-pcheckout
51
+ [codeclimate]: https://codeclimate.com/github/vrybas/git-pcheckout
data/Rakefile CHANGED
@@ -1 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ task :default => :spec
4
+ RSpec::Core::RakeTask.new
data/bin/git-pcheckout CHANGED
@@ -16,6 +16,6 @@
16
16
  # If pull request URL specified(from source repo):
17
17
  # - treats it as if just a branch name was specified (goto 1)
18
18
 
19
- require_relative '../git-pcheckout.rb'
19
+ require_relative '../lib/git-pcheckout.rb'
20
20
 
21
- GitPcheckout.(ARGV[0])
21
+ GitPcheckout::Base.new(ARGV[0]).perform
@@ -0,0 +1,7 @@
1
+ GEM
2
+ specs:
3
+
4
+ PLATFORMS
5
+ ruby
6
+
7
+ DEPENDENCIES
@@ -1,14 +1,10 @@
1
1
  class HandleBranch < Struct.new(:branch)
2
2
 
3
- def self.call(*args)
4
- new(*args).call
5
- end
6
-
7
3
  def initialize(branch)
8
4
  self.branch = branch
9
5
  end
10
6
 
11
- def call
7
+ def perform
12
8
  if branch_exists_locally?
13
9
  checkout_local_branch && pull_from_origin
14
10
  else
@@ -1,3 +1,3 @@
1
1
  module GitPcheckout
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,32 +1,35 @@
1
- require_relative 'lib/handle-branch.rb'
1
+ require_relative 'git-pcheckout/handle_branch'
2
2
 
3
- class GitPcheckout < Struct.new(:arg)
4
-
5
- def self.call(*args)
6
- new(*args).call
7
- end
3
+ module GitPcheckout
4
+ class Base < Struct.new(:arg)
5
+ def initialize(arg)
6
+ self.arg = arg
7
+ end
8
8
 
9
- def initialize(arg)
10
- self.arg = arg
11
- end
9
+ def perform
10
+ validate_current_branch_state
12
11
 
13
- def call
14
- if pull_request_url?(arg)
15
- handle_pull_request_url(arg)
16
- else
17
- handle_branch_name(arg)
12
+ if pull_request_url?(arg)
13
+ handle_pull_request_url(arg)
14
+ else
15
+ handle_branch_name(arg)
16
+ end
18
17
  end
19
- end
20
18
 
21
- private
19
+ private
20
+
21
+ def validate_current_branch_state
22
+ if dirty_branch?
23
+ puts errors[:dirty_branch]
24
+ exit(1)
25
+ end
26
+ end
22
27
 
23
28
  def pull_request_url?(arg)
24
29
  arg.match /https:\/\/github.com\//
25
30
  end
26
31
 
27
32
  def handle_pull_request_url(url)
28
- puts "handling Pull Request URL..."
29
-
30
33
  branch_name_with_prefix = checkout_pull_request_branch(url)
31
34
 
32
35
  if source_repository_branch?(branch_name_with_prefix)
@@ -35,6 +38,7 @@ class GitPcheckout < Struct.new(:arg)
35
38
  end
36
39
 
37
40
  def checkout_pull_request_branch(url)
41
+ puts "handling Pull Request URL..."
38
42
  out = `hub checkout #{url}`
39
43
  return false if out == ''
40
44
  out.scan(/Branch (.+) set/).flatten.first
@@ -57,7 +61,8 @@ class GitPcheckout < Struct.new(:arg)
57
61
  end
58
62
 
59
63
  def handle_source_branch(branch_name_with_prefix)
60
- handle_branch_name(substitute_prefix(branch_name_with_prefix)) &&
64
+ branch_name = substitute_prefix(branch_name_with_prefix)
65
+ handle_branch_name(branch_name)
61
66
  delete_branch(branch_name_with_prefix)
62
67
  end
63
68
 
@@ -66,6 +71,29 @@ class GitPcheckout < Struct.new(:arg)
66
71
  end
67
72
 
68
73
  def handle_branch_name(branch)
69
- HandleBranch.(branch)
74
+ HandleBranch.new(branch).perform
70
75
  end
76
+
77
+ def dirty_branch?
78
+ status_lines = get_status_lines
79
+ return false if status_lines.empty?
80
+
81
+ if modified_deleted_renamed?(status_lines)
82
+ puts status_lines
83
+ true
84
+ end
85
+ end
86
+
87
+ def get_status_lines
88
+ `git status -s`.split("\n").map(&:lstrip)
89
+ end
90
+
91
+ def modified_deleted_renamed?(lines)
92
+ lines.any? { |line| line.match(/(^M|^D|^R)\s/) }
93
+ end
94
+
95
+ def errors
96
+ { dirty_branch: "Please, commit your changes or stash them before you can switch branches"}
97
+ end
98
+ end
71
99
  end
@@ -1,60 +1,68 @@
1
- require_relative '../git-pcheckout.rb'
1
+ require 'spec_helper'
2
2
 
3
- describe GitPcheckout do
3
+ describe GitPcheckout::Base do
4
+ before do
5
+ allow_any_instance_of(described_class).to receive(:system).and_return(true)
4
6
 
5
- context ".call" do
6
- it "should delegate to #new() with all params" do
7
- expect_any_instance_of(described_class).to receive("call").and_return(true)
8
- instance = described_class.("foo")
9
- expect(instance).to be
10
- end
11
-
12
- it "should send #call() to created instance" do
13
- expect_any_instance_of(described_class).to receive("call")
14
- described_class.("foo")
15
- end
7
+ handle_branch = double(:handle_branch, perform: 'handle_branch')
8
+ HandleBranch.stub(:new).and_return(handle_branch)
16
9
  end
17
10
 
18
- context "#initialize" do
19
- it "should set @arg" do
20
- instance = described_class.new("foo")
21
- expect(instance.arg).to eql("foo")
11
+ context '#perofrm' do
12
+ it 'doesn\'t run if current branch is dirty' do
13
+ instance = described_class.new('foo')
14
+ instance.stub(:dirty_branch?).and_return(true)
15
+ expect { instance.perform }.to raise_error(SystemExit)
22
16
  end
23
- end
24
17
 
25
- context "#call" do
26
- context "plain branch name" do
27
- it "should handle plain branch name" do
28
- expect_any_instance_of(described_class).to receive("pull_request_url?").and_return(false)
29
- expect_any_instance_of(HandleBranch).to receive("call").and_return(true)
30
- described_class.("foo")
18
+ context 'plain branch name' do
19
+ it 'handles plain branch name' do
20
+ instance = described_class.new('foo')
21
+ instance.stub(:dirty_branch?).and_return(false)
22
+
23
+ expect(instance.perform).to eql('handle_branch')
31
24
  end
32
25
  end
33
26
 
34
- context "pull request url" do
35
- it "should handle pull_request_url" do
36
- expect_any_instance_of(described_class).to receive("handle_pull_request_url").and_return(true)
37
- described_class.("https://github.com/user/repo.git")
27
+ context 'pull request URL' do
28
+ let(:origin_url) { "git@github.com/#{user_name}/repo-name.git" }
29
+ let(:pull_request_url) { "https://github.com/#{user_name}/repo-name/pull/18" }
30
+ let(:branch_name) { "#{user_name}-branch-name" }
31
+ let(:instance) { described_class.new(pull_request_url) }
32
+
33
+ before do
34
+ instance.stub(:dirty_branch?).and_return(false)
35
+ instance.stub(:origin_url).and_return(origin_url)
36
+ instance.stub(:checkout_pull_request_branch).and_return(branch_name)
38
37
  end
39
38
 
40
- it "should pull branch with prefix" do
41
- origin_url = "git@github.com/user/repo-name.git"
42
- url = "https://github.com/forked-user/repo-name/pull/18"
43
- expect_any_instance_of(described_class).to receive("origin_url").and_return(origin_url)
44
- expect_any_instance_of(described_class).to receive("checkout_pull_request_branch").and_return("forked-user-branch_name")
45
- described_class.(url)
39
+ context 'when pull request is from the same repo' do
40
+ let(:user_name) { 'user' }
41
+
42
+ it 'gets plain branch name out of pull request' do
43
+ expect(instance).to receive(:substitute_prefix)
44
+ instance.perform
45
+ end
46
+
47
+ it 'handles plain branch name' do
48
+ expect(instance).to receive(:handle_branch_name)
49
+ instance.perform
50
+ end
51
+
52
+ it 'deletes temporary branch' do
53
+ expect(instance).to receive(:delete_branch).with(branch_name)
54
+ instance.perform
55
+ end
46
56
  end
47
57
 
48
- it "should handle a source branch, specified in pull request" do
49
- origin_url = "git@github.com/user/repo-name.git"
50
- url = "https://github.com/user/repo-name/pull/18"
51
- expect_any_instance_of(described_class).to receive("origin_url").twice.and_return(origin_url)
52
- expect_any_instance_of(described_class).to receive("checkout_pull_request_branch").and_return("user-branch_name")
53
- expect(HandleBranch).to receive("call").with("branch_name").and_return(true)
54
- expect_any_instance_of(described_class).to receive("delete_branch").and_return(true)
55
- described_class.(url)
58
+ context 'when pull request is from the fork' do
59
+ let(:user_name) { 'forked-user' }
60
+
61
+ it 'creates branch locally with fork prefix' do
62
+ expect(instance).to receive(:checkout_pull_request_branch)
63
+ instance.perform
64
+ end
56
65
  end
57
66
  end
58
-
59
67
  end
60
68
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe HandleBranch do
4
+ let(:handle_branch) { described_class.new('foo') }
5
+
6
+ context '#perform' do
7
+ it 'checkouts and pulls if branch exists' do
8
+ handle_branch.stub('branch_exists_locally?').and_return(true)
9
+
10
+ expect(handle_branch).to receive('checkout_local_branch').and_return(true)
11
+ expect(handle_branch).to receive('pull_from_origin').and_return(true)
12
+
13
+ handle_branch.perform
14
+ end
15
+
16
+ it 'fetches and checkouts with track if branch not exist' do
17
+ handle_branch.stub('branch_exists_locally?').and_return(false)
18
+
19
+ expect(handle_branch).to receive('fetch_from_origin').and_return(true)
20
+ expect(handle_branch).to receive('checkout_and_track_branch').and_return(true)
21
+
22
+ handle_branch.perform
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+ config.order = 'random'
11
+ end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-pcheckout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Rybas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-23 00:00:00.000000000 Z
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.5'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Git command to evenly checkout local/remote branches and source/fork
@@ -61,7 +61,8 @@ executables:
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
- - .gitignore
64
+ - ".gitignore"
65
+ - ".rspec"
65
66
  - Gemfile
66
67
  - Guardfile
67
68
  - LICENSE.txt
@@ -69,11 +70,13 @@ files:
69
70
  - Rakefile
70
71
  - bin/git-pcheckout
71
72
  - git-pcheckout.gemspec
72
- - git-pcheckout.rb
73
+ - git-pcheckout.gemspec.lock
74
+ - lib/git-pcheckout.rb
75
+ - lib/git-pcheckout/handle_branch.rb
73
76
  - lib/git-pcheckout/version.rb
74
- - lib/handle-branch.rb
75
77
  - spec/git-pcheckout_spec.rb
76
- - spec/lib/handle-branch_spec.rb
78
+ - spec/lib/handle_branch_spec.rb
79
+ - spec/spec_helper.rb
77
80
  - tmp/.keep
78
81
  homepage: https://github.com/vrybas/git-pcheckout
79
82
  licenses:
@@ -85,12 +88,12 @@ require_paths:
85
88
  - lib
86
89
  required_ruby_version: !ruby/object:Gem::Requirement
87
90
  requirements:
88
- - - ! '>='
91
+ - - ">="
89
92
  - !ruby/object:Gem::Version
90
93
  version: '0'
91
94
  required_rubygems_version: !ruby/object:Gem::Requirement
92
95
  requirements:
93
- - - ! '>='
96
+ - - ">="
94
97
  - !ruby/object:Gem::Version
95
98
  version: '0'
96
99
  requirements: []
@@ -102,4 +105,5 @@ summary: Git command to evenly checkout local/remote branches and source/fork pu
102
105
  requests by URL (with Hub)
103
106
  test_files:
104
107
  - spec/git-pcheckout_spec.rb
105
- - spec/lib/handle-branch_spec.rb
108
+ - spec/lib/handle_branch_spec.rb
109
+ - spec/spec_helper.rb
@@ -1,40 +0,0 @@
1
- require_relative '../../lib/handle-branch.rb'
2
-
3
- describe HandleBranch do
4
-
5
- context ".call" do
6
- it "should delegate to #new() with all params" do
7
- expect_any_instance_of(HandleBranch).to receive("call").and_return(true)
8
- instance = HandleBranch.("foo")
9
- expect(instance).to be
10
- end
11
-
12
- it "should send #call() to created instance" do
13
- expect_any_instance_of(HandleBranch).to receive("call")
14
- HandleBranch.("foo")
15
- end
16
- end
17
-
18
- context "#initialize" do
19
- it "should set @branch" do
20
- instance = HandleBranch.new("foo")
21
- expect(instance.branch).to eql("foo")
22
- end
23
- end
24
-
25
- context "#call" do
26
- it "should checkout and pull if branch exists" do
27
- expect_any_instance_of(HandleBranch).to receive("branch_exists_locally?").and_return(true)
28
- expect_any_instance_of(HandleBranch).to receive("checkout_local_branch").and_return(true)
29
- expect_any_instance_of(HandleBranch).to receive("pull_from_origin").and_return(true)
30
- HandleBranch.("foo")
31
- end
32
-
33
- it "should fetch and checkout with track if branch not exist" do
34
- expect_any_instance_of(HandleBranch).to receive("branch_exists_locally?").and_return(false)
35
- expect_any_instance_of(HandleBranch).to receive("fetch_from_origin").and_return(true)
36
- expect_any_instance_of(HandleBranch).to receive("checkout_and_track_branch").and_return(true)
37
- HandleBranch.("foo")
38
- end
39
- end
40
- end