Add the nothave command to perforce and find the file (in Ruby) that has not been added to depot)

Source: Internet
Author: User
Tags perforce

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.