From patchwork Thu Sep 10 19:03:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768833 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E31F14F6 for ; Thu, 10 Sep 2020 19:06:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A2B721D81 for ; Thu, 10 Sep 2020 19:06:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="u4Zhh1zt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726898AbgIJTGQ (ORCPT ); Thu, 10 Sep 2020 15:06:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725933AbgIJTEC (ORCPT ); Thu, 10 Sep 2020 15:04:02 -0400 Received: from mail-lf1-x143.google.com (mail-lf1-x143.google.com [IPv6:2a00:1450:4864:20::143]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 108F5C061756 for ; Thu, 10 Sep 2020 12:04:02 -0700 (PDT) Received: by mail-lf1-x143.google.com with SMTP id y2so4164001lfy.10 for ; Thu, 10 Sep 2020 12:04:01 -0700 (PDT) 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=Is63qXrgEioUL+Hsy1MzTxoyErJDjmHx/ZVlOmLE17w=; b=u4Zhh1ztXtELIQBq93kJ6DYj/K2n2sq0qKgcLXjt2rpXe2hPNEjj3zSQiJMYiKyMqX g3QVALetEKj8C9Ikan87Nm3GYE1xhs209skxnEcE6+a3TeSC+dj+7GTUoSnJmfksqcGV 3ikc90NWNTJGiHJTheCUnoaMkeVp2tOF1VzJfcvUxtSjpl4v+MBDyJjgm5IOQqrN991/ qbliE2EHvToR6SxR9DNy0ZiR03hgm0EeUdA1UBego1UIeHwy2cV4isU4if/HddfPeEHZ k7q047sHlXrmzq+kXFv4CqZn6dAdjOLeNurkp1BY7VOzmwd42J0s0Jchr6SUFu/owVLu P7WA== 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=Is63qXrgEioUL+Hsy1MzTxoyErJDjmHx/ZVlOmLE17w=; b=pxxevNDTYYEoS2EWCxXgpyZ+zCw+2nK+hC37uO6UTbLkdQcUhHEonhwqcjtBFnalTY JHgFFaC0K4XCuzqYwbQI9TZusY2Me1M4QtvqNcLhvZVAhj2rx09moSxVMbArIQ1q7tqc f9doha+CIZdfIdrpZ+9gCtZ9yo6GCYO3ugNV545bNxYsFXBirWpeohFPhyS+BUp2veA5 uRBqmAX0umv1zlK9bFISTsTvuZwb2dmGcX4ZDogxpuNFvuL9GNtDT3XXBSLfdqyIhGtC rnYSgch66UDuyTpMn6Fe8xooyl7hSjRUSrxKwOFaLvKW3jBs/3TaxvQ5loEis6GWEjip Po7g== X-Gm-Message-State: AOAM5313CU5QtXLnIk/pq9o+YK0nl+sEPtJoHOKd2l+vW1KbNkV63h2L pkEQ7IjYfWM9lXUSFASc3hMeN/Vxlmc= X-Google-Smtp-Source: ABdhPJwQJFLG/AtI7gw56WAMcYZsuKzka4ksCjGTRkBqO48OBRlw7Q/wJFlwCeL82xbpxACRDNttAg== X-Received: by 2002:a05:6512:5cd:: with SMTP id o13mr4800653lfo.171.1599764640154; Thu, 10 Sep 2020 12:04:00 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.03.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:03:59 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 1/8] wt-status: replace sha1 mentions with oid Date: Thu, 10 Sep 2020 21:03:35 +0200 Message-Id: <734ea28effdcb8650ab5e6ac2aa5398400faf1c5.1599762679.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org `abbrev_sha1_in_line()` uses a `struct object_id oid` and should be fully prepared to handle non-SHA1 object ids. Rename it to `abbrev_oid_in_line()`. A few comments in `wt_status_get_detached_from()` mention "sha1". The variable they refer to was renamed in e86ab2c1cd ("wt-status: convert to struct object_id", 2017-02-21). Update the comments to reference "oid" instead. Signed-off-by: Martin Ågren --- wt-status.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wt-status.c b/wt-status.c index c560cbe860..59be457015 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1232,7 +1232,7 @@ static int split_commit_in_progress(struct wt_status *s) * The function assumes that the line does not contain useless spaces * before or after the command. */ -static void abbrev_sha1_in_line(struct strbuf *line) +static void abbrev_oid_in_line(struct strbuf *line) { struct strbuf **split; int i; @@ -1282,7 +1282,7 @@ static int read_rebase_todolist(const char *fname, struct string_list *lines) strbuf_trim(&line); if (!line.len) continue; - abbrev_sha1_in_line(&line); + abbrev_oid_in_line(&line); string_list_append(lines, line.buf); } fclose(f); @@ -1575,9 +1575,9 @@ static void wt_status_get_detached_from(struct repository *r, } if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref) == 1 && - /* sha1 is a commit? match without further lookup */ + /* oid is a commit? match without further lookup */ (oideq(&cb.noid, &oid) || - /* perhaps sha1 is a tag, try to dereference to a commit */ + /* perhaps oid is a tag, try to dereference to a commit */ ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL && oideq(&cb.noid, &commit->object.oid)))) { const char *from = ref; From patchwork Thu Sep 10 19:03:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768835 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5690759D for ; Thu, 10 Sep 2020 19:06:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2B0A421D7E for ; Thu, 10 Sep 2020 19:06:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bUwV4D1E" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726512AbgIJTGL (ORCPT ); Thu, 10 Sep 2020 15:06:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725913AbgIJTEE (ORCPT ); Thu, 10 Sep 2020 15:04:04 -0400 Received: from mail-lf1-x143.google.com (mail-lf1-x143.google.com [IPv6:2a00:1450:4864:20::143]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61242C061757 for ; Thu, 10 Sep 2020 12:04:04 -0700 (PDT) Received: by mail-lf1-x143.google.com with SMTP id x69so4200966lff.3 for ; Thu, 10 Sep 2020 12:04:04 -0700 (PDT) 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=WiR7Hb4i3YmxO4bXLMHG8A8PTI3jQ/wOd4ke80+KSQo=; b=bUwV4D1ECxc/q5lHG7Ifl1tTCtanGudACt2OINh4JPB7Y50z2m9ONcEGKTRzfGges/ WM3IfkZftx6d/CUXgD4CbfLjsl2OEJzxPC9vp/et6ls+YE7gBNC0vpXiwgQ87jzApGsh Iq07b84VVMer9wPKF4Aqg754a22464ISZMa39x+tOo8gGKnU0ClDIL7uMLeSipGJfc94 sES9n1pDSMZTslUHQOtIgTBmS6j2q2ne4IGhORGs5YNzPqtK2/ScvfLuhZiOWI6Wzv5N +GkKojgbQUME6mLtNYAHbvvLJ5tU6lj5jQTkam2TxQ2MI1ZScICujcD1GEL/vr3721xD 8ZXA== 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=WiR7Hb4i3YmxO4bXLMHG8A8PTI3jQ/wOd4ke80+KSQo=; b=W6bSjcnFDstLz3JTw87DIJY3GEkUXR3sltrqWIy6iM5f3l7tSVzrQy0/PANeOktvhR Fv1dQwwMHerSyKT2byePn4+nxTuArqc6n2BsR5MJeDiOZBpMg1xfn9NEqFZpFN011x1r tq7gAoJjfjHLHHiDwRc88py90YyWICYxxF36Nv46/oRnQQUM2LdeoRLSyLpTOPKInPPL cams5UMmNATYUflEB7I+eSC8SfSB+sr5NY3aUFQHZYZrZKyXHMShA68GxsJeC0nYjQJk cE8jOBF7ujgy/ZglUwSiWMlbTdLWydu6WO3qGCQbFN/trPFmInApDQXG8GVOZn4aEfvZ bsYA== X-Gm-Message-State: AOAM53295OMPJbzDawPudXi9vCpfowWIrXcpqM9E1NK96yROUsb96tO7 SUAWawd8hi30YQ7FpCINRzSeRv4pWBE= X-Google-Smtp-Source: ABdhPJwdab0cD8ugREXGFoZfSVGZgesv2YjZu4vN8v0eowfCXck0ywvxezmYn4Vgt8Yplmlhn7BFag== X-Received: by 2002:ac2:46d1:: with SMTP id p17mr4960158lfo.216.1599764642399; Thu, 10 Sep 2020 12:04:02 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:04:01 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 2/8] wt-status: print to s->fp, not stdout Date: Thu, 10 Sep 2020 21:03:36 +0200 Message-Id: <7ada884d7c61b4ae6fd002d93a5ac982a63071b7.1599762679.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We pass around a `FILE *` in the `struct wt_status` and almost always print to it. But in a few places, we write to `stdout` instead, either explicitly through `fprintf(stdout, ...)` or implicitly with `printf(...)` (and a few `putchar(...)`). Always be explicit about writing to `s->fp`. To the best of my understanding, this never mattered in practice because these spots are involved in various forms of `git status` which always end up at standard output anyway. When we do write to another file, it's because we're creating a commit message template, and these code paths aren't involved. But let's be consistent to help future readers and avoid future bugs. Signed-off-by: Martin Ågren --- wt-status.c | 57 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/wt-status.c b/wt-status.c index 59be457015..3e0b5d8017 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1806,29 +1806,36 @@ static void wt_longstatus_print(struct wt_status *s) ; /* nothing */ else if (s->workdir_dirty) { if (s->hints) - printf(_("no changes added to commit " - "(use \"git add\" and/or \"git commit -a\")\n")); + fprintf(s->fp, _("no changes added to commit " + "(use \"git add\" and/or " + "\"git commit -a\")\n")); else - printf(_("no changes added to commit\n")); + fprintf(s->fp, _("no changes added to " + "commit\n")); } else if (s->untracked.nr) { if (s->hints) - printf(_("nothing added to commit but untracked files " - "present (use \"git add\" to track)\n")); + fprintf(s->fp, _("nothing added to commit but " + "untracked files present (use " + "\"git add\" to track)\n")); else - printf(_("nothing added to commit but untracked files present\n")); + fprintf(s->fp, _("nothing added to commit but " + "untracked files present\n")); } else if (s->is_initial) { if (s->hints) - printf(_("nothing to commit (create/copy files " - "and use \"git add\" to track)\n")); + fprintf(s->fp, _("nothing to commit (create/" + "copy files and use \"git " + "add\" to track)\n")); else - printf(_("nothing to commit\n")); + fprintf(s->fp, _("nothing to commit\n")); } else if (!s->show_untracked_files) { if (s->hints) - printf(_("nothing to commit (use -u to show untracked files)\n")); + fprintf(s->fp, _("nothing to commit (use -u to " + "show untracked files)\n")); else - printf(_("nothing to commit\n")); + fprintf(s->fp, _("nothing to commit\n")); } else - printf(_("nothing to commit, working tree clean\n")); + fprintf(s->fp, _("nothing to commit, working tree " + "clean\n")); } if(s->show_stash) wt_longstatus_print_stash_summary(s); @@ -1851,12 +1858,12 @@ static void wt_shortstatus_unmerged(struct string_list_item *it, } color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how); if (s->null_termination) { - fprintf(stdout, " %s%c", it->string, 0); + fprintf(s->fp, " %s%c", it->string, 0); } else { struct strbuf onebuf = STRBUF_INIT; const char *one; one = quote_path(it->string, s->prefix, &onebuf); - printf(" %s\n", one); + fprintf(s->fp, " %s\n", one); strbuf_release(&onebuf); } } @@ -1869,16 +1876,16 @@ static void wt_shortstatus_status(struct string_list_item *it, if (d->index_status) color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status); else - putchar(' '); + fputc(' ', s->fp); if (d->worktree_status) color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status); else - putchar(' '); - putchar(' '); + fputc(' ', s->fp); + fputc(' ', s->fp); if (s->null_termination) { - fprintf(stdout, "%s%c", it->string, 0); + fprintf(s->fp, "%s%c", it->string, 0); if (d->rename_source) - fprintf(stdout, "%s%c", d->rename_source, 0); + fprintf(s->fp, "%s%c", d->rename_source, 0); } else { struct strbuf onebuf = STRBUF_INIT; const char *one; @@ -1886,20 +1893,20 @@ static void wt_shortstatus_status(struct string_list_item *it, if (d->rename_source) { one = quote_path(d->rename_source, s->prefix, &onebuf); if (*one != '"' && strchr(one, ' ') != NULL) { - putchar('"'); + fputc('"', s->fp); strbuf_addch(&onebuf, '"'); one = onebuf.buf; } - printf("%s -> ", one); + fprintf(s->fp, "%s -> ", one); strbuf_release(&onebuf); } one = quote_path(it->string, s->prefix, &onebuf); if (*one != '"' && strchr(one, ' ') != NULL) { - putchar('"'); + fputc('"', s->fp); strbuf_addch(&onebuf, '"'); one = onebuf.buf; } - printf("%s\n", one); + fprintf(s->fp, "%s\n", one); strbuf_release(&onebuf); } } @@ -1908,13 +1915,13 @@ static void wt_shortstatus_other(struct string_list_item *it, struct wt_status *s, const char *sign) { if (s->null_termination) { - fprintf(stdout, "%s %s%c", sign, it->string, 0); + fprintf(s->fp, "%s %s%c", sign, it->string, 0); } else { struct strbuf onebuf = STRBUF_INIT; const char *one; one = quote_path(it->string, s->prefix, &onebuf); color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign); - printf(" %s\n", one); + fprintf(s->fp, " %s\n", one); strbuf_release(&onebuf); } } From patchwork Thu Sep 10 19:03:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768837 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B0D1A59D for ; Thu, 10 Sep 2020 19:07:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8CE2C21556 for ; Thu, 10 Sep 2020 19:07:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jg3EzRSz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726603AbgIJTF6 (ORCPT ); Thu, 10 Sep 2020 15:05:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726512AbgIJTEG (ORCPT ); Thu, 10 Sep 2020 15:04:06 -0400 Received: from mail-lj1-x244.google.com (mail-lj1-x244.google.com [IPv6:2a00:1450:4864:20::244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E409FC061786 for ; Thu, 10 Sep 2020 12:04:05 -0700 (PDT) Received: by mail-lj1-x244.google.com with SMTP id w3so9605727ljo.5 for ; Thu, 10 Sep 2020 12:04:05 -0700 (PDT) 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=nkUeDnvS+/tJxB2K4Ll4oLteH5VSW+eTzWNY+jwhTJ8=; b=jg3EzRSzZJV2uuAdDxLW+eOmbdVD00hT1TRNriSrEoRs2AVNsO+2f6bpzlxuXXRFfZ qu+6YOs4DtJzz8QVrZTnlnYDKvLz6wb+thrzJRqyUNYttcAWuqm1sHWYpKPcn+KU+KyM m4NJANmLd2v9AFJMM90KINiuhmCBC3EBJavhOUKVgjv8dsejeK2nSBkENRWIUSNgcJjw hbVmS0fJ9eO9bwaamvaqRaAOHJMYObC/+VuzRHgdAzNpqUVzYJOjtaw6snANobjEwjPT D1e0J4NvNSPWbdJdK46oW4eJPJMZ4PEBlxA/HVyouTuvYcfyr8Ibzf2X+mfutyyOFwGW 731Q== 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=nkUeDnvS+/tJxB2K4Ll4oLteH5VSW+eTzWNY+jwhTJ8=; b=WxKYPH5MwEP+/EjEdOHcTaR+NLPcJ/yk6ZA6ZI2YH2NiT/LL0lIvv0/Ut6n/O8LTT5 xh6ZfWN6Yctva1YTUm9aHv/kBsQf1YhCwsuTRTmUMi6KCzGmjf1FdAX6AnKMuw2BNtGP geGdI9k2MKi4pBwt3wbYhp5YHOE1qBJO4w/VwbhLF+6y3mrz0uvVvzi+GFd9P9+LK8p2 uepNZTuUiOsnubMehSAq4dKJxfRtItsazZs1BrmMIWOvnF5YV5JnIJY1VGQRsVPkyUfM S22XFUgHyABes2jOGQgBYEZjcEpUJOgnrX6ksDYYkVbtLh8FgLqdvxSf4eiDsGgI9xeS +aZw== X-Gm-Message-State: AOAM530HQSLlrxFXRFhE6F+UrOsn6uBz1Yn6CmdfRQH5Nj6+z3V5Bw/S rSmj6/fo6zcfPNYv0Sc7dz1M8XJCsH8= X-Google-Smtp-Source: ABdhPJw/9JWRSFAYXD+qUiT/U9rNAJKNVQUV8eZkhTUJEbtNz7LmGTwoCc3fbRyEjqXGgxzD2G1Y/w== X-Received: by 2002:a05:651c:554:: with SMTP id q20mr5550382ljp.348.1599764644015; Thu, 10 Sep 2020 12:04:04 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.04.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:04:03 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 3/8] wt-status: introduce wt_status_state_free_buffers() Date: Thu, 10 Sep 2020 21:03:37 +0200 Message-Id: X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When we have a `struct wt_status_state`, we manually free its `branch`, `onto` and `detached_from`, or sometimes just one or two of them. Provide a function `wt_status_state_free_buffers()` which does the freeing. The callers are still aware of these fields, e.g., they check whether `branch` was populated or not. But this way, they don't need to know about *all* of them, and if `struct wt_status_state` gets more fields, they will not need to learn to free them. Users of `struct wt_status` (which contains a `wt_status_state`) already have `wt_status_collect_free_buffers()` (corresponding to `wt_status_collect()`) which we can also teach to use this new helper. Finally, note that we're currently leaving dangling pointers behind. Some callers work on a stack-allocated struct, where this is obviously ok. But for the users of `run_status()` in builtin/commit.c, there are ample opportunities for someone to mistakenly use those dangling pointers. We seem to be ok for now, but it's a use-after-free waiting to happen. Let's leave NULL-pointers behind instead. Signed-off-by: Martin Ågren --- wt-status.h | 7 +++++++ ref-filter.c | 4 +--- worktree.c | 5 ++--- wt-status.c | 11 ++++++++--- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/wt-status.h b/wt-status.h index f1fa0ec1a7..35b44c388e 100644 --- a/wt-status.h +++ b/wt-status.h @@ -151,7 +151,14 @@ void wt_status_add_cut_line(FILE *fp); void wt_status_prepare(struct repository *r, struct wt_status *s); void wt_status_print(struct wt_status *s); void wt_status_collect(struct wt_status *s); +/* + * Frees the buffers allocated by wt_status_collect. + */ void wt_status_collect_free_buffers(struct wt_status *s); +/* + * Frees the buffers of the wt_status_state. + */ +void wt_status_state_free_buffers(struct wt_status_state *s); void wt_status_get_state(struct repository *repo, struct wt_status_state *state, int get_detached_from); diff --git a/ref-filter.c b/ref-filter.c index 8447cb09be..3f63bb01de 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1502,9 +1502,7 @@ char *get_head_description(void) strbuf_addstr(&desc, _("no branch")); strbuf_addch(&desc, ')'); - free(state.branch); - free(state.onto); - free(state.detached_from); + wt_status_state_free_buffers(&state); return strbuf_detach(&desc, NULL); } diff --git a/worktree.c b/worktree.c index cba2e54598..23dd547e44 100644 --- a/worktree.c +++ b/worktree.c @@ -369,8 +369,7 @@ int is_worktree_being_rebased(const struct worktree *wt, state.branch && starts_with(target, "refs/heads/") && !strcmp(state.branch, target + strlen("refs/heads/"))); - free(state.branch); - free(state.onto); + wt_status_state_free_buffers(&state); return found_rebase; } @@ -385,7 +384,7 @@ int is_worktree_being_bisected(const struct worktree *wt, state.branch && starts_with(target, "refs/heads/") && !strcmp(state.branch, target + strlen("refs/heads/")); - free(state.branch); + wt_status_state_free_buffers(&state); return found_rebase; } diff --git a/wt-status.c b/wt-status.c index 3e0b5d8017..5c4cc4805f 100644 --- a/wt-status.c +++ b/wt-status.c @@ -782,9 +782,14 @@ void wt_status_collect(struct wt_status *s) void wt_status_collect_free_buffers(struct wt_status *s) { - free(s->state.branch); - free(s->state.onto); - free(s->state.detached_from); + wt_status_state_free_buffers(&s->state); +} + +void wt_status_state_free_buffers(struct wt_status_state *state) +{ + FREE_AND_NULL(state->branch); + FREE_AND_NULL(state->onto); + FREE_AND_NULL(state->detached_from); } static void wt_longstatus_print_unmerged(struct wt_status *s) From patchwork Thu Sep 10 19:03:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768845 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F2E1C59D for ; Thu, 10 Sep 2020 19:08:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CAC7F207DE for ; Thu, 10 Sep 2020 19:08:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dgDZxEGq" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727119AbgIJTH6 (ORCPT ); Thu, 10 Sep 2020 15:07:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726853AbgIJTEt (ORCPT ); Thu, 10 Sep 2020 15:04:49 -0400 Received: from mail-lf1-x144.google.com (mail-lf1-x144.google.com [IPv6:2a00:1450:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C1EDC061795 for ; Thu, 10 Sep 2020 12:04:07 -0700 (PDT) Received: by mail-lf1-x144.google.com with SMTP id b12so4169556lfp.9 for ; Thu, 10 Sep 2020 12:04:07 -0700 (PDT) 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=QBUA3jj+YP61Y3WKAoytR+bVRW6HWGsJZ+tg3CvYGwg=; b=dgDZxEGqV4JXAxotNK5+6a3elE6TnahP4/ba64OReAWw9lxannAEjmyQjlyUwUpIgz UXX8fYgZ+s6OHMxHAFRvs+rbIiwt722RvQyD3UseDktR4UlW5DA5gesBMud8AgZi+xIU u3I48fFHrCtzv275iRitaGUSxbjqGUpttjylDXND+z/Ri1La9LcHb0R8RVIzetJ8wV8K aPIHTiQ95fYqabi86o1i8GfawfibsdzhyehUh/rJx+b+ryQFosyMFo8DwevM1TivbpMn oknb9s/AIyeCQtUgj/nNn1GFjE5slNBULgUcFK55CQNmUMDQM9KN6vflAqwmuewjeTb6 flkA== 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=QBUA3jj+YP61Y3WKAoytR+bVRW6HWGsJZ+tg3CvYGwg=; b=hnxvbHQJT9IuZCLuyvdERJYS6goxWHYCzHBbd7rsLpwone2w4ceGE5nXMABgu1S7G1 2pu2cynCvJ7/0MCxeyW3oC7YPDKoeecpzssil/eIqvV5lkPApfqGFOlSREUCCNQ+JFiK ivI2UygovIIyjdDThDqwhSwkLYw2fr4vtSPGrejbIt7R84JKF/hEHJy83c2gXg8A85V+ dbreJ1efzG0vlJxDN/BIn5q5JbfhcAK/yJ+Lldfk2KCuEaKwNwlzp7pMoCk0fyI7njHT UuFzqfCvNoQ+smDF9J1WZ0jdAqBuZMHhmGFLEA/yrb25LxyUGex5+2bAVgWArYLeSPCx XNKw== X-Gm-Message-State: AOAM531TAEbCilIVbFWbgU4C2MIhi9VjaGxVUA0x0lLLZ4OBqSjY5CKY Piu4yMBBQKgCG4avraTGuF3kWlzEAzA= X-Google-Smtp-Source: ABdhPJywZDoGK8VhECz3GRpeC86NKe6HHLD7CECWgP6ixz50FdVbW17xd4sjmgUlAc5X/raPGoTBzA== X-Received: by 2002:a19:3fc9:: with SMTP id m192mr4944807lfa.36.1599764645695; Thu, 10 Sep 2020 12:04:05 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.04.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:04:05 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 4/8] worktree: drop useless call to strbuf_reset Date: Thu, 10 Sep 2020 21:03:38 +0200 Message-Id: <58a2469cc18839e57b45f687b6e484d69161a34c.1599762679.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org There's no need to reset the strbuf immediately after initializing it. Signed-off-by: Martin Ågren --- worktree.c | 1 - 1 file changed, 1 deletion(-) diff --git a/worktree.c b/worktree.c index 23dd547e44..64a9e78997 100644 --- a/worktree.c +++ b/worktree.c @@ -552,7 +552,6 @@ const char *worktree_ref(const struct worktree *wt, const char *refname) { static struct strbuf sb = STRBUF_INIT; - strbuf_reset(&sb); strbuf_worktree_ref(wt, &sb, refname); return sb.buf; } From patchwork Thu Sep 10 19:03:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768847 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1889E618 for ; Thu, 10 Sep 2020 19:08:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE14D20795 for ; Thu, 10 Sep 2020 19:08:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TEE2qlh8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726769AbgIJTIR (ORCPT ); Thu, 10 Sep 2020 15:08:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726928AbgIJTEt (ORCPT ); Thu, 10 Sep 2020 15:04:49 -0400 Received: from mail-lf1-x142.google.com (mail-lf1-x142.google.com [IPv6:2a00:1450:4864:20::142]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95186C061796 for ; Thu, 10 Sep 2020 12:04:10 -0700 (PDT) Received: by mail-lf1-x142.google.com with SMTP id x69so4201136lff.3 for ; Thu, 10 Sep 2020 12:04:10 -0700 (PDT) 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=PJMiaa1kh/x8DHZTd9ip+DvzZSE8k8+nmz9DEVmqn8g=; b=TEE2qlh8XP0Jolhxfv/58gCdioMw4ic/ah1ggJpa6vHZNgR7PMAZfUELg5aISj8TRD CMlTiwd578HFO1hSsfnuOjehTjNthr+ydpIDB74SW4xxIzuyMFL7HbBnY0ZDgublKFii 2O2oQ9nJdRu7Hj8sLoA4qrFHfxqNrlB9+s2wm3ZbKwoZOXfzDPAWApzVW7mP//GJX4rh /J4Vqi/4JOC7i5F0eGNaGnasphnhQCGGlyvaipvDRLdMXkeZAapHTyJ+tFfwofX0cUMZ 81NxyyB7iDb3I/tY1nBQkobjL8cGmPEK8r6Tu5P/CktFhzT3XWYf5x1ZMpJMXtuv+BEy z7sQ== 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=PJMiaa1kh/x8DHZTd9ip+DvzZSE8k8+nmz9DEVmqn8g=; b=R2ATI5JV/tvi075PczirfE/TbjiPAnFbx9tU55ee24R2pNvPolkyPo1C0eZAZ4tnkL AcH8QY/QpD9RkB3oNfE3fbvEZem4kj2esk1VTlNdrnkPdzMgg3wu6AMMC1hBFLar46s3 +BvpQ3Rm5rQQTjEjqzuKsF45WhYhCYwScmhZqqdXdWXmSW9ag3UJIB1f4eKnBhT1VsOS DLyuBZWs+2u6j5Hr8kAuoq6sKqRwtDG21HMM6nDOPWCFQnLq1uMv5edzCiWkdiLTd2BA 8CKwhDJt1va4TqVIRTzOtsjtBRmZfgq7z7ifcphKdsUxdoFnfwVZaejQbqgAdyWQhG4V L/1w== X-Gm-Message-State: AOAM5307B//gF5ebSRago3nE1ZMOSzKrCBSRVZdZLj+cd1ZatLvwsUL5 tMZBg8NTCkjU9SkF35ylWkx9tywlAlk= X-Google-Smtp-Source: ABdhPJwr0iAcmCad8qzZFsww3/nMhz4nbwcCNV/fZeY4hSysgKsQxtUmgyJGFfxCSUi0vr6oyeiSuQ== X-Received: by 2002:a19:430f:: with SMTP id q15mr4885208lfa.191.1599764648801; Thu, 10 Sep 2020 12:04:08 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.04.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:04:06 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 5/8] worktree: update renamed variable in comment Date: Thu, 10 Sep 2020 21:03:39 +0200 Message-Id: <9c1e321fbc281414e5d4202e83790ae08dce11da.1599762679.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The comment above `add_head_info()` mentions "head_sha1", but it was renamed to "head_oid" in 0f05154c70 ("worktree: convert struct worktree to object_id", 2017-10-15). Update the comment. Signed-off-by: Martin Ågren --- worktree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worktree.c b/worktree.c index 64a9e78997..050f22dd65 100644 --- a/worktree.c +++ b/worktree.c @@ -21,7 +21,7 @@ void free_worktrees(struct worktree **worktrees) } /** - * Update head_sha1, head_ref and is_detached of the given worktree + * Update head_oid, head_ref and is_detached of the given worktree */ static void add_head_info(struct worktree *wt) { From patchwork Thu Sep 10 19:03:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768827 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D47E1159A for ; Thu, 10 Sep 2020 19:05:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A943920882 for ; Thu, 10 Sep 2020 19:05:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hXZy9Z2h" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727041AbgIJTFT (ORCPT ); Thu, 10 Sep 2020 15:05:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727001AbgIJTEt (ORCPT ); Thu, 10 Sep 2020 15:04:49 -0400 Received: from mail-lf1-x141.google.com (mail-lf1-x141.google.com [IPv6:2a00:1450:4864:20::141]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4E15C061798 for ; Thu, 10 Sep 2020 12:04:13 -0700 (PDT) Received: by mail-lf1-x141.google.com with SMTP id q8so4199142lfb.6 for ; Thu, 10 Sep 2020 12:04:13 -0700 (PDT) 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=kO9q9kW9UN3frhajJtx1JcSBjIZpukmwshqHlQdrHUo=; b=hXZy9Z2hY8DpULOhY8dlB/Dml737OXDDdgC6i4DnXZGJDOKGFZBtWn+8+LT+EruIOB hHpoOcC83sniW+NJEsN+k31gCIe73moI6KP8WEeqal5SSIuB/H6o/UCyXo+bIm1Di5uQ lK5JlKyDoWoVL4QFy5yviYLi811HcStNSUs7LMX87JUmZmurOdWPTMcVKpi74rymeXC5 MJfq9YrJ6DqgxWqpLNw6qMHnOFOHjEVS/pIldxU2T+NqpLhVi1i0vQTA38FQC29d+QJ6 xJjPeBk2XBRw9uzXhaL6ragZ9w6pO3Ewa/xXz3rQJ9jCPDqNBsEad8QwLx6KcAiOsTM+ iP5w== 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=kO9q9kW9UN3frhajJtx1JcSBjIZpukmwshqHlQdrHUo=; b=hl1sFYJ6feZCm7q7t9+kW82gmPqhPbWmoi8/ZRq7VLK8Cj/NCEOtKk94LdqMwn3Tv9 5IVgIMaHMUlRhL94MlRV7onBrVwjKrmai7L8AadW0UCT256fuMprF3NibT5jVahT5Ct+ ts0VTHImXRhj6dAHVTGZjktdJ7wysK7HNhaJBCgQ2L1CRJquyq/Droc28rn7LDQ2OxnE ihE8HM2hyoyUt65XjPG5DaaSks85MH9rzxx+kqItmJ21mujoARjfpZa+p4obJh8WBR0i zpeROU1VkKfAm15oNpp3Shti+5NnjPdj4dDFPzWyUQMEmX/iwasAmrfaaFai+aSw6WOX pYxw== X-Gm-Message-State: AOAM530m5YYpBDzfNCi6fIF69o+OCa8t4YyEwkhq2oWIyq/3Mfo6yDoi cFOCqx/j5TkIvHTbjbvJdGK3aCnQN7I= X-Google-Smtp-Source: ABdhPJwF/eZ2CnqlKf3HVk449VfneilJmYGJoDgg9wleHCTIiLsl/dEUtqr2nS5jCHnJYdEaoKg4nQ== X-Received: by 2002:a19:834a:: with SMTP id f71mr5052331lfd.59.1599764650798; Thu, 10 Sep 2020 12:04:10 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.04.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:04:10 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 6/8] worktree: rename copy-pasted variable Date: Thu, 10 Sep 2020 21:03:40 +0200 Message-Id: <6d913ea3e0c30cc1dbcff05974b5d990797e8dc2.1599762679.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org As the commit message of 04a3dfb8b5 ("worktree.c: check whether branch is bisected in another worktree", 2016-04-22) indicates, the function `is_worktree_being_bisected()` is based on the older function `is_worktree_being_rebased()`. This heritage can also be seen in the name of the variable where we store our return value: It was never adapted while copy-editing and remains as `found_rebase`. Rename the variable to make clear that we're looking for a bisect(ion), nothing else. Signed-off-by: Martin Ågren --- worktree.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/worktree.c b/worktree.c index 050f22dd65..a2d0d20564 100644 --- a/worktree.c +++ b/worktree.c @@ -377,15 +377,15 @@ int is_worktree_being_bisected(const struct worktree *wt, const char *target) { struct wt_status_state state; - int found_rebase; + int found_bisect; memset(&state, 0, sizeof(state)); - found_rebase = wt_status_check_bisect(wt, &state) && - state.branch && - starts_with(target, "refs/heads/") && - !strcmp(state.branch, target + strlen("refs/heads/")); + found_bisect = wt_status_check_bisect(wt, &state) && + state.branch && + starts_with(target, "refs/heads/") && + !strcmp(state.branch, target + strlen("refs/heads/")); wt_status_state_free_buffers(&state); - return found_rebase; + return found_bisect; } /* From patchwork Thu Sep 10 19:03:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768829 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CE537159A for ; Thu, 10 Sep 2020 19:05:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A735F2075E for ; Thu, 10 Sep 2020 19:05:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fKDtvmrR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726927AbgIJTF2 (ORCPT ); Thu, 10 Sep 2020 15:05:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727007AbgIJTEt (ORCPT ); Thu, 10 Sep 2020 15:04:49 -0400 Received: from mail-lf1-x144.google.com (mail-lf1-x144.google.com [IPv6:2a00:1450:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BDF6C061799 for ; Thu, 10 Sep 2020 12:04:14 -0700 (PDT) Received: by mail-lf1-x144.google.com with SMTP id b22so625667lfs.13 for ; Thu, 10 Sep 2020 12:04:13 -0700 (PDT) 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=xnVS8SVkF/VPsWN5tE7dSjuaShxo7UkMgytOJqiyMvM=; b=fKDtvmrRZwoWH3z6WU5IbrNSaho5EmXc8olFeUAIogA8d6qOoReL3AGOE7M6s9eIYb lD59kjezn7EQnoXz51vdwgIGOB0hB3z4w5DFUcwyZTKShIM4UNoCGhlIk9jpxt36Ot/A MlICq8v5xZOyw6laPcnbcA8E2TsM4Im2HCdxB6w8jQHpqFBFaN/sLLK0Z1q6zpPL4gHe IVL/oo/NoR5k8wTlbGICIh/wIJaaItv9yocTPNuP7w4tYiEXZ9sVgD7ed/8bhTQXMITs Hydbu+D9tnWptrYb/xZ0go5b9s749Zc1JOBNhIpvUERTeZNXKSL50wMcnJt0Uq3o9euc ZxRA== 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=xnVS8SVkF/VPsWN5tE7dSjuaShxo7UkMgytOJqiyMvM=; b=tL974X5iMIqQwIepdy9umZV/lobj4npcr1muB9nbfZTvUMCSZ0RUYuV9MjnV1N3RHy lscmZ2UQ0Gbkf7mhYxCTSFkaiNxu5cjg+jxYFP4c9CsEU57ZoICVfhhNJymJXa4T2Wdc kfYdIu4GJlePtIPIyTdKlpHn/05KJMOPw7aLvNI3PXxWVUeWRFResO87z0FsefZ623Qy XZoGsoikW8naaQHMnpiJ/5xgVCKac/58xSlVHM0SxfgM1YN/uBVETIZ+tNalj+UpTSad FHEqDhW1NUaJEfjcoBQcOwAv060T3OILjAgHWk+k6bFqqJsQ2wS9o3ANzJrBr/cv3BQY 0eNg== X-Gm-Message-State: AOAM531fg1VB7pl+OYEgxpzkMhDXHNm3Vl/NDFQI8LC3ohBH5XoFeNfK 5gOuO6GUCxZRKZhAKWA56QepigDwTGM= X-Google-Smtp-Source: ABdhPJwKqMrC22brO4mA+oosckNTVl/C6FWqMBgV6+jHjNNedkYmU5C2O4yxRzRfwKVc+2AI+VDpyQ== X-Received: by 2002:a05:6512:512:: with SMTP id o18mr4870967lfb.98.1599764652243; Thu, 10 Sep 2020 12:04:12 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:04:11 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 7/8] worktree: use skip_prefix to parse target Date: Thu, 10 Sep 2020 21:03:41 +0200 Message-Id: <872dc384c53dac61717639cdeccb44a08d5f024d.1599762679.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Instead of checking for "refs/heads/" using `starts_with()`, then skipping past "refs/heads/" using `strlen()`, just use `skip_prefix()`. In `is_worktree_being_rebased()`, we can adjust the indentation while we're here and lose a pair of parentheses which isn't needed and which might even make the reader wonder what they're missing and why that grouping is there. Signed-off-by: Martin Ågren --- worktree.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/worktree.c b/worktree.c index a2d0d20564..faac87422c 100644 --- a/worktree.c +++ b/worktree.c @@ -364,11 +364,11 @@ int is_worktree_being_rebased(const struct worktree *wt, memset(&state, 0, sizeof(state)); found_rebase = wt_status_check_rebase(wt, &state) && - ((state.rebase_in_progress || - state.rebase_interactive_in_progress) && - state.branch && - starts_with(target, "refs/heads/") && - !strcmp(state.branch, target + strlen("refs/heads/"))); + (state.rebase_in_progress || + state.rebase_interactive_in_progress) && + state.branch && + skip_prefix(target, "refs/heads/", &target) && + !strcmp(state.branch, target); wt_status_state_free_buffers(&state); return found_rebase; } @@ -382,8 +382,8 @@ int is_worktree_being_bisected(const struct worktree *wt, memset(&state, 0, sizeof(state)); found_bisect = wt_status_check_bisect(wt, &state) && state.branch && - starts_with(target, "refs/heads/") && - !strcmp(state.branch, target + strlen("refs/heads/")); + skip_prefix(target, "refs/heads/", &target) && + !strcmp(state.branch, target); wt_status_state_free_buffers(&state); return found_bisect; } From patchwork Thu Sep 10 19:03:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11768843 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 565FC59D for ; Thu, 10 Sep 2020 19:07:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2DBE620882 for ; Thu, 10 Sep 2020 19:07:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gXMyOmcN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727101AbgIJTHu (ORCPT ); Thu, 10 Sep 2020 15:07:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726812AbgIJTEx (ORCPT ); Thu, 10 Sep 2020 15:04:53 -0400 Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDC13C06179A for ; Thu, 10 Sep 2020 12:04:15 -0700 (PDT) Received: by mail-lj1-x241.google.com with SMTP id s205so9599938lja.7 for ; Thu, 10 Sep 2020 12:04:15 -0700 (PDT) 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=4a1LPn6RpS2ZYMcSASWXRo5meExpp02Y5LHw0fw9Qe4=; b=gXMyOmcNwc9lNRVAeBNh089jadeawpMEcE57tHE9ld3LUULdKi0js53R7lUh7e9nb5 xOazRBW5gGoNTvE6oWlbEQ/+M59xUCuuZqGEo+IyWvkv35NHIxZpzWMgKd+uoUtJ5H4N exQdRpK0zHcguNsEmhFpn+pV7gOJWwYgDWdVHtD+zD02qBNtDZnXh9+69rxz2Q5Meyt1 QjQgqSfz4KRr2FEW3kNJfidHdLbIWWdlW4WoeExyMeDLTwRQuC1jQn5ISuBLjTBFtPMX iXsc+l6AbzYvJij2ePx8lBYY996odJxDjBEOa9w55i1BF4fOP82b5RtaksRP/8vQe4iD 1K3g== 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=4a1LPn6RpS2ZYMcSASWXRo5meExpp02Y5LHw0fw9Qe4=; b=CYAijRoJ7Tos6Sr9STONV0y79dI+4fzJZve0c5jlZmtWSRDVqpeNP9IPrN6RR9blIT miazOS83fhK/lIsZFKMhxnkHYWHB2sCUfQd+gJ0i0yqVk1O62z8UayRDpElCCH3yC8kJ YNDn8c+3rpDlBSH9vVeqYrKOpm3m2WYMoAc6z4ufUw5p8PCyuc/5I+yLjDDsJTuO80Ff kicX56RpLP/nlQk+FhUdnOYSPVxtIO57AcdWYVGEiNSOJhhICZMpOJeR5p07M9dDNWJ2 Y+oi/JT7Y+nxnwLWTyWx0CON2UzTpCflIBYPosPBlF1KXh819nwzMosMa0pwMuGgIoKP eBJQ== X-Gm-Message-State: AOAM53340jEveKpfTA/M8W4Vpu7uXtDg2tFP4B0E6jHWWohD5pqs26h2 7JGSiMwCZqylo/YVPb35zhpHmTfNgDo= X-Google-Smtp-Source: ABdhPJxOCn1AEI+177rbfbgD1PLK+uYix4EtSFYn5dKI07jmADck+FCfZUCKs8SUZr9nTI/zp3zO0g== X-Received: by 2002:a2e:b610:: with SMTP id r16mr5518177ljn.226.1599764653867; Thu, 10 Sep 2020 12:04:13 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id v11sm1549815lfg.39.2020.09.10.12.04.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2020 12:04:13 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano Subject: [PATCH 8/8] worktree: simplify search for unique worktree Date: Thu, 10 Sep 2020 21:03:42 +0200 Message-Id: <8383c246f8c23e61dedd69d6e69c72d51fd6b469.1599762679.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We track the number of worktrees we've found and break out of the loop early once we hit 2. This is because we're not really interested in the number of matches -- we just want to make sure that we don't find more than one worktree that matches the suffix. This can be done just as well by checking the NULL-ness of the pointer where we collect our answer-to-be. Drop the redundant `nr_found` variable. Signed-off-by: Martin Ågren --- worktree.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/worktree.c b/worktree.c index faac87422c..ac754b88ff 100644 --- a/worktree.c +++ b/worktree.c @@ -172,13 +172,13 @@ static struct worktree *find_worktree_by_suffix(struct worktree **list, const char *suffix) { struct worktree *found = NULL; - int nr_found = 0, suffixlen; + int suffixlen; suffixlen = strlen(suffix); if (!suffixlen) return NULL; - for (; *list && nr_found < 2; list++) { + for (; *list; list++) { const char *path = (*list)->path; int pathlen = strlen(path); int start = pathlen - suffixlen; @@ -186,11 +186,12 @@ static struct worktree *find_worktree_by_suffix(struct worktree **list, /* suffix must start at directory boundary */ if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && !fspathcmp(suffix, path + start)) { + if (found) + return NULL; found = *list; - nr_found++; } } - return nr_found == 1 ? found : NULL; + return found; } struct worktree *find_worktree(struct worktree **list,