git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 00/31] Support multiple checkouts

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jul 13, 2014, 04:50 UTC
Message-ID
<1405227068-25506-1-git-send-email-pclouds@gmail.com>
In-Reply-To
<1404891197-18067-1-git-send-email-pclouds@gmail.com>

v7 fixes all comments from Eric and Max. Jeff's two patches are dropped because they have landed in latest master now. Diff against v6:

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 470f979..57999fa 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1215,7 +1215,7 @@ gc.prunereposexpire::
 	When 'git gc' is run, it will call
 	'prune --repos --expire 3.months.ago'.
 	Override the grace period with this config variable. The value
-	"now" may be used to disable the grace period and always prune
+	"now" may be used to disable the grace period and prune
 	$GIT_DIR/repos immediately.
 
 gc.reflogexpire::
diff --git a/builtin/checkout.c b/builtin/checkout.c
index dc8503a..c83f476 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1010,15 +1010,13 @@ static int check_linked_checkout(struct branch_info *new,
 				  const char *name, const char *path)
 {
 	struct strbuf sb = STRBUF_INIT;
-	char *start, *end;
-	if (strbuf_read_file(&sb, path, 0) < 0)
-		return 0;
-	if (!starts_with(sb.buf, "ref:")) {
+	const char *start, *end;
+	if (strbuf_read_file(&sb, path, 0) < 0 ||
+	    !skip_prefix(sb.buf, "ref:", &start)) {
 		strbuf_release(&sb);
 		return 0;
 	}
 
-	start = sb.buf + 4;
 	while (isspace(*start))
 		start++;
 	end = start;
@@ -1200,8 +1198,14 @@ static int parse_branchname_arg(int argc, const char **argv,
 	else
 		new->path = NULL; /* not an existing branch */
 
-	if (new->path)
-		check_linked_checkouts(new);
+	if (new->path) {
+		unsigned char sha1[20];
+		int flag;
+		char *head_ref = resolve_refdup("HEAD", sha1, 0, &flag);
+		if (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path))
+			check_linked_checkouts(new);
+		free(head_ref);
+	}
 
 	new->commit = lookup_commit_reference_gently(rev, 1);
 	if (!new->commit) {
diff --git a/builtin/gc.c b/builtin/gc.c
index 1190183..0c65808 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -57,6 +57,17 @@ static void remove_pidfile_on_signal(int signo)
 	raise(signo);
 }
 
+static int git_config_date_string(const char **output,
+				  const char *var, const char *value)
+{
+	if (value && strcmp(value, "now")) {
+		unsigned long now = approxidate("now");
+		if (approxidate(value) >= now)
+			return error(_("Invalid %s: '%s'"), var, value);
+	}
+	return git_config_string(output, var, value);
+}
+
 static int gc_config(const char *var, const char *value, void *cb)
 {
 	if (!strcmp(var, "gc.packrefs")) {
@@ -86,22 +97,10 @@ static int gc_config(const char *var, const char *value, void *cb)
 		detach_auto = git_config_bool(var, value);
 		return 0;
 	}
-	if (!strcmp(var, "gc.pruneexpire")) {
-		if (value && strcmp(value, "now")) {
-			unsigned long now = approxidate("now");
-			if (approxidate(value) >= now)
-				return error(_("Invalid %s: '%s'"), var, value);
-		}
-		return git_config_string(&prune_expire, var, value);
-	}
-	if (!strcmp(var, "gc.prunereposexpire")) {
-		if (value && strcmp(value, "now")) {
-			unsigned long now = approxidate("now");
-			if (approxidate(value) >= now)
-				return error(_("Invalid %s: '%s'"), var, value);
-		}
-		return git_config_string(&prune_repos_expire, var, value);
-	}
+	if (!strcmp(var, "gc.pruneexpire"))
+		return git_config_date_string(&prune_expire, var, value);
+	if (!strcmp(var, "gc.prunereposexpire"))
+		return git_config_date_string(&prune_repos_expire, var, value);
 	return git_default_config(var, value, cb);
 }
 
@@ -295,7 +294,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		OPT__QUIET(&quiet, N_("suppress progress reporting")),
 		{ OPTION_STRING, 0, "prune", &prune_expire, N_("date"),
 			N_("prune unreferenced objects"),
-			PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire},
+			PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
 		OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
 		OPT_BOOL(0, "auto", &auto_gc, N_("enable auto-gc mode")),
 		OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
diff --git a/builtin/prune.c b/builtin/prune.c
index 6db6bcc..28b7adf 100644
--- a/builtin/prune.c
+++ b/builtin/prune.c
@@ -112,23 +112,41 @@ static void prune_object_dir(const char *path)
 	}
 }
 
-static const char *prune_repo_dir(const char *id, struct stat *st)
+static int prune_repo_dir(const char *id, struct stat *st, struct strbuf *reason)
 {
 	char *path;
 	int fd, len;
+
+	if (!is_directory(git_path("repos/%s", id))) {
+		strbuf_addf(reason, _("Removing repos/%s: not a valid directory"), id);
+		return 1;
+	}
 	if (file_exists(git_path("repos/%s/locked", id)))
-		return NULL;
+		return 0;
 	if (stat(git_path("repos/%s/gitdir", id), st)) {
 		st->st_mtime = expire;
-		return _("gitdir does not exist");
+		strbuf_addf(reason, _("Removing repos/%s: gitdir file does not exist"), id);
+		return 1;
 	}
 	fd = open(git_path("repos/%s/gitdir", id), O_RDONLY);
+	if (fd < 0) {
+		st->st_mtime = expire;
+		strbuf_addf(reason, _("Removing repos/%s: unable to read gitdir file (%s)"),
+			    id, strerror(errno));
+		return 1;
+	}
 	len = st->st_size;
 	path = xmalloc(len + 1);
 	read_in_full(fd, path, len);
 	close(fd);
-	while (path[len - 1] == '\n' || path[len - 1] == '\r')
+	while (len && (path[len - 1] == '\n' || path[len - 1] == '\r'))
 		len--;
+	if (!len) {
+		st->st_mtime = expire;
+		strbuf_addf(reason, _("Removing repos/%s: invalid gitdir file"), id);
+		free(path);
+		return 1;
+	}
 	path[len] = '\0';
 	if (!file_exists(path)) {
 		struct stat st_link;
@@ -139,41 +157,48 @@ static const char *prune_repo_dir(const char *id, struct stat *st)
 		 */
 		if (!stat(git_path("repos/%s/link", id), &st_link) &&
 		    st_link.st_nlink > 1)
-			return NULL;
-		return _("gitdir points to non-existing file");
+			return 0;
+		strbuf_addf(reason, _("Removing repos/%s: gitdir file points to non-existent location"), id);
+		return 1;
 	}
 	free(path);
-	return NULL;
+	return 0;
 }
 
 static void prune_repos_dir(void)
 {
-	const char *reason;
+	struct strbuf reason = STRBUF_INIT;
+	struct strbuf path = STRBUF_INIT;
 	DIR *dir = opendir(git_path("repos"));
 	struct dirent *d;
-	int removed = 0;
+	int ret;
 	struct stat st;
 	if (!dir)
 		return;
 	while ((d = readdir(dir)) != NULL) {
 		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
 			continue;
-		if ((reason = prune_repo_dir(d->d_name, &st)) != NULL &&
-		    st.st_mtime <= expire) {
-			struct strbuf sb = STRBUF_INIT;
-			if (show_only || verbose)
-				printf(_("Removing repos/%s: %s\n"), d->d_name, reason);
-			if (show_only)
-				continue;
-			strbuf_addstr(&sb, git_path("repos/%s", d->d_name));
-			remove_dir_recursively(&sb, 0);
-			strbuf_release(&sb);
-			removed = 1;
-		}
+		strbuf_reset(&reason);
+		if (!prune_repo_dir(d->d_name, &st, &reason) ||
+		    st.st_mtime > expire)
+			continue;
+		if (show_only || verbose)
+			printf("%s\n", reason.buf);
+		if (show_only)
+			continue;
+		strbuf_reset(&path);
+		strbuf_addstr(&path, git_path("repos/%s", d->d_name));
+		ret = remove_dir_recursively(&path, 0);
+		if (ret < 0 && errno == ENOTDIR)
+			ret = unlink(path.buf);
+		if (ret)
+			error(_("failed to remove: %s"), strerror(errno));
 	}
 	closedir(dir);
-	if (removed)
+	if (!show_only)
 		rmdir(git_path("repos"));
+	strbuf_release(&reason);
+	strbuf_release(&path);
 }
 
 /*
diff --git a/t/t2025-checkout-to.sh b/t/t2025-checkout-to.sh
index a219851..b0d97a0 100755
--- a/t/t2025-checkout-to.sh
+++ b/t/t2025-checkout-to.sh
@@ -54,6 +54,14 @@ test_expect_success 'detach if the same branch is already checked out' '
 	)
 '
 
+test_expect_success 'not detach on re-checking out current branch' '
+	(
+		cd there &&
+		git checkout newmaster &&
+		git symbolic-ref HEAD
+	)
+'
+
 test_expect_success 'checkout --to from a bare repo' '
 	(
 		git clone --bare . bare &&
diff --git a/t/t2026-prune-linked-checkouts.sh b/t/t2026-prune-linked-checkouts.sh
new file mode 100755
index 0000000..4ccfa4e
--- /dev/null
+++ b/t/t2026-prune-linked-checkouts.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+test_description='prune $GIT_DIR/repos'
+
+. ./test-lib.sh
+
+test_expect_success 'prune --repos on normal repo' '
+	git prune --repos &&
+	test_must_fail git prune --repos abc
+'
+
+test_expect_success 'prune files inside $GIT_DIR/repos' '
+	mkdir .git/repos &&
+	: >.git/repos/abc &&
+	git prune --repos --verbose >actual &&
+	cat >expect <<EOF &&
+Removing repos/abc: not a valid directory
+EOF
+	test_i18ncmp expect actual &&
+	! test -f .git/repos/abc &&
+	! test -d .git/repos
+'
+
+test_expect_success 'prune directories without gitdir' '
+	mkdir -p .git/repos/def/abc &&
+	: >.git/repos/def/def &&
+	cat >expect <<EOF &&
+Removing repos/def: gitdir file does not exist
+EOF
+	git prune --repos --verbose >actual &&
+	test_i18ncmp expect actual &&
+	! test -d .git/repos/def &&
+	! test -d .git/repos
+'
+
+test_expect_success POSIXPERM 'prune directories with unreadable gitdir' '
+	mkdir -p .git/repos/def/abc &&
+	: >.git/repos/def/def &&
+	: >.git/repos/def/gitdir &&
+	chmod u-r .git/repos/def/gitdir &&
+	git prune --repos --verbose >actual &&
+	test_i18ngrep "Removing repos/def: unable to read gitdir file" actual &&
+	! test -d .git/repos/def &&
+	! test -d .git/repos
+'
+
+test_expect_success 'prune directories with invalid gitdir' '
+	mkdir -p .git/repos/def/abc &&
+	: >.git/repos/def/def &&
+	: >.git/repos/def/gitdir &&
+	git prune --repos --verbose >actual &&
+	test_i18ngrep "Removing repos/def: invalid gitdir file" actual &&
+	! test -d .git/repos/def &&
+	! test -d .git/repos
+'
+
+test_expect_success 'prune directories with gitdir pointing to nowhere' '
+	mkdir -p .git/repos/def/abc &&
+	: >.git/repos/def/def &&
+	echo "$TRASH_DIRECTORY"/nowhere >.git/repos/def/gitdir &&
+	git prune --repos --verbose >actual &&
+	test_i18ngrep "Removing repos/def: gitdir file points to non-existent location" actual &&
+	! test -d .git/repos/def &&
+	! test -d .git/repos
+'
+
+test_expect_success 'not prune locked checkout' '
+	test_when_finished rm -r .git/repos
+	mkdir -p .git/repos/ghi &&
+	: >.git/repos/ghi/locked &&
+	git prune --repos &&
+	test -d .git/repos/ghi
+'
+
+test_expect_success 'not prune recent checkouts' '
+	test_when_finished rm -r .git/repos
+	mkdir zz &&
+	mkdir -p .git/repos/jlm &&
+	echo "$TRASH_DIRECTORY"/zz >.git/repos/jlm/gitdir &&
+	git prune --repos --verbose &&
+	test -d .git/repos/jlm
+'
+
+test_done
-- 
1.9.1.346.ga2b5940
Previous: Dennis KaarsemakerNext: Nguyễn Thái Ngọc Duy
Message 40 of 83 in “Support multiple checkouts”
  1. 00/32 Support multiple checkoutsNguyễn Thái Ngọc Duy, Jul 9, 2014
  2. 01/32 path.c: make get_pathname() return strbuf instead of static bufferNguyễn Thái Ngọc Duy, Jul 9, 2014
  3. 02/32 path.c: make get_pathname() call sites return const char *Nguyễn Thái Ngọc Duy, Jul 9, 2014
  4. 03/32 git_snpath(): retire and replace with strbuf_git_path()Nguyễn Thái Ngọc Duy, Jul 9, 2014
  5. 04/32 path.c: rename vsnpath() to do_git_path()Nguyễn Thái Ngọc Duy, Jul 9, 2014
  6. 05/32 path.c: group git_path(), git_pathdup() and strbuf_git_path() togetherNguyễn Thái Ngọc Duy, Jul 9, 2014
  7. 06/32 setup_git_env: use git_pathdup instead of xmalloc + sprintfNguyễn Thái Ngọc Duy, Jul 9, 2014
  8. 07/32 setup_git_env(): introduce git_path_from_env() helperNguyễn Thái Ngọc Duy, Jul 9, 2014
  9. 08/32 git_path(): be aware of file relocation in $GIT_DIRNguyễn Thái Ngọc Duy, Jul 9, 2014
  10. 09/32 *.sh: respect $GIT_INDEX_FILENguyễn Thái Ngọc Duy, Jul 9, 2014
  11. 10/32 reflog: avoid constructing .lock path with git_pathNguyễn Thái Ngọc Duy, Jul 9, 2014
  12. 11/32 fast-import: use git_path() for accessing .git dir instead of get_git_dir()Nguyễn Thái Ngọc Duy, Jul 9, 2014
  13. 12/32 commit: use SEQ_DIR instead of hardcoding "sequencer"Nguyễn Thái Ngọc Duy, Jul 9, 2014
  14. 13/32 $GIT_COMMON_DIR: a new environment variableNguyễn Thái Ngọc Duy, Jul 9, 2014
  15. 14/32 git-sh-setup.sh: use rev-parse --git-path to get $GIT_DIR/objectsNguyễn Thái Ngọc Duy, Jul 9, 2014
  16. 15/32 *.sh: avoid hardcoding $GIT_DIR/hooks/...Nguyễn Thái Ngọc Duy, Jul 9, 2014
  17. 16/32 git-stash: avoid hardcoding $GIT_DIR/logs/....Nguyễn Thái Ngọc Duy, Jul 9, 2014
  18. 17/32 setup.c: convert is_git_directory() to use strbufNguyễn Thái Ngọc Duy, Jul 9, 2014
  19. 18/32 setup.c: detect $GIT_COMMON_DIR in is_git_directory()Nguyễn Thái Ngọc Duy, Jul 9, 2014
  20. 19/32 setup.c: convert check_repository_format_gently to use strbufNguyễn Thái Ngọc Duy, Jul 9, 2014
  21. 20/32 setup.c: detect $GIT_COMMON_DIR check_repository_format_gently()Nguyễn Thái Ngọc Duy, Jul 9, 2014
  22. 21/32 setup.c: support multi-checkout repo setupNguyễn Thái Ngọc Duy, Jul 9, 2014
  23. 22/32 wrapper.c: wrapper to open a file, fprintf then closeNguyễn Thái Ngọc Duy, Jul 9, 2014
  24. 23/32 use new wrapper write_file() for simple file writingNguyễn Thái Ngọc Duy, Jul 9, 2014
  25. 24/32 checkout: support checking out into a new working directoryNguyễn Thái Ngọc Duy, Jul 9, 2014
  26. 25/32 checkout: clean up half-prepared directories in --to modeNguyễn Thái Ngọc Duy, Jul 9, 2014
  27. 26/32 checkout: detach if the branch is already checked out elsewhereNguyễn Thái Ngọc Duy, Jul 9, 2014
  28. Max KirillovJul 12, 2014
  29. 27/32 prune: strategies for linked checkoutsNguyễn Thái Ngọc Duy, Jul 9, 2014
  30. Eric SunshineJul 9, 2014
  31. 28/32 gc: style change -- no SP before closing bracketNguyễn Thái Ngọc Duy, Jul 9, 2014
  32. Eric SunshineJul 9, 2014
  33. Junio C HamanoJul 14, 2014
  34. 29/32 gc: support prune --reposNguyễn Thái Ngọc Duy, Jul 9, 2014
  35. Eric SunshineJul 9, 2014
  36. 30/32 count-objects: report unused files in $GIT_DIR/repos/...Nguyễn Thái Ngọc Duy, Jul 9, 2014
  37. 31/32 git_path(): keep "info/sparse-checkout" per work-treeNguyễn Thái Ngọc Duy, Jul 9, 2014
  38. 32/32 checkout: don't require a work tree when checking out into a new oneNguyễn Thái Ngọc Duy, Jul 9, 2014
  39. Dennis KaarsemakerJul 11, 2014
  40. 00/31 Support multiple checkoutsNguyễn Thái Ngọc Duy, Jul 13, 2014
  41. 01/31 path.c: make get_pathname() return strbuf instead of static bufferNguyễn Thái Ngọc Duy, Jul 13, 2014
  42. 02/31 path.c: make get_pathname() call sites return const char *Nguyễn Thái Ngọc Duy, Jul 13, 2014
  43. 03/31 git_snpath(): retire and replace with strbuf_git_path()Nguyễn Thái Ngọc Duy, Jul 13, 2014
  44. 04/31 path.c: rename vsnpath() to do_git_path()Nguyễn Thái Ngọc Duy, Jul 13, 2014
  45. 05/31 path.c: group git_path(), git_pathdup() and strbuf_git_path() togetherNguyễn Thái Ngọc Duy, Jul 13, 2014
  46. 06/31 git_path(): be aware of file relocation in $GIT_DIRNguyễn Thái Ngọc Duy, Jul 13, 2014
  47. 07/31 *.sh: respect $GIT_INDEX_FILENguyễn Thái Ngọc Duy, Jul 13, 2014
  48. 08/31 reflog: avoid constructing .lock path with git_pathNguyễn Thái Ngọc Duy, Jul 13, 2014
  49. 09/31 fast-import: use git_path() for accessing .git dir instead of get_git_dir()Nguyễn Thái Ngọc Duy, Jul 13, 2014
  50. 10/31 commit: use SEQ_DIR instead of hardcoding "sequencer"Nguyễn Thái Ngọc Duy, Jul 13, 2014
  51. 11/31 $GIT_COMMON_DIR: a new environment variableNguyễn Thái Ngọc Duy, Jul 13, 2014
  52. Eric SunshineJul 23, 2014
  53. 12/31 git-sh-setup.sh: use rev-parse --git-path to get $GIT_DIR/objectsNguyễn Thái Ngọc Duy, Jul 13, 2014
  54. 13/31 *.sh: avoid hardcoding $GIT_DIR/hooks/...Nguyễn Thái Ngọc Duy, Jul 13, 2014
  55. 14/31 git-stash: avoid hardcoding $GIT_DIR/logs/....Nguyễn Thái Ngọc Duy, Jul 13, 2014
  56. 15/31 setup.c: convert is_git_directory() to use strbufNguyễn Thái Ngọc Duy, Jul 13, 2014
  57. 16/31 setup.c: detect $GIT_COMMON_DIR in is_git_directory()Nguyễn Thái Ngọc Duy, Jul 13, 2014
  58. 17/31 setup.c: convert check_repository_format_gently to use strbufNguyễn Thái Ngọc Duy, Jul 13, 2014
  59. 18/31 setup.c: detect $GIT_COMMON_DIR check_repository_format_gently()Nguyễn Thái Ngọc Duy, Jul 13, 2014
  60. 19/31 setup.c: support multi-checkout repo setupNguyễn Thái Ngọc Duy, Jul 13, 2014
  61. 20/31 wrapper.c: wrapper to open a file, fprintf then closeNguyễn Thái Ngọc Duy, Jul 13, 2014
  62. 21/31 use new wrapper write_file() for simple file writingNguyễn Thái Ngọc Duy, Jul 13, 2014
  63. 22/31 checkout: support checking out into a new working directoryNguyễn Thái Ngọc Duy, Jul 13, 2014
  64. Max KirillovJul 17, 2014
  65. Junio C HamanoJul 17, 2014
  66. Eric SunshineJul 18, 2014
  67. 23/31 checkout: clean up half-prepared directories in --to modeNguyễn Thái Ngọc Duy, Jul 13, 2014
  68. Eric SunshineJul 20, 2014
  69. Eric SunshineJul 21, 2014
  70. Duy NguyenJul 23, 2014
  71. 24/31 checkout: detach if the branch is already checked out elsewhereNguyễn Thái Ngọc Duy, Jul 13, 2014
  72. 25/31 prune: strategies for linked checkoutsNguyễn Thái Ngọc Duy, Jul 13, 2014
  73. Thomas RastJul 18, 2014
  74. Duy NguyenJul 19, 2014
  75. 26/31 gc: style change -- no SP before closing bracketNguyễn Thái Ngọc Duy, Jul 13, 2014
  76. 27/31 gc: factor out gc.pruneexpire parsing codeNguyễn Thái Ngọc Duy, Jul 13, 2014
  77. 28/31 gc: support prune --reposNguyễn Thái Ngọc Duy, Jul 13, 2014
  78. 29/31 count-objects: report unused files in $GIT_DIR/repos/...Nguyễn Thái Ngọc Duy, Jul 13, 2014
  79. 30/31 git_path(): keep "info/sparse-checkout" per work-treeNguyễn Thái Ngọc Duy, Jul 13, 2014
  80. 31/31 checkout: don't require a work tree when checking out into a new oneNguyễn Thái Ngọc Duy, Jul 13, 2014
  81. Junio C HamanoJul 14, 2014
  82. Duy NguyenJul 14, 2014
  83. Junio C HamanoJul 14, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.