2

TLDR

How does one run a git p4 submit on a bare repo? Which command line option to git p4 submit is that?

LONG STORY

I would like my team within my company, which uses perforce, to move to git. I want to use Git-P4 to achieve that. I want to have a section of perforce cloned to a git repo, and make that a remote repo, so that people would clone that, push changes to the remote repo and I would periodically resubmit the changes made in the remote repo back to the perforce. So I followed this tutorial

http://answers.perforce.com/articles/KB_Article/Git-P4

which boiled down to this command:

git p4 clone //depot/path.to/folder@all folder --bare

That works, then on my client machine I do

git clone "user1@server:/home/user1/path/to/folder"

and that's fine and it shows up so I make an edit to a test file, then do the

git add file
git commit -m 'test'
git push.

and that works, but then back on the remote repo now in the folder with the repo, I do,

git p4 rebase

and I get this

git p4 rebase
Performing incremental import into refs/remotes/p4/master git branch
Depot paths: //depot/path.to/folder/
No changes to import!
fatal: This operation must be run in a work tree
Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up-to-date or stash away all your changes with git stash.

and

git p4 submit

gives me this:

Perforce checkout for depot path //depot/path.to/folder/ located at /home/user1/path/to/perforce.folder/
Synchronizing p4 checkout...
... - file(s) up-to-date.
fatal: Not a git repository: '.'
Command failed: ['git', 'rev-list', '--no-merges', 'remotes/p4/master..master']

So the problem is that the the git repo can't be a bare repo when I do a submit, it has to be a regular one with a working directory, right? But if that's the case, why even give me a --bare option? The reason I'm doing the --bare option is that when I left it out I got a different set of errors about uncommitted changes in the working copy of the remote repo. But how do I make the git p4 submit without any errors?

TinyGrasshopper
  • 2,190
  • 2
  • 17
  • 28
  • For reference here's what happened when i left out the --bare option. http://stackoverflow.com/questions/15512700/replace-working-copy-of-git-repo-with-actual-contents-of-repo-in-git/ – TinyGrasshopper Mar 20 '13 at 05:36

2 Answers2

3

My understanding of git p4 is quite limited, but in theory this should work.

To use git p4 in a way such that both your bare git repo and the perforce repos are synchronized, you would need to maintain at least 3 different types of repositories:

  • BareGitRepo - A bare repo that you've created. I would suggest not to use any git p4 commands in this repo directly. Just a regular bare git repo should be sufficient. All developers using git should be cloning this repository directly and pushing changes only into this repo as remote.
  • GitP4Repo A non-bare repo cloned from BareGitRepo, but to sync up with perforce. This is where you run all the git p4 commands.
  • P4Workspace - A perforce client workspace using which you can submit changes from the git repo to perforce.

In the GitP4Repo:

  • Setup the repo initially using git p4 clone <PERFORCE_FILE_PATH>
  • Sync the changes from BareGitRepo regularly. (Something like git fetch origin && git rebase origin/master master)
  • Rebase changes from p4 regularly using git p4 rebase. (Which in essence does git p4 sync followed by a git rebase p4/master)
  • Submit the changes to perforce, using git p4 submit. This is where you would need the P4Workspace mapped in your client workspace.
  • Push the changes merged from perforce, back to BareGitRepo using git push origin master

Gotchas I can think of:

  • These steps need to be run periodically (depending on how frequent you want the perforce commits to be synchronized with your git repo).
  • The git commit SHA1s will possibly change when you run a git p4 rebase since it turn invokes git rebase to overlay the commits from your git repo on top of the newer perforce commits. I'm not sure if there is an option to use git merge instead, and preserve the SHA1 hashes of the git commits.

Finally to answer your question, why git p4 clone has a --bare option, I can only imagine that git-p4's clone command tries to be similar to the regular git clone command.

git clone also has a --bare option which is used to create a bare repository. And no, you cannot run a git pull or git checkout or git rebase on such a repository either.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • Should the BareGitRepo be created from scratch or cloned from the GitP4Repo, because if it should be cloned, I tried that and it didn't work. (can't recall the error off-hand) If it should be made from scratch, where would it get the files from? Just copied from another non-bare repo synced to the bare repo? – TinyGrasshopper May 08 '13 at 14:30
  • Have you made any progress on that setup? I am currently also trying to setup the same scenario but still struggling with it. – Martin Oct 17 '14 at 09:48
  • @Martin - I actually did not get to trying this setup in my previous organization. Instead I actually now moved to an organization which uses `git` as the primary setup :) – Tuxdude Oct 17 '14 at 18:05
1

You can't submit from a bare repo. A bare repo is only for cloning from and pushing changes to. If you want to submit you will need to clone the bare repo in another location.

Why would you want a bare repo then? Well, it is used when you are hosting a git project that has a simple master repository that everyone pushes to. Usually the bare repo lives on a server that is not directly used by anyone. Keeping a 'bare' repo allows git to keep all of the data compressed as efficiently as possible. In many cases you might see 10:1 compression ratio compared to a single revision of a checked out repo.

cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • That's doesn't seem possible. If I clone a bare repo from perforce, I'll only ever be able to git p4 submit from the repo that I git p4 cloned from, right? If I clone a new non-bare repo from the bare repo, it won't have any of the perforce information that will let me git p4 submit, will it? Where does that information reside? – TinyGrasshopper May 08 '13 at 14:24
  • No, you can git-p4 submit from any copy of the cloned repo created using git-p4. The perforce information is store in all of the commit comments in the repo. You are somewhat correct though ... for git p4 submit to work on a cloned client, the 'p4 client' must be defined to match the one used during the git-p4 clone. There are some gotchas, like username mapping that could cause problems though. – cmcginty May 10 '13 at 09:19