Module: Git
- Defined in:
- lib/git/contest/git.rb,
lib/git/contest.rb,
lib/git/contest/version.rb
Overview
version.rb
Copyright © 2013-2014 Hiroyuki Sano <sh19910711 at gmail.com> Licensed under the MIT-License.
Defined Under Namespace
Modules: Contest
Class Method Summary collapse
- .all_branches ⇒ Object
- .branch_exists(branch_name) ⇒ Object
-
.compare_branches(first, second) ⇒ Object
0: same 1: first branch needs ff 2: second branch needs ff 3: branch needs merge 4: there is no merge.
- .contest_has_develop_configured ⇒ Object
- .contest_has_master_configured ⇒ Object
- .contest_has_prefix_configured ⇒ Object
- .contest_is_initialized ⇒ Object
- .contest_resolve_nameprefix(name, prefix) ⇒ Object
- .current_branch ⇒ Object
- .do(*args) ⇒ Object
-
.do_no_echo(*args) ⇒ Object
use return value.
- .is_clean_working_tree ⇒ Object
- .local_branch_exists(branch_name) ⇒ Object
- .local_branches ⇒ Object
- .remote_branch_exists(branch_name) ⇒ Object
- .remote_branches ⇒ Object
- .repo_is_headless ⇒ Object
- .require_branch(branch) ⇒ Object
- .require_branch_absent(branch) ⇒ Object
- .require_branches_equal(local, remote) ⇒ Object
- .require_clean_working_tree ⇒ Object
- .require_local_branch(branch) ⇒ Object
- .require_remote_branch(branch) ⇒ Object
Class Method Details
.all_branches ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/git/contest/git.rb', line 86 def self.all_branches cmd_ret1 = Git.do 'branch --no-color' cmd_ret2 = Git.do 'branch -r --no-color' lines = ( cmd_ret1 + cmd_ret2 ).lines lines.map {|line| line.gsub(/^[*]?\s*/, '').gsub(/\s*$/, '').strip } end |
.branch_exists(branch_name) ⇒ Object
68 69 70 |
# File 'lib/git/contest/git.rb', line 68 def self.branch_exists(branch_name) Git.all_branches().include?(branch_name) end |
.compare_branches(first, second) ⇒ Object
0: same 1: first branch needs ff 2: second branch needs ff 3: branch needs merge 4: there is no merge
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/git/contest/git.rb', line 122 def self.compare_branches first, second commit1 = Git.do "rev-parse \"#{first}\"" commit2 = Git.do "rev-parse \"#{second}\"" if commit1 != commit2 if Git.do_no_echo("merge-base \"#{commit1}\" \"#{commit2}\"") return 4 else base = Git.do "merge-base \"#{commit1}\" \"#{commit2}\"" if commit1 == base return 1 elsif commit2 == base return 2 else return 3 end end else return 0 end end |
.contest_has_develop_configured ⇒ Object
34 35 36 37 |
# File 'lib/git/contest/git.rb', line 34 def self.contest_has_develop_configured develop = (Git.do 'config --get git.contest.branch.develop').strip develop != '' && Git.local_branches().include?(develop) end |
.contest_has_master_configured ⇒ Object
29 30 31 32 |
# File 'lib/git/contest/git.rb', line 29 def self.contest_has_master_configured master = (Git.do 'config --get git.contest.branch.master').strip master != '' && Git.local_branches().include?(master) end |
.contest_has_prefix_configured ⇒ Object
39 40 41 |
# File 'lib/git/contest/git.rb', line 39 def self.contest_has_prefix_configured Git.do_no_echo 'config --get git.contest.branch.prefix' end |
.contest_is_initialized ⇒ Object
23 24 25 26 27 |
# File 'lib/git/contest/git.rb', line 23 def self.contest_is_initialized Git.contest_has_master_configured && Git.contest_has_prefix_configured && Git.do('config --get git.contest.branch.master') != Git.do('config --get git.contest.branch.develop') end |
.contest_resolve_nameprefix(name, prefix) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/git/contest/git.rb', line 43 def self.contest_resolve_nameprefix name, prefix if Git.local_branch_exists "#{prefix}/#{name}" return name end branches = Git.local_branches().select {|branch| branch.start_with? "#{prefix}/#{name}" } if branches.size == 0 abort "No branch matches prefix '#{name}'" else if branches.size == 1 return branches[0][prefix.length..-1] else abort "Multiple branches match prefix '#{name}'" end end end |
.current_branch ⇒ Object
95 96 97 98 99 |
# File 'lib/git/contest/git.rb', line 95 def self.current_branch ret = Git.do('branch --no-color').lines ret = ret.grep /^\*/ ret[0].gsub(/^[* ] /, '').strip end |
.do(*args) ⇒ Object
10 11 12 13 |
# File 'lib/git/contest/git.rb', line 10 def self.do(*args) puts "git #{args.join(' ')}" if ENV['GIT_CONTEST_DEBUG'] == 'ON' `git #{args.join(' ')} 2>&1`.strip end |
.do_no_echo(*args) ⇒ Object
use return value
16 17 18 19 20 |
# File 'lib/git/contest/git.rb', line 16 def self.do_no_echo(*args) puts "git #{args.join(' ')}" if ENV['GIT_CONTEST_DEBUG'] == 'ON' `git #{args.join(' ')} 2>&1` $?.success? end |
.is_clean_working_tree ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/git/contest/git.rb', line 101 def self.is_clean_working_tree if ! Git.do_no_echo 'diff --no-ext-diff --ignore-submodules --quiet --exit-code' return 1 elsif ! Git.do_no_echo 'diff-index --cached --quiet --ignore-submodules HEAD --' return 2 else return 0 end end |
.local_branch_exists(branch_name) ⇒ Object
64 65 66 |
# File 'lib/git/contest/git.rb', line 64 def self.local_branch_exists(branch_name) Git.local_branches().include?(branch_name) end |
.local_branches ⇒ Object
79 80 81 82 83 84 |
# File 'lib/git/contest/git.rb', line 79 def self.local_branches cmd_ret = Git.do 'branch --no-color' cmd_ret.lines.map {|line| line.gsub(/^[*]?\s*/, '').gsub(/\s*$/, '').strip } end |
.remote_branch_exists(branch_name) ⇒ Object
60 61 62 |
# File 'lib/git/contest/git.rb', line 60 def self.remote_branch_exists(branch_name) Git.remote_branches().include?(branch_name) end |
.remote_branches ⇒ Object
72 73 74 75 76 77 |
# File 'lib/git/contest/git.rb', line 72 def self.remote_branches cmd_ret = Git.do 'branch -r --no-color' cmd_ret.lines.map {|line| line.gsub(/^[*]?\s*/, '').gsub(/\s*$/, '').strip } end |
.repo_is_headless ⇒ Object
111 112 113 |
# File 'lib/git/contest/git.rb', line 111 def self.repo_is_headless ! Git.do_no_echo 'rev-parse --quiet --verify HEAD' end |
.require_branch(branch) ⇒ Object
143 144 145 146 147 |
# File 'lib/git/contest/git.rb', line 143 def self.require_branch(branch) if ! Git.all_branches().include?(branch) abort "Branch #{branch} does not exist." end end |
.require_branch_absent(branch) ⇒ Object
149 150 151 152 153 |
# File 'lib/git/contest/git.rb', line 149 def self.require_branch_absent(branch) if Git.all_branches().include?(branch) abort "Branch #{branch} already exists. Pick another name." end end |
.require_branches_equal(local, remote) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/git/contest/git.rb', line 177 def self.require_branches_equal local, remote Git.require_local_branch local Git.require_remote_branch remote ret = Git.compare_branches local, remote if ret > 0 puts "Branches '#{local}' and '#{remote}' have diverged." if ret == 1 abort "And branch #{local} may be fast-forwarded." elsif ret == 2 puts "And local branch #{local} is ahead of #{remote}" else abort "Branches need merging first." end end end |
.require_clean_working_tree ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/git/contest/git.rb', line 155 def self.require_clean_working_tree ret = Git.is_clean_working_tree if ret == 1 abort "fatal: Working tree contains unstaged changes. Aborting." end if ret == 2 abort "fatal: Index contains uncommited changes. Aborting." end end |
.require_local_branch(branch) ⇒ Object
165 166 167 168 169 |
# File 'lib/git/contest/git.rb', line 165 def self.require_local_branch branch if ! Git.local_branch_exists branch abort "fatal: Local branch '#{branch}' does not exist and is required." end end |
.require_remote_branch(branch) ⇒ Object
171 172 173 174 175 |
# File 'lib/git/contest/git.rb', line 171 def self.require_remote_branch branch if ! Git.remote_branch_exists branch abort "fatal: Remote branch '#{branch}' does not exist and is required." end end |