Class: GitProc::GitLib
- Inherits:
-
Object
- Object
- GitProc::GitLib
- Defined in:
- lib/git-process/git_lib.rb
Overview
Provides Git commands
noinspection RubyTooManyMethodsInspection
Class Method Summary collapse
- .find_workdir(dir) ⇒ Object
-
.log_level(opts) ⇒ Fixnum
Decodes the [Hash] to determine what logging level to use.
Instance Method Summary collapse
-
#add(file) ⇒ String
‘git add`.
-
#branch(branch_name, opts = {}) ⇒ String
Does branch manipulation.
- #branches ⇒ GitBranches
- #checkout(branch_name, opts = {}) ⇒ void
-
#command(cmd, opts = [], chdir = true, redirect = '') { ... } ⇒ String
Executes the given git command.
-
#commit(msg = nil) ⇒ String
‘git commit`.
-
#config ⇒ GitConfig
The git configuration.
-
#delete_sync_control_file!(branch_name) ⇒ void
Delete the sync control file for the branch.
-
#fetch(name = remote.name) ⇒ String
‘git fetch`.
-
#fetch_changes(output) ⇒ Hash
With lists for each of :new_branch, :new_tag, :force_updated, :deleted, :updated.
- #fetch_remote_changes(remote_name = nil) ⇒ void
-
#has_a_remote? ⇒ Boolean
Does this have a remote defined?.
-
#initialize(dir, logging_opts) ⇒ GitLib
constructor
A new instance of GitLib.
-
#is_parked? ⇒ Boolean
Is the current branch the “parked” branch?.
-
#log_count ⇒ int
The number of commits that exist in the current branch.
-
#log_level ⇒ Fixnum
The logging level to use; defaults to Logger::WARN.
- #log_level=(lvl) ⇒ void
-
#logger ⇒ GitLogger
The logger to use.
-
#merge(base, opts = {}) ⇒ String
‘git merge`.
-
#porcelain_status ⇒ String
The raw porcelain status string.
-
#previous_remote_sha(current_branch, remote_branch) ⇒ String?
The previous remote sha ONLY IF it is not the same as the new remote sha; otherwise nil.
-
#proc_merge(base, opts = {}) ⇒ Object
Executes a merge, but translates any GitExecuteError to a MergeError.
-
#proc_rebase(base, opts = {}) ⇒ Object
Executes a rebase, but translates any GitExecuteError to a RebaseError.
-
#push(remote_name, local_branch, remote_branch, opts = {}) ⇒ String
Pushes the given branch to the server.
-
#push_delete(branch_name, remote_name, opts) ⇒ String
Pushes the given branch to the server.
-
#push_to_remote(local_branch, remote_branch, remote_name, opts) ⇒ String
Pushes the given branch to the server.
-
#push_to_server(local_branch, remote_branch, opts = {}) ⇒ void
Push the repository to the server.
-
#read_sync_control_file(branch_name) ⇒ String?
The SHA-1 of the latest sync performed for the branch, or nil if none is recorded.
-
#rebase(upstream, opts = {}) ⇒ String
‘git rebase`.
-
#rebase_continue ⇒ String
‘git rebase –continue`.
-
#remote ⇒ GitRemote
The git remote configuration.
- #remote_branch_sha(remote_branch) ⇒ Object
- #remote_branches ⇒ GitBranches
-
#remove(files, opts = {}) ⇒ String
Remove the files from the Index.
-
#reset(rev_name, opts = {}) ⇒ Object
Resets the Index/Working Directory to the given revision.
- #rev_list(start_revision, end_revision, opts = {}) ⇒ Object
-
#rev_parse(name) ⇒ String?
(also: #sha)
Translate the commit-ish name to the SHA-1 hash value.
- #set_upstream_branch(branch_name, upstream) ⇒ Object
-
#show(refspec) ⇒ String
‘git show`.
-
#stash_pop ⇒ String
‘git stash –pop`.
-
#stash_save ⇒ String
‘git stash –save`.
-
#status ⇒ Status
Returns the status of the git repository.
-
#sync_control_file_exists?(branch_name) ⇒ Boolean
Does the sync control file exist?.
-
#workdir ⇒ Dir
The working directory.
-
#workdir=(dir) ⇒ void
Sets the working directory to use for the (non-bare) repository.
-
#write_sync_control_file(branch_name) ⇒ void
Writes the current SHA-1 for the tip of the branch to the “sync control file”.
Constructor Details
Class Method Details
.find_workdir(dir) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/git-process/git_lib.rb', line 111 def self.find_workdir(dir) if dir == File::SEPARATOR return nil elsif File.directory?(File.join(dir, '.git')) return dir else return find_workdir(File.("#{dir}#{File::SEPARATOR}..")) end end |
.log_level(opts) ⇒ Fixnum
Decodes the [Hash] to determine what logging level to use
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/git-process/git_lib.rb', line 59 def self.log_level(opts) if opts[:log_level] return opts[:log_level] elsif opts[:quiet] return Logger::ERROR elsif opts[:verbose] return Logger::DEBUG else return Logger::INFO end end |
Instance Method Details
#add(file) ⇒ String
‘git add`
252 253 254 255 |
# File 'lib/git-process/git_lib.rb', line 252 def add(file) logger.info { "Adding #{[*file].join(', ')}" } return command(:add, ['--', file]) end |
#branch(branch_name, opts = {}) ⇒ String
Does branch manipulation.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/git-process/git_lib.rb', line 382 def branch(branch_name, opts = {}) if branch_name if opts[:delete] return delete_branch(branch_name, opts[:force]) elsif opts[:rename] return rename_branch(branch_name, opts[:rename]) elsif opts[:upstream] return set_upstream_branch(branch_name, opts[:upstream]) else base_branch = opts[:base_branch] || 'master' if opts[:force] return change_branch(branch_name, base_branch) else return create_branch(branch_name, base_branch) end end else #list_branches(opts) return list_branches(opts[:all], opts[:remote], opts[:no_color]) end end |
#branches ⇒ GitBranches
357 358 359 |
# File 'lib/git-process/git_lib.rb', line 357 def branches GitProc::GitBranches.new(self) end |
#checkout(branch_name, opts = {}) ⇒ void
This method returns an undefined value.
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'lib/git-process/git_lib.rb', line 537 def checkout(branch_name, opts = {}) args = [] args << '--no-track' if opts[:no_track] args << '-b' if opts[:new_branch] args << branch_name args << opts[:new_branch] if opts[:new_branch] branches = branches() command(:checkout, args) branches << GitBranch.new(branch_name, opts[:new_branch] != nil, self) if block_given? yield command(:checkout, branches.current.name) branches.current else branches[branch_name] end end |
#command(cmd, opts = [], chdir = true, redirect = '') { ... } ⇒ String
Executes the given git command
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
# File 'lib/git-process/git_lib.rb', line 646 def command(cmd, opts = [], chdir = true, redirect = '', &block) ENV['GIT_INDEX_FILE'] = File.join(workdir, '.git', 'index') ENV['GIT_DIR'] = File.join(workdir, '.git') ENV['GIT_WORK_TREE'] = workdir path = workdir git_cmd = create_git_command(cmd, opts, redirect) out = command_git_cmd(path, git_cmd, chdir, block) if logger logger.debug(git_cmd) logger.debug(out) end handle_exitstatus($?, git_cmd, out) end |
#commit(msg = nil) ⇒ String
‘git commit`
263 264 265 266 |
# File 'lib/git-process/git_lib.rb', line 263 def commit(msg = nil) logger.info 'Committing changes' return command(:commit, msg.nil? ? nil : ['-m', msg]) end |
#config ⇒ GitConfig
Returns the git configuration.
224 225 226 227 228 229 |
# File 'lib/git-process/git_lib.rb', line 224 def config if @config.nil? @config = GitConfig.new(self) end return @config end |
#delete_sync_control_file!(branch_name) ⇒ void
This method returns an undefined value.
Delete the sync control file for the branch
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/git-process/git_lib.rb', line 700 def delete_sync_control_file!(branch_name) filename = sync_control_filename(branch_name) logger.debug { "Deleting sync control file, #{filename}" } # on some systems, especially Windows, the file may be locked. wait for it to unlock counter = 10 while counter > 0 begin File.delete(filename) counter = 0 rescue counter = counter - 1 sleep(0.25) end end end |
#fetch(name = remote.name) ⇒ String
‘git fetch`
311 312 313 314 315 316 317 318 |
# File 'lib/git-process/git_lib.rb', line 311 def fetch(name = remote.name) logger.info 'Fetching the latest changes from the server' output = self.command(:fetch, ['-p', name]) log_fetch_changes(fetch_changes(output)) return output end |
#fetch_changes(output) ⇒ Hash
Returns with lists for each of :new_branch, :new_tag, :force_updated, :deleted, :updated.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/git-process/git_lib.rb', line 322 def fetch_changes(output) changed = output.split("\n") changes = {:new_branch => [], :new_tag => [], :force_updated => [], :deleted => [], :updated => []} line = changed.shift until line.nil? do case line when /^\s\s\s/ m = /^\s\s\s(\S+)\s+(\S+)\s/.match(line) changes[:updated] << "#{m[2]} (#{m[1]})" when /^\s\*\s\[new branch\]/ m = /^\s\*\s\[new branch\]\s+(\S+)\s/.match(line) changes[:new_branch] << m[1] when /^\s\*\s\[new tag\]/ m = /^\s\*\s\[new tag\]\s+(\S+)\s/.match(line) changes[:new_tag] << m[1] when /^\sx\s/ m = /^\sx\s\[deleted\]\s+\(none\)\s+->\s+[^\/]+\/(\S+)/.match(line) changes[:deleted] << m[1] when /^\s\+\s/ m = /^\s\+\s(\S+)\s+(\S+)\s/.match(line) changes[:force_updated] << "#{m[2]} (#{m[1]})" else # ignore the line end line = changed.shift end changes end |
#fetch_remote_changes(remote_name = nil) ⇒ void
This method returns an undefined value.
123 124 125 126 127 128 129 |
# File 'lib/git-process/git_lib.rb', line 123 def fetch_remote_changes(remote_name = nil) if remote.exists? fetch(remote_name || remote.name) else logger.debug 'Can not fetch latest changes because there is no remote defined' end end |
#has_a_remote? ⇒ Boolean
Returns does this have a remote defined?.
242 243 244 |
# File 'lib/git-process/git_lib.rb', line 242 def has_a_remote? remote.exists? end |
#is_parked? ⇒ Boolean
Returns is the current branch the “parked” branch?.
189 190 191 192 |
# File 'lib/git-process/git_lib.rb', line 189 def is_parked? mybranches = self.branches() return mybranches.parking == mybranches.current end |
#log_count ⇒ int
Returns the number of commits that exist in the current branch.
559 560 561 |
# File 'lib/git-process/git_lib.rb', line 559 def log_count command(:log, '--oneline').split(/\n/).length end |
#log_level ⇒ Fixnum
Returns the logging level to use; defaults to Logger::WARN.
73 74 75 |
# File 'lib/git-process/git_lib.rb', line 73 def log_level @log_level || Logger::WARN end |
#log_level=(lvl) ⇒ void
This method returns an undefined value.
80 81 82 |
# File 'lib/git-process/git_lib.rb', line 80 def log_level=(lvl) @log_level = lvl end |
#logger ⇒ GitLogger
Returns the logger to use.
42 43 44 45 46 47 |
# File 'lib/git-process/git_lib.rb', line 42 def logger if @logger.nil? @logger = GitLogger.new(log_level) end return @logger end |
#merge(base, opts = {}) ⇒ String
‘git merge`
298 299 300 301 302 303 304 |
# File 'lib/git-process/git_lib.rb', line 298 def merge(base, opts= {}) logger.info { "Merging #{branches.current.name} with #{base}" } args = [] args << '-s' << opts[:merge_strategy] if opts[:merge_strategy] args << base return command(:merge, args) end |
#porcelain_status ⇒ String
Returns the raw porcelain status string.
589 590 591 |
# File 'lib/git-process/git_lib.rb', line 589 def porcelain_status command(:status, '--porcelain') end |
#previous_remote_sha(current_branch, remote_branch) ⇒ String?
Returns the previous remote sha ONLY IF it is not the same as the new remote sha; otherwise nil.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/git-process/git_lib.rb', line 163 def previous_remote_sha(current_branch, remote_branch) return nil unless has_a_remote? return nil unless remote_branches.include?(remote_branch) control_file_sha = read_sync_control_file(current_branch) old_sha = control_file_sha || remote_branch_sha(remote_branch) fetch_remote_changes new_sha = remote_branch_sha(remote_branch) if old_sha != new_sha logger.info('The remote branch has changed since the last time') return old_sha else logger.debug 'The remote branch has not changed since the last time' return nil end end |
#proc_merge(base, opts = {}) ⇒ Object
Executes a merge, but translates any GitProc::GitExecuteError to a MergeError
153 154 155 156 157 158 159 |
# File 'lib/git-process/git_lib.rb', line 153 def proc_merge(base, opts = {}) begin return merge(base, opts) rescue GitExecuteError => merge_error raise MergeError.new(merge_error., self) end end |
#proc_rebase(base, opts = {}) ⇒ Object
Executes a rebase, but translates any GitProc::GitExecuteError to a RebaseError
138 139 140 141 142 143 144 |
# File 'lib/git-process/git_lib.rb', line 138 def proc_rebase(base, opts = {}) begin return rebase(base, opts) rescue GitExecuteError => rebase_error raise RebaseError.new(rebase_error., self) end end |
#push(remote_name, local_branch, remote_branch, opts = {}) ⇒ String
Pushes the given branch to the server.
419 420 421 422 423 424 425 |
# File 'lib/git-process/git_lib.rb', line 419 def push(remote_name, local_branch, remote_branch, opts = {}) if opts[:delete] return push_delete(remote_branch || local_branch, remote_name, opts) else return push_to_remote(local_branch, remote_branch, remote_name, opts) end end |
#push_delete(branch_name, remote_name, opts) ⇒ String
remove the opts param
Pushes the given branch to the server.
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/git-process/git_lib.rb', line 476 def push_delete(branch_name, remote_name, opts) remote_name ||= 'origin' args = [remote_name] if branch_name rb = branch_name elsif !(opts[:delete].is_a? TrueClass) rb = opts[:delete] else raise ArgumentError.new('Need a branch name to delete.') end int_branch = config.master_branch if rb == int_branch raise GitProc::GitProcessError.new("Can not delete the integration branch '#{int_branch}'") end logger.info { "Deleting remote branch '#{rb}' on '#{remote_name}'." } args << '--delete' << rb return command(:push, args) end |
#push_to_remote(local_branch, remote_branch, remote_name, opts) ⇒ String
Pushes the given branch to the server.
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/git-process/git_lib.rb', line 439 def push_to_remote(local_branch, remote_branch, remote_name, opts) remote_name ||= 'origin' args = [remote_name] local_branch ||= branches.current remote_branch ||= local_branch args << '-f' if opts[:force] logger.info do if local_branch == remote_branch "Pushing to '#{remote_branch}' on '#{remote_name}'." else "Pushing #{local_branch} to '#{remote_branch}' on '#{remote_name}'." end end args << "#{local_branch}:#{remote_branch}" return command(:push, args) end |
#push_to_server(local_branch, remote_branch, opts = {}) ⇒ void
This method returns an undefined value.
Push the repository to the server.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/git-process/git_lib.rb', line 206 def push_to_server(local_branch, remote_branch, opts = {}) if opts[:local] logger.debug('Not pushing to the server because the user selected local-only.') elsif not has_a_remote? logger.debug('Not pushing to the server because there is no remote.') elsif local_branch == config.master_branch logger.warn('Not pushing to the server because the current branch is the mainline branch.') else opts[:prepush].call if opts[:prepush] push(remote.name, local_branch, remote_branch, :force => opts[:force]) opts[:postpush].call if opts[:postpush] end end |
#read_sync_control_file(branch_name) ⇒ String?
Returns the SHA-1 of the latest sync performed for the branch, or nil if none is recorded.
680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/git-process/git_lib.rb', line 680 def read_sync_control_file(branch_name) filename = sync_control_filename(branch_name) if File.exists?(filename) sha = File.open(filename) do |file| file.readline.chop end logger.debug "Read sync control file, #{filename}: #{sha}" sha else logger.debug "Sync control file, #{filename}, was not found" nil end end |
#rebase(upstream, opts = {}) ⇒ String
‘git rebase`
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/git-process/git_lib.rb', line 277 def rebase(upstream, opts = {}) args = [] if opts[:interactive] logger.info { "Interactively rebasing #{branches.current.name} against #{upstream}" } args << '-i' args << upstream elsif opts[:oldbase] logger.info { "Doing rebase from #{opts[:oldbase]} against #{upstream} on #{branches.current.name}" } args << '--onto' << upstream << opts[:oldbase] << branches.current.name else logger.info { "Rebasing #{branches.current.name} against #{upstream}" } args << upstream end return command('rebase', args) end |
#rebase_continue ⇒ String
‘git rebase –continue`
503 504 505 |
# File 'lib/git-process/git_lib.rb', line 503 def rebase_continue command(:rebase, '--continue') end |
#remote ⇒ GitRemote
Returns the git remote configuration.
233 234 235 236 237 238 |
# File 'lib/git-process/git_lib.rb', line 233 def remote if @remote.nil? @remote = GitProc::GitRemote.new(config) end return @remote end |
#remote_branch_sha(remote_branch) ⇒ Object
182 183 184 185 |
# File 'lib/git-process/git_lib.rb', line 182 def remote_branch_sha(remote_branch) logger.debug { "getting sha for remotes/#{remote_branch}" } return rev_parse("remotes/#{remote_branch}") rescue '' end |
#remote_branches ⇒ GitBranches
363 364 365 |
# File 'lib/git-process/git_lib.rb', line 363 def remote_branches GitProc::GitBranches.new(self, :remote => true) end |
#remove(files, opts = {}) ⇒ String
Remove the files from the Index
571 572 573 574 575 576 |
# File 'lib/git-process/git_lib.rb', line 571 def remove(files, opts = {}) args = [] args << '-f' if opts[:force] args << [*files] command(:rm, args) end |
#reset(rev_name, opts = {}) ⇒ Object
Resets the Index/Working Directory to the given revision
601 602 603 604 605 606 607 608 609 |
# File 'lib/git-process/git_lib.rb', line 601 def reset(rev_name, opts = {}) args = [] args << '--hard' if opts[:hard] args << rev_name logger.info { "Resetting #{opts[:hard] ? '(hard)' : ''} to #{rev_name}" } command(:reset, args) end |
#rev_list(start_revision, end_revision, opts = {}) ⇒ Object
612 613 614 615 616 617 618 |
# File 'lib/git-process/git_lib.rb', line 612 def rev_list(start_revision, end_revision, opts ={}) args = [] args << "-#{opts[:num_revs]}" if opts[:num_revs] args << '--oneline' if opts[:oneline] args << "#{start_revision}..#{end_revision}" command('rev-list', args) end |
#rev_parse(name) ⇒ String? Also known as: sha
Translate the commit-ish name to the SHA-1 hash value
626 627 628 629 |
# File 'lib/git-process/git_lib.rb', line 626 def rev_parse(name) sha = command('rev-parse', ['--revs-only', name]) return sha.empty? ? nil : sha end |
#set_upstream_branch(branch_name, upstream) ⇒ Object
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 |
# File 'lib/git-process/git_lib.rb', line 726 def set_upstream_branch(branch_name, upstream) logger.info { "Setting upstream/tracking for branch '#{branch_name}' to '#{upstream}'." } if has_a_remote? parts = upstream.split(/\//) if parts.length() > 1 potential_remote = parts.shift if remote.remote_names.include?(potential_remote) config["branch.#{branch_name}.remote"] = potential_remote config["branch.#{branch_name}.merge"] = "refs/heads/#{parts.join('/')}" end else config["branch.#{branch_name}.merge"] = "refs/heads/#{upstream}" end else config["branch.#{branch_name}.merge"] = "refs/heads/#{upstream}" end # The preferred way assuming using git 1.8 cli #command(:branch, ['--set-upstream-to', upstream, branch_name]) end |
#show(refspec) ⇒ String
‘git show`
527 528 529 |
# File 'lib/git-process/git_lib.rb', line 527 def show(refspec) command(:show, refspec) end |
#stash_pop ⇒ String
‘git stash –pop`
519 520 521 |
# File 'lib/git-process/git_lib.rb', line 519 def stash_pop command(:stash, %w(pop)) end |
#stash_save ⇒ String
‘git stash –save`
511 512 513 |
# File 'lib/git-process/git_lib.rb', line 511 def stash_save command(:stash, %w(save)) end |
#status ⇒ Status
Returns the status of the git repository.
583 584 585 |
# File 'lib/git-process/git_lib.rb', line 583 def status GitStatus.new(self) end |
#sync_control_file_exists?(branch_name) ⇒ Boolean
Returns does the sync control file exist?.
720 721 722 723 |
# File 'lib/git-process/git_lib.rb', line 720 def sync_control_file_exists?(branch_name) filename = sync_control_filename(branch_name) File.exist?(filename) end |
#workdir ⇒ Dir
Returns the working directory.
86 87 88 |
# File 'lib/git-process/git_lib.rb', line 86 def workdir @workdir end |
#workdir=(dir) ⇒ void
This method returns an undefined value.
Sets the working directory to use for the (non-bare) repository.
If the directory is not part of an existing repository, a new repository is created. (i.e., “git init”)
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/git-process/git_lib.rb', line 98 def workdir=(dir) workdir = GitLib.find_workdir(dir) if workdir.nil? @workdir = dir logger.info { "Initializing new repository at #{dir}" } return command(:init) else @workdir = workdir logger.debug { "Opening existing repository at #{dir}" } end end |
#write_sync_control_file(branch_name) ⇒ void
This method returns an undefined value.
Writes the current SHA-1 for the tip of the branch to the “sync control file”
670 671 672 673 674 675 |
# File 'lib/git-process/git_lib.rb', line 670 def write_sync_control_file(branch_name) latest_sha = rev_parse(branch_name) filename = sync_control_filename(branch_name) logger.debug { "Writing sync control file, #{filename}, with #{latest_sha}" } File.open(filename, 'w') { |f| f.puts latest_sha } end |