>From f26f976b83e29be567af9b056c5031203f83a094 Mon Sep 17 00:00:00 2001
From: David Mansfield <david@cobite.com>
Date: Tue, 1 Apr 2008 20:56:08 -0400
Subject: [PATCH] redo the branch creation process based on new metadata provided by cvsps

cvsps used to only provide the 'Ancestor Branch:' information in the branch
for creating new branches.  Actually, this was both insufficient and broken.
In fact, to correctly create a branch, you need to know the point at which
the branch comes from, not information about the first commit on the branch.

cvsps has been modified to provide just that.  So this change incorporates
that data and changes the way we create branches.

Now we create all branches immediately after commiting the changeset that
precedes the branching.
---
 git-cvsimport.perl |   66 ++++++++++++++++++++-------------------------------
 1 files changed, 26 insertions(+), 40 deletions(-)

diff --git a/git-cvsimport.perl b/git-cvsimport.perl
index a7fd466..0444af8 100755
--- a/git-cvsimport.perl
+++ b/git-cvsimport.perl
@@ -692,8 +692,8 @@ sub write_tree () {
 	return $tree;
 }
 
-my ($patchset,$date,$author_name,$author_email,$branch,$ancestor,$tag,$logmsg);
-my (@old,@new,@skipped,%ignorebranch,@branches);
+my ($patchset,$date,$author_name,$author_email,$branch,$tag,$logmsg);
+my (@old,@new,@skipped,%ignorebranch,@child_branches);
 
 # commits that cvsps cannot place anywhere...
 $ignorebranch{'#CVSPS_NO_BRANCH'} = 1;
@@ -711,11 +711,7 @@ sub commit {
 	    unless ($index{$branch}) {
 		$index{$branch} = tmpnam();
 		$ENV{GIT_INDEX_FILE} = $index{$branch};
-		if ($ancestor) {
-		    system("git-read-tree", "$remote/$ancestor");
-		} else {
-		    system("git-read-tree", "$remote/$branch");
-		}
+		system("git-read-tree", "$remote/$branch");
 		die "read-tree failed: $?\n" if $?;
 	    }
 	}
@@ -787,7 +783,9 @@ sub commit {
 
 		print "Created tag '$xtag' on '$branch'\n" if $opt_v;
 	}
-};
+
+	return $cid;
+}
 
 my $commitcount = 1;
 while (<CVS>) {
@@ -826,12 +824,9 @@ while (<CVS>) {
 		$branch = $_;
 		$state = 5;
 	} elsif ($state == 5 and s/^Ancestor branch:\s+//) {
-		s/\s+$//;
-		$ancestor = $_;
-		$ancestor = $opt_o if $ancestor eq "HEAD";
+		# now ignored.  see 'Branches' below
 		$state = 6;
 	} elsif ($state == 5) {
-		$ancestor = undef;
 		$state = 6;
 		redo;
 	} elsif ($state == 6 and s/^Tag:\s+//) {
@@ -844,7 +839,7 @@ while (<CVS>) {
 		$state = 12;
 	} elsif ($state == 12 and s/^Branches:\s+//) {
 		s/\s+$//;
-		@branches = split /,/;
+		@child_branches = split /,/;
 		$state = 7;
 	} elsif ($state == 7 and /^Log:/) {
 		$logmsg = "";
@@ -871,33 +866,7 @@ while (<CVS>) {
 			$state = 11;
 			next;
 		}
-		if ($ancestor) {
-			if ($ancestor eq $branch) {
-				print STDERR "Branch $branch erroneously stems from itself -- changed ancestor to $opt_o\n";
-				$ancestor = $opt_o;
-			}
-			if (defined get_headref("$remote/$branch")) {
-				print STDERR "Branch $branch already exists!\n";
-				$state=11;
-				next;
-			}
-			my $id = get_headref("$remote/$ancestor");
-			if (!$id) {
-				print STDERR "Branch $ancestor does not exist!\n";
-				$ignorebranch{$branch} = 1;
-				$state=11;
-				next;
-			}
 
-			system(qw(git update-ref -m cvsimport),
-				"$remote/$branch", $id);
-			if($? != 0) {
-				print STDERR "Could not create branch $branch\n";
-				$ignorebranch{$branch} = 1;
-				$state=11;
-				next;
-			}
-		}
 		$last_branch = $branch if $branch ne $last_branch;
 		$state = 9;
 	} elsif ($state == 8) {
@@ -945,7 +914,24 @@ while (<CVS>) {
 		if ($opt_L && $commitcount > $opt_L) {
 			last;
 		}
-		commit();
+		my $cid = commit();
+		# the just made commit is the ancestor of all its branches
+		for my $child_branch (@child_branches) {
+			print "Creating child branch $child_branch\n" if $opt_v;
+			if (defined get_headref("$remote/$child_branch")) {
+				print STDERR "Branch $child_branch already exists!\n";
+				$state=11;
+				next;
+			}
+			system(qw(git update-ref -m cvsimport),
+				"$remote/$child_branch", $cid);
+			if($? != 0) {
+				print STDERR "Could not create branch $child_branch\n";
+				$ignorebranch{$child_branch} = 1;
+				$state=11;
+				next;
+			}
+		}
 		if (($commitcount & 1023) == 0) {
 			system("git repack -a -d");
 		}
-- 
1.5.4.1

