4

I am using P4.NET to control perforce programmatically but there doesn't seem to be a way to specify global options like this:

http://www.perforce.com/perforce/doc.current/manuals/cmdref/o.gopts.html

Maybe someone else has an experience on how to do this?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

1 Answers1

4

You can set the global environment values using the P4Connection class like so

P4Connection p4 = new P4Connection();   
p4.Host = "127.0.0.1";    
p4.Port = 1666;   
p4.User = "joan.verge";   
p4.Client = "joanverge_main";   

If these values are not set, it will use the environment settings set in your client configuration file or windows registry (for Linux and OSX there is a configuration file in the ~.p4 directory).

If you use Perforce via the command line I would highly recommend you set up a client configurations (Note: I only know how to do this on Windows, refer to P4 KB for other platforms).

First set your global environment settings using the command line.

p4 set P4USER=joan.verge
p4 set P4PORT=127.0.0.1:1666

To test the above have been set correctly, use p4 info.

Next, put the workspace/client specific settings into a text file named p4config (no extension) and place it in the root of your workspace/client.

P4CLIENT=joanverge_main

Set one more environment variable,

p4 set P4CONFIG=p4config

Copy the p4config file to another workspace/client, edit the P4CLIENT variable to match.

Now on the command line, navigate to both directories and compare the p4 info command.

Also, setting this up allows you to use the P4 windows explorer navigation across multiple workspaces.

Edit: you might find this StackOverflow question useful that was answered by Mike a few months ago with setting the Charset property: P4.Net and P4CHARSET.

Community
  • 1
  • 1
Dennis
  • 20,275
  • 4
  • 64
  • 80
  • Thanks man, I wish I have seen this sooner. Found it while fiddling with the P4Connection p4 object. But thanks for the detailed answer, it helps big time. – Joan Venge Sep 01 '11 at 17:40
  • Also just out of curiosity, I only set the client and do operations on that client. Is this enough? Do I also need to set the user, port and host, etc? Setting only the client seems to work fine. But I don't know P4 as much. – Joan Venge Sep 01 '11 at 17:41
  • 1
    Whatever you do not set **will** be taken from the environment variables as needed. How much do you trust your users that they will have their Perforce set up correctly? – Dennis Sep 01 '11 at 17:45
  • 1
    Following up on what Dennis said, I would definitely choose to be explicit about setting user, host, and port. It's a little more code, but it can save you some time debugging why connections suddenly stopped working, only to realize that P4USER or P4PORT were changed. – Mike O'Connor Sep 01 '11 at 17:54
  • 1
    I agree with Mike. I believe in our integration of P4 we sanity check the environment variables and prompt the user with a connection dialog (with the information we know pre-filled) if anything is amiss. When you do the sanity checking log the results (you are using a logger right?) and when something goes wrong it will be easier to spot that P4USER is null etc. – Dennis Sep 01 '11 at 18:15
  • Thanks guys I will be sure to use them then. So host, port, user, client is enough to make it as robust as possible? Also it's 1 person every PC, does the user change? Like only 1 possible user can be using a workspace, right? Or are you guys saying, it's all like that, but if they still wrote some wrong data into the env variables, it's gonna fail, and perforce isn't gonna figure it out from the server side. I guess that's what you mean. – Joan Venge Sep 01 '11 at 19:51
  • Also guys instead of strong all these values separately, is it not better to make an object with those values and pass them or something? Just curious how you do it. – Joan Venge Sep 01 '11 at 20:25
  • That is a design decision for you; I would be inclined to create a class `P4Environment` that wraps all these values and has a static methods for sanity checking etc. However, before you rush off to implement this first have a look to see if one is included in P4.NET. – Dennis Sep 01 '11 at 20:58
  • What about the global flags that are not present in the environment variables, e.g. -I option to get the progress indicator? – Serge Oct 22 '15 at 17:28