submodule--helper: refactor "errmsg_str" to be a "struct strbuf"

Refactor code added in e83e3333b57 (submodule: port submodule subcommand 'summary' from shell to C, 2020-08-13) so that "errmsg" and "errmsg_str" are folded into one. The distinction between the empty string and NULL is something that's tested for by e.g. "t/t7401-submodule-summary.sh". This is in preparation for fixing a memory leak the "struct strbuf" in the pre-image. Let's also pass a "const char *" to print_submodule_summary(), as it should not be modifying the "errmsg". Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Reviewed-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Sep 1, 2022 at 01:17 UTC 9d02f9499ffe7af9ce7e449e482820d437f84026
1 file changed +6 -8
builtin/submodule--helper.c
+6 -8
@@ -761,7 +761,7 @@ static char *verify_submodule_committish(const char *sm_path,
761 return strbuf_detach(&result, NULL);
762 }
763
764 -static void print_submodule_summary(struct summary_cb *info, char *errmsg,
764 +static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
765 int total_commits, const char *displaypath,
766 const char *src_abbrev, const char *dst_abbrev,
767 struct module_cb *p)
@@ -819,7 +819,7 @@ static void generate_submodule_summary(struct summary_cb *info,
819 {
820 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
821 int missing_src = 0, missing_dst = 0;
822 - char *errmsg = NULL;
822 + struct strbuf errmsg = STRBUF_INIT;
823 int total_commits = -1;
824
825 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
@@ -920,23 +920,21 @@ static void generate_submodule_summary(struct summary_cb *info,
920 * submodule, i.e., deleted or changed to blob
921 */
922 if (S_ISGITLINK(p->mod_dst)) {
923 - struct strbuf errmsg_str = STRBUF_INIT;
923 if (missing_src && missing_dst) {
925 - strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commits %s and %s\n",
924 + strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
925 displaypath, oid_to_hex(&p->oid_src),
926 oid_to_hex(&p->oid_dst));
927 } else {
929 - strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commit %s\n",
928 + strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
929 displaypath, missing_src ?
930 oid_to_hex(&p->oid_src) :
931 oid_to_hex(&p->oid_dst));
932 }
934 - errmsg = strbuf_detach(&errmsg_str, NULL);
933 }
934 }
935
938 - print_submodule_summary(info, errmsg, total_commits,
939 - displaypath, src_abbrev,
936 + print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
937 + total_commits, displaypath, src_abbrev,
938 dst_abbrev, p);
939
940 free(displaypath);