Setting up git-p4

| Git | perforce

I couldn’t find a good article on setting up git-p4, and for some reason most of the articles out there point to dead URLs. So I figured I’d quickly write this up.

Git-p4 is a tool for using git on top of a perforce server. This is great if your team wants to work with a DVCS, but your company requires a centralised VCS.

There were multiple github projects 1, 2, 3, that had forks of the git-p4 script, however, I found the latest mixed in with the official git source code. So download it from here. Tip: Don’t forget to “chmod +x git-p4.py” once it’s downloaded.

Next, I wanted to be able to type “git p4″, so I used an alias to do this. Edit/create the file ~/.gitconfig and add:

[alias]
p4 = !~/bin/git-p4.py

Once that’s done you should be able to:

# Fetch only the head
git p4 clone //depot/path/to/project/ project

# or Fetch all revisions (takes a little longer)
git p4 clone //depot/path/to/project/@all project

If that returns an error, such as “p4 returned an error: Perforce password (P4PASSWD) invalid or unset.”, then you most likely don’t have your p4 command line set up. To set mine up, I added the following to my .bashrc

export P4CLIENT=myclient
export P4HOST=perforce:1666
export P4USER=myuser
export P4EDITOR=nano

and then logged in

p4 login

More information on that set up can be found in the official documentation.

Once cloned, you can change to the project directory and use many of the normal git commands, such as “git log”. You may notice that your newly checked out git repo only has one commit. If you want to see a full history use @all on the end of your depot path.