git_commands 2.1.0 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -4
- data/README.md +30 -36
- data/Rakefile +0 -1
- data/bin/aggregate +9 -0
- data/bin/console +1 -1
- data/bin/purge +9 -0
- data/bin/rebase +9 -0
- data/git_commands.gemspec +3 -3
- data/lib/git_commands/cli.rb +53 -0
- data/lib/git_commands/colorize.rb +6 -7
- data/lib/git_commands/command.rb +48 -51
- data/lib/git_commands/prompt.rb +12 -7
- data/lib/git_commands/version.rb +1 -1
- data/lib/git_commands.rb +6 -1
- metadata +12 -6
- data/lib/tasks/git_commands.rake +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdcb9347219a2d30498979c09bf72df199d467d6
|
4
|
+
data.tar.gz: e28679b3e3a1d502f3c2f332a8613931975baf40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc67e861437e2e785b670c860875dd08defda841f517c1055bba73f7da5c418197171fe373c67b7edb60abd18f95797de25be237ba995431ee2e4372cd6f381c
|
7
|
+
data.tar.gz: 6fc5ae0086a7e7acd78976e465fb597148e6a255653faf9d5fe0c4a516cec496b55bc4aa9c11b377ae8625f97b43b1c9875caa870dd1c185dad70cea2ae2fc31
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
* [Scope](#scope)
|
4
4
|
* [Installation](#installation)
|
5
5
|
* [Usage](#usage)
|
6
|
-
* [
|
6
|
+
* [Help](#help)
|
7
7
|
* [rebase](#rebase)
|
8
8
|
* [purge](#purge)
|
9
9
|
* [aggregate](#aggregate)
|
@@ -13,8 +13,6 @@ This script will facilitate adopting a subset of the branch-featuring workflow c
|
|
13
13
|
* each **feature** will have **its own branch**
|
14
14
|
* **feature** branches **derive** directly **form master**
|
15
15
|
* **integration** of master to feature branch happens **via rebasing**
|
16
|
-
* rebasing interactively is used on feature branch to **squash commits** to get a **single one per feature** branch
|
17
|
-
* **pushing with force** on local branches is not an issue
|
18
16
|
* **release** branches are created **aggregating multiple branches** into a new one
|
19
17
|
|
20
18
|
## Scope
|
@@ -24,56 +22,52 @@ The scope of this is helping out in the following cases:
|
|
24
22
|
|
25
23
|
## Installation
|
26
24
|
I assume you have GIT installed ;)
|
27
|
-
|
28
|
-
|
25
|
+
Just install the gem to use the binaries commands.
|
26
|
+
```
|
27
|
+
gem isntall git_commands
|
28
|
+
```
|
29
29
|
|
30
30
|
## Usage
|
31
|
-
Here are the main
|
31
|
+
Here are the main commands:
|
32
32
|
|
33
|
-
###
|
34
|
-
|
35
|
-
* from the command line, by splitting a comma separated list
|
36
|
-
* by reading a file where names are listed on each line
|
37
|
-
In case **no branches** are fetched the **script halts**.
|
33
|
+
### Help
|
34
|
+
Each command has an help option that can be displayed:
|
38
35
|
|
39
|
-
Is also assumed you're pointing to a project directory somewhere, so the script could move in and execute the GIT commands for you.
|
40
|
-
|
41
|
-
To call this task with arguments call it like that:
|
42
|
-
```ruby
|
43
|
-
rake git_commands:setup repo=git_repository base_dir=repo_path branches_file=file_listing_branches branches=list,of,branches,separated,by,comma
|
44
36
|
```
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
37
|
+
rebase --help
|
38
|
+
Usage: rebase --repo=./Sites/oro --branches=feature/add_bin,fetaure/remove_rake_task
|
39
|
+
-r, --repo=REPO The path to the existing GIT repository
|
40
|
+
-b, --branches=BRANCHES The comma-separated list of branches or the path to a .branches files
|
41
|
+
-h, --help Prints this help
|
42
|
+
```
|
50
43
|
|
51
44
|
### rebase
|
52
|
-
This is probably the most useful command in case you have several
|
53
|
-
Consider after the rebase the branch is pushed to origin with force, so be aware in case more than one programmer access the same branch from different computers.
|
45
|
+
This is probably the most useful command in case you have several branches to rebase with _origin/master_ frequently.
|
54
46
|
A confirmation is asked to continue.
|
55
47
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
48
|
+
```
|
49
|
+
rebase --repo=~/Sites/greatest_hits --branches=feature/love_me_tender,feature/teddybear,feature/return_to_sender
|
50
|
+
```
|
51
|
+
|
52
|
+
You can also specify as the *branches* the path to a file containing multiple branches on each line:
|
53
|
+
|
54
|
+
```
|
55
|
+
rebase --repo=~/Sites/greatest_hits --branches=~/greatest_hits/.branches
|
60
56
|
```
|
61
57
|
|
62
58
|
### purge
|
63
59
|
This command remove the specified branches locally and remotely.
|
64
60
|
A confirmation is asked before each removal.
|
65
|
-
|
66
|
-
```
|
67
|
-
|
68
|
-
rake git_commands:purge repo=my_repo branches=old_branch,older_branch,oldest_branch
|
61
|
+
|
62
|
+
```
|
63
|
+
purge --repo=~/temp/top_20 --branches=release/in_the_ghetto
|
69
64
|
```
|
70
65
|
|
71
66
|
### aggregate
|
72
67
|
It should be useful to aggregate your branches into a single one in case you want to create a release branch.
|
73
|
-
It uses the following naming convention:
|
68
|
+
It uses the following naming convention: *release/yyyy_mm_dd*
|
74
69
|
A confirmation is asked to continue.
|
75
|
-
|
76
|
-
```
|
77
|
-
|
78
|
-
rake git_commands:aggregate repo=my_repo branches_file=/tmp/to_release
|
70
|
+
|
71
|
+
```
|
72
|
+
aggregate --repo=~/Sites/greatest_hits --branches=~/greatest_hits/.branches
|
79
73
|
```
|
data/Rakefile
CHANGED
data/bin/aggregate
ADDED
data/bin/console
CHANGED
data/bin/purge
ADDED
data/bin/rebase
ADDED
data/git_commands.gemspec
CHANGED
@@ -11,11 +11,11 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.summary = "Utility library to rebase and aggregate your project branches"
|
12
12
|
s.homepage = "https://github.com/costajob/git_commands.git"
|
13
13
|
s.license = "MIT"
|
14
|
-
s.required_ruby_version = ">= 1.
|
14
|
+
s.required_ruby_version = ">= 2.1.8"
|
15
15
|
|
16
16
|
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|s|features)/}) }
|
17
|
-
s.bindir = "
|
18
|
-
s.executables =
|
17
|
+
s.bindir = "bin"
|
18
|
+
s.executables = %w[rebase aggregate purge]
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
s.add_development_dependency "bundler", "~> 1.11"
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "optparse"
|
2
|
+
require "git_commands/command"
|
3
|
+
|
4
|
+
module GitCommands
|
5
|
+
class CLI
|
6
|
+
VALID_COMMANDS = %w[rebase aggregate purge]
|
7
|
+
|
8
|
+
class UnknownCommandError < ArgumentError; end
|
9
|
+
|
10
|
+
def initialize(command_name:, args: ARGV, out: STDOUT, command_klass: Command)
|
11
|
+
@command_name = check_command_name(command_name)
|
12
|
+
@command_klass = command_klass
|
13
|
+
@args = args
|
14
|
+
@out = out
|
15
|
+
@repo = nil
|
16
|
+
@branches = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def call
|
20
|
+
parser.parse!(@args)
|
21
|
+
command = @command_klass.new(repo: @repo, branches: @branches)
|
22
|
+
command.send(@command_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
private def create_command
|
26
|
+
return @command if @command
|
27
|
+
end
|
28
|
+
|
29
|
+
private def check_command_name(name)
|
30
|
+
return name if VALID_COMMANDS.include?(name)
|
31
|
+
fail UnknownCommandError, "#{name} is not a supported command"
|
32
|
+
end
|
33
|
+
|
34
|
+
private def parser
|
35
|
+
OptionParser.new do |opts|
|
36
|
+
opts.banner = "Usage: #{@command_name} --repo=./Sites/oro --branches=feature/add_bin,fetaure/remove_rake_task"
|
37
|
+
|
38
|
+
opts.on("-rREPO", "--repo=REPO", "The path to the existing GIT repository") do |repo|
|
39
|
+
@repo = repo
|
40
|
+
end
|
41
|
+
|
42
|
+
opts.on("-bBRANCHES", "--branches=BRANCHES", "The comma-separated list of branches or the path to a .branches files") do |branches|
|
43
|
+
@branches = branches
|
44
|
+
end
|
45
|
+
|
46
|
+
opts.on("-h", "--help", "Prints this help") do
|
47
|
+
@out.puts opts
|
48
|
+
exit
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -10,13 +10,12 @@ module GitCommands
|
|
10
10
|
:grey => 37
|
11
11
|
}
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
"\e[#{code}m#{self}\e[0m"
|
13
|
+
refine String do
|
14
|
+
GitCommands::Colorize::CODES.each do |message, code|
|
15
|
+
define_method(message) do
|
16
|
+
"\e[#{code}m#{self}\e[0m"
|
17
|
+
end
|
18
|
+
end
|
20
19
|
end
|
21
20
|
end
|
22
21
|
end
|
data/lib/git_commands/command.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "pathname"
|
2
|
+
require "fileutils"
|
3
|
+
require "net/http"
|
4
|
+
require "git_commands/prompt"
|
5
5
|
|
6
6
|
module GitCommands
|
7
7
|
class Command
|
@@ -9,36 +9,37 @@ module GitCommands
|
|
9
9
|
|
10
10
|
class GitError < StandardError; end
|
11
11
|
class NoBranchesError < StandardError; end
|
12
|
+
class NoentRepositoryError < ArgumentError; end
|
12
13
|
|
13
|
-
GITHUB_HOST =
|
14
|
-
BASE_DIR = File.join(ENV['HOME'], 'Sites')
|
14
|
+
GITHUB_HOST = "github.com"
|
15
15
|
UNFINISHED_REBASE_FILES = %w(rebase-merge rebase-apply)
|
16
16
|
|
17
17
|
def self.check_connection
|
18
|
-
!!Net::HTTP.new(GITHUB_HOST).head(
|
18
|
+
!!Net::HTTP.new(GITHUB_HOST).head("/")
|
19
19
|
rescue Errno::ENETUNREACH => e
|
20
|
-
raise e,
|
20
|
+
raise e, "There is no connection!"
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
attr_reader :out
|
24
|
+
|
25
|
+
def initialize(repo:, branches:, out: STDOUT)
|
24
26
|
self.class.check_connection
|
25
|
-
@
|
26
|
-
@
|
27
|
-
@
|
28
|
-
@
|
29
|
-
fetch_branches
|
27
|
+
@out = out
|
28
|
+
@repo = fetch_repo(repo)
|
29
|
+
@branches = fetch_branches(branches)
|
30
|
+
@timestamp = Time.new.strftime("%Y-%m-%d")
|
30
31
|
check_branches
|
31
32
|
end
|
32
33
|
|
33
34
|
def purge
|
34
35
|
enter_repo do
|
35
36
|
@branches.each do |branch|
|
36
|
-
error(
|
37
|
+
error("Trying ro remove master!", GitError) if branch == "master"
|
37
38
|
warning("Removing branch: #{branch}")
|
38
|
-
confirm(
|
39
|
+
confirm("Remove local branch") do
|
39
40
|
`git branch -D #{branch}`
|
40
41
|
end
|
41
|
-
confirm(
|
42
|
+
confirm("Remove remote branch") do
|
42
43
|
`git push origin :#{branch}`
|
43
44
|
end
|
44
45
|
end
|
@@ -46,90 +47,86 @@ module GitCommands
|
|
46
47
|
end
|
47
48
|
|
48
49
|
def rebase
|
49
|
-
confirm(
|
50
|
+
confirm("Proceed rebasing these branches") do
|
50
51
|
enter_repo do
|
51
52
|
@branches.each do |branch|
|
52
53
|
warning("Rebasing branch: #{branch}")
|
53
54
|
`git checkout #{branch}`
|
54
55
|
`git pull origin #{branch}`
|
55
56
|
rebase_with_master
|
56
|
-
`git push origin #{branch}
|
57
|
+
`git push origin #{branch}`
|
57
58
|
`git checkout master`
|
58
59
|
`git branch -D #{branch}`
|
59
|
-
success
|
60
|
+
success "Rebased successfully!"
|
60
61
|
end
|
61
62
|
end
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
65
66
|
def aggregate
|
66
|
-
temp = "
|
67
|
-
|
67
|
+
temp = "temp/#{@timestamp}"
|
68
|
+
aggregate = "release/#{@timestamp}"
|
69
|
+
confirm("Aggregate branches into #{aggregate}") do
|
68
70
|
enter_repo do
|
69
|
-
`git branch #{
|
71
|
+
`git branch #{aggregate}`
|
70
72
|
@branches.each do |branch|
|
71
73
|
warning("Merging branch: #{branch}")
|
72
74
|
`git checkout -b #{temp} origin/#{branch} --no-track`
|
73
75
|
rebase_with_master
|
74
|
-
`git rebase #{
|
75
|
-
`git checkout #{
|
76
|
+
`git rebase #{aggregate}`
|
77
|
+
`git checkout #{aggregate}`
|
76
78
|
`git merge #{temp}`
|
77
79
|
`git branch -d #{temp}`
|
78
80
|
end
|
79
81
|
end
|
80
|
-
success
|
82
|
+
success "#{aggregate} branch created"
|
81
83
|
end
|
82
84
|
end
|
83
85
|
|
84
|
-
private
|
85
|
-
|
86
|
-
|
87
|
-
@repo_path ||= Pathname::new(File.join(@base_dir, @repo))
|
86
|
+
private def fetch_repo(repo)
|
87
|
+
return Pathname::new(repo) if File.exist?(repo)
|
88
|
+
fail NoentRepositoryError, "#{repo} is not a valid GIT repository!"
|
88
89
|
end
|
89
90
|
|
90
|
-
def
|
91
|
-
|
92
|
-
|
93
|
-
@branches = File.foreach(@branches_file).map(&:strip)
|
91
|
+
private def check_branches
|
92
|
+
error("No branches have been loaded!", NoBranchesError) if @branches.empty?
|
93
|
+
print_branches
|
94
94
|
end
|
95
95
|
|
96
|
-
def
|
97
|
-
|
98
|
-
|
96
|
+
private def fetch_branches(branches)
|
97
|
+
warning("Loading branches file")
|
98
|
+
return File.foreach(branches).map(&:strip) if File.exist?(branches)
|
99
|
+
branches.to_s.split(",").map(&:strip)
|
99
100
|
end
|
100
101
|
|
101
|
-
def print_branches
|
102
|
+
private def print_branches
|
102
103
|
size = @branches.to_a.size
|
103
|
-
plural = size > 1 ?
|
104
|
+
plural = size > 1 ? "es" : ""
|
104
105
|
success "Successfully loaded #{size} branch#{plural}:"
|
105
|
-
puts @branches.each_with_index.map { |branch, i| "#{(i+1).to_s.rjust(2,
|
106
|
+
@out.puts @branches.each_with_index.map { |branch, i| "#{(i+1).to_s.rjust(2, "0")}. #{branch}" } + [""]
|
106
107
|
end
|
107
108
|
|
108
|
-
def pull_master
|
109
|
+
private def pull_master
|
109
110
|
`git checkout master`
|
110
111
|
`git pull`
|
111
112
|
end
|
112
113
|
|
113
|
-
def rebase_with_master
|
114
|
+
private def rebase_with_master
|
114
115
|
`git rebase origin/master`
|
115
|
-
error(
|
116
|
+
error("Halting unfinished rebase!", GitError) { `git rebase --abort` } if unfinished_rebase?
|
116
117
|
end
|
117
118
|
|
118
|
-
def enter_repo
|
119
|
-
Dir.chdir
|
119
|
+
private def enter_repo
|
120
|
+
Dir.chdir(@repo) do
|
120
121
|
pull_master
|
121
122
|
yield
|
122
123
|
end
|
123
124
|
end
|
124
125
|
|
125
|
-
def unfinished_rebase?
|
126
|
+
private def unfinished_rebase?
|
126
127
|
UNFINISHED_REBASE_FILES.any? do |name|
|
127
|
-
File.exists?(
|
128
|
+
File.exists?(@repo.join(".git", name))
|
128
129
|
end
|
129
130
|
end
|
130
|
-
|
131
|
-
def aggregate_name
|
132
|
-
@aggregate_name ||= Time.new.strftime("rb_%Y-%m-%d")
|
133
|
-
end
|
134
131
|
end
|
135
132
|
end
|
data/lib/git_commands/prompt.rb
CHANGED
@@ -1,24 +1,29 @@
|
|
1
|
-
require
|
1
|
+
require "git_commands/colorize"
|
2
2
|
|
3
3
|
module GitCommands
|
4
|
+
using Colorize
|
4
5
|
module Prompt
|
5
6
|
VALID_ANSWERS = %w[Y y N n]
|
6
7
|
|
7
8
|
class AbortError < StandardError; end
|
8
9
|
|
9
|
-
def
|
10
|
+
def out
|
11
|
+
@out ||= STDOUT
|
12
|
+
end
|
13
|
+
|
14
|
+
def warning(message, char = "*")
|
10
15
|
spacer = (char * (message.size + 4)).grey
|
11
|
-
puts "\n", spacer, "#{char} #{message.to_s.yellow} #{char}", spacer, "\n"
|
16
|
+
out.puts "\n", spacer, "#{char} #{message.to_s.yellow} #{char}", spacer, "\n"
|
12
17
|
end
|
13
18
|
|
14
19
|
def error(message, error = StandardError)
|
15
|
-
puts message.to_s.red
|
20
|
+
out.puts message.to_s.red
|
16
21
|
yield if block_given?
|
17
22
|
fail error, message
|
18
23
|
end
|
19
24
|
|
20
25
|
def success(message)
|
21
|
-
puts message.to_s.green
|
26
|
+
out.puts message.to_s.green
|
22
27
|
end
|
23
28
|
|
24
29
|
def confirm(message)
|
@@ -29,14 +34,14 @@ module GitCommands
|
|
29
34
|
when /y/i
|
30
35
|
yield
|
31
36
|
else
|
32
|
-
error(
|
37
|
+
error("Aborted operation!", AbortError)
|
33
38
|
end
|
34
39
|
end
|
35
40
|
|
36
41
|
private
|
37
42
|
|
38
43
|
def ask(message)
|
39
|
-
print message.cyan
|
44
|
+
out.print message.cyan
|
40
45
|
input
|
41
46
|
end
|
42
47
|
|
data/lib/git_commands/version.rb
CHANGED
data/lib/git_commands.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: git_commands
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- costajob
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,7 +69,10 @@ dependencies:
|
|
69
69
|
description:
|
70
70
|
email:
|
71
71
|
- costajob@gmail.com
|
72
|
-
executables:
|
72
|
+
executables:
|
73
|
+
- rebase
|
74
|
+
- aggregate
|
75
|
+
- purge
|
73
76
|
extensions: []
|
74
77
|
extra_rdoc_files: []
|
75
78
|
files:
|
@@ -80,15 +83,18 @@ files:
|
|
80
83
|
- README.md
|
81
84
|
- Rakefile
|
82
85
|
- Vagrantfile
|
86
|
+
- bin/aggregate
|
83
87
|
- bin/console
|
88
|
+
- bin/purge
|
89
|
+
- bin/rebase
|
84
90
|
- bin/setup
|
85
91
|
- git_commands.gemspec
|
86
92
|
- lib/git_commands.rb
|
93
|
+
- lib/git_commands/cli.rb
|
87
94
|
- lib/git_commands/colorize.rb
|
88
95
|
- lib/git_commands/command.rb
|
89
96
|
- lib/git_commands/prompt.rb
|
90
97
|
- lib/git_commands/version.rb
|
91
|
-
- lib/tasks/git_commands.rake
|
92
98
|
homepage: https://github.com/costajob/git_commands.git
|
93
99
|
licenses:
|
94
100
|
- MIT
|
@@ -101,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
107
|
requirements:
|
102
108
|
- - ">="
|
103
109
|
- !ruby/object:Gem::Version
|
104
|
-
version: 1.
|
110
|
+
version: 2.1.8
|
105
111
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
112
|
requirements:
|
107
113
|
- - ">="
|
data/lib/tasks/git_commands.rake
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'git_commands/command'
|
2
|
-
|
3
|
-
namespace :git_commands do
|
4
|
-
desc <<END
|
5
|
-
Setup the command instance:
|
6
|
-
> rake git_utils:setup repo=git_repository base_dir=repo_path branches_file=file_listing_branches branches=list,of,branches,separated,by,comma
|
7
|
-
END
|
8
|
-
task :setup do
|
9
|
-
@command = GitCommands::Command::new(:repo => ENV['repo'],
|
10
|
-
:base_dir => ENV['base_dir'],
|
11
|
-
:branches_file => ENV['branches_file'],
|
12
|
-
:branches => ENV['branches'])
|
13
|
-
end
|
14
|
-
|
15
|
-
desc 'Purge specified branches locally and from origin'
|
16
|
-
task :purge => :setup do
|
17
|
-
@command.purge
|
18
|
-
end
|
19
|
-
|
20
|
-
desc 'Rebase specified branches with master'
|
21
|
-
task :rebase => :setup do
|
22
|
-
@command.rebase
|
23
|
-
end
|
24
|
-
|
25
|
-
desc 'Aggregate specified branches into a single one'
|
26
|
-
task :aggregate => :setup do
|
27
|
-
@command.aggregate
|
28
|
-
end
|
29
|
-
end
|