From: Christian Couder <christian.couder@gmail.com>
To: Prathamesh Chavan <pc44800@gmail.com>
Cc: git <git@vger.kernel.org>, Stefan Beller <sbeller@google.com>
Subject: Re: [GSoC][PATCH 8/8] submodule: port submodule subcommand 'summary' from shell to C
Date: Wed, 19 Jul 2017 16:53:15 +0200 [thread overview]
Message-ID: <CAP8UFD3ySECbyzCooSLetSk-qdnjj1kpvcjVMAO8oGmsFYsEMQ@mail.gmail.com> (raw)
In-Reply-To: <20170718204904.3768-9-pc44800@gmail.com>
On Tue, Jul 18, 2017 at 10:49 PM, Prathamesh Chavan <pc44800@gmail.com> wrote:
> +static void print_submodule_summary(struct summary_cb *info,
> + struct module_cb *p)
> +{
> + int missing_src = 0;
> + int missing_dst = 0;
> + char *displaypath;
> + char *sha1_abbr_src;
> + char *sha1_abbr_dst;
> + int errmsg = 0;
> + int total_commits = -1;
> + struct strbuf sb_sha1_src = STRBUF_INIT;
> + struct strbuf sb_sha1_dst = STRBUF_INIT;
> + char *sha1_dst = oid_to_hex(&p->oid_dst);
> + char *sha1_src = oid_to_hex(&p->oid_src);
> + char *sm_git_dir = xstrfmt("%s/.git", p->sm_path);
> + int is_sm_git_dir = 0;
> +
> + if (!info->cached && !strcmp(sha1_dst, sha1_to_hex(null_sha1))) {
> + if (S_ISGITLINK(p->mod_dst)) {
> + struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
> + struct strbuf sb_rev_parse = STRBUF_INIT;
> +
> + cp_rev_parse.git_cmd = 1;
> + cp_rev_parse.no_stderr = 1;
> + cp_rev_parse.dir = p->sm_path;
> + prepare_submodule_repo_env(&cp_rev_parse.env_array);
> +
> + argv_array_pushl(&cp_rev_parse.args,
> + "rev-parse", "HEAD", NULL);
> + if (!capture_command(&cp_rev_parse, &sb_rev_parse, 0)) {
> + strbuf_strip_suffix(&sb_rev_parse, "\n");
> + sha1_dst = xstrdup(sb_rev_parse.buf);
> + }
> + strbuf_release(&sb_rev_parse);
> + } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
> + struct child_process cp_hash_object = CHILD_PROCESS_INIT;
> + struct strbuf sb_hash_object = STRBUF_INIT;
> +
> + cp_hash_object.git_cmd = 1;
> + argv_array_pushl(&cp_hash_object.args,
> + "hash-object", p->sm_path,
> + NULL);
> + if (!capture_command(&cp_hash_object,
> + &sb_hash_object, 0)) {
> + strbuf_strip_suffix(&sb_hash_object, "\n");
> + sha1_dst = xstrdup(sb_hash_object.buf);
> + }
> + strbuf_release(&sb_hash_object);
> + } else {
> + if (p->mod_dst != 0)
Maybe: if (p->mod_dst)
> + die(_("unexpected mode %d\n"), p->mod_dst);
> + }
> + }
> +
> + if (is_git_directory(sm_git_dir))
> + is_sm_git_dir = 1;
> +
> + if (is_sm_git_dir && S_ISGITLINK(p->mod_src)) {
> + struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
> +
> + cp_rev_parse.git_cmd = 1;
> + cp_rev_parse.no_stdout = 1;
> + cp_rev_parse.dir = p->sm_path;
> + prepare_submodule_repo_env(&cp_rev_parse.env_array);
> +
> + argv_array_pushl(&cp_rev_parse.args, "rev-parse", "-q",
> + "--verify", NULL);
> + argv_array_pushf(&cp_rev_parse.args, "%s^0", sha1_src);
> +
> + if (run_command(&cp_rev_parse))
> + missing_src = 1;
> + }
> +
> + if (is_sm_git_dir && S_ISGITLINK(p->mod_dst)) {
> + struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
> +
> + cp_rev_parse.git_cmd = 1;
> + cp_rev_parse.no_stdout = 1;
> + cp_rev_parse.dir = p->sm_path;
> + prepare_submodule_repo_env(&cp_rev_parse.env_array);
> +
> + argv_array_pushl(&cp_rev_parse.args, "rev-parse", "-q",
> + "--verify", NULL);
> + argv_array_pushf(&cp_rev_parse.args, "%s^0", sha1_dst);
> +
> + if (run_command(&cp_rev_parse))
> + missing_dst = 1;
> + }
The code of the 2 big if() { } clauses above look very similar and
could be factorized to avoid duplicating code.
[...]
> + if (errmsg) {
> + /*
> + * Don't give error msg for modification whose dst is not
> + * submodule, i.e. deleted or changed to blob
> + */
> + if (S_ISGITLINK(p->mod_src)) {
> + if (missing_src && missing_dst) {
> + printf(_(" Warn: %s doesn't contain commits %s and %s\n"),
> + displaypath, sha1_src, sha1_dst);
> + } else if (missing_src) {
> + printf(_(" Warn: %s doesn't contain commit %s\n"),
> + displaypath, sha1_src);
> + } else {
> + printf(_(" Warn: %s doesn't contain commit %s\n"),
> + displaypath, sha1_dst);
> + }
> + }
> +
Spurious new line.
> + } else if (is_sm_git_dir) {
> + if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
> + struct child_process cp_log = CHILD_PROCESS_INIT;
> + char *limit = NULL;
> +
> + if (info->summary_limits > 0)
> + limit = xstrfmt("-%d", info->summary_limits);
> +
> + cp_log.git_cmd = 1;
> + cp_log.dir = p->sm_path;
> + prepare_submodule_repo_env(&cp_log.env_array);
> +
> + argv_array_pushl(&cp_log.args, "log", NULL);
> + if (limit)
> + argv_array_push(&cp_log.args, limit);
> + argv_array_pushl(&cp_log.args, "--pretty= %m %s",
> + "--first-parent", NULL);
> + argv_array_pushf(&cp_log.args, "%s...%s", sha1_src,
> + sha1_dst);
> +
> + run_command(&cp_log);
> +
Spurious new line.
> + } else if (S_ISGITLINK(p->mod_dst)) {
> + struct child_process cp_log = CHILD_PROCESS_INIT;
> +
> + cp_log.git_cmd = 1;
> + cp_log.dir = p->sm_path;
> + prepare_submodule_repo_env(&cp_log.env_array);
> +
> + argv_array_pushl(&cp_log.args, "log",
> + "--pretty= > %s", "-1",
> + sha1_dst, NULL);
> +
> + run_command(&cp_log);
> + } else {
> + struct child_process cp_log = CHILD_PROCESS_INIT;
> +
> + cp_log.git_cmd = 1;
> + cp_log.dir = p->sm_path;
> + prepare_submodule_repo_env(&cp_log.env_array);
> +
> + argv_array_pushl(&cp_log.args, "log",
> + "--pretty= < %s",
> + "-1", sha1_src, NULL);
> +
> + run_command(&cp_log);
> + }
It looks like you could factorize the big if () { } else { } clause
above using something like:
if (S_ISGITLINK(p->mod_dst))
argv_array_pushl(&cp_log.args, "log",
"--pretty= > %s", "-1",
sha1_dst, NULL);
else
argv_array_pushl(&cp_log.args, "log",
"--pretty= < %s", "-1",
sha1_src, NULL);
> + }
> + printf("\n");
> +}
> +
> +static void prepare_submodule_summary(struct summary_cb *info,
> + struct module_cb_list *list)
> +{
> + int i;
> + for (i = 0; i < list->nr; i++) {
> + struct module_cb *p = list->entries[i];
> + struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
> +
> + if (p->status == 'D' || p->status == 'T') {
> + print_submodule_summary(info, p);
> + continue;
> + }
> +
> + if (info->for_status) {
> + char *config_key;
> + const char *ignore_config = "none";
> + const char *value;
> + const struct submodule *sub = submodule_from_path(null_sha1, p->sm_path);
> +
> + if (sub) {
> + config_key = xstrfmt("submodule.%s.ignore",
> + sub->name);
> + if (!git_config_get_value(config_key, &value))
> + ignore_config = value;
> + else if (sub->ignore)
> + ignore_config = sub->ignore;
> +
> + if (p->status != 'A' && !strcmp(ignore_config,
> + "all"))
Maybe in the case where p->status == 'A' we could avoid computing ignore_config.
> + continue;
> + }
It looks like config_key is not freed.
> + }
[...]
> + cp_rev.git_cmd = 1;
> + argv_array_pushl(&cp_rev.args, "rev-parse", "-q", "--verify",
> + NULL);
> + if (argc)
> + argv_array_push(&cp_rev.args, argv[0]);
> + else
> + argv_array_pushl(&cp_rev.args, "HEAD", NULL);
Maybe the last 4 lines replaced with:
argv_array_push(&cp_rev.args, argc ? argv[0] : "HEAD");
or even the last 6 lines with:
argv_array_pushl(&cp_rev.args, "rev-parse", "-q", "--verify",
argc ? argv[0] : "HEAD", NULL);
> + if (!capture_command(&cp_rev, &sb_rev, 0)) {
> + strbuf_strip_suffix(&sb_rev, "\n");
> + head = xstrdup(sb_rev.buf);
It looks like "head" is not freed below.
I wonder if the "head" variable is really needed. Couldn't "sb_rev" be
used all along?
next prev parent reply other threads:[~2017-07-19 14:53 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-18 20:48 [GSoC][PATCH 0/8] Update: Week 9 Prathamesh Chavan
2017-07-18 20:48 ` [GSoC][PATCH 1/8] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-07-18 20:48 ` [GSoC][PATCH 2/8] submodule--helper: introduce for_each_submodule_list() Prathamesh Chavan
2017-07-18 20:48 ` [GSoC][PATCH 3/8] submodule: port set_name_rev() from shell to C Prathamesh Chavan
2017-07-18 20:49 ` [GSoC][PATCH 4/8] submodule: port submodule subcommand 'status' " Prathamesh Chavan
2017-07-18 21:39 ` Stefan Beller
2017-07-18 22:32 ` Junio C Hamano
2017-07-18 22:44 ` Stefan Beller
2017-07-18 22:47 ` Junio C Hamano
2017-07-18 20:49 ` [GSoC][PATCH 5/8] submodule: port submodule subcommand 'sync' " Prathamesh Chavan
2017-07-18 22:23 ` Stefan Beller
2017-07-20 19:36 ` Prathamesh Chavan
2017-07-20 19:57 ` Stefan Beller
2017-07-18 20:49 ` [GSoC][PATCH 6/8] submodule: port submodule subcommand 'deinit' " Prathamesh Chavan
2017-07-19 14:04 ` Christian Couder
2017-07-18 20:49 ` [GSoC][PATCH 7/8] diff: change scope of the function count_lines() Prathamesh Chavan
2017-07-18 20:49 ` [GSoC][PATCH 8/8] submodule: port submodule subcommand 'summary' from shell to C Prathamesh Chavan
2017-07-19 14:53 ` Christian Couder [this message]
-- strict thread matches above, loose matches on Subject: below --
2017-07-10 22:45 [GSoC] Update: Week 8 Prathamesh Chavan
2017-07-10 22:54 ` [GSoC][PATCH 1/8] submodule--helper: introduce get_submodule_displaypath() Prathamesh Chavan
2017-07-10 22:54 ` [GSoC][PATCH 8/8] submodule: port submodule subcommand 'summary' from shell to C 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=CAP8UFD3ySECbyzCooSLetSk-qdnjj1kpvcjVMAO8oGmsFYsEMQ@mail.gmail.com \
--to=christian.couder@gmail.com \
--cc=git@vger.kernel.org \
--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).