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

[PATCH 5/6] get_exporter: use argv_array

From
Jeff King <peff@peff.net>
Date
May 15, 2014, 08:34 UTC
Message-ID
<20140515083444.GE26866@sigill.intra.peff.net>
In-Reply-To
<20140515082943.GA26473@sigill.intra.peff.net>

This simplifies the code and avoids a fixed array size that we might accidentally overflow. It also prevents a leak after finish_command is run, by using the argv_array that run-command manages for us.

Signed-off-by: Jeff King <peff@peff.net>
---
 transport-helper.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)
diff --git a/transport-helper.c b/transport-helper.c
index fefd34f..9f8f3b1 100644
--- a/transport-helper.c
+++ b/transport-helper.c
@@ -418,30 +418,24 @@ static int get_exporter(struct transport *transport,
 {
 	struct helper_data *data = transport->data;
 	struct child_process *helper = get_helper(transport);
-	int argc = 0, i;
-	struct strbuf tmp = STRBUF_INIT;
+	int i;
 
 	memset(fastexport, 0, sizeof(*fastexport));
 
 	/* we need to duplicate helper->in because we want to use it after
 	 * fastexport is done with it. */
 	fastexport->out = dup(helper->in);
-	fastexport->argv = xcalloc(6 + revlist_args->nr, sizeof(*fastexport->argv));
-	fastexport->argv[argc++] = "fast-export";
-	fastexport->argv[argc++] = "--use-done-feature";
-	fastexport->argv[argc++] = data->signed_tags ?
-		"--signed-tags=verbatim" : "--signed-tags=warn-strip";
-	if (data->export_marks) {
-		strbuf_addf(&tmp, "--export-marks=%s.tmp", data->export_marks);
-		fastexport->argv[argc++] = strbuf_detach(&tmp, NULL);
-	}
-	if (data->import_marks) {
-		strbuf_addf(&tmp, "--import-marks=%s", data->import_marks);
-		fastexport->argv[argc++] = strbuf_detach(&tmp, NULL);
-	}
+	argv_array_push(&fastexport->args, "fast-export");
+	argv_array_push(&fastexport->args, "--use-done-feature");
+	argv_array_push(&fastexport->args, data->signed_tags ?
+		"--signed-tags=verbatim" : "--signed-tags=warn-strip");
+	if (data->export_marks)
+		argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
+	if (data->import_marks)
+		argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
 
 	for (i = 0; i < revlist_args->nr; i++)
-		fastexport->argv[argc++] = revlist_args->items[i].string;
+		argv_array_push(&fastexport->args, revlist_args->items[i].string);
 
 	fastexport->git_cmd = 1;
 	return start_command(fastexport);
-- 
2.0.0.rc1.436.g03cb729
Previous: Jeff KingNext: Jeff King
Message 6 of 9 in “build argv_array into run-command”
  1. 0/6 build argv_array into run-commandJeff King, May 15, 2014
  2. 1/6 run-command: store an optional argv_arrayJeff King, May 15, 2014
  3. 2/6 run_column_filter: use argv_arrayJeff King, May 15, 2014
  4. 3/6 git_connect: use argv_arrayJeff King, May 15, 2014
  5. 4/6 get_helper: use run-command's internal argv_arrayJeff King, May 15, 2014
  6. 5/6 get_exporter: use argv_arrayJeff King, May 15, 2014
  7. 6/6 get_importer: use run-command's internal argv_arrayJeff King, May 15, 2014
  8. 7/6 argv-array: drop "detach" codeJeff King, May 15, 2014
  9. Junio C HamanoMay 15, 2014

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.