How do you add an AltRoot for Windows using `p4 client` non-interactively?

30 Views Asked by At

I am trying to add an Windows AltRoot to my p4 client automatically in command line without needing to edit the client form interactively. I am doing this from a Linux machine. Below is the command I tried:

p4 client -o | sed "s@AltRoot:@AltRoot:\n\tN:\\\path\\\to\\\the\\\client@" | p4 client -i

However, p4 client is recognizing the drive number N: as an invalid p4 client field name.

How do we get around this?

2

There are 2 best solutions below

0
Samwise On BEST ANSWER

Use the --field global option to edit the fields produced by a <spec> -o command. This option uses the built-in spec parser so you don't need to fiddle with regexes.

You can then pipe the result directly to p4 client -i:

C:\Perforce\test>p4 --field AltRoots+=N:\path\to\the\client client -o | p4 client -i
Client Samwise-dvcs-1509687817 saved.

C:\Perforce\test>p4 client -o | grep -A1 AltRoots
#  AltRoots:    Up to two alternate client workspace roots.
#  Options:     Client options:
--
AltRoots:
        N:\path\to\the\client
0
DXZ On

I debugged the sed output by sending it to a file:

p4 client -o | sed "s@AltRoot:@AltRoot:\n\tN:\\\path\\\to\\\the\\\client@" > p4client.txt

I realized that the replacement also happens to the comment line containing AltRoot, which resulted in the following output:

# ...
#  AltRoots:
    N:\path\to\the\client    Up to two alternate client workspace roots.
#  Options:     Client options:
# ...

The inserted uncommented line is the cause of the problem.

The fix turns out to be as simple as modifying the sed expression by add ^ before the search pattern to exclude the match in the comment section:

p4 client -o | sed "s@^AltRoot:@AltRoot:\n\tN:\\\path\\\to\\\the\\\client@" | p4 client -i