Top Level Namespace

Defined Under Namespace

Modules: Contest, Git

Instance Method Summary collapse

Instance Method Details

#get_configObject



44
45
46
47
# File 'lib/git/contest/common.rb', line 44

def get_config
  config_path = File.expand_path($git_contest_config)
  YAML.load_file config_path
end

#git_all_branchesObject



83
84
85
86
87
88
89
90
# File 'lib/git/contest/git.rb', line 83

def git_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

#git_branch_exists(branch_name) ⇒ Object



65
66
67
# File 'lib/git/contest/git.rb', line 65

def git_branch_exists(branch_name)
  git_all_branches().include?(branch_name)
end

#git_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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/git/contest/git.rb', line 119

def git_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}\"") > 0
      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

#git_contest_has_develop_configuredObject



31
32
33
34
# File 'lib/git/contest/git.rb', line 31

def git_contest_has_develop_configured
  develop = (git_do 'config --get git.contest.branch.develop').strip
  develop != '' && git_local_branches().include?(develop)
end

#git_contest_has_master_configuredObject



26
27
28
29
# File 'lib/git/contest/git.rb', line 26

def git_contest_has_master_configured
  master = (git_do 'config --get git.contest.branch.master').strip
  master != '' && git_local_branches().include?(master)
end

#git_contest_has_prefix_configuredObject



36
37
38
# File 'lib/git/contest/git.rb', line 36

def git_contest_has_prefix_configured
  git_do_no_echo 'config --get git.contest.branch.prefix'
end

#git_contest_is_initializedObject



20
21
22
23
24
# File 'lib/git/contest/git.rb', line 20

def git_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

#git_contest_resolve_nameprefix(name, prefix) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/git/contest/git.rb', line 40

def git_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

#git_current_branchObject



92
93
94
95
96
# File 'lib/git/contest/git.rb', line 92

def git_current_branch
  ret = git_do('branch --no-color').lines
  ret = ret.grep /^\*/
  ret[0].gsub(/^[* ] /, '').strip
end

#git_do(*args) ⇒ Object

git.rb

Copyright © 2013 Hiroyuki Sano <sh19910711 at gmail.com> Licensed under the MIT-License.



8
9
10
11
# File 'lib/git/contest/git.rb', line 8

def git_do(*args)
  puts "git #{args.join(' ')}" if ENV['GIT_CONTEST_DEBUG'] == 'ON'
  return `git #{args.join(' ')} 2>&1`.strip
end

#git_do_no_echo(*args) ⇒ Object

use return value



14
15
16
17
# File 'lib/git/contest/git.rb', line 14

def git_do_no_echo(*args)
  puts "git #{args.join(' ')}" if ENV['GIT_CONTEST_DEBUG'] == 'ON'
  system "git #{args.join(' ')} >/dev/null 2>&1"
end

#git_is_clean_working_treeObject



98
99
100
101
102
103
104
105
106
# File 'lib/git/contest/git.rb', line 98

def git_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

#git_local_branch_exists(branch_name) ⇒ Object



61
62
63
# File 'lib/git/contest/git.rb', line 61

def git_local_branch_exists(branch_name)
  git_local_branches().include?(branch_name)
end

#git_local_branchesObject



76
77
78
79
80
81
# File 'lib/git/contest/git.rb', line 76

def git_local_branches
  cmd_ret = git_do 'branch --no-color'
  cmd_ret.lines.map {|line|
    line.gsub(/^[*]?\s*/, '').gsub(/\s*$/, '').strip
  }
end

#git_remote_branch_exists(branch_name) ⇒ Object



57
58
59
# File 'lib/git/contest/git.rb', line 57

def git_remote_branch_exists(branch_name)
  git_remote_branches().include?(branch_name)
end

#git_remote_branchesObject



69
70
71
72
73
74
# File 'lib/git/contest/git.rb', line 69

def git_remote_branches
  cmd_ret = git_do 'branch -r --no-color'
  cmd_ret.lines.map {|line|
    line.gsub(/^[*]?\s*/, '').gsub(/\s*$/, '').strip
  }
end

#git_repo_is_headlessObject



108
109
110
# File 'lib/git/contest/git.rb', line 108

def git_repo_is_headless
  ! git_do_no_echo 'rev-parse --quiet --verify HEAD'
end

#initObject



14
15
16
17
# File 'lib/git/contest/common.rb', line 14

def init
  init_global
  init_home
end

#init_globalObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/git/contest/common.rb', line 19

def init_global
  $git_contest_home   = File.expand_path(ENV['GIT_CONTEST_HOME'] || "~/.git-contest")
  $git_contest_config = File.expand_path(ENV['GIT_CONTEST_CONFIG'] || "#{$git_contest_home}/config.yml")
  if git_do_no_echo 'branch'
    $MASTER = git_do 'config --get git.contest.branch.master'
    $PREFIX = git_do 'config --get git.contest.branch.prefix'
    $ORIGIN = git_do 'config --get git.contest.origin'
    if $ORIGIN == ''
      $ORIGIN = 'origin'
    end
    $GIT_CONTEST_GIT_OK = true
  else
    $GIT_CONTEST_GIT_OK = false
  end
end

#init_homeObject



35
36
37
38
39
40
41
42
# File 'lib/git/contest/common.rb', line 35

def init_home
  if ! FileTest.exists? $git_contest_home
    FileUtils.mkdir $git_contest_home
  end
  if ! FileTest.exists? $git_contest_config
    FileUtils.touch $git_contest_config
  end
end

#is_test_mode?Boolean

test.rb

Copyright © 2013 Hiroyuki Sano <sh19910711 at gmail.com> Licensed under the MIT-License.

Returns:

  • (Boolean)


8
9
10
# File 'lib/git/contest/test.rb', line 8

def is_test_mode?
  ENV['TEST_MODE'] === 'TRUE'
end

#require_branch(branch) ⇒ Object



140
141
142
143
144
# File 'lib/git/contest/git.rb', line 140

def require_branch(branch)
  if ! git_all_branches().include?(branch)
    abort "Branch #{branch} does not exist."
  end
end

#require_branch_absent(branch) ⇒ Object



146
147
148
149
150
# File 'lib/git/contest/git.rb', line 146

def 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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/git/contest/git.rb', line 174

def require_branches_equal local, remote
  require_local_branch local
  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_treeObject



152
153
154
155
156
157
158
159
160
# File 'lib/git/contest/git.rb', line 152

def 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



162
163
164
165
166
# File 'lib/git/contest/git.rb', line 162

def 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



168
169
170
171
172
# File 'lib/git/contest/git.rb', line 168

def require_remote_branch branch
  if ! git_remote_branch_exists branch
    abort "fatal: Remote branch '#{branch}' does not exist and is required."
  end
end