From patchwork Sun Sep 27 13:15: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: 11802115 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 E95FA1668 for ; Sun, 27 Sep 2020 13:16:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE7022389F for ; Sun, 27 Sep 2020 13:16:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lkl1R0Qy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726265AbgI0NQI (ORCPT ); Sun, 27 Sep 2020 09:16:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726149AbgI0NQI (ORCPT ); Sun, 27 Sep 2020 09:16:08 -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 E7078C0613CE for ; Sun, 27 Sep 2020 06:16:07 -0700 (PDT) Received: by mail-lf1-x141.google.com with SMTP id 77so8027533lfj.0 for ; Sun, 27 Sep 2020 06:16: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=Is63qXrgEioUL+Hsy1MzTxoyErJDjmHx/ZVlOmLE17w=; b=lkl1R0QyYQDQNFeeLMNzIaA8B195HxtvoFEm6fHk/GJqElRv9oe3xoHZUeqinSCr+e gPdml4XSXhLEenb1KlSK1Iij8et9bDpdfmsf1CKSm+Q8s4SkSs+9gc9GU3qg0WQNbOF8 3uWJ3LghoJZv9FxIenyqC+/bJEfokS3XPqouFge/mBIlN5zAgcMwHMZ2wcWiOo7kcXvN ZaArt2WYJYTM8sM9AkliK9s9qRbS4v+AlE4eqpV+eAIabAL7TjENH29kdzWYlzgkJB1M 8LsSLk0xQrgfPEAM4N/ElUckEBVq9eSUrE4dej+xyjGcg8FqCJPBsPz19Dp1t2nx+h/W LMYQ== 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=ai3sLp+yL5cHfwnnKn9mPZAIkhNEb2TVGd453aAUCng5CiM0RhNZh9sgRopD0QGz8D qddAjd9S7pNnhNrXsjRIrRCveyurFY+6a2IYwN790OGfXfbL8ksqsUSFITjHPF9oq9/k zWGgEbzSuazLjKdH9mLq0gfchza1iYqQJuB7Bk2nq3rV+X4i/CPMZVtRK06EY/ZjzLFn 3f14lRiDWqs9Ax4fLEj6mrAH+w505ffGT8vl8ZEXie1KfYLmtmEn+/OHdAo7UJRnJK4s VmLapwNupIUOYR03olWTLOohGLhQCOGIkbcy8JDt2QwgXeIOOTTIrFzVrOlYsyduZApA QzMQ== X-Gm-Message-State: AOAM5316hM2SbF+UhrTq3ZjY5Auy4/x7U7ZH04KCsJIZsDCdckzTlZOK RMJngWXFtSO3G72YOuOuwppXS5mfC1c= X-Google-Smtp-Source: ABdhPJxVZSU32Oy2Txv2jY0FzPy+dzfm08XCc/4B73BNSkRdjchEcBSStvq4UKJxIVYfSZVXoocAeA== X-Received: by 2002:a05:6512:110a:: with SMTP id l10mr2735605lfg.552.1601212566077; Sun, 27 Sep 2020 06:16:06 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id o8sm2487616lfa.44.2020.09.27.06.16.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 06:16:05 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Taylor Blau Subject: [PATCH v2 1/7] wt-status: replace sha1 mentions with oid Date: Sun, 27 Sep 2020 15:15:41 +0200 Message-Id: <734ea28effdcb8650ab5e6ac2aa5398400faf1c5.1600281351.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 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 Sun Sep 27 13:15: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: 11802117 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 1ECF5618 for ; Sun, 27 Sep 2020 13:16:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F3C742389F for ; Sun, 27 Sep 2020 13:16:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MA53YT8c" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726281AbgI0NQL (ORCPT ); Sun, 27 Sep 2020 09:16:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726149AbgI0NQK (ORCPT ); Sun, 27 Sep 2020 09:16:10 -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 354A5C0613CE for ; Sun, 27 Sep 2020 06:16:10 -0700 (PDT) Received: by mail-lj1-x241.google.com with SMTP id u4so6103824ljd.10 for ; Sun, 27 Sep 2020 06:16: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=WiR7Hb4i3YmxO4bXLMHG8A8PTI3jQ/wOd4ke80+KSQo=; b=MA53YT8c/9QAOKCdWKJR9icDqRbpvBj6Fsl+kpcFxWJYl9UT+7HOvOzeewm3UIc1RR Io2xm0lHundsOIZLRrqb+GlzijcTsq9Yd/5U/zV2YHY7OMhTzKA2ezbAjupv82xZH0bY /r5wgGtb8YN1igKuy45rjHQ836VyH/gGR2ECu9wpuKG989RVGgR/NLYV5bXdO9pzGmVt yvAflw9KVeieN7PZFRl4KbwssxwIFexLW1g9xC+6hpB1YYSC0dvVWWxuyxWvakwT5Irl VYZ005NSnCx8kgnVBCsNSegQgde3RT1k/9+mcUf+AYnnK76ELDvlD8jS75msqVHfbhN3 hPXA== 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=T5ZeqaGi2R5Js65j6oVSOZmgnUhQsmAUBf0StRKtrg4GhyCTX7n7TLcSNmVxygwNXH cU/cViGaq02p1LFbCpZ9gr+wBA6WQeBRjoRvZYlmohuG5j6/DFMkhEkrs+tQeQ+6J0bd tWV4Lz5QAnHGdnnIMyUaeDJ8ycIoz3Ug7v9DmwcilsNUGz1BvBTSewM2P84obx6mukDy C6gNC208N+E/q3a4CkoafS/irstBcI3KzNt1GijEI7fASmmxPCvQdHxx8DtPFdbZWJkA 4B+SrGMTM8O9YFBOIvO1tBZfB0AvKQ0whRLCMmMYlN0k69+uifWSgYgZ4q1EhADyT6Dz /rrg== X-Gm-Message-State: AOAM530l/VdZVb6D1U8r1oZcj/ZZ1I/Vo9ioaNAdey+JHJMK6wEWE+SO 43g1rJcxtx6NlIMlqMqrIn5PyXDXliM= X-Google-Smtp-Source: ABdhPJyIDOZijgdsEdBU3XbePqRlRf9voUsLDmQnACzjflQpSq4Wg/hGgb0dO4WALD8ANfCoPHoVzQ== X-Received: by 2002:a2e:575d:: with SMTP id r29mr1453483ljd.183.1601212568172; Sun, 27 Sep 2020 06:16: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 o8sm2487616lfa.44.2020.09.27.06.16.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 06:16:07 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Taylor Blau Subject: [PATCH v2 2/7] wt-status: print to s->fp, not stdout Date: Sun, 27 Sep 2020 15:15:42 +0200 Message-Id: <7ada884d7c61b4ae6fd002d93a5ac982a63071b7.1600281351.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 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 Sun Sep 27 13:15:43 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: 11802123 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 86E0F1668 for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6BC2F2389F for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KiP0EFiN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726314AbgI0NQP (ORCPT ); Sun, 27 Sep 2020 09:16:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726149AbgI0NQM (ORCPT ); Sun, 27 Sep 2020 09:16:12 -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 DA12CC0613CE for ; Sun, 27 Sep 2020 06:16:11 -0700 (PDT) Received: by mail-lf1-x144.google.com with SMTP id u8so8028224lff.1 for ; Sun, 27 Sep 2020 06:16:11 -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=KiP0EFiNT3pEK9xtteBEXIEUgjhhnh0mIbgcrH/7MbhPAhU1AVoazOQknVuME5CYXS iQiPm2+9+qjy1WlpvLdDaqKSYJ23fToKVMrS9nCQtTU7CJkQ5UCaWX2rskWWpWA7R+t5 UycDJt0sB9cEqF3hKZADL4QK+HKuMSXA6iNa1H50UV2ep753pLcIM6foNFxRejNEgzqJ BT78Tsgm12VR1NGXVxe0pF1S3siXR3p3OaMeMwUq1634hfeiXtq7ONIQKcnPRdIEW1Jg h14SWZBqKw1P56vjxfLxqRKo+I26pwLxtiNZtRdh4sqJ7ac6tRIr0J+BuEQTwjI9xqxT BpMw== 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=pG+zMFg6HpeSP0HgFxe4nC+rOcoCAU+/kFxi6KxEe1vBjbCN6M4TRf2ED60sTlj0uz f4jSKjm+85hxYTw///tMCSqLkGoT6VMZNYhBV3/d5X/jO5WjA4b+J1LL4XITnOax65pL IeqDONCFzRJBCQEqNG2bJZkFt4GWpBVZzFaSuT3NWeWofmYYsnX7NuLr7bLMLJ9oEgly GsQYUoCIr+rqXkdn1SS6cRvEo8BOCYbI7WRbmig/0NVHRsqMr2ej9/7FvQVbwijUGmDy 1e1rnKmsfSCMizZaKJZeu8KLZgCDnHLmJdHl9jsHj8UFV7iXCH870ibYWCvSUN9N1PlL cxEg== X-Gm-Message-State: AOAM530kP+wgYGfgFHGGzrnXzRDmS8ziDb1utLFXcP1q6mzPqVZzoKz6 6AHQhoxhSUWZHvV9B6ejRaNHFue1ApY= X-Google-Smtp-Source: ABdhPJyPG9KPUvgXDNTd5b6Cir48X3zQv7oNsQDw69ydeZF87+vE9LjXZXBK61a7cB/gym2uEA1bLQ== X-Received: by 2002:ac2:5541:: with SMTP id l1mr2646812lfk.89.1601212570072; Sun, 27 Sep 2020 06:16: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 o8sm2487616lfa.44.2020.09.27.06.16.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 06:16:09 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Taylor Blau Subject: [PATCH v2 3/7] wt-status: introduce wt_status_state_free_buffers() Date: Sun, 27 Sep 2020 15:15:43 +0200 Message-Id: X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 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 Sun Sep 27 13:15:44 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: 11802121 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 633BF618 for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 467432389F for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LqAcSY0W" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726309AbgI0NQO (ORCPT ); Sun, 27 Sep 2020 09:16:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726185AbgI0NQO (ORCPT ); Sun, 27 Sep 2020 09:16:14 -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 9C97AC0613D3 for ; Sun, 27 Sep 2020 06:16:13 -0700 (PDT) Received: by mail-lf1-x142.google.com with SMTP id 77so8027698lfj.0 for ; Sun, 27 Sep 2020 06:16: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=aevlg/FGJxSsLzjA9QWJE1dKGUO92VUSoULqA/qtorI=; b=LqAcSY0WaPqQctM21ChYI9C0lso9hkg1WT4CSHOgt2wKwTfq4Tx70NAcGrZI2OFoPq whFxQPYUN6dtMu6YjbaHY9o1engetCnMkJbBzd2uyxUxpwp5hv08iRan6OlsuIhD1k3f 2mWklMOX7Kav6LEGdWnmFaQwc0dn/kXJxJys5fmRGM4JaUlaxSXZbeg8h8FgMKB1Yv1t /uk5Er4rcimdmuxvWcsub21y7rM6hAytZT1yo7+60FHNAWMI4EaZDKCQ97cjphP4OfFC ZfVq7QNBv7DNaReLViPyQEpfOla2x04wvSfT8xLGHAMbOqGSmDQHum4ckowWS98ZItSg ubdA== 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=aevlg/FGJxSsLzjA9QWJE1dKGUO92VUSoULqA/qtorI=; b=QT8XSfWdT/B+iPfIvDKc48sX5E1ckLOL8roj2eXbVp9CTKrwUdO+OvS2tyc9H89skm vtcvJDF0xzRW8Oclp77riJodfm+w/XfA0gQL2j3uX/JLoLYuJBbgigC8uhYa1cjmv6K9 xOdp7GssCmilhtpr6bZnNm5cUmCZkVNORSLRez5SM00cLuGUJCEE/RTzRyp7b/HOi9Z3 jNhtbOd2kMclbrjSXq9tz4ZMHrzh8WTWXdoBiN/BEYLZoos+k8ozX08yui2xjcgW3MKZ floftJvgkX9ysmgVHxl9Ww3BsFyZ64TArdwm7cm78j6z59l9f3tjIRdc9ryDbXHNcgCg yHpw== X-Gm-Message-State: AOAM532v1D5BcEBmYJfK/1o3v0tpgmMSD/G5wTO4Uofx+qIMHQHHvWy/ JbYSlFzAqgM7L7E+mC/H9uAAOvLCyFo= X-Google-Smtp-Source: ABdhPJzhIOYbEuG2qbY/ZqTwm7WK2ApEqcAjcPOCOkuwqXitTKwsWrcpOplc+E6mW7XywoUEeOCEkQ== X-Received: by 2002:ac2:43d5:: with SMTP id u21mr2743858lfl.135.1601212571807; Sun, 27 Sep 2020 06:16:11 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id o8sm2487616lfa.44.2020.09.27.06.16.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 06:16:11 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Taylor Blau Subject: [PATCH v2 4/7] worktree: inline `worktree_ref()` into its only caller Date: Sun, 27 Sep 2020 15:15:44 +0200 Message-Id: X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We have `strbuf_worktree_ref()`, which works on a strbuf, and a wrapper for it, `worktree_ref()` which returns a string. We even make this wrapper available through worktree.h. But it only has a single caller, sitting right next to it in worktree.c. Just inline the wrapper into its only caller. This means the caller can quite naturally reuse a single strbuf. We currently achieve something similar by having a static strbuf in the wrapper. Signed-off-by: Martin Ågren --- worktree.h | 7 ------- worktree.c | 17 ++++++----------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/worktree.h b/worktree.h index 516744c433..1449b6bf5d 100644 --- a/worktree.h +++ b/worktree.h @@ -136,11 +136,4 @@ void strbuf_worktree_ref(const struct worktree *wt, struct strbuf *sb, const char *refname); -/* - * Return a refname suitable for access from the current ref - * store. The result will be destroyed at the next call. - */ -const char *worktree_ref(const struct worktree *wt, - const char *refname); - #endif diff --git a/worktree.c b/worktree.c index 23dd547e44..a37d543394 100644 --- a/worktree.c +++ b/worktree.c @@ -548,18 +548,10 @@ void strbuf_worktree_ref(const struct worktree *wt, strbuf_addstr(sb, refname); } -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; -} - int other_head_refs(each_ref_fn fn, void *cb_data) { struct worktree **worktrees, **p; + struct strbuf refname = STRBUF_INIT; int ret = 0; worktrees = get_worktrees(); @@ -571,14 +563,17 @@ int other_head_refs(each_ref_fn fn, void *cb_data) if (wt->is_current) continue; + strbuf_reset(&refname); + strbuf_worktree_ref(wt, &refname, "HEAD"); if (!refs_read_ref_full(get_main_ref_store(the_repository), - worktree_ref(wt, "HEAD"), + refname.buf, RESOLVE_REF_READING, &oid, &flag)) - ret = fn(worktree_ref(wt, "HEAD"), &oid, flag, cb_data); + ret = fn(refname.buf, &oid, flag, cb_data); if (ret) break; } free_worktrees(worktrees); + strbuf_release(&refname); return ret; } From patchwork Sun Sep 27 13:15:45 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: 11802125 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 AC6156CB for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 915D22389F for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mi6uEfrA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726325AbgI0NQS (ORCPT ); Sun, 27 Sep 2020 09:16:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726149AbgI0NQP (ORCPT ); Sun, 27 Sep 2020 09:16:15 -0400 Received: from mail-lj1-x242.google.com (mail-lj1-x242.google.com [IPv6:2a00:1450:4864:20::242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70401C0613CE for ; Sun, 27 Sep 2020 06:16:15 -0700 (PDT) Received: by mail-lj1-x242.google.com with SMTP id n25so6136656ljj.4 for ; Sun, 27 Sep 2020 06:16: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=1zWAzbbGyNF4ujqsy3ndtQc7P0WBsL0MXLnk6Y9t6lw=; b=mi6uEfrAqHudX8d+Yo5tg5KCf7T2R/xOcf5UUTZB1pDpSOK+N1IoYXEduNBYLAG46m P6tmwohxl5MOTmIqz6MtqAQRAZ3lQcxWhHIfEL0wxfQ1BrMjM4mtv6MoSnQRNgqBLh2Z ZTHki62O3BAxxdyQAv3ILobiTT77l200sAnBAh8L710LD2CF7SKo6hHgsjITrykdvqh8 PZCPEAx6O49f2rWbUs2V4h5KrGvkW04bWHc7SdPWRwd3Qgex4UJCYNSIbYStTMoVwk9U 6iyO5h6ROP9wEC3LJpdyQVZF7M0vmrspO0XrR7vq/dg3o40D+KhCfwkoLsj0jn6XicNQ k1Dg== 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=1zWAzbbGyNF4ujqsy3ndtQc7P0WBsL0MXLnk6Y9t6lw=; b=Qkf31odQH58GGC3ut8pj8iWyUBwfV3wXIiMZymq5f+NQbMeRkZkNggwjL0KnUXKq9C pjEjCKsVkh48iG0SYs1x7Nr9SEuSHCgIV6WWPCyVqROC8eKQrn1eNymfnNh6sx2T7np4 ywXXsrF3OQchULE5l1BEZgSjaLE29u7f/BZYpfB9VFwII4hJLs69/2Fcp8VSfLcc5Fmc hJ/Gq2clHOs+ohdDy2xFctbDZzJtRR0ja7WQPpxiR2q87nt+rCQ+K9HAFUfG5P3mkU/B QP10jYUtQgYd+Jmem94xDAEeqPR6wprJRyMOdMbpGbSUW3qiBr/w3E6zutnNzn6hmr21 jrWA== X-Gm-Message-State: AOAM531aovfFTMjwlx+P6lilW+g9DlzvoFW5bCl2aZst3kDfUgvTXyCm Rx29PmIYIn8jBUtPdhdES0ADu8hvOus= X-Google-Smtp-Source: ABdhPJx2A2O+iONIL/w3NWK3HZGWnndMTzMgvxcieLPuDFUzUTCga4zdhoEt0zRGRonbw6nhWUEYnA== X-Received: by 2002:a2e:9d98:: with SMTP id c24mr3546141ljj.281.1601212573626; Sun, 27 Sep 2020 06:16: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 o8sm2487616lfa.44.2020.09.27.06.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 06:16:13 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Taylor Blau Subject: [PATCH v2 5/7] worktree: update renamed variable in comment Date: Sun, 27 Sep 2020 15:15:45 +0200 Message-Id: X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 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 a37d543394..5c9ecec466 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 Sun Sep 27 13:15:46 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: 11802127 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 D233317D4 for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B6D6523976 for ; Sun, 27 Sep 2020 13:16:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="uFoeiCPb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726348AbgI0NQU (ORCPT ); Sun, 27 Sep 2020 09:16:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726315AbgI0NQS (ORCPT ); Sun, 27 Sep 2020 09:16:18 -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 CB346C0613D3 for ; Sun, 27 Sep 2020 06:16:17 -0700 (PDT) Received: by mail-lf1-x143.google.com with SMTP id b22so7971719lfs.13 for ; Sun, 27 Sep 2020 06:16:17 -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=4j7jqVrpQS6vmAshehR1KBBcVzEimHU29GnLiCNjuQE=; b=uFoeiCPbJdCfu7BGhKeY2MmltObmrVbe7YMD6f6t6v3GAE92WgRLpaBV+QHSCb5EqU Jx+gkhBuieU0DxQPPNWmOIYSNrDsC9w6nhJOFPWAUlZGW59TG0WKgbCQDt5/RnCYI4J6 rjfFTkmiafovfU3xoGTdmk66vo9TwXpW4EHlJf6nqfcB7a0PbuXMWV8qer7VKq51lBBG eOkiE8DNAKgAS/AF6bvnYsmAXEWFQLae9T9jJbf7nalinc9nZwXLE0939v/vIFYQLImg sLsxTDmWmVIiz8HViQuWTku5QTneBg0mXPoP2K6SSQVEXIC91MwoiPOkvpKVIF5EhrbK fiJw== 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=4j7jqVrpQS6vmAshehR1KBBcVzEimHU29GnLiCNjuQE=; b=O6jyjLzU7t8fZmOwQtD2jnJ4YtYbU9b1f21NFnRikKjSLPIexJDDGe/KB5OD8HoUCb kgBEE0ZHhFiPtTpL25799KjzOOk77tG0YpjnVOUfrnPv5tanTQa7dGKCL1UbECpzGW8B QGc3lxd9HWt3DA7XMLuLNm+KMI217/PmXccLRfnqE8rZYnl5EFDIwUi8fiaEQpo0L6Ca 8ia6ANfur7yweByaajkpuvktZjQdujTyRC/Dc+IckhMGlYmEu1F/amsDQtmyvIwvJeum BJtHaP7flOIiTHOm8mbWsTapXO/tN2rYMfowN1UhZnnr+1suOk0rZpjCPtWilx1jEiP8 BAkA== X-Gm-Message-State: AOAM5316zfYCdX/7BSYgwQCUic5/aG0uqL4FI7xJJE5q4wQdwCcBjCaG oqpJN3LHwec919z4ODxXN+2MqEeL4fE= X-Google-Smtp-Source: ABdhPJzsfqX26nKYyU/5QDm1XMB9bj8r/DZs1DV1jSgPRNo6MU6JDg6Of0O8n/YCMSxqOjw6iqvwrg== X-Received: by 2002:a19:ad08:: with SMTP id t8mr2285997lfc.41.1601212576062; Sun, 27 Sep 2020 06:16:16 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id o8sm2487616lfa.44.2020.09.27.06.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 06:16:15 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Taylor Blau Subject: [PATCH v2 6/7] worktree: rename copy-pasted variable Date: Sun, 27 Sep 2020 15:15:46 +0200 Message-Id: <70e6cea3312bf0a587e6614ae06fb5fab17163ba.1600281351.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 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 5c9ecec466..b5702aa4eb 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 Sun Sep 27 13:15:47 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: 11802129 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 8D7B4618 for ; Sun, 27 Sep 2020 13:16:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7228F2389F for ; Sun, 27 Sep 2020 13:16:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="d8BcAz8T" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726315AbgI0NQY (ORCPT ); Sun, 27 Sep 2020 09:16:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726335AbgI0NQU (ORCPT ); Sun, 27 Sep 2020 09:16:20 -0400 Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B9037C0613CE for ; Sun, 27 Sep 2020 06:16:19 -0700 (PDT) Received: by mail-lj1-x243.google.com with SMTP id s205so6119811lja.7 for ; Sun, 27 Sep 2020 06:16:19 -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=dXrASULAHGNYsC1y+ahucKK9zRdkJW7jlQCLWe6CIkI=; b=d8BcAz8Tv1d0TP7N3qEN8Q3ElF4vt5u8kfiaP+bB4THepYTTYmj9rWeU+15AV4E5Y4 5h4cznwjoGhH69RFWYspWCnnhq0fjYfPlfRuzimZlMHma96LX2CTcNQViLabN1td1xKf 9MqGl6plZhokNcLZ+toAWv2SPnNcgnOWIt1v5emg3Oi0CUShd5w1cwlpom2Cey8RIgpt gafYdqEB5+JEdTL/JDbWXO5dugrMLyto1hFMKrNDqGNq3FcwTTqsHmBwa4wRNpbya5LF //xp5pAB8EyisOc9BUO9Bb0IbNQeETBc26oSAqOttWiYfrUVuIjjgpjsXODCMGim2xfw pCMA== 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=dXrASULAHGNYsC1y+ahucKK9zRdkJW7jlQCLWe6CIkI=; b=tGzn2sQ/5QTFA3ir3LpLWE7/IaqY/P0BeqXXzj2O90yFWG/YycGcv1I027Z9hRDFoh c3ThWmbYwFuYeruDfU2lo/DTKi7bbIwz6Mne+b9p94Q8vTr3x/mgYjOOIHb0vbffDz9H F1Bsmb9SNoQfvuf9wPadQ7smg3qfQUnBDlktSZ0zuA/kHd6t2vGWMYcxXJbf4P4X2pSR rA7Tz5AURQBff6MeMKfnp7/21+HWA0s220pEMPjPOeu5a6R0ZtE1pZzMAxLTPQA6IdJw 6qivCleQBfS/f+Hc7lUNx0wx+DnJrtJ78qedu14xcp3wud0esxqUY3dct3ee2S4d7xbM gsRw== X-Gm-Message-State: AOAM533bJFFupjpNQ4JE3YOOBVQp7JqiF119AMjiK79n7e98/UX116Pq Eou7ggmDV1/oSPdQANzD3GCSKAb5Rm4= X-Google-Smtp-Source: ABdhPJxxEbnMmvk0MatZC4/7y13s1uvd2goQDGnUXSntOCj+c78niFiDayMVU+OOYZbs39+D/TyzQg== X-Received: by 2002:a2e:7301:: with SMTP id o1mr3608180ljc.257.1601212578004; Sun, 27 Sep 2020 06:16:18 -0700 (PDT) Received: from localhost.localdomain (92-33-153-30.customers.ownit.se. [92.33.153.30]) by smtp.gmail.com with ESMTPSA id o8sm2487616lfa.44.2020.09.27.06.16.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Sep 2020 06:16:17 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Taylor Blau Subject: [PATCH v2 7/7] worktree: use skip_prefix to parse target Date: Sun, 27 Sep 2020 15:15:47 +0200 Message-Id: X-Mailer: git-send-email 2.28.0.277.g9b3c35fffd In-Reply-To: References: MIME-Version: 1.0 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 b5702aa4eb..a75230a950 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; }