git_wrapper 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +4 -0
  3. data/README.md +74 -0
  4. data/Rakefile +1 -0
  5. data/git_wrapper.gemspec +21 -0
  6. data/lib/git_wrapper/commands/add.rb +21 -0
  7. data/lib/git_wrapper/commands/branch.rb +73 -0
  8. data/lib/git_wrapper/commands/checkout.rb +21 -0
  9. data/lib/git_wrapper/commands/commit.rb +21 -0
  10. data/lib/git_wrapper/commands/config.rb +26 -0
  11. data/lib/git_wrapper/commands/diff.rb +27 -0
  12. data/lib/git_wrapper/commands/fetch.rb +21 -0
  13. data/lib/git_wrapper/commands/git.rb +44 -0
  14. data/lib/git_wrapper/commands/init.rb +16 -0
  15. data/lib/git_wrapper/commands/log.rb +82 -0
  16. data/lib/git_wrapper/commands/merge.rb +16 -0
  17. data/lib/git_wrapper/commands/pull.rb +21 -0
  18. data/lib/git_wrapper/commands/push.rb +27 -0
  19. data/lib/git_wrapper/commands/remote.rb +49 -0
  20. data/lib/git_wrapper/commands/remove.rb +16 -0
  21. data/lib/git_wrapper/commands/reset.rb +36 -0
  22. data/lib/git_wrapper/commands/revert.rb +23 -0
  23. data/lib/git_wrapper/commands/shell.rb +67 -0
  24. data/lib/git_wrapper/commands/show.rb +40 -0
  25. data/lib/git_wrapper/commands/status.rb +17 -0
  26. data/lib/git_wrapper/commands/tag.rb +54 -0
  27. data/lib/git_wrapper/repository.rb +208 -0
  28. data/lib/git_wrapper/results/diff_name_status.rb +28 -0
  29. data/lib/git_wrapper/results/file_status.rb +21 -0
  30. data/lib/git_wrapper/results/log_info.rb +23 -0
  31. data/lib/git_wrapper/results/status_porcelain.rb +40 -0
  32. data/lib/git_wrapper/version.rb +3 -0
  33. data/lib/git_wrapper.rb +45 -0
  34. data/spec/repository_spec.rb +886 -0
  35. data/spec/spec_helper.rb +10 -0
  36. data/spec/status_porcelain_parser_spec.rb +78 -0
  37. data/spec/support/helpers/file_helper.rb +37 -0
  38. data/spec/support/matchers/git_status_matchers.rb +40 -0
  39. metadata +105 -0
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in git_wrapper.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # GitWrapper
2
+
3
+ OO git command line wrapper
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'git_wrapper'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install git_wrapper
18
+
19
+ ## Requirments
20
+
21
+ Git client must be installed and inculded in system path
22
+
23
+ ## Usage
24
+
25
+ ### Example
26
+
27
+ repo = Repository.new(folder_name)
28
+
29
+ repo.init
30
+ # or
31
+ repo.init_bare
32
+
33
+ repo.add 'file_name'
34
+ # or
35
+ repo.add_all
36
+
37
+ repo.status
38
+
39
+ repo.commit 'message'
40
+
41
+ repo.add_remote 'origin', 'git@localhost:repo.git'
42
+
43
+ repo.push 'origin', 'master'
44
+
45
+ ### Supported commands
46
+
47
+ - Add
48
+ - Branch
49
+ - Checkout
50
+ - Commit
51
+ - Config
52
+ - Diff
53
+ - Fecht
54
+ - Init
55
+ - Log
56
+ - Merge
57
+ - Pull
58
+ - Push
59
+ - Remote
60
+ - Remove
61
+ - Reset
62
+ - Revert
63
+ - Show
64
+ - Status
65
+ - Tag
66
+
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "git_wrapper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'git_wrapper'
7
+ s.version = GitWrapper::VERSION
8
+ s.authors = ['Gabriel Naiman']
9
+ s.email = ['gabynaiman@gmail.com']
10
+ s.homepage = 'https://github.com/gabynaiman/git_wrapper'
11
+ s.summary = 'OO git command line wrapper'
12
+ s.description = 'OO git command line wrapper'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_runtime_dependency 'nokogiri'
20
+ s.add_development_dependency 'rspec'
21
+ end
@@ -0,0 +1,21 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Add < Git
4
+
5
+ def all
6
+ @file = '-A'
7
+ self
8
+ end
9
+
10
+ def file(file_name)
11
+ @file = to_relative_path(file_name)
12
+ self
13
+ end
14
+
15
+ def command
16
+ "add \"#{@file}\""
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,73 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Branch < Git
4
+
5
+ def create(name)
6
+ @mode = :create
7
+ @name = name
8
+ self
9
+ end
10
+
11
+ def from(commit)
12
+ @commit = commit
13
+ self
14
+ end
15
+
16
+ def remove(name)
17
+ @mode = :remove
18
+ @name = name
19
+ self
20
+ end
21
+
22
+ def remote(remote)
23
+ @remote = remote
24
+ self
25
+ end
26
+
27
+ def list
28
+ @mode = :list
29
+ self
30
+ end
31
+
32
+ def current
33
+ @mode = :current
34
+ self
35
+ end
36
+
37
+ def command
38
+ command = 'branch '
39
+
40
+ if @mode == :create
41
+ command += "#{@name} #{@commit.nil? ? '' : @commit}"
42
+ elsif @mode == :remove
43
+ if @remote.nil?
44
+ command += "-D #{@name}"
45
+ else
46
+ command = "push #{@remote} --delete #{@name}"
47
+ end
48
+ elsif @mode == :list || @mode == :current
49
+ command += '-a'
50
+ else
51
+ raise 'Unespecified branch mode'
52
+ end
53
+
54
+ command
55
+ end
56
+
57
+ def result
58
+ return result_list if @mode == :list
59
+ return result_current if @mode == :current
60
+ super
61
+ end
62
+
63
+ def result_list
64
+ output.split("\n").map{|b| b[2..b.length]}
65
+ end
66
+
67
+ def result_current
68
+ output.split("\n").select{|b| b.start_with?('*')}.map{|b| b[2..b.length]}.first
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,21 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Checkout < Git
4
+
5
+ def commit(commit)
6
+ @commit = commit
7
+ self
8
+ end
9
+
10
+ def into(new_branch)
11
+ @new_branch = new_branch
12
+ self
13
+ end
14
+
15
+ def command
16
+ "checkout #{@commit} #{@new_branch.nil? ? '' : "-b #{@new_branch}" }"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Commit < Git
4
+
5
+ def message(message)
6
+ @message = message
7
+ self
8
+ end
9
+
10
+ def author(name, email)
11
+ @author = "#{name} <#{email}>"
12
+ self
13
+ end
14
+
15
+ def command
16
+ "commit -m \"#{@message}\" #{@author ? "--author \"#{@author}\"" : ''}"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Config < Git
4
+
5
+ def key(key)
6
+ @key = key
7
+ self
8
+ end
9
+
10
+ def value(value)
11
+ @value = value
12
+ self
13
+ end
14
+
15
+ def command
16
+ "config #{@key} #{@value}"
17
+ end
18
+
19
+ def result
20
+ return @success if @value || !@success
21
+ @output[0..(@output.length - 2)].strip
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Diff < Git
4
+
5
+ def with(commit)
6
+ @commit = commit
7
+ self
8
+ end
9
+
10
+ def reverse
11
+ @reverse = true
12
+ self
13
+ end
14
+
15
+ def command
16
+ "diff #{@commit}#{@reverse ? ' -R ' : ''} --name-status"
17
+ end
18
+
19
+ def result
20
+ output.split(/\n/).map do |line|
21
+ Results::DiffNameStatus.parse(line)
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Fetch < Git
4
+
5
+ def remote(remote)
6
+ @remote = remote
7
+ self
8
+ end
9
+
10
+ def all
11
+ @remote = '--all'
12
+ self
13
+ end
14
+
15
+ def command
16
+ "fetch #{@remote}"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Git
4
+ attr_reader :location_folder
5
+ attr_reader :output
6
+ attr_reader :error
7
+
8
+ def initialize(location_folder)
9
+ @location_folder = location_folder
10
+ end
11
+
12
+ def execute
13
+ begin
14
+ @output, @error, status = Shell.execute("git #{command}", :chdir => @location_folder)
15
+ @success = status.success?
16
+
17
+ log = {
18
+ :command => command,
19
+ :location_folder => @location_folder,
20
+ :output => @output,
21
+ :error => @error
22
+ }
23
+ GitWrapper.logger.debug "[GitWrapper] #{log}"
24
+
25
+ return result
26
+ rescue Exception => e
27
+ GitWrapper.logger.error "[GitWrapper] #{e.message}"
28
+ @error = e.message
29
+ return false
30
+ end
31
+ end
32
+
33
+ def result
34
+ @success
35
+ end
36
+
37
+ def to_relative_path(file_name)
38
+ base_folder = location_folder.gsub("\\", "/")
39
+ file_name.gsub("\\", "/").gsub(base_folder + "/", "")
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Init < Git
4
+
5
+ def bare
6
+ @bare = true
7
+ self
8
+ end
9
+
10
+ def command
11
+ "init#{@bare ? ' --bare' : ''}"
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,82 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Log < Git
4
+ ATTRIBUTES = {
5
+ :commit_hash => 'H',
6
+ :abbreviated_commit_hash => 'h',
7
+ :tree_hash => 'T',
8
+ :abbreviated_tree_hash => 't',
9
+ :parent_hashes => 'P',
10
+ :abbreviated_parent_hashes => 'p',
11
+ :author_name => 'an',
12
+ :author_name_mailmap => 'aN',
13
+ :author_email => 'ae',
14
+ :author_email_mailmap => 'aE',
15
+ :author_date => 'ad',
16
+ :author_date_rfc2822 => 'aD',
17
+ :author_date_relative => 'aR',
18
+ :author_date_unix => 'at',
19
+ :author_date_iso => 'ai',
20
+ :commiter_name => 'cn',
21
+ :commiter_name_mailmap => 'cN',
22
+ :commiter_email => 'ce',
23
+ :commiter_email_mailmap => 'cE',
24
+ :commiter_date => 'cd',
25
+ :commiter_date_rfc2822 => 'cD',
26
+ :commiter_date_relative => 'cR',
27
+ :commiter_date_unix => 'ct',
28
+ :commiter_date_iso => 'ci',
29
+ :ref_names => 'd',
30
+ :encoding => 'e',
31
+ :subject => 's',
32
+ :sanitized_subject_line => 'f',
33
+ :body => 'b',
34
+ :raw_body => 'B',
35
+ :commit_notes => 'N',
36
+ :reflog_selector => 'gD',
37
+ :shortened_reflog_selector => 'gd',
38
+ :reflog_subject => 'gs'
39
+ }
40
+
41
+ def file(file_name)
42
+ @file = to_relative_path(file_name)
43
+ self
44
+ end
45
+
46
+ def commit(commit)
47
+ @commit = commit
48
+ self
49
+ end
50
+
51
+ def command
52
+ command = "log --format=\"<log>#{xml_structure}</log>\""
53
+ command += " #{@commit}" if @commit
54
+ command += " \"#{@file}\"" if @file
55
+ command
56
+ end
57
+
58
+ def result
59
+ if output.nil?
60
+ return nil if @commit
61
+ return []
62
+ end
63
+
64
+ results = Nokogiri::XML("<logs>#{output}</logs>").xpath('logs/log').map do |element|
65
+ Results::LogInfo.new(Hash[*element.children.map { |node| [node.name.to_sym, node.text] }.flatten])
66
+ end
67
+
68
+ return results.first if @commit
69
+ results
70
+ end
71
+
72
+ private
73
+
74
+ def xml_structure
75
+ ATTRIBUTES.map { |attribute, placeholder|
76
+ "<#{attribute}>%#{placeholder}</#{attribute}>"
77
+ }.join
78
+ end
79
+
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,16 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Merge < Git
4
+
5
+ def commit(commit)
6
+ @commit = commit
7
+ self
8
+ end
9
+
10
+ def command
11
+ "merge #{@commit}"
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Pull < Git
4
+
5
+ def remote(remote)
6
+ @remote = remote
7
+ self
8
+ end
9
+
10
+ def branch(branch)
11
+ @branch = branch
12
+ self
13
+ end
14
+
15
+ def command
16
+ "pull #{@remote} #{@branch}"
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Push < Git
4
+
5
+ def remote(remote)
6
+ @remote = remote
7
+ self
8
+ end
9
+
10
+ def branch(branch)
11
+ @branch = branch
12
+ @mode = :branch
13
+ self
14
+ end
15
+
16
+ def tags
17
+ @mode = :tags
18
+ self
19
+ end
20
+
21
+ def command
22
+ "push #{@remote} #{@mode == :branch ? @branch : '--tags'}"
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,49 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Remote < Git
4
+
5
+ def name(name)
6
+ @name = name
7
+ self
8
+ end
9
+
10
+ def add(url)
11
+ @url = url
12
+ @mode = :add
13
+ self
14
+ end
15
+
16
+ def remove
17
+ @mode = :remove
18
+ self
19
+ end
20
+
21
+ def list
22
+ @mode = :list
23
+ self
24
+ end
25
+
26
+ def command
27
+ command = "remote "
28
+
29
+ if @mode == :add
30
+ command += "add #{@name} \"#{@url}\""
31
+ elsif @mode == :remove
32
+ command += "rm #{@name}"
33
+ elsif @mode == :list
34
+ command += "show"
35
+ else
36
+ raise "Unespecified remote mode"
37
+ end
38
+
39
+ command
40
+ end
41
+
42
+ def result
43
+ return @output.split(/\n/) if @mode == :list
44
+ super
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Remove < Git
4
+
5
+ def file(file_name)
6
+ @file = to_relative_path(file_name)
7
+ self
8
+ end
9
+
10
+ def command
11
+ "rm \"#{@file}\""
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Reset < Git
4
+
5
+ def commit(commit)
6
+ @commit = commit
7
+ self
8
+ end
9
+
10
+ def soft
11
+ @mode = :soft
12
+ self
13
+ end
14
+
15
+ def hard
16
+ @mode = :hard
17
+ self
18
+ end
19
+
20
+ def merge
21
+ @mode = :merge
22
+ self
23
+ end
24
+
25
+ def keep
26
+ @mode = :keep
27
+ self
28
+ end
29
+
30
+ def command
31
+ "reset #{@mode ? "--#{@mode}" : ''} #{@commit}"
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ module GitWrapper
2
+ module Commands
3
+ class Revert < Git
4
+
5
+ def commit(commit)
6
+ @commit = commit
7
+ @merge = false
8
+ self
9
+ end
10
+
11
+ def merge(commit)
12
+ @commit = commit
13
+ @merge = true
14
+ self
15
+ end
16
+
17
+ def command
18
+ "revert#{@merge ? ' -m 1' : ''} --no-edit #{@commit}"
19
+ end
20
+
21
+ end
22
+ end
23
+ end