Here's a tiny Perl script that does that without using any regexes:
foreach(`p4 -F %domainName% branches`) {
chomp;
if (`p4 -F %code0% populate -fn -b $_ -s $ARGV[0]` != 554768862) { print $_."\n"; }
}
Sample output (where each branch maps //depot/main
but only foo
maps //depot/foo
):
C:\Perforce\test>p4 branches
Branch bar 2020/03/16 'Created by Samwise. '
Branch foo 2020/03/16 'Created by Samwise. '
Branch spaces 2020/03/16 'Created by Samwise. '
C:\Perforce\test>perl get-branch.pl //depot/foo/bleh
foo
C:\Perforce\test>perl get-branch.pl //depot/main/bleh
bar
foo
spaces
It works by running a populate
command over each branch spec with the specified file path, and looking for the specific error message no source file(s) in branch view
(which is error code 554768862; I got this by playing around with p4 -e populate ...
). If it doesn't get that message it assumes the path is mapped by the branch view. Note that this script as written is not robust in the face of other errors (e.g. permissions).