7

Given a path (on my computer), how can I test whether that file is under version control (ie. a copy exists in the Perforce depot)? I'd like to test this at the command line.

3 Answers 3

7

Check p4 help files. In short, you run p4 files <your path here> and it will give you the depot path to that file. If it isn't in the depot, you'll get "no such file(s)".

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Mark. Any idea how to read the latest docs online? I can only find 12.1 perforce.com/perforce/r12.1/manuals/cmdref/help.html
The current docs are always available at perforce.com/perforce/doc.current/manuals/p4guide
Hum the page on the diff command has disappeared perforce.com/perforce/r12.1/manuals/cmdref/diff.html
I thought about that too, but the problem with p4 where is that it will also tell you where a file WOULD be mapped if it existed. For example, if you are mapping //depot/project/... to a root of C:\project and you do a p4 where C:\project\thisfiledoesnotexist.txt it will still give you //depot/project/thisfiledoesnotexist.txt //myclient/project/thisfiledoesnotexist.txt C:\project\thisfiledoesnotexist.txt which isn't as helpful as I wish it were. :-)
|
3

For scripting, p4 files FILE is insufficient because it doesn't change its exit code when there is no such file.

Instead, you can pass it through grep, which looks for perforce paths' telltale pair of leading slashes:

# silently true when the file exists or false when it does not.
p4_exists() { 
  p4 files -e "$1" 2>/dev/null |grep -q ^//
}

You can get rid of the 2>/dev/null and grep's -q if you want visible output.

Before p4 files version 2012.1 (say p4 files version 2011.1), it didn't support -e. You'll have to add |grep -v ' - delete [^-]*$' before the grep above.

⚠ Warning: A future p4 release could change the formatting and break this logic.

2 Comments

instead of a file how do we test the repo folder path is valid or not ?
@Ciastopiekarz – great question, but I don't have an easy answer. I suggest asking it in a new SO question. Provide a link to this question as background information.
1

Similar to Adam Katz's solution except more likely to be supported by future releases of p4, you can pass global option -s which 'prepends a descriptive field' to each line. This is one of 'text', 'info', 'error' or 'exit' followed by a colon (and a space, it seems). This is intended for facilitating scripting.

For all files passed in to the p4 -s files command, you should get one line back per file. If the file exists in the depot, the line starts with info: whereas if the file does not exist in the depot, the line starts with error:. For example:

info: //depot/<depot-path-to-file>
error: <local-path-to-file>

So, essentially, the status lines are the equivalent of an exit code but on a per-file basis. An exit code alone wouldn't cope neatly with an arbitrary number of files passed in.

Note that if there is a different error (e.g. a connection error) then an error line is still output. So, if you really want the processing to be robust, you may want to combine this with what Adam Katz suggested or perhaps grep for the basename of the file in the output line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.