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

[PATCH 10/15] update submodules: add submodule_go_from_to

From
Stefan Beller <sbeller@google.com>
Date
Feb 16, 2017, 00:38 UTC
Message-ID
<20170216003811.18273-11-sbeller@google.com>
In-Reply-To
<20170216003811.18273-1-sbeller@google.com>

In later patches we introduce the options and flag for commands that modify the working directory, e.g. git-checkout.

This piece of code will be used universally for
all these working tree modifications as it
* supports dry run to answer the question:
  "Is it safe to change the submodule to this new state?"
  e.g. is it overwriting untracked files or are there local
  changes that would be overwritten?
* supports a force flag that can be used for resetting
  the tree.
Signed-off-by: Stefan Beller <sbeller@google.com>
---
 submodule.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 submodule.h |   5 ++
 2 files changed, 156 insertions(+)
diff --git a/submodule.c b/submodule.c
index b262c5b0ad..84cc62f3bb 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1250,6 +1250,157 @@ int bad_to_remove_submodule(const char *path, unsigned flags)
 	return ret;
 }
 
+static int submodule_has_dirty_index(const struct submodule *sub)
+{
+	ssize_t len;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct strbuf buf = STRBUF_INIT;
+	int ret = 0;
+
+	prepare_submodule_repo_env_no_git_dir(&cp.env_array);
+
+	cp.git_cmd = 1;
+	argv_array_pushl(&cp.args, "diff-index", "--cached", "HEAD", NULL);
+	cp.no_stdin = 1;
+	cp.out = -1;
+	cp.dir = sub->path;
+	if (start_command(&cp))
+		die("could not recurse into submodule %s", sub->path);
+
+	len = strbuf_read(&buf, cp.out, 1024);
+	if (len > 2)
+		ret = 1;
+
+	close(cp.out);
+	if (finish_command(&cp))
+		die("could not recurse into submodule %s", sub->path);
+
+	strbuf_release(&buf);
+	return ret;
+}
+
+void submodule_clean_index(const char *path)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	prepare_submodule_repo_env_no_git_dir(&cp.env_array);
+
+	cp.git_cmd = 1;
+	cp.no_stdin = 1;
+	cp.dir = path;
+
+	argv_array_pushf(&cp.args, "--super-prefix=%s/", path);
+	argv_array_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
+
+	argv_array_push(&cp.args, EMPTY_TREE_SHA1_HEX);
+
+	if (run_command(&cp))
+		die("could not clean submodule index");
+}
+
+/**
+ * Moves a submodule at a given path from a given head to another new head.
+ * For edge cases (a submodule coming into existence or removing a submodule)
+ * pass NULL for old or new respectively.
+ *
+ * TODO: move dryrun and forced to flags.
+ */
+int submodule_go_from_to(const char *path,
+			 const char *old,
+			 const char *new,
+			 int dry_run,
+			 int force)
+{
+	int ret = 0;
+	struct child_process cp = CHILD_PROCESS_INIT;
+	const struct submodule *sub;
+
+	sub = submodule_from_path(null_sha1, path);
+
+	if (!sub)
+		die("BUG: could not get submodule information for '%s'", path);
+
+	if (!dry_run) {
+		if (old) {
+			if (!submodule_uses_gitfile(path))
+				absorb_git_dir_into_superproject("", path,
+					ABSORB_GITDIR_RECURSE_SUBMODULES);
+		} else {
+			struct strbuf sb = STRBUF_INIT;
+			strbuf_addf(&sb, "%s/modules/%s",
+				    get_git_common_dir(), sub->name);
+			connect_work_tree_and_git_dir(path, sb.buf);
+			strbuf_release(&sb);
+
+			/* make sure the index is clean as well */
+			submodule_clean_index(path);
+		}
+	}
+
+	if (old && !force) {
+		/* Check if the submodule has a dirty index. */
+		if (submodule_has_dirty_index(sub)) {
+			/* print a thing here? */
+			return -1;
+		}
+	}
+
+	prepare_submodule_repo_env_no_git_dir(&cp.env_array);
+
+	cp.git_cmd = 1;
+	cp.no_stdin = 1;
+	cp.dir = path;
+
+	argv_array_pushf(&cp.args, "--super-prefix=%s/", path);
+	argv_array_pushl(&cp.args, "read-tree", NULL);
+
+	if (dry_run)
+		argv_array_push(&cp.args, "-n");
+	else
+		argv_array_push(&cp.args, "-u");
+
+	if (force)
+		argv_array_push(&cp.args, "--reset");
+	else
+		argv_array_push(&cp.args, "-m");
+
+	argv_array_push(&cp.args, old ? old : EMPTY_TREE_SHA1_HEX);
+	argv_array_push(&cp.args, new ? new : EMPTY_TREE_SHA1_HEX);
+
+	if (run_command(&cp)) {
+		ret = -1;
+		goto out;
+	}
+
+	if (!dry_run) {
+		if (new) {
+			struct child_process cp1 = CHILD_PROCESS_INIT;
+			/* also set the HEAD accordingly */
+			cp1.git_cmd = 1;
+			cp1.no_stdin = 1;
+			cp1.dir = path;
+
+			argv_array_pushl(&cp1.args, "update-ref", "HEAD",
+					 new ? new : EMPTY_TREE_SHA1_HEX, NULL);
+
+			if (run_command(&cp1)) {
+				ret = -1;
+				goto out;
+			}
+		} else {
+			struct strbuf sb = STRBUF_INIT;
+
+			strbuf_addf(&sb, "%s/.git", path);
+			unlink_or_warn(sb.buf);
+			strbuf_release(&sb);
+
+			if (is_empty_dir(path))
+				rmdir_or_warn(path);
+		}
+	}
+out:
+	return ret;
+}
+
 static int find_first_merges(struct object_array *result, const char *path,
 		struct commit *a, struct commit *b)
 {
diff --git a/submodule.h b/submodule.h
index 46d9f0f293..3336607bfc 100644
--- a/submodule.h
+++ b/submodule.h
@@ -102,6 +102,11 @@ extern int push_unpushed_submodules(struct sha1_array *commits,
 extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
 extern int parallel_submodules(void);
 
+extern int submodule_go_from_to(const char *path,
+				const char *old,
+				const char *new,
+				int dry_run, int force);
+
 /*
  * Prepare the "env_array" parameter of a "struct child_process" for executing
  * a submodule by clearing any repo-specific envirionment variables, but
-- 
2.12.0.rc1.16.ge4278d41a0.dirty
Previous: Stefan BellerNext: Junio C Hamano
Message 49 of 94 in “Checkout aware of Submodules!”
  1. 00/14 Checkout aware of Submodules!Stefan Beller, Feb 15, 2017
  2. 01/14 lib-submodule-update.sh: reorder create_lib_submodule_repoStefan Beller, Feb 15, 2017
  3. brian m. carlsonFeb 15, 2017
  4. 02/14 lib-submodule-update.sh: define tests for recursing into submodulesStefan Beller, Feb 15, 2017
  5. Brandon WilliamsFeb 15, 2017
  6. Stefan BellerFeb 15, 2017
  7. 07/14 update submodules: introduce is_interesting_submoduleStefan Beller, Feb 15, 2017
  8. Brandon WilliamsFeb 15, 2017
  9. Stefan BellerFeb 15, 2017
  10. 06/14 update submodules: add a config option to determine if submodules are updatedStefan Beller, Feb 15, 2017
  11. 05/14 update submodules: add submodule config parsingStefan Beller, Feb 15, 2017
  12. Junio C HamanoFeb 15, 2017
  13. 11/14 unpack-trees: check if we can perform the operation for submodulesStefan Beller, Feb 15, 2017
  14. 10/14 unpack-trees: pass old oid to verify_clean_submoduleStefan Beller, Feb 15, 2017
  15. 09/14 update submodules: add submodule_go_from_toStefan Beller, Feb 15, 2017
  16. brian m. carlsonFeb 15, 2017
  17. 04/14 connect_work_tree_and_git_dir: safely create leading directoriesStefan Beller, Feb 15, 2017
  18. Junio C HamanoFeb 15, 2017
  19. Stefan BellerFeb 15, 2017
  20. Junio C HamanoFeb 15, 2017
  21. 13/14 entry.c: update submodules when interestingStefan Beller, Feb 15, 2017
  22. 14/14 builtin/checkout: add --recurse-submodules switchStefan Beller, Feb 15, 2017
  23. brian m. carlsonFeb 15, 2017
  24. Stefan BellerFeb 16, 2017
  25. 12/14 read-cache: remove_marked_cache_entries to wipe selected submodules.Stefan Beller, Feb 15, 2017
  26. 03/14 make is_submodule_populated gentlyStefan Beller, Feb 15, 2017
  27. Junio C HamanoFeb 15, 2017
  28. Stefan BellerFeb 15, 2017
  29. 08/14 update submodules: move up prepare_submodule_repo_envStefan Beller, Feb 15, 2017
  30. brian m. carlsonFeb 15, 2017
  31. Junio C HamanoFeb 15, 2017
  32. 00/14 Checkout aware of Submodules!Stefan Beller, Feb 16, 2017
  33. 05/15 connect_work_tree_and_git_dir: safely create leading directoriesStefan Beller, Feb 16, 2017
  34. Junio C HamanoFeb 16, 2017
  35. Stefan BellerFeb 16, 2017
  36. Junio C HamanoFeb 16, 2017
  37. 07/15 update submodules: add a config option to determine if submodules are updatedStefan Beller, Feb 16, 2017
  38. 03/15 lib-submodule-update.sh: define tests for recursing into submodulesStefan Beller, Feb 16, 2017
  39. Junio C HamanoFeb 16, 2017
  40. Stefan BellerFeb 22, 2017
  41. 02/15 lib-submodule-update.sh: do not use ./. as submodule remoteStefan Beller, Feb 16, 2017
  42. 08/15 submodules: introduce check to see whether to touch a submoduleStefan Beller, Feb 16, 2017
  43. Junio C HamanoFeb 16, 2017
  44. Jacob KellerFeb 16, 2017
  45. Jacob KellerFeb 17, 2017
  46. Stefan BellerFeb 21, 2017
  47. 04/15 make is_submodule_populated gentlyStefan Beller, Feb 16, 2017
  48. 09/15 update submodules: move up prepare_submodule_repo_envStefan Beller, Feb 16, 2017
  49. 10/15 update submodules: add submodule_go_from_toStefan Beller, Feb 16, 2017
  50. Junio C HamanoFeb 16, 2017
  51. Stefan BellerFeb 16, 2017
  52. 06/15 update submodules: add submodule config parsingStefan Beller, Feb 16, 2017
  53. Jacob KellerFeb 17, 2017
  54. Stefan BellerFeb 21, 2017
  55. Jacob KellerFeb 21, 2017
  56. 13/15 read-cache: remove_marked_cache_entries to wipe selected submodules.Stefan Beller, Feb 16, 2017
  57. Junio C HamanoFeb 16, 2017
  58. 15/15 builtin/checkout: add --recurse-submodules switchStefan Beller, Feb 16, 2017
  59. 11/15 unpack-trees: pass old oid to verify_clean_submoduleStefan Beller, Feb 16, 2017
  60. Junio C HamanoFeb 16, 2017
  61. 12/15 unpack-trees: check if we can perform the operation for submodulesStefan Beller, Feb 16, 2017
  62. Brandon WilliamsFeb 16, 2017
  63. Junio C HamanoFeb 16, 2017
  64. Jacob KellerFeb 17, 2017
  65. Stefan BellerFeb 21, 2017
  66. Jacob KellerFeb 21, 2017
  67. Stefan BellerFeb 21, 2017
  68. Jacob KellerFeb 22, 2017
  69. 14/15 entry.c: update submodules when interestingStefan Beller, Feb 16, 2017
  70. 01/15 lib-submodule-update.sh: reorder create_lib_submodule_repoStefan Beller, Feb 16, 2017
  71. Junio C HamanoFeb 16, 2017
  72. Jacob KellerFeb 16, 2017
  73. Stefan BellerFeb 16, 2017
  74. Jacob KellerFeb 16, 2017
  75. 00/14 Checkout aware of Submodules!Stefan Beller, Feb 23, 2017
  76. 08/15 submodules: introduce check to see whether to touch a submoduleStefan Beller, Feb 23, 2017
  77. 09/15 update submodules: move up prepare_submodule_repo_envStefan Beller, Feb 23, 2017
  78. 01/15 lib-submodule-update.sh: reorder create_lib_submodule_repoStefan Beller, Feb 23, 2017
  79. 04/15 make is_submodule_populated gentlyStefan Beller, Feb 23, 2017
  80. 03/15 lib-submodule-update.sh: define tests for recursing into submodulesStefan Beller, Feb 23, 2017
  81. 11/15 unpack-trees: pass old oid to verify_clean_submoduleStefan Beller, Feb 23, 2017
  82. 02/15 lib-submodule-update.sh: do not use ./. as submodule remoteStefan Beller, Feb 23, 2017
  83. 06/15 update submodules: add submodule config parsingStefan Beller, Feb 23, 2017
  84. 14/15 entry.c: update submodules when interestingStefan Beller, Feb 23, 2017
  85. 12/15 unpack-trees: check if we can perform the operation for submodulesStefan Beller, Feb 23, 2017
  86. 05/15 connect_work_tree_and_git_dir: safely create leading directoriesStefan Beller, Feb 23, 2017
  87. 07/15 update submodules: add a config option to determine if submodules are updatedStefan Beller, Feb 23, 2017
  88. 10/15 update submodules: add submodule_move_headStefan Beller, Feb 23, 2017
  89. Ramsay JonesFeb 24, 2017
  90. Stefan BellerFeb 24, 2017
  91. 13/15 read-cache, remove_marked_cache_entries: wipe selected submodules.Stefan Beller, Feb 23, 2017
  92. 15/15 builtin/checkout: add --recurse-submodules switchStefan Beller, Feb 23, 2017
  93. Ramsay JonesFeb 24, 2017
  94. Stefan BellerFeb 24, 2017

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.