class Object
Public Instance Methods
addAll()
click to toggle source
# File lib/branch_cli/branch.rb, line 180 def addAll runCommand("git reset --mixed") runCommand("git add . -A") end
branchesDiverged()
click to toggle source
On branch master Your branch and 'origin/master' have diverged, and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean
On branch master Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working directory clean
On branch master Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
# File lib/branch_cli/branch.rb, line 77 def branchesDiverged prettyPrint("\n😱 You appear to have a diverged branch:".f.Red) let matches = gitStatus.matches(forRegex: "(Your branch .*\\s*.*)") matches.each do |match| prettyPrint(match) end promptKeepLocal end
detectAhead()
click to toggle source
# File lib/branch_cli/branch.rb, line 55 def detectAhead let status = gitStatus if status.contains("can be fast-forwarded.") || status.contains("is ahead of 'origin") || status.contains(" have diverged") branchesDiverged end end
detectChanges()
click to toggle source
# File lib/branch_cli/branch.rb, line 29 def detectChanges let status = gitStatus if status.contains("to be committed") || status.contains("for commit:") || status.contains("Untracked files:") uncommitedChanges end end
fetch()
click to toggle source
# File lib/branch_cli/branch.rb, line 185 def fetch let fetch = runCommand("git fetch") let error = fetch.stderr if error.contains("No remote repository specified") prettyPrint("\n⚠️ No remote repository is setup\n".f.Yellow) return end if fetch.exitStatus != 0 prettyPrint("\n🤔 Failed to fetch.".f.Red) exit(1) end end
getCurrentBranch()
click to toggle source
# File lib/branch_cli/branch.rb, line 9 def getCurrentBranch let result = runCommand("git symbolic-ref HEAD") let matches = result.stdout.matches(forRegex: "heads\\/(.*)") if matches.count == 0 return nil else return Branch.new(name: matches[0]) end end
gitStatus()
click to toggle source
# File lib/branch_cli/branch.rb, line 130 def gitStatus return runCommand("git status").stdout end
let(*args)
click to toggle source
# File lib/branch_cli/swift_compatibility.rb, line 2 def let(*args) end
main(arguments)
click to toggle source
# File lib/branch_cli/main.rb, line 1 def main(arguments) Options.reset let options = Options.sharedInstance options.loadOptions(arguments: arguments) if options.isShowVersion spec = Gem::Specification::load("#{BranchCli.root}/branch_cli.gemspec") prettyPrint("branch cli #{spec.version}") exit(0) end if options.isShowList printRecentBranches exit(0) end if options.isHelp prettyPrint("usage: branch BRANCH-NAME [ARGS]") prettyPrint("") prettyPrint("--version | -v \tShows the current version") prettyPrint("--verbose \t\tPrints all the git commands as they run") prettyPrint("--list | -l \t\tPrints the most recently updated branches") prettyPrint("--prefer=PREFERENCE \tWhere PREFERENCE is local or remote, will use the set preference rather than ask") prettyPrint("--help | help \tShows this help") exit(0) end addAll if options.isBranchSupplied setCurrentBranch(Branch.new(name: options.suppliedBranch)) else printCurrentBranch printGitStatus(preceedingNewline: true) end end
prettyPrint(str)
click to toggle source
# File lib/branch_cli/print.rb, line 1 def prettyPrint(str) printer = Formatador.new printer.instance_eval { @indent = 0 } printer.display_line(str.strip) end
printCurrentBranch()
click to toggle source
# File lib/branch_cli/branch.rb, line 99 def printCurrentBranch let branchName = (getCurrentBranch && getCurrentBranch.name) || "no branch" prettyPrint("On branch #{branchName.s.Bold}") end
printGitStatus(preceedingNewline: false)
click to toggle source
# File lib/branch_cli/branch.rb, line 134 def printGitStatus(preceedingNewline: false) let diff = gitStatus.matches(forRegex: "\t([a-z ]*:.*)") if diff.count > 0 && preceedingNewline prettyPrint("") end diff.each do |line| prettyPrint("\t#{line}".f.Green) end end
printRecentBranches()
click to toggle source
# File lib/branch_cli/branch.rb, line 104 def printRecentBranches let command = runCommand("git for-each-ref --sort=-committerdate --format=\"%(refname)\" --count=30 refs/heads/ refs/remotes") let references = command.stdout.components(separatedBy: "\n") commits = references.map do |r| Commit.new(message: "", sha: r.clearQuotes) end strings = commits.map do |c| c.printableFormat("%Cgreen%cr%Creset %C(yellow)%d%Creset %C(bold blue)<%an>%Creset%n") end var lastStr = "" strings.each do |str| if str != lastStr prettyPrint(str) # avoid dups end lastStr = str end end
promptContinueAnyway()
click to toggle source
# File lib/branch_cli/branch.rb, line 145 def promptContinueAnyway puts "" response = Ask.list "Continue anyway? Changes will be lost", [ "Stop", "Continue" ] exit(1) if response == 0 end
promptKeepLocal()
click to toggle source
# File lib/branch_cli/branch.rb, line 155 def promptKeepLocal let choice: String let options = Options.sharedInstance if options.preferLocal choice = "local" elsif options.preferRemote choice = "remote" else puts "" response = Ask.list "Keep remote or local copy?", [ "Remote", "Local" ] choice = "remote" if response == 0 choice = "local" if response == 1 end if choice != "remote" prettyPrint("Using local branch (user specified)") exit(0) end end
resetLocal()
click to toggle source
# File lib/branch_cli/branch.rb, line 36 def resetLocal runCommand("git reset --hard") end
resetToOrigin()
click to toggle source
# File lib/branch_cli/branch.rb, line 88 def resetToOrigin let origin = getCurrentBranch.origin let reset = runCommand("git reset --hard #{origin}") if reset.exitStatus == 0 prettyPrint("Using remote branch".f.Green) else prettyPrint("Using local branch (no origin branch found)".f.Green) end end
runCommand(command, args: [])
click to toggle source
# File lib/branch_cli/run.rb, line 7 def runCommand(command, args: []) command_with_args = "#{command} #{args.join(" ")}" if Options.sharedInstance.isVerbose prettyPrint(command_with_args.f.Blue) end Open3.popen3(command_with_args) do |stdin, stdout, stderr, wait_thr| RunResult.new(stdout: stdout.read, stderr: stderr.read, exitStatus: wait_thr.value.to_i) end end
setCurrentBranch(branch)
click to toggle source
# File lib/branch_cli/branch.rb, line 19 def setCurrentBranch(branch) prettyPrint("Switching to branch #{branch.name.s.Bold}...") fetch detectChanges resetLocal switchBranch(branch) detectAhead resetToOrigin end
switchBranch(branch)
click to toggle source
# File lib/branch_cli/branch.rb, line 40 def switchBranch(branch) runCommand("git checkout #{branch.name}") if getCurrentBranch.name != branch.name runCommand("git checkout -b #{branch.name}") end runCommand("git branch --set-upstream-to=origin/#{branch.name}") if getCurrentBranch.name != branch.name prettyPrint("🤔 Failed to switch branch".f.Red) exit(1) end end
uncommitedChanges()
click to toggle source
# File lib/branch_cli/branch.rb, line 124 def uncommitedChanges prettyPrint("\n😱 You appear to have uncommited changes:".f.Red) printGitStatus promptContinueAnyway end
var(*args)
click to toggle source
# File lib/branch_cli/swift_compatibility.rb, line 5 def var(*args) end