girc 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = Girc
2
+
3
+ A console handler for git commands.
4
+
5
+
data/girc.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ require 'girc/version'
3
+
4
+ Gem::Specification.new 'girc', Girc::VERSION do |s|
5
+ s.description = "Girc is a ruby console handler for git commands."
6
+ s.summary = "a ruby console handler for git commands"
7
+ s.authors = ["Huang Wei"]
8
+ s.email = "huangw@pe-po.com"
9
+ s.homepage = "https://github.com/huangw/girc-gem"
10
+ s.files = `git ls-files`.split("\n") - %w[.gitignore]
11
+ s.test_files = Dir.glob("{spec,test}/**/*.rb")
12
+ s.rdoc_options = %w[--line-numbers --inline-source --title Girc --main README.rdoc --encoding=UTF-8]
13
+
14
+ end
15
+
@@ -0,0 +1,3 @@
1
+ module Girc
2
+ VERSION = '0.0.1'
3
+ end
data/lib/girc.rb ADDED
@@ -0,0 +1,138 @@
1
+
2
+ module Girc
3
+ class Console
4
+ attr_accessor :git
5
+
6
+ def initialize cmd = 'git', v = true
7
+ @git = cmd
8
+ @v = v
9
+ end
10
+
11
+ def info msg
12
+ return unless @v
13
+ puts "[GITC] #{msg}" if msg.size > 0
14
+ end
15
+
16
+ # general informations
17
+ # -----------------------
18
+
19
+ # return modified files (without additions/deletions)
20
+ def modified_files
21
+ files = Array.new
22
+ `#{@git} status`.split("\n").each do |line|
23
+ if /modified\:\s+(?<file_>.+)/ =~ line
24
+ files << File.expand_path(file_)
25
+ end
26
+ end
27
+ files
28
+ end
29
+
30
+ # return the value of user.name in configuration
31
+ def me
32
+ `#{@git} config --list`.split("\n").each do |line|
33
+ matches = /user\.name\=(.+)/.match line
34
+ return matches[1] if matches
35
+ end
36
+ "?"
37
+ end
38
+
39
+ # all branches include remote branches
40
+ def branches
41
+ branch_list = Array.new
42
+ `#{@git} branch -a`.split("\n").each do |line|
43
+ line.gsub!('* ', '')
44
+ line.gsub!(/\s/, '')
45
+ branch_list << line unless branch_list.include? line
46
+ end
47
+ branch_list
48
+ end
49
+
50
+ # the branch currently working on
51
+ def current_branch
52
+ `#{@git} branch`.split("\n").each do |line|
53
+ if /\*/.match line
54
+ return line.gsub('* ', '')
55
+ end
56
+ end
57
+ end
58
+
59
+ # is the working directory has modified file
60
+ def wd_clean?
61
+ clean = true
62
+ `#{@git} status`.split("\n").each do |line|
63
+ clean = false if /Changes/.match line
64
+ end
65
+ clean
66
+ end
67
+
68
+ # modifications
69
+ # --------------------
70
+
71
+ # pull from the remote use fetch/merge
72
+ def pull remote = 'origin'
73
+ cb = self.current_branch
74
+ info "Fetch from #{remote}"
75
+ rslt = `#{@git} fetch #{remote}`
76
+ raise "fetch failed with message: #{rslt}" unless $?.success?
77
+ info rslt
78
+ info `#{@git} merge #{remote}/#{cb}`
79
+ end
80
+
81
+ # create a new branch, if remote set, push it to remote too
82
+ # then switch to that branch
83
+ def new_branch branch, remote=nil
84
+ raise "You need clean up you working directory" unless wd_clean?
85
+ raise "Branch #{branch} already exists" if self.branches.include? branch
86
+ `#{@git} checkout -b #{branch}`
87
+ `#{@git} push #{remote} #{branch}` if remote
88
+ end
89
+
90
+ # delete a branch
91
+ def del_branch branch, remote=nil
92
+ rslt = `#{@git} branch -d #{branch}`
93
+ raise "Cat not delete branch #{branch}: #{rslt}" unless $?.success?
94
+ `#{@git} push #{remote} :#{branch}` if remote
95
+ end
96
+
97
+ def stash
98
+ unless wd_clean?
99
+ info "Save you change to stash"
100
+ `#{@git} add .`
101
+ `#{@git} stash`
102
+ end
103
+ end
104
+
105
+ def stash_pop
106
+ raise "You may clean up you work directroy first before pop out from the stash" unless wd_clean?
107
+ info "Pop out from you last stash"
108
+ `#{@git} stash pop`
109
+ end
110
+
111
+ # remote from a specified remote ref
112
+ def rebase remote = 'origin', branch = 'develop'
113
+ cb = self.current_branch
114
+ self.stash
115
+
116
+ if branch == self.current_branch
117
+ info "Rebase pull from remote"
118
+ `#{@git} pull --rebase #{remote} #{branch}`
119
+ else
120
+ info "Switch to branch #{branch}"
121
+ `#{@git} fetch #{remote}`
122
+ rslt = `#{@git} checkout #{branch}`
123
+ raise "Checkout failed: #{rslt}" unless $?.success?
124
+ info "Update (rabase) branch"
125
+ rslt = `#{@git} pull --rebase #{remote} #{branch}`
126
+ raise "Rebase pull for #{branch} failed: #{rslt}"
127
+ info "Switch back to branch #{cb}"
128
+ `#{@git} checkout #{cb}`
129
+ info "Merge from #{branch}"
130
+ rslt = `#{@git} merge #{branch}`
131
+ raise "Merge with #{branch} failed: #{rslt}"
132
+ end
133
+
134
+ self.stash_pop
135
+ end
136
+
137
+ end
138
+ end
data/new_b.rb ADDED
@@ -0,0 +1,10 @@
1
+ $: << 'lib'
2
+
3
+ require 'girc'
4
+
5
+ gc = Girc::Console.new
6
+
7
+ gc.new_branch 'solo', 'origin'
8
+ p gc.branches
9
+ `git co develop`
10
+ gc.del_branch 'solo', 'origin'
data/test.rb ADDED
@@ -0,0 +1,7 @@
1
+ $: << 'lib'
2
+
3
+ require 'girc'
4
+
5
+ gc = Girc::Console.new
6
+
7
+ p gc.send(:"#{ARGV[0]}")
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: girc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Huang Wei
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Girc is a ruby console handler for git commands.
15
+ email: huangw@pe-po.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.rdoc
21
+ - girc.gemspec
22
+ - lib/girc.rb
23
+ - lib/girc/version.rb
24
+ - new_b.rb
25
+ - test.rb
26
+ homepage: https://github.com/huangw/girc-gem
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options:
30
+ - --line-numbers
31
+ - --inline-source
32
+ - --title
33
+ - Girc
34
+ - --main
35
+ - README.rdoc
36
+ - --encoding=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.25
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: a ruby console handler for git commands
57
+ test_files: []