git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 09/10] gc: use strvec_pushf(), avoid redundant strbuf_detach()

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Oct 14, 2022, 15:40 UTC
Message-ID
<patch-09.10-0f5524e40ad-20221014T153426Z-avarab@gmail.com>
In-Reply-To
<cover-00.10-00000000000-20221014T153426Z-avarab@gmail.com>

Adjust code added in 3797a0a7b7a (maintenance: use Windows scheduled tasks, 2021-01-05) to use strvec_pushf() directly. Rather than having a function that returns a strbuf_detach() we need to free and have the "strvec_pushl()" do its own "xstrdup()" we can format this in-place.

By changing this we only have the "strvec_clear()" between the "run_command_v_opt()" and the "return result" in "schtasks_remove_task()". In the subsequent commit we'll start using a helper which'll allow us to skip the "strvec_clear()".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/gc.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c
index 075b4637660..519e64e86ee 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -1894,12 +1894,7 @@ static int is_schtasks_available(void)
 #endif
 }
 
-static char *schtasks_task_name(const char *frequency)
-{
-	struct strbuf label = STRBUF_INIT;
-	strbuf_addf(&label, "Git Maintenance (%s)", frequency);
-	return strbuf_detach(&label, NULL);
-}
+#define SCHTASKS_NAME_FMT "Git Maintenance (%s)"
 
 static int schtasks_remove_task(enum schedule_priority schedule)
 {
@@ -1907,16 +1902,15 @@ static int schtasks_remove_task(enum schedule_priority schedule)
 	int result;
 	struct strvec args = STRVEC_INIT;
 	const char *frequency = get_frequency(schedule);
-	char *name = schtasks_task_name(frequency);
 
 	get_schedule_cmd(&cmd, NULL);
 	strvec_split(&args, cmd);
-	strvec_pushl(&args, "/delete", "/tn", name, "/f", NULL);
+	strvec_pushl(&args, "/delete", "/tn", NULL);
+	strvec_pushf(&args, SCHTASKS_NAME_FMT, frequency);
+	strvec_pushl(&args, "/f", NULL);
 
 	result = run_command_v_opt(args.v, 0);
-
 	strvec_clear(&args);
-	free(name);
 	return result;
 }
 
@@ -1935,7 +1929,6 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
 	const char *xml;
 	struct tempfile *tfile;
 	const char *frequency = get_frequency(schedule);
-	char *name = schtasks_task_name(frequency);
 	struct strbuf tfilename = STRBUF_INIT;
 
 	get_schedule_cmd(&cmd, NULL);
@@ -2028,8 +2021,10 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
 	      "</Task>\n";
 	fprintf(tfile->fp, xml, exec_path, exec_path, frequency);
 	strvec_split(&child.args, cmd);
-	strvec_pushl(&child.args, "/create", "/tn", name, "/f", "/xml",
-				  get_tempfile_path(tfile), NULL);
+	strvec_pushl(&child.args, "/create", "/tn", NULL);
+	strvec_pushf(&child.args, SCHTASKS_NAME_FMT, frequency);
+	strvec_pushl(&child.args, "/f", "/xml",
+		     get_tempfile_path(tfile), NULL);
 	close_tempfile_gently(tfile);
 
 	child.no_stdout = 1;
@@ -2040,7 +2035,6 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
 	result = finish_command(&child);
 
 	delete_tempfile(&tfile);
-	free(name);
 	return result;
 }
 
-- 
2.38.0.1092.g8c0298861b0
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 16 of 39 in “run-command API: add run_command_{l,sv}_opt()”
  1. 00/10 run-command API: add run_command_{l,sv}_opt()Ævar Arnfjörð Bjarmason, Oct 14, 2022
  2. 01/10 run-command.c: refactor run_command_*_tr2() to internal helpersÆvar Arnfjörð Bjarmason, Oct 14, 2022
  3. Junio C HamanoOct 14, 2022
  4. 02/10 merge: remove always-the-same "verbose" argumentsÆvar Arnfjörð Bjarmason, Oct 14, 2022
  5. Junio C HamanoOct 14, 2022
  6. 03/10 run-command API: add and use a run_command_l_opt()Ævar Arnfjörð Bjarmason, Oct 14, 2022
  7. Junio C HamanoOct 14, 2022
  8. Jeff KingOct 14, 2022
  9. Junio C HamanoOct 14, 2022
  10. 04/10 am: use run_command_l_opt() for show_patch()Ævar Arnfjörð Bjarmason, Oct 14, 2022
  11. Junio C HamanoOct 14, 2022
  12. 05/10 run-command API docs: clarify & fleshen out run_command_v_opt*() docsÆvar Arnfjörð Bjarmason, Oct 14, 2022
  13. 06/10 run-command API: remove RUN_COMMAND_STDOUT_TO_STDERR flagÆvar Arnfjörð Bjarmason, Oct 14, 2022
  14. 08/10 run-command API & users: remove run_command_v_opt_tr2()Ævar Arnfjörð Bjarmason, Oct 14, 2022
  15. 07/10 run-command API & diff.c: remove run_command_v_opt_cd_env()Ævar Arnfjörð Bjarmason, Oct 14, 2022
  16. 09/10 gc: use strvec_pushf(), avoid redundant strbuf_detach()Ævar Arnfjörð Bjarmason, Oct 14, 2022
  17. 10/10 run-command API: add and use a run_command_sv_opt()Ævar Arnfjörð Bjarmason, Oct 14, 2022
  18. René ScharfeOct 14, 2022
  19. 00/10 run-command API: add run_command_{l,sv}_opt()Ævar Arnfjörð Bjarmason, Oct 17, 2022
  20. 01/10 run-command.c: refactor run_command_*_tr2() to internal helpersÆvar Arnfjörð Bjarmason, Oct 17, 2022
  21. 02/10 merge: remove always-the-same "verbose" argumentsÆvar Arnfjörð Bjarmason, Oct 17, 2022
  22. 04/10 am: use run_command_l_opt() for show_patch()Ævar Arnfjörð Bjarmason, Oct 17, 2022
  23. 03/10 run-command API: add and use a run_command_l_opt()Ævar Arnfjörð Bjarmason, Oct 17, 2022
  24. 05/10 run-command API docs: clarify & fleshen out run_command_v_opt*() docsÆvar Arnfjörð Bjarmason, Oct 17, 2022
  25. 06/10 run-command API: remove RUN_COMMAND_STDOUT_TO_STDERR flagÆvar Arnfjörð Bjarmason, Oct 17, 2022
  26. 07/10 run-command API & diff.c: remove run_command_v_opt_cd_env()Ævar Arnfjörð Bjarmason, Oct 17, 2022
  27. 08/10 run-command API & users: remove run_command_v_opt_tr2()Ævar Arnfjörð Bjarmason, Oct 17, 2022
  28. 10/10 run-command API: add and use a run_command_sv_opt()Ævar Arnfjörð Bjarmason, Oct 17, 2022
  29. 09/10 gc: use strvec_pushf(), avoid redundant strbuf_detach()Ævar Arnfjörð Bjarmason, Oct 17, 2022
  30. Junio C HamanoOct 18, 2022
  31. Ævar Arnfjörð BjarmasonOct 18, 2022
  32. Jeff KingOct 18, 2022
  33. Junio C HamanoOct 19, 2022
  34. Jeff KingOct 19, 2022
  35. Junio C HamanoOct 19, 2022
  36. Jeff KingOct 19, 2022
  37. Junio C HamanoOct 19, 2022
  38. René ScharfeOct 20, 2022
  39. Junio C HamanoOct 20, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.