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)


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

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 ⇒ Object



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

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)


64
65
66
# File 'lib/git/stash/sclib/command.rb', line 64

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

#changesCount ⇒ Object

trackedファイルの変更数



140
141
142
143
144
# File 'lib/git/stash/sclib/command.rb', line 140

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

#exec(cmd) ⇒ Object

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



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

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

#execForRebase(name, rebaseCmd) ⇒ Object

rebase専用コマンド



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/git/stash/sclib/command.rb', line 199

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



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

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

#execRet(cmd) ⇒ Object



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

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

#execRetQuiet(cmd) ⇒ Object



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

def execRetQuiet(cmd)
  execRet "#{cmd} > /dev/null 2>&1"
end

#getBackup ⇒ Object



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

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

#getPatchRemain ⇒ Object



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

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

#getTmp ⇒ Object



80
81
82
# File 'lib/git/stash/sclib/command.rb', line 80

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

#gitdirExist? ⇒ Boolean

他のコマンド

Returns:

  • (Boolean)


30
31
32
# File 'lib/git/stash/sclib/command.rb', line 30

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

#listup(_branchName, all) ⇒ Object


stash-commit --- stash-commmitのbranch一覧



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

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



192
193
194
# File 'lib/git/stash/sclib/command.rb', line 192

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)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/git/stash/sclib/command.rb', line 154

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)


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

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

#revision(target = 'HEAD') ⇒ Object



40
41
42
# File 'lib/git/stash/sclib/command.rb', line 40

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

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

引数のブランチは同じ?

Returns:

  • (Boolean)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/git/stash/sclib/command.rb', line 174

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/git/stash/sclib/command.rb', line 102

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 <<-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



99
100
101
# File 'lib/git/stash/sclib/command.rb', line 99

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

#title ⇒ Object



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

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

#tuneLimit ⇒ Object




223
224
225
226
# File 'lib/git/stash/sclib/command.rb', line 223

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