Skip to content

Commit ab08517

Browse files
committed
Merge branch 'va/git-p4-branch'
* va/git-p4-branch: t9801: do not overuse test_must_fail git-p4: Change p4 command invocation git-p4: Add test case for complex branch import git-p4: Search for parent commit on branch creation
2 parents 412a79f + e7d7a56 commit ab08517

File tree

2 files changed

+133
-9
lines changed

2 files changed

+133
-9
lines changed

contrib/fast-import/git-p4

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,8 @@ class P4Sync(Command, P4UserMap):
14301430
self.cloneExclude = []
14311431
self.useClientSpec = False
14321432
self.clientSpecDirs = None
1433+
self.tempBranches = []
1434+
self.tempBranchLocation = "git-p4-tmp"
14331435

14341436
if gitConfig("git-p4.syncFromOrigin") == "false":
14351437
self.syncWithOrigin = False
@@ -1451,6 +1453,14 @@ class P4Sync(Command, P4UserMap):
14511453
.replace("%25", "%")
14521454
return path
14531455

1456+
# Force a checkpoint in fast-import and wait for it to finish
1457+
def checkpoint(self):
1458+
self.gitStream.write("checkpoint\n\n")
1459+
self.gitStream.write("progress checkpoint\n\n")
1460+
out = self.gitOutput.readline()
1461+
if self.verbose:
1462+
print "checkpoint finished: " + out
1463+
14541464
def extractFilesFromCommit(self, commit):
14551465
self.cloneExclude = [re.sub(r"\.\.\.$", "", path)
14561466
for path in self.cloneExclude]
@@ -1958,10 +1968,24 @@ class P4Sync(Command, P4UserMap):
19581968
self.importChanges(changes)
19591969
return True
19601970

1971+
def searchParent(self, parent, branch, target):
1972+
parentFound = False
1973+
for blob in read_pipe_lines(["git", "rev-list", "--reverse", "--no-merges", parent]):
1974+
blob = blob.strip()
1975+
if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
1976+
parentFound = True
1977+
if self.verbose:
1978+
print "Found parent of %s in commit %s" % (branch, blob)
1979+
break
1980+
if parentFound:
1981+
return blob
1982+
else:
1983+
return None
1984+
19611985
def importChanges(self, changes):
19621986
cnt = 1
19631987
for change in changes:
1964-
description = p4Cmd("describe %s" % change)
1988+
description = p4Cmd(["describe", str(change)])
19651989
self.updateOptionDict(description)
19661990

19671991
if not self.silent:
@@ -2014,7 +2038,21 @@ class P4Sync(Command, P4UserMap):
20142038
parent = self.initialParents[branch]
20152039
del self.initialParents[branch]
20162040

2017-
self.commit(description, filesForCommit, branch, [branchPrefix], parent)
2041+
blob = None
2042+
if len(parent) > 0:
2043+
tempBranch = os.path.join(self.tempBranchLocation, "%d" % (change))
2044+
if self.verbose:
2045+
print "Creating temporary branch: " + tempBranch
2046+
self.commit(description, filesForCommit, tempBranch, [branchPrefix])
2047+
self.tempBranches.append(tempBranch)
2048+
self.checkpoint()
2049+
blob = self.searchParent(parent, branch, tempBranch)
2050+
if blob:
2051+
self.commit(description, filesForCommit, branch, [branchPrefix], blob)
2052+
else:
2053+
if self.verbose:
2054+
print "Parent of %s not found. Committing into head of %s" % (branch, parent)
2055+
self.commit(description, filesForCommit, branch, [branchPrefix], parent)
20182056
else:
20192057
files = self.extractFilesFromCommit(description)
20202058
self.commit(description, files, self.branch, self.depotPaths,
@@ -2349,6 +2387,12 @@ class P4Sync(Command, P4UserMap):
23492387
self.gitOutput.close()
23502388
self.gitError.close()
23512389

2390+
# Cleanup temporary branches created during import
2391+
if self.tempBranches != []:
2392+
for branch in self.tempBranches:
2393+
read_pipe("git update-ref -d %s" % branch)
2394+
os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
2395+
23522396
return True
23532397

23542398
class P4Rebase(Command):

t/t9801-git-p4-branch.sh

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ test_expect_success 'add simple p4 branches' '
172172
echo file1 >file1 &&
173173
echo file2 >file2 &&
174174
p4 add file1 file2 &&
175-
p4 submit -d "branch1" &&
175+
p4 submit -d "Create branch1" &&
176176
p4 integrate //depot/branch1/... //depot/branch2/... &&
177-
p4 submit -d "branch2" &&
177+
p4 submit -d "Integrate branch2 from branch1" &&
178178
echo file3 >file3 &&
179179
p4 add file3 &&
180180
p4 submit -d "add file3 in branch1" &&
181181
p4 open file2 &&
182182
echo update >>file2 &&
183183
p4 submit -d "update file2 in branch1" &&
184184
p4 integrate //depot/branch1/... //depot/branch3/... &&
185-
p4 submit -d "branch3"
185+
p4 submit -d "Integrate branch3 from branch1"
186186
)
187187
'
188188

@@ -203,17 +203,17 @@ test_expect_success 'git-p4 clone simple branches' '
203203
test -f file1 &&
204204
test -f file2 &&
205205
test -f file3 &&
206-
grep -q update file2 &&
206+
grep update file2 &&
207207
git reset --hard p4/depot/branch2 &&
208208
test -f file1 &&
209209
test -f file2 &&
210210
test ! -f file3 &&
211-
test_must_fail grep -q update file2 &&
211+
! grep update file2 &&
212212
git reset --hard p4/depot/branch3 &&
213213
test -f file1 &&
214214
test -f file2 &&
215215
test -f file3 &&
216-
grep -q update file2 &&
216+
grep update file2 &&
217217
cd "$cli" &&
218218
cd branch1 &&
219219
p4 edit file2 &&
@@ -222,7 +222,87 @@ test_expect_success 'git-p4 clone simple branches' '
222222
cd "$git" &&
223223
git reset --hard p4/depot/branch1 &&
224224
"$GITP4" rebase &&
225-
grep -q file2_ file2
225+
grep file2_ file2
226+
)
227+
'
228+
229+
# Create a complex branch structure in P4 depot to check if they are correctly
230+
# cloned. The branches are created from older changelists to check if git-p4 is
231+
# able to correctly detect them.
232+
# The final expected structure is:
233+
# `branch1
234+
# | `- file1
235+
# | `- file2 (updated)
236+
# | `- file3
237+
# `branch2
238+
# | `- file1
239+
# | `- file2
240+
# `branch3
241+
# | `- file1
242+
# | `- file2 (updated)
243+
# | `- file3
244+
# `branch4
245+
# | `- file1
246+
# | `- file2
247+
# `branch5
248+
# `- file1
249+
# `- file2
250+
# `- file3
251+
test_expect_success 'git-p4 add complex branches' '
252+
test_when_finished cleanup_git &&
253+
test_create_repo "$git" &&
254+
(
255+
cd "$cli" &&
256+
changelist=$(p4 changes -m1 //depot/... | cut -d" " -f2) &&
257+
changelist=$(($changelist - 5)) &&
258+
p4 integrate //depot/branch1/...@$changelist //depot/branch4/... &&
259+
p4 submit -d "Integrate branch4 from branch1@${changelist}" &&
260+
changelist=$(($changelist + 2)) &&
261+
p4 integrate //depot/branch1/...@$changelist //depot/branch5/... &&
262+
p4 submit -d "Integrate branch5 from branch1@${changelist}"
263+
)
264+
'
265+
266+
# Configure branches through git-config and clone them. git-p4 will only be able
267+
# to clone the original structure if it is able to detect the origin changelist
268+
# of each branch.
269+
test_expect_success 'git-p4 clone complex branches' '
270+
test_when_finished cleanup_git &&
271+
test_create_repo "$git" &&
272+
(
273+
cd "$git" &&
274+
git config git-p4.branchList branch1:branch2 &&
275+
git config --add git-p4.branchList branch1:branch3 &&
276+
git config --add git-p4.branchList branch1:branch4 &&
277+
git config --add git-p4.branchList branch1:branch5 &&
278+
"$GITP4" clone --dest=. --detect-branches //depot@all &&
279+
git log --all --graph --decorate --stat &&
280+
git reset --hard p4/depot/branch1 &&
281+
test_path_is_file file1 &&
282+
test_path_is_file file2 &&
283+
test_path_is_file file3 &&
284+
grep update file2 &&
285+
git reset --hard p4/depot/branch2 &&
286+
test_path_is_file file1 &&
287+
test_path_is_file file2 &&
288+
test_path_is_missing file3 &&
289+
! grep update file2 &&
290+
git reset --hard p4/depot/branch3 &&
291+
test_path_is_file file1 &&
292+
test_path_is_file file2 &&
293+
test_path_is_file file3 &&
294+
grep update file2 &&
295+
git reset --hard p4/depot/branch4 &&
296+
test_path_is_file file1 &&
297+
test_path_is_file file2 &&
298+
test_path_is_missing file3 &&
299+
! grep update file2 &&
300+
git reset --hard p4/depot/branch5 &&
301+
test_path_is_file file1 &&
302+
test_path_is_file file2 &&
303+
test_path_is_file file3 &&
304+
! grep update file2 &&
305+
test_path_is_missing .git/git-p4-tmp
226306
)
227307
'
228308

0 commit comments

Comments
 (0)