I used to make this mistake when using perforce: I wrote a new file and forgot to add it to the perforce depot to submit it in a rush. Other people could not sync and compile it, affecting the team progress. A ruby script is written to check which files in the current client are not added to the depot. Run p4nothave before each submit to know which files are not added. In addition, you can use p4nothave | P4-X-add to add these files to the depot.
The basic idea is to first use P4 have to obtain which files in the current directory are in the depot, and then traverse the directory to find the files that are not in the depot. When implementing this function, you must consider that some files have been added but no submit is available, so P4 have will not display it. You need to run P4 opened to find the files that have been added but have not been 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