module Git

git.rb

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

version.rb

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

Public Class Methods

all_branches() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

0: same 1: first branch needs ff 2: second branch needs ff 3: branch needs merge 4: there is no merge

# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

use return value

# 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() click to toggle source
# 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) click to toggle source
# File lib/git/contest/git.rb, line 64
def self.local_branch_exists(branch_name)
  Git.local_branches().include?(branch_name)
end
local_branches() click to toggle source
# 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) click to toggle source
# File lib/git/contest/git.rb, line 60
def self.remote_branch_exists(branch_name)
  Git.remote_branches().include?(branch_name)
end
remote_branches() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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