From: Prathamesh Chavan <pc44800@gmail.com>
To: gitster@pobox.com
Cc: christian.couder@gmail.com, git@vger.kernel.org,
hanwen@google.com, pc44800@gmail.com, sbeller@google.com
Subject: [PATCH v7 3/3] submodule: port submodule subcommand 'status' from shell to C
Date: Fri, 6 Oct 2017 18:54:15 +0530 [thread overview]
Message-ID: <20171006132415.2876-4-pc44800@gmail.com> (raw)
In-Reply-To: <20171006132415.2876-1-pc44800@gmail.com>
This aims to make git-submodule 'status' a built-in. Hence, the function
cmd_status() is ported from shell to C. This is done by introducing
four functions: module_status(), submodule_status_cb(),
submodule_status() and print_status().
The function module_status() acts as the front-end of the subcommand.
It parses subcommand's options and then calls the function
module_list_compute() for computing the list of submodules. Then
this functions calls for_each_listed_submodule() looping through the
list obtained.
Then for_each_listed_submodule() calls submodule_status_cb() for each of
the submodule in its list. The function submodule_status_cb() calls
submodule_status() after passing appropriate arguments to the funciton.
Function submodule_status() is responsible for generating the status
each submodule it is called for, and then calls print_status().
Finally, the function print_status() handles the printing of submodule's
status.
Function set_name_rev() is also ported from git-submodule to the
submodule--helper builtin function compute_rev_name(), which now
generates the value of the revision name as required.
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 | 198 ++++++++++++++++++++++++++++++++++++++++++++
git-submodule.sh | 61 +-------------
2 files changed, 199 insertions(+), 60 deletions(-)
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 29e3fde16..d366e8e7b 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -13,8 +13,13 @@
#include "remote.h"
#include "refs.h"
#include "connect.h"
+#include "revision.h"
+#include "diffcore.h"
+#include "diff.h"
#define OPT_QUIET (1 << 0)
+#define OPT_CACHED (1 << 1)
+#define OPT_RECURSIVE (1 << 2)
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
void *cb_data);
@@ -244,6 +249,44 @@ 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)) {
+ strbuf_strip_suffix(&sb, "\n");
+ return strbuf_detach(&sb, NULL);
+ }
+ }
+
+ strbuf_release(&sb);
+ return NULL;
+}
+
struct module_list {
const struct cache_entry **entries;
int alloc, nr;
@@ -503,6 +546,160 @@ static int module_init(int argc, const char **argv, const char *prefix)
return 0;
}
+struct status_cb {
+ const char *prefix;
+ unsigned int flags;
+};
+
+#define STATUS_CB_INIT { NULL, 0 }
+
+static void print_status(unsigned int flags, char state, const char *path,
+ const struct object_id *oid, const char *displaypath)
+{
+ if (flags & OPT_QUIET)
+ return;
+
+ printf("%c%s %s", state, oid_to_hex(oid), displaypath);
+
+ if (state == ' ' || state == '+')
+ printf(" (%s)", compute_rev_name(path, oid_to_hex(oid)));
+
+ printf("\n");
+}
+
+static int handle_submodule_head_ref(const char *refname,
+ const struct object_id *oid, int flags,
+ void *cb_data)
+{
+ struct object_id *output = cb_data;
+ if (oid)
+ oidcpy(output, oid);
+
+ return 0;
+}
+
+static void status_submodule(const char *path, const struct object_id *ce_oid,
+ unsigned int ce_flags, const char *prefix,
+ unsigned int flags)
+{
+ char *displaypath;
+ struct argv_array diff_files_args = ARGV_ARRAY_INIT;
+ struct rev_info rev;
+ int diff_files_result;
+
+ if (!submodule_from_path(&null_oid, path))
+ die(_("no submodule mapping found in .gitmodules for path '%s'"),
+ path);
+
+ displaypath = get_submodule_displaypath(path, prefix);
+
+ if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
+ print_status(flags, 'U', path, &null_oid, displaypath);
+ goto cleanup;
+ }
+
+ if (!is_submodule_active(the_repository, path)) {
+ print_status(flags, '-', path, ce_oid, displaypath);
+ goto cleanup;
+ }
+
+ argv_array_pushl(&diff_files_args, "diff-files",
+ "--ignore-submodules=dirty", "--quiet", "--",
+ path, NULL);
+
+ git_config(git_diff_basic_config, NULL);
+ init_revisions(&rev, prefix);
+ rev.abbrev = 0;
+ diff_files_args.argc = setup_revisions(diff_files_args.argc,
+ diff_files_args.argv,
+ &rev, NULL);
+ diff_files_result = run_diff_files(&rev, 0);
+
+ if (!diff_result_code(&rev.diffopt, diff_files_result)) {
+ print_status(flags, ' ', path, ce_oid,
+ displaypath);
+ } else if (!(flags & OPT_CACHED)) {
+ struct object_id oid;
+
+ if (refs_head_ref(get_submodule_ref_store(path),
+ handle_submodule_head_ref, &oid))
+ die(_("could not resolve HEAD ref inside the"
+ "submodule '%s'"), path);
+
+ print_status(flags, '+', path, &oid, displaypath);
+ } else {
+ print_status(flags, '+', path, ce_oid, displaypath);
+ }
+
+ if (flags & OPT_RECURSIVE) {
+ struct child_process cpr = CHILD_PROCESS_INIT;
+
+ cpr.git_cmd = 1;
+ cpr.dir = path;
+ prepare_submodule_repo_env(&cpr.env_array);
+
+ argv_array_push(&cpr.args, "--super-prefix");
+ argv_array_pushf(&cpr.args, "%s/", displaypath);
+ argv_array_pushl(&cpr.args, "submodule--helper", "status",
+ "--recursive", NULL);
+
+ if (flags & OPT_CACHED)
+ argv_array_push(&cpr.args, "--cached");
+
+ if (flags & OPT_QUIET)
+ argv_array_push(&cpr.args, "--quiet");
+
+ if (run_command(&cpr))
+ die(_("failed to recurse into submodule '%s'"), path);
+ }
+
+cleanup:
+ argv_array_clear(&diff_files_args);
+ free(displaypath);
+}
+
+static void status_submodule_cb(const struct cache_entry *list_item,
+ void *cb_data)
+{
+ struct status_cb *info = cb_data;
+ status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
+ info->prefix, info->flags);
+}
+
+static int module_status(int argc, const char **argv, const char *prefix)
+{
+ struct status_cb info = STATUS_CB_INIT;
+ struct pathspec pathspec;
+ struct module_list list = MODULE_LIST_INIT;
+ int quiet = 0;
+
+ struct option module_status_options[] = {
+ OPT__QUIET(&quiet, N_("Suppress submodule status output")),
+ OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
+ OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
+ OPT_END()
+ };
+
+ const char *const git_submodule_helper_usage[] = {
+ N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
+ NULL
+ };
+
+ argc = parse_options(argc, argv, prefix, module_status_options,
+ git_submodule_helper_usage, 0);
+
+ if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
+ return 1;
+
+ info.prefix = prefix;
+ if (quiet)
+ info.flags |= OPT_QUIET;
+
+ for_each_listed_submodule(&list, status_submodule_cb, &info);
+
+ return 0;
+}
+
static int module_name(int argc, const char **argv, const char *prefix)
{
const struct submodule *sub;
@@ -1300,6 +1497,7 @@ static struct cmd_struct commands[] = {
{"resolve-relative-url", resolve_relative_url, 0},
{"resolve-relative-url-test", resolve_relative_url_test, 0},
{"init", module_init, SUPPORT_SUPER_PREFIX},
+ {"status", module_status, SUPPORT_SUPER_PREFIX},
{"remote-branch", resolve_remote_submodule_branch, 0},
{"push-check", push_check, 0},
{"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
diff --git a/git-submodule.sh b/git-submodule.sh
index 66d1ae8ef..156255a9e 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
#
@@ -1016,54 +1004,7 @@ cmd_status()
shift
done
- {
- git submodule--helper list --prefix "$wt_prefix" "$@" ||
- echo "#unmatched" $?
- } |
- while read -r mode sha1 stage sm_path
- do
- die_if_unmatched "$mode" "$sha1"
- name=$(git submodule--helper name "$sm_path") || exit
- displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
- if test "$stage" = U
- then
- say "U$sha1 $displaypath"
- continue
- fi
- if ! git submodule--helper is-active "$sm_path" ||
- {
- ! test -d "$sm_path"/.git &&
- ! test -f "$sm_path"/.git
- }
- then
- say "-$sha1 $displaypath"
- continue;
- fi
- if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
- then
- set_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"
- say "+$sha1 $displaypath$revname"
- fi
-
- if test -n "$recursive"
- then
- (
- prefix="$displaypath/"
- sanitize_submodule_env
- wt_prefix=
- cd "$sm_path" &&
- eval cmd_status
- ) ||
- die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
- fi
- done
+ git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} "$@"
}
#
# Sync remote urls for submodules
--
2.14.2
next prev parent reply other threads:[~2017-10-06 13:24 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-21 16:15 [GSoC][PATCH 1/4] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-08-21 16:15 ` [GSoC][PATCH 2/4] submodule--helper: introduce for_each_submodule_list() Prathamesh Chavan
2017-08-22 22:37 ` Junio C Hamano
2017-08-21 16:15 ` [GSoC][PATCH 3/4] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-08-21 16:47 ` Heiko Voigt
2017-08-21 17:24 ` Prathamesh Chavan
2017-08-21 16:15 ` [GSoC][PATCH 4/4] submodule: port submodule subcommand 'status' " Prathamesh Chavan
2017-08-22 22:29 ` [GSoC][PATCH 1/4] submodule--helper: introduce get_submodule_displaypath() Junio C Hamano
2017-08-23 18:15 ` [GSoC][PATCH v2 0/4] submodule: Incremental rewrite of git-submodules Prathamesh Chavan
2017-08-23 18:15 ` [GSoC][PATCH v2 1/4] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-08-23 18:15 ` [GSoC][PATCH v2 2/4] submodule--helper: introduce for_each_submodule() Prathamesh Chavan
2017-08-23 19:13 ` Junio C Hamano
2017-08-23 19:31 ` Stefan Beller
2017-08-23 19:52 ` Junio C Hamano
2017-08-24 19:50 ` [GSoC][PATCH v3 0/4] Incremental rewrite of git-submodules Prathamesh Chavan
2017-08-24 19:50 ` [GSoC][PATCH v3 1/4] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-08-24 19:50 ` [GSoC][PATCH v3 2/4] submodule--helper: introduce for_each_listed_submodule() Prathamesh Chavan
2017-08-24 19:50 ` [GSoC][PATCH v3 3/4] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-08-24 19:50 ` [GSoC][PATCH v3 4/4] submodule: port submodule subcommand 'status' " Prathamesh Chavan
2017-08-25 18:51 ` [GSoC][PATCH v3 0/4] Incremental rewrite of git-submodules Junio C Hamano
2017-08-25 19:15 ` Stefan Beller
2017-08-25 20:32 ` Junio C Hamano
2017-08-27 11:50 ` Prathamesh Chavan
2017-08-28 11:55 ` [GSoC][PATCH v4 " Prathamesh Chavan
2017-08-28 11:55 ` [GSoC][PATCH v4 1/4] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-09-21 15:06 ` Han-Wen Nienhuys
2017-08-28 11:55 ` [GSoC][PATCH v4 2/4] submodule--helper: introduce for_each_listed_submodule() Prathamesh Chavan
2017-08-28 11:55 ` [GSoC][PATCH v4 3/4] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-09-21 15:31 ` Han-Wen Nienhuys
2017-08-28 11:55 ` [GSoC][PATCH v4 4/4] submodule: port submodule subcommand 'status' " Prathamesh Chavan
2017-09-21 16:10 ` Han-Wen Nienhuys
2017-09-24 12:08 ` [PATCH v5 0/4] Incremental rewrite of git-submodules Prathamesh Chavan
2017-09-24 12:08 ` [PATCH v5 1/4] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-09-25 3:35 ` Junio C Hamano
2017-09-24 12:08 ` [PATCH v5 2/4] submodule--helper: introduce for_each_listed_submodule() Prathamesh Chavan
2017-09-25 3:43 ` Junio C Hamano
2017-09-24 12:08 ` [PATCH v5 3/4] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-09-25 3:51 ` Junio C Hamano
2017-09-25 3:55 ` Junio C Hamano
2017-09-24 12:08 ` [PATCH v5 4/4] submodule: port submodule subcommand 'status' " Prathamesh Chavan
2017-09-25 5:06 ` Junio C Hamano
2017-09-29 9:44 ` [PATCH v6 0/3] Incremental rewrite of git-submodules Prathamesh Chavan
2017-09-29 9:44 ` [PATCH v6 1/3] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-10-02 0:44 ` Junio C Hamano
2017-09-29 9:44 ` [PATCH v6 2/3] submodule--helper: introduce for_each_listed_submodule() Prathamesh Chavan
2017-10-02 0:55 ` Junio C Hamano
2017-09-29 9:44 ` [PATCH v6 3/3] submodule: port submodule subcommand 'status' from shell to C Prathamesh Chavan
2017-10-02 1:08 ` Junio C Hamano
2017-10-06 13:24 ` [PATCH v7 0/3] Incremental rewrite of git-submodules Prathamesh Chavan
2017-10-06 13:24 ` [PATCH v7 1/3] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-10-06 21:12 ` Eric Sunshine
2017-10-06 13:24 ` [PATCH v7 2/3] submodule--helper: introduce for_each_listed_submodule() Prathamesh Chavan
2017-10-06 21:56 ` Eric Sunshine
2017-10-06 13:24 ` Prathamesh Chavan [this message]
2017-10-07 8:51 ` [PATCH v7 0/3] Incremental rewrite of git-submodules Junio C Hamano
2017-10-07 9:35 ` Eric Sunshine
2017-10-02 0:39 ` [PATCH v6 " Junio C Hamano
2017-08-23 18:15 ` [GSoC][PATCH v2 3/4] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-08-23 18:15 ` [GSoC][PATCH v2 4/4] submodule: port submodule subcommand 'status' " 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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171006132415.2876-4-pc44800@gmail.com \
--to=pc44800@gmail.com \
--cc=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=hanwen@google.com \
--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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).