Module: Cmd

Extended by:
Cmd
Included in:
Cmd
Defined in:
lib/git/stash/sclib/command.rb

Instance Method Summary collapse

Instance Method Details

#branchExist?(branch) ⇒ Boolean

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

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# 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

#branchNameObject



50
51
52
53
54
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/git/stash/sclib/command.rb', line 68

def branchRefExist?(branch)
  execRetQuiet "test -f \"#{gitdir}/refs/#{branch}\""
end

#changesCountObject

trackedファイルの変更数



152
153
154
155
156
# 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) ⇒ Object

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



14
15
16
# File 'lib/git/stash/sclib/command.rb', line 14

def exec(cmd)
  raise "failed, cmd:#{cmd}" if !execRet cmd
end

#execForRebase(name, rebaseCmd) ⇒ Object

rebase専用コマンド



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 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) ⇒ Object



23
24
25
# File 'lib/git/stash/sclib/command.rb', line 23

def execQuiet(cmd)
  raise "failed, cmd:#{cmd}" if !execRetQuiet cmd
end

#execRet(cmd) ⇒ Object



18
19
20
21
# File 'lib/git/stash/sclib/command.rb', line 18

def execRet(cmd)
  Kernel.system(cmd)
  $?.success?
end

#execRetQuiet(cmd) ⇒ Object



27
28
29
30
# 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

#getBackupObject



90
91
92
# File 'lib/git/stash/sclib/command.rb', line 90

def getBackup
  findFirstCommitStashRef(){|line| line.match(/#{BACKUP_SUFFIX}$/)}
end

#getPatchRemainObject



87
88
89
# File 'lib/git/stash/sclib/command.rb', line 87

def getPatchRemain
  findFirstCommitStashRef(){|line| line.match(/#{PATCH_REMAIN_SUFFIX}$/)}
end

#getTmpObject



84
85
86
# File 'lib/git/stash/sclib/command.rb', line 84

def getTmp
  findFirstCommitStashRef(){|line| line.match(/#{TMP_SUFFIX}$/)}
end

#gitdirExist?Boolean

他のコマンド

Returns:

  • (Boolean)


34
35
36
# File 'lib/git/stash/sclib/command.rb', line 34

def gitdirExist?
  execRetQuiet 'git rev-parse --git-dir'
end

#listup(_branchName, all) ⇒ Object


stash-commit — stash-commmitのbranch一覧



74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object

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



204
205
206
# 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') ⇒ Boolean

引数のブランチは親子?

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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?Boolean

Returns:

  • (Boolean)


161
162
163
164
# 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') ⇒ Object



44
45
46
# File 'lib/git/stash/sclib/command.rb', line 44

def revision(target='HEAD')
  `git rev-parse --short #{target}`.chomp
end

#sameBranch?(a, b = 'HEAD') ⇒ Boolean

引数のブランチは同じ?

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# 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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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) ⇒ Object



109
110
111
# File 'lib/git/stash/sclib/command.rb', line 109

def stashName(branch, no)
  "#{PREFIX}/#{branch}@#{no}"
end

#titleObject



47
48
49
# File 'lib/git/stash/sclib/command.rb', line 47

def title
  NKF.nkf('-w', `git log -1 --pretty=format:\"%s\"`)
end

#tuneLimitObject




235
236
237
238
# File 'lib/git/stash/sclib/command.rb', line 235

def tuneLimit
  # 一番軽いと思われる外部コマンド
  `echo tuneLimit`
end