Class: GitClient::Repository
- Inherits:
-
Object
- Object
- GitClient::Repository
- Defined in:
- lib/fwtoolkit/git_client/git_client.rb
Instance Method Summary collapse
- #add_files_to_index(files = nil) ⇒ Object
- #branch_exist?(branch_name) ⇒ Boolean
-
#commit(message) ⇒ Object
returns the commit hash.
- #current_branch ⇒ Object
- #ensure_branch_exists(branch_name) ⇒ Object
- #ensure_workdir_clean ⇒ Object
- #init ⇒ Object
-
#initialize(repo_path) ⇒ Repository
constructor
A new instance of Repository.
- #initialized? ⇒ Boolean
- #merge(from_branch, into_branch = nil, msg = nil) ⇒ Object
- #merging? ⇒ Boolean
- #pop_stash ⇒ Object
- #rebase(from_branch, into_branch = nil) ⇒ Object
- #remotes ⇒ Object
- #save_stash ⇒ Object
-
#status ⇒ Object
x index, y working_dir X Y Meaning ————————————————- [MD] not updated M [ MD] updated in index A [ MD] added to index D [ M] deleted from index R [ MD] renamed in index C [ MD] copied in index [MARC] index and work tree matches [ MARC] M work tree changed since index [ MARC] D deleted in work tree ————————————————- D D unmerged, both deleted A U unmerged, added by us U D unmerged, deleted by them U A unmerged, added by them D U unmerged, deleted by us A A unmerged, both added U U unmerged, both modified ————————————————- ? ? untracked ! ! ignored ————————————————-.
- #submodules ⇒ Object
- #submodules_update(conf) ⇒ Object
- #switch_branch(branch_name, force_create = true) ⇒ Object
- #workdir_clean? ⇒ Boolean
Constructor Details
#initialize(repo_path) ⇒ Repository
Returns a new instance of Repository.
17 18 19 20 21 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 17 def initialize(repo_path) @repo_root = repo_path @git_folder = File.join(repo_path, '.git') @merge = false end |
Instance Method Details
#add_files_to_index(files = nil) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 41 def add_files_to_index(files=nil) files = files ? files : '.' files = files.is_a?(Array) ? files : [files] files.each do |file| git "add \"#{file}\"" end end |
#branch_exist?(branch_name) ⇒ Boolean
135 136 137 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 135 def branch_exist?(branch_name) git('branch').split("\n").any? { |line| line.include? branch_name } end |
#commit(message) ⇒ Object
returns the commit hash
150 151 152 153 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 150 def commit() output = git "commit -m\"#{}\"" output[/\[\w* (\w*)\] .*/,1] end |
#current_branch ⇒ Object
36 37 38 39 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 36 def current_branch current_branch = git('branch').split("\n").delete_if { |line| line[0] != "*" } current_branch.first.gsub("* ", "") end |
#ensure_branch_exists(branch_name) ⇒ Object
139 140 141 142 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 139 def ensure_branch_exists(branch_name) return if branch_exist? branch_name raise GitError, "Branch #{branch_name} doesn't exists" end |
#ensure_workdir_clean ⇒ Object
144 145 146 147 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 144 def ensure_workdir_clean return if workdir_clean? raise GitError, 'The working directory is not clean' end |
#init ⇒ Object
31 32 33 34 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 31 def init raise GitError.new("The folder #{@repo_root} already contains a git repository") if initialized? git 'init' end |
#initialized? ⇒ Boolean
23 24 25 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 23 def initialized? File.exists? @git_folder end |
#merge(from_branch, into_branch = nil, msg = nil) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 162 def merge(from_branch, into_branch=nil, msg=nil) ensure_branch_exists from_branch ensure_branch_exists into_branch if into_branch ensure_workdir_clean git "checkout \"#{into_branch}\"" begin merge = true msg.nil? ? git("merge --no-ff #{from_branch}"): git("merge --no-ff #{from_branch} -m \"#{msg}\"") rescue GitError => e git 'merge --abort' if e.conflict? merge = false raise e end end |
#merging? ⇒ Boolean
27 28 29 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 27 def merging? return @merge end |
#pop_stash ⇒ Object
183 184 185 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 183 def pop_stash git 'stash pop' end |
#rebase(from_branch, into_branch = nil) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 187 def rebase(from_branch, into_branch=nil) ensure_branch_exists from_branch ensure_branch_exists into_branch if into_branch ensure_workdir_clean git "checkout \"#{into_branch}\"" begin merge = true git "rebase \"#{from_branch}\"" rescue GitError => e git 'rebase --abort' if e.conflict? merge = false raise e end end |
#remotes ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 103 def remotes remotes = git 'remote -v' remotes_hash = Hash.new remotes.split("\n").each do |line| split = line.split(' ')[0..1] remotes_hash[symbolicate(split[0])] = split[1] end if remotes remotes_hash end |
#save_stash ⇒ Object
179 180 181 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 179 def save_stash git 'stash save' end |
#status ⇒ Object
x index, y working_dir
X Y Meaning
-------------------------------------------------
[MD] not updated
M [ MD] updated in index
A [ MD] added to index
D [ M] deleted from index
R [ MD] renamed in index
C [ MD] copied in index
[MARC] index and work tree matches
[ MARC] M work tree changed since index
[ MARC] D deleted in work tree
-------------------------------------------------
D D unmerged, both deleted
A U unmerged, added by us
U D unmerged, deleted by them
U A unmerged, added by them
D U unmerged, deleted by us
A A unmerged, both added
U U unmerged, both modified
-------------------------------------------------
? ? untracked
! ! ignored
-------------------------------------------------
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 77 def status output = git 'status -s' status_hash = Hash.new return status_hash if output.length == 0 || merging? output.split("\n").each do |entry| raw_status = entry[0..1] status = case raw_status ##needs to split leaving the freaking spaces on! when / (M|D)/ then :idx_not_up_to_date when /(A|M|R)M/ then :idx_not_up_to_date when /A / then :idx_added when /M / then :idx_modified when /D / then :idx_deleted when /R / then :idx_renamed when /\?\?/ then :untracked when /(DD|AA|(U|D|A)(U|D|A))/ then :merging else :unknown end filename = entry.split(' ')[1] status_hash[status] ||= [] status_hash[status] << filename end status_hash end |
#submodules ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 113 def submodules submodules_hash = Hash.new return submodules_hash unless File.exists? File.join(@repo_root, '.gitmodules') submodules = git "config -f .gitmodules --get-regexp '^submodule\..*\.path$'" submodules.split("\n").each do |line| split = line.split(' ')[0..1] submodule_name = split[0][/^submodule.(.*).path$/, 1] submodule_path = split[1] submodule_remote = (git "config -f .gitmodules --get-all 'submodule.#{submodule_name}.url'").delete "\n" submodules_hash[submodule_path] = submodule_remote end submodules_hash end |
#submodules_update(conf) ⇒ Object
129 130 131 132 133 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 129 def submodules_update(conf) return if submodules.empty? git "submodule init" if(conf[:init]) git "submodule update --recursive" end |
#switch_branch(branch_name, force_create = true) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 155 def switch_branch(branch_name, force_create=true) ensure_branch_exists branch_name unless force_create git "branch #{branch_name}" if force_create && !branch_exist?(branch_name) git "checkout \"#{branch_name}\"" unless current_branch == branch_name end |
#workdir_clean? ⇒ Boolean
49 50 51 |
# File 'lib/fwtoolkit/git_client/git_client.rb', line 49 def workdir_clean? return status.empty? end |