Skip to contentSkip to conversation

Search for parent commit in new branch import

PATCH In review Vitor Antunes 2 participants 11 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Search for parent commit in new branch import

This is mostly a RFC/RFH (Request For Help;) regarding the code I used on this patch. I think that this is not a clean solution, but it shows the algorithm I want to implement. The worst points are:

1. Relies on a sleep() to avoid a race condition. I'm using 0.1s, but it still fails sometimes, so it needs to be bigger.
2. Requires the --force option of fast-import.

Test case is included.

Thanks in advance to everyone who contributes.

Vitor Antunes (2):
git-p4: Search for parent commit on branch creation
git-p4: Add complex test case for branch import

 contrib/fast-import/git-p4 |   22 +++++++++++-
 t/t9800-git-p4.sh          |   77 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 2 deletions(-)
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/2 git-p4: Search for parent commit on branch creation

The new commit is applied sequentially to each blob of the parent branch. The first blob which results in a zero diff with the new commit is considered the parent commit. If none is found, then the commit is applied to the top of the parent branch.
This algorithm requires a checkpoint and a time.sleep() to be able to use diff-tree, making it slower than what would be desirable. It also requires using --force argument of fast-import, leaving lots of trash commits when finished.

---
 contrib/fast-import/git-p4 |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)
contrib/fast-import/git-p4 +20 -2
index f622a38..0bced6d 100755
@@ -1671,7 +1671,25 @@ class P4Sync(Command, P4UserMap):
                             parent = self.initialParents[branch]
                             del self.initialParents[branch]
 
-                        self.commit(description, filesForCommit, branch, [branchPrefix], parent)
+                        parentFound = 0
+                        if len(parent) > 0:
+                            self.gitStream.write("checkpoint\n\n")
+                            time.sleep(0.1)
+                            for blob in read_pipe_lines("git rev-list --reverse --no-merges %s" % parent):
+                                blob = blob.strip()
+                                self.gitStream.write("reset %s\nfrom %s\n" % (branch, blob))
+                                self.commit(description, filesForCommit, branch, [branchPrefix], blob)
+                                self.gitStream.write("checkpoint\n\n")
+                                time.sleep(0.1)
+                                if len( read_pipe("git diff-tree %s %s" % (blob, branch)) ) == 0:
+                                    parentFound = 1
+                                    if self.verbose:
+                                        print "Found parent of %s in commit %s" % (branch, blob)
+                                    break
+                        if not parentFound:
+                            if self.verbose:
+                                print "Parent of %s not found. Committing into head of %s" % (branch, parent)
+                            self.commit(description, filesForCommit, branch, [branchPrefix], parent)
                 else:
                     files = self.extractFilesFromCommit(description)
                     self.commit(description, files, self.branch, self.depotPaths,
@@ -1948,7 +1966,7 @@ class P4Sync(Command, P4UserMap):
 
         self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
 
-        importProcess = subprocess.Popen(["git", "fast-import"],
+        importProcess = subprocess.Popen(["git", "fast-import", "--force"],
                                          stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                          stderr=subprocess.PIPE);
         self.gitOutput = importProcess.stdout
Signature · 1 line
1.7.5.4
#2 lore raw

PATCH 2/2 git-p4: Add complex test case for branch import

In reply to Vitor Antunes

Check if branches created from old changelists are correctly imported.

---
 t/t9800-git-p4.sh |   77 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 77 insertions(+), 0 deletions(-)
t/t9800-git-p4.sh +77 -0
index 15704d2..9f757d7 100755
@@ -454,6 +454,83 @@ test_expect_success 'git-p4 clone simple branches' '
 	rm -rf "$git" && mkdir "$git"
 '
 
+# Create a complex branch structure in P4 depot to check if they are correctly
+# cloned. The branches are created from older changelists to check if git-p4 is
+# able to correctly detect them.
+# The final expected structure is:
+# `branch1
+# | `- file1
+# | `- file2 (updated)
+# | `- file3
+# `branch2
+# | `- file1
+# | `- file2
+# `branch3
+# | `- file1
+# | `- file2 (updated)
+# | `- file3
+# `branch4
+# | `- file1
+# | `- file2
+# `branch5
+#   `- file1
+#   `- file2
+#   `- file3
+test_expect_success 'add p4 complex branches' '
+	cd "$cli" &&
+	changelist=$(p4 changes -m1 //depot/... | cut -d \  -f 2) &&
+	changelist=$(($changelist - 5)) &&
+	p4 integrate //depot/branch1/...@$changelist //depot/branch4/... &&
+	p4 submit -d "branch4" &&
+	changelist=$(($changelist + 2)) &&
+	p4 integrate //depot/branch1/...@$changelist //depot/branch5/... &&
+	p4 submit -d "branch5" &&
+	cd "$TRASH_DIRECTORY"
+'
+
+# Configure branches through git-config and clone them. git-p4 will only be able
+# to clone the original structure if it is able to detect the origin changelist
+# of each branch.
+test_expect_success 'git-p4 clone complex branches' '
+	git init "$git" &&
+	cd "$git" &&
+	git config git-p4.branchList branch1:branch2 &&
+	git config --add git-p4.branchList branch1:branch3 &&
+	git config --add git-p4.branchList branch1:branch4 &&
+	git config --add git-p4.branchList branch1:branch5 &&
+	cd "$TRASH_DIRECTORY" &&
+	"$GITP4" clone --dest="$git" --detect-branches //depot@all &&
+	cd "$git" &&
+	git log --all --graph --decorate --stat &&
+	git reset --hard p4/depot/branch1 &&
+	test -f file1 &&
+	test -f file2 &&
+	test -f file3 &&
+	grep -q update file2 &&
+	git reset --hard p4/depot/branch2 &&
+	test -f file1 &&
+	test -f file2 &&
+	test \! -f file3 &&
+	! grep -q update file2 &&
+	git reset --hard p4/depot/branch3 &&
+	test -f file1 &&
+	test -f file2 &&
+	test -f file3 &&
+	grep -q update file2 &&
+	git reset --hard p4/depot/branch4 &&
+	test -f file1 &&
+	test -f file2 &&
+	test \! -f file3 &&
+	! grep -q update file2 &&
+	git reset --hard p4/depot/branch5 &&
+	test -f file1 &&
+	test -f file2 &&
+	test -f file3 &&
+	! grep -q update file2 &&
+	cd "$TRASH_DIRECTORY" &&
+	rm -rf "$git" && mkdir "$git"
+'
+
 test_expect_success 'shutdown' '
 	pid=`pgrep -f p4d` &&
 	test -n "$pid" &&
Signature · 1 line
1.7.5.4
#3 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

[email protected] wrote on Fri, 19 Aug 2011 01:09 +0100:

Quoted 3 lines

Check if branches created from old changelists are correctly imported.

Style edits.

		-- Pete

---------------8<-------------------

Quoted 1 line

From 5d1103a97b60570423782fd3873a2b413ddb1d5b Mon Sep 17 00:00:00 2001

From: Pete Wyckoff <[email protected]>
Date: Sat, 20 Aug 2011 14:31:49 -0400
Subject: [PATCH 3/4] git-p4: complex branch test edits

A few simplifications and style edits here too.

---
 t/t9800-git-p4.sh |   21 +++++++++------------
 1 files changed, 9 insertions(+), 12 deletions(-)
t/t9800-git-p4.sh +9 -12
index 5bcb8b4..cf3a70e 100755
@@ -489,11 +489,11 @@ test_expect_success 'git-p4 clone simple branches' '
 #   `- file3
 test_expect_success 'add p4 complex branches' '
 	cd "$cli" &&
-	changelist=$(p4 changes -m1 //depot/... | cut -d \  -f 2) &&
-	changelist=$(($changelist - 5)) &&
+	changelist=$(p4 changes -m1 //depot/... | cut -d" " -f2) &&
+	changelist=$((changelist - 5)) &&
 	p4 integrate //depot/branch1/...@$changelist //depot/branch4/... &&
 	p4 submit -d "branch4" &&
-	changelist=$(($changelist + 2)) &&
+	changelist=$((changelist + 2)) &&
 	p4 integrate //depot/branch1/...@$changelist //depot/branch5/... &&
 	p4 submit -d "branch5" &&
 	cd "$TRASH_DIRECTORY"
@@ -503,15 +503,14 @@ test_expect_success 'add p4 complex branches' '
 # to clone the original structure if it is able to detect the origin changelist
 # of each branch.
 test_expect_success 'git-p4 clone complex branches' '
-	git init "$git" &&
+	test_when_finished cleanup_git &&
+	test_create_repo "$git" &&
 	cd "$git" &&
 	git config git-p4.branchList branch1:branch2 &&
 	git config --add git-p4.branchList branch1:branch3 &&
 	git config --add git-p4.branchList branch1:branch4 &&
 	git config --add git-p4.branchList branch1:branch5 &&
-	cd "$TRASH_DIRECTORY" &&
-	"$GITP4" clone --dest="$git" --detect-branches //depot@all &&
-	cd "$git" &&
+	"$GITP4" clone --dest=. --detect-branches //depot@all &&
 	git log --all --graph --decorate --stat &&
 	git reset --hard p4/depot/branch1 &&
 	test -f file1 &&
@@ -521,7 +520,7 @@ test_expect_success 'git-p4 clone complex branches' '
 	git reset --hard p4/depot/branch2 &&
 	test -f file1 &&
 	test -f file2 &&
-	test \! -f file3 &&
+	test ! -f file3 &&
 	! grep -q update file2 &&
 	git reset --hard p4/depot/branch3 &&
 	test -f file1 &&
@@ -531,15 +530,13 @@ test_expect_success 'git-p4 clone complex branches' '
 	git reset --hard p4/depot/branch4 &&
 	test -f file1 &&
 	test -f file2 &&
-	test \! -f file3 &&
+	test ! -f file3 &&
 	! grep -q update file2 &&
 	git reset --hard p4/depot/branch5 &&
 	test -f file1 &&
 	test -f file2 &&
 	test -f file3 &&
-	! grep -q update file2 &&
-	cd "$TRASH_DIRECTORY" &&
-	rm -rf "$git" && mkdir "$git"
+	! grep -q update file2
 '
 
 test_expect_success 'shutdown' '
Signature · 1 line
1.7.5.4
#4 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

Quoted 3 lines · On Sat, 20 Aug 2011 15:12:03 -0400 Pete Wyckoff <[email protected]> wrote:

Style edits.

		-- Pete

I still need some help on the code part of this RFC patch. I really do not like the way it is implemented. Do you see other possible ways of achieving the same algorithm?

Thanks,

Signature · 1 line
Vitor Antunes
#5 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

[email protected] wrote on Mon, 22 Aug 2011 01:13 +0100:

Quoted 3 lines

I still need some help on the code part of this RFC patch. I really do not like the way it is implemented. Do you see other possible ways of achieving the same algorithm?

I think I understand the problem now, and have a small test case to see what's going wrong. Your solution is definitely sub-optimal. :) Is there anything that can be done with --import-marks and --export-marks?

Could you guess at the branch point using p4? I.e. for each file in the new branch, grab the change that caused its most recent revision (the #xx part). Pick the max of those changes. In git, find the commit whith "change = ..." that you just discovered. That's the parent.

Even this won't be perfect, and I'm not even sure if it's less icky than diff-tree.

		-- Pete
#6 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

Quoted 4 lines · Pete Wyckoff

I think I understand the problem now, and have a small test case to see what's going wrong.  Your solution is definitely sub-optimal.  :)  Is there anything that can be done with --import-marks and --export-marks?

Don't know if you agree, but I think it would be better to keep git-p4 from storing extra files in the filesystem. And, as far as I understood, these two options require extra files.
But I don't think marks would help us that much. We already have something similar: the changelist number in the commit log. The big problem is that P4 does not store a pointer to the previous changelist.

Quoted 5 lines

Could you guess at the branch point using p4?  I.e. for each file in the new branch, grab the change that caused its most recent revision (the #xx part).  Pick the max of those changes.  In git, find the commit whith "change = ..." that you just discovered. That's the parent.

Yes, I think this would work. Nevertheless, I would prefer not to do so much pooling on the P4 server because when we're in a remote location in regards to the P4 proxy server this kind of command becomes rather slow. And if the branch has many files this may become cumbersome.

Quoted 2 lines

Even this won't be perfect, and I'm not even sure if it's less icky than diff-tree.

From what I can see, the two possible solutions would be:

a) Running a "filelog" on all files. This may become slower when the branch has lots of files. There's also the problem in needing to contact the P4 proxy server every time.

b) Using diff-tree. This will be slower if the original changelist is very far from the latest one, but has the advantage of being run locally. It has the disadvantage of not having an apparent compatibility with fast-import.

In my personal point of view, diff-tree solution still looks better, if it weren't for the fast-import issue. Now if we could just figure out how to overcome this issue...

Signature · 1 line
Vitor Antunes
#7 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

[email protected] wrote on Tue, 23 Aug 2011 23:02 +0100:

Quoted 6 lines · Pete Wyckoff
Quoted 4 lines

I think I understand the problem now, and have a small test case to see what's going wrong.  Your solution is definitely sub-optimal.  :)  Is there anything that can be done with --import-marks and --export-marks?

Quoted 6 lines

Don't know if you agree, but I think it would be better to keep git-p4 from storing extra files in the filesystem. And, as far as I understood, these two options require extra files.
But I don't think marks would help us that much. We already have something similar: the changelist number in the commit log. The big problem is that P4 does not store a pointer to the previous changelist.

Agreed.

Quoted 20 lines
Quoted 5 lines

Could you guess at the branch point using p4?  I.e. for each file in the new branch, grab the change that caused its most recent revision (the #xx part).  Pick the max of those changes.  In git, find the commit whith "change = ..." that you just discovered. That's the parent.

Yes, I think this would work. Nevertheless, I would prefer not to do so much pooling on the P4 server because when we're in a remote location in regards to the P4 proxy server this kind of command becomes rather slow. And if the branch has many files this may become cumbersome.

Quoted 2 lines

Even this won't be perfect, and I'm not even sure if it's less icky than diff-tree.

From what I can see, the two possible solutions would be:

a) Running a "filelog" on all files. This may become slower when the branch has lots of files. There's also the problem in needing to contact the P4 proxy server every time.

Quoted 8 lines

b) Using diff-tree. This will be slower if the original changelist is very far from the latest one, but has the advantage of being run locally. It has the disadvantage of not having an apparent compatibility with fast-import.

In my personal point of view, diff-tree solution still looks better, if it weren't for the fast-import issue. Now if we could just figure out how to overcome this issue...

Nice analysis. P4 can indeed be the bottleneck, even on near servers if there are lots of files. Diff-tree seems like the better choice to find the branch point. I don't understand fully why the way we are using fast-import causes these problems. Will look at that next.

		-- Pete
#8 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

Quoted 5 lines · Pete Wyckoff

Nice analysis.  P4 can indeed be the bottleneck, even on near servers if there are lots of files.  Diff-tree seems like the better choice to find the branch point.  I don't understand fully why the way we are using fast-import causes these problems.  Will look at that next.

I came up with some ideas during sleep:

1. When doing a "checkpoint" it would make sense to have some kind of feedback loop to know when fast-import completes it. In fact, I've just looked at fast-import man page and the "progress" command seems to do exactly that! Myabe we should crease a specific function for "checkpoint" that would call also "progress" instead of calling os.sleep().

2. In order to avoid needing to use "--force" it would be nice to have some form of "drop" command in fast-import that would allows us to actively drop an older commit after calling "reset" (this could even be an option of "reset"). This way fast-import would not find dangling commits in the end of the import. Maybe there's already some sort of command to achieve this in fast-import...? We could probably ask to one of its maintainers.

Signature · 1 line
Vitor Antunes
#9 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

Quoted 6 lines · Vitor Antunes

1. When doing a "checkpoint" it would make sense to have some kind of feedback loop to know when fast-import completes it. In fact, I've just looked at fast-import man page and the "progress" command seems to do exactly that! Myabe we should crease a specific function for "checkpoint" that would call also "progress" instead of calling os.sleep().

# Force a checkpoint in fast-import and wait for it to finish def checkpoint(self):
self.gitStream.write("checkpoint\n\n") self.gitStream.write("progress checkpoint\n\n") out = self.gitOutput.readline()
if self.verbose:
print "checkpoint finished: " + out

This seems to work! :)

Quoted 7 lines

2. In order to avoid needing to use "--force" it would be nice to have some form of "drop" command in fast-import that would allows us to actively drop an older commit after calling "reset" (this could even be an option of "reset"). This way fast-import would not find dangling commits in the end of the import.  Maybe there's already some sort of command to achieve this in fast-import...? We could probably ask to one of its maintainers.

Just missing point 2 now.

Signature · 1 line
Vitor Antunes
#10 lore raw

Re: [PATCH/RFC 2/2] git-p4: Add complex test case for branch import

[email protected] wrote on Wed, 24 Aug 2011 22:23 +0100:

Quoted 16 lines · Vitor Antunes
Quoted 6 lines

1. When doing a "checkpoint" it would make sense to have some kind of feedback loop to know when fast-import completes it. In fact, I've just looked at fast-import man page and the "progress" command seems to do exactly that! Myabe we should crease a specific function for "checkpoint" that would call also "progress" instead of calling os.sleep().

# Force a checkpoint in fast-import and wait for it to finish def checkpoint(self):
self.gitStream.write("checkpoint\n\n") self.gitStream.write("progress checkpoint\n\n") out = self.gitOutput.readline()
if self.verbose:
print "checkpoint finished: " + out

Quoted 1 line

This seems to work! :)

That's brilliant. And not even hacky. :)

Quoted 9 lines
Quoted 7 lines

2. In order to avoid needing to use "--force" it would be nice to have some form of "drop" command in fast-import that would allows us to actively drop an older commit after calling "reset" (this could even be an option of "reset"). This way fast-import would not find dangling commits in the end of the import.  Maybe there's already some sort of command to achieve this in fast-import...? We could probably ask to one of its maintainers.

Just missing point 2 now.

How about this. Create the commit onto a fake branch name, something involving the changeid would be okay. Remember this so we can delete it later. For a parent, you can use anything, so the top of the parent branch would be okay. Checkpoint.

Use rev-parse like you do. But go from the most recent commit on back, hoping that the branch was created recently. I suspect this is the case for those using "git-p4 sync" with detect-branches to discover new ones periodically.

For each ref, diff-tree like you do. When it is found, generate the commit again, with its real branch name and parent now.

When it exits, remember to delete the temp branches. Would be nice to do this even on error exit, but that's harder. Maybe the fake branch name should be something like "refs/git-p4-temp/change-30214" instead of refs/heads/... to indicate they are special. GC will commit the commits eventually.

The other thought I had was to use "blob" with a mark name on each file. Use --export-marks=... Checkpoint. Read the blob ids corresponding to files on the branch. Then iterate back the revs and do "git ls-tree -r ref^{tree}". Compare its blobs to the new ones until a matching commit is found. Finally assemble the blobs into a commit; no need to write them again. This seems harder.

Here's a commit that refactors the tests and adds a bunch more branch tests. They all pass with your checkpoint/progress fix, and still using --force.

		-- Pete

---------------------8<---------------
From f1e11b78cbb74fd4347df5fff64a373c3e449f68 Mon Sep 17 00:00:00 2001
From: Pete Wyckoff <[email protected]>
Date: Mon, 22 Aug 2011 22:20:33 -0400
Subject: [PATCH] git-p4 tests: refactor and add branch tests

Separate the tests related to detecting p4 branches into their own file, and add a few more.

Introduce a library for functions that are common to both git-p4 test files.

---
 t/lib-git-p4.sh          |   55 ++++++++
 t/t9800-git-p4.sh        |  182 +-------------------------
 t/t9801-git-p4-branch.sh |  326 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 388 insertions(+), 175 deletions(-)
 create mode 100644 t/lib-git-p4.sh
 create mode 100755 t/t9801-git-p4-branch.sh
t/lib-git-p4.sh new file +55 -0
new file mode 100644
index 0000000..dbc1499
@@ -0,0 +1,55 @@
+#
+# Library code for git-p4 tests
+#
+
+. ./test-lib.sh
+
+( p4 -h && p4d -h ) >/dev/null 2>&1 || {
+	skip_all='skipping git-p4 tests; no p4 or p4d'
+	test_done
+}
+
+GITP4=$GIT_BUILD_DIR/contrib/fast-import/git-p4
+P4DPORT=10669
+
+export P4PORT=localhost:$P4DPORT
+export P4CLIENT=client
+
+db="$TRASH_DIRECTORY/db"
+cli="$TRASH_DIRECTORY/cli"
+git="$TRASH_DIRECTORY/git"
+
+start_p4d()
+{
+	mkdir -p "$db" &&
+	p4d -q -d -r "$db" -p $P4DPORT &&
+	mkdir -p "$cli" &&
+	mkdir -p "$git" &&
+	cd "$cli" &&
+	p4 client -i <<-EOF
+	Client: client
+	Description: client
+	Root: $cli
+	View: //depot/... //client/...
+	EOF
+}
+
+kill_p4d()
+{
+	pid=`pgrep -f p4d` &&
+	test -n "$pid" &&
+	for i in {1..5} ; do
+	    test_debug "ps wl `echo $pid`" &&
+	    kill $pid 2>/dev/null &&
+	    pgrep -f p4d >/dev/null || break &&
+	    sleep 0.2
+	done &&
+	rm -rf "$db" &&
+	rm -rf "$cli"
+}
+
+cleanup_git() {
+	cd "$TRASH_DIRECTORY" &&
+	rm -rf "$git" &&
+	mkdir "$git"
+}
t/t9800-git-p4.sh +7 -175

205 lines not on this page · raw message

t/t9801-git-p4-branch.sh new file +326 -0

326 lines not on this page · raw message

Signature · 1 line
1.7.5.4