Syncing Two Labels

Info & Tags

Article #:
29
Created:
04/16/07
Modified:
10/27/08
Tags:
labels, labelsync, multiple labels, sync

Related Articles

PROBLEM

How do I sync to two labels at once?


SOLUTION

In Perforce a label is a list of filenames and revisions. The semantics of p4 sync @labelname is different than the semantics of "checkout based on a label" in other SCM systems. Because p4 sync operates on the entire client view unless a filepattern is given on the command, p4 sync @labelname syncs the entire client workspace with the label, including deleting files in the workspace which are not listed in the label.

Thus if you have two labels, and you sync your entire client workspace to each label in succession, you do not end up with the union of the two. You end up synced to the second label. However, by specifying a filepattern on the p4 sync command you can limit the files affected. For example:

p4 sync //depot/projectA/...@label

only affects files in the //depot/projectA.

Note that if you want to sync to the files in a label without removing any files from your client workspace, use the @label,label syntax. For example:

p4 sync @label,label

The above command syncs only the files specified in the label- and does not touch the rest of your client files.

Again, if you p4 sync using the @label syntax (as opposed to @label,label), files in your workspace which are not listed in the label are removed.

The removal of files not in the label is a consistent behavior, but might cause difficulties for those used to other SCM systems. The typical scenario which motivates this question is the following:

  • users working on project A label their files (say A42)
  • users working on project B label their files (say B42)
  • the "build process" requires the files specified by A42 and B42

If project A and project B occupy separate directories in the depot, you can sync to both labels at once with:

p4 sync //depot/projectA/...@A42 //depot/projectB/...@B42

If, however, the directory organization of project A and project B does not permit this, you can create a label which is the union of labels A42 and B42;

p4 labelsync -l both42 @A42
p4 labelsync -a -l both42 @B42

(The -a (add) flag was added to p4 label in Release 97.3.)

Now you can sync to labelled files in both projects with:

p4 sync @both42

An alternative to creating a combined label is using the @label,label syntax mentioned above. In the @label,label scenario you can (optionally) empty your client workspace, then sync to each label in turn. For example:

p4 sync #none
p4 sync @A42,A42
p4 sync @B42,B42

Be aware that, whenever you are syncing files -- to a label or any other revision specifier -- files opened for edit are not touched.

Any discussion of labels and Perforce would be incomplete without mentioning the label's relationship with atomic changes. Labels are used in most SCM systems because those systems do not support atomic changes; thus labelling revisions is the only way to denote a snapshot of a particular cross section of code. In Perforce you do not really need to use labels unless you are denoting an inconsistent cross-section; say, most files from Tuesday, but this particular file from Friday. All the labels specified above with "@labelname" can be replaced with change numbers.