git_helper 1.3.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +17 -13
  3. data/README.md +37 -44
  4. data/Rakefile +1 -37
  5. data/bin/git-helper +10 -28
  6. data/lib/git_helper.rb +3 -5
  7. data/lib/git_helper/change_remote.rb +53 -40
  8. data/lib/git_helper/checkout_default.rb +1 -1
  9. data/lib/git_helper/clean_branches.rb +1 -4
  10. data/lib/git_helper/code_request.rb +95 -0
  11. data/lib/git_helper/empty_commit.rb +1 -1
  12. data/lib/git_helper/forget_local_commits.rb +7 -0
  13. data/lib/git_helper/git_config_reader.rb +1 -1
  14. data/lib/git_helper/gitlab_client.rb +0 -1
  15. data/lib/git_helper/highline_cli.rb +21 -17
  16. data/lib/git_helper/local_code.rb +67 -19
  17. data/lib/git_helper/merge_request.rb +55 -73
  18. data/lib/git_helper/new_branch.rb +2 -13
  19. data/lib/git_helper/octokit_client.rb +0 -1
  20. data/lib/git_helper/pull_request.rb +44 -67
  21. data/lib/git_helper/version.rb +1 -1
  22. data/plugins.zip +0 -0
  23. data/spec/git_helper/change_remote_spec.rb +173 -0
  24. data/spec/git_helper/checkout_default_spec.rb +19 -0
  25. data/spec/git_helper/clean_branches_spec.rb +19 -0
  26. data/spec/git_helper/code_request_spec.rb +259 -0
  27. data/spec/git_helper/empty_commit_spec.rb +19 -0
  28. data/spec/git_helper/forget_local_commits_spec.rb +19 -0
  29. data/spec/git_helper/git_config_reader_spec.rb +60 -0
  30. data/spec/git_helper/gitlab_client_spec.rb +26 -0
  31. data/spec/git_helper/highline_cli_spec.rb +215 -0
  32. data/spec/git_helper/local_code_spec.rb +231 -0
  33. data/spec/git_helper/merge_request_spec.rb +234 -0
  34. data/spec/git_helper/new_branch_spec.rb +44 -0
  35. data/spec/git_helper/octokit_client_spec.rb +26 -0
  36. data/spec/git_helper/pull_request_spec.rb +246 -0
  37. data/spec/spec_helper.rb +0 -7
  38. metadata +41 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7959639f26bda279bb9154f76ba1d5041802d5ac63fbb02d6df3ca85d55040ca
4
- data.tar.gz: 59144b9c94dd74ac2bc8c05417363ee37f926a2266fe623e3c13032750910025
3
+ metadata.gz: 8e4c2767e4d4858d3ed4f8791d0dc82577e9fdf051aff59b1f6516c4f13bd4a4
4
+ data.tar.gz: 686894c11b437666221793b475cdf81eb1c99437fcc1ee87aef6a3c07d33e9a4
5
5
  SHA512:
6
- metadata.gz: 104ed8f0605c4471b0e4163dfe7148a4a5b2139871150e5577ed371035419122d9c64571aa2ff0897d812e5a08982c3cbd544e6d9936d36d792df2f0fbe9248d
7
- data.tar.gz: 4eef1f32e7c147279228388ade7056a46568cacae9fe61dcd58534b42b50c2eb422a8fd9c043ff9fdf138595dccf8a17d7771be877fa2c2ba6bf13eaaa9ec5ff
6
+ metadata.gz: 32ba80af9ea352cd22c035ecb5e16c2315bae91c9f2b6b3f49a8d5518ffdc4bd533c9b38e6a8652798bd78672e439ba489d8e58346935f3d43ec21dbc3f248ab
7
+ data.tar.gz: '093a11e7c9d46548ad0127d31c101a1fcf69e0aba7c7a1e4c1873dbebadb78bb2f3791175c3702b081fb36d788c1bb4cbe0149833ee58180f038269eeb1f2523'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- git_helper (1.0.0)
4
+ git_helper (2.0.2)
5
5
  gitlab (~> 4.16)
6
6
  gli (~> 2.13)
7
7
  highline (~> 2.0)
@@ -61,18 +61,23 @@ GEM
61
61
  coderay (~> 1.1)
62
62
  method_source (~> 1.0)
63
63
  public_suffix (4.0.6)
64
- rake (12.3.3)
64
+ rake (13.0.1)
65
65
  rb-fsevent (0.10.4)
66
66
  rb-inotify (0.10.1)
67
67
  ffi (~> 1.0)
68
- rspec (2.99.0)
69
- rspec-core (~> 2.99.0)
70
- rspec-expectations (~> 2.99.0)
71
- rspec-mocks (~> 2.99.0)
72
- rspec-core (2.99.2)
73
- rspec-expectations (2.99.2)
74
- diff-lcs (>= 1.1.3, < 2.0)
75
- rspec-mocks (2.99.4)
68
+ rspec (3.9.0)
69
+ rspec-core (~> 3.9.0)
70
+ rspec-expectations (~> 3.9.0)
71
+ rspec-mocks (~> 3.9.0)
72
+ rspec-core (3.9.3)
73
+ rspec-support (~> 3.9.3)
74
+ rspec-expectations (3.9.2)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.9.0)
77
+ rspec-mocks (3.9.1)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.9.0)
80
+ rspec-support (3.9.3)
76
81
  sawyer (0.8.2)
77
82
  addressable (>= 2.3.5)
78
83
  faraday (> 0.8, < 2.0)
@@ -88,10 +93,9 @@ PLATFORMS
88
93
  DEPENDENCIES
89
94
  bundler (~> 2.1)
90
95
  git_helper!
91
- guard (~> 2.6)
92
96
  guard-rspec (~> 4.3)
93
- rake (~> 12.3.3)
94
- rspec (~> 2.99)
97
+ rake (~> 13.0)
98
+ rspec (~> 3.9)
95
99
 
96
100
  BUNDLED WITH
97
101
  2.1.4
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  gem install git_helper
7
7
  ```
8
8
 
9
- Some of the commands in this gem can be used without any additional configuration. However, others utilize special GitHub or GitLab configuration. To provide access tokens for this, create a `~/.git_config.yml` file. The contents should look like this:
9
+ Some of the commands in this gem can be used without any additional configuration. However, others utilize special GitHub or GitLab configuration. To provide access tokens for this, create a `~/.git_helper/config.yml` file. The contents should look like this:
10
10
 
11
11
  ```
12
12
  :github_user: GITHUB-USERNAME
@@ -30,15 +30,22 @@ git-helper --version
30
30
  As an additional option, you can set each of the following commands to be a git plugin, meaning you can call them in a way that feels even more git-native:
31
31
 
32
32
  ```bash
33
- # As a Gem # As a Plugin
34
- git-helper clean-branches git clean-branches
35
- git-helper pull-request --create git pull-request --create
33
+ # As a Gem # As a Plugin
34
+ git-helper clean-branches git clean-branches
35
+ git-helper code-request --create git code-request --create
36
36
  ```
37
37
 
38
- To do this, clone this repository from GitHub, and add the following line to your `~/.bash_profile`:
38
+ To do this, download the `plugins.zip` file in the root of this directory. Unzip and place the contents in the appropriate location:
39
39
 
40
+ ```bash
41
+ mkdir ~/.git_helper
42
+ unzip path/to/downloaded/plugins.zip -d ~/.git_helper
40
43
  ```
41
- export PATH=/your/path/to/this/repository/git_helper/plugins:$PATH
44
+
45
+ Now, the plugins will live in `~/.git_helper/plugins/*`. Add the following line to your `~/.bash_profile`:
46
+
47
+ ```
48
+ export PATH=/path/to/computer/home/.git_helper/plugins:$PATH
42
49
  ```
43
50
 
44
51
  And then run `source ~/.bash_profile`.
@@ -71,7 +78,7 @@ If you're going to make using git workflows easier, might as well provide lots o
71
78
 
72
79
  This can be used when switching the owners of a GitHub repo. When you switch a username, GitHub only makes some changes for you. With this command, you no longer have to manually walk through local repo and switch the remotes from each one into a remote with the new username.
73
80
 
74
- This command will go through every directory in a directory, see if it is a git directory, and then will check to see if the old username is included in the remote URL of that git directory. If it is, then the command will change the remote URL to instead point to the new username's remote URL. To run the command, run:
81
+ This command will go through every directory in a directory, and see if it is a git directory. It will then ask the user if they wish to process the git directory in question. The command does not yet know if there's any changes to be made. If the user says 'yes', then it will check to see if the old username is included in the remote URL of that git directory. If it is, then the command will change the remote URL to instead point to the new username's remote URL. To run the command, run:
75
82
 
76
83
  ```bash
77
84
  git-helper change-remote OLD-OWNER NEW-OWNER
@@ -93,75 +100,61 @@ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/CORRECT-DEFAULT-BR
93
100
 
94
101
  ### `clean-branches`
95
102
 
96
- This command will bring you to the repository's default branch, `git pull`, `git fetch -p`, and will clean up your local branches on your machine by seeing which ones are existing on the remote, and updating yours accordingly. To run:
103
+ This command will bring you to the repository's default branch, `git pull`, `git fetch -p`, and will clean up your local branches on your machine by seeing which ones are existing on the remote, and updating yours accordingly. To clean your local branches, run:
97
104
 
98
105
  ```bash
99
106
  git-helper clean-branches
100
107
  ```
101
108
 
102
- ### `empty-commit`
103
-
104
- For some reason, I'm always forgetting the commands to create an empty commit. So with this command, it becomes easy. The commit message of this commit will be `'Empty commit'`. To run the command, run:
105
-
106
- ```bash
107
- git-helper empty-commit
108
- ```
109
-
110
- ### `merge-request`
109
+ ### `code-request`
111
110
 
112
- This command can be used to handily make new GitLab merge requests and to accept merge requests from the command line. The command uses the Ruby wrapper [`Gitlab`](https://github.com/NARKOZ/gitlab) to do this, so make sure you have a `.git_config.yml` file set up in the home directory of your computer. For instructions on how to do that, see [Usage](#usage).
111
+ This command can be used to handily make new GitHub/GitLab pull/merge requests and to merge those requests from the command line. The command uses either the [`Octokit::Client`](https://octokit.github.io/octokit.rb/Octokit/Client.html) or [`Gitlab` client](https://github.com/NARKOZ/gitlab) to do this, so make sure you have a `~/.git_helper/config.yml` file set up in the home directory of your computer. For instructions on how to do that, see [Gem Usage](#gem-usage).
113
112
 
114
- After setup is complete, you can call the file, and send in a flag indicating whether to create a pull request, `-c`, or to merge a pull request, `-m`.
113
+ After setup is complete, you can call the file, and send in a flag indicating whether to create a code request, `-c`, or to merge a code request, `-m`.
115
114
 
116
115
  ```bash
117
- git-helper merge-request -c
116
+ git-helper code-request -c
118
117
  # OR
119
- git-helper merge-request -m
118
+ git-helper code-request -m
120
119
  ```
121
120
 
122
- If you're trying to create a merge request, the command will provide an autogenerated merge request title based on your branch name. It will separate the branch name by `'_'` if underscores are in the branch, or `'-'` if dashes are present. Then it will join the list of words together by spaces. If there's a pattern in the form of `jira-123` or `jira_123` in the first part of the branch name, then it'll add `JIRA-123` to the first part of the pull request. You can respond 'yes' or 'no'. If you respond 'no', you can provide your own merge request title.
121
+ If you're trying to create a code request, the command will provide an autogenerated code request title based on your branch name. It will separate the branch name by `'_'` if underscores are in the branch, or `'-'` if dashes are present. Then it will join the list of words together by spaces. If there's a pattern in the form of `jira-123` or `jira_123` in the first part of the branch name, then it'll add `JIRA-123` to the first part of the code request. You can respond 'yes' or 'no'. If you respond 'no', you can provide your own code request title.
123
122
 
124
123
  The command will also ask you if the default branch of the repository is the proper base branch to use. You can say 'yes' or 'no'. If you respond 'no', then you can give the command your chosen base base.
125
124
 
126
- Lastly, it'll ask the user to apply any merge request templates found at any `*/merge_request_template.md` file or any file in `.gitlab/merge_request_templates/*.md`. Applying any template is optional, and from the command's standpoint, a user can make an empty merge request if they desire. If a GitLab project automatically adds a template, then it may create a merge request with a default template anyway.
125
+ Lastly, for GitHub, it'll ask the user to apply any code request templates found at any `*/pull_request_template.md` file or any file in `.github/PULL_REQUEST_TEMPLATE/*.md`. Applying any template is optional, and a user can make an empty pull request if they desire. For GitLab, it'll ask the user to apply any merge request templates found at any `*/merge_request_template.md` file or any file in `.gitlab/merge_request_templates/*.md`. Applying any template is optional, and from the command's standpoint, a user can make an empty merge request if they desire. If a GitLab project automatically adds a template, then it may create a merge request with a default template anyway.
127
126
 
128
- If you're requesting to merge a merge request, the command will ask you the number ID of the merge request you wish to accept. The command will also ask you what type of merge to do: regular merging or squashing. The commit message to use during the merge/squash will be the title of the pull request.
127
+ If you're requesting to merge a code request, the command will ask you the number ID of the code request you wish to merge. For GitHub, the command will also ask you what type of merge to do: regular merging, squashing, or rebasing. The commit message to use during the merge/squash/rebase will be the title of the pull request. For GitLab, the command will also ask you what type of merge to do: regular merging or squashing. The commit message to use during the merge/squash will be the title of the merge request.
129
128
 
130
129
  If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
131
130
 
132
- ### `new-branch`
131
+ ### `empty-commit`
133
132
 
134
- This command is useful for making new branches in a repository on the command line. To run the command, run:
133
+ For some reason, I'm always forgetting the commands to create an empty commit. So with this command, it becomes easy. The commit message of this commit will be `'Empty commit'`. To run the command, run:
135
134
 
136
135
  ```bash
137
- git-helper new-branch
138
- # OR
139
- git-helper new-branch NEW_BRANCH_NAME
136
+ git-helper empty-commit
140
137
  ```
141
138
 
142
- The command either accepts a branch name right away or it will ask you for the name of your new branch. Make sure your input does not contain any spaces or special characters.
143
-
144
- ### `pull-request`
145
-
146
- This command can be used to handily make new GitHub pull requests and to merge pull requests from the command line. The command uses the [`Octokit::Client`](https://octokit.github.io/octokit.rb/Octokit/Client.html) to do this, so make sure you have a `.git_config.yml` file set up in the home directory of your computer. For instructions on how to do that, see [Usage](#usage).
139
+ ### `forget-local-commits`
147
140
 
148
- After setup is complete, you can call the file, and send in a flag indicating whether to create a pull request, `-c`, or to merge a pull request, `-m`.
141
+ This command is handy if you locally have a bunch of commits you wish to completely get rid of. This command basically does a hard reset to `origin/HEAD`. Once you forget them, they're completely gone, so run carefully. To test it out, run:
149
142
 
150
143
  ```bash
151
- git-helper pull-request -c
152
- # OR
153
- git-helper pull-request -m
144
+ git-helper forget-local-commits
154
145
  ```
155
146
 
156
- If you're trying to create a pull request, the command will provide an autogenerated pull request title based on your branch name. It will separate the branch name by `'_'` if underscores are in the branch, or `'-'` if dashes are present. Then it will join the list of words together by spaces. If there's a pattern in the form of `jira-123` or `jira_123` in the first part of the branch name, then it'll add `JIRA-123` to the first part of the pull request. You can respond 'yes' or 'no'. If you respond 'no', you can provide your own pull request title.
157
-
158
- The command will also ask you if the default branch of the repository is the proper base branch to use. You can say 'yes' or 'no'. If you respond 'no', then you can give the command your chosen base base.
147
+ ### `new-branch`
159
148
 
160
- Lastly, it'll ask the user to apply any pull request templates found at any `*/pull_request_template.md` file or any file in `.github/PULL_REQUEST_TEMPLATE/*.md`. Applying any template is optional, and a user can make an empty pull request if they desire.
149
+ This command is useful for making new branches in a repository on the command line. To run the command, run:
161
150
 
162
- If you're requesting to merge a pull request, the command will ask you the number ID of the pull request you wish to merge. The command will also ask you what type of merge to do: regular merging, squashing, or rebasing. The commit message to use during the merge/squash/rebase will be the title of the pull request.
151
+ ```bash
152
+ git-helper new-branch
153
+ # OR
154
+ git-helper new-branch NEW_BRANCH_NAME
155
+ ```
163
156
 
164
- If you're getting stuck, you can run the command with a `--help` flag instead, to get some more information.
157
+ The command either accepts a branch name right away or it will ask you for the name of your new branch. Make sure your input does not contain any spaces or special characters.
165
158
 
166
159
  ## Contributing
167
160
 
data/Rakefile CHANGED
@@ -1,37 +1 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
- begin
8
- require 'rdoc/task'
9
- rescue LoadError
10
- require 'rdoc/rdoc'
11
- require 'rake/rdoctask'
12
- RDoc::Task = Rake::RDocTask
13
- end
14
-
15
- RDoc::Task.new(:rdoc) do |rdoc|
16
- rdoc.rdoc_dir = 'rdoc'
17
- rdoc.title = 'Hubstats'
18
- rdoc.options << '--line-numbers'
19
- rdoc.rdoc_files.include('README.rdoc')
20
- rdoc.rdoc_files.include('lib/**/*.rb')
21
- end
22
-
23
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
- load 'rails/tasks/engine.rake'
25
-
26
- Bundler::GemHelper.install_tasks
27
-
28
- require 'rake/testtask'
29
-
30
- Rake::TestTask.new(:test) do |t|
31
- t.libs << 'lib'
32
- t.libs << 'test'
33
- t.pattern = 'test/**/*_test.rb'
34
- t.verbose = false
35
- end
36
-
37
- task :default => :test
1
+ require "bundler/gem_tasks"
@@ -22,16 +22,14 @@ arg :new_owner
22
22
  desc "Update a repository's remote URLs from an old GitHub owner to a new owner."
23
23
  command 'change-remote' do |c|
24
24
  c.action do |global_options, options, args|
25
- require_relative '../lib/git_helper/change_remote.rb'
26
25
  raise ArgumentError, 'You must specify an old owner and a new owner' unless args.count == 2
27
- GitHelper::ChangeRemote.new.execute(args[0], args[1])
26
+ GitHelper::ChangeRemote.new(args[0], args[1]).execute
28
27
  end
29
28
  end
30
29
 
31
30
  desc "Checks out the default branch of a repo based on the local remote branches."
32
31
  command 'checkout-default' do |c|
33
32
  c.action do |global_options, options, args|
34
- require_relative '../lib/git_helper/checkout_default.rb'
35
33
  GitHelper::CheckoutDefault.new.execute
36
34
  end
37
35
  end
@@ -39,7 +37,6 @@ end
39
37
  desc "Clean a repository's git branches."
40
38
  command 'clean-branches' do |c|
41
39
  c.action do |global_options, options, args|
42
- require_relative '../lib/git_helper/clean_branches.rb'
43
40
  GitHelper::CleanBranches.new.execute
44
41
  end
45
42
  end
@@ -47,27 +44,14 @@ end
47
44
  desc "Creates an empty commit with a basic commit message."
48
45
  command 'empty-commit' do |c|
49
46
  c.action do |global_options, options, args|
50
- require_relative '../lib/git_helper/empty_commit.rb'
51
47
  GitHelper::EmptyCommit.new.execute
52
48
  end
53
49
  end
54
50
 
55
- desc 'Create a GitLab merge request from the current branch.'
56
- command 'merge-request' do |c|
57
- c.switch [:c, :create], desc: 'Create a new pull request'
58
- c.switch [:m, :merge], desc: 'Merge an existing pull request'
59
-
51
+ desc "Reset your local git commits to origin/HEAD."
52
+ command 'forget-local-commits' do |c|
60
53
  c.action do |global_options, options, args|
61
- require_relative '../lib/git_helper/merge_request.rb'
62
- raise ArgumentError, "You must specify an action (either '-m'/'--merge' or '-c'/'--create')" unless options[:create] || options[:merge]
63
-
64
- options = global_options.merge(options)
65
-
66
- if options[:create]
67
- GitHelper::GitLabMergeRequest.new.create
68
- elsif options[:merge]
69
- GitHelper::GitLabMergeRequest.new.merge
70
- end
54
+ GitHelper::ForgetLocalCommits.new.execute
71
55
  end
72
56
  end
73
57
 
@@ -75,26 +59,24 @@ arg :new_branch_name, optional: true
75
59
  desc 'Create a new branch for features, bug fixes, or experimentation.'
76
60
  command 'new-branch' do |c|
77
61
  c.action do |global_options, options, args|
78
- require_relative '../lib/git_helper/new_branch.rb'
79
62
  GitHelper::NewBranch.new.execute(args[0])
80
63
  end
81
64
  end
82
65
 
83
- desc 'Create a GitHub pull request from the current branch.'
84
- command 'pull-request' do |c|
85
- c.switch [:c, :create], desc: 'Create a new pull request'
86
- c.switch [:m, :merge], desc: 'Merge an existing pull request'
66
+ desc 'Create or merge a GitHub or GitLab pull/merge request from the current branch. The command will figure out whether your code project belongs to GitHub or GitLab'
67
+ command 'code-request' do |c|
68
+ c.switch [:c, :create], desc: 'Create a new pull/merge request'
69
+ c.switch [:m, :merge], desc: 'Merge an existing pull/merge request'
87
70
 
88
71
  c.action do |global_options, options, args|
89
- require_relative '../lib/git_helper/pull_request.rb'
90
72
  raise ArgumentError, "You must specify an action (either '-m'/'--merge' or '-c'/'--create')" unless options[:create] || options[:merge]
91
73
 
92
74
  options = global_options.merge(options)
93
75
 
94
76
  if options[:create]
95
- GitHelper::GitHubPullRequest.new.create
77
+ GitHelper::CodeRequest.new.create
96
78
  elsif options[:merge]
97
- GitHelper::GitHubPullRequest.new.merge
79
+ GitHelper::CodeRequest.new.merge
98
80
  end
99
81
  end
100
82
  end
@@ -1,7 +1,5 @@
1
- require 'highline'
2
- require 'yaml'
3
- require 'gitlab'
4
- require 'octokit'
5
- require_relative 'git_helper/version'
1
+ Dir[File.expand_path(File.join(File.dirname(File.absolute_path(__FILE__)), 'git_helper')) + "/**/*.rb"].each do |file|
2
+ require_relative file
3
+ end
6
4
 
7
5
  module GitHelper; end
@@ -1,55 +1,68 @@
1
1
  module GitHelper
2
2
  class ChangeRemote
3
- def execute(old_owner, new_owner)
4
- current_dir = Dir.pwd
5
- nested_dirs = Dir.entries(current_dir).select do |entry|
6
- entry_dir = File.join(current_dir, entry)
3
+ attr_accessor :old_owner, :new_owner
4
+
5
+ def initialize(old_owner, new_owner)
6
+ @old_owner = old_owner
7
+ @new_owner = new_owner
8
+ end
9
+
10
+ def execute
11
+ original_dir = Dir.pwd
12
+ nested_dirs = Dir.entries(original_dir).select do |entry|
13
+ entry_dir = File.join(original_dir, entry)
7
14
  File.directory?(entry_dir) && !(entry == '.' || entry == '..')
8
15
  end
9
16
 
10
- nested_dirs.each do |dir|
11
- Dir.chdir dir
12
- if File.exist?('.git')
13
- puts "Found git directory: #{dir}."
14
- remotes = `git remote -v`.split("\n")
15
- remotes.each do |remote|
16
- if resp.include?(old_owner)
17
- puts " Git directory's remote is pointing to: '#{old_owner}'."
18
- swap_ssh(old_owner, new_owner, remote) if remote.scan(/(git@)/).any?
19
- swap_https(old_owner, new_owner, remote) if remote.scan(/(https:\/\/)/).any?
20
- else
21
- puts " No need to update remote."
22
- end
23
- end
24
- end
25
- Dir.chdir current_dir
17
+ nested_dirs.each do |nested_dir|
18
+ process_dir(nested_dir, original_dir)
19
+ end
20
+ end
21
+
22
+ private def process_dir(current_dir, original_dir)
23
+ Dir.chdir(current_dir)
24
+
25
+ if File.exist?('.git')
26
+ process_git_repository if cli.process_directory_remotes?(current_dir)
26
27
  end
28
+
29
+ Dir.chdir(original_dir)
27
30
  end
28
31
 
29
- private def swap_https(old_owner, new_owner, remote)
30
- repo = remote.scan(/https:\/\/[\S]+\/([\S]*).git/).first.first
31
- remote_name = remote.scan(/([a-zA-z]+)/).first.first
32
- source_name = scan(/https:\/\/([a-zA-z.]+)\//).first.first
33
- puts " Changing the remote URL #{remote_name} to be 'https://#{source_name}/#{new_owner}/#{repo}.git'."
34
- begin
35
- `git remote set-url #{remote_name} https://#{source_name}:#{new_owner}/#{repo}.git`
36
- puts " Done."
37
- rescue Exception => e
38
- puts " Could not complete: #{e.message}"
32
+ private def process_git_repository
33
+ local_code.remotes.each do |remote|
34
+ if remote.include?(old_owner)
35
+ process_remote(remote)
36
+ else
37
+ puts " Found remote is not pointing to #{old_owner}."
38
+ end
39
39
  end
40
+ puts "\n"
40
41
  end
41
42
 
42
- private def swap_ssh(old_owner, new_owner, remote)
43
- repo = remote.scan(/\/([\S]*).git/).first.first
44
- remote_name = remote.scan(/([a-zA-z]+)/).first.first
45
- source_name = remote.scan(/git@([a-zA-z.]+):/).first.first
46
- puts " Changing the remote URL #{remote_name} to be 'git@#{source_name}:#{new_owner}/#{repo}.git'."
47
- begin
48
- `git remote set-url #{remote_name} git@#{source_name}:#{new_owner}/#{repo}.git`
49
- puts " Done."
50
- rescue Exception => e
51
- puts " Could not complete: #{e.message}"
43
+ private def process_remote(remote)
44
+ remote_name = local_code.remote_name(remote)
45
+
46
+ if local_code.ssh_remote?(remote)
47
+ repo = local_code.remote_project(remote)
48
+ source_name = local_code.remote_source(remote)
49
+ remote_url = "git@#{source_name}:#{new_owner}/#{repo}.git"
50
+ elsif local_code.https_remote?(remote)
51
+ repo = local_code.remote_project(remote)
52
+ source_name = local_code.remote_source(remote)
53
+ remote_url = "https://#{source_name}/#{new_owner}/#{repo}.git"
52
54
  end
55
+
56
+ puts " Changing the remote URL #{remote_name} to be '#{remote_url}'."
57
+ local_code.change_remote(remote_name, remote_url)
58
+ end
59
+
60
+ private def local_code
61
+ @local_code ||= GitHelper::LocalCode.new
62
+ end
63
+
64
+ private def cli
65
+ @cli ||= GitHelper::HighlineCli.new
53
66
  end
54
67
  end
55
68
  end