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

[PATCH v7 1/3] submodule--helper: introduce get_submodule_displaypath()

From
Prathamesh Chavan <pc44800@gmail.com>
Date
Oct 6, 2017, 13:24 UTC
Message-ID
<20171006132415.2876-2-pc44800@gmail.com>
In-Reply-To
<20171006132415.2876-1-pc44800@gmail.com>

Introduce function get_submodule_displaypath() to replace the code occurring in submodule_init() for generating displaypath of the submodule with a call to it.

This new function will also be used in other parts of the system in later patches.

Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Prathamesh Chavan <pc44800@gmail.com>
---
 builtin/submodule--helper.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 06ed02f99..56c1c52e2 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -219,6 +219,26 @@ static int resolve_relative_url_test(int argc, const char **argv, const char *pr
 	return 0;
 }
 
+/* the result should be freed by the caller. */
+static char *get_submodule_displaypath(const char *path, const char *prefix)
+{
+	const char *super_prefix = get_super_prefix();
+
+	if (prefix && super_prefix) {
+		BUG("cannot have prefix '%s' and superprefix '%s'",
+		    prefix, super_prefix);
+	} else if (prefix) {
+		struct strbuf sb = STRBUF_INIT;
+		char *displaypath = xstrdup(relative_path(path, prefix, &sb));
+		strbuf_release(&sb);
+		return displaypath;
+	} else if (super_prefix) {
+		return xstrfmt("%s%s", super_prefix, path);
+	} else {
+		return xstrdup(path);
+	}
+}
+
 struct module_list {
 	const struct cache_entry **entries;
 	int alloc, nr;
@@ -334,15 +354,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
 	struct strbuf sb = STRBUF_INIT;
 	char *upd = NULL, *url = NULL, *displaypath;
 
-	if (prefix && get_super_prefix())
-		die("BUG: cannot have prefix and superprefix");
-	else if (prefix)
-		displaypath = xstrdup(relative_path(path, prefix, &sb));
-	else if (get_super_prefix()) {
-		strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
-		displaypath = strbuf_detach(&sb, NULL);
-	} else
-		displaypath = xstrdup(path);
+	displaypath = get_submodule_displaypath(path, prefix);
 
 	sub = submodule_from_path(&null_oid, path);
 
@@ -357,9 +369,9 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
 	 * Set active flag for the submodule being initialized
 	 */
 	if (!is_submodule_active(the_repository, path)) {
-		strbuf_reset(&sb);
 		strbuf_addf(&sb, "submodule.%s.active", sub->name);
 		git_config_set_gently(sb.buf, "true");
+		strbuf_reset(&sb);
 	}
 
 	/*
@@ -367,7 +379,6 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
 	 * To look up the url in .git/config, we must not fall back to
 	 * .gitmodules, so look it up directly.
 	 */
-	strbuf_reset(&sb);
 	strbuf_addf(&sb, "submodule.%s.url", sub->name);
 	if (git_config_get_string(sb.buf, &url)) {
 		if (!sub->url)
@@ -404,9 +415,9 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
 				_("Submodule '%s' (%s) registered for path '%s'\n"),
 				sub->name, url, displaypath);
 	}
+	strbuf_reset(&sb);
 
 	/* Copy "update" setting when it is not set yet */
-	strbuf_reset(&sb);
 	strbuf_addf(&sb, "submodule.%s.update", sub->name);
 	if (git_config_get_string(sb.buf, &upd) &&
 	    sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
-- 
2.14.2
Previous: Prathamesh ChavanNext: Eric Sunshine
Message 50 of 59 in “submodule--helper: introduce get_submodule_displaypath()”
  1. Prathamesh ChavanAug 21, 2017
  2. [GSoC][PATCH 2/4] submodule--helper: introduce for_each_submodule_list()Prathamesh Chavan, Aug 21, 2017
  3. Junio C HamanoAug 22, 2017
  4. [GSoC][PATCH 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 21, 2017
  5. Heiko VoigtAug 21, 2017
  6. Prathamesh ChavanAug 21, 2017
  7. [GSoC][PATCH 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 21, 2017
  8. Junio C HamanoAug 22, 2017
  9. [GSoC][PATCH v2 0/4] submodule: Incremental rewrite of git-submodulesPrathamesh Chavan, Aug 23, 2017
  10. [GSoC][PATCH v2 1/4] submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Aug 23, 2017
  11. [GSoC][PATCH v2 2/4] submodule--helper: introduce for_each_submodule()Prathamesh Chavan, Aug 23, 2017
  12. Junio C HamanoAug 23, 2017
  13. Stefan BellerAug 23, 2017
  14. Junio C HamanoAug 23, 2017
  15. [GSoC][PATCH v3 0/4] Incremental rewrite of git-submodulesPrathamesh Chavan, Aug 24, 2017
  16. [GSoC][PATCH v3 1/4] submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Aug 24, 2017
  17. [GSoC][PATCH v3 2/4] submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Aug 24, 2017
  18. [GSoC][PATCH v3 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 24, 2017
  19. [GSoC][PATCH v3 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 24, 2017
  20. Junio C HamanoAug 25, 2017
  21. Stefan BellerAug 25, 2017
  22. Junio C HamanoAug 25, 2017
  23. Prathamesh ChavanAug 27, 2017
  24. [GSoC][PATCH v4 0/4] Incremental rewrite of git-submodulesPrathamesh Chavan, Aug 28, 2017
  25. [GSoC][PATCH v4 1/4] submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Aug 28, 2017
  26. [GSoC][PATCH v4 1/4] submodule--helper: introduce get_submodule_displaypath()Han-Wen Nienhuys, Sep 21, 2017
  27. [GSoC][PATCH v4 2/4] submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Aug 28, 2017
  28. [GSoC][PATCH v4 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 28, 2017
  29. [GSoC][PATCH v4 3/4] submodule: port set_name_rev() from shell to CHan-Wen Nienhuys, Sep 21, 2017
  30. [GSoC][PATCH v4 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 28, 2017
  31. [GSoC][PATCH v4 4/4] submodule: port submodule subcommand 'status' from shell to CHan-Wen Nienhuys, Sep 21, 2017
  32. 0/4 Incremental rewrite of git-submodulesPrathamesh Chavan, Sep 24, 2017
  33. 1/4 submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Sep 24, 2017
  34. Junio C HamanoSep 25, 2017
  35. 2/4 submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Sep 24, 2017
  36. Junio C HamanoSep 25, 2017
  37. 3/4 submodule: port set_name_rev() from shell to CPrathamesh Chavan, Sep 24, 2017
  38. Junio C HamanoSep 25, 2017
  39. Junio C HamanoSep 25, 2017
  40. 4/4 submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Sep 24, 2017
  41. Junio C HamanoSep 25, 2017
  42. 0/3 Incremental rewrite of git-submodulesPrathamesh Chavan, Sep 29, 2017
  43. 1/3 submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Sep 29, 2017
  44. Junio C HamanoOct 2, 2017
  45. 2/3 submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Sep 29, 2017
  46. Junio C HamanoOct 2, 2017
  47. 3/3 submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Sep 29, 2017
  48. Junio C HamanoOct 2, 2017
  49. 0/3 Incremental rewrite of git-submodulesPrathamesh Chavan, Oct 6, 2017
  50. 1/3 submodule--helper: introduce get_submodule_displaypath()Prathamesh Chavan, Oct 6, 2017
  51. Eric SunshineOct 6, 2017
  52. 2/3 submodule--helper: introduce for_each_listed_submodule()Prathamesh Chavan, Oct 6, 2017
  53. Eric SunshineOct 6, 2017
  54. 3/3 submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Oct 6, 2017
  55. Junio C HamanoOct 7, 2017
  56. Eric SunshineOct 7, 2017
  57. Junio C HamanoOct 2, 2017
  58. [GSoC][PATCH v2 3/4] submodule: port set_name_rev() from shell to CPrathamesh Chavan, Aug 23, 2017
  59. [GSoC][PATCH v2 4/4] submodule: port submodule subcommand 'status' from shell to CPrathamesh Chavan, Aug 23, 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.