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ファイルの変更数



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

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専用コマンド



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

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 "error: could not apply \#{revision name}...\n\nproblem resolved, run \"git stash-commit --continue\".\nskip this patch, run \"git stash-commit --skip\".\ncancel this time, run \"git stash-commit --abort\".\n"
    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>%1"
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で返す



200
201
202
# File 'lib/git/stash/sclib/command.rb', line 200

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)


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

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)


157
158
159
160
# File 'lib/git/stash/sclib/command.rb', line 157

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)


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

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
# 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'`
  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 "\#{stashCommitListAllString} | \\\ngrep -E \"^.+\#{renameOld}@.+$\" | \\\nawk '{old=$0; new=$0; sub(\"\#{renameOld}\", \"\#{renameNew}\", new); print old; print new;}' | \\\nxargs -L 2 git branch -m\n"
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




231
232
233
234
# File 'lib/git/stash/sclib/command.rb', line 231

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