9

I did:

git p4 clone //depot/path/to/project/trunk/@all project

to create the master branch of project. Now I want to clone //depot/path/to/project/release to the release branch of project. How is that done?

UPDATE: Using --detect-branches doesn't work, either. It reports that it's updating two branches (when there are really three branches) but git branch reports only master exists.

Noel Yap
  • 18,822
  • 21
  • 92
  • 144

4 Answers4

11

Here are my setup notes from when I was using git-p4. It might be helpful:

  • Download the p4 linux client. Store the file in ~/bin or /usr/local/bin and chmod +x

  • Setup git-p4 as root

    chmod 755 /usr/share/doc/git/contrib/fast-import/git-p4
    ln -s /usr/share/doc/git/contrib/fast-import/git-p4 /usr/local/bin
    
  • Define Git globals for git-p4

    git config --global alias.p4 '!git-p4'
    git config --global git-p4.detectRenames true
    git config --global git-p4.detectCopies true
    
  • Set defines for direct 'p4' usage

    export P4PORT=SERVER_NAME:PORT_NUMBER
    
  • Set login credentials

    export P4USER=USER_NAME
    export P4PASSWD=PASSWORD
    
  • Select Perforce branches using P4 'client'

    Run the 'p4 client' command, and add only the paths/branches you are interested in. If you want to name the client work space you can add an optional CLIENT_NAME argument to the end of the command. This will allow you to use different client definitions on the same machine.

    $ p4 client [CLIENT_NAME]
    
    View:
    //depot/main... //CLIENT_NAME/main...
    //depot/patch... //CLIENT_NAME/patch...
    //depot/dev... //CLIENT_NAME/dev...
    
  • Clone the repository

    • Simple import

      git p4 clone --use-client-spec --detect-branches //depot@all GIT_DIR
      
    • Advanced import

      git init PROJ; cd PROJ/
      git config git-p4.branchList main:patch
      git config --add git-p4.branchList main:dev
      git p4 clone --use-client-spec //depot@all .
      
  • Submit Changes Back to Perforce

    In order to submit changes to Perforce, it requires a client workspace, separate from the git working tree. It is recommend that the workspace is on the same file system your Perforce git working directory.

    Additionally, a reference to the workspace path is stored on the Perforce server, and will be used during the p4 submit command.

    The first step is to create the local client workspace. CLIENT_NAME is an optional argument. If you do not define it, p4 will use your host name.

    p4 client [CLIENT_NAME]
    

    You will be moved to a file editor before completing the p4 command. This lets you change any of the client settings before they are sent to the server. You must change the Root value to a new directory outside of your git tree (e.g. ../p4-working) Also, verify the Owner and Client values before exiting. These values are taking from your environment, and can not be changed in the editor.

    p4 clients | grep USERNAME
    

    If you did not use the default client name, it must be defined in your local git config:

    git config git-p4.client CLIENT_NAME
    

    When you are ready to push your code changes, use the commands:

    git p4 rebase
    git p4 submit
    

    You can remove clients from the sever when no longer in use:

    p4 client -d CLIENT_NAME
    
cmcginty
  • 113,384
  • 42
  • 163
  • 163
1

With Git 2.30 (Q1 2021), "git(man) p4" now honors init.defaultBranch configuration.

That could be an alternative solution to detect-branches or --branch:

See commit 1b09d19 (08 Nov 2020) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit ca8870d, 11 Nov 2020)

p4: respect init.defaultBranch

Signed-off-by: Johannes Schindelin

In git p4 clone, we hard-code the branch name master instead of looking what the actual initial branch name is.
Let's fix that.

Furthermore:

With Git 2.37 (Q3 2022), git p4 sync --branch is more complete:

See commit 17f273f (04 Apr 2022) by Tao Klerks (TaoK).
(Merged by Junio C Hamano -- gitster -- in commit af3a320, 20 May 2022)

git-p4: support explicit sync of arbitrary existing git-p4 refs

Signed-off-by: Tao Klerks

With the --branch argument of the sync subcommand, git-p4 enables you to import a perforce branch/path to an arbitrary Git ref, using a full ref path, or to refs/remotes/p4/* or refs/heads/p4/*, depending on --import-local, using a short ref name.

However, when you later want to explicitly sync such a given ref to pick up subsequent p4 changes, it only works if the ref was placed in the p4 path and has only one path component (no "/").

This limitation results from a bad assumption in the existing-branch sync logic, and also means you cannot individually sync branches detected by --detect-branches, as these also get a "/" in their names.

Fix git p4 sync --branch, when called with an existing ref, so that it works correctly regardless of whether the ref is in the p4 path or not, and (in the case of refs in the p4 path) regardless of whether it has a "/" in its short name or not.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

I believe "detect branches" relies on you having a branch mapping with the same name as the branch path in your depot. If that is not the case, you probably need to use the suggested method of defining the git-p4.branchList config value in an empty git repo before running the git-p4 clone command.

cmcginty
  • 113,384
  • 42
  • 163
  • 163
  • Ah, this makes sense. I was wondering how the branch detection 'magic' worked. I'll give this a try. Thanks. – Noel Yap Mar 11 '13 at 16:59
  • This is still not working for me. I suspect merges have gone in both directions on at least some of our branches and it's throwing a wrench into the works of git-p4. – Noel Yap Mar 13 '13 at 18:22
  • It's worked for me in the past on a very large repo, with someone of most confusing branches/merging I've ever seen. Since the git-p4 is written in python, its actually not that hard to debug. – cmcginty Mar 15 '13 at 19:43
0

Horribly late answer, but there's so little documentation on git-p4 that I hope it's still useful.

I also struggled to add branches to an existing repository, and never got the detect-branches magic to work with branch mappings.

I had a little more success doing it in stages manually, explicitly telling git-p4 which path to import and using the --branch option. I believe I also manually created the release branch in the existing repository before running that.

For this question, that would be git p4 sync //depot/path/to/project/release --branch=release

I believe this would work using clone too, as clone is mostly just sync behind the scenes in the git-p4 code, but once you already have a repository, I think sync would be safer.

ojchase
  • 1,041
  • 1
  • 10
  • 22