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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00a6f3b64ac568ed9a8a00e28c157015f70a5892
4
- data.tar.gz: deeb00dac84552dd33e4f8bfde79aaae31d8ee90
3
+ metadata.gz: fdcb9347219a2d30498979c09bf72df199d467d6
4
+ data.tar.gz: e28679b3e3a1d502f3c2f332a8613931975baf40
5
5
  SHA512:
6
- metadata.gz: dd423813e1be43a52b56011ef9c49540b759342e04fca35981967b618166c8613a1ac4eff440b69bebaabcaa0bee6ce1edd15850220e704d239b206a47cef16a
7
- data.tar.gz: 44a626d66746877bb03092572b74541858607a81f698d46e69887747660f6f181a48f4b36e43c16a62fcac1dc0dec6a3a55f9dafb66e2774f5d9b8590a3d00f5
6
+ metadata.gz: fc67e861437e2e785b670c860875dd08defda841f517c1055bba73f7da5c418197171fe373c67b7edb60abd18f95797de25be237ba995431ee2e4372cd6f381c
7
+ data.tar.gz: 6fc5ae0086a7e7acd78976e465fb597148e6a255653faf9d5fe0c4a516cec496b55bc4aa9c11b377ae8625f97b43b1c9875caa870dd1c185dad70cea2ae2fc31
data/.travis.yml CHANGED
@@ -1,9 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3
6
- - 2.0.0
7
3
  - 2.1.8
8
4
  - 2.2.1
9
5
  - 2.3.0
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  * [Scope](#scope)
4
4
  * [Installation](#installation)
5
5
  * [Usage](#usage)
6
- * [setup](#setup)
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
- You will probably use this gem standalone, since i see no use in including it into another project.
28
- Just clone the Github repository, move to the gem directory and start using rake tasks.
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 Rake tasks:
31
+ Here are the main commands:
32
32
 
33
- ### setup
34
- The core of the library is automating multiple branches fetching, this action happens in two concurrent ways:
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
- Here are the arguments list:
46
- * **repo**: the repository name you want to automate git commant to
47
- * **base_dir**: the base path to your GIT repo, excluding its name (specified eralier). It defaults to HOME/Sites
48
- * **branches_file**: the path to the file, if any, listing the branches names. It defaults to the **.branches** file inside of your repo path (you need to add it to the .gitignore then)
49
- * **branches**: a list of branches separated by comma (optional), if specified it has precedence over the branches_file
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 branch to rebase with _origin/master_ frequently.
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
- As the other tasks, it depends on the setup one, so it accepts the same arguments:
57
- ```ruby
58
- # loads branches from the repo .branches file, repo si located at HOME/Sites/my_repo
59
- rake git_commands:rebase repo=my_repo
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
- It uses the same arguments as setup:
66
- ```ruby
67
- # purge old branches specified at the command line, repo is located at HOME/Sites/my_repo
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: rb_yyyy_mm_dd
68
+ It uses the following naming convention: *release/yyyy_mm_dd*
74
69
  A confirmation is asked to continue.
75
- It uses the same arguments as setup:
76
- ```ruby
77
- # aggregate branches listed in the /tmp/to_release file, repo si located at HOME/Sites/my_repo
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
@@ -1,6 +1,5 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rake/testtask'
3
- import 'lib/tasks/git_commands.rake'
4
3
 
5
4
  Rake::TestTask.new(:spec) do |t|
6
5
  t.libs << 'lib'
data/bin/aggregate ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path("../../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require "git_commands"
7
+
8
+ cli = GitCommands::CLI.new(command_name: "aggregate")
9
+ cli.call
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "git_utils"
4
+ require "git_commands"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
data/bin/purge ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path("../../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require "git_commands"
7
+
8
+ cli = GitCommands::CLI.new(command_name: "purge")
9
+ cli.call
data/bin/rebase ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path("../../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require "git_commands"
7
+
8
+ cli = GitCommands::CLI.new(command_name: "rebase")
9
+ cli.call
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.9.2"
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 = "exe"
18
- s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
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
- end
14
- end
15
-
16
- String.instance_eval do
17
- GitCommands::Colorize::CODES.each do |message, code|
18
- define_method(message) do
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
@@ -1,7 +1,7 @@
1
- require 'pathname'
2
- require 'fileutils'
3
- require 'net/http'
4
- require 'git_commands/prompt'
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 = 'github.com'
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, 'There is no connection!'
20
+ raise e, "There is no connection!"
21
21
  end
22
22
 
23
- def initialize(options = {})
23
+ attr_reader :out
24
+
25
+ def initialize(repo:, branches:, out: STDOUT)
24
26
  self.class.check_connection
25
- @repo = options[:repo] || error('Please specify a valid repository name!', ArgumentError)
26
- @base_dir = options[:base_dir] || BASE_DIR
27
- @branches_file = options[:branches_file] || repo_path.join('.branches')
28
- @branches = options[:branches].to_s.split(',')
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('Trying ro remove master!', GitError) if branch == 'master'
37
+ error("Trying ro remove master!", GitError) if branch == "master"
37
38
  warning("Removing branch: #{branch}")
38
- confirm('Remove local branch') do
39
+ confirm("Remove local branch") do
39
40
  `git branch -D #{branch}`
40
41
  end
41
- confirm('Remove remote branch') do
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('Proceed rebasing these branches') do
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} -f`
57
+ `git push origin #{branch}`
57
58
  `git checkout master`
58
59
  `git branch -D #{branch}`
59
- success 'Rebased successfully!'
60
+ success "Rebased successfully!"
60
61
  end
61
62
  end
62
63
  end
63
64
  end
64
65
 
65
66
  def aggregate
66
- temp = "temp_#{aggregate_name}"
67
- confirm("Aggregate branches into #{aggregate_name}") do
67
+ temp = "temp/#{@timestamp}"
68
+ aggregate = "release/#{@timestamp}"
69
+ confirm("Aggregate branches into #{aggregate}") do
68
70
  enter_repo do
69
- `git branch #{aggregate_name}`
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 #{aggregate_name}`
75
- `git checkout #{aggregate_name}`
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 'Aggregate branch created'
82
+ success "#{aggregate} branch created"
81
83
  end
82
84
  end
83
85
 
84
- private
85
-
86
- def repo_path
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 fetch_branches
91
- return unless @branches.empty? && File.exist?(@branches_file)
92
- warning('Loading branches file')
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 check_branches
97
- error('No branches have been loaded!', NoBranchesError) if @branches.empty?
98
- print_branches
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 ? 'es' : ''
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, '0')}. #{branch}" } + ['']
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('Halting unfinished rebase!', GitError) { `git rebase --abort` } if unfinished_rebase?
116
+ error("Halting unfinished rebase!", GitError) { `git rebase --abort` } if unfinished_rebase?
116
117
  end
117
118
 
118
- def enter_repo
119
- Dir.chdir repo_path do
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?(repo_path.join('.git', name))
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
@@ -1,24 +1,29 @@
1
- require 'git_commands/colorize'
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 warning(message, char = '*')
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('Aborted operation!', AbortError)
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
 
@@ -1,3 +1,3 @@
1
1
  module GitCommands
2
- VERSION = "2.1.0"
2
+ VERSION = "3.0.1"
3
3
  end
data/lib/git_commands.rb CHANGED
@@ -1 +1,6 @@
1
- require 'git_commands/command'
1
+ lib = File.expand_path("../../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require "git_commands/version"
5
+ require "git_commands/command"
6
+ require "git_commands/cli"
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: 2.1.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - costajob
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-22 00:00:00.000000000 Z
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.9.2
110
+ version: 2.1.8
105
111
  required_rubygems_version: !ruby/object:Gem::Requirement
106
112
  requirements:
107
113
  - - ">="
@@ -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