Module: FalkorLib::Git
- Defined in:
- lib/falkorlib/git/base.rb
Overview
Management of Git operations
Class Method Summary collapse
-
.add(path, msg = "", options = {}) ⇒ Object
Add a file/whatever to Git and commit it Supported options: * :force [boolean]: force the add.
-
.branch?(path = Dir.pwd) ⇒ Boolean
Get the current git branch.
-
.command?(cmd) ⇒ Boolean
Check the availability of a given git command.
-
.commits?(path) ⇒ Boolean
Check if the repositories already holds some commits.
-
.config(key, dir = Dir.pwd, options = {}) ⇒ Object
config ###### Retrieve the Git configuration You can propose a pattern as key Supported options: * :list [boolean] list all configurations * :hash [boolean] return a Hash.
-
.config_warn(type = :subtrees) ⇒ Object
Raise a warning message if subtree/submodule section is not present.
-
.create_branch(branch, path = Dir.pwd) ⇒ Object
Create a new branch.
-
.create_remote(name, url, path = Dir.pwd, opts = {}) ⇒ Object
Create a new remote <name> targeting url <url> You can pass additional options expected by git remote add in <opts>, for instance as follows:.
-
.delete_branch(branch, path = Dir.pwd, opts = { :force => false }) ⇒ Object
Delete a branch.
-
.dirty?(path = Dir.pwd) ⇒ Boolean
Check if a git directory is in dirty mode git diff –shortstat 2> /dev/null | tail -n1.
-
.fetch(path = Dir.pwd) ⇒ Object
Fetch the latest changes.
-
.gitdir(path = Dir.pwd) ⇒ Object
Return the git root directory for the path (current directory by default).
-
.grab(branch, path = Dir.pwd, remote = 'origin') ⇒ Object
Grab a remote branch.
-
.init(path = Dir.pwd, _options = {}) ⇒ Object
Initialize a git repository.
-
.init?(path = Dir.pwd) ⇒ Boolean
Check if a git directory has been initialized.
-
.last_tag_commit(path = Dir.pwd) ⇒ Object
Get the last tag commit, or nil if no tag can be found.
-
.list_branch(path = Dir.pwd) ⇒ Object
Get an array of the local branches present (first element is always the current branch).
-
.list_files(path = Dir.pwd) ⇒ Object
List the files currently under version.
-
.list_tag(path = Dir.pwd) ⇒ Object
Get a hash table of tags under the format { <tag> => <commit> }.
-
.publish(branch, path = Dir.pwd, remote = 'origin') ⇒ Object
Publish a branch on the remote.
-
.remotes(path = Dir.pwd) ⇒ Object
List of Git remotes.
-
.remotes?(path = Dir.pwd) ⇒ Boolean
Check existence of remotes.
-
.rootdir(path = Dir.pwd) ⇒ Object
Return the Git working tree from the proposed path (current directory by default).
-
.submodule_init(path = Dir.pwd, submodules = , _options = {}) ⇒ Object
Initialize git submodule from the configuration.
-
.submodule_update(path = Dir.pwd) ⇒ Object
Update the Git submodules to the local registered version.
-
.submodule_upgrade(path = Dir.pwd) ⇒ Object
Upgrade the Git submodules to the latest HEAD version from the remote.
-
.subtree_diff(path = Dir.pwd) ⇒ Object
Show difference between local subtree(s) and their remotes“.
-
.subtree_init(path = Dir.pwd) ⇒ Object
Initialize git subtrees from the configuration.
-
.subtree_init?(path = Dir.pwd) ⇒ Boolean
Check if the subtrees have been initialized.
-
.subtree_up(path = Dir.pwd) ⇒ Object
(also: subtree_pull)
Pull the latest changes, assuming the git repository is not dirty.
-
.tag(name, path = Dir.pwd, opts = {}) ⇒ Object
Create a new tag You can add extra options to the git tag command through the opts hash.
Class Method Details
.add(path, msg = "", options = {}) ⇒ Object
Add a file/whatever to Git and commit it Supported options:
-
:force [boolean]: force the add
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/falkorlib/git/base.rb', line 243 def add(path, msg = "", = {}) exit_status = 0 dir = File.realpath(File.dirname(path)) root = rootdir(path) relative_path_to_root = Pathname.new( File.realpath(path) ).relative_path_from Pathname.new(root) real_msg = ((msg.empty?) ? "add '#{relative_path_to_root}'" : msg) opts = '-f' if [:force] Dir.chdir( dir ) do exit_status = run %( git add #{opts} #{path} git commit -s -m "#{real_msg}" #{path} ) end exit_status.to_i end |
.branch?(path = Dir.pwd) ⇒ Boolean
Get the current git branch
194 195 196 |
# File 'lib/falkorlib/git/base.rb', line 194 def branch?(path = Dir.pwd) list_branch(path)[0] end |
.command?(cmd) ⇒ Boolean
Check the availability of a given git command
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/falkorlib/git/base.rb', line 60 def command?(cmd) cg = MiniGit::Capturing.new cmd_list = cg.help :a => true # typical run: # usage: git [--version] [--help] [-C <path>] [-c name=value] # [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] # [-p|--paginate|--no-pager] [--no-replace-objects] [--bare] # [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] # <command> [<args>] # # available git commands in '/usr/local/Cellar/git/1.8.5.2/libexec/git-core' # # add [...] \ # [...] | The part we are interested in, delimited by '\n\n' sequence # [...] / # # 'git help -a' and 'git help -g' lists available subcommands and some # concept guides. See 'git help <command>' or 'git help <concept>' # to read about a specific subcommand or concept l = cmd_list.split("\n\n") l.shift # useless first part #ap l subl = l.each_index.select { |i| l[i] =~ /^\s\s+/ } # find sublines that starts with at least two whitespaces #ap subl return false if subl.empty? subl.any? { |i| l[i].split.include?(cmd) } end |
.commits?(path) ⇒ Boolean
Check if the repositories already holds some commits
50 51 52 53 54 55 56 57 |
# File 'lib/falkorlib/git/base.rb', line 50 def commits?(path) res = false Dir.chdir(path) do _stdout, _stderr, exit_status = Open3.capture3( "git rev-parse HEAD" ) res = (exit_status.to_i.zero?) end res end |
.config(key, dir = Dir.pwd, options = {}) ⇒ Object
config ###### Retrieve the Git configuration You can propose a pattern as key Supported options:
* :list [boolean] list all configurations
* :hash [boolean] return a Hash
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/falkorlib/git/base.rb', line 153 def config(key, dir = Dir.pwd, = {}) #info "Retrieve the Git configuration" res = nil if ([:list] || (key.is_a? Regexp) || (key =~ /\*/)) cg = MiniGit::Capturing.new(dir) res = (cg.config :list => true).split("\n") res.select! { |e| e.match(/^#{key}/) } unless key == '*' #res = res.map { |e| e.split('=') }.to_h if options[:hash] res = Hash[ res.map { |e| e.split('=') } ] if [:hash] else g = MiniGit.new(dir) res = g[key] res = { key => g[key] } if [:hash] end #ap res res end |
.config_warn(type = :subtrees) ⇒ Object
Raise a warning message if subtree/submodule section is not present
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/falkorlib/git/base.rb', line 475 def config_warn(type = :subtrees) warn "You shall setup 'Falkorlib.config.git[#{type.to_sym}]' to configure #{type} as follows:" warn " FalkorLib.config.git do |c|" warn " c[#{type.to_sym}] = {" warn " '<subdir>' => {" warn " :url => '<giturl>'," warn " :branch => 'develop' # if different from master" warn " }," warn " }" warn " end" if type == :submodules warn "This will configure the Git submodule into FalkorLib.config.git.submodulesdir" warn "i.e. '#{FalkorLib.config.git[:submodulesdir]}'" if FalkorLib.config.git[:submodulesdir] end end |
.create_branch(branch, path = Dir.pwd) ⇒ Object
Create a new branch
132 133 134 135 136 137 |
# File 'lib/falkorlib/git/base.rb', line 132 def create_branch(branch, path = Dir.pwd) #ap method(__method__).parameters.map { |arg| arg[1] } g = MiniGit.new(path) error "not yet any commit performed -- You shall do one" unless commits?(path) g.branch branch.to_s end |
.create_remote(name, url, path = Dir.pwd, opts = {}) ⇒ Object
Create a new remote <name> targeting url <url> You can pass additional options expected by git remote add in <opts>, for instance as follows:
create_remote('origin', url, dir, { :fetch => true })
319 320 321 322 |
# File 'lib/falkorlib/git/base.rb', line 319 def create_remote(name, url, path = Dir.pwd, opts = {}) g = MiniGit.new(path) g.remote :add, opts, name, url.to_s end |
.delete_branch(branch, path = Dir.pwd, opts = { :force => false }) ⇒ Object
Delete a branch.
140 141 142 143 144 |
# File 'lib/falkorlib/git/base.rb', line 140 def delete_branch(branch, path = Dir.pwd, opts = { :force => false }) g = MiniGit.new(path) error "'#{branch}' is not a valid existing branch" unless list_branch(path).include?( branch ) g.branch ((opts[:force]) ? :D : :d) => branch.to_s end |
.dirty?(path = Dir.pwd) ⇒ Boolean
Check if a git directory is in dirty mode git diff –shortstat 2> /dev/null | tail -n1
261 262 263 264 265 266 |
# File 'lib/falkorlib/git/base.rb', line 261 def dirty?(path = Dir.pwd) g = MiniGit.new(path) a = g.capturing.diff :shortstat => true #ap a !a.empty? end |
.fetch(path = Dir.pwd) ⇒ Object
Fetch the latest changes
173 174 175 176 177 |
# File 'lib/falkorlib/git/base.rb', line 173 def fetch(path = Dir.pwd) Dir.chdir( path ) do execute "git fetch --all -v" end end |
.gitdir(path = Dir.pwd) ⇒ Object
Return the git root directory for the path (current directory by default)
126 127 128 129 |
# File 'lib/falkorlib/git/base.rb', line 126 def gitdir(path = Dir.pwd) g = MiniGit.new g.find_git_dir(path)[0] end |
.grab(branch, path = Dir.pwd, remote = 'origin') ⇒ Object
Grab a remote branch
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/falkorlib/git/base.rb', line 199 def grab(branch, path = Dir.pwd, remote = 'origin') exit_status = 1 error "no branch provided" if branch.nil? #remotes = FalkorLib::Git.remotes(path) branches = FalkorLib::Git.list_branch(path) if branches.include? "remotes/#{remote}/#{branch}" info "Grab the branch '#{remote}/#{branch}'" exit_status = execute_in_dir(FalkorLib::Git.rootdir( path ), "git branch --track #{branch} #{remote}/#{branch}") else warning "the remote branch '#{remote}/#{branch}' cannot be found" end exit_status end |
.init(path = Dir.pwd, _options = {}) ⇒ Object
Initialize a git repository
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/falkorlib/git/base.rb', line 91 def init(path = Dir.pwd, = {}) # FIXME: for travis test: ensure the global git configurations # 'user.email' and 'user.name' are set [ 'user.name', 'user.email' ].each do |userconf| next unless MiniGit[userconf].nil? warn "The Git global configuration '#{userconf}' is not set so" warn "you should *seriously* consider setting them by running\n\t git config --global #{userconf} 'your_#{userconf.sub(/\./, '_')}'" default_val = ENV['USER'] default_val += '@domain.org' if userconf =~ /email/ warn "Now putting a default value '#{default_val}' you could change later on" run %( git config --global #{userconf} "#{default_val}" ) #MiniGit[userconf] = default_val end exit_status = 1 Dir.mkdir( path ) unless Dir.exist?( path ) Dir.chdir( path ) do execute "git init" unless FalkorLib.config.debug exit_status = $?.to_i end # #puts "#init #{path}" # Dir.chdir( "#{path}" ) do # %x[ pwd && git init ] unless FalkorLib.config.debug # end exit_status end |
.init?(path = Dir.pwd) ⇒ Boolean
Check if a git directory has been initialized
40 41 42 43 44 45 46 47 |
# File 'lib/falkorlib/git/base.rb', line 40 def init?(path = Dir.pwd) begin MiniGit.new(path) rescue Exception return false end true end |
.last_tag_commit(path = Dir.pwd) ⇒ Object
Get the last tag commit, or nil if no tag can be found
282 283 284 285 286 287 288 289 290 |
# File 'lib/falkorlib/git/base.rb', line 282 def last_tag_commit(path = Dir.pwd) res = "" g = MiniGit.new(path) unless (g.capturing.tag :list => true).empty? # git rev-list --tags --max-count=1 res = (g.capturing.rev_list :tags => true, :max_count => 1).chomp end res end |
.list_branch(path = Dir.pwd) ⇒ Object
Get an array of the local branches present (first element is always the current branch)
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/falkorlib/git/base.rb', line 181 def list_branch(path = Dir.pwd) cg = MiniGit::Capturing.new(path) res = cg.branch :a => true res = res.split("\n") # Eventually reorder to make the first element of the array the current branch i = res.find_index { |e| e =~ /^\*\s/ } res[0], res[i] = res[i], res[0] unless (i.nil? || i.zero?) res.each { |e| e.sub!(/^\*?\s+/, '') } res end |
.list_files(path = Dir.pwd) ⇒ Object
List the files currently under version
235 236 237 238 |
# File 'lib/falkorlib/git/base.rb', line 235 def list_files(path = Dir.pwd) g = MiniGit.new(path) g.capturing.ls_files.split end |
.list_tag(path = Dir.pwd) ⇒ Object
Get a hash table of tags under the format
{ <tag> => <commit> }
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/falkorlib/git/base.rb', line 270 def list_tag(path = Dir.pwd) res = {} cg = MiniGit::Capturing.new(path) unless (cg.tag :list => true).empty? # git show-ref --tags a = (cg.show_ref :tags => true).split("\n") res = Hash[ a.collect { |item| item.split(' refs/tags/') } ].invert end res end |
.publish(branch, path = Dir.pwd, remote = 'origin') ⇒ Object
Publish a branch on the remote
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/falkorlib/git/base.rb', line 214 def publish(branch, path = Dir.pwd, remote = 'origin') exit_status = 1 error "no branch provided" if branch.nil? #remotes = FalkorLib::Git.remotes(path) branches = FalkorLib::Git.list_branch(path) Dir.chdir(FalkorLib::Git.rootdir( path ) ) do if branches.include? "remotes/#{remote}/#{branch}" warning "the remote branch '#{remote}/#{branch}' already exists" else info "Publish the branch '#{branch}' on the remote '#{remote}'" exit_status = run %( git push #{remote} #{branch}:refs/heads/#{branch} git fetch #{remote} git branch -u #{remote}/#{branch} #{branch} ) end end exit_status end |
.remotes(path = Dir.pwd) ⇒ Object
List of Git remotes
303 304 305 306 |
# File 'lib/falkorlib/git/base.rb', line 303 def remotes(path = Dir.pwd) g = MiniGit.new(path) g.capturing.remote.split end |
.remotes?(path = Dir.pwd) ⇒ Boolean
Check existence of remotes
309 310 311 |
# File 'lib/falkorlib/git/base.rb', line 309 def remotes?(path = Dir.pwd) !remotes(path).empty? end |
.rootdir(path = Dir.pwd) ⇒ Object
Return the Git working tree from the proposed path (current directory by default)
120 121 122 123 |
# File 'lib/falkorlib/git/base.rb', line 120 def rootdir(path = Dir.pwd) g = MiniGit.new g.find_git_dir(path)[1] end |
.submodule_init(path = Dir.pwd, submodules = , _options = {}) ⇒ Object
Initialize git submodule from the configuration
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/falkorlib/git/base.rb', line 335 def submodule_init(path = Dir.pwd, submodules = FalkorLib.config.git[:submodules], = {}) exit_status = 1 git_root_dir = rootdir(path) if File.exist?("#{git_root_dir}/.gitmodules") unless submodules.empty? # TODO: Check if it contains all submodules of the configuration end end #ap FalkorLib.config.git Dir.chdir(git_root_dir) do exit_status = FalkorLib::Git.submodule_update( git_root_dir ) submodules.each do |subdir, conf| next if conf[:url].nil? url = conf[:url] dir = "#{FalkorLib.config.git[:submodulesdir]}/#{subdir}" branch = (conf[:branch].nil?) ? 'master' : conf[:branch] if File.directory?( dir ) puts " ... the git submodule '#{subdir}' is already setup." else info "adding Git submodule '#{dir}' from '#{url}'" exit_status = run %( git submodule add -b #{branch} #{url} #{dir} git commit -s -m "Add Git submodule '#{dir}' from '#{url}'" .gitmodules #{dir} ) end end end exit_status end |
.submodule_update(path = Dir.pwd) ⇒ Object
Update the Git submodules to the local registered version
366 367 368 369 370 371 372 |
# File 'lib/falkorlib/git/base.rb', line 366 def submodule_update(path = Dir.pwd) execute_in_dir(rootdir(path), %( git submodule init git submodule update )) end |
.submodule_upgrade(path = Dir.pwd) ⇒ Object
Upgrade the Git submodules to the latest HEAD version from the remote
375 376 377 378 379 380 |
# File 'lib/falkorlib/git/base.rb', line 375 def submodule_upgrade(path = Dir.pwd) execute_in_dir(rootdir(path), %{ git submodule foreach 'git fetch origin; git checkout $(git rev-parse --abbrev-ref HEAD); git reset --hard origin/$(git rev-parse --abbrev-ref HEAD); git submodule update --recursive; git clean -dfx' }) end |
.subtree_diff(path = Dir.pwd) ⇒ Object
Show difference between local subtree(s) and their remotes“
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/falkorlib/git/base.rb', line 424 def subtree_diff(path = Dir.pwd) raise ArgumentError, "Git 'subtree' command is not available" unless FalkorLib::Git.command? "subtree" if FalkorLib.config.git[:subtrees].empty? FalkorLib::Git.config_warn(:subtrees) return 1 end exit_status = 0 git_root_dir = rootdir(path) Dir.chdir(git_root_dir) do FalkorLib.config.git[:subtrees].each do |dir, conf| next if conf[:url].nil? #url = conf[:url] remote = dir.gsub(/\//, '-') branch = (conf[:branch].nil?) ? 'master' : conf[:branch] remotes = FalkorLib::Git.remotes raise IOError, "The git remote '#{remote}' is not configured" unless remotes.include?( remote ) raise IOError, "The git subtree directory '#{dir}' does not exists" unless File.directory?( File.join(git_root_dir, dir) ) info "Git diff on subtree '#{dir}' with remote '#{remote}/#{branch}'" exit_status = execute "git diff #{remote}/#{branch} #{FalkorLib::Git.branch?( git_root_dir )}:#{dir}" end end exit_status end |
.subtree_init(path = Dir.pwd) ⇒ Object
Initialize git subtrees from the configuration
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/falkorlib/git/base.rb', line 384 def subtree_init(path = Dir.pwd) raise ArgumentError, "Git 'subtree' command is not available" unless FalkorLib::Git.command? "subtree" if FalkorLib.config.git[:subtrees].empty? FalkorLib::Git.config_warn(:subtrees) return 1 end exit_status = 0 git_root_dir = rootdir(path) Dir.chdir(git_root_dir) do FalkorLib.config.git[:subtrees].each do |dir, conf| next if conf[:url].nil? url = conf[:url] remote = dir.gsub(/\//, '-') branch = (conf[:branch].nil?) ? 'master' : conf[:branch] remotes = FalkorLib::Git.remotes unless remotes.include?( remote ) info "Initialize Git remote '#{remote}' from URL '#{url}'" exit_status = execute "git remote add --no-tags -f #{remote} #{url}" end unless File.directory?( File.join(git_root_dir, dir) ) info "initialize Git subtree '#{dir}'" exit_status = execute "git subtree add --prefix #{dir} --squash #{remote}/#{branch}" end end end exit_status end |
.subtree_init?(path = Dir.pwd) ⇒ Boolean
Check if the subtrees have been initialized. Actually based on a naive check of sub-directory existence
414 415 416 417 418 419 420 |
# File 'lib/falkorlib/git/base.rb', line 414 def subtree_init?(path = Dir.pwd) res = true FalkorLib.config.git[:subtrees].keys.each do |dir| res &&= File.directory?(File.join(path, dir)) end res end |
.subtree_up(path = Dir.pwd) ⇒ Object Also known as: subtree_pull
Pull the latest changes, assuming the git repository is not dirty
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/falkorlib/git/base.rb', line 449 def subtree_up(path = Dir.pwd) error "Unable to pull subtree(s): Dirty Git repository" if FalkorLib::Git.dirty?( path ) exit_status = 0 git_root_dir = rootdir(path) Dir.chdir(git_root_dir) do FalkorLib.config.git[:subtrees].each do |dir, conf| next if conf[:url].nil? #url = conf[:url] remote = dir.gsub(/\//, '-') branch = (conf[:branch].nil?) ? 'master' : conf[:branch] remotes = FalkorLib::Git.remotes info "Pulling changes into subtree '#{dir}' using remote '#{remote}/#{branch}'" raise IOError, "The git remote '#{remote}' is not configured" unless remotes.include?( remote ) info "\t\\__ fetching remote '#{remotes.join(',')}'" FalkorLib::Git.fetch( git_root_dir ) raise IOError, "The git subtree directory '#{dir}' does not exists" unless File.directory?( File.join(git_root_dir, dir) ) info "\t\\__ pulling changes" exit_status = execute "git subtree pull --prefix #{dir} --squash #{remote} #{branch}" #exit_status = puts "git subtree pull --prefix #{dir} --squash #{remote} #{branch}" end end exit_status end |
.tag(name, path = Dir.pwd, opts = {}) ⇒ Object
297 298 299 300 |
# File 'lib/falkorlib/git/base.rb', line 297 def tag(name, path = Dir.pwd, opts = {}) g = MiniGit.new(path) g.tag opts, name end |