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_HI, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org [IPv6:2604:1380:45d1:ec00::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 0B2CC1F44D for ; Tue, 23 Apr 2024 21:28:48 +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=d5cPgxIe; 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 5ECF61C22D1A for ; Tue, 23 Apr 2024 21:28:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4EFCA143C5C; Tue, 23 Apr 2024 21:28:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="d5cPgxIe" Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62E7D143881 for ; Tue, 23 Apr 2024 21:28:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713907707; cv=none; b=nHFyfAt4UEZuMjG4wzTTBpanlwDlm3HHuIoOWFsbKjGrQ8N1GapSyhfXObV7tV5fqeGriUUajbKB6nJie5DalNXCQety4LUnJsZSlWc0EPjxDmUiylrM7+yK6Dke41HxCT0f6xON6yOmztUVQIz/Gku+cWBfDOhE+yxnL+e1cDY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713907707; c=relaxed/simple; bh=w4pqoQmkFvDucu77CL+TWFf6zJwyuHSUQhx/CRY89NU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nxNCRVFXo+x97qjubrf0iDh2T5VZOoaWkTKrdh5XvYDoaYnlzxoD+6oLfJBnUqyY1JVNO/1vXZvLtyHXoytsw7oUdfvBzg52LCb/03SOTVBAiepnWC++TyMkZWoLxIbJLtEr9N4RuNJ+qScUEAINUOd6yBnfNYAUafSh/SEZ1dQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=d5cPgxIe; arc=none smtp.client-ip=209.85.218.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-a5883518135so121225166b.3 for ; Tue, 23 Apr 2024 14:28:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713907704; x=1714512504; 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=hy+xrnGgkhMGybj2Hd78neFd9IvH29GqM7hPGHzaINw=; b=d5cPgxIeaignKY61irV+fztPrXXJLeM/BbhkXQhNnT1OX7RzoYgl0ZkUOivbzOAxOF 5aGM4EkllNS4eehvMQ4Xy7D9GX37sUOpM+Q/VvMFJY/UznnC/Ff0hDCnqvhrlIIRfA+R HT5BtWsT22GthWIEwyKmB639+6BKyxa2GpEG5pqN9Hj1vFc1slbvdSpGxg20PjN8ieG5 1iHUt3S6U+3DTJ1WNAvI+HnLyWT2xHk1bLz8fbOP29R0gU9dILQr1P+xRku4qnAOnkZM Qc3oVOuY7rbnVeKUu+XHJMsQTIITwm91mRYKxhfYmCWwUI+h7xDxZUDO72dEC1Grhedd tQFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713907704; x=1714512504; 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=hy+xrnGgkhMGybj2Hd78neFd9IvH29GqM7hPGHzaINw=; b=j5fh1+UFE6jgG23SeoLGvee89OACXWamjkx5PtAKkwBrf/p1sr0+V/vBWBEg+KEU1u c7cCbgI5RrpeVIFt55M8PLMSIOub8L2JRaHPDkmpgWssPw5FUrC+H3rcmfoudimFzxtv aLHY45lRXjzMpEtDy1C5hA1XikiragVJlqdAepPGSZJX+2FxnEqQ/58HL6ghTpCS1dsS OZqedTcaCQj0GgH+9mXjhjQ27/XVSZ82fnMkpfOuE+3I8lAB4pB7eBcfclUsJ+4unIWQ INWvigoqDNppZDtycxFd29AZo+pduaUJ3B4EMQPwUi8Z4A33oedFvtDNUP6VDDLka+jy nDeQ== X-Forwarded-Encrypted: i=1; AJvYcCWJuDLCJwUA6WsejU3LhvIf3+M91O+S8pOM+H4ObVfWcCahJ102oaZmTlr42ruy0X0ODcR5EhX522H4aSxRmnv43pNE X-Gm-Message-State: AOJu0Yz7ADhTJo2P25dmH2be+nPegnSmsKn1Q138FLIKzEUNVc9aacke rk+Yly182H1X9TXzPO6/uVGw6/J9+KGyiopViHDXFZLFgIOeR52Y X-Google-Smtp-Source: AGHT+IGzzw0mp8IVassIWnA6sHejJhC7RUZ2cE+3FS0tHOJH+t+ifY6omDB2vx1AcLTCkXeSGqUZXw== X-Received: by 2002:a17:906:aace:b0:a55:5a28:cd53 with SMTP id kt14-20020a170906aace00b00a555a28cd53mr382102ejb.8.1713907703457; Tue, 23 Apr 2024 14:28:23 -0700 (PDT) Received: from laptop.fritz.box ([2a02:2455:826e:4900:23ba:342:e06:b489]) by smtp.gmail.com with ESMTPSA id f17-20020a17090660d100b00a587236e646sm1864275ejk.174.2024.04.23.14.28.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 14:28:23 -0700 (PDT) From: Karthik Nayak X-Google-Original-From: Karthik Nayak To: karthik.188@gmail.com Cc: chris.torek@gmail.com, git@vger.kernel.org, gitster@pobox.com, ps@pks.im Subject: [PATCH v3 2/8] update-ref: support parsing ref targets in `parse_next_oid` Date: Tue, 23 Apr 2024 23:28:12 +0200 Message-ID: <20240423212818.574123-3-knayak@gitlab.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240423212818.574123-1-knayak@gitlab.com> References: <20240412095908.1134387-1-knayak@gitlab.com> <20240423212818.574123-1-knayak@gitlab.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Karthik Nayak The `parse_next_oid` is used for parsing the next oid present in the input buffer. Extend this function to also parse refnames in the form `ref:`. This will be used in the upcoming commits to add symref support to the existing update-ref commands. Since `parse_next_oid` now also parses refs apart from oids, we rename the function to a more apt name `parse_next_arg`. Signed-off-by: Karthik Nayak --- builtin/update-ref.c | 58 ++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/builtin/update-ref.c b/builtin/update-ref.c index 21fdbf6ac8..98ec356394 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -88,6 +88,11 @@ static char *parse_refname(const char **next) */ #define PARSE_SHA1_ALLOW_EMPTY 0x02 +/* + * Parse refname targets using the ref: format. + */ +#define PARSE_REFNAME_TARGETS 0x04 + /* * Parse an argument separator followed by the next argument, if any. * If there is an argument, convert it to a SHA-1, write it to sha1, @@ -95,10 +100,13 @@ static char *parse_refname(const char **next) * return 0. If there is no argument at all (not even the empty * string), return 1 and leave *next unchanged. If the value is * provided but cannot be converted to a SHA-1, die. flags can - * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY. + * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY and/or + * PARSE_REFNAME_TARGETS. When PARSE_REFNAME_TARGETS is set, parse + * the argument as `ref:` and store the refname into + * the target strbuf. */ -static int parse_next_oid(const char **next, const char *end, - struct object_id *oid, +static int parse_next_arg(const char **next, const char *end, + struct object_id *oid, struct strbuf *target, const char *command, const char *refname, int flags) { @@ -118,8 +126,17 @@ static int parse_next_oid(const char **next, const char *end, (*next)++; *next = parse_arg(*next, &arg); if (arg.len) { - if (repo_get_oid(the_repository, arg.buf, oid)) - goto invalid; + if (repo_get_oid(the_repository, arg.buf, oid)) { + const char *value; + if (flags & PARSE_REFNAME_TARGETS && + skip_prefix(arg.buf, "ref:", &value)) { + if (check_refname_format(value, REFNAME_ALLOW_ONELEVEL)) + die("invalid ref format: %s", value); + strbuf_addstr(target, value); + } else { + goto invalid; + } + } } else { /* Without -z, an empty value means all zeros: */ oidclr(oid); @@ -136,8 +153,17 @@ static int parse_next_oid(const char **next, const char *end, *next += arg.len; if (arg.len) { - if (repo_get_oid(the_repository, arg.buf, oid)) - goto invalid; + if (repo_get_oid(the_repository, arg.buf, oid)) { + const char *value; + if (flags & PARSE_REFNAME_TARGETS && + skip_prefix(arg.buf, "ref:", &value)) { + if (check_refname_format(value, REFNAME_ALLOW_ONELEVEL)) + die("invalid ref format: %s", value); + strbuf_addstr(target, value); + } else { + goto invalid; + } + } } else if (flags & PARSE_SHA1_ALLOW_EMPTY) { /* With -z, treat an empty value as all zeros: */ warning("%s %s: missing , treating as zero", @@ -192,12 +218,12 @@ static void parse_cmd_update(struct ref_transaction *transaction, if (!refname) die("update: missing "); - if (parse_next_oid(&next, end, &new_oid, "update", refname, - PARSE_SHA1_ALLOW_EMPTY)) + if (parse_next_arg(&next, end, &new_oid, NULL, + "update", refname, PARSE_SHA1_ALLOW_EMPTY)) die("update %s: missing ", refname); - have_old = !parse_next_oid(&next, end, &old_oid, "update", refname, - PARSE_SHA1_OLD); + have_old = !parse_next_arg(&next, end, &old_oid, NULL, + "update", refname, PARSE_SHA1_OLD); if (*next != line_termination) die("update %s: extra input: %s", refname, next); @@ -225,7 +251,7 @@ static void parse_cmd_create(struct ref_transaction *transaction, if (!refname) die("create: missing "); - if (parse_next_oid(&next, end, &new_oid, "create", refname, 0)) + if (parse_next_arg(&next, end, &new_oid, NULL, "create", refname, 0)) die("create %s: missing ", refname); if (is_null_oid(&new_oid)) @@ -256,8 +282,8 @@ static void parse_cmd_delete(struct ref_transaction *transaction, if (!refname) die("delete: missing "); - if (parse_next_oid(&next, end, &old_oid, "delete", refname, - PARSE_SHA1_OLD)) { + if (parse_next_arg(&next, end, &old_oid, NULL, + "delete", refname, PARSE_SHA1_OLD)) { have_old = 0; } else { if (is_null_oid(&old_oid)) @@ -289,8 +315,8 @@ static void parse_cmd_verify(struct ref_transaction *transaction, if (!refname) die("verify: missing "); - if (parse_next_oid(&next, end, &old_oid, "verify", refname, - PARSE_SHA1_OLD)) + if (parse_next_arg(&next, end, &old_oid, NULL, + "verify", refname, PARSE_SHA1_OLD)) oidclr(&old_oid); if (*next != line_termination) -- 2.43.GIT