@@ -425,6 +425,27 @@ int init_db(const char *git_dir, const char *real_git_dir,
validate_hash_algorithm(&repo_fmt, hash);
+ /*
+ * At this point, the_repository we have in-core does not look
+ * anything like one that we would see initialized in an already
+ * working repository after calling setup_git_directory().
+ *
+ * Calling repository.c::initialize_the_repository() may have
+ * prepared the .index .objects and .parsed_objects members, but
+ * other members like .gitdir, .commondir, etc. have not been
+ * initialized.
+ *
+ * Many API functions assume they are working with the_repository
+ * that has sensibly been initialized, but because we haven't
+ * really read from an existing repository, we need to hand-craft
+ * the necessary members of the structure to get out of this
+ * chicken-and-egg situation.
+ *
+ * For now, we update the hash algorithm member to what the
+ * validate_hash_algorithm() call decided for us.
+ */
+ repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
+
reinit = create_default_files(template_dir, original_git_dir,
initial_branch, &repo_fmt,
flags & INIT_DB_QUIET);