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

[PATCH v5 3/4] submodule: port set_name_rev() from shell to C

From
Prathamesh Chavan <pc44800@gmail.com>
Date
Sep 24, 2017, 12:08 UTC
Message-ID
<20170924120858.26813-4-pc44800@gmail.com>
In-Reply-To
<20170924120858.26813-1-pc44800@gmail.com>

Function set_name_rev() is ported from git-submodule to the submodule--helper builtin. The function compute_rev_name() generates the value of the revision name as required. The function get_rev_name() calls compute_rev_name() and receives the revision name, and later handles its formatting and printing.

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 | 63 +++++++++++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 16 ++----------
 2 files changed, 65 insertions(+), 14 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index d12790b5c..7ca8e8153 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -246,6 +246,68 @@ static char *get_submodule_displaypath(const char *path, const char *prefix)
 	}
 }
 
+static char *compute_rev_name(const char *sub_path, const char* object_id)
+{
+	struct strbuf sb = STRBUF_INIT;
+	const char ***d;
+
+	static const char *describe_bare[] = {
+		NULL
+	};
+
+	static const char *describe_tags[] = {
+		"--tags", NULL
+	};
+
+	static const char *describe_contains[] = {
+		"--contains", NULL
+	};
+
+	static const char *describe_all_always[] = {
+		"--all", "--always", NULL
+	};
+
+	static const char **describe_argv[] = {
+		describe_bare, describe_tags, describe_contains,
+		describe_all_always, NULL
+	};
+
+	for (d = describe_argv; *d; d++) {
+		struct child_process cp = CHILD_PROCESS_INIT;
+		prepare_submodule_repo_env(&cp.env_array);
+		cp.dir = sub_path;
+		cp.git_cmd = 1;
+		cp.no_stderr = 1;
+
+		argv_array_push(&cp.args, "describe");
+		argv_array_pushv(&cp.args, *d);
+		argv_array_push(&cp.args, object_id);
+
+		if (!capture_command(&cp, &sb, 0) && sb.len) {
+			strbuf_strip_suffix(&sb, "\n");
+			return strbuf_detach(&sb, NULL);
+		}
+	}
+
+	strbuf_release(&sb);
+	return NULL;
+}
+
+static int get_rev_name(int argc, const char **argv, const char *prefix)
+{
+	char *revname;
+	if (argc != 3)
+		die("get-rev-name only accepts two arguments: <path> <sha1>");
+
+	revname = compute_rev_name(argv[1], argv[2]);
+	if (revname && revname[0])
+		printf(" (%s)", revname);
+	printf("\n");
+
+	free(revname);
+	return 0;
+}
+
 struct module_list {
 	const struct cache_entry **entries;
 	int alloc, nr;
@@ -1293,6 +1355,7 @@ static struct cmd_struct commands[] = {
 	{"relative-path", resolve_relative_path, 0},
 	{"resolve-relative-url", resolve_relative_url, 0},
 	{"resolve-relative-url-test", resolve_relative_url_test, 0},
+	{"get-rev-name", get_rev_name, 0},
 	{"init", module_init, SUPPORT_SUPER_PREFIX},
 	{"remote-branch", resolve_remote_submodule_branch, 0},
 	{"push-check", push_check, 0},
diff --git a/git-submodule.sh b/git-submodule.sh
index 66d1ae8ef..5211361c5 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -758,18 +758,6 @@ cmd_update()
 	}
 }
 
-set_name_rev () {
-	revname=$( (
-		sanitize_submodule_env
-		cd "$1" && {
-			git describe "$2" 2>/dev/null ||
-			git describe --tags "$2" 2>/dev/null ||
-			git describe --contains "$2" 2>/dev/null ||
-			git describe --all --always "$2"
-		}
-	) )
-	test -z "$revname" || revname=" ($revname)"
-}
 #
 # Show commit summary for submodules in index or working tree
 #
@@ -1041,14 +1029,14 @@ cmd_status()
 		fi
 		if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
 		then
-			set_name_rev "$sm_path" "$sha1"
+			revname=$(git submodule--helper get-rev-name "$sm_path" "$sha1")
 			say " $sha1 $displaypath$revname"
 		else
 			if test -z "$cached"
 			then
 				sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
 			fi
-			set_name_rev "$sm_path" "$sha1"
+			revname=$(git submodule--helper get-rev-name "$sm_path" "$sha1")
 			say "+$sha1 $displaypath$revname"
 		fi
 
-- 
2.13.0
Previous: Junio C HamanoNext: Junio C Hamano
Message 37 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.