So I've spent some time to get this to work under different scenarios in bash, and even though it is not dotnet specific I thought I'd share my results, as the concepts are universal.
You have two options to get files from remote:
- Check out and sync a workspace mapping using a temporary client/workspace, like Sam Stafford wrote in his answer. Here is that in bash with comments:
# p4 uses this global variable as client/workspace name by default.
# Alternatively, you can also use -c in every command.
P4CLIENT="TEMP_SCRIPT_CLIENT"
# mapping remote folder to relative workspace folder (recursive)
wokspaceMapping="//depot/dir1/... //$P4CLIENT/..."
p4 client -d $P4CLIENT # make sure the workspace does not exist already
# Creating a client on the console gives an editor popup to confirm the
# workspace specification, unless you provide a specification on stdin.
# You can however generate one to stdout, and pipe the result to stdin again.
p4 --field View="$wokspaceMapping" --field Root="/some/local/path" client -o |
p4 client -i
p4 sync -p # download all the files
p4 client -d $P4CLIENT # remove workspace when you are done.
- Use
p4 print
to print the content of each file as jhwist wrote suggested, so you don't need to define a workspace at all. The disadvantage is that you have to handle each file individually, and create any directories yourself.
p4RemoteRoot="//depot/dir1"
# First, list files and strip output to file path
# because `p4 files` prints something like this:
# //depot/dir1/file1.txt#1 - add change 43444817 (text)
# //depot/dir1/folder/file2.txt#11 - edit change 43783713 (text)
files="$(p4 files $p4RemoteRoot/... | sed 's/\(.*\)#.*/\1/')"
for wsFile in $files; do
# construct the local path from the remote one
targetFile="$localPath/${wsFile#$p4RemoteRoot/}"
# create the parent dir if it doesn't exist. p4 files doesn't list directories
mkdir -p $(dirname $targetFile)
# print the file content from remote and write that to the local file.
p4 print -q $wsFile > $targetFile
done
Note: I couldn't find any documentation for the --field
argument, but it seems you can use everything from "Form Fields" as specified in the docs: https://www.perforce.com/manuals/v18.2/cmdref/Content/CmdRef/p4_client.html