module Cmd

Public Instance Methods

branchExist?(branch) click to toggle source

引数のブランチは存在してるか?

# File lib/git/stash/sclib/command.rb, line 57
def branchExist?(branch)
  `git branch`.each_line do |_line|
    line = _line
    line = line[1..-1] if line[0] == '*'
    line = line.strip
    return true if line == branch
  end

  return false
end
branchName() click to toggle source
# File lib/git/stash/sclib/command.rb, line 50
def branchName
  `git branch`.each_line do |line|
    return NKF.nkf('-w', line[1..-1].strip)  if line[0] == '*'
  end
end
branchRefExist?(branch) click to toggle source

引数のdetached branchは存在してるか?

# File lib/git/stash/sclib/command.rb, line 68
def branchRefExist?(branch)
  execRetQuiet "test -f \"#{gitdir}/refs/#{branch}\""
end
changesCount() click to toggle source

trackedファイルの変更数

# File lib/git/stash/sclib/command.rb, line 152
def changesCount
  count = 0
  `git status --untracked-files=no --short`.each_line {count += 1}
  "#{count}"
end
exec(cmd) click to toggle source

成否を戻り値か例外で知らせる

# File lib/git/stash/sclib/command.rb, line 14
def exec(cmd)
  raise "failed, cmd:#{cmd}" if !execRet cmd
end
execForRebase(name, rebaseCmd) click to toggle source

rebase専用コマンド

# File lib/git/stash/sclib/command.rb, line 211
  def execForRebase(name, rebaseCmd)
    o, e, s = Open3::capture3 rebaseCmd
    puts o if o != ''
    if !s.success?
      # NOTE : コンフリクト時、
      #        標準出力にコンフリクトメッセージ
      #        標準エラー出力に--continue | --skip | --abortについて
      if o.match('CONFLICT') and o.match('Merge conflict')
        STDERR.puts <<-EOS
error: could not apply #{revision name}...

problem resolved, run "git stash-commit --continue".
skip this patch, run "git stash-commit --skip".
cancel this time, run "git stash-commit --abort".
EOS
      else
        STDERR.puts e
      end
      raise "failed, cmd:#{rebaseCmd}"
    end
  end
execQuiet(cmd) click to toggle source
# File lib/git/stash/sclib/command.rb, line 23
def execQuiet(cmd)
  raise "failed, cmd:#{cmd}" if !execRetQuiet cmd
end
execRet(cmd) click to toggle source
# File lib/git/stash/sclib/command.rb, line 18
def execRet(cmd)
  Kernel.system(cmd)
  $?.success?
end
execRetQuiet(cmd) click to toggle source
# File lib/git/stash/sclib/command.rb, line 27
def execRetQuiet(cmd)
  return execRet "#{cmd} > /dev/null 2>&1" if !win?
  execRet "#{cmd} > nul 2> nul"
end
getBackup() click to toggle source
# File lib/git/stash/sclib/command.rb, line 90
def getBackup
  findFirstCommitStashRef(){|line| line.match(/#{BACKUP_SUFFIX}$/)}
end
getPatchRemain() click to toggle source
# File lib/git/stash/sclib/command.rb, line 87
def getPatchRemain
  findFirstCommitStashRef(){|line| line.match(/#{PATCH_REMAIN_SUFFIX}$/)}
end
getTmp() click to toggle source
# File lib/git/stash/sclib/command.rb, line 84
def getTmp
  findFirstCommitStashRef(){|line| line.match(/#{TMP_SUFFIX}$/)}
end
gitdirExist?() click to toggle source

他のコマンド

# File lib/git/stash/sclib/command.rb, line 34
def gitdirExist?
  execRetQuiet 'git rev-parse --git-dir'
end
listup(_branchName, all) click to toggle source

stash-commit — stash-commmitのbranch一覧

# File lib/git/stash/sclib/command.rb, line 74
def listup(_branchName, all)
  preCmd = "git branch | sed -E 's/^\\*/ /' | awk '{print $1}' | grep -E '^#{PREFIX}/'"
  if all
    print `#{preCmd}`
  else
    # グループ表示
    rootBranch = _branchName.match(/^(#{PREFIX}\/)?(.+?)(@.+)?$/)[2]
    print `#{preCmd} | grep "#{rootBranch}"`
  end
end
mergeBaseHash(a, b) click to toggle source

2つのブランチの交差点をcommit hashで返す

# File lib/git/stash/sclib/command.rb, line 204
def mergeBaseHash(a, b)
  `git show-branch --merge-base \"#{a}\" \"#{b}\"`.chomp[0...7] # [0...7]: --short
end
parentChildBranch?(a, b='HEAD') click to toggle source

引数のブランチは親子?

# File lib/git/stash/sclib/command.rb, line 166
def parentChildBranch?(a, b='HEAD')
  hashs = `git rev-parse \"#{a}\" \"#{b}\" \"#{a}~\" \"#{b}~\"`.split

  hash_a_parent = hashs[0] || ''
  hash_b_parent = hashs[1] || ''
  hash_a_child  = hashs[2] || ''
  hash_b_child  = hashs[3] || ''

              if hash_a_parent == ''
    puts 'illegal branch'
    return false
  end
  if hash_b_parent == ''
    puts 'illegal branch'
    return false
  end

  hash_a_parent == hash_b_child or hash_b_parent == hash_a_child
end
rebaseInProgress?() click to toggle source

rebase中? stackoverflow.com/questions/3921409/how-to-know-if-there-is-a-git-rebase-in-progress rebase-apply : rebase rebase-merge : rebase -i

# File lib/git/stash/sclib/command.rb, line 161
def rebaseInProgress?
              git_dir = gitdir
  execRetQuiet "test -d \"#{git_dir}/rebase-merge\" -o -d \"#{git_dir}/rebase-apply\""
end
revision(target='HEAD') click to toggle source
# File lib/git/stash/sclib/command.rb, line 44
def revision(target='HEAD')
  `git rev-parse --short #{target}`.chomp
end
sameBranch?(a, b='HEAD') click to toggle source

引数のブランチは同じ?

# File lib/git/stash/sclib/command.rb, line 186
def sameBranch?(a, b='HEAD')
  hashs = `git rev-parse \"#{a}\" \"#{b}\"`.split

  hash_a = hashs[0] || ''
  hash_b = hashs[1] || ''

              if hash_a == ''
    puts 'illegal branch'
    return false
  end
  if hash_b == ''
    puts 'illegal branch'
    return false
  end

  hash_a == hash_b
end
stashCommitRename(renameOld, renameNew) click to toggle source
# File lib/git/stash/sclib/command.rb, line 112
  def stashCommitRename(renameOld, renameNew)
    # NOTE : 利用頻度低いので未tuning

    # 名前被りチェック
    preCmd = "#{stashCommitListAllString} | sed -E 's/^#{PREFIX}\\/(.+)@.+$/\\1/g' | sort | uniq"

    # renameOldの存在チェック
    if `#{preCmd} | grep -w \"#{renameOld}\" | wc -l | tr -d '\n'` == '0'
      puts "'#{renameOld}' name is not found"
      return false
    end

    beforeCount = `#{preCmd} | wc -l | tr -d '\n'`
    # NOTE: /利用のため、validateで弾いてる@をsed用の区切りに利用する
    #afterCount = `#{preCmd} | sed 's/#{renameOld}/#{renameNew}/' | sort | uniq | wc -l | tr -d '\n'`
    afterCount = `#{preCmd} | sed 's@#{renameOld}@#{renameNew}@' | sort | uniq | wc -l | tr -d '\n'`

    # 数が減っている(= 名前が被ってる)
    if beforeCount != afterCount
      puts 'name is overlap'
      return false
    end

    # ここまでくれば安心
    # rename処理
    execRet <<-EOS
#{stashCommitListAllString} | \
  grep -E "^.+#{renameOld}@.+$" | \
  awk '{old=$0; new=$0; sub("#{renameOld}", "#{renameNew}", new); print old; print new;}' | \
  xargs -L 2 git branch -m
EOS
  end
stashName(branch, no) click to toggle source
# File lib/git/stash/sclib/command.rb, line 109
def stashName(branch, no)
  "#{PREFIX}/#{branch}@#{no}"
end
title() click to toggle source
# File lib/git/stash/sclib/command.rb, line 47
def title
  NKF.nkf('-w', `git log -1 --pretty=format:\"%s\"`)
end
tuneLimit() click to toggle source

# File lib/git/stash/sclib/command.rb, line 235
def tuneLimit
  # 一番軽いと思われる外部コマンド
  `echo tuneLimit`
end

Private Instance Methods

findFirstCommitStashRef(&pred) click to toggle source
# File lib/git/stash/sclib/command.rb, line 94
def findFirstCommitStashRef(&pred)
  ret = ''
  if !win? then
    ret = `find #{gitdir}/refs/#{PREFIX} -type f 2> /dev/null`
  else
    ret = `find #{gitdir}/refs/#{PREFIX} -type f 2> nul`
  end
  ret.each_line do |_line|
    line = _line.strip
    line = line.sub(/^.*\.git\/refs\//, '')
    return line if pred.call line
  end
  return ''
end
gitdir() click to toggle source
# File lib/git/stash/sclib/command.rb, line 39
def gitdir
  @_memo_gitdir = @_memo_gitdir || `git rev-parse --git-dir`.chomp
end
stashCommitListAllString() click to toggle source
# File lib/git/stash/sclib/command.rb, line 145
def stashCommitListAllString
  "git branch | sed -E 's/^\\*/ /' | awk '{print $1}' | grep -E '^#{PREFIX}/'"
end