git-p4: fix P4 label import for unprocessed commits

With --detect-labels enabled, git-p4 will try to create tags using git fast-import by writing a "tag" clause to the fast-import stream. If the commit that the tag references has not yet actually been processed by fast-import, then the tag can't be created and git-p4 fails to import the P4 label. Teach git-p4 to use fast-import "marks" when creating tags which reference commits created during the current run of the program. Commits created before the current run are still referenced in the old way using a normal git commit. Signed-off-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Luke Diamand committed Aug 27, 2015 at 08:18 UTC b43702ac56e602d5163ef662fb9caf382da90b94
2 files changed +18 -9
git-p4.py
+17 -8
@@ -2322,8 +2322,11 @@ class P4Sync(Command, P4UserMap):
2322 else:
2323 return "%s <a@b>" % userid
2324
2325 - # Stream a p4 tag
2325 def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
2326 + """ Stream a p4 tag.
2327 + commit is either a git commit, or a fast-import mark, ":<p4commit>"
2328 + """
2329 +
2330 if verbose:
2331 print "writing tag %s for commit %s" % (labelName, commit)
2332 gitStream.write("tag %s\n" % labelName)
@@ -2374,7 +2377,7 @@ class P4Sync(Command, P4UserMap):
2377 self.clientSpecDirs.update_client_spec_path_cache(files)
2378
2379 self.gitStream.write("commit %s\n" % branch)
2377 -# gitStream.write("mark :%s\n" % details["change"])
2380 + self.gitStream.write("mark :%s\n" % details["change"])
2381 self.committedChanges.add(int(details["change"]))
2382 committer = ""
2383 if author not in self.users:
@@ -2493,13 +2496,19 @@ class P4Sync(Command, P4UserMap):
2496 if change.has_key('change'):
2497 # find the corresponding git commit; take the oldest commit
2498 changelist = int(change['change'])
2496 - gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
2497 - "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
2498 - if len(gitCommit) == 0:
2499 - print "importing label %s: could not find git commit for changelist %d" % (name, changelist)
2500 - else:
2501 - gitCommit = gitCommit.strip()
2499 + if changelist in self.committedChanges:
2500 + gitCommit = ":%d" % changelist # use a fast-import mark
2501 commitFound = True
2502 + else:
2503 + gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
2504 + "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
2505 + if len(gitCommit) == 0:
2506 + print "importing label %s: could not find git commit for changelist %d" % (name, changelist)
2507 + else:
2508 + commitFound = True
2509 + gitCommit = gitCommit.strip()
2510 +
2511 + if commitFound:
2512 # Convert from p4 time format
2513 try:
2514 tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
t/t9811-git-p4-label-import.sh
+1 -1
@@ -222,7 +222,7 @@ p4_head_revision() {
222 # has not been seen. The presence of a label on a commit
223 # we haven't seen should not cause git-p4 to fail. It should
224 # merely skip that label, and still import other labels.
225 -test_expect_failure 'importing labels with missing revisions' '
225 +test_expect_success 'importing labels with missing revisions' '
226 test_when_finished cleanup_git &&
227 (
228 rm -fr "$cli" "$git" &&