git_undo 0.1.0 → 1.0.0

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: 1853641d4fa2c27843cd182b10778d67b8858275
4
- data.tar.gz: d653f3b5f5e7be2bc42cad3f8aa4d44772202163
3
+ metadata.gz: aac96fa48b02f5d29aaba9d226f71d1b40639258
4
+ data.tar.gz: 78a41f2f818f03e83eee4477febdfd96a33bfb36
5
5
  SHA512:
6
- metadata.gz: 66aca25a347e2c1e0fbc5d3a2fc16af236679780c3dab97dfeb29ee8637ad0d6f52c5a1cce5d1b4fbc9369bd73cd2ea340dde8e9c03abbb15c0bf08c3feffbcf
7
- data.tar.gz: 309266bd6ddc9b052b5dd4bd0d51b7e8debc3f14a9fbb04661d3c2b0abfbf0d2c41137a857f5a395153d572ba7ef480e5451d1e80c302daa87956de624871dbd
6
+ metadata.gz: fdf1f74895bbb86627a3d98e96d39b14bb6eaed9012b1b9a7e3b3e6ad0af06545c5cdaf6f3afe863b4b2017e74b30f3a11da6e0ff71bc2ba27ccd3c25d4fb7ac
7
+ data.tar.gz: 34cebab2965d49e10244d927f0715891d89cd3acb0ce5351d29ae2acf4754782510d562ede8f42284d0289e015c009e625fa2eecc4b234cb2748f7ca448ab9b2
@@ -5,5 +5,5 @@ require_relative '../config/environment.rb'
5
5
  if history_file = ENV['HISTFILE']
6
6
  GitManager.new(history_file).run
7
7
  else
8
- GitManager.setup
8
+ GitSetup.new.run
9
9
  end
@@ -1,9 +1,23 @@
1
+ require 'pry'
2
+
1
3
  class GitManager
4
+ VALID_COMMANDS = ['add','commit','merge','checkout','co']
5
+
2
6
  def initialize(history_file)
3
7
  @history_file = history_file
4
8
  @command_list = []
5
9
  end
6
10
 
11
+ def run
12
+ get_commands
13
+ if last_command
14
+ puts "Last git command was: `#{last_command}`"
15
+ undo_message(last_command)
16
+ else
17
+ puts "No git commands found!"
18
+ end
19
+ end
20
+
7
21
  def get_commands
8
22
  File.open(@history_file) do |file|
9
23
  file.each do |line|
@@ -16,57 +30,57 @@ class GitManager
16
30
  end
17
31
 
18
32
  def last_command
19
- @command_list.last
33
+ last = ''
34
+ index = @command_list.length - 1
35
+ while last.empty? && index >= 0
36
+ command = parse_command(@command_list[index])[:action]
37
+ if VALID_COMMANDS.include?(command)
38
+ last = @command_list[index]
39
+ end
40
+ index -= 1
41
+ end
42
+ return last
20
43
  end
21
44
 
22
- def run
23
- get_commands
24
- puts "Last git command was: `#{last_command}`"
45
+ def parse_command(command)
46
+ tokens = command.split(' ')
47
+ action = tokens[1]
48
+ arguments = tokens[2..-1].join(' ')
49
+ return { action: action, arguments: arguments }
25
50
  end
26
51
 
27
- def self.setup
28
- puts "It looks like you haven't run the initial setup. Would you like to do so now (y/n)?"
29
- if gets.chomp.downcase == 'y'
30
- puts "This will involve appending an alias to your .bash_profile. Okay to proceed?"
31
- if gets.chomp.downcase == 'y'
32
- puts "Thanks!"
33
- update_bash_profile
34
- else
35
- puts "Goodbye!"
52
+ def undo_command(action, arguments)
53
+ case action
54
+ when 'add'
55
+ "git reset #{arguments}"
56
+ when 'commit'
57
+ "git reset --soft HEAD~"
58
+ when 'merge'
59
+ "git reset --merge ORIG_HEAD"
60
+ when 'checkout','co'
61
+ undo_command = "git checkout -"
62
+ if arguments.start_with?('-b')
63
+ #also delete branch
64
+ branch_name = arguments.split.last
65
+ undo_command += " && git branch -D #{branch_name}"
36
66
  end
37
- else
38
- puts "Goodbye!"
67
+ undo_command
39
68
  end
40
69
  end
41
70
 
42
- def self.update_bash_profile
43
- alias_exists = false
44
- # last_line_alias = false
45
- # position = 0
46
- # line_count = 0
47
- # index = 0
48
- # max_line_count = 0
49
- file = File.open(File.expand_path('~' + '/.bash_profile'),'r+')
50
-
51
- file.readlines.each do |line|
52
- # line_count += 1
53
- if /\A[\s]*alias/.match line
54
- if /\A[\s]*alias gitundo="HISTFILE=\$HISTFILE gitundo"\n\z/.match line
55
- alias_exists = true
56
- end
57
- # max_line_count = line_count
58
- # elsif last_line_alias
59
- # position = file.pos
60
- # last_line_alias = false
71
+ def undo_message(command)
72
+ command_hash = parse_command(command)
73
+ undo = undo_command(command_hash[:action], command_hash[:arguments])
74
+ if !undo
75
+ puts "Sorry, I don't know how to undo that command"
76
+ else
77
+ puts "To undo, run `#{undo}`\nWould you like to automatically run this command now? (y/N)"
78
+ option = gets.chomp.downcase
79
+ if option == 'y'
80
+ puts undo
81
+ %x[ #{undo} ]
61
82
  end
62
83
  end
63
- unless alias_exists
64
- file.write("# Git Undo\n")
65
- file.write("alias gitundo=\"HISTFILE=$HISTFILE gitundo\"\n")
66
- puts "Please run `source ~/.bash_profile && cd .` to reload configuration"
67
- end
68
- file.close
69
- # puts max_line_count + 1
70
- # puts position
71
84
  end
85
+
72
86
  end
metadata CHANGED
@@ -1,69 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_undo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Randall Reed, Jr.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-12 00:00:00.000000000 Z
11
+ date: 2016-06-08 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.11'
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.11'
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: '10.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: '10.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: '3.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: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Command line utility to read from bash history, find last git command,
56
70
  and output command to undo that operation.
57
71
  email:
58
72
  - randallreedjr@gmail.com
59
73
  executables:
60
- - gitundo
74
+ - git-undo
61
75
  extensions: []
62
76
  extra_rdoc_files: []
63
77
  files:
64
- - bin/gitundo
65
- - config/environment.rb
66
78
  - lib/git_undo/git_manager.rb
79
+ - config/environment.rb
80
+ - bin/git-undo
67
81
  homepage: https://github.com/randallreedjr/git_undo
68
82
  licenses:
69
83
  - MIT
@@ -74,17 +88,17 @@ require_paths:
74
88
  - lib
75
89
  required_ruby_version: !ruby/object:Gem::Requirement
76
90
  requirements:
77
- - - ">="
91
+ - - '>='
78
92
  - !ruby/object:Gem::Version
79
93
  version: '0'
80
94
  required_rubygems_version: !ruby/object:Gem::Requirement
81
95
  requirements:
82
- - - ">="
96
+ - - '>='
83
97
  - !ruby/object:Gem::Version
84
98
  version: '0'
85
99
  requirements: []
86
100
  rubyforge_project:
87
- rubygems_version: 2.5.1
101
+ rubygems_version: 2.0.14.1
88
102
  signing_key:
89
103
  specification_version: 4
90
104
  summary: Undo last git command