Skip to contentSkip to conversation

git-p4: Improve branch support

PATCH Superseded Vitor Antunes 1 participant 4 messages Jun 15, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover git-p4: Improve branch support

In this second submission I've split the patch into 3 different commits, one for each individual update. I also added a description of the newly added config options to git-p4.txt.

Vitor Antunes (3):
git-p4: Correct branch base depot path detection
git-p4: Allow filtering Perforce branches by user
git-p4: Allow branch definition with git config

 contrib/fast-import/git-p4     |   40 ++++++++++++++++++++++++++++++++++++----
 contrib/fast-import/git-p4.txt |   13 +++++++++++++
 2 files changed, 49 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH v2 1/3 git-p4: Correct branch base depot path detection

When branch detection is enabled each branch is named in git after their relative depot path in Perforce. To do this the depot paths are compared against each other to find their common base path. The current algorithm makes this comparison on a character by character basis.
Assuming we have the following branches:

//depot/branches/featureA
//depot/branches/featureB

Then the base depot path would be //depot/branches/feature, which is an invalid depot path.
The current patch fixes this by splitting the path into a list and comparing the list entries, making it choose correctly //depot/branches as the base path.

---
 contrib/fast-import/git-p4 |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
contrib/fast-import/git-p4 +5 -3
index a92beb6..fad3371 100755
@@ -1599,12 +1599,14 @@ class P4Sync(Command):
                     else:
                         paths = []
                         for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
-                            for i in range(0, min(len(cur), len(prev))):
-                                if cur[i] <> prev[i]:
+                            prev_list = prev.split("/")
+                            cur_list = cur.split("/")
+                            for i in range(0, min(len(cur_list), len(prev_list))):
+                                if cur_list[i] <> prev_list[i]:
                                     i = i - 1
                                     break
 
-                            paths.append (cur[:i + 1])
+                            paths.append ("/".join(cur_list[:i + 1]))
 
                         self.previousDepotPaths = paths
 
Signature · 1 line
1.7.4.1
#2 lore raw

PATCH v2 2/3 git-p4: Allow filtering Perforce branches by user

In reply to Vitor Antunes

All branches in the Perforce server are downloaded to allow branch detection. If you have a centralized server on a remote location and there is a big number of branches this operation can take some time.
This patch adds the configuration option git-p4.branchUser to allow filtering the branch list by user. Although this limits the branch maintenance in Perforce to be done by a single user, it might be an advantage when the number of branches being used in a specific depot is very small when compared with the branches available in the server.

---
 contrib/fast-import/git-p4     |    8 +++++++-
 contrib/fast-import/git-p4.txt |    6 ++++++
 2 files changed, 13 insertions(+), 1 deletions(-)
contrib/fast-import/git-p4 +7 -1
index fad3371..646afc1 100755
@@ -1245,7 +1245,13 @@ class P4Sync(Command):
     def getBranchMapping(self):
         lostAndFoundBranches = set()
 
-        for info in p4CmdList("branches"):
+        user = gitConfig("git-p4.branchUser")
+        if len(user) > 0:
+            command = "branches -u %s" % user
+        else:
+            command = "branches"
+
+        for info in p4CmdList(command):
             details = p4Cmd("branch -o %s" % info["branch"])
             viewIdx = 0
             while details.has_key("View%s" % viewIdx):
contrib/fast-import/git-p4.txt +6 -0
index e09da44..5b85081 100644
@@ -196,6 +196,12 @@ able to find the relevant client.  This client spec will be used to
 both filter the files cloned by git and set the directory layout as
 specified in the client (this implies --keep-path style semantics).
 
+git-p4.branchUser
+
+Only use branch specifications defined by the selected username.
+
+  git config [--global] git-p4.branchUser username
+
 Implementation Details...
 =========================
 
Signature · 1 line
1.7.4.1
#3 lore raw

PATCH v2 3/3 git-p4: Allow branch definition with git config

In reply to Vitor Antunes

Perforce does not strictly require the usage of branch specifications to create branches. In these cases the branch detection code of git-p4 will not be able to import them.
This patch adds support for git-p4.branchList configuration option, allowing branches to be defined in git config.

---
 contrib/fast-import/git-p4     |   24 ++++++++++++++++++++++++
 contrib/fast-import/git-p4.txt |    7 +++++++
 2 files changed, 31 insertions(+), 0 deletions(-)
contrib/fast-import/git-p4 +24 -0
index 646afc1..3ad9600 100755
@@ -338,6 +338,11 @@ def gitConfig(key):
         _gitConfig[key] = read_pipe("git config %s" % key, ignore_error=True).strip()
     return _gitConfig[key]
 
+def gitConfigList(key):
+    if not _gitConfig.has_key(key):
+        _gitConfig[key] = read_pipe("git config --get-all %s" % key, ignore_error=True).strip().split(os.linesep)
+    return _gitConfig[key]
+
 def p4BranchesInGit(branchesAreInRemotes = True):
     branches = {}
 
@@ -1280,6 +1285,25 @@ class P4Sync(Command):
                     if source not in self.knownBranches:
                         lostAndFoundBranches.add(source)
 
+        # Perforce does not strictly require branches to be defined, so we also
+        # check git config for a branch list.
+        #
+        # Example of branch definition in git config file:
+        # [git-p4]
+        #   branchList=main:branchA
+        #   branchList=main:branchB
+        #   branchList=branchA:branchC
+        configBranches = gitConfigList("git-p4.branchList")
+        for branch in configBranches:
+            if branch:
+                (source, destination) = branch.split(":")
+                self.knownBranches[destination] = source
+
+                lostAndFoundBranches.discard(destination)
+
+                if source not in self.knownBranches:
+                    lostAndFoundBranches.add(source)
+
 
         for branch in lostAndFoundBranches:
             self.knownBranches[branch] = branch
contrib/fast-import/git-p4.txt +7 -0
index 5b85081..3cc2309 100644
@@ -202,6 +202,13 @@ Only use branch specifications defined by the selected username.
 
   git config [--global] git-p4.branchUser username
 
+git-p4.branchList
+
+List of branches to be imported when branch detection is enabled.
+
+  git config [--global] git-p4.branchList main:branchA
+  git config [--global] --add git-p4.branchList main:branchB
+
 Implementation Details...
 =========================
 
Signature · 1 line
1.7.4.1