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)


55
56
57
58
59
60
61
62
63
64
# File 'lib/git/stash/sclib/command.rb', line 55

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



48
49
50
51
52
# File 'lib/git/stash/sclib/command.rb', line 48

def branchName
  `git branch`.each_line do |line|
    return line[1..-1].strip if line[0] == '*'
  end
end

#branchRefExist?(branch) ⇒ Boolean

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

Returns:

  • (Boolean)


66
67
68
# File 'lib/git/stash/sclib/command.rb', line 66

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

#changesCountObject

trackedファイルの変更数



150
151
152
153
154
# File 'lib/git/stash/sclib/command.rb', line 150

def changesCount
  count = 0
  `git status --untracked-files=no --short`.each_line {count += 1}
  "#{count}"
end

#exec(cmd) ⇒ Object

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



12
13
14
# File 'lib/git/stash/sclib/command.rb', line 12

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

#execForRebase(name, rebaseCmd) ⇒ Object

rebase専用コマンド



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/git/stash/sclib/command.rb', line 209

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



21
22
23
# File 'lib/git/stash/sclib/command.rb', line 21

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

#execRet(cmd) ⇒ Object



16
17
18
19
# File 'lib/git/stash/sclib/command.rb', line 16

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

#execRetQuiet(cmd) ⇒ Object



25
26
27
28
# File 'lib/git/stash/sclib/command.rb', line 25

def execRetQuiet(cmd)
  return execRet "#{cmd} > /dev/null 2>&1" if !win?
  execRet "#{cmd} > nul 2> nul"
end

#getBackupObject



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

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

#getPatchRemainObject



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

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

#getTmpObject



82
83
84
# File 'lib/git/stash/sclib/command.rb', line 82

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

#gitdirExist?Boolean

他のコマンド

Returns:

  • (Boolean)


32
33
34
# File 'lib/git/stash/sclib/command.rb', line 32

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

#listup(_branchName, all) ⇒ Object


stash-commit — stash-commmitのbranch一覧



72
73
74
75
76
77
78
79
80
81
# File 'lib/git/stash/sclib/command.rb', line 72

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で返す



202
203
204
# File 'lib/git/stash/sclib/command.rb', line 202

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)


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/git/stash/sclib/command.rb', line 164

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)


159
160
161
162
# File 'lib/git/stash/sclib/command.rb', line 159

def rebaseInProgress?
git_dir = gitdir
  execRetQuiet "test -d \"#{git_dir}/rebase-merge\" -o -d \"#{git_dir}/rebase-apply\""
end

#revision(target = 'HEAD') ⇒ Object



42
43
44
# File 'lib/git/stash/sclib/command.rb', line 42

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

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

引数のブランチは同じ?

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/git/stash/sclib/command.rb', line 184

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



110
111
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
# File 'lib/git/stash/sclib/command.rb', line 110

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



107
108
109
# File 'lib/git/stash/sclib/command.rb', line 107

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

#titleObject



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

def title
  `git log -1 --pretty=format:\"%s\"`
end

#tuneLimitObject




233
234
235
236
# File 'lib/git/stash/sclib/command.rb', line 233

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