git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Prathamesh Chavan <pc44800@gmail.com>
Cc: hanwen@google.com, christian.couder@gmail.com,
	git@vger.kernel.org, sbeller@google.com
Subject: Re: [PATCH v5 4/4] submodule: port submodule subcommand 'status' from shell to C
Date: Mon, 25 Sep 2017 14:06:58 +0900	[thread overview]
Message-ID: <xmqq4lrrfjt9.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <20170924120858.26813-5-pc44800@gmail.com> (Prathamesh Chavan's message of "Sun, 24 Sep 2017 17:38:58 +0530")

Prathamesh Chavan <pc44800@gmail.com> writes:

> 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
> three functions: module_status(), submodule_status() and print_status().

Well then it does not just aim to, but it does, doesn't it ;-)?

> @@ -559,6 +544,151 @@ static int module_init(int argc, const char **argv, const char *prefix)
>  	return 0;
>  }
>  
> +struct status_cb {
> +	const char *prefix;
> +	unsigned int quiet: 1;
> +	unsigned int recursive: 1;
> +	unsigned int cached: 1;
> +};
> +#define STATUS_CB_INIT { NULL, 0, 0, 0 }
> +
> +static void print_status(struct status_cb *info, char state, const char *path,
> +			 const struct object_id *oid, const char *displaypath)
> +{
> +	if (info->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;
> +}

All of the above look sensible.

> +static void status_submodule(const struct cache_entry *list_item, void *cb_data)
> +{
> +	struct status_cb *info = cb_data;
> +	char *displaypath;
> +	struct argv_array diff_files_args = ARGV_ARRAY_INIT;
> +
> +	if (!submodule_from_path(&null_oid, list_item->name))
> +		die(_("no submodule mapping found in .gitmodules for path '%s'"),
> +		      list_item->name);
> +
> +	displaypath = get_submodule_displaypath(list_item->name, info->prefix);
> +
> +	if (ce_stage(list_item)) {
> +		print_status(info, 'U', list_item->name,
> +			     &null_oid, displaypath);
> +		goto cleanup;
> +	}
> +
> +	if (!is_submodule_active(the_repository, list_item->name)) {
> +		print_status(info, '-', list_item->name, &list_item->oid,
> +			     displaypath);
> +		goto cleanup;
> +	}
> +
> +	argv_array_pushl(&diff_files_args, "diff-files",
> +			 "--ignore-submodules=dirty", "--quiet", "--",
> +			 list_item->name, NULL);
> +
> +	if (!cmd_diff_files(diff_files_args.argc, diff_files_args.argv,
> +			    info->prefix)) {

Calling any cmd_foo() from places other than git.c, especially if it
is done more than once, is a source of bug.  I didn't check closely
if cmd_diff_files() currently has any of the potential problems, but
these functions are free to (1) modify global or file-scope static
variables, assuming that they will not be called again, leaving
these variables in a state different from the original and making
cmd_foo() unsuitable to be called twice, and (2) exit instead of
return at the end, among other things.

The functions in diff-lib.c (I think run_diff_files() for this
application) are designed to be called multiple times as library-ish
helpers; this caller should be using them instead.

> +		print_status(info, ' ', list_item->name, &list_item->oid,
> +			     displaypath);
> +	} else {
> +		if (!info->cached) {

By using "else if (!info->cached) {" here, you can reduce the
nesting level, perhaps?

> +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;
> +	int cached = 0;
> +	int recursive = 0;
> +
> +	struct option module_status_options[] = {
> +		OPT__QUIET(&quiet, N_("Suppress submodule status output")),
> +		OPT_BOOL(0, "cached", &cached, N_("Use commit stored in the index instead of the one stored in the submodule HEAD")),
> +		OPT_BOOL(0, "recursive", &recursive, N_("Recurse into nested submodules")),
> +		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;
> +	info.quiet = !!quiet;
> +	info.recursive = !!recursive;
> +	info.cached = !!cached;
> +
> +	for_each_listed_submodule(&list, status_submodule, &info);
> +
> +	return 0;
> +}

The same comment as [2/4] on "init_submodule()?  don't you want
init_submodule_cb() instead?" applies to "status_submodule()".  I
suspect that in the future we would want more direct calls to show
the status of one single submodule, without indirectly controlling
what is chosen via the &list interface, and if that is the case,
you'd want the interface into the leaf level helper function to be a
more direct one that is not based on "void *cb_data", with a thin
wrapper around it that is to be used as the callback function by
for_each_listed_submodule().

Other than that, looks very cleanly done.

Thanks.

  reply	other threads:[~2017-09-25  5:07 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 [this message]
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                                     ` [PATCH v7 3/3] submodule: port submodule subcommand 'status' from shell to C Prathamesh Chavan
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=xmqq4lrrfjt9.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=hanwen@google.com \
    --cc=pc44800@gmail.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).