Class: Girc::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/girc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd = 'git', v = true) ⇒ Console

Returns a new instance of Console.



6
7
8
9
# File 'lib/girc.rb', line 6

def initialize cmd = 'git', v = true
  @git = cmd
  @v = v
end

Instance Attribute Details

#gitObject

Returns the value of attribute git.



4
5
6
# File 'lib/girc.rb', line 4

def git
  @git
end

Instance Method Details

#branchesObject

all branches include remote branches



46
47
48
49
50
51
52
53
54
# File 'lib/girc.rb', line 46

def branches
  branch_list = Array.new
  `#{@git} branch -a`.split("\n").each do |line|
    line.gsub!('* ', '')
    line.gsub!(/\s/, '')
    branch_list << line unless branch_list.include? line
  end
  branch_list
end

#configObject

return config list as a hash



31
32
33
34
35
36
37
38
# File 'lib/girc.rb', line 31

def config
  h = Hash.new
  `#{@git} config --list`.split("\n").each do |line|
    key, value = line.split("=")
    h[key] = value
  end
  h
end

#current_branchObject

the branch currently working on



57
58
59
60
61
62
63
# File 'lib/girc.rb', line 57

def current_branch
  `#{@git} branch`.split("\n").each do |line|
    if /\*/.match line
      return line.gsub('* ', '')
    end
  end
end

#del_branch(branch, remote = nil) ⇒ Object

delete a branch



97
98
99
100
101
# File 'lib/girc.rb', line 97

def del_branch branch, remote=nil
  rslt = `#{@git} branch -d #{branch}`
  raise "Cat not delete branch #{branch}: #{rslt}" unless $?.success?
  `#{@git} push #{remote} :#{branch}` if remote
end

#info(msg) ⇒ Object



11
12
13
14
# File 'lib/girc.rb', line 11

def info msg
  return unless @v
  puts "[GITC] #{msg}" if msg.size > 0
end

#meObject

return the value of user.name in configuration



41
42
43
# File 'lib/girc.rb', line 41

def me
  config["user.name"] || "?"
end

#modified_filesObject

return modified files (without additions/deletions)



20
21
22
23
24
25
26
27
28
# File 'lib/girc.rb', line 20

def modified_files
  files = Array.new
  `#{@git} status`.split("\n").each do |line|
    if /modified\:\s+(?<file_>.+)/ =~ line
      files << File.expand_path(file_)
    end
  end
  files
end

#new_branch(branch, remote = nil) ⇒ Object

create a new branch, if remote set, push it to remote too then switch to that branch



89
90
91
92
93
94
# File 'lib/girc.rb', line 89

def new_branch branch, remote=nil
  raise "You need clean up you working directory" unless wd_clean?
  raise "Branch #{branch} already exists" if self.branches.include? branch
  `#{@git} checkout -b #{branch}`
  `#{@git} push #{remote} #{branch}` if remote
end

#pull(remote = 'origin') ⇒ Object

pull from the remote use fetch/merge



78
79
80
81
82
83
84
85
# File 'lib/girc.rb', line 78

def pull remote = 'origin'
  cb = self.current_branch
  info "Fetch from #{remote}"
  rslt = `#{@git} fetch #{remote}`
  raise "fetch failed with message: #{rslt}" unless $?.success?
  info rslt
  info `#{@git} merge #{remote}/#{cb}`
end

#rebase(remote = 'origin', branch = 'develop') ⇒ Object

remote from a specified remote ref



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/girc.rb', line 118

def rebase remote = 'origin', branch = 'develop'
  cb = self.current_branch
  self.stash

  if branch == self.current_branch
    info "Rebase pull from remote"
    `#{@git} pull --rebase #{remote} #{branch}`
  else
    info "Switch to branch #{branch}"
    `#{@git} fetch #{remote}`
    rslt = `#{@git} checkout #{branch}`
    raise "Checkout failed: #{rslt}" unless $?.success?
    info "Update (rabase) branch"
    rslt = `#{@git} pull --rebase #{remote} #{branch}`
    raise "Rebase pull for #{branch} failed: #{rslt}"
    info "Switch back to branch #{cb}"
    `#{@git} checkout #{cb}`
    info "Merge from #{branch}"
    rslt = `#{@git} merge #{branch}`
    raise "Merge with #{branch} failed: #{rslt}"
  end

  self.stash_pop
end

#stashObject



103
104
105
106
107
108
109
# File 'lib/girc.rb', line 103

def stash 
  unless wd_clean?
    info "Save you change to stash"
    `#{@git} add .`
    `#{@git} stash`
  end
end

#stash_popObject



111
112
113
114
115
# File 'lib/girc.rb', line 111

def stash_pop
  raise "You may clean up you work directroy first before pop out from the stash" unless wd_clean?
  info "Pop out from you last stash"
  `#{@git} stash pop`
end

#wd_clean?Boolean

is the working directory has modified file

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/girc.rb', line 66

def wd_clean?
  clean = true
  `#{@git} status`.split("\n").each do |line|
    clean = false if /Changes/.match line
  end
  clean
end