How to test whether file at path exists in repo?

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)".


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.

Tags:

Perforce