以前在使用Perforce時犯過這樣的錯誤:編寫了新的檔案,忘了添加到 Perforce depot 中就匆匆submit,別人sync下來編譯不過,影響團隊進度。編寫了一個Ruby指令碼,用於檢查當前client中有哪些檔案沒有添加到depot中,每次submit之前運行一下 p4nothave,就能知道還有哪些檔案沒有add進去。另外用 p4nothave | p4 -x - add 可以把這些檔案都add到depot中。
基本思路,先用 p4 have 獲得目前的目錄下有哪些檔案是在depot中的,再遍曆目錄,找出不在depot的中的檔案。在實現時要考慮到有些檔案已經add但還沒有submit,那麼p4 have不會顯示它,需要再運行p4 opened找出那些已經add卻還沒有submit的檔案。
#!/usr/bin/env ruby
# Usage: p4nothave [paths]
require 'find'
def filter_out(f)
# filter out temp files
if File.basename(f) =~ /^/./
return true
end
false
end
def check_p4_output(firstline)
if firstline =~ /^Client '(.*?)' unknown/
puts "Client '#$1' unknown at path '#{Dir.getwd}'"
exit 1
end
end
def p4have(path)
have = IO.popen("p4 have 2>&1").readlines
check_p4_output(have[0])
in_depot = {}
have.each do |line|
# format of p4 have:
#
# //depot/trunk/hello#1 - /home/schen/p4client/trunk/hello
#
if line =~ /^.*? - (.*)$/
in_depot[$1] = 1
end
end
return in_depot
end
def p4added(path)
opened = IO.popen("p4 opened 2>&1").readlines
check_p4_output(opened[0])
in_depot = {}
opened.each do |line|
# format of p4 opened:
#
# //depot/trunk/p4scripts/p4nothave.rb#1 - add default change (text)
#
if line =~ /^(.*?)((#|@)(.*?))? - add/
# get the local filename from remote filename
# format of p4 where:
#
# //depot/trunk/temp //schen_asus_1/trunk/temp /home/schen/p4client/trunk/temp
#
`p4 where #$1` =~ /^(.*) (.*) (.*)/
in_depot[$3] = 1
end
end
return in_depot
end
def nothave(path)
in_depot = p4have(path).merge(p4added(path))
Find.find(path) do |f|
if File.file?(f) && !in_depot.include?(File.expand_path(f))
if !filter_out(f)
puts f
end
end
end
end
paths = ARGV.empty? ? ["."] : ARGV
paths.each do |path|
nothave(path)
end