Jump to content


p4 command line to access multiple connections?

p4 command commands connection

  • Please log in to reply
9 replies to this topic

#1 rasonage

rasonage

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 22 August 2019 - 01:35 AM

I can open multiple connections in p4v, however if I have opted to "Use current connection for environment settings" I'm pretty much locked out of getting information on other connections via p4 commands.
Saving a config file and setting P4CONFIG only works within the current connection, and I need to access all connections and their info from the command prompt so I can switch between them easily.

currently working in 2017.2 (can not upgrade to 2019 by directive of our IT guy) on a 64bit windows 10 OS.

#2 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 928 posts

Posted 22 August 2019 - 04:36 AM

View Postrasonage, on 22 August 2019 - 01:35 AM, said:

Saving a config file and setting P4CONFIG only works within the current connection

Use your normal CLI terminal rather than the one P4V opens.  (P4V will set env variables that override everything.)  Then your P4CONFIG files will work normally and you can switch between connections easily.

#3 rasonage

rasonage

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 22 August 2019 - 06:49 PM

Thanks for responding, but I was already using the windows command line, not the one from p4v
and when one connection's environment is set up I can't see a way to access environment settings from other connections as user has, and that's what I need to do.

#4 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 928 posts

Posted 22 August 2019 - 08:47 PM

The idea behind having multiple P4CONFIG files (which I thought you were using, but now I'm guessing not?) is that you have a different one in each directory (which is also where you have your workspace for each connection).  Just as an example, suppose you work with three different servers -- your main internal server at "computer:1666", a test server at "play:1999", and an external server at "public:1666".  The workspaces for these three servers live in the local paths C:\main, C:\test\999, and C:\public.  You can set up P4CONFIG files like this:

p4 set P4CONFIG=p4config.txt
echo P4PORT=computer:1666>c:\main\p4config.txt
echo P4PORT=play:1999>c:\test\999\p4config.txt
echo P4PORT=public:1666>c:\public\p4config.txt


Now when you cd into one of those directories, you're automatically connecting to the corresponding server.  If you have different usernames on the different servers, just add a P4USER=yourname line to the relevant config file(s).  Ditto if you need to use different client names, charsets, etc.

Here's the section from Practical Perforce on P4CONFIG files.

#5 rasonage

rasonage

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 27 August 2019 - 11:41 PM

the problem I'm running into is, using your example:
if my workspace is set to P4PORT=computer:1666>c:\main\ then P4CONFIG variable is already pointing to p4config.txt (P4CONFIG only points to a file, not it's path, so I can't have a p4config file outside of the current connection) and I can't access the information from c:\test\999\p4config.txt.
I need to derive and access the root path of the second and third connection (and other subsequent connections) while in the first connection.

#6 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 928 posts

Posted 28 August 2019 - 12:08 AM

You *can* connect to another server by using the global flags, but I don't understand why you'd want to, because that's so much more cumbersome, and I'm kind of afraid that if I just say "use the global flags like this" you'll come away with the belief that this is a lot more difficult than it is.  :)

Could you give me some kind of example of what it is you're trying to do that makes you not want to just open another terminal window or cd over to the other workspace?

#7 rasonage

rasonage

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 28 August 2019 - 11:18 PM

I'm writing a tool that does some of the basic p4 functions, but it I found a bug where if you're working with a file that's part of a different connection than what the user currently has in their environment settings, the p4 part of the tool won't check out, mark for add, sync, shelve, or submit said file, and checking out a file for edit or add makes said file write-able... if a file isn't write-able things can break and cause other issues.

I had also intended the p4 part of  the tool to work in a batch process, of the which are not supposed to be babysat and should work even if the user forgot to switch their connection/environment variables in p4v (or if we're working across a multitude of projects, each a possible separate p4 connection).

While I could have the tool/script search an entire system for every p4config file, that slows things to a crawl while that process is running, or I could have the tool save its own p4config file locally that saves each connection environment settings every time that tool is used (but for cases where a user is trying to access files of a depot never before accessed by that tool, it will break), I noticed the p4v - connection - environment settings accomplishes this in a flash by unchecking and rechecking "Use current connection for environment settings". Hence why I'd like to access that functionality.

#8 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 928 posts

Posted 29 August 2019 - 02:16 PM

Something you'll have to consider for this tool before even thinking about how to specify the connection settings is how it's going to know what all the connection settings should be if that information isn't anywhere in the user's environment...

If the user has P4CONFIG files set up, and you know what the local path of the file is, you don't have to search for anything; just have your tool switch its working directory to where the file is (you can also use the -d global option to "fake" this for p4's purposes), and it'll automatically pick up the right P4CONFIG file.  Easy-peasy!  This is what I'd do with a tool I was writing for my own environment, since I always have P4CONFIG files everywhere for this exact reason.

Based on the information you've given so far, this is how I'd implement this tool (i.e. have it cd to where the file is), and I'd just let users know that if they have multiple connections they'll need to either switch to the right one in their global environment before running the tool OR set up P4CONFIG files so it (and tools like it) can just work automatically.  Trying to make it work magically with no help from the user at all is potentially going to make a lot of work for yourself.

If your tool knows the server and depot path of the file, but it's being executed in an unfamiliar environment where it doesn't know its local path, that's actually a pretty hard problem to solve, because you'll need to figure out the name of the workspace in order to be able to ask the server where this file lives on this client host.  One solution there might be to create a new workspace (in a new temp directory) and clean it up once you're done modifying the file.  If you're pretty sure there is a workspace on the current client host, and you want to use it, but you don't know what it is, you can connect to the server, run "p4 clients", and check the Host and View fields for potential matches.

If your tool somehow innately knows all the connection settings (port, user, AND client), despite them being different from what's in the current execution environment, and the user doesn't have P4CONFIG files set up, then you can manually specify the settings with the global options -p, -u, and -c.  Make sure to specify them on every command you run that should be associated with that connection, since by their nature they aren't persistent (that's what P4CONFIG/env settings are for).

#9 rasonage

rasonage

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 30 August 2019 - 12:09 AM

looks like I have to write the tool in one of the latter ways you mentioned, though I was having some trouble with running "p4 clients." It would only list one of two things.. clients of the user's current connection, or all known clients EXCEPT other client connections of the current user (that would still only list the current connected client, which as before... I need all of the clients of the current user). and the last option is similar to one work around I was trying but come into a problem if a user is trying to do something on a connection the tool hasn't been connected to before so it couldn't learn of said connection.

Thanks for your time.

#10 Sambwise

Sambwise

    Advanced Member

  • Members
  • PipPipPip
  • 928 posts

Posted 30 August 2019 - 03:00 PM

Quote

I was having some trouble with running "p4 clients." It would only list one of two things.. clients of the user's current connection, or all known clients EXCEPT other client connections of the current user (that would still only list the current connected client, which as before... I need all of the clients of the current user)

You want "p4 clients -u USER".  This does not exclude the current client:

C:\Perforce\test>p4 set P4CLIENT
P4CLIENT=Samwise-dvcs-1509687817 (config 'c:\Perforce\test\p4config.txt')

C:\Perforce\test>p4 clients -u Samwise
Client Samwise-dvcs-1509687817 2019/07/12 root c:\Perforce\test 'Created by Samwise. '
Client Sprocket-COMPY386 2019/07/25 root C:\Perforce\test\sprocket 'Created by Samwise. '
Client Widget-COMPY386 2019/07/24 root c:\Perforce\widget 'Created by Samwise. '

Keep in mind that this is returning clients on that server -- it depends on you already knowing the correct port and the user.  If you don't already know what port to connect to, you should be using the P4CONFIG method I mentioned earlier to try to discover it (and then you'll get the user and client for free).  I still don't have a good mental picture of what this tool is doing (I don't even know what information you're able to pass to it) so it's hard to be any more specific, but in general I think you want to be making use of P4CONFIG rather than grasping around and trying to guess what the connection settings should be.





Also tagged with one or more of these keywords: p4, command, commands, connection

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users