From patchwork Thu Jan 7 10:36:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 12003527 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88151C433E0 for ; Thu, 7 Jan 2021 10:38:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3C85D2312F for ; Thu, 7 Jan 2021 10:38:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727932AbhAGKiP (ORCPT ); Thu, 7 Jan 2021 05:38:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726522AbhAGKiO (ORCPT ); Thu, 7 Jan 2021 05:38:14 -0500 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3423AC0612FC for ; Thu, 7 Jan 2021 02:37:10 -0800 (PST) Received: by mail-pl1-x636.google.com with SMTP id g3so3284546plp.2 for ; Thu, 07 Jan 2021 02:37:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wdLUcs3RHXTDanAA+JFePnfaSbgyLTYONomgKCN8nyY=; b=IIKcrxK/yzdHoiEDXZf65PA0f6LvutURcAoh+eAf1aU+SZo4pjXAA9jZlcExUNwU+P I1CT7tFJwCMWze5jRGDeQEiqqwaIi8/w26A7ykXMZjgx0H+6Hcaue9Ua9sTA/bY3I9CJ gWEXdUfk68q+lUmMSQs8oZ0mkXP9kU6avRCDVGupqZwAIEGaDJc1nukUAxe+XVlFe5A2 XXzK4JCCSffKcuWgo8X5Be8doTu/NZ2iGa7gUhj4vVCx6Tf+4QYtt/DWS5zn9fShrOdk VXLdlYtp2vhjr5S7HaU+71ijS8BiuqqmPNjlcaXGTVcg9dvIz0x/Qo+3RApXkI1Ta0Vj H4ZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wdLUcs3RHXTDanAA+JFePnfaSbgyLTYONomgKCN8nyY=; b=sSa+6fjzI+EA9AqA2c1gDBinUuFDjl/iFCmn2tBTIDuIRHcYg2MqGRz3hAxzFejLBz rbWYVZ6nxxrPrOcverqABqEY6ll3KVwx3JnshvAENWdh95In8HTqCHBHlSmdxGQIj7yH vSILXijXWwgCDJj5OrZ1UtRarrCPH0xcGJBccrX4RyUDZi4xuhxqBVfDRlv9YgyDudS/ 4ibKGVmgVcyv7M2CLT3eMPW4fd+AhxXIoDTN/jEQjCjCyU+2cSE4YXeC/lNSaOkS43ua BCvitJIE+MOOjFeEQ4dKHcKeNNhgmfgS+2C7tYmrOl5Jd2zqZPWjpaMl+f2cDbVZrdjY DeCQ== X-Gm-Message-State: AOAM531vI7f97Wc8wwctoED6Ld+qgQ+IQQNiAKA2+8iBybvRK25JPPsZ B0ZCDIBLDUiIwBdHw8E9F9kG3Ivy2aI= X-Google-Smtp-Source: ABdhPJxtqSf7hsuyOYyO7eN2LOoSSLXzX8Xr+FpwJBFUaY2FIIzlVI+dlyKOsDPH4F4CU3K8OgmnHg== X-Received: by 2002:a17:90b:4d09:: with SMTP id mw9mr7854478pjb.199.1610015829509; Thu, 07 Jan 2021 02:37:09 -0800 (PST) Received: from archbookpro.hsd1.ca.comcast.net ([2601:647:4201:c540::414c]) by smtp.gmail.com with ESMTPSA id bg20sm1777416pjb.6.2021.01.07.02.37.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 02:37:08 -0800 (PST) From: Denton Liu To: Git Mailing List Cc: =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?q?SZEDER?= =?utf-8?q?_G=C3=A1bor?= , Junio C Hamano Subject: [PATCH v3 1/2] refs: factor out set_read_ref_cutoffs() Date: Thu, 7 Jan 2021 02:36:58 -0800 Message-Id: <8f14ec39970b6cbf9b6615485316063306706e6a.1610015769.git.liu.denton@gmail.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This block of code is duplicated twice. In a future commit, it will be duplicated for a third time. Factor out the common functionality into set_read_ref_cutoffs(). In the case of read_ref_at_ent(), we are incrementing `cb->reccnt` at the beginning of the function. Move these to right before the return so that the `cb->reccnt - 1` is changed to `cb->reccnt` and it can be cleanly factored out into set_read_ref_cutoffs(). The duplication of the increment statements will be removed in a future patch. Signed-off-by: Denton Liu --- refs.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/refs.c b/refs.c index 13dc2c3291..bfdd04aefd 100644 --- a/refs.c +++ b/refs.c @@ -882,25 +882,30 @@ struct read_ref_at_cb { int *cutoff_cnt; }; +static void set_read_ref_cutoffs(struct read_ref_at_cb *cb, + timestamp_t timestamp, int tz, const char *message) +{ + if (cb->msg) + *cb->msg = xstrdup(message); + if (cb->cutoff_time) + *cb->cutoff_time = timestamp; + if (cb->cutoff_tz) + *cb->cutoff_tz = tz; + if (cb->cutoff_cnt) + *cb->cutoff_cnt = cb->reccnt; +} + static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid, const char *email, timestamp_t timestamp, int tz, const char *message, void *cb_data) { struct read_ref_at_cb *cb = cb_data; - cb->reccnt++; cb->tz = tz; cb->date = timestamp; if (timestamp <= cb->at_time || cb->cnt == 0) { - if (cb->msg) - *cb->msg = xstrdup(message); - if (cb->cutoff_time) - *cb->cutoff_time = timestamp; - if (cb->cutoff_tz) - *cb->cutoff_tz = tz; - if (cb->cutoff_cnt) - *cb->cutoff_cnt = cb->reccnt - 1; + set_read_ref_cutoffs(cb, timestamp, tz, message); /* * we have not yet updated cb->[n|o]oid so they still * hold the values for the previous record. @@ -917,11 +922,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid, warning(_("log for ref %s unexpectedly ended on %s"), cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); + cb->reccnt++; oidcpy(&cb->ooid, ooid); oidcpy(&cb->noid, noid); cb->found_it = 1; return 1; } + cb->reccnt++; oidcpy(&cb->ooid, ooid); oidcpy(&cb->noid, noid); if (cb->cnt > 0) @@ -935,14 +942,7 @@ static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid { struct read_ref_at_cb *cb = cb_data; - if (cb->msg) - *cb->msg = xstrdup(message); - if (cb->cutoff_time) - *cb->cutoff_time = timestamp; - if (cb->cutoff_tz) - *cb->cutoff_tz = tz; - if (cb->cutoff_cnt) - *cb->cutoff_cnt = cb->reccnt; + set_read_ref_cutoffs(cb, timestamp, tz, message); oidcpy(cb->oid, ooid); if (is_null_oid(cb->oid)) oidcpy(cb->oid, noid); From patchwork Thu Jan 7 10:36:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 12003529 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5D581C433DB for ; Thu, 7 Jan 2021 10:38:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 13C9023142 for ; Thu, 7 Jan 2021 10:38:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727937AbhAGKiP (ORCPT ); Thu, 7 Jan 2021 05:38:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727327AbhAGKiO (ORCPT ); Thu, 7 Jan 2021 05:38:14 -0500 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83538C0612FD for ; Thu, 7 Jan 2021 02:37:11 -0800 (PST) Received: by mail-pf1-x42c.google.com with SMTP id t22so3613965pfl.3 for ; Thu, 07 Jan 2021 02:37:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mg0L325WpXh8JpTEiT9H1jmvZ2S6ZfP1FQtd/xmStvQ=; b=U5lkI3eWuzyOWdLhayjAGTuNsqngRenZTFemkeqYtlKzFuGX/H9bzLiPziCEgMtzmn WB0sYtJT2v3J5nNWiPaLRRlclhcGVoSA/7XssxMVioQW5M/kCkcBlO9Wqch9pJJXQ8be VJ6/8KNZS13P/RDyBy8Wuy1AbRO3hxPWau7+7lDwLuHUpBLG5LNOLIQUBzSs/a5lZ8Ob 7tMw7hiPTqdApTdi/j6kv/CByfh1UpbAzLrZLAO+7S4QxPsOI7mG1hMtAq8qwNhwtC12 zGaooQsoTLka7ImqI5tm87X4x5aRey7daSa/eeqigPi0wvwyg/sZonHWrOFwyNYmec3L zCXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mg0L325WpXh8JpTEiT9H1jmvZ2S6ZfP1FQtd/xmStvQ=; b=mhzjv1lyZ8G0gnhPvCZ1UkgRKH+dtDKypoa1wwFpsXkb5Xtx0vKzjaPzItQrxbOUkF QG4KZok8yois3HEip5jYjr3hRoZRwaByd+wZahB5aCk28G4yFnajzs0hruRUbnSiqCKs KwFiIRHek6lR/+FP48ExSz8nNIbkxGptUlBnMVOWB9ZLLcQLZ1shtwNLqYNwWPcakG/k f4blL4CqAYCmV6a/4h0FdTWb0Q8yEA1HUyCQ7e4EnABlD0VwHOo6g6+vVx/e2avjN40U TobnNL1Rkkw4649vrLFqBY6G8FimCCELv4j/aI/XAGJYQfIHQSoxKdJWuwkykYEFcYC5 ZC0A== X-Gm-Message-State: AOAM531oQuxW4XRnPjQsrrSSYKWowTUj0Mr6kUpE8s9K1LR/v8kXOCuq 5MA5Tu8JHNluQswNcvO+GpvHgtBenfE= X-Google-Smtp-Source: ABdhPJxbZb0Z9N+OIMQVYSi5hkqKKwrxQmgL2t4gpoFZVhfVd9wNrYjEkebvSdQ/tDI/W6oEgHpdGg== X-Received: by 2002:a63:515f:: with SMTP id r31mr1245939pgl.210.1610015830738; Thu, 07 Jan 2021 02:37:10 -0800 (PST) Received: from archbookpro.hsd1.ca.comcast.net ([2601:647:4201:c540::414c]) by smtp.gmail.com with ESMTPSA id bg20sm1777416pjb.6.2021.01.07.02.37.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Jan 2021 02:37:10 -0800 (PST) From: Denton Liu To: Git Mailing List Cc: =?utf-8?q?Martin_=C3=85gren?= , =?utf-8?q?SZEDER?= =?utf-8?q?_G=C3=A1bor?= , Junio C Hamano Subject: [PATCH v3 2/2] refs: allow @{n} to work with n-sized reflog Date: Thu, 7 Jan 2021 02:36:59 -0800 Message-Id: X-Mailer: git-send-email 2.30.0 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This sequence works $ git checkout -b newbranch $ git commit --allow-empty -m one $ git show -s newbranch@{1} and shows the state that was immediately after the newbranch was created. But then if you do $ git reflog expire --expire=now refs/heads/newbranch $ git commit --allow=empty -m two $ git show -s newbranch@{1} you'd be scolded with fatal: log for 'newbranch' only has 1 entries While it is true that it has only 1 entry, we have enough information in that single entry that records the transition between the state in which the tip of the branch was pointing at commit 'one' to the new commit 'two' built on it, so we should be able to answer "what object newbranch was pointing at?". But we refuse to do so. Make @{0} the special case where we use the new side to look up that entry. Otherwise, look up @{n} using the old side of the (n-1)th entry of the reflog. Suggested-by: Junio C Hamano Signed-off-by: Denton Liu --- refs.c | 46 ++++++++++++++++++++++++++----------- t/t1503-rev-parse-verify.sh | 4 ++-- t/t1508-at-combinations.sh | 12 ++++++++++ 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/refs.c b/refs.c index bfdd04aefd..e70dcd33f7 100644 --- a/refs.c +++ b/refs.c @@ -900,40 +900,53 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid, const char *message, void *cb_data) { struct read_ref_at_cb *cb = cb_data; + int reached_count; cb->tz = tz; cb->date = timestamp; - if (timestamp <= cb->at_time || cb->cnt == 0) { + /* + * It is not possible for cb->cnt == 0 on the first itertion because + * that special case is handled in read_ref_at(). + */ + if (cb->cnt > 0) + cb->cnt--; + reached_count = cb->cnt == 0 && !is_null_oid(ooid); + if (timestamp <= cb->at_time || reached_count) { set_read_ref_cutoffs(cb, timestamp, tz, message); /* * we have not yet updated cb->[n|o]oid so they still * hold the values for the previous record. */ - if (!is_null_oid(&cb->ooid)) { - oidcpy(cb->oid, noid); - if (!oideq(&cb->ooid, noid)) - warning(_("log for ref %s has gap after %s"), + if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid)) + warning(_("log for ref %s has gap after %s"), cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); - } - else if (cb->date == cb->at_time) + if (reached_count) + oidcpy(cb->oid, ooid); + else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time) oidcpy(cb->oid, noid); else if (!oideq(noid, cb->oid)) warning(_("log for ref %s unexpectedly ended on %s"), cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822))); - cb->reccnt++; - oidcpy(&cb->ooid, ooid); - oidcpy(&cb->noid, noid); cb->found_it = 1; - return 1; } cb->reccnt++; oidcpy(&cb->ooid, ooid); oidcpy(&cb->noid, noid); - if (cb->cnt > 0) - cb->cnt--; - return 0; + return cb->found_it; +} + +static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid, + const char *email, timestamp_t timestamp, + int tz, const char *message, void *cb_data) +{ + struct read_ref_at_cb *cb = cb_data; + + set_read_ref_cutoffs(cb, timestamp, tz, message); + oidcpy(cb->oid, noid); + /* We just want the first entry */ + return 1; } static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid, @@ -967,6 +980,11 @@ int read_ref_at(struct ref_store *refs, const char *refname, cb.cutoff_cnt = cutoff_cnt; cb.oid = oid; + if (cb.cnt == 0) { + refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb); + return 0; + } + refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb); if (!cb.reccnt) { diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh index dc9fe3cbf1..a7e9b4863d 100755 --- a/t/t1503-rev-parse-verify.sh +++ b/t/t1503-rev-parse-verify.sh @@ -86,8 +86,8 @@ test_expect_success 'fails silently when using -q' ' test_expect_success 'fails silently when using -q with deleted reflogs' ' ref=$(git rev-parse HEAD) && git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" && - git reflog delete --updateref --rewrite refs/test@{0} && - test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 && + git reflog delete --updateref --rewrite refs/test@{1} && + test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 && test_must_be_empty error ' diff --git a/t/t1508-at-combinations.sh b/t/t1508-at-combinations.sh index 4a9964e9dc..528a77287c 100755 --- a/t/t1508-at-combinations.sh +++ b/t/t1508-at-combinations.sh @@ -99,4 +99,16 @@ test_expect_success 'create path with @' ' check "@:normal" blob content check "@:fun@ny" blob content +test_expect_success '@{1} works with only one reflog entry' ' + git checkout -B newbranch master && + git reflog expire --expire=now refs/heads/newbranch && + git commit --allow-empty -m "first after expiration" && + test_cmp_rev newbranch~ newbranch@{1} +' + +test_expect_success '@{0} works with empty reflog' ' + git checkout -B newbranch master && + git reflog expire --expire=now refs/heads/newbranch && + test_cmp_rev newbranch newbranch@{0} +' test_done