Perforce 98.2 p4 User's Guide
<< Previous Chapter
Resolving File Conflicts
Table of Contents
Index
Perforce on the Web
Next Chapter >>
Changelists

Chapter 6
Perforce Basics: Miscellaneous Topics

The manual thus far has provided an introduction to the basic functionality provided by Perforce, and subsequent chapters cover the more advanced features. In between are a host of other, smaller facilities; this chapter covers these topics. Included here is information on the following:

  • Changing your Perforce environment with the P4CONFIG environment variable;

  • Using passwords to prevent impersonation by other users;

  • Command-line flags common to all Perforce commands;

  • How to work on files while not connected to a Perforce server;

  • Refreshing the client workspace;

  • Renaming files;

  • Flags that allow form data to be read from a file;

  • Recommendations for organization of files within the depot.

Reconfiguring the Perforce Environment with $P4CONFIG


Some Perforce users have multiple client workspaces, each of which may connect to a different port. There are three ways of changing your Perforce environment on the fly:

  • Reset your environment or registry variables each time you want to move to a new workspace;

  • Use command-line flags (discussed in the next section) to override the value of the environment variables P4PORT, P4CLIENT, and P4USER;

  • Use the environment variable or registry variable P4CONFIG to point to a file containing a specification for the current Perforce environment.

The use of P4CONFIG is quite simple and elegant. P4CONFIG names a file (for example, .p4env) that is used to store variable settings. Whenever a Perforce command is executed, the current working directory and its parent directories are searched for a file with the name stored in P4CONFIG. If a file with that name is found, the values of P4PORT, P4CLIENT, etc., are read from that file. If no file of the given name is found, the current values of the Perforce environment variables are used.

Each variable setting in the file stands alone on a line and must be in the form

   P4VAR=value

Values that can be stored in the P4CONFIG file are:

P4CLIENT
P4DIFF
P4EDITOR
P4MERGE
P4PASSWD
P4PORT
P4USER

Example
Using P4CONFIG to automatically reconfigure the Perforce environment
Ed has two workspaces that he often switches between. The first workspace is elmproj; it has a client root of /usr/edk/elm, and connects to the Perforce server at ida:1818. The second workspace is called graphicwork; its client root is /usr/edk/other/graphics, and it uses the Perforce server at warhol:1666. Ed gives the P4CONFIG environment variable the value .p4settings. He creates a file called .p4settings in /usr/edk/elm; the file contains this text:

   P4CLIENT=elmprojP4PORT=ida:1818

He creates a second .p4settings file in /usr/edk/other/graphics; it contains

   P4PORT=warhol:1666
   P4CLIENT=graphicwork

He always works within the directories his files are located in; when he is anywhere beneath /usr/edk/other/graphics, his Perforce client will be graphicwork, and when he's underneath /usr/edk/elmproj, his client is elmproj.

The values found in the file specified by P4CONFIG will override any environment or registry variables that have been set. Command-line flags (discussed in the next section) will override the values found in the P4CONFIG file.

P4CONFIG automates the process of changing the Perforce environment variables; new settings take effect whenever you switch your current working directory from one client workspace directory to another. If you use multiple client workspaces, this is a very useful feature.

Perforce Passwords

By default, any user can assume the identity of any other Perforce user by setting the value of P4USER to a different username, by using the -u flag with the p4 command, or by setting the value of P4USER configuration within the file described by P4CONFIG. Although this makes it easy to perform tasks for other users when necessary, it can also lead to security problems.
! Warning !
Do not use the comment character # within a password. The password stored will be those characters up to, but not including, the comment character.
To prevent another user from impersonating you within Perforce, use the p4 user command and set the form's Password: field to a string. Only whitespace and the # character may not be used. (For security's sake, it is best to set the password from a p4 client located on the same machine as the p4d server). No one, including the user who set the password, will be able to use any p4 command without providing the password to Perforce. You can provide this password to Perforce in one of three ways:

  • set the value of the environment or registry variable P4PASSWD to the correct password;

  • use the -P password flag between p4 and the actual command when giving a Perforce command (e.g., p4 -P thisismypassword submit);

  • set the value of P4PASSWD within the file described by P4CONFIG.

For security's sake, the password should always be set while logged onto the server.

Be careful! Once a user's Perforce password has been set in the p4 user form, there is no way for that user to use Perforce if they forget their password and the value of P4PASSWD has not been properly set. If this happens, the Perforce superuser will have to reset or remove the password.

Command-Line Flags Common to All Perforce Commands


Six flags are available for use with all Perforce commands. These flags are given between the system command p4 and the command argument taken by p4. These flags are:

Flag Meaning Example
-c clientname Runs the command on the specified client. Overrides the P4CLIENT environment variable. p4 -c joe edit //depot/foo

Opens file foo for editing under client workspace joe.

-d directory

Specifies the current directory, overriding the environment variable PWD. p4 -d ~elm/src edit foo bar

Opens files foo and bar for edit; these files are found relative to ~elm/src.

-p server_addr

Gives the p4d server's listening address, overriding P4PORT. p4 -p mama:1818 clients

Reports a list of clients on the server on host mama, port 1818.

-P password Supplies a Perforce password, overriding the value of P4PASSWD. Usually used in combination with the -u user flag. p4 -u ida -P idas_pw job

Create a new job as user ida, using ida's Perforce password.

-u username

Specifies a Perforce user, overriding the P4USER environment variable. Only those commands that the specified user has access to on may be run. p4 -u bill user

Presents the p4 user form to edit the specification for user bill. The command will work without the -P flag only if bill has not set a Perforce password.

-x filename

Instructs p4 to read arguments, one per line, from the named file. See Working Detached section, below

All Perforce commands can take these flags, even commands for which these flag usages are clearly useless; e.g. p4 -u bill -d /usr/joe help. Other flags are available as well; these additional flags are command dependent. Please consult the Command Reference or use p4 help commandname to find the flags defined for each command.

Working Detached


Under normal circumstances, users work in their client workspace with a functioning network connection to a Perforce server. As they edit files, they are supposed to announce their intentions to the server with p4 edit, and the server responds by noting the edit in the depot's metadata, and by unlocking the file in the client workspace. However, it is not always possible for a network connection to be present; a method is needed for users to work entirely detached from the server

The scheme is as follows:

  • The user works on files without giving Perforce commands; instead, OS commands are given that manually change the permissions on files, and then these files are edited or deleted.

  • If the files were not edited within the client workspace, they should be copied to the client workspace when the network connection is reestablished.

  • The p4 diff reporting program is used to find all files in the workspace that have changed without Perforce's knowledge. Output from this command is used to bring the depot in sync with the client workspace

Finding Changed Files with `p4 diff'

The p4 diff reporting command is used to compare a file in the client workspace with the corresponding file in the depot. Its behavior can be modified with two flags:
Note
It is perfectly safe to use p4 edit on any file; this command gives the local file write permissions, but does not otherwise alter it.
p4 diff Variation

Meaning

p4 diff -se Tells the names of unopened files that are present on the client, but whose contents are different than the files last taken by the client with p4 sync. These files are candidates for p4 edit.

p4 diff -sd Reports the names of unopened files missing from the client. These files are candidates for p4 delete.

Using `p4 diff' to Update the Depot

The p4 diff variations described above can be used in combination with the -x flag to bring the state of the depot in sync with the changes made to the client workspace.

To open changed files for edit after working detached, use

p4 diff -se > CHANGED_FILES
p4 -x CHANGED_FILES edit

To delete files from the depot that were removed from the client workspace, use

p4 diff -sd > DEL_FILES
p4 -x DEL_FILES delete

As always, these edit and delete requests are stored in a changelist, which is not processed until the p4 submit command is given.

Refreshing files


The process of syncing a depot with a formerly-detached client workspace has a converse: it is possible for Perforce to become confused about the contents of a client workspace through the accidental use of the local OS deletion command. For example, suppose that you accidently delete a client workspace file via the UNIX rm command, and that the file is one that you wanted to keep. Even after a submit, p4 have will still list the file as being present in the workspace.

Just as the process described above will bring the depot in sync with the client workspace, p4 sync -f files can be used to bring the client workspace in sync with the files the depot thinks you have. This command is mostly a recovery tool for bringing the client workspace back into sync with the depot after accidentally removing or damaging files managed by Perforce.

Recommendations for Organizing the Depot


The default view brought up by p4 client maps the entire depot to the entire client workspace. If the client workspace is named eds_elm, the default view would look like this:

   //depot/...   //eds_elm/...

This is the easiest mapping, and can be used for the most simple Perforce depots, but mapping the entire depot to the workspace can lead to problems later on. Suppose your server currently stores files for only one project, but another project is added later: everyone who has a client workspace mapped as above will wind up receiving all the files from both projects into their workspaces. Additionally, the default view does not facilitate branch creation.

Note
The depot is divided into subdirectories simply by setting up the proper mappings within the client views.
The safest way to organize the depot, even from the start, is to create one subdirectory per project within the depot. For example, if your company is working on three projects, codenamed foo, bar, and zeus, three subtrees might be created within the depot: //depot/foo, //depot/bar, and //depot/zeus. If Joe is working on the foo project, his mapping might look like this:

   //depot/foo/...    //joe/...

And Sarah, who's working on the bar and zeus projects, might set up her client workspace as:

//depot/bar/...    //sarah/bar/...
//depot/zeus/...   //sarah/zeus/...

This sort of organization can be extended on the fly to as many projects and branches as are needed.

Another way of solving the same problem would be to have the Perforce system administrator create one depot for each project or branch. Please see Chapter 16 for details.

Renaming Files


Although Perforce doesn't have a rename command, a file can be renamed by using p4 integrate to copy the file from one location in the depot to another, deleting the file from the original location, and then submitting the changelist that includes the integrate and the delete. The process is as follows:

p4 integrate from_files to_files
p4 delete from_files
p4 submit

Reference
Perforce access levels are explained in chapter 14.
The from_file will be moved to the directory and renamed according to the to_file specifier. For example, if from_file is d1/foo and to_file is d2/bar, then foo will be moved to the d2 directory, and will be renamed bar. The from_file and to_file specifiers may include wildcards, as long as they are matched on both sides. Perforce write access is needed on all the specified files.

Reading Forms from Standard Input; Writing Forms to Standard Output


Any commands that require the user to fill in a form, such as the p4 client and p4 submit commands, can read the form from standard input with the -i flag. An example is

   p4 client -i < filename

where filename contains the field names and values expected by the form. Similarly, the -o flag can be used to write a form specification to standard output.

The commands that display forms and can therefore use these flags are:

p4 branch p4 change

p4 client p4 job

p4 label p4 protect

p4 submit* p4 user

* p4 submit can take the -i flag, but not the -o flag.


Perforce 98.2 p4 User's Guide
<< Previous Chapter
Resolving File Conflicts
Table of Contents
Index
Perforce on the Web
Next Chapter >>
Changelists
Please send comments and questions about this manual to [email protected].
Copyright 1997, 1998 Perforce Software. All rights reserved.
Last updated: 08/10/98