From patchwork Wed Jan 22 07:51:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meet Soni X-Patchwork-Id: 13946974 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 8E2861C1F27 for ; Wed, 22 Jan 2025 07:52:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737532381; cv=none; b=KlkMsJhI69EUz1tLR/K+Pw3Mj9KOz7gHirFn+xGF2outeDcXXLx+/rzVXFRzyoXrWbEgfCS8VWmPo2FDcasPmyt9sm/pvJttMT6pgjEjiXJvh1z7bXHNrEh7jI19IGIGXsUcxq87sDBG+Yk1yXTr2h+kWofT1qsKHw+EYt8JEDU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737532381; c=relaxed/simple; bh=HlTQqT6mSg7y2tTmbsaijEx3ewXGv45+eLLcrEJBQ8U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WpnUsWoU4XZElrpIantD2feo3k4x7EGWHZDRZMSBHka4aSdCGOX00wQ2v4+0qX/h49xrOEK/vyCJY4HDSPOD6iJo1GDciDw+Ik9j8uq7+yedejOdgbZe17otGBbE3Uu7dI+fr4LLsrbqJCP1Z2akFWvPqWx9GuCamVOm97KCE4E= 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=YtllUqTg; arc=none smtp.client-ip=209.85.214.177 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YtllUqTg" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21a7ed0155cso106196005ad.3 for ; Tue, 21 Jan 2025 23:52:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737532378; x=1738137178; 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=GxM0PfrEgSIO5fAbpftuW0vwVbj96S5P+kMWkRByfow=; b=YtllUqTgNPrla+lWrMGMBwFqep0NQdJbG68MVmZb1zkMgnd0/VpKL9CdjWrgkEsPj1 OfrUR4c8SZDfcvXsecxnPmgLE02Ut1sGZLZ+FCBf2tQKS7PurXK9WIcKSUYYeFqQvjB2 EllMM4vALXAHXDOYP/ACCkhXqIAMnX4NCV2jWgO3/886UNiAtRcS88RKVD8QYEsqGW6Y rzVKYN+MaGvsDlqO1wIZphwgZorqLiMudMjU/RAyF44fj02n/Rg8DfgISNNzJy4U31xN WnFaUm5FP2fswY2RvZfEWUPQoNq51v1DFaCSjKDPmcaY3TwPkK87phyiulP/GoZGNwhz O/0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737532378; x=1738137178; 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=GxM0PfrEgSIO5fAbpftuW0vwVbj96S5P+kMWkRByfow=; b=YdZ6qpqT3l6iD/OfDI8SXQkPiI+h8RYf8HtNxrvpP1xxI5sWoyCcn/kYwFvR2CrV6B CMeDyvidxX3WKHkile/hxtD2inItPUMSsknmlaO45U7FUBfFMywmbCGgREE2KYFpcSHn WARdQtuKZe5HqCASJNMtYO0de8dp9qEq+nsUJiRwnV1LVK/kWOKWyP143EH7sJ+rnfps xuTOSexjHNsfi3Kb93mAsomIxJ+Sf+QwJ1elt+sTMQr/JVj0WazKRjPROJ5nl0LEzPSS TcpDvovSsV8shVq0VV65SIU3AAkyBcEy8HbMBiyFoNZSwKqXc1gIXAz1PiOBkYdu8WVR h9Tg== X-Gm-Message-State: AOJu0Yzxr3Y/5oAdl1m7x6oZoNXSxeJOGukPGzRk6cVFu7ZU4NIZve34 1XgBJQXzua9CQFW+mzXsXN2ta5c/ScIN39vi35u6F8oass6Yxpfng3rbklQ1FTk= X-Gm-Gg: ASbGncs3qNd5wQP5Mzwi75obaWksHHD0QCk0TvKKtYa8UvEAJuR0fVKsgFCir90nIE1 hwIhCNTTWHsGjnYqUuA0Rt782xgy8JijTSULU0amwOUmiBl61bdMT1fRqmR/mjwg1HnX+eoQSfl uh97Hlw9AsRnkVlRZcUCwHWuU2sIvQlhijzQ0HVq4Q3FqD687l5Ppflj0jgR5m7pLbHc+3Z0Kqu WZyl1wjDeavkdw+QLjKzTNOdn9mWfegDve7IqfQ9Z9yGcp6FInLaGbLttqZE/glMKln X-Google-Smtp-Source: AGHT+IGWhIjyODzxsCgViF1hvLd0lGjSQBsYaHa6gldAZLmPpPu5iJFn4b0lkvSQOqCk/fciW1EvwA== X-Received: by 2002:a17:902:f644:b0:211:8404:a957 with SMTP id d9443c01a7336-21c355f6aa9mr360933365ad.41.1737532378308; Tue, 21 Jan 2025 23:52:58 -0800 (PST) Received: from meet.. ([103.240.169.233]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2d3acce3sm89284155ad.119.2025.01.21.23.52.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 23:52:57 -0800 (PST) From: Meet Soni To: git@vger.kernel.org Cc: shubham.kanodia10@gmail.com, Meet Soni , Patrick Steinhardt , Jeff King , Jacob Keller , Jacob Keller , Pavel Rappo , Junio C Hamano Subject: [RFC PATCH 1/3] refspec: relocate omit_name_by_refspec and related functions Date: Wed, 22 Jan 2025 13:21:52 +0530 Message-Id: <20250122075154.5697-2-meetsoni3017@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250122075154.5697-1-meetsoni3017@gmail.com> References: <20250122075154.5697-1-meetsoni3017@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move the functions `omit_name_by_refspec()`, `refspec_match()`, and `match_name_with_pattern()` from `remote.c` to `refspec.c`. These functions focus on refspec matching, so placing them in `refspec.c` aligns with the separation of concerns. Keep refspec-related logic in `refspec.c` and remote-specific logic in `remote.c` for better code organization. Signed-off-by: Meet Soni --- refspec.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ refspec.h | 8 ++++++++ remote.c | 48 ------------------------------------------------ remote.h | 6 ------ 4 files changed, 56 insertions(+), 54 deletions(-) diff --git a/refspec.c b/refspec.c index 6d86e04442..66989a1d75 100644 --- a/refspec.c +++ b/refspec.c @@ -276,3 +276,51 @@ void refspec_ref_prefixes(const struct refspec *rs, } } } + +int match_name_with_pattern(const char *key, const char *name, + const char *value, char **result) +{ + const char *kstar = strchr(key, '*'); + size_t klen; + size_t ksuffixlen; + size_t namelen; + int ret; + if (!kstar) + die(_("key '%s' of pattern had no '*'"), key); + klen = kstar - key; + ksuffixlen = strlen(kstar + 1); + namelen = strlen(name); + ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && + !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); + if (ret && value) { + struct strbuf sb = STRBUF_INIT; + const char *vstar = strchr(value, '*'); + if (!vstar) + die(_("value '%s' of pattern has no '*'"), value); + strbuf_add(&sb, value, vstar - value); + strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen); + strbuf_addstr(&sb, vstar + 1); + *result = strbuf_detach(&sb, NULL); + } + return ret; +} + +static int refspec_match(const struct refspec_item *refspec, + const char *name) +{ + if (refspec->pattern) + return match_name_with_pattern(refspec->src, name, NULL, NULL); + + return !strcmp(refspec->src, name); +} + +int omit_name_by_refspec(const char *name, struct refspec *rs) +{ + int i; + + for (i = 0; i < rs->nr; i++) { + if (rs->items[i].negative && refspec_match(&rs->items[i], name)) + return 1; + } + return 0; +} diff --git a/refspec.h b/refspec.h index 69d693c87d..9556d08bd5 100644 --- a/refspec.h +++ b/refspec.h @@ -71,4 +71,12 @@ struct strvec; void refspec_ref_prefixes(const struct refspec *rs, struct strvec *ref_prefixes); +/* + * Check whether a name matches any negative refspec in rs. Returns 1 if the + * name matches at least one negative refspec, and 0 otherwise. + */ +int omit_name_by_refspec(const char *name, struct refspec *rs); +int match_name_with_pattern(const char *key, const char *name, + const char *value, char **result); + #endif /* REFSPEC_H */ diff --git a/remote.c b/remote.c index 0f6fba8562..40c2418065 100644 --- a/remote.c +++ b/remote.c @@ -907,54 +907,6 @@ void ref_push_report_free(struct ref_push_report *report) } } -static int match_name_with_pattern(const char *key, const char *name, - const char *value, char **result) -{ - const char *kstar = strchr(key, '*'); - size_t klen; - size_t ksuffixlen; - size_t namelen; - int ret; - if (!kstar) - die(_("key '%s' of pattern had no '*'"), key); - klen = kstar - key; - ksuffixlen = strlen(kstar + 1); - namelen = strlen(name); - ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && - !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); - if (ret && value) { - struct strbuf sb = STRBUF_INIT; - const char *vstar = strchr(value, '*'); - if (!vstar) - die(_("value '%s' of pattern has no '*'"), value); - strbuf_add(&sb, value, vstar - value); - strbuf_add(&sb, name + klen, namelen - klen - ksuffixlen); - strbuf_addstr(&sb, vstar + 1); - *result = strbuf_detach(&sb, NULL); - } - return ret; -} - -static int refspec_match(const struct refspec_item *refspec, - const char *name) -{ - if (refspec->pattern) - return match_name_with_pattern(refspec->src, name, NULL, NULL); - - return !strcmp(refspec->src, name); -} - -int omit_name_by_refspec(const char *name, struct refspec *rs) -{ - int i; - - for (i = 0; i < rs->nr; i++) { - if (rs->items[i].negative && refspec_match(&rs->items[i], name)) - return 1; - } - return 0; -} - struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs) { struct ref **tail; diff --git a/remote.h b/remote.h index bda10dd5c8..0d109fa9c9 100644 --- a/remote.h +++ b/remote.h @@ -261,12 +261,6 @@ int resolve_remote_symref(struct ref *ref, struct ref *list); */ struct ref *ref_remove_duplicates(struct ref *ref_map); -/* - * Check whether a name matches any negative refspec in rs. Returns 1 if the - * name matches at least one negative refspec, and 0 otherwise. - */ -int omit_name_by_refspec(const char *name, struct refspec *rs); - /* * Remove all entries in the input list which match any negative refspec in * the refspec list. From patchwork Wed Jan 22 07:51:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meet Soni X-Patchwork-Id: 13946975 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 D999CE57D for ; Wed, 22 Jan 2025 07:53:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737532394; cv=none; b=Wa0inXZ57c7OAEYH2iHTE12D6eURCiCjQK9W50zJnvBbZufevp01Iyhsvj6+TYAlMoq7TSS6mJTuMfAWTvSWwixiubo2vm4Teul6bFbHshShqk2wZ9ztU84X4jHYTR+trY7h6m2ihyMRZ8h5ReRUVtbtrxL5AZYIEqIM0BJZ2fM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737532394; c=relaxed/simple; bh=RM9oxGvIddV6y0RTRNUr/6IFUdaZHAYwOD4WpppURKM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=W1qw5JR/zmw8pnGE8Sxfgt1Tvk2KShyb4nAh5xapTTwSI69IgASYlmHYUe5+JluwqbZKS3gXgiembs2uhYfWFy/1DNLIOFC9Y56aBUgXXKZ7iHwGFMu8M3l13CQlDyHaR8UCXEyfa6kLy9THnnsn6nIJworSiUJKKkw1VxSntDw= 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=NFmri7vL; arc=none smtp.client-ip=209.85.214.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NFmri7vL" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-21670dce0a7so136927935ad.1 for ; Tue, 21 Jan 2025 23:53:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737532391; x=1738137191; 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=RKWIurZ/gr8rxXXZJw9UR1eZYaROcq+xytn8YmmF7ng=; b=NFmri7vLlPzAOema+Ut5oTM+T2ZIziWE7+Ug/MbrptdwNgbkqa6ruyfvbNN3tuvW3P pB0gjiGOOIvxRIVBKuNV6VwT6EQ/+peq7uAekoM4/UVcTDQa9s9vZH76/ih7M2EB20Cv w6NxoXSRmHsJT2VRqyFnZSCWlwW9IsakuQjrFoX0KRM9h+B0l47W3DXiDkVBzN96RxLa SauGvxVfFdLkYJ0JQLL/vDau4BjwdnqF9fMRFy+U1qqkwE7SRbpJebNDyKu0KgMsVy0G RRWjQLAmB1mNgu3Dhv9MLk78aD2aa6YqjykxPqIDD1UoHfwaWnuiKMTedhdY3ENTy69N cizw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737532391; x=1738137191; 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=RKWIurZ/gr8rxXXZJw9UR1eZYaROcq+xytn8YmmF7ng=; b=VSpW1b6KRrTxq78tDZv8TfQMrbkI1kq5gLV4/TvDfW4W9PiAdIsH2iwHryvulvhJ+c jwQlv/9DpLBgVtg6yTQ7j3aXjt4REOM+ZAlbqDhv+/Xq9IJmFGl9uP3mIwh8AX3yG3dU wSguQrc2vADw/UxYW56XJ4mbUry1z2UH9/9pwhDe/OcZ9PLkQZqTIZdKulPoo+gmWCtY 9HQrpywXikUVd8st4P4PniD2e6ZEJdsBaiSaUBkvwD+WDHMG3t8qw011iHZ1z9UeLn3s fP+wb1nQNO9lzwnOXpZHLWbbM+nO/uARQ98JTtISdnx31eaktlSsLEZT9X/koJFTCuGO +3DA== X-Gm-Message-State: AOJu0YxD4qAlPF6okl9AsieOLBslVmcaumFPKqK3l0WWSU+ehfJFim2j 6wLDAd8Cq77izCjz3cgnQi+ShPpZWij67b6812QdI7wT7wC5ZxLrycnmCm6R X-Gm-Gg: ASbGncuMWoNA8arSnV4reLNeutYivvsX9sayPKbIdxAiLysEkrfyoS+q7QkM8+Npxkx MSx7VcZStK0nGx+4Xej5xcKV/lA1BEWpUu06AuP+SupzgR8yaP5cdfP0o0K9ByknZgjzxnqpLJr VgyvPJzyR1lOsSA+2ZhQAsInNM1hYTeGe/tO2TAoVHIk/RqDE/sCjENj0JcFIf2YEM7Tw6DMy0A I3McoOHULDjn8SfvxmN1+YjMN8nvxVbGPIyCoOCovrQm8vBAVgsxHXsJ00/BPc+7N1E X-Google-Smtp-Source: AGHT+IEuQENaoE8yL/tBUGK1ncueNp8NyVLwESDqSxY3c5iuPXcY5JZV3z/rN0ke/zpr9SuQAuh50g== X-Received: by 2002:a17:902:f54e:b0:215:72aa:693f with SMTP id d9443c01a7336-21c352de455mr265651085ad.9.1737532391076; Tue, 21 Jan 2025 23:53:11 -0800 (PST) Received: from meet.. ([103.240.169.233]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2d3acce3sm89284155ad.119.2025.01.21.23.53.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 23:53:10 -0800 (PST) From: Meet Soni To: git@vger.kernel.org Cc: shubham.kanodia10@gmail.com, Meet Soni , Elijah Newren , Nipunn Koorapati , Jacob Keller , Junio C Hamano Subject: [RFC PATCH 2/3] refspec: relocate query related functions Date: Wed, 22 Jan 2025 13:21:53 +0530 Message-Id: <20250122075154.5697-3-meetsoni3017@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250122075154.5697-1-meetsoni3017@gmail.com> References: <20250122075154.5697-1-meetsoni3017@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move the functions `query_refspecs()`, `query_refspecs_multiple()` and `query_matches_negative_refspec()` from `remote.c` to `refspec.c`. These functions focus on querying refspecs, so centralizing them in `refspec.c` improves code organization by keeping refspec-related logic in one place. Signed-off-by: Meet Soni --- refspec.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ refspec.h | 7 ++++ remote.c | 122 ----------------------------------------------------- remote.h | 1 - 4 files changed, 130 insertions(+), 123 deletions(-) diff --git a/refspec.c b/refspec.c index 66989a1d75..72b3911110 100644 --- a/refspec.c +++ b/refspec.c @@ -5,6 +5,7 @@ #include "gettext.h" #include "hash.h" #include "hex.h" +#include "string-list.h" #include "strvec.h" #include "refs.h" #include "refspec.h" @@ -324,3 +325,125 @@ int omit_name_by_refspec(const char *name, struct refspec *rs) } return 0; } + +static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query) +{ + int i, matched_negative = 0; + int find_src = !query->src; + struct string_list reversed = STRING_LIST_INIT_DUP; + const char *needle = find_src ? query->dst : query->src; + + /* + * Check whether the queried ref matches any negative refpsec. If so, + * then we should ultimately treat this as not matching the query at + * all. + * + * Note that negative refspecs always match the source, but the query + * item uses the destination. To handle this, we apply pattern + * refspecs in reverse to figure out if the query source matches any + * of the negative refspecs. + * + * The first loop finds and expands all positive refspecs + * matched by the queried ref. + * + * The second loop checks if any of the results of the first loop + * match any negative refspec. + */ + for (i = 0; i < rs->nr; i++) { + struct refspec_item *refspec = &rs->items[i]; + char *expn_name; + + if (refspec->negative) + continue; + + /* Note the reversal of src and dst */ + if (refspec->pattern) { + const char *key = refspec->dst ? refspec->dst : refspec->src; + const char *value = refspec->src; + + if (match_name_with_pattern(key, needle, value, &expn_name)) + string_list_append_nodup(&reversed, expn_name); + } else if (refspec->matching) { + /* For the special matching refspec, any query should match */ + string_list_append(&reversed, needle); + } else if (!refspec->src) { + BUG("refspec->src should not be null here"); + } else if (!strcmp(needle, refspec->src)) { + string_list_append(&reversed, refspec->src); + } + } + + for (i = 0; !matched_negative && i < reversed.nr; i++) { + if (omit_name_by_refspec(reversed.items[i].string, rs)) + matched_negative = 1; + } + + string_list_clear(&reversed, 0); + + return matched_negative; +} + +void query_refspecs_multiple(struct refspec *rs, + struct refspec_item *query, + struct string_list *results) +{ + int i; + int find_src = !query->src; + + if (find_src && !query->dst) + BUG("query_refspecs_multiple: need either src or dst"); + + if (query_matches_negative_refspec(rs, query)) + return; + + for (i = 0; i < rs->nr; i++) { + struct refspec_item *refspec = &rs->items[i]; + const char *key = find_src ? refspec->dst : refspec->src; + const char *value = find_src ? refspec->src : refspec->dst; + const char *needle = find_src ? query->dst : query->src; + char **result = find_src ? &query->src : &query->dst; + + if (!refspec->dst || refspec->negative) + continue; + if (refspec->pattern) { + if (match_name_with_pattern(key, needle, value, result)) + string_list_append_nodup(results, *result); + } else if (!strcmp(needle, key)) { + string_list_append(results, value); + } + } +} + +int query_refspecs(struct refspec *rs, struct refspec_item *query) +{ + int i; + int find_src = !query->src; + const char *needle = find_src ? query->dst : query->src; + char **result = find_src ? &query->src : &query->dst; + + if (find_src && !query->dst) + BUG("query_refspecs: need either src or dst"); + + if (query_matches_negative_refspec(rs, query)) + return -1; + + for (i = 0; i < rs->nr; i++) { + struct refspec_item *refspec = &rs->items[i]; + const char *key = find_src ? refspec->dst : refspec->src; + const char *value = find_src ? refspec->src : refspec->dst; + + if (!refspec->dst || refspec->negative) + continue; + if (refspec->pattern) { + if (match_name_with_pattern(key, needle, value, result)) { + query->force = refspec->force; + return 0; + } + } else if (!strcmp(needle, key)) { + *result = xstrdup(value); + query->force = refspec->force; + return 0; + } + } + return -1; +} diff --git a/refspec.h b/refspec.h index 9556d08bd5..d3c97bfdc5 100644 --- a/refspec.h +++ b/refspec.h @@ -1,6 +1,8 @@ #ifndef REFSPEC_H #define REFSPEC_H +#include "string-list.h" + #define TAG_REFSPEC "refs/tags/*:refs/tags/*" /** @@ -79,4 +81,9 @@ int omit_name_by_refspec(const char *name, struct refspec *rs); int match_name_with_pattern(const char *key, const char *name, const char *value, char **result); +int query_refspecs(struct refspec *rs, struct refspec_item *query); +void query_refspecs_multiple(struct refspec *rs, + struct refspec_item *query, + struct string_list *results); + #endif /* REFSPEC_H */ diff --git a/remote.c b/remote.c index 40c2418065..2c46611821 100644 --- a/remote.c +++ b/remote.c @@ -925,128 +925,6 @@ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs) return ref_map; } -static int query_matches_negative_refspec(struct refspec *rs, struct refspec_item *query) -{ - int i, matched_negative = 0; - int find_src = !query->src; - struct string_list reversed = STRING_LIST_INIT_DUP; - const char *needle = find_src ? query->dst : query->src; - - /* - * Check whether the queried ref matches any negative refpsec. If so, - * then we should ultimately treat this as not matching the query at - * all. - * - * Note that negative refspecs always match the source, but the query - * item uses the destination. To handle this, we apply pattern - * refspecs in reverse to figure out if the query source matches any - * of the negative refspecs. - * - * The first loop finds and expands all positive refspecs - * matched by the queried ref. - * - * The second loop checks if any of the results of the first loop - * match any negative refspec. - */ - for (i = 0; i < rs->nr; i++) { - struct refspec_item *refspec = &rs->items[i]; - char *expn_name; - - if (refspec->negative) - continue; - - /* Note the reversal of src and dst */ - if (refspec->pattern) { - const char *key = refspec->dst ? refspec->dst : refspec->src; - const char *value = refspec->src; - - if (match_name_with_pattern(key, needle, value, &expn_name)) - string_list_append_nodup(&reversed, expn_name); - } else if (refspec->matching) { - /* For the special matching refspec, any query should match */ - string_list_append(&reversed, needle); - } else if (!refspec->src) { - BUG("refspec->src should not be null here"); - } else if (!strcmp(needle, refspec->src)) { - string_list_append(&reversed, refspec->src); - } - } - - for (i = 0; !matched_negative && i < reversed.nr; i++) { - if (omit_name_by_refspec(reversed.items[i].string, rs)) - matched_negative = 1; - } - - string_list_clear(&reversed, 0); - - return matched_negative; -} - -static void query_refspecs_multiple(struct refspec *rs, - struct refspec_item *query, - struct string_list *results) -{ - int i; - int find_src = !query->src; - - if (find_src && !query->dst) - BUG("query_refspecs_multiple: need either src or dst"); - - if (query_matches_negative_refspec(rs, query)) - return; - - for (i = 0; i < rs->nr; i++) { - struct refspec_item *refspec = &rs->items[i]; - const char *key = find_src ? refspec->dst : refspec->src; - const char *value = find_src ? refspec->src : refspec->dst; - const char *needle = find_src ? query->dst : query->src; - char **result = find_src ? &query->src : &query->dst; - - if (!refspec->dst || refspec->negative) - continue; - if (refspec->pattern) { - if (match_name_with_pattern(key, needle, value, result)) - string_list_append_nodup(results, *result); - } else if (!strcmp(needle, key)) { - string_list_append(results, value); - } - } -} - -int query_refspecs(struct refspec *rs, struct refspec_item *query) -{ - int i; - int find_src = !query->src; - const char *needle = find_src ? query->dst : query->src; - char **result = find_src ? &query->src : &query->dst; - - if (find_src && !query->dst) - BUG("query_refspecs: need either src or dst"); - - if (query_matches_negative_refspec(rs, query)) - return -1; - - for (i = 0; i < rs->nr; i++) { - struct refspec_item *refspec = &rs->items[i]; - const char *key = find_src ? refspec->dst : refspec->src; - const char *value = find_src ? refspec->src : refspec->dst; - - if (!refspec->dst || refspec->negative) - continue; - if (refspec->pattern) { - if (match_name_with_pattern(key, needle, value, result)) { - query->force = refspec->force; - return 0; - } - } else if (!strcmp(needle, key)) { - *result = xstrdup(value); - query->force = refspec->force; - return 0; - } - } - return -1; -} - char *apply_refspecs(struct refspec *rs, const char *name) { struct refspec_item query; diff --git a/remote.h b/remote.h index 0d109fa9c9..f3da64dc41 100644 --- a/remote.h +++ b/remote.h @@ -267,7 +267,6 @@ struct ref *ref_remove_duplicates(struct ref *ref_map); */ struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs); -int query_refspecs(struct refspec *rs, struct refspec_item *query); char *apply_refspecs(struct refspec *rs, const char *name); int check_push_refs(struct ref *src, struct refspec *rs); From patchwork Wed Jan 22 07:51:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Meet Soni X-Patchwork-Id: 13946976 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 D68C9E57D for ; Wed, 22 Jan 2025 07:53:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737532398; cv=none; b=IWmHksykzsEFizgn71VVXsRwtk6bNfirA/TgOs3AgSzkaqUn2/+l7uBo8M/HgxEFR/ZNmLLBIi3/1HaUIx/hWqd7zyNUURQ/a9eQ3G7vv8aSs/lwx54cv0FH1qZ45CdC62Wo94tvmIphzs+M2ARqWH+E1w/YKeUjCVzqCahgDjg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737532398; c=relaxed/simple; bh=P9PyOLm13BcMyHTDXYBSupefqWXBfFM3LIhi8AvL8jw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PiTeRRLdBUWeBjOYlAqNqKHS44ZKKH3tH9Mp67OXHIgRgLUX6Fo/zrPYxIUCxea5wRieMJVrVPivR0xkFW0dpoclOTAFoPd6O1cEjpdaRbqC9+Tc7do0rZrdtP5U5G/qIuYVKKGsvmYrF8zr3Xixv0T699cYhUZ8neUjUqprjWY= 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=Jr2c8VZx; arc=none smtp.client-ip=209.85.214.181 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Jr2c8VZx" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2161eb94cceso82185245ad.2 for ; Tue, 21 Jan 2025 23:53:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737532395; x=1738137195; 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=Y2j14fQmQ8WlL5R0Z6RIc/P5QGsyK62aUlqlruP1d3E=; b=Jr2c8VZxL8UakrUjU07AzXhsaka9RaqTms5m2CGEQtnLIv68esqMWOPhCry3e9mSwW 0KLkl7TStGr410S/DAY9wBByzZD0alTHbiSWan72plMtfIj4yde28lypaxw/DgoQVkXE 7p26T05irzH1z/Z+Q2TCRA1f3HKK+s5hmj0MW/oMp61g1FEY4DAoGs+cCRc1wpViOydN 7wzWxz0vtdnqrwQu55IX84Na86TNa7qwJY+kzP/r+atmvh3fTe3489in08Q378FQxKmQ yENOXeetUFjmZu1fhl6CTI5NcxXxkWS6Y7QzRMW1hBX/6zNLIhz2Ye09sRZgtE2Wllgn pSOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737532395; x=1738137195; 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=Y2j14fQmQ8WlL5R0Z6RIc/P5QGsyK62aUlqlruP1d3E=; b=T4ACXz9TTkTr99KMeJH2jRbiL2Ea4TwRWDc53uAts02OxZ4kzJxkUL8asLILG6ul2S D7UwKirCzJk9DLe/brO0yT43Jl/QOZsR1GIG1PWJDK7qzbxt8JriGDzDdLALLkvc+xq6 YujhvnhmFvVh7LBP7kmIzaTnLUGTMzyxKKb2LiYbdX/SMmnpJ8aNwvWmAWJW/aoPPac5 H0tXiF9XAyUS0GqI/Ce5wh+CHn9QQhmedoMoDRmUoJAHcbwrk3KfPnrw4hVU/EUZuej+ fBmGHtKyS1oULx5isbhVG1tmlFJGleXdLA7fkpaGtAQTSlU3SnpLNH86bCHF9lWEz0QR zrKw== X-Gm-Message-State: AOJu0YycbfzFFa5sX+ZQX0M16HFmbIVKbXp16ttWbSJKaYGpv5wSK0cz KX41Hwsq++fkc7YM+ehTm2jWD7/393iRJNA2rW5TIsx80kHyMhgE3joLVLOJzOk= X-Gm-Gg: ASbGncsSwdhdjvc/cb609GAJQ+fzdZwAdZI4Y9Edj8wtSvTj/L6/mIGl7ENnVHPdMgD 0/x8yROiaUJ51PHAl4j5wGBeV2s7eZk+/IDkr97TJd/Vz4Ry7MufdNuifW5g3QGKuWS0LdhYXzw MlNkHlu9hkhd9/majFSnsEjy3Zc12e/6xadS/IpJ1UdfM2SUYYNqb5bNMGQf/rgi4fhzIqb4AIL nIMK2PQ1nKu5LwBbhgwVe4sgAjB3E1vEWzA5DT7Ubd8hbuggy6Ybuo/Q6qUYeptHVDX X-Google-Smtp-Source: AGHT+IHrgTcE9zZkDPf9Ae0vOzWL0OaYJkGTgQ1oVTLVD5rThhGfiZvrhUwZJsqTiMa4rSsi/x5Cmw== X-Received: by 2002:a17:903:703:b0:216:31aa:1308 with SMTP id d9443c01a7336-21c355e35c9mr247081835ad.34.1737532395296; Tue, 21 Jan 2025 23:53:15 -0800 (PST) Received: from meet.. ([103.240.169.233]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2d3acce3sm89284155ad.119.2025.01.21.23.53.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2025 23:53:14 -0800 (PST) From: Meet Soni To: git@vger.kernel.org Cc: shubham.kanodia10@gmail.com, Meet Soni , Elijah Newren , Jeff King , Junio C Hamano , Jacob Keller , Patrick Steinhardt Subject: [RFC PATCH 3/3] refspec: relocate apply_refspecs and related funtions Date: Wed, 22 Jan 2025 13:21:54 +0530 Message-Id: <20250122075154.5697-4-meetsoni3017@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250122075154.5697-1-meetsoni3017@gmail.com> References: <20250122075154.5697-1-meetsoni3017@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move the functions `apply_refspecs()` and `apply_negative_refspecs()` from `remote.c` to `refspec.c`. These functions focus on applying refspecs, so centralizing them in `refspec.c` improves code organization by keeping refspec-related logic in one place. Signed-off-by: Meet Soni --- refspec.c | 32 ++++++++++++++++++++++++++++++++ refspec.h | 8 ++++++++ remote.c | 31 ------------------------------- remote.h | 8 -------- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/refspec.c b/refspec.c index 72b3911110..d279d6032a 100644 --- a/refspec.c +++ b/refspec.c @@ -9,6 +9,7 @@ #include "strvec.h" #include "refs.h" #include "refspec.h" +#include "remote.h" #include "strbuf.h" /* @@ -447,3 +448,34 @@ int query_refspecs(struct refspec *rs, struct refspec_item *query) } return -1; } + +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs) +{ + struct ref **tail; + + for (tail = &ref_map; *tail; ) { + struct ref *ref = *tail; + + if (omit_name_by_refspec(ref->name, rs)) { + *tail = ref->next; + free(ref->peer_ref); + free(ref); + } else + tail = &ref->next; + } + + return ref_map; +} + +char *apply_refspecs(struct refspec *rs, const char *name) +{ + struct refspec_item query; + + memset(&query, 0, sizeof(struct refspec_item)); + query.src = (char *)name; + + if (query_refspecs(rs, &query)) + return NULL; + + return query.dst; +} diff --git a/refspec.h b/refspec.h index d3c97bfdc5..294068d226 100644 --- a/refspec.h +++ b/refspec.h @@ -86,4 +86,12 @@ void query_refspecs_multiple(struct refspec *rs, struct refspec_item *query, struct string_list *results); +/* + * Remove all entries in the input list which match any negative refspec in + * the refspec list. + */ +struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs); + +char *apply_refspecs(struct refspec *rs, const char *name); + #endif /* REFSPEC_H */ diff --git a/remote.c b/remote.c index 2c46611821..641dd1125f 100644 --- a/remote.c +++ b/remote.c @@ -907,37 +907,6 @@ void ref_push_report_free(struct ref_push_report *report) } } -struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs) -{ - struct ref **tail; - - for (tail = &ref_map; *tail; ) { - struct ref *ref = *tail; - - if (omit_name_by_refspec(ref->name, rs)) { - *tail = ref->next; - free(ref->peer_ref); - free(ref); - } else - tail = &ref->next; - } - - return ref_map; -} - -char *apply_refspecs(struct refspec *rs, const char *name) -{ - struct refspec_item query; - - memset(&query, 0, sizeof(struct refspec_item)); - query.src = (char *)name; - - if (query_refspecs(rs, &query)) - return NULL; - - return query.dst; -} - int remote_find_tracking(struct remote *remote, struct refspec_item *refspec) { return query_refspecs(&remote->fetch, refspec); diff --git a/remote.h b/remote.h index f3da64dc41..b4bb16af0e 100644 --- a/remote.h +++ b/remote.h @@ -261,14 +261,6 @@ int resolve_remote_symref(struct ref *ref, struct ref *list); */ struct ref *ref_remove_duplicates(struct ref *ref_map); -/* - * Remove all entries in the input list which match any negative refspec in - * the refspec list. - */ -struct ref *apply_negative_refspecs(struct ref *ref_map, struct refspec *rs); - -char *apply_refspecs(struct refspec *rs, const char *name); - int check_push_refs(struct ref *src, struct refspec *rs); int match_push_refs(struct ref *src, struct ref **dst, struct refspec *rs, int flags);