March 24, 2015

Attack of the Clones

Media & Entertainment

I previously described how easy it is to get started with a local repository and branch to your heart’s content, but you’re not always going to be starting new projects or working by yourself. So today’s post covers getting started with a shared server instead, which takes only a single command:

perforce@p4demo:~$ p4 -d talkhouse1 clone -p 1666 -f 
//depot/Talkhouse/main-dev/...
Server sysop-dvcs-1426117249 saved.
Cloning from '1666'...
fetch load revisions 351 finishing
fetch load integrations 687 finishing
check files 100% finishing
fetch archives 100% finishing
commit revisions 100% finishing
sync 324 finishing
23 change(s) containing a total of 351 file revision(s) were
successfully fetched.

There are a couple of things worth noting. First, I’ve cloned from a file specification (the –f argument). This provides flexibility because, unlike any other Distributed Version Control System (DVCS,) you can clone any part of a repository. If you need just one folder, then clone just that folder; no more cloning everything whether you need it or not. The price of that flexibility is that you get exactly what you request as a single branch named main.

If you want to bring down multiple branches at once, however, you may clone instead from a remote specification (the -r argument), but that requires it to be defined on the server. We’ll look at remote specs later in this series; for now we’re keeping it simple. Next, let’s look at the history of a file:

perforce@p4demo:~/talkhouse1$ p4 filelog build.xml
//stream/main/build.xml
... #4 change 12272 edit on 2014/07/16 by Joe_Coder@jc_admin (text)
'Modified build file to reflect '
... #3 change 12271 edit on 2014/06/16 by Joe_Coder@jc_dev (text) 'make
build post to Swarm activi'
... #2 change 12270 edit on 2014/06/04 by Joe_Coder@jc_dev (text) 'make 
earl the tester '
... #1 change 12268 add on 2014/06/04 by Joe_Coder@jc_dev (text) 'make
Talkhouse main build '

The second thing worth noting is that we got all the revisions by default. If you need less history, however, it’s a simple matter of adding an argument:

perforce@p4demo:~$ p4 -d talkhouse2 clone -m 1 -p 1666 -f 
//depot/Talkhouse/main-dev/...
Server sysop-dvcs-1426117351 saved.
Cloning from '1666'...
fetch load revisions 325 finishing
fetch load integrations 634 finishing
check files 100% finishing
fetch archives 100% finishing
commit revisions 100% finishing
sync 324 finishing
10 change(s) containing a total of 325 file revision(s) were 
successfully fetched.

The “-m 1” argument specifies a depth of one. We can verify what we got by listing the log for the same file examined earlier:

perforce@p4demo:~/talkhouse2$ p4 filelog build.xml
//stream/main/build.xml
... #4 change 12255 edit on 2014/07/16 by Joe_Coder@jc_admin (text) 
'Modified build file to reflect ''

This is great for continuous integration, testing, and delivery systems. It reduces the load on the shared server and provides faster execution when you need only the latest and greatest.

No matter how you choose to bring down files and their history, everything is now local. I’ll make a change using classic DVCS workflow in the clone with all the revisions:

perforce@p4demo:~/talkhouse1$ vim build.xml
perforce@p4demo:~/talkhouse1$ p4 reconcile
//stream/main/build.xml#4 - opened for edit
perforce@p4demo:~/talkhouse1$ p4 submit -d "Updated description."
Submitting change 12273.
Locking 1 files ...
edit //stream/main/build.xml#5
Change 12273 submitted.

When I’m ready to send my work back to the server for the rest of the team, I issue a single command:

perforce@p4demo:~/talkhouse1$ p4 push
1 change(s) containing a total of 1 file revision(s) were successfully 
pushed.

In this case we’re dealing with a single change, but the push command will send all the work you’ve done locally. So you can commit as many times as you like, try different ideas, and share only when you’re ready. Speaking of which, what about that “shallow clone” we made earlier? How does it get the work I just committed? Again it’s easy, requiring only a single command:

perforce@p4demo:~/talkhouse2$ p4 fetch
1 change(s) containing a total of 1 file revision(s) were successfully 
fetched.

And that’s about it for the basics of working with a shared server. Stay tuned to this space for more posts about the new DVCS features of Perforce Helix.

All Perforce Version Management & Collaboration Products Are Free for 20 Users, Now With DVCS Capabilities.