static int prepare_repo_cmd(void)
{
	unsigned char sha1[20];
	int nongit = 0;
	int rc;

	/* The path to the git repository. */
	setenv("GIT_DIR", ctx.repo->path, 1);

	/* Do not look in /etc/ for gitconfig and gitattributes. */
	setenv("GIT_CONFIG_NOSYSTEM", "1", 1);
	setenv("GIT_ATTR_NOSYSTEM", "1", 1);
	unsetenv("HOME");
	unsetenv("XDG_CONFIG_HOME");

	/* Setup the git directory and initialize the notes system. Both of these
	 * load local configuration from the git repository, so we do them both while
	 * the HOME variables are unset. */
	setup_git_directory_gently(&nongit);
	init_display_notes(NULL);

	if (nongit) {
		const char *name = ctx.repo->name;
		rc = errno;
		ctx.page.title = fmtalloc("%s - %s", ctx.cfg.root_title,
						"config error");
		ctx.repo = NULL;
		cgit_print_http_headers();
		cgit_print_docstart();
		cgit_print_pageheader();
		cgit_print_error("Failed to open %s: %s", name,
				 rc ? strerror(rc) : "Not a valid git repository");
		cgit_print_docend();
		return 1;
	}
	ctx.page.title = fmtalloc("%s - %s", ctx.repo->name, ctx.repo->desc);

	if (!ctx.repo->defbranch)
		ctx.repo->defbranch = guess_defbranch();

	if (!ctx.qry.head) {
		ctx.qry.nohead = 1;
		ctx.qry.head = find_default_branch(ctx.repo);
	}

	if (!ctx.qry.head) {
		cgit_print_http_headers();
		cgit_print_docstart();
		cgit_print_pageheader();
		cgit_print_error("Repository seems to be empty");
		if (!strcmp(ctx.qry.page, "summary")) {
			html("<table class='list'><tr class='nohover'><td>&nbsp;</td></tr><tr class='nohover'><th class='left'>Clone</th></tr>\n");
			cgit_prepare_repo_env(ctx.repo);
			cgit_add_clone_urls(print_no_repo_clone_urls);
			html("</table>\n");
		}
		cgit_print_docend();
		return 1;
	}

	if (get_sha1(ctx.qry.head, sha1)) {
		char *tmp = xstrdup(ctx.qry.head);
		ctx.qry.head = ctx.repo->defbranch;
		cgit_print_error_page(404, "Not found",
				"Invalid branch: %s", tmp);
		free(tmp);
		return 1;
	}
	string_list_sort(&ctx.repo->submodules);
	cgit_prepare_repo_env(ctx.repo);
	choose_readme(ctx.repo);
	return 0;
}
static int prepare_repo_cmd(void)
{
	unsigned char sha1[20];
	int nongit = 0;
	int rc;

	 
	setenv("GIT_DIR", ctx.repo->path, 1);

	 
	setenv("GIT_CONFIG_NOSYSTEM", "1", 1);
	setenv("GIT_ATTR_NOSYSTEM", "1", 1);
	unsetenv("HOME");
	unsetenv("XDG_CONFIG_HOME");

	 
	setup_git_directory_gently(&nongit);
	init_display_notes(NULL);

	if (nongit) {
		const char *name = ctx.repo->name;
		rc = errno;
		ctx.page.title = fmtalloc("%s - %s", ctx.cfg.root_title,
						"config error");
		ctx.repo = NULL;
		cgit_print_http_headers();
		cgit_print_docstart();
		cgit_print_pageheader();
		cgit_print_error("Failed to open %s: %s", name,
				 rc ? strerror(rc) : "Not a valid git repository");
		cgit_print_docend();
		return 1;
	}
	ctx.page.title = fmtalloc("%s - %s", ctx.repo->name, ctx.repo->desc);

	if (!ctx.repo->defbranch)
		ctx.repo->defbranch = guess_defbranch();

	if (!ctx.qry.head) {
		ctx.qry.nohead = 1;
		ctx.qry.head = find_default_branch(ctx.repo);
	}

	if (!ctx.qry.head) {
		cgit_print_http_headers();
		cgit_print_docstart();
		cgit_print_pageheader();
		cgit_print_error("Repository seems to be empty");
		if (!strcmp(ctx.qry.page, "summary")) {
			html("<table class='list'><tr class='nohover'><td>&nbsp;</td></tr><tr class='nohover'><th class='left'>Clone</th></tr>\n");
			cgit_prepare_repo_env(ctx.repo);
			cgit_add_clone_urls(print_no_repo_clone_urls);
			html("</table>\n");
		}
		cgit_print_docend();
		return 1;
	}

	if (get_sha1(ctx.qry.head, sha1)) {
		char *tmp = xstrdup(ctx.qry.head);
		ctx.qry.head = ctx.repo->defbranch;
		cgit_print_error_page(404, "Not found",
				"Invalid branch: %s", tmp);
		free(tmp);
		return 1;
	}
	string_list_sort(&ctx.repo->submodules);
	cgit_prepare_repo_env(ctx.repo);
	choose_readme(ctx.repo);
	return 0;
}