All of lore.kernel.org
 help / color / mirror / Atom feed
From: Prathamesh Chavan <pc44800@gmail.com>
To: pc44800@gmail.com
Cc: christian.couder@gmail.com, git@vger.kernel.org, sbeller@google.com
Subject: [GSoC][PATCH 3/6 v2] submodule: port set_name_rev from shell to C
Date: Tue, 27 Jun 2017 04:41:05 +0530	[thread overview]
Message-ID: <20170626231108.23640-3-pc44800@gmail.com> (raw)
In-Reply-To: <20170626231108.23640-1-pc44800@gmail.com>

Since later on we want to port submodule subcommand status, and since
set_name_rev is part of cmd_status, hence this function is ported. It
has been ported to function print_name_rev in C, which calls get_name_rev
to get the revname, and after formatting it, print_name_rev prints it.
And hence in this way, the command `git submodule--helper print-name-rev
"sm_path" "sha1"` sets value of revname in git-submodule.sh

The function get_name_rev returns the stdout of the git describe
commands. Since there are four different git-describe commands used for
generating the name rev, four child_process are introduced, each successive
child process running only when previous has no stdout. The order of these
four git-describe commands is maintained the same as it was in the function
set_name_rev() in shell script.

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 | 69 +++++++++++++++++++++++++++++++++++++++++++++
 git-submodule.sh            | 16 ++---------
 2 files changed, 71 insertions(+), 14 deletions(-)

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 5180659fd..86112ac92 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -244,6 +244,74 @@ static char *get_submodule_displaypath(const char *path, const char *prefix)
 	}
 }
 
+enum describe_step {
+	step_bare,
+	step_tags,
+	step_contains,
+	step_all_always,
+	step_end
+};
+
+static char *get_name_rev(const char *sub_path, const char* object_id)
+{
+	struct strbuf sb = STRBUF_INIT;
+	enum describe_step cur_step;
+
+	for (cur_step = step_bare; cur_step < step_end; cur_step++) {
+		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;
+
+		switch (cur_step) {
+			case step_bare:
+				argv_array_pushl(&cp.args, "describe",
+						 object_id, NULL);
+				break;
+			case step_tags:	
+				argv_array_pushl(&cp.args, "describe",
+						 "--tags", object_id, NULL);
+				break;
+			case step_contains:
+				argv_array_pushl(&cp.args, "describe",
+						 "--contains", object_id,
+						 NULL);
+				break;
+			case step_all_always:
+				argv_array_pushl(&cp.args, "describe",
+						 "--all", "--always",
+						 object_id, NULL);
+				break;
+			default:
+				BUG("unknown describe step '%d'", cur_step);
+		}
+
+		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 print_name_rev(int argc, const char **argv, const char *prefix)
+{
+	char *namerev;
+	if (argc != 3)
+		die("print-name-rev only accepts two arguments: <path> <sha1>");
+
+	namerev = get_name_rev(argv[1], argv[2]);
+	if (namerev && namerev[0])
+		printf(" (%s)", namerev);
+	printf("\n");
+
+	return 0;
+}
+
 struct module_list {
 	const struct cache_entry **entries;
 	int alloc, nr;
@@ -1405,6 +1473,7 @@ static struct cmd_struct commands[] = {
 	{"resolve-relative-url", resolve_relative_url, 0},
 	{"resolve-relative-url-test", resolve_relative_url_test, 0},
 	{"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
+	{"print-name-rev", print_name_rev, 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 c88e0ff7e..b28a6ba8e 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -722,18 +722,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
 #
@@ -1005,14 +993,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 print-name-rev "$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 print-name-rev "$sm_path" "$sha1")
 			say "+$sha1 $displaypath$revname"
 		fi
 
-- 
2.13.0


  parent reply	other threads:[~2017-06-26 23:11 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-26 23:09 [GSoC] Update: Week 6 Prathamesh Chavan
2017-06-26 23:11 ` [GSoC][PATCH 1/6 v2] submodule--helper: introduce for_each_submodule_list Prathamesh Chavan
2017-06-26 23:11   ` [GSoC][PATCH 2/6 v2] submodule: port subcommand foreach from shell to C Prathamesh Chavan
2017-06-27  6:06     ` Christian Couder
2017-06-26 23:11   ` Prathamesh Chavan [this message]
2017-06-26 23:11   ` [GSoC][PATCH 4/6 v2] submodule: port submodule subcommand status Prathamesh Chavan
2017-06-26 23:11   ` [GSoC][PATCH 5/6 v2] submodule: port submodule subcommand sync from shell to C Prathamesh Chavan
2017-06-27  6:37     ` Christian Couder
2017-06-26 23:11   ` [GSoC][PATCH 6/6 v2] submodule: port submodule subcommand 'deinit' " Prathamesh Chavan
2017-06-27  7:18     ` Christian Couder
2017-06-28 19:53   ` [GSoC][PATCH 1/6 v2] submodule--helper: introduce for_each_submodule_list Junio C Hamano
2017-06-29 11:47     ` Prathamesh Chavan
2017-06-30 19:47 ` [GSoC][PATCH 1/5 v3] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-06-30 19:47   ` [GSoC][PATCH 2/5 v3] submodule--helper: introduce for_each_submodule_list() Prathamesh Chavan
2017-06-30 19:47   ` [GSoC][PATCH 3/5 v3] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-06-30 22:49     ` Junio C Hamano
2017-06-30 19:47   ` [GSoC][PATCH 4/5 v3] submodule: port submodule subcommand 'status' " Prathamesh Chavan
2017-06-30 23:08     ` Junio C Hamano
2017-06-30 19:47   ` [GSoC][PATCH 5/5 v3] submodule: port submodule subcommand 'sync' " Prathamesh Chavan
2017-06-30 20:11     ` Stefan Beller
2017-06-30 23:19     ` Junio C Hamano
2017-06-30 22:37   ` [GSoC][PATCH 1/5 v3] submodule--helper: introduce get_submodule_displaypath() Junio C Hamano
2017-07-13 20:05   ` [GSoC][PATCH 1/5 v4] " Prathamesh Chavan
2017-07-13 20:05     ` [GSoC][PATCH 2/5 v4] submodule--helper: introduce for_each_submodule_list() Prathamesh Chavan
2017-07-13 20:05     ` [GSoC][PATCH 3/5 v4] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-07-13 20:05     ` [GSoC][PATCH 4/5 v4] submodule: port submodule subcommand 'status' " Prathamesh Chavan
2017-07-13 22:44       ` Stefan Beller
2017-07-13 20:05     ` [GSoC][PATCH 5/5 v4] submodule: port submodule subcommand 'sync' " Prathamesh Chavan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170626231108.23640-3-pc44800@gmail.com \
    --to=pc44800@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=sbeller@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.