git_commands 3.0.5 → 3.1.6

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: c525d19105de0f72959b99b1e287d27315d38c53
4
- data.tar.gz: 9d43e49473f0431e72d74be76c780e829fd42da0
3
+ metadata.gz: 2115eda28aa459f4ca512e8e734cd859702da356
4
+ data.tar.gz: 5950edb6f580c4c06f35cb763a3f75e746ed15d5
5
5
  SHA512:
6
- metadata.gz: 531c7c39db27dab500b11196560f826345635fdb98b3d4fde0900daa8742b96b6ff844203a7e374e7b2cce03837d34603f86c24c233fba5ad5f84300630af1eb
7
- data.tar.gz: e689eff2f19be16612c97b7e2ed6306848942cb698e66dba30c34b944ff94060da0f32b550675b91836cbbec086dfcec2c0433064044bdd8c52bd85c4ce7ad61
6
+ metadata.gz: c247805c7a0fb198f760c499f1f1bcfd12ea911adec58b8366e5b8b0058852ffd74bf0fc57afffd616ce6f13817f9aa7958e10a5638a7308f7f33036182a676a
7
+ data.tar.gz: d44b3d70b5b9ecbe9f3e22e4cfd9d327c26d5fff177228684793eae31fd097c4be37f0cab2e684fca30fbe4ea481726cfc4881ed1458d39bf8ac9454b44e8fbd
@@ -3,6 +3,8 @@ require "git_commands/command"
3
3
 
4
4
  module GitCommands
5
5
  class CLI
6
+ include Prompt
7
+
6
8
  VALID_COMMANDS = %w[rebase aggregate purge]
7
9
 
8
10
  class UnknownCommandError < ArgumentError; end
@@ -20,6 +22,8 @@ module GitCommands
20
22
  parser.parse!(@args)
21
23
  command = @command_klass.new(repo: @repo, branches: @branches)
22
24
  command.send(@command_name)
25
+ rescue StandardError => e
26
+ error(e.message)
23
27
  end
24
28
 
25
29
  private def create_command
@@ -39,7 +43,7 @@ module GitCommands
39
43
  @repo = repo
40
44
  end
41
45
 
42
- opts.on("-bBRANCHES", "--branches=BRANCHES", "The comma-separated list of branches or the path to a .branches files") do |branches|
46
+ opts.on("-bBRANCHES", "--branches=BRANCHES", "The comma-separated list of branches or the path to a file listing branches names on each line") do |branches|
43
47
  @branches = branches
44
48
  end
45
49
 
@@ -20,21 +20,28 @@ module GitCommands
20
20
  raise e, "There is no connection!"
21
21
  end
22
22
 
23
+ def self.git_repo?(repo)
24
+ `git rev-parse --is-inside-work-tree 2> /dev/null`.strip == "true"
25
+ end
26
+
27
+ def self.valid_branch?(branch)
28
+ `git rev-parse --verify origin/#{branch} 2> /dev/null`.match(/^[0-9a-z]+/)
29
+ end
30
+
23
31
  attr_reader :out
24
32
 
25
33
  def initialize(repo:, branches:, out: STDOUT)
26
34
  self.class.check_connection
27
35
  @out = out
28
36
  @repo = fetch_repo(repo)
29
- @branches = fetch_branches(branches)
37
+ @branches = fetch_branches(String(branches))
30
38
  @timestamp = Time.new.strftime("%Y-%m-%d")
31
- check_branches
39
+ print_branches
32
40
  end
33
41
 
34
42
  def purge
35
43
  enter_repo do
36
44
  @branches.each do |branch|
37
- error("Trying ro remove master!", GitError) if branch == "master"
38
45
  warning("Removing branch: #{branch}")
39
46
  confirm("Remove local branch") do
40
47
  `git branch -D #{branch}`
@@ -77,6 +84,7 @@ module GitCommands
77
84
  `git checkout #{aggregate}`
78
85
  `git merge #{temp}`
79
86
  `git branch -d #{temp}`
87
+ `git checkout master`
80
88
  end
81
89
  end
82
90
  success "#{aggregate} branch created"
@@ -84,19 +92,36 @@ module GitCommands
84
92
  end
85
93
 
86
94
  private def fetch_repo(repo)
87
- return Pathname::new(repo) if File.exist?(repo)
88
- fail NoentRepositoryError, "#{repo} is not a valid GIT repository!"
95
+ fail NoentRepositoryError, "'#{repo}' is not a valid GIT repository!" unless valid_repo?(repo)
96
+ Pathname::new(repo)
89
97
  end
90
98
 
91
- private def check_branches
92
- error("No branches have been loaded!", NoBranchesError) if @branches.empty?
93
- print_branches
99
+ private def valid_repo?(repo)
100
+ return false unless File.directory?(repo)
101
+ Dir.chdir(repo) do
102
+ self.class.git_repo?(repo)
103
+ end
94
104
  end
95
105
 
96
- private def fetch_branches(branches)
106
+ private def fetch_branches(src)
97
107
  warning("Loading branches file")
98
- return File.foreach(branches).map(&:strip) if File.exist?(branches)
99
- branches.to_s.split(",").map(&:strip)
108
+ branches = File.foreach(src).map(&:strip) if valid_file?(src)
109
+ branches ||= src.split(",").map(&:strip)
110
+ branches.tap do |list|
111
+ fail(NoBranchesError, "No branches have been loaded!") if list.empty?
112
+ list.each { |branch| check_branch(branch) }
113
+ end
114
+ end
115
+
116
+ private def check_branch(branch)
117
+ Dir.chdir(@repo) do
118
+ fail(GitError, "Master branch cannot be included into commands operations!") if branch == "master"
119
+ fail(GitError, "Branch '#{branch}' does not exist!") unless self.class.valid_branch?(branch)
120
+ end
121
+ end
122
+
123
+ private def valid_file?(branches)
124
+ File.exist?(branches) && !File.directory?(branches)
100
125
  end
101
126
 
102
127
  private def print_branches
@@ -113,7 +138,10 @@ module GitCommands
113
138
 
114
139
  private def rebase_with_master
115
140
  `git rebase origin/master`
116
- error("Halting unfinished rebase!", GitError) { `git rebase --abort` } if unfinished_rebase?
141
+ if unfinished_rebase?
142
+ `git rebase --abort`
143
+ fail(GitError, "Halting unfinished rebase!")
144
+ end
117
145
  end
118
146
 
119
147
  private def enter_repo
@@ -5,23 +5,19 @@ module GitCommands
5
5
  module Prompt
6
6
  VALID_ANSWERS = %w[Y y N n]
7
7
 
8
+ class AbortError < StandardError; end
9
+
8
10
  def out
9
11
  @out ||= STDOUT
10
12
  end
11
13
 
12
- def warning(message, char = "*")
13
- spacer = (char * (message.size + 4)).grey
14
- out.puts "\n", spacer, "#{char} #{message.to_s.yellow} #{char}", spacer, "\n"
15
- end
16
-
17
- def error(message, error = StandardError)
18
- out.puts message.to_s.red
19
- yield if block_given?
20
- fail error, message
14
+ def warning(message)
15
+ out.puts "\n#{message}...".yellow
21
16
  end
22
17
 
23
18
  def success(message)
24
19
  out.puts message.to_s.green
20
+ true
25
21
  end
26
22
 
27
23
  def confirm(message)
@@ -32,23 +28,21 @@ module GitCommands
32
28
  when /y/i
33
29
  yield
34
30
  else
35
- abort!("Aborted operation!")
31
+ fail(AbortError, "Aborted operation!")
36
32
  end
37
33
  end
38
34
 
39
- private
40
-
41
- def ask(message)
42
- out.print message.cyan
43
- input
44
- end
45
-
46
- def abort!(message)
35
+ def error(message)
47
36
  out.puts message.to_s.red
48
37
  exit
49
38
  end
50
39
 
51
- def input
40
+ private def ask(message)
41
+ out.print message.cyan
42
+ input
43
+ end
44
+
45
+ private def input
52
46
  STDIN.gets.chomp
53
47
  end
54
48
  end
@@ -1,3 +1,3 @@
1
1
  module GitCommands
2
- VERSION = "3.0.5"
2
+ VERSION = "3.1.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_commands
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.5
4
+ version: 3.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - costajob