git am from scratch

7 messages, 3 authors, 2016-06-15 · open the first message on its own page

git am from scratch

From: Andreas Gruenbacher <hidden>
Date: 2016-06-15 22:46:25

Hello,

lates git (e986ceb0): there is a bug in git am when trying to recreate the 
entire history of a repository:

	$ git format-patch --stdout rcs-history \
		| ( rm -rf fOo && mkdir fOo && cd fOo && git init && git am )
	Initialized empty Git repository in /home/agruen/git/patch/foo/.git/
	fatal: HEAD: not a valid SHA1
	fatal: bad revision 'HEAD'

When the first commit is added by hand instead, git am will import the rest of 
the mbox without problems. This is annoying because it's at least not 
immediately obvious how to recreate the initial commit with all the metadata.

Is this easily fixed?

Thanks,
Andreas

Re: git am from scratch

From: Eric Raible <hidden>
Date: 2016-06-15 22:46:25

Andreas Gruenbacher <agruen <at> suse.de> writes:
When the first commit is added by hand instead, git am will import the rest 
Or:
git init && git commit --allow-empty -m'initial'

Re: git am from scratch

From: Jeff King <hidden>
Date: 2016-06-15 22:46:25

On Thu, Mar 19, 2009 at 04:09:24PM +0100, Andreas Gruenbacher wrote:
lates git (e986ceb0): there is a bug in git am when trying to recreate the 
entire history of a repository:
I don't think this has ever worked in any version of git.

It might be nice to have, since it does come up once in a while, so I
suspect patches would be welcome.

But...
When the first commit is added by hand instead, git am will import the
rest of the mbox without problems. This is annoying because it's at
least not immediately obvious how to recreate the initial commit with
all the metadata.

Is this easily fixed?
There are many other ways to do this. Have you tried:

  - simply pulling the old repo into the new one?

  - if they are not connected in real-time, creating a bundle from the
    first and pulling it into the latter?

  - if you want to tweak the commit data during the process, have you
    tried using fast-export/fast-import rather than format-patch/am?

-Peff

Re: git am from scratch

From: Andreas Gruenbacher <hidden>
Date: 2016-06-15 22:46:25

On Thursday, 19 March 2009 21:18:17 Jeff King wrote:
I don't think this has ever worked in any version of git.
I did find a way to help myself in the end. Still it was still a major, 
unnecessary annoyance.

I ran into this problem when trying to reconstruct a project's history (after 
going RCS -> CVS -> git many things were still wrong like unrelated RCS files 
which ended up in the history, RCS files being moved to the Attic in the 
original tree to indicate deletes [which means they will happily live on from 
a CVS point of view], etc.).

The easiest way I could find how to get rid of all the mess without going 
totally insane was to git-format-patch the repository, fix up the major 
things in the mbox, and then recreate the history.

In the end I ended up preparing the initial commit by hand followed 
by "commit -c HASH" to preserve the metadata. (The cvsimport branch had a 
different commit at its tip, so I couldn't use commit --amend on that.) After 
that, I git-am'ed the rest of the mbox onto that reconstructed commit. This 
could have been significantly easier.
There are many other ways to do this.
That's really not the point. Thanks though.

Andreas

Re: git am from scratch

From: Jeff King <hidden>
Date: 2016-06-15 22:46:25

On Thu, Mar 19, 2009 at 09:42:49PM +0100, Andreas Gruenbacher wrote:
quoted
I don't think this has ever worked in any version of git.
I did find a way to help myself in the end. Still it was still a major, 
unnecessary annoyance.
Sure, and that is why I suggested a patch for git-am might be welcome;
it's not the right tool for the job, but it seems to be one that people
naturally think of.
I ran into this problem when trying to reconstruct a project's history (after 
going RCS -> CVS -> git many things were still wrong like unrelated RCS files 
which ended up in the history, RCS files being moved to the Attic in the 
original tree to indicate deletes [which means they will happily live on from 
a CVS point of view], etc.).
Yikes. Out of curiosity, what did you use to do the CVS import?


Anyway, here is a not-very-well-tested patch to get "git am" to apply on
top of an empty repository (i.e., it worked on my utterly simplistic
test case and I didn't think too hard about what else might have been
broken). Maybe it will give a good start to somebody who wants to work
on this.

---
diff --git a/git-am.sh b/git-am.sh
index d339075..bcc600d 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -290,17 +290,23 @@ else
 		: >"$dotest/rebasing"
 	else
 		: >"$dotest/applying"
-		git update-ref ORIG_HEAD HEAD
+		if git rev-parse --quiet --verify HEAD; then
+			git update-ref ORIG_HEAD HEAD
+		else
+			rm -f "$GIT_DIR/ORIG_HEAD"
+		fi
 	fi
 fi
 
 case "$resolved" in
 '')
-	files=$(git diff-index --cached --name-only HEAD --) || exit
-	if test "$files"
-	then
-		: >"$dotest/dirtyindex"
-		die "Dirty index: cannot apply patches (dirty: $files)"
+	if git rev-parse --quiet --verify HEAD; then
+		files=$(git diff-index --cached --name-only HEAD --) || exit
+		if test "$files"
+		then
+			: >"$dotest/dirtyindex"
+			die "Dirty index: cannot apply patches (dirty: $files)"
+		fi
 	fi
 esac
 
@@ -541,7 +547,7 @@ do
 	fi
 
 	tree=$(git write-tree) &&
-	parent=$(git rev-parse --verify HEAD) &&
+	parent=$(git rev-parse --quiet --verify HEAD)
 	commit=$(
 		if test -n "$ignore_date"
 		then
@@ -552,7 +558,7 @@ do
 			GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
 			export GIT_COMMITTER_DATE
 		fi &&
-		git commit-tree $tree -p $parent <"$dotest/final-commit"
+		git commit-tree $tree ${parent:+-p $parent} <"$dotest/final-commit"
 	) &&
 	git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
 	stop_here $this

Re: git am from scratch

From: Andreas Gruenbacher <hidden>
Date: 2016-06-15 22:46:25

On Thursday, 19 March 2009 22:02:14 Jeff King wrote:
Yikes. Out of curiosity, what did you use to do the CVS import?
git-cvsimport

Andreas

Re: git am from scratch

From: Jeff King <hidden>
Date: 2016-06-15 22:46:25

On Fri, Mar 20, 2009 at 02:06:24AM +0100, Andreas Gruenbacher wrote:
On Thursday, 19 March 2009 22:02:14 Jeff King wrote:
quoted
Yikes. Out of curiosity, what did you use to do the CVS import?
git-cvsimport
Ah. It sounds like you have already resolved all of your issues by
munging the resulting history, but you may have better success on a
complex history with a different tool like cvs2git or parsecvs (even if
you have fixed it, you may want to investigate them to double-check your
result).

-Peff
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help