From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org [IPv6:2604:1380:45e3:2400::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id C5F071F406 for ; Thu, 2 Nov 2023 13:52:47 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=SAVtH2pD; dkim-atps=neutral Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 84D81281BCD for ; Thu, 2 Nov 2023 13:52:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 260001C288; Thu, 2 Nov 2023 13:52:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SAVtH2pD" Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B776018E3E for ; Thu, 2 Nov 2023 13:52:18 +0000 (UTC) Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1FC2134 for ; Thu, 2 Nov 2023 06:52:15 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-32ded3eb835so643625f8f.0 for ; Thu, 02 Nov 2023 06:52:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698933133; x=1699537933; darn=vger.kernel.org; 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=ImVsE3pHpna1OMPcMzvPChWkRGt3L0Pv/Sl/c9RZsyI=; b=SAVtH2pDpJbU+WGzaaAvji4zLLFtNB1F5m2uljuV7S1bdVwRgJq1vBZ5/rkfdJvtvR PvWPICTA2qOlcpZal265L1pZ3LeMAEBiEpRJTiok5qSShTwESBO4+Q5LlYH/Y/QdP0v6 nPTFRMea8tFyRI5vCfhylXCRcUuiOYCl2lA75tWamDUY6kq32SYE6eqY/dg0TZBiS0ZO 7CYYxBbohuM96UNjUBEDZ5ALVxAFZno4r/eO46twU6jSGvJAns/LhKlr2qf7bM5LkDNJ Z2T6U5tUTyg5oZKrCCaGj5kL8TpYo8Fk1s8g3b7kkCmiFaSb5ifKfoTqLk+NyVWkxD0L avTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698933133; x=1699537933; 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=ImVsE3pHpna1OMPcMzvPChWkRGt3L0Pv/Sl/c9RZsyI=; b=CWlny5/MRGkvh8e/M5DIHKSDDJ6+uLdxH1cWZC+ZkTInGeOzuzhg8f8T30tj5VyBZ0 gJv//K6biDvcVaZA+FapEkECoe2z8tj0JGP7iZjsmddOBEXLiz+7y/ChOZtNP8j2GMu9 UZEHXap10hjemAVFlwPhboNmxTxf6zlHYTTYR5A3CpE5NYYq44d9ES/OCSZdPc0E2Kst hKvXopCC9fp24V605YJjc+s4tq7oqrxmsv0unyir3cfMAoTnI9QpJD7nToEsO+SOmp/X ofDxT3AevWUIYVVIgkgYanHIpdHx7mSDhpoG/14R/3ioQKRf/zOxFA0PVZTeyX8i23Vm szkQ== X-Gm-Message-State: AOJu0YwpZHmKVPWleR2P/Wcp6gMaeaIrDLwYZpzQPsFayUtqCABud2+e 2YtONaGmr7Rh0lgCQtHionmnJbaXubs= X-Google-Smtp-Source: AGHT+IHErN4F4/iWqxjZFhHXyOxMIHM4cc8uAeqcXzSjhhlx3TCyWDNK7hoD5yvXxy4WCM6Rfge8RA== X-Received: by 2002:a05:6000:b0a:b0:32d:b9c5:82fc with SMTP id dj10-20020a0560000b0a00b0032db9c582fcmr13881880wrb.36.1698933133362; Thu, 02 Nov 2023 06:52:13 -0700 (PDT) Received: from christian-Precision-5550.. ([2a04:cec0:116d:4e46:313c:1f52:4963:91b6]) by smtp.gmail.com with ESMTPSA id e6-20020adffc46000000b0030647449730sm2514996wrs.74.2023.11.02.06.52.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 06:52:12 -0700 (PDT) From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , Patrick Steinhardt , Johannes Schindelin , Elijah Newren , John Cai , Derrick Stolee , Phillip Wood , Calvin Wan , Toon Claes , Dragan Simic , Linus Arver , Christian Couder Subject: [PATCH v6 03/14] replay: start using parse_options API Date: Thu, 2 Nov 2023 14:51:40 +0100 Message-ID: <20231102135151.843758-4-christian.couder@gmail.com> X-Mailer: git-send-email 2.42.0.496.g529a7fda40 In-Reply-To: <20231102135151.843758-1-christian.couder@gmail.com> References: <20231010123847.2777056-1-christian.couder@gmail.com> <20231102135151.843758-1-christian.couder@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Unsubscribe-Post: List-Unsubscribe=One-Click From: Elijah Newren Instead of manually parsing arguments, let's start using the parse_options API. This way this new builtin will look more standard, and in some upcoming commits will more easily be able to handle more command line options. Note that we plan to later use standard revision ranges instead of hardcoded " " arguments. When we will use standard revision ranges, it will be easier to check if there are no spurious arguments if we keep ARGV[0], so let's call parse_options() with PARSE_OPT_KEEP_ARGV0 even if we don't need ARGV[0] right now to avoid some useless code churn. Co-authored-by: Christian Couder Signed-off-by: Elijah Newren Signed-off-by: Christian Couder --- builtin/replay.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/builtin/replay.c b/builtin/replay.c index f2d8444417..afabb844d3 100644 --- a/builtin/replay.c +++ b/builtin/replay.c @@ -15,7 +15,7 @@ #include "lockfile.h" #include "merge-ort.h" #include "object-name.h" -#include "read-cache-ll.h" +#include "parse-options.h" #include "refs.h" #include "revision.h" #include "sequencer.h" @@ -92,6 +92,7 @@ static struct commit *create_commit(struct tree *tree, int cmd_replay(int argc, const char **argv, const char *prefix) { struct commit *onto; + const char *onto_name = NULL; struct commit *last_commit = NULL, *last_picked_commit = NULL; struct object_id head; struct lock_file lock = LOCK_INIT; @@ -105,16 +106,32 @@ int cmd_replay(int argc, const char **argv, const char *prefix) struct strbuf branch_name = STRBUF_INIT; int ret = 0; - if (argc == 2 && !strcmp(argv[1], "-h")) { - printf("git replay --onto # EXPERIMENTAL\n"); - exit(129); + const char * const replay_usage[] = { + N_("git replay --onto # EXPERIMENTAL"), + NULL + }; + struct option replay_options[] = { + OPT_STRING(0, "onto", &onto_name, + N_("revision"), + N_("replay onto given commit")), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, replay_options, replay_usage, + PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT); + + if (!onto_name) { + error(_("option --onto is mandatory")); + usage_with_options(replay_usage, replay_options); } - if (argc != 5 || strcmp(argv[1], "--onto")) - die("usage: read the code, figure out how to use it, then do so"); + if (argc != 3) { + error(_("bad number of arguments")); + usage_with_options(replay_usage, replay_options); + } - onto = peel_committish(argv[2]); - strbuf_addf(&branch_name, "refs/heads/%s", argv[4]); + onto = peel_committish(onto_name); + strbuf_addf(&branch_name, "refs/heads/%s", argv[2]); /* Sanity check */ if (repo_get_oid(the_repository, "HEAD", &head)) @@ -126,6 +143,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix) BUG("Could not read index"); repo_init_revisions(the_repository, &revs, prefix); + revs.verbose_header = 1; revs.max_parents = 1; revs.cherry_mark = 1; @@ -134,7 +152,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix) revs.right_only = 1; revs.sort_order = REV_SORT_IN_GRAPH_ORDER; revs.topo_order = 1; - strvec_pushl(&rev_walk_args, "", argv[4], "--not", argv[3], NULL); + + strvec_pushl(&rev_walk_args, "", argv[2], "--not", argv[1], NULL); if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1) { ret = error(_("unhandled options")); @@ -197,8 +216,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix) &last_commit->object.oid, &last_picked_commit->object.oid, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { - error(_("could not update %s"), argv[4]); - die("Failed to update %s", argv[4]); + error(_("could not update %s"), argv[2]); + die("Failed to update %s", argv[2]); } if (create_symref("HEAD", branch_name.buf, reflog_msg.buf) < 0) die(_("unable to update HEAD")); @@ -210,8 +229,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix) &last_commit->object.oid, &head, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { - error(_("could not update %s"), argv[4]); - die("Failed to update %s", argv[4]); + error(_("could not update %s"), argv[2]); + die("Failed to update %s", argv[2]); } } ret = (result.clean == 0); -- 2.42.0.496.g529a7fda40