bobster
bobster

Reputation: 41

Looking for a way to sync Perforce <-> Git

Our team is looking to migrate our perforce server over to Git. Is there a way to sync check-ins from a branch in our Github Server back to Perforce to keep them in sync? I have been looking at git-p4 and there seems to be lots of documentation of how to sync Perforce -> Git but not the other way around. I would ideally like to have it syncing both ways perforce <-> git, is that possible with p4-git?

Upvotes: 4

Views: 4540

Answers (5)

Luke
Luke

Reputation: 609

For most of the time, merge conflicts were sorted out automatically. Perforce was the "master". People submitting from Perforce always had their change go straight through. People submitting from git would have their change submitted via this process:

  1. lock master git repo
  2. fetch upstream p4 changes
  3. rebase against upstream p4
  4. submit to p4 if that all went ok

That still left a very small window between (3) and (4) where someone in Perforce land could submit a conflicting change to the same files. But in practice that only happened a couple of times, over the course of several years of probably hundreds of commits per week.

In those two cases I went in and manually patched things up which was a bit clunky. I think you could just automatically discard the problematic change quite easily, but in the end, switching to git entirely made this problem go away.

Upvotes: 2

pgpb.padilla
pgpb.padilla

Reputation: 2418

I have faced this problem myself and as far as I know there's nothing of the kind.

In my case I have to basically:

  • Create a patch for the changes in Git: git diff sha1..sha2 > mypatch.diff
  • Make a list of the affected files: git diff --name-only sha1..sha2 > files.list
  • Apply the patch in the P4 repo: git apply mypatch.diff
  • Reconcile changes in P4: for each file in files.list; p4 reconcile $file; done
  • Edit your P4 change list and then submit it (I'm sure this can be automated too but I haven't had the need for it).

I have a couple of scripts that help me with this, you can find them here: https://github.com/pgpbpadilla/git-p4-helpers#sharing-changes-git-p4

I have been using this workflow for ~6mo and it works for most cases.

Caveats

  • This creates a single change lists for all commits in the sha range (a..b).

Upvotes: 1

Luke
Luke

Reputation: 609

In the past I setup something like this. I had a gitolite repo and a p4 server. Changes pushed to the gitolite repo would be picked up by a cron job and turned into P4 commits (via git-p4 submit). Similarly, changes submitted to p4 would be picked up by the same cron job and synced back to git (via git p4 rebase).

There were two git repos involved: the gitolite repo that normal git developers committed to, and a separate git-p4 based repo where the git-p4 operations took place.

I had a fairly small shell script to co-ordinate everything. The main tricky areas were:

  • locking: you have to forcibly rebase the gitolite repo so you need some way to ensure developers don't lose changes when this happens (I used a lock file).

  • Merge conflicts. Very occasionally two people would edit the same section of a file in P4 and git at the same time.

Upvotes: 1

cmcginty
cmcginty

Reputation: 117018

Perforce GitFusion can do this, but the developer would have to push changes to the GitFusion server instead of the github server.

Upvotes: 2

Vitor
Vitor

Reputation: 1976

Git-p4 is designed such that the git repository is initialized with data imported from Perforce. After this initial import bidirectional communication between git and Perforce repositories is fully supported, with the exception of branches/merges that have limited support.

To import updates from Perforce to git:

git p4 sync

To submit changes from git to Perforce:

git p4 submit

For more details regarding git-p4 configuration please consult its documentation.

Update: I would always advise to test any flows in temporary repositories before deploying.

Upvotes: 1

Related Questions