From patchwork Mon Oct 17 17:49:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009184 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97D72C4332F for ; Mon, 17 Oct 2022 17:49:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230198AbiJQRtc (ORCPT ); Mon, 17 Oct 2022 13:49:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230119AbiJQRt3 (ORCPT ); Mon, 17 Oct 2022 13:49:29 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B95AA6241 for ; Mon, 17 Oct 2022 10:49:27 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id w18so19588776wro.7 for ; Mon, 17 Oct 2022 10:49:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=faxuVvn15f5FM2R9CndWqssCYLLiEtQwAFTqilyjCeM=; b=L0gmQPqkxsr6QpVAl2T3GrSmDrKB/MVKQo6gFGqQ2w6jdmT7VJ+0EF348Om2hv4U1v 2UlgTVHXLegCwoaFEBdZe9pmCYdt4uU51SsklLKYYG5ji7H5fuqBem3KJqiel+/xr6Yr KC/9REMuPR82+s2T3XkiKyHQPKE1xCK13Oj6JQ1qZtFyEM9+JxhMsimOacz6dzStKHnu 2GbYVtsv95zHIZiYEVLB1UZjCl7BUOS2yxSfbyboxAlVNYmdTPXCeDmholVk1c9aAZiS dpWOOtrYbRawnBsZPpjEaECFaRD00gibLQ9b6AizxitXpHOLow0Ytcmm1qYVgdDeI6hL 9jbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=faxuVvn15f5FM2R9CndWqssCYLLiEtQwAFTqilyjCeM=; b=oNrsQ9wuxdfIYr61r+mMPw/yf1JkEVMWa/R2YtG9v6t42JY9vA4zaxBh/Z9fXcZ895 EDXJbFd70zBSAo/kyFutDY+d7od5gTHFW7rOEw9mwHNtNQP6wEtZ/1TFj7Mup2awYwwT lTrT5xjHGz8E1V/4EgmM1dvq+eTcZ+F7LWYRmdn7tKSkWbmePgOvonqohNeV5p355lWz NF4E6PvthTXx3P9lPo54x6XWxuoQGTjUt5e1dhT1Dw3i4iJv/mQiOkikObEVQopV1ztB 7UgKwguzroJQlioIznYKpSyrRBuNeXTRt+LEF3NWTwIwbJAub8/uSn3HplbyDTAHYASM WNSA== X-Gm-Message-State: ACrzQf0ye7K+UIhgGbkqEd6pnjpx/otvLqK/X/u+rzREMlZVHCQQCy7y biznQb13T4Cr1t+vnJ+3HhE5dLGNhLDv4Q== X-Google-Smtp-Source: AMsMyM7Mxkz+o+NpIXe12nlvgR48mZeyrDvrvRFwTvYdqrvpWvSyB322OGAHJf3lazjg9CbrCw7ryw== X-Received: by 2002:a5d:6e8e:0:b0:21d:ea5:710f with SMTP id k14-20020a5d6e8e000000b0021d0ea5710fmr6895269wrz.48.1666028965946; Mon, 17 Oct 2022 10:49:25 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:24 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 01/10] run-command.c: refactor run_command_*_tr2() to internal helpers Date: Mon, 17 Oct 2022 19:49:12 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Refactor the run_command_v_opt_cd_env_tr2() function to helpers which takes a "struct child_process *cmd" argument. This will allow for adding a helper which won't need to copy a set of arguments to the "cmd.args" we'd otherwise have to populate from the "argv". Splitting out the part of the function that sets the various members from "opt" will help in the subsequent commit. Signed-off-by: Ævar Arnfjörð Bjarmason --- run-command.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/run-command.c b/run-command.c index 5ec3a46dccf..4e75f83b5e7 100644 --- a/run-command.c +++ b/run-command.c @@ -1004,6 +1004,24 @@ int run_command(struct child_process *cmd) return finish_command(cmd); } +static void run_command_set_opts(struct child_process *cmd, int opt) +{ + cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; + cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0; + cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; + cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; + cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0; + cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; + cmd->wait_after_clean = opt & RUN_WAIT_AFTER_CLEAN ? 1 : 0; + cmd->close_object_store = opt & RUN_CLOSE_OBJECT_STORE ? 1 : 0; +} + +static int run_command_v_opt_1(struct child_process *cmd, int opt) +{ + run_command_set_opts(cmd, opt); + return run_command(cmd); +} + int run_command_v_opt(const char **argv, int opt) { return run_command_v_opt_cd_env(argv, opt, NULL, NULL); @@ -1024,19 +1042,11 @@ int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, { struct child_process cmd = CHILD_PROCESS_INIT; strvec_pushv(&cmd.args, argv); - cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; - cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0; - cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; - cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; - cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0; - cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; - cmd.wait_after_clean = opt & RUN_WAIT_AFTER_CLEAN ? 1 : 0; - cmd.close_object_store = opt & RUN_CLOSE_OBJECT_STORE ? 1 : 0; cmd.dir = dir; if (env) strvec_pushv(&cmd.env, (const char **)env); cmd.trace2_child_class = tr2_class; - return run_command(&cmd); + return run_command_v_opt_1(&cmd, opt); } #ifndef NO_PTHREADS From patchwork Mon Oct 17 17:49:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0556AC4332F for ; Mon, 17 Oct 2022 17:49:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230291AbiJQRtf (ORCPT ); Mon, 17 Oct 2022 13:49:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbiJQRtb (ORCPT ); Mon, 17 Oct 2022 13:49:31 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFCA1A47C for ; Mon, 17 Oct 2022 10:49:28 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id a3so19680727wrt.0 for ; Mon, 17 Oct 2022 10:49:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z6Dl86hj3Vupae3jBYIAt/oYs9QyoZVSqsiEHcykyAc=; b=EnBQx2nHvryWL+1I2wzPiSif0Ax4KerwUOGErgRi+AAXXT4fXAf9mwXXv5bAu1BFd1 H1FB+nCI1E5xhexelN2LfqCpGJUG3Rf9eXkS/mLB8xVe1r+wrsK/RMIR0VDO6UOCnByC ZmczaZ3f6pdug5TxWRNaxT1+qnslPGhaK4KsHwJqggopQJBXZ7/aNy7yZQMvNYc4fqVA 4DVuLooeoun8/8/w8fynnn5nrKM2oPp2AeG5MfSD41PUA5fAZ4oOpRNKqobZEqAVmE+D K2C5djFjv+duiQ+GmrP6CYf/W7LwRVkB8h1owTOdUF8fqQFKUObpnGF+7H98/I3A29yy tzqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z6Dl86hj3Vupae3jBYIAt/oYs9QyoZVSqsiEHcykyAc=; b=BPu9rb9AShBNxGjIGwj5/vEpQAvk0RZQYUKNGlFX8joEYahxaXHA5OclOCJb9PmnGQ FlHMAtCxZn5I4TEalnH7mZwPEkOCjesy/Qyi1s92qh3rgEdAhre6ATqx8i9OUxaLX0on aKBrSzYfySZHcD60oPhV2Da8YF4pGfGKbhuLthm1fk0ixVJMbxHnm8XrfBuL4s9FBT3z R6TaEeUR4pbeCWMB2z79jAnuUL+/QFoDQot1NM3xPzMDXmSexVJ06ExUSEsEyuROLd+4 7/nOYHAiqEeI51xzdWyRLGFP2UKAaAwcuY0zGBNqC4fNLbstUWcjAW4VDfygQXATOZAf JO5g== X-Gm-Message-State: ACrzQf33xMs1bgFBIqTwcQGsHA376j1JGdrWDzAg7Nm2O+kdRNHUyBst lQXIZaSdnbfvWK4s+GIzHAggGw76UU1i9A== X-Google-Smtp-Source: AMsMyM5VD7dab6dPeArO28gkG4bDq5pls3pIeygIBDtMj8XvSqNLc/EV6hC0LKIzAjYD+Keybx9gsA== X-Received: by 2002:a5d:6483:0:b0:22e:4804:8be4 with SMTP id o3-20020a5d6483000000b0022e48048be4mr6666903wri.528.1666028966995; Mon, 17 Oct 2022 10:49:26 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:26 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 02/10] merge: remove always-the-same "verbose" arguments Date: Mon, 17 Oct 2022 19:49:13 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Simplify the code that builds the arguments for the "read-tree" invocation in reset_hard() and read_empty() to remove the "verbose" parameter. Before 172b6428d06 (do not overwrite untracked during merge from unborn branch, 2010-11-14) there was a "reset_hard()" function that would be called in two places, one of those passed a "verbose=1", the other a "verbose=0". After 172b6428d06 when read_empty() was split off from reset_hard() both of these functions only had one caller. The "verbose" in read_empty() would always be false, and the one in reset_hard() would always be true. There was never a good reason for the code to act this way, it happened because the read_empty() function was a copy/pasted and adjusted version of reset_hard(). Since we're no longer conditionally adding the "-v" parameter here (and we'd only add it for "reset_hard()" we'll be able to move to a simpler and safer run-command API in the subsequent commit. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/merge.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/builtin/merge.c b/builtin/merge.c index 5900b81729d..3bb49d805b4 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -345,14 +345,12 @@ static int save_state(struct object_id *stash) return rc; } -static void read_empty(const struct object_id *oid, int verbose) +static void read_empty(const struct object_id *oid) { int i = 0; const char *args[7]; args[i++] = "read-tree"; - if (verbose) - args[i++] = "-v"; args[i++] = "-m"; args[i++] = "-u"; args[i++] = empty_tree_oid_hex(); @@ -363,14 +361,13 @@ static void read_empty(const struct object_id *oid, int verbose) die(_("read-tree failed")); } -static void reset_hard(const struct object_id *oid, int verbose) +static void reset_hard(const struct object_id *oid) { int i = 0; const char *args[6]; args[i++] = "read-tree"; - if (verbose) - args[i++] = "-v"; + args[i++] = "-v"; args[i++] = "--reset"; args[i++] = "-u"; args[i++] = oid_to_hex(oid); @@ -385,7 +382,7 @@ static void restore_state(const struct object_id *head, { struct strvec args = STRVEC_INIT; - reset_hard(head, 1); + reset_hard(head); if (is_null_oid(stash)) goto refresh_cache; @@ -1470,7 +1467,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) check_trust_level); remote_head_oid = &remoteheads->item->object.oid; - read_empty(remote_head_oid, 0); + read_empty(remote_head_oid); update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR); goto done; From patchwork Mon Oct 17 17:49:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009188 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73558C43217 for ; Mon, 17 Oct 2022 17:49:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230441AbiJQRto (ORCPT ); Mon, 17 Oct 2022 13:49:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230253AbiJQRtc (ORCPT ); Mon, 17 Oct 2022 13:49:32 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 000472DF4 for ; Mon, 17 Oct 2022 10:49:29 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id bk15so19548411wrb.13 for ; Mon, 17 Oct 2022 10:49:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LxuY9uD7ru9CWhIB6aOF0nMfUMaFN1P/a9jmbZMk/UQ=; b=Thb1VMZCb/JYdI0KrlzprNUpsE5eUQDvmQ6dZ/ZnUOhreLRx5bJr9wpiQBxSOmj3hj QCj3IEDMo7OduiHZDPCZN3mlcrUxEBoaQR5RoDAWwZoIgZvLnBPvYksTgry6Nm+BvKuU dGPiPcJdR4CWBRdphceZlcyuC//t90QLdpUAeOBoPyHuGXtXS3ElVFaMByvB8ShM7J6p DZUNrCRhRWY8q6ez2S8ZbuoEniDYatrSQEmtTiazzvvad1kxOaRzBSyUXcoFZE8XzTrY GMvqEmMyBY8afAhBYuGXA69TX6bdus9eAS0PGPynTks3r0ct/dC63xLIuMLl1mQcxBCz qGVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LxuY9uD7ru9CWhIB6aOF0nMfUMaFN1P/a9jmbZMk/UQ=; b=HFCP6j2ublql/v2L2WWtBSx2YxWRDw631XVggN69fgjJWKanP/2soWUld82HHodqlO 0hHCa3DQpC0GyZkI6ajvWPqExrTaRYhNc8d/V5uSkrNnGcT6o6FCCFR/WDc/LYX8sutV Vj/rhKA7ziGkrUKGVlMbS2MDdiBRxgb9bzsL0/fIIwszqKOcje/ut/ZQRtU/IPtrnPO3 o5xwVb0ZmbzGKQVC+/xTznNnzW2nWEAicW/mQ8lkllzJV3NCDfVkY6AxwHkg/MRhXVwA dIGevibMSZDcHwe+bBpbik3zrCtl0mmzr8kFMhbZ8sMjFzo27f/KydYLUQxwdS6G9bPY OXGA== X-Gm-Message-State: ACrzQf1y+eFtkKRr0lb/VLAsIlYiTzmbTH8yqGFDj5WUq2PMbbTkzUA9 dBxV091Hm5P9WkM4grAGo+i9wibMPhRYyA== X-Google-Smtp-Source: AMsMyM7apggPVP7JlsDsfzo8hI/W2iYRBC65oycUrR3RZHE5t9cQoxW2SvyyC05kiAqbifIvO3ulng== X-Received: by 2002:a05:6000:168f:b0:232:bd06:d5c4 with SMTP id y15-20020a056000168f00b00232bd06d5c4mr6671029wrd.122.1666028968057; Mon, 17 Oct 2022 10:49:28 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:27 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 03/10] run-command API: add and use a run_command_l_opt() Date: Mon, 17 Oct 2022 19:49:14 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When run_command() is used with strvec_pushl() as in the pre-image of b004c902827 (gc: simplify maintenance_task_pack_refs(), 2022-10-04) we get the benefit of the LAST_ARG_MUST_BE_NULL. See 9fe3edc47f1 (Add the LAST_ARG_MUST_BE_NULL macro, 2013-07-18). Let's provide and use a run_command_l_opt() function, which gives us the best of both worlds. We'll now need less code to accomplish the same thing, and this version's safer as we can assert that the terminating NULL is present. The "builtin/bisect--helper.c" would be a large beneficiary of this API, with "15 insertions(+), 26 deletions(-)", but let's leave that aside for now, as there's an outstanding topic that's extensively modifying it. Signed-off-by: Ævar Arnfjörð Bjarmason --- bisect.c | 19 +++++++++---------- builtin/clone.c | 16 ++++++---------- builtin/difftool.c | 14 ++++++-------- builtin/gc.c | 22 ++++++++-------------- builtin/merge.c | 35 ++++++----------------------------- compat/mingw.c | 8 +++----- ll-merge.c | 4 +--- run-command.c | 15 +++++++++++++++ run-command.h | 13 +++++++++++-- sequencer.c | 4 +--- t/helper/test-fake-ssh.c | 4 +--- 11 files changed, 67 insertions(+), 87 deletions(-) diff --git a/bisect.c b/bisect.c index fd581b85a72..415b0e7b0b2 100644 --- a/bisect.c +++ b/bisect.c @@ -738,18 +738,17 @@ enum bisect_error bisect_checkout(const struct object_id *bisect_rev, update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); argv_checkout[2] = bisect_rev_hex; - if (no_checkout) { + if (no_checkout) update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); - } else { - if (run_command_v_opt(argv_checkout, RUN_GIT_CMD)) - /* - * Errors in `run_command()` itself, signaled by res < 0, - * and errors in the child process, signaled by res > 0 - * can both be treated as regular BISECT_FAILED (-1). - */ - return BISECT_FAILED; - } + else if (run_command_l_opt(RUN_GIT_CMD, "checkout", "-q", + bisect_rev_hex, "--", NULL)) + /* + * Errors in `run_command()` itself, signaled by res < 0, + * and errors in the child process, signaled by res > 0 + * can both be treated as regular BISECT_FAILED (-1). + */ + return BISECT_FAILED; commit = lookup_commit_reference(the_repository, bisect_rev); format_commit_message(commit, "[%H] %s%n", &commit_msg, &pp); diff --git a/builtin/clone.c b/builtin/clone.c index ed8d44bb6ab..a1514363190 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -651,23 +651,19 @@ static void update_head(const struct ref *our, const struct ref *remote, static int git_sparse_checkout_init(const char *repo) { - struct strvec argv = STRVEC_INIT; - int result = 0; - strvec_pushl(&argv, "-C", repo, "sparse-checkout", "set", NULL); - /* * We must apply the setting in the current process * for the later checkout to use the sparse-checkout file. */ core_apply_sparse_checkout = 1; - if (run_command_v_opt(argv.v, RUN_GIT_CMD)) { + if (run_command_l_opt(RUN_GIT_CMD, "-C", repo, "sparse-checkout", + "set", NULL)) { error(_("failed to initialize sparse-checkout")); - result = 1; + return 1; } - strvec_clear(&argv); - return result; + return 0; } static int checkout(int submodule_progress, int filter_submodules) @@ -862,11 +858,11 @@ static void write_refspec_config(const char *src_ref_prefix, static void dissociate_from_references(void) { - static const char* argv[] = { "repack", "-a", "-d", NULL }; char *alternates = git_pathdup("objects/info/alternates"); if (!access(alternates, F_OK)) { - if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN)) + if (run_command_l_opt(RUN_GIT_CMD|RUN_COMMAND_NO_STDIN, + "repack", "-a", "-d", NULL)) die(_("cannot repack to clean up")); if (unlink(alternates) && errno != ENOENT) die_errno(_("cannot unlink temporary alternates file")); diff --git a/builtin/difftool.c b/builtin/difftool.c index 4b10ad1a369..ed211a87322 100644 --- a/builtin/difftool.c +++ b/builtin/difftool.c @@ -44,8 +44,8 @@ static int difftool_config(const char *var, const char *value, void *cb) static int print_tool_help(void) { - const char *argv[] = { "mergetool", "--tool-help=diff", NULL }; - return run_command_v_opt(argv, RUN_GIT_CMD); + return run_command_l_opt(RUN_GIT_CMD, "mergetool", "--tool-help=diff", + NULL); } static int parse_index_info(char *p, int *mode1, int *mode2, @@ -361,7 +361,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix, struct index_state wtindex; struct checkout lstate, rstate; int flags = RUN_GIT_CMD, err = 0; - const char *helper_argv[] = { "difftool--helper", NULL, NULL, NULL }; + const char *helper_command = "difftool--helper"; struct hashmap wt_modified, tmp_modified; int indices_loaded = 0; @@ -563,16 +563,14 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix, } strbuf_setlen(&ldir, ldir_len); - helper_argv[1] = ldir.buf; strbuf_setlen(&rdir, rdir_len); - helper_argv[2] = rdir.buf; - if (extcmd) { - helper_argv[0] = extcmd; + helper_command = extcmd; flags = 0; } else setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1); - ret = run_command_v_opt(helper_argv, flags); + ret = run_command_l_opt(flags, helper_command, ldir.buf, rdir.buf, + NULL); /* TODO: audit for interaction with sparse-index. */ ensure_full_index(&wtindex); diff --git a/builtin/gc.c b/builtin/gc.c index 243ee85d283..075b4637660 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -58,8 +58,6 @@ static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; static struct strvec reflog = STRVEC_INIT; static struct strvec repack = STRVEC_INIT; static struct strvec prune = STRVEC_INIT; -static struct strvec prune_worktrees = STRVEC_INIT; -static struct strvec rerere = STRVEC_INIT; static struct tempfile *pidfile; static struct lock_file log_lock; @@ -167,9 +165,8 @@ static void gc_config(void) struct maintenance_run_opts; static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts) { - const char *argv[] = { "pack-refs", "--all", "--prune", NULL }; - - return run_command_v_opt(argv, RUN_GIT_CMD); + return run_command_l_opt(RUN_GIT_CMD, "pack-refs", "--all", "--prune", + NULL); } static int too_many_loose_objects(void) @@ -574,8 +571,6 @@ int cmd_gc(int argc, const char **argv, const char *prefix) strvec_pushl(&reflog, "reflog", "expire", "--all", NULL); strvec_pushl(&repack, "repack", "-d", "-l", NULL); strvec_pushl(&prune, "prune", "--expire", NULL); - strvec_pushl(&prune_worktrees, "worktree", "prune", "--expire", NULL); - strvec_pushl(&rerere, "rerere", "gc", NULL); /* default expiry time, overwritten in gc_config */ gc_config(); @@ -688,14 +683,13 @@ int cmd_gc(int argc, const char **argv, const char *prefix) } } - if (prune_worktrees_expire) { - strvec_push(&prune_worktrees, prune_worktrees_expire); - if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD)) - die(FAILED_RUN, prune_worktrees.v[0]); - } + if (prune_worktrees_expire && + run_command_l_opt(RUN_GIT_CMD, "worktree", "prune", "--expire", + prune_worktrees_expire, NULL)) + die(FAILED_RUN, "worktree"); - if (run_command_v_opt(rerere.v, RUN_GIT_CMD)) - die(FAILED_RUN, rerere.v[0]); + if (run_command_l_opt(RUN_GIT_CMD, "rerere", "gc", NULL)) + die(FAILED_RUN, "rerere"); report_garbage = report_pack_garbage; reprepare_packed_git(the_repository); diff --git a/builtin/merge.c b/builtin/merge.c index 3bb49d805b4..9c04b588f68 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -347,55 +347,32 @@ static int save_state(struct object_id *stash) static void read_empty(const struct object_id *oid) { - int i = 0; - const char *args[7]; - - args[i++] = "read-tree"; - args[i++] = "-m"; - args[i++] = "-u"; - args[i++] = empty_tree_oid_hex(); - args[i++] = oid_to_hex(oid); - args[i] = NULL; - - if (run_command_v_opt(args, RUN_GIT_CMD)) + if (run_command_l_opt(RUN_GIT_CMD, "read-tree", "-m", "-u", + empty_tree_oid_hex(), oid_to_hex(oid), NULL)) die(_("read-tree failed")); } static void reset_hard(const struct object_id *oid) { - int i = 0; - const char *args[6]; - - args[i++] = "read-tree"; - args[i++] = "-v"; - args[i++] = "--reset"; - args[i++] = "-u"; - args[i++] = oid_to_hex(oid); - args[i] = NULL; - - if (run_command_v_opt(args, RUN_GIT_CMD)) + if (run_command_l_opt(RUN_GIT_CMD, "read-tree", "-v", "--reset", "-u", + oid_to_hex(oid), NULL)) die(_("read-tree failed")); } static void restore_state(const struct object_id *head, const struct object_id *stash) { - struct strvec args = STRVEC_INIT; - reset_hard(head); if (is_null_oid(stash)) goto refresh_cache; - strvec_pushl(&args, "stash", "apply", "--index", "--quiet", NULL); - strvec_push(&args, oid_to_hex(stash)); - /* * It is OK to ignore error here, for example when there was * nothing to restore. */ - run_command_v_opt(args.v, RUN_GIT_CMD); - strvec_clear(&args); + run_command_l_opt(RUN_GIT_CMD, "stash", "apply", "--index", "--quiet", + oid_to_hex(stash), NULL); refresh_cache: if (discard_cache() < 0 || read_cache() < 0) diff --git a/compat/mingw.c b/compat/mingw.c index 901375d5841..4f5392c5796 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -196,17 +196,15 @@ static int read_yes_no_answer(void) static int ask_yes_no_if_possible(const char *format, ...) { char question[4096]; - const char *retry_hook[] = { NULL, NULL, NULL }; + char *retry_hook; va_list args; va_start(args, format); vsnprintf(question, sizeof(question), format, args); va_end(args); - if ((retry_hook[0] = mingw_getenv("GIT_ASK_YESNO"))) { - retry_hook[1] = question; - return !run_command_v_opt(retry_hook, 0); - } + if ((retry_hook = mingw_getenv("GIT_ASK_YESNO"))) + return !run_command_l_opt(0, retry_hook, question, NULL); if (!isatty(_fileno(stdin)) || !isatty(_fileno(stderr))) return 0; diff --git a/ll-merge.c b/ll-merge.c index 8955d7e1f6e..740689b7bd6 100644 --- a/ll-merge.c +++ b/ll-merge.c @@ -193,7 +193,6 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn, struct strbuf cmd = STRBUF_INIT; struct strbuf_expand_dict_entry dict[6]; struct strbuf path_sq = STRBUF_INIT; - const char *args[] = { NULL, NULL }; int status, fd, i; struct stat st; enum ll_merge_result ret; @@ -219,8 +218,7 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn, strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict); - args[0] = cmd.buf; - status = run_command_v_opt(args, RUN_USING_SHELL); + status = run_command_l_opt(RUN_USING_SHELL, cmd.buf, NULL); fd = open(temp[1], O_RDONLY); if (fd < 0) goto bad; diff --git a/run-command.c b/run-command.c index 4e75f83b5e7..fa23fd0b803 100644 --- a/run-command.c +++ b/run-command.c @@ -1016,6 +1016,21 @@ static void run_command_set_opts(struct child_process *cmd, int opt) cmd->close_object_store = opt & RUN_CLOSE_OBJECT_STORE ? 1 : 0; } +int run_command_l_opt(int opt, ...) +{ + struct child_process cmd = CHILD_PROCESS_INIT; + va_list ap; + const char *arg; + + va_start(ap, opt); + while ((arg = va_arg(ap, const char *))) + strvec_push(&cmd.args, arg); + va_end(ap); + + run_command_set_opts(&cmd, opt); + return run_command(&cmd); +} + static int run_command_v_opt_1(struct child_process *cmd, int opt) { run_command_set_opts(cmd, opt); diff --git a/run-command.h b/run-command.h index 0e85e5846a5..6320d70f062 100644 --- a/run-command.h +++ b/run-command.h @@ -151,8 +151,8 @@ struct child_process { /** * The functions: child_process_init, start_command, finish_command, - * run_command, run_command_v_opt, run_command_v_opt_cd_env, child_process_clear - * do the following: + * run_command, run_command_l_opt, run_command_v_opt, + * run_command_v_opt_cd_env, child_process_clear do the following: * * - If a system call failed, errno is set and -1 is returned. A diagnostic * is printed. @@ -254,6 +254,15 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, const char *const *env, const char *tr2_class); +/** + * The run_command_l_opt() function run_command_v_opt() takes a list + * of strings terminated by a NULL. Use it instead of + * run_command_v_opt() when possible, as it allows the compiler to + * check that the "argv" is NULL-delimited. + */ +LAST_ARG_MUST_BE_NULL +int run_command_l_opt(int opt, ...); + /** * Execute the given command, sending "in" to its stdin, and capturing its * stdout and stderr in the "out" and "err" strbufs. Any of the three may diff --git a/sequencer.c b/sequencer.c index debb2ecbafe..20495db9de2 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3558,12 +3558,10 @@ static int error_failed_squash(struct repository *r, static int do_exec(struct repository *r, const char *command_line) { - const char *child_argv[] = { NULL, NULL }; int dirty, status; fprintf(stderr, _("Executing: %s\n"), command_line); - child_argv[0] = command_line; - status = run_command_v_opt(child_argv, RUN_USING_SHELL); + status = run_command_l_opt(RUN_USING_SHELL, command_line, NULL); /* force re-reading of the cache */ if (discard_index(r->index) < 0 || repo_read_index(r) < 0) diff --git a/t/helper/test-fake-ssh.c b/t/helper/test-fake-ssh.c index 12beee99ad2..7967478a40a 100644 --- a/t/helper/test-fake-ssh.c +++ b/t/helper/test-fake-ssh.c @@ -8,7 +8,6 @@ int cmd_main(int argc, const char **argv) struct strbuf buf = STRBUF_INIT; FILE *f; int i; - const char *child_argv[] = { NULL, NULL }; /* First, print all parameters into $TRASH_DIRECTORY/ssh-output */ if (!trash_directory) @@ -25,6 +24,5 @@ int cmd_main(int argc, const char **argv) /* Now, evaluate the *last* parameter */ if (argc < 2) return 0; - child_argv[0] = argv[argc - 1]; - return run_command_v_opt(child_argv, RUN_USING_SHELL); + return run_command_l_opt(RUN_USING_SHELL, argv[argc - 1], NULL); } From patchwork Mon Oct 17 17:49:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009186 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A747BC4332F for ; Mon, 17 Oct 2022 17:49:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230387AbiJQRtm (ORCPT ); Mon, 17 Oct 2022 13:49:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230272AbiJQRtc (ORCPT ); Mon, 17 Oct 2022 13:49:32 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12E22BCA6 for ; Mon, 17 Oct 2022 10:49:31 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id fn7-20020a05600c688700b003b4fb113b86so10558603wmb.0 for ; Mon, 17 Oct 2022 10:49:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aQmFvnekpMV6f3ZF1ugz1NHwlNEbxXsqFB1IIhRPS/g=; b=qqQeS4n47FHNKmpLX68H2IISod6U/xHg3KlGkTM2XB6I5Z3Pd+NIeMJ4Gkck7wlEKF +/XC7WRxVkRD8rLi3y1ELdYIEJaEhbOBIpcxYnpYzL9dRXoHMmSJ9rIzyCMMB9+0qrxm 2FRF2B1z+nEa4eVD1ImTm1G+zZyq+DctENTslF+IRWabeBi7gM1fok7nHAl9HStVwbp6 SKGvTYN1gNMeVR1xbIcunJXRVhlIJIP48sY+aDryqEITJve0ziobPnc7Bd62pNh6NVL0 NSZoz4ePtlyE0g2K+Cn2JduOp5Uua3WxCeDOnTTGL/YKsheOzIC3uI7LJDD56a+WjYnC xhIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aQmFvnekpMV6f3ZF1ugz1NHwlNEbxXsqFB1IIhRPS/g=; b=mceDUKutVCEkjzjotMWb/VSw6Fok0b7GGhIkLSjR2cWY0jUqHItD/uIOUqlzlqitaC eZVx7i7/LCFj0/ulGnsY90pMvvTrRNdar3cvMfBSNhEJw/EhlNa/y+m/pWCgi2Gl0Pdi nUKkAI1lAX1N74YaHV5tS3VRCZka5tzlZczhVbAK81IMuZV5VUIuwGJH5I1XIIn0eEty VMpETqrP6IFDIwjnirxSaUrY6HaumZLCsJYy6lc5JKzV3iGatpPYwQ6IJ4iZePpIMPgD jCSBNcY1gKk5IwRtPdR94UI41C7NMgfeqZiGEZhXHvRds7syjmIH1D/eNb+wdzVOZUx7 1nag== X-Gm-Message-State: ACrzQf3pb1RMH9BOav6SZdLGrYvn9hxmqUQ3/yroDB8GEYm9/OZxLDhw NSRyZGy7yUYzvHC0ihdnu+cAK7v+Hn2K2g== X-Google-Smtp-Source: AMsMyM41Fyudc6sW5jdBsL3NVOjC5UTNqoTNtTG8AHb7kvbT5HVjMX09X9nD9r4aB03ioUT4dfyX0A== X-Received: by 2002:a05:600c:4f89:b0:3b4:a6fc:89e5 with SMTP id n9-20020a05600c4f8900b003b4a6fc89e5mr19426014wmq.149.1666028969238; Mon, 17 Oct 2022 10:49:29 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:28 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 04/10] am: use run_command_l_opt() for show_patch() Date: Mon, 17 Oct 2022 19:49:15 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The "git show" invocation added in 66335298a47 (rebase: add --show-current-patch, 2018-02-11) is one where we're not calling oid_to_hex() twice. So we can rely on the static buffer that oid_to_hex() points to, rather than xstrdup()-ing it. As a result we can use the run_command_l_opt() function. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/am.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index 39fea248330..1d298a91306 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -2186,16 +2186,10 @@ static int show_patch(struct am_state *state, enum show_patch_type sub_mode) const char *patch_path; int len; - if (!is_null_oid(&state->orig_commit)) { - const char *av[4] = { "show", NULL, "--", NULL }; - char *new_oid_str; - int ret; - - av[1] = new_oid_str = xstrdup(oid_to_hex(&state->orig_commit)); - ret = run_command_v_opt(av, RUN_GIT_CMD); - free(new_oid_str); - return ret; - } + if (!is_null_oid(&state->orig_commit)) + return run_command_l_opt(RUN_GIT_CMD, "show", + oid_to_hex(&state->orig_commit), + "--", NULL); switch (sub_mode) { case SHOW_PATCH_RAW: From patchwork Mon Oct 17 17:49:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7F60DC4332F for ; Mon, 17 Oct 2022 17:49:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230260AbiJQRtp (ORCPT ); Mon, 17 Oct 2022 13:49:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230325AbiJQRth (ORCPT ); Mon, 17 Oct 2022 13:49:37 -0400 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5770F58A for ; Mon, 17 Oct 2022 10:49:32 -0700 (PDT) Received: by mail-wm1-x336.google.com with SMTP id t4so9112827wmj.5 for ; Mon, 17 Oct 2022 10:49:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=09jczzBKoiZ5VT0vFNyyBObMkIOVjeQ1lFzwJFExFSk=; b=c9N68i+t+Sc8+fJVpAhl1L2XbK8JtlCVl0bkv8RFyfGQo/OB3lwii/8MSUJfK/6/N9 KlYvEuuZnnBQOt3LpI9UL63/eibl+SRPOBhHMqaqVNgMbt0d24YfTwZO/qv1HWbdCVU2 Og9YLd19Wxz0ybzjCdT+qVRiNXsuagYyoeB3vnaYfCi2Iri+8GwtTQnHDDZXKDJFOFLN 5qSaJX1qxr7Db2E+gFr94LgGSUx/oX9dvczduYZ7CVkmzIy6OKiO6/c28PncFSk4ml3G zce3FOsD3W3/CeCljmWNdZ2c7NdivMhFXm/W0vJXx+3opLtFabWxURdaibcKIcaV6mTP xFkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=09jczzBKoiZ5VT0vFNyyBObMkIOVjeQ1lFzwJFExFSk=; b=KLk6PryMffU3qSihPyXyt6HuLIOjHAezU5ZDr2GQ1TdDm6FmolTM2PQPQzhJvaNZZ7 XTJVTSNAPp9RlLgQVcaFHeK9/JvOB+MUoNiTLPcLyDNOXCUuTWE8dcuzti5Ry1xB+cbT /7u4ub80XZc92W3hLcabHrU/onGvrJBfh1kMvUBhz0eEBmFqY/2574J6AsPAJRdeVnqb Ib0gJEFiBkDju6nEdSPFLi6Y4KGsIrWv5vzkLpRUB/gb7RpgKGYHiMh8+3FpLdACAmRm IYmQ0aH2eEuX+SUaj+ataKAsH44jKXYZygEhqJOt6GhOQTttqHrNHsXir7wC6fGnvMuS EPqQ== X-Gm-Message-State: ACrzQf1zM+wDaMlMUp/V1YzNp5QRCjCsQt+jRrZTExkL0xa0a2KKpiYk LS/WssXkCcoK9TM738Ajt7ZWVCnzDKVNWw== X-Google-Smtp-Source: AMsMyM77z9WBgHVQq6XDB5TbRKIFJJUdD3EkCFQggf2UqIrZFzBlfJ4fY7OnCyXsxJszM5D2davMqw== X-Received: by 2002:a05:600c:4e8a:b0:3c6:da7c:8d71 with SMTP id f10-20020a05600c4e8a00b003c6da7c8d71mr16993100wmq.16.1666028970702; Mon, 17 Oct 2022 10:49:30 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:29 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 05/10] run-command API docs: clarify & fleshen out run_command_v_opt*() docs Date: Mon, 17 Oct 2022 19:49:16 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a discussion of the flags that were missing to the run_command_v_opt*() docs, and in doing so format them such that we can easily add or remove flags from a table in the future, rather than having them tied up in prose. Let's also clarify why the user might want to use this API over run_command() itself. Signed-off-by: Ævar Arnfjörð Bjarmason --- run-command.h | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/run-command.h b/run-command.h index 6320d70f062..cf250e36426 100644 --- a/run-command.h +++ b/run-command.h @@ -234,13 +234,28 @@ int run_auto_maintenance(int quiet); #define RUN_CLOSE_OBJECT_STORE (1<<7) /** - * Convenience functions that encapsulate a sequence of - * start_command() followed by finish_command(). The argument argv - * specifies the program and its arguments. The argument opt is zero - * or more of the flags `RUN_COMMAND_NO_STDIN`, `RUN_GIT_CMD`, - * `RUN_COMMAND_STDOUT_TO_STDERR`, or `RUN_SILENT_EXEC_FAILURE` - * that correspond to the members .no_stdin, .git_cmd, - * .stdout_to_stderr, .silent_exec_failure of `struct child_process`. + * The run_command_v_opt*() API is a convenience wrapper for an + * underlying run_command(). + * + * It's intended to be used when the user already has an "argv" they'd + * like to use. As opposed to the "struct child_process"'s "args" + * member, which will be strvec_clear()'d by calling run_command(), + * the caller owns the "argv", which is not altered by invoking these + * functions. + * + * The "opt" flags that will cause various underlying run_command() + * members to be set. The flags and the corresponding struct members + * are: + * + * - RUN_COMMAND_NO_STDIN: .no_stdin + * - RUN_GIT_CMD: .git_cmd + * - RUN_COMMAND_STDOUT_TO_STDERR: .stdout_to_stderr + * - RUN_SILENT_EXEC_FAILURE: .silent_exec_failure + * - RUN_USING_SHELL: .use_shell + * - RUN_CLEAN_ON_EXIT: .clean_on_exit + * - RUN_WAIT_AFTER_CLEAN: .wait_after_clean + * - RUN_CLOSE_OBJECT_STORE: .close_object_store + * * The argument dir corresponds the member .dir. The argument env * corresponds to the member .env. */ From patchwork Mon Oct 17 17:49:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFBCEC4332F for ; Mon, 17 Oct 2022 17:49:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230456AbiJQRtr (ORCPT ); Mon, 17 Oct 2022 13:49:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230400AbiJQRtn (ORCPT ); Mon, 17 Oct 2022 13:49:43 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99AC714D12 for ; Mon, 17 Oct 2022 10:49:35 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id y10so9155687wma.0 for ; Mon, 17 Oct 2022 10:49:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0Mo1VnrPzDKuOigK23quXQsUrqYTK7579fyvcbIHPNs=; b=bOS0k+BRX7qa5AeJLFaO/zv7kLAsachOQG0f1dyYnrDmBL3TpV3a1bdfi6llaDrgRU ufgfr+i9Eq8bWMdGWZOJmuy3FWNyN/O9jg55XTfqEx/Uo2vXtouITZT5rpoZKtO0MWjq gCZUaBn0hS7gI8IW38v9r5PVzfLLNTUzx+NovXxCUHhP44+eLxDUp5bD8GBsCf/qCvma PQ8K/U+z3CNZXMS+8Om9FgGNFqEYtL/im2dbQZZNQ4/iK+8+qRbaHIIldRTCFpz/IWug 6Q8dtSmdLdmUHsuCiAL1m84iGr/4R5xQ5oFDCGjae69ta42JqCsGd/FAwjJU+TrdQYYL 0Xaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0Mo1VnrPzDKuOigK23quXQsUrqYTK7579fyvcbIHPNs=; b=1BZ+jVhmTIh11Wv3pU9YTdu0Nm0jQt5shO1yqb3DrIJf+HxXozbHitQuf4jknaMB3o DnUiRK/BbXpWjyJZb4vH2XmpoVQZwszxONgwIpXn6GJAPsuJn6GiRi01zgQRlfJM5aVq OCclSZ13lFaJbGLVMxRVhNMQ/yXEcGrhL+PPlWDq0gMacqObEUYW06BDLglDT5WwQn8v W79BqJoHGHLXAYMLQoSXnZDvcCy63U/x37wP8Ep/N+vDawPW4utA4ChjfQPDbFzjU5L6 j/3bX8aMDFD851zDbqYioWsP0pvTJ2BNqX/OC8NU2MgMcnqvvbMRS78PlgG8oQRaHRKH vdQg== X-Gm-Message-State: ACrzQf1NVb05ODBfc6WiU3qIeNlSZzya/kHULHMiV/VKscc+b5Ws9ACQ u29sTuDyn3No30zx0xdFRVlJqJZLYxxvVA== X-Google-Smtp-Source: AMsMyM4B4sEQiJSFDBJVfJCkdXig0z/sv8MSfpPpOSJz0ut6GP4Fdcm2C77b4COLPZO6DNQNdZ8mSA== X-Received: by 2002:a05:600c:4588:b0:3c6:f8b2:dd34 with SMTP id r8-20020a05600c458800b003c6f8b2dd34mr3551725wmo.178.1666028973126; Mon, 17 Oct 2022 10:49:33 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:31 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 06/10] run-command API: remove RUN_COMMAND_STDOUT_TO_STDERR flag Date: Mon, 17 Oct 2022 19:49:17 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The run_command_v_opt*() API is meant to handle various common cases where we don't want the verbosity of calling the underlying run_command(), but we're explicitly not trying to cover the full API surface of run_command() itself. So if we're not using some of these flags we probably won't need them again, any future user who needs to set "stdout_to_stderr" can just use run_command() itself, and the convenience API shouldn't be cluttered by trying to handle all the needs of various one-offs. So let's remove the RUN_COMMAND_STDOUT_TO_STDERR flag, it hasn't been used since 860a2ebecd2 (receive-pack: send auto-gc output over sideband 2, 2016-06-05) when its last user started using the underlying API directly. Signed-off-by: Ævar Arnfjörð Bjarmason --- run-command.c | 1 - run-command.h | 12 +++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/run-command.c b/run-command.c index fa23fd0b803..f9a0b13f62f 100644 --- a/run-command.c +++ b/run-command.c @@ -1008,7 +1008,6 @@ static void run_command_set_opts(struct child_process *cmd, int opt) { cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0; cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0; - cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0; cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0; cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; diff --git a/run-command.h b/run-command.h index cf250e36426..0af01bd9b67 100644 --- a/run-command.h +++ b/run-command.h @@ -226,12 +226,11 @@ int run_auto_maintenance(int quiet); #define RUN_COMMAND_NO_STDIN (1<<0) #define RUN_GIT_CMD (1<<1) -#define RUN_COMMAND_STDOUT_TO_STDERR (1<<2) -#define RUN_SILENT_EXEC_FAILURE (1<<3) -#define RUN_USING_SHELL (1<<4) -#define RUN_CLEAN_ON_EXIT (1<<5) -#define RUN_WAIT_AFTER_CLEAN (1<<6) -#define RUN_CLOSE_OBJECT_STORE (1<<7) +#define RUN_SILENT_EXEC_FAILURE (1<<2) +#define RUN_USING_SHELL (1<<3) +#define RUN_CLEAN_ON_EXIT (1<<4) +#define RUN_WAIT_AFTER_CLEAN (1<<5) +#define RUN_CLOSE_OBJECT_STORE (1<<6) /** * The run_command_v_opt*() API is a convenience wrapper for an @@ -249,7 +248,6 @@ int run_auto_maintenance(int quiet); * * - RUN_COMMAND_NO_STDIN: .no_stdin * - RUN_GIT_CMD: .git_cmd - * - RUN_COMMAND_STDOUT_TO_STDERR: .stdout_to_stderr * - RUN_SILENT_EXEC_FAILURE: .silent_exec_failure * - RUN_USING_SHELL: .use_shell * - RUN_CLEAN_ON_EXIT: .clean_on_exit From patchwork Mon Oct 17 17:49:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009190 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80A93C433FE for ; Mon, 17 Oct 2022 17:50:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230516AbiJQRuD (ORCPT ); Mon, 17 Oct 2022 13:50:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230445AbiJQRto (ORCPT ); Mon, 17 Oct 2022 13:49:44 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A0EC15FDE for ; Mon, 17 Oct 2022 10:49:36 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id a10so19541695wrm.12 for ; Mon, 17 Oct 2022 10:49:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nNskV7eshI778L5zHkGD0goQmmiCiGjkSQk99xb7+p0=; b=SrIv3vCWleB5mW+cLUgF5O3qU8+66/Kft7PVjn2XSDwpbcSU0GSE5ZqO/4LDVvKYdU rtjbthtMQv2aCIEGbOo8+ZAsPAMYzETO1TU6Wg8G9hstx34HwwuOjZfHtaKZq6Qk0m24 7vLlumscXzszAsradCywmIRu2QORen8vo22XK4rDwr+QkkjNE5qTpSNho0kqKtGU8EKz zVj/HV4XcdXEihi4X7AvZPalTKS87yM055z9PrinpTt3X3dhzuUF57JxWAsyWl+paxku 0KQdG1UMTeIoc9QS5mS5CL+jPHXcshc5ozbm8RVyFieV8bEDWOZDjiFx7qs1SK9nx/7x IFZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nNskV7eshI778L5zHkGD0goQmmiCiGjkSQk99xb7+p0=; b=pwlGvT3qnTKQzELGf7POJvzLvQJwHpmlZ+GX0ugBaR6ViQELYaNuyxpanwAzNer0ei vIlb0B7hTFBez8YLAzTZKdv13zfw2CDi/1VF6sZUvVucpQVCl+RY/jUReP5vi8qwrbO6 CijRhMfiSLgcp4OGkWFIJsADbnW6PS8bQ8ROqdFR5N9Eg/NyhxoStI4Fjc0KEEOElIlX foRsuXmM8YEZanFE3nGktm52b9lDDTkI5ImFW8tAH+zvE134jU4p5Rt+0881HFx06iT8 914v6Up6ZCXfq2vOP3yr6LJnNDB16oFEUK4B5YlPytKE+J04z3PKjbjQppz+QAr6jFax oKnQ== X-Gm-Message-State: ACrzQf3c9ZiRZ9HKYhrbNaUZMRANU7s+ODqgshIyU6PhVITb/teLhbsS OnYqFdO6MEila+KPL3tfK3DCVnEvfon6+w== X-Google-Smtp-Source: AMsMyM6v9Gdjw6ZzmJkqWxqv0RqrcmR0sbnbjmLbI34/3S8IQwzmu32QT/2OMSKcdrrgwdwHNw31Wg== X-Received: by 2002:a5d:6d06:0:b0:232:b56c:e5c3 with SMTP id e6-20020a5d6d06000000b00232b56ce5c3mr6548072wrq.506.1666028974209; Mon, 17 Oct 2022 10:49:34 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:33 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 07/10] run-command API & diff.c: remove run_command_v_opt_cd_env() Date: Mon, 17 Oct 2022 19:49:18 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As we'll see in subsequent commits most of the run_command_v_opt_*() API users are a bad fit for what we'd like to do with that API. The ideal use-case for it is something where we already have an "argv", and don't want the one-shot semantics of run_command(). In the case of run_command_v_opt_cd_env() there was only user of it, and that user was just accumulating complexity by not using run_command() directly. By not having to maintain its own "argv" and "env" it doesn't have to strvec_clear() them both, which in the case of run_command() will be done by child_process_clear() before it returns. Signed-off-by: Ævar Arnfjörð Bjarmason --- diff.c | 26 ++++++++++++-------------- run-command.c | 7 +------ run-command.h | 3 +-- tmp-objdir.h | 6 ++++-- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/diff.c b/diff.c index 648f6717a55..392530016fa 100644 --- a/diff.c +++ b/diff.c @@ -4278,35 +4278,33 @@ static void run_external_diff(const char *pgm, const char *xfrm_msg, struct diff_options *o) { - struct strvec argv = STRVEC_INIT; - struct strvec env = STRVEC_INIT; struct diff_queue_struct *q = &diff_queued_diff; + struct child_process cmd = CHILD_PROCESS_INIT; - strvec_push(&argv, pgm); - strvec_push(&argv, name); + strvec_push(&cmd.args, pgm); + strvec_push(&cmd.args, name); if (one && two) { - add_external_diff_name(o->repo, &argv, name, one); + add_external_diff_name(o->repo, &cmd.args, name, one); if (!other) - add_external_diff_name(o->repo, &argv, name, two); + add_external_diff_name(o->repo, &cmd.args, name, two); else { - add_external_diff_name(o->repo, &argv, other, two); - strvec_push(&argv, other); - strvec_push(&argv, xfrm_msg); + add_external_diff_name(o->repo, &cmd.args, other, two); + strvec_push(&cmd.args, other); + strvec_push(&cmd.args, xfrm_msg); } } - strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter); - strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); + strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter); + strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); diff_free_filespec_data(one); diff_free_filespec_data(two); - if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v)) + cmd.use_shell = 1; + if (run_command(&cmd)) die(_("external diff died, stopping at %s"), name); remove_tempfile(); - strvec_clear(&argv); - strvec_clear(&env); } static int similarity_index(struct diff_filepair *p) diff --git a/run-command.c b/run-command.c index f9a0b13f62f..25a978fb027 100644 --- a/run-command.c +++ b/run-command.c @@ -1038,7 +1038,7 @@ static int run_command_v_opt_1(struct child_process *cmd, int opt) int run_command_v_opt(const char **argv, int opt) { - return run_command_v_opt_cd_env(argv, opt, NULL, NULL); + return run_command_v_opt_cd_env_tr2(argv, opt, NULL, NULL, NULL); } int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class) @@ -1046,11 +1046,6 @@ int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class) return run_command_v_opt_cd_env_tr2(argv, opt, NULL, NULL, tr2_class); } -int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env) -{ - return run_command_v_opt_cd_env_tr2(argv, opt, dir, env, NULL); -} - int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, const char *const *env, const char *tr2_class) { diff --git a/run-command.h b/run-command.h index 0af01bd9b67..2574d46cb70 100644 --- a/run-command.h +++ b/run-command.h @@ -152,7 +152,7 @@ struct child_process { /** * The functions: child_process_init, start_command, finish_command, * run_command, run_command_l_opt, run_command_v_opt, - * run_command_v_opt_cd_env, child_process_clear do the following: + * child_process_clear do the following: * * - If a system call failed, errno is set and -1 is returned. A diagnostic * is printed. @@ -263,7 +263,6 @@ int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class); * env (the environment) is to be formatted like environ: "VAR=VALUE". * To unset an environment variable use just "VAR". */ -int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env); int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, const char *const *env, const char *tr2_class); diff --git a/tmp-objdir.h b/tmp-objdir.h index 76efc7edee5..c96aa77396d 100644 --- a/tmp-objdir.h +++ b/tmp-objdir.h @@ -10,9 +10,11 @@ * * Example: * + * struct child_process cmd = CHILD_PROCESS_INIT; + * ... * struct tmp_objdir *t = tmp_objdir_create("incoming"); - * if (!run_command_v_opt_cd_env(cmd, 0, NULL, tmp_objdir_env(t)) && - * !tmp_objdir_migrate(t)) + * strvec_pushl(&cmd.env, tmp_objdir_env(t)); + * if (!run_command(&cmd) && !tmp_objdir_migrate(t)) * printf("success!\n"); * else * die("failed...tmp_objdir will clean up for us"); From patchwork Mon Oct 17 17:49:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009191 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF5A3C43217 for ; Mon, 17 Oct 2022 17:50:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230517AbiJQRuF (ORCPT ); Mon, 17 Oct 2022 13:50:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230447AbiJQRto (ORCPT ); Mon, 17 Oct 2022 13:49:44 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A36715FF8 for ; Mon, 17 Oct 2022 10:49:36 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id fn7-20020a05600c688700b003b4fb113b86so10558786wmb.0 for ; Mon, 17 Oct 2022 10:49:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bu1Mw+iMZUbADGlnnmkAMuktq2G8TJ6IytTSFQu4lRI=; b=e1qycXqi2b+h/yjl16cZWP19qSM0J3DGE9taXsCkE+p4T6heUd80AEgR6a7AZTYcMJ MdL2ObZNn5YPbne2aBB+HdyrsoFbFZ9Z+isQPhlIQIOQyW+HrBFr3+qcKKJVCbM02/Db KEXotqy+o1CjqxXxVXf5V32gus0besKfEcUQbVYQBNIl3c9DlSwPbg9IagfYgdpta9rS czzwJ084KY3HCtmAW4Hr/Oy6YY7Rtug2q26jfDnqTVNTT+1beaItowpvbACjhuhiCKZM yR5/LEMpMHd+pPpPkPL6vTgxnm5UOxodyPWB7OZ4M/SeuVlXvdKl1VyOT+zixlzGa5v/ w25g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bu1Mw+iMZUbADGlnnmkAMuktq2G8TJ6IytTSFQu4lRI=; b=uDj4UOkIeJ/bC3xJBmwogTFj+UogK4+4vRBhFmpO9mlnQL3jpvxLdilEChDqPSjVbB /OWff2V0X0Y41zSYL6xByZ2aaBExtcsZ2inu88Z0P14W0JdJSHUEvfZGgWsKmQx4ESCo RAhzZxMeWCUezIj0RpWtFzQ47sbe7kVN6Bjql//uchG3EZnxHvoxVhTmFlbNbRG/HVRd N9O7C8BJ9H1Jum4cgCNa1NP+68Nn98/Ew23o7+8EPfEUv37TtQZJV9GLBZ4TswY4z35j Ic9CfbsO3XS2Aftg4BGxz003EZtAo9C2kSUCdK8nBnoCJUx5XFulEY13YVuXiIp7NY6X EZvg== X-Gm-Message-State: ACrzQf042T+jQHWosJIToNDNI4Cg5mYeYMQj8oIxhHr99qwAOOJEB54K M8brd3MUeZ5BKuqkLVKrqGTXOuRay7ovGQ== X-Google-Smtp-Source: AMsMyM6OLJ5FJJvzG+kpaLmxZJRDGsQZTO1NBZWsKn+CBdaeql25ltU9evasIwxmzP7pCggGgemmqA== X-Received: by 2002:a05:600c:35ce:b0:3c6:809a:b5c3 with SMTP id r14-20020a05600c35ce00b003c6809ab5c3mr8160325wmq.206.1666028975345; Mon, 17 Oct 2022 10:49:35 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:34 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 08/10] run-command API & users: remove run_command_v_opt_tr2() Date: Mon, 17 Oct 2022 19:49:19 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As noted in the preceding commit for run_command_v_opt_cd_env() that function's users could more easily use the underlying run_command() directly. In the case of the "git.c" user that can be argued the other way, given the slight line count increase here, but part of that's because the "args" in "git.c" was being leaked, which we now don't have to worry about. That just leaves the spawn_daemon() user in "fsmonitor-ipc.c", it's likewise slightly more verbose as a result, but by making it use run_command() we can remove this entire part of the API. As noted in a preceding commit run_command_v_opt*() should be aimed at handling various common cases, not these one-offs. The "fsmonitor-ipc.c" caller would be nicer with a hypothetical run_command_l_opt_tr2(), but let's not maintain such a thing only for it, as it would be its only user. Signed-off-by: Ævar Arnfjörð Bjarmason --- fsmonitor-ipc.c | 10 +++++++--- git.c | 15 +++++++++------ run-command.c | 26 +++----------------------- run-command.h | 19 +++---------------- 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/fsmonitor-ipc.c b/fsmonitor-ipc.c index 789e7397baa..7251f48e456 100644 --- a/fsmonitor-ipc.c +++ b/fsmonitor-ipc.c @@ -56,10 +56,14 @@ enum ipc_active_state fsmonitor_ipc__get_state(void) static int spawn_daemon(void) { - const char *args[] = { "fsmonitor--daemon", "start", NULL }; + struct child_process cmd = CHILD_PROCESS_INIT; - return run_command_v_opt_tr2(args, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD, - "fsmonitor"); + cmd.git_cmd = 1; + cmd.no_stdin = 1; + cmd.trace2_child_class = "fsmonitor"; + strvec_pushl(&cmd.args, "fsmonitor--daemon", "start", NULL); + + return run_command(&cmd); } int fsmonitor_ipc__send_query(const char *since_token, diff --git a/git.c b/git.c index da411c53822..ccf444417b5 100644 --- a/git.c +++ b/git.c @@ -787,7 +787,7 @@ static int run_argv(int *argcp, const char ***argv) if (!done_alias) handle_builtin(*argcp, *argv); else if (get_builtin(**argv)) { - struct strvec args = STRVEC_INIT; + struct child_process cmd = CHILD_PROCESS_INIT; int i; /* @@ -804,18 +804,21 @@ static int run_argv(int *argcp, const char ***argv) commit_pager_choice(); - strvec_push(&args, "git"); + strvec_push(&cmd.args, "git"); for (i = 0; i < *argcp; i++) - strvec_push(&args, (*argv)[i]); + strvec_push(&cmd.args, (*argv)[i]); - trace_argv_printf(args.v, "trace: exec:"); + trace_argv_printf(cmd.args.v, "trace: exec:"); /* * if we fail because the command is not found, it is * OK to return. Otherwise, we just pass along the status code. */ - i = run_command_v_opt_tr2(args.v, RUN_SILENT_EXEC_FAILURE | - RUN_CLEAN_ON_EXIT | RUN_WAIT_AFTER_CLEAN, "git_alias"); + cmd.silent_exec_failure = 1; + cmd.clean_on_exit = 1; + cmd.wait_after_clean = 1; + cmd.trace2_child_class = "git_alias"; + i = run_command(&cmd); if (i >= 0 || errno != ENOENT) exit(i); die("could not execute builtin %s", **argv); diff --git a/run-command.c b/run-command.c index 25a978fb027..0066ace85fa 100644 --- a/run-command.c +++ b/run-command.c @@ -1010,7 +1010,6 @@ static void run_command_set_opts(struct child_process *cmd, int opt) cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0; cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0; cmd->use_shell = opt & RUN_USING_SHELL ? 1 : 0; - cmd->clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0; cmd->wait_after_clean = opt & RUN_WAIT_AFTER_CLEAN ? 1 : 0; cmd->close_object_store = opt & RUN_CLOSE_OBJECT_STORE ? 1 : 0; } @@ -1030,32 +1029,13 @@ int run_command_l_opt(int opt, ...) return run_command(&cmd); } -static int run_command_v_opt_1(struct child_process *cmd, int opt) -{ - run_command_set_opts(cmd, opt); - return run_command(cmd); -} - int run_command_v_opt(const char **argv, int opt) -{ - return run_command_v_opt_cd_env_tr2(argv, opt, NULL, NULL, NULL); -} - -int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class) -{ - return run_command_v_opt_cd_env_tr2(argv, opt, NULL, NULL, tr2_class); -} - -int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, - const char *const *env, const char *tr2_class) { struct child_process cmd = CHILD_PROCESS_INIT; + strvec_pushv(&cmd.args, argv); - cmd.dir = dir; - if (env) - strvec_pushv(&cmd.env, (const char **)env); - cmd.trace2_child_class = tr2_class; - return run_command_v_opt_1(&cmd, opt); + run_command_set_opts(&cmd, opt); + return run_command(&cmd); } #ifndef NO_PTHREADS diff --git a/run-command.h b/run-command.h index 2574d46cb70..2b1fe3cde5c 100644 --- a/run-command.h +++ b/run-command.h @@ -228,12 +228,11 @@ int run_auto_maintenance(int quiet); #define RUN_GIT_CMD (1<<1) #define RUN_SILENT_EXEC_FAILURE (1<<2) #define RUN_USING_SHELL (1<<3) -#define RUN_CLEAN_ON_EXIT (1<<4) -#define RUN_WAIT_AFTER_CLEAN (1<<5) -#define RUN_CLOSE_OBJECT_STORE (1<<6) +#define RUN_WAIT_AFTER_CLEAN (1<<4) +#define RUN_CLOSE_OBJECT_STORE (1<<5) /** - * The run_command_v_opt*() API is a convenience wrapper for an + * The run_command_v_opt() function is a convenience wrapper for an * underlying run_command(). * * It's intended to be used when the user already has an "argv" they'd @@ -250,21 +249,9 @@ int run_auto_maintenance(int quiet); * - RUN_GIT_CMD: .git_cmd * - RUN_SILENT_EXEC_FAILURE: .silent_exec_failure * - RUN_USING_SHELL: .use_shell - * - RUN_CLEAN_ON_EXIT: .clean_on_exit - * - RUN_WAIT_AFTER_CLEAN: .wait_after_clean * - RUN_CLOSE_OBJECT_STORE: .close_object_store - * - * The argument dir corresponds the member .dir. The argument env - * corresponds to the member .env. */ int run_command_v_opt(const char **argv, int opt); -int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class); -/* - * env (the environment) is to be formatted like environ: "VAR=VALUE". - * To unset an environment variable use just "VAR". - */ -int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, - const char *const *env, const char *tr2_class); /** * The run_command_l_opt() function run_command_v_opt() takes a list From patchwork Mon Oct 17 17:49:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009193 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A31C4C4332F for ; Mon, 17 Oct 2022 17:50:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231124AbiJQRuI (ORCPT ); Mon, 17 Oct 2022 13:50:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230451AbiJQRtr (ORCPT ); Mon, 17 Oct 2022 13:49:47 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9327617887 for ; Mon, 17 Oct 2022 10:49:38 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id bu30so19586138wrb.8 for ; Mon, 17 Oct 2022 10:49:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RfABQ+K2gnW/3jQ18MR3JhVnEMMSZVxIPOYFnQSNeQc=; b=iO0ldZeCgwZG9bgIMER+zXtFqe79CaL9DVZwpad+wlYKtsPref5ostCq9rCYdEupiz grfTEVvzoiW+JtjVh1czrmP1mqxCFq4BkPw5wYwxqIZ0bd5iNefAJJq754F060oIRAlg tbLvd94XI546rmMvnLOn14aDieoSdVNElq7Lk+BZGq7zFpLNE4MxIByFQfv+XQ+oftIo 5BUb8K2RWtk13m2sNEpfG8GP56RSJGEAc6VKIdkY2f899kn2aCBpMbS2OhuHBia1CvVU gyKHaOyuZqulnnQzyRQ1egGjyT0V2cRINvuNZ/VAL05uvWO7wDcvGxMtWiWSMUmZsUDU kZqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RfABQ+K2gnW/3jQ18MR3JhVnEMMSZVxIPOYFnQSNeQc=; b=BcuFUnLPFC4Y2VukfPQSBTO/V6w6CdGlrm6HfMdcXAfCGOIPar2l14ArOej3eXHRaT iA91F4jByEbhJe+b1lzxiCy2KtNmLKxsYArcE6tRvvI+Vm996+ZD4k4aCXye/+mogYdy BojAGaC0naeZ548p+jkX71i4Suf0PZof9dOLJwceFmSOdB6XIMfkXpzdRdMiQLBTw85j EYmr8a6qS+nWHckoD3+j6dWUjQwXxZ2UJqBg9F7WPPGL/8PW8M4DFAaU7j1TxxEL1Nor 9W9ZDZj+nCpv5ZJRp4/aS649HKNZahgUUssQypaxP7W7Zo8MdD/DRAJJZkgCE0+6JAmQ vdKw== X-Gm-Message-State: ACrzQf16f5hGtdHGiFm9B/dFi7aUjLF87m1zLVhl5a5RjQJezxm0VCuP l7jcehSFUONcBSJNzm4DaE+rXh6gfU3h1A== X-Google-Smtp-Source: AMsMyM5S4nd5KGDCQX46GsJNY7Bz41W4kmEfto7GwddSC8JqejjwodbbJvofM/BCqrDezsC45OU4NQ== X-Received: by 2002:adf:dfc1:0:b0:22f:edf7:b11b with SMTP id q1-20020adfdfc1000000b0022fedf7b11bmr6894751wrn.583.1666028976518; Mon, 17 Oct 2022 10:49:36 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:35 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 09/10] gc: use strvec_pushf(), avoid redundant strbuf_detach() Date: Mon, 17 Oct 2022 19:49:20 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org 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 --- 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 "\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; } From patchwork Mon Oct 17 17:49:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 13009192 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3C74C433FE for ; Mon, 17 Oct 2022 17:50:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230457AbiJQRuH (ORCPT ); Mon, 17 Oct 2022 13:50:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230453AbiJQRtr (ORCPT ); Mon, 17 Oct 2022 13:49:47 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BACB9178A9 for ; Mon, 17 Oct 2022 10:49:38 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id a3so19681349wrt.0 for ; Mon, 17 Oct 2022 10:49:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BW69Gms6ZCN63XObi08yq0tn7lfTlwTRY0tybCpvj6U=; b=gdqszjxrye+H4mHeZS/6VBA5FTikz/kL/cDk3uRbD/oM9rE3aOVcS6ecORqqswpKUM +KYkWWMLO7kUq/Nrl5Y9Nvq/oTSzCLyCgWMxXU7CmLHC0En6TBNIga7oZ2dJi3QAl6+1 0eskz0oqtgYkRUDWwMw1S+MKUIxyFzN4jliE/+NdLUsgZJquv8Id6kj/M+SM6j3Itc0a PnAaUklcPqmMd6v596z2qwAJDbQoxYC+ciA+H7KOkyLQqwCItHlH9gOy2bJ1V4efPmvk LwoNeq5A1vmt8PwTkY9744cjyG2ySVDQM/uCbpv0b7M1zgJEyc2L0kFqVZyysEV8ryQp /GtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BW69Gms6ZCN63XObi08yq0tn7lfTlwTRY0tybCpvj6U=; b=7Vgw4Y5PVcspi5lGiXKl/1G5NFFSqtwgxDubUKuNPPVGFubG/fUo0XKwyimeU8NSee FWufqkiC4P52Z9vKKhNXi1Xw3DZrvfDmTbNzF6aERULI4dbxTXsK7orn/vWydhZ+eIxa YQ6XFhlxVtsypHYeyOsAEYb8TaXqEWIJHx/NwaQYKETbUem5nq2icXbesFt8ZlFnvO/b KCvnUdBjm/GJDeRb8yKE9plCnrQsqDQozPMTZOkDbn/Cds0RaRBAZqocdG3uin8uND2V /ikIcBOEludqESxXWuXM5i9mhsYBT3ftWF+O9iB4xC2rEM1HOPLXJbkFv6ZE7dVogdRm WHtg== X-Gm-Message-State: ACrzQf0m/SzM+D/r0l4milljx1P/jAaVvF8Et9mYeuWxt2BVxYyvjO+K HFsx99dNYWqcZ66syybcXAn7JbOTclFaqA== X-Google-Smtp-Source: AMsMyM7dURkj0dGpusTGKYhSsei51qpGiar20QdKU268OsyuTDa8K74QLzqxlVoQwWUIuL5Y7mwzxw== X-Received: by 2002:a5d:64ee:0:b0:22c:df95:532f with SMTP id g14-20020a5d64ee000000b0022cdf95532fmr6795504wri.61.1666028977697; Mon, 17 Oct 2022 10:49:37 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id e26-20020a05600c4b9a00b003a5537bb2besm10676023wmp.25.2022.10.17.10.49.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 10:49:36 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , =?utf-8?q?Ren=C3=A9_Scharfe?= , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= Subject: [PATCH v2 10/10] run-command API: add and use a run_command_sv_opt() Date: Mon, 17 Oct 2022 19:49:21 +0200 Message-Id: X-Mailer: git-send-email 2.38.0.1093.gcda8671d6c6 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add a run_command_sv_opt() convenience wrapper for run_command_v_opt(), as noted in the API documentation this is for the common case of wanting to construct a "struct strvec" to pass to run_command_v_opt(), and as it's a one-shot to strvec_clear() it afterwards. Let's convert those API users that were using a "ret" variable to carry over to "return" after a "strvec_clear()" to use this new function instead. Because we pass the "struct strvec *" to the function we can also avoid copying the arguments to the "args" member of the "struct child_process", as we were doing with run_command_v_opt(). Instead we can use memcpy() and strvec_clear() to do the moral equivalent of a strbuf_{detach,attach}(). The strvec API doesn't have a strvec_attach(), we could add it here while at it, but let's avoid generalizing the interface for now and migrate the "struct strvec *" in the "run_command_sv_opt()" instead. Let's leave aside the user in "builtin/bisect--helper.c"'s bisect_visualize(). There's an outstanding topic that's extensively modifying it. Signed-off-by: Ævar Arnfjörð Bjarmason --- add-interactive.c | 3 +-- builtin/add.c | 6 ++---- builtin/clone.c | 3 +-- builtin/gc.c | 5 +---- builtin/pull.c | 15 +++------------ builtin/remote.c | 5 +---- merge.c | 3 +-- run-command.c | 11 +++++++++++ run-command.h | 16 +++++++++++++++- scalar.c | 6 +----- sequencer.c | 11 ++--------- 11 files changed, 39 insertions(+), 45 deletions(-) diff --git a/add-interactive.c b/add-interactive.c index f071b2a1b4f..9c86f3b9532 100644 --- a/add-interactive.c +++ b/add-interactive.c @@ -1007,8 +1007,7 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps, if (files->selected[i]) strvec_push(&args, files->items.items[i].string); - res = run_command_v_opt(args.v, 0); - strvec_clear(&args); + res = run_command_sv_opt(&args, 0); } putchar('\n'); diff --git a/builtin/add.c b/builtin/add.c index f84372964c8..7c783eebc0e 100644 --- a/builtin/add.c +++ b/builtin/add.c @@ -240,7 +240,7 @@ static int refresh(int verbose, const struct pathspec *pathspec) int run_add_interactive(const char *revision, const char *patch_mode, const struct pathspec *pathspec) { - int status, i; + int i; struct strvec argv = STRVEC_INIT; int use_builtin_add_i = git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1); @@ -282,9 +282,7 @@ int run_add_interactive(const char *revision, const char *patch_mode, /* pass original pathspec, to be re-parsed */ strvec_push(&argv, pathspec->items[i].original); - status = run_command_v_opt(argv.v, RUN_GIT_CMD); - strvec_clear(&argv); - return status; + return run_command_sv_opt(&argv, RUN_GIT_CMD); } int interactive_add(const char **argv, const char *prefix, int patch) diff --git a/builtin/clone.c b/builtin/clone.c index a1514363190..8e43781e147 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -756,8 +756,7 @@ static int checkout(int submodule_progress, int filter_submodules) "--single-branch" : "--no-single-branch"); - err = run_command_v_opt(args.v, RUN_GIT_CMD); - strvec_clear(&args); + return run_command_sv_opt(&args, RUN_GIT_CMD); } return err; diff --git a/builtin/gc.c b/builtin/gc.c index 519e64e86ee..8393e19b504 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1899,7 +1899,6 @@ static int is_schtasks_available(void) static int schtasks_remove_task(enum schedule_priority schedule) { const char *cmd = "schtasks"; - int result; struct strvec args = STRVEC_INIT; const char *frequency = get_frequency(schedule); @@ -1909,9 +1908,7 @@ static int schtasks_remove_task(enum schedule_priority schedule) strvec_pushf(&args, SCHTASKS_NAME_FMT, frequency); strvec_pushl(&args, "/f", NULL); - result = run_command_v_opt(args.v, 0); - strvec_clear(&args); - return result; + return run_command_sv_opt(&args, 0); } static int schtasks_remove_tasks(void) diff --git a/builtin/pull.c b/builtin/pull.c index 403a24d7ca6..2f36823c97e 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -516,7 +516,6 @@ static void parse_repo_refspecs(int argc, const char **argv, const char **repo, static int run_fetch(const char *repo, const char **refspecs) { struct strvec args = STRVEC_INIT; - int ret; strvec_pushl(&args, "fetch", "--update-head-ok", NULL); @@ -582,9 +581,7 @@ static int run_fetch(const char *repo, const char **refspecs) strvec_pushv(&args, refspecs); } else if (*refspecs) BUG("refspecs without repo?"); - ret = run_command_v_opt(args.v, RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE); - strvec_clear(&args); - return ret; + return run_command_sv_opt(&args, RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE); } /** @@ -653,7 +650,6 @@ static int update_submodules(void) */ static int run_merge(void) { - int ret; struct strvec args = STRVEC_INIT; strvec_pushl(&args, "merge", NULL); @@ -696,9 +692,7 @@ static int run_merge(void) strvec_push(&args, "--allow-unrelated-histories"); strvec_push(&args, "FETCH_HEAD"); - ret = run_command_v_opt(args.v, RUN_GIT_CMD); - strvec_clear(&args); - return ret; + return run_command_sv_opt(&args, RUN_GIT_CMD); } /** @@ -879,7 +873,6 @@ static int get_rebase_newbase_and_upstream(struct object_id *newbase, static int run_rebase(const struct object_id *newbase, const struct object_id *upstream) { - int ret; struct strvec args = STRVEC_INIT; strvec_push(&args, "rebase"); @@ -913,9 +906,7 @@ static int run_rebase(const struct object_id *newbase, strvec_push(&args, oid_to_hex(upstream)); - ret = run_command_v_opt(args.v, RUN_GIT_CMD); - strvec_clear(&args); - return ret; + return run_command_sv_opt(&args, RUN_GIT_CMD); } static int get_can_ff(struct object_id *orig_head, diff --git a/builtin/remote.c b/builtin/remote.c index 910f7b9316a..5d3534db19c 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -1510,7 +1510,6 @@ static int update(int argc, const char **argv, const char *prefix) }; struct strvec fetch_argv = STRVEC_INIT; int default_defined = 0; - int retval; argc = parse_options(argc, argv, prefix, options, builtin_remote_update_usage, @@ -1536,9 +1535,7 @@ static int update(int argc, const char **argv, const char *prefix) } } - retval = run_command_v_opt(fetch_argv.v, RUN_GIT_CMD); - strvec_clear(&fetch_argv); - return retval; + return run_command_sv_opt(&fetch_argv, RUN_GIT_CMD); } static int remove_all_fetch_refspecs(const char *key) diff --git a/merge.c b/merge.c index 2382ff66d35..487debacecb 100644 --- a/merge.c +++ b/merge.c @@ -33,8 +33,7 @@ int try_merge_command(struct repository *r, for (j = remotes; j; j = j->next) strvec_push(&args, merge_argument(j->item)); - ret = run_command_v_opt(args.v, RUN_GIT_CMD); - strvec_clear(&args); + ret = run_command_sv_opt(&args, RUN_GIT_CMD); discard_index(r->index); if (repo_read_index(r) < 0) diff --git a/run-command.c b/run-command.c index 0066ace85fa..724fc581c89 100644 --- a/run-command.c +++ b/run-command.c @@ -1038,6 +1038,17 @@ int run_command_v_opt(const char **argv, int opt) return run_command(&cmd); } +int run_command_sv_opt(struct strvec *args, int opt) +{ + struct child_process cmd = CHILD_PROCESS_INIT; + + /* TODO: We could encapsulate this with a strvec_attach() */ + memcpy(&cmd.args, args, sizeof(*args)); + strvec_init(args); + run_command_set_opts(&cmd, opt); + return run_command(&cmd); +} + #ifndef NO_PTHREADS static pthread_t main_thread; static int main_thread_set; diff --git a/run-command.h b/run-command.h index 2b1fe3cde5c..8e8403f3bb3 100644 --- a/run-command.h +++ b/run-command.h @@ -151,7 +151,7 @@ struct child_process { /** * The functions: child_process_init, start_command, finish_command, - * run_command, run_command_l_opt, run_command_v_opt, + * run_command, run_command_l_opt, run_command_v_opt, run_command_sv_opt, * child_process_clear do the following: * * - If a system call failed, errno is set and -1 is returned. A diagnostic @@ -262,6 +262,20 @@ int run_command_v_opt(const char **argv, int opt); LAST_ARG_MUST_BE_NULL int run_command_l_opt(int opt, ...); +/** + * The run_command_sv_opt() function is a wrapper for + * run_command_v_opt(). It takes a "struct strvec *args" which + * similarly will be strvec_clear()'d before returning. + * + * Use it for the common case of constructing a "struct strvec" for a + * one-shot run_command_v_opt() invocation. + * + * The "args" will migrated the "cmd.args" member of an underlying + * "struct child_process", in a way that avoids making an extra copy. + */ +RESULT_MUST_BE_USED +int run_command_sv_opt(struct strvec *args, int opt); + /** * Execute the given command, sending "in" to its stdin, and capturing its * stdout and stderr in the "out" and "err" strbufs. Any of the three may diff --git a/scalar.c b/scalar.c index 6de9c0ee523..3480bf73cbd 100644 --- a/scalar.c +++ b/scalar.c @@ -72,7 +72,6 @@ static int run_git(const char *arg, ...) struct strvec argv = STRVEC_INIT; va_list args; const char *p; - int res; va_start(args, arg); strvec_push(&argv, arg); @@ -80,10 +79,7 @@ static int run_git(const char *arg, ...) strvec_push(&argv, p); va_end(args); - res = run_command_v_opt(argv.v, RUN_GIT_CMD); - - strvec_clear(&argv); - return res; + return run_command_sv_opt(&argv, RUN_GIT_CMD); } struct scalar_config { diff --git a/sequencer.c b/sequencer.c index 20495db9de2..7ee0e05512c 100644 --- a/sequencer.c +++ b/sequencer.c @@ -3183,7 +3183,6 @@ static int rollback_is_safe(void) static int reset_merge(const struct object_id *oid) { - int ret; struct strvec argv = STRVEC_INIT; strvec_pushl(&argv, "reset", "--merge", NULL); @@ -3191,10 +3190,7 @@ static int reset_merge(const struct object_id *oid) if (!is_null_oid(oid)) strvec_push(&argv, oid_to_hex(oid)); - ret = run_command_v_opt(argv.v, RUN_GIT_CMD); - strvec_clear(&argv); - - return ret; + return run_command_sv_opt(&argv, RUN_GIT_CMD); } static int rollback_single_pick(struct repository *r) @@ -4866,7 +4862,6 @@ static int pick_commits(struct repository *r, static int continue_single_pick(struct repository *r, struct replay_opts *opts) { struct strvec argv = STRVEC_INIT; - int ret; if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) @@ -4887,9 +4882,7 @@ static int continue_single_pick(struct repository *r, struct replay_opts *opts) */ strvec_pushl(&argv, "--no-edit", "--cleanup=strip", NULL); - ret = run_command_v_opt(argv.v, RUN_GIT_CMD); - strvec_clear(&argv); - return ret; + return run_command_sv_opt(&argv, RUN_GIT_CMD); } static int commit_staged_changes(struct repository *r,