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

[PATCH 2/4] submodule.c: factor start_ls_files_dot_dot out of get_superproject_working_tree

From
Stefan Beller <sbeller@google.com>
Date
Nov 8, 2017, 19:55 UTC
Message-ID
<20171108195509.7839-3-sbeller@google.com>
In-Reply-To
<20171108195509.7839-1-sbeller@google.com>

We'll reuse the code of the factored out function shortly, when exploring the superproject for another aspect. Instead of knowing the root of the superproject we'll find out about the gitlink value.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 submodule.c | 53 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 19 deletions(-)
diff --git a/submodule.c b/submodule.c
index 239d94d539..4fcb64469e 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1977,15 +1977,13 @@ void absorb_git_dir_into_superproject(const char *prefix,
 	}
 }
 
-const char *get_superproject_working_tree(void)
+/* Starts a child process `ls-files` one directory above the root of the repo. */
+static int start_ls_files_dot_dot(struct child_process *cp, struct strbuf *out)
 {
-	struct child_process cp = CHILD_PROCESS_INIT;
-	struct strbuf sb = STRBUF_INIT;
 	const char *one_up = real_path_if_valid("../");
-	const char *cwd = xgetcwd();
-	const char *ret = NULL;
 	const char *subpath;
-	int code;
+	char *cwd = xgetcwd();
+	struct strbuf sb = STRBUF_INIT;
 	ssize_t len;
 
 	if (!is_inside_work_tree())
@@ -1994,31 +1992,48 @@ const char *get_superproject_working_tree(void)
 		 * We might have a superproject, but it is harder
 		 * to determine.
 		 */
-		return NULL;
+		return -1;
 
 	if (!one_up)
-		return NULL;
+		return -1;
 
 	subpath = relative_path(cwd, one_up, &sb);
 
-	prepare_submodule_repo_env(&cp.env_array);
-	argv_array_pop(&cp.env_array);
+	prepare_submodule_repo_env(&cp->env_array);
+	argv_array_pop(&cp->env_array);
 
-	argv_array_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
+	argv_array_pushl(&cp->args, "--literal-pathspecs", "-C", "..",
 			"ls-files", "-z", "--stage", "--full-name", "--",
 			subpath, NULL);
-	strbuf_reset(&sb);
 
-	cp.no_stdin = 1;
-	cp.no_stderr = 1;
-	cp.out = -1;
-	cp.git_cmd = 1;
+	cp->no_stdin = 1;
+	cp->no_stderr = 1;
+	cp->out = -1;
+	cp->git_cmd = 1;
 
-	if (start_command(&cp))
+	if (start_command(cp))
 		die(_("could not start ls-files in .."));
 
-	len = strbuf_read(&sb, cp.out, PATH_MAX);
-	close(cp.out);
+	len = strbuf_read(out, cp->out, PATH_MAX);
+	close(cp->out);
+
+	strbuf_release(&sb);
+	free(cwd);
+	return len;
+}
+
+const char *get_superproject_working_tree(void)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct strbuf sb = STRBUF_INIT;
+	const char *cwd = xgetcwd();
+	const char *ret = NULL;
+	int code;
+	ssize_t len;
+
+	len = start_ls_files_dot_dot(&cp, &sb);
+	if (len < 0)
+		return NULL;
 
 	if (starts_with(sb.buf, "160000")) {
 		int super_sub_len;
-- 
2.15.0.128.g40905b34bf.dirty
Previous: Stefan BellerNext: Jonathan Tan
Message 5 of 16 in “git-status reports relation to superproject”
  1. 0/4 git-status reports relation to superprojectStefan Beller, Nov 8, 2017
  2. 1/4 remote, revision: factor out exclusive counting between two commitsStefan Beller, Nov 8, 2017
  3. 3/4 submodule.c: get superprojects gitlink valueStefan Beller, Nov 8, 2017
  4. 4/4 git-status: report reference to superprojectStefan Beller, Nov 8, 2017
  5. 2/4 submodule.c: factor start_ls_files_dot_dot out of get_superproject_working_treeStefan Beller, Nov 8, 2017
  6. Jonathan TanNov 8, 2017
  7. [RFD] Long term plan with submodule refs?Stefan Beller, Nov 9, 2017
  8. Jonathan TanNov 9, 2017
  9. Junio C HamanoNov 9, 2017
  10. Junio C HamanoNov 9, 2017
  11. Stefan BellerNov 9, 2017
  12. Jacob KellerNov 9, 2017
  13. Stefan BellerNov 9, 2017
  14. Jacob KellerNov 10, 2017
  15. Stefan BellerNov 10, 2017
  16. Jacob KellerNov 11, 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.