From patchwork Mon Aug 17 13:23:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Jean-No=C3=ABl_Avila_via_GitGitGadget?= X-Patchwork-Id: 11718247 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 CEEE4739 for ; Mon, 17 Aug 2020 13:23:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B3D612076E for ; Mon, 17 Aug 2020 13:23:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mFjmLXDf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728574AbgHQNXQ (ORCPT ); Mon, 17 Aug 2020 09:23:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728521AbgHQNXO (ORCPT ); Mon, 17 Aug 2020 09:23:14 -0400 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 737CCC061342 for ; Mon, 17 Aug 2020 06:23:12 -0700 (PDT) Received: by mail-wm1-x341.google.com with SMTP id g75so13867655wme.4 for ; Mon, 17 Aug 2020 06:23:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=RgfrtBj6saJEPQY1qJEclkcFsm/IEepd/IbEMlZzF8w=; b=mFjmLXDfP/c/52PzJvtWkWsPFEZh8kSTEtohm+/HIc5/AdNvpov8j4mGPjyIOzu1sA I4DEWO//6G06ip2XVTPuzDi7Xc8SV90XEbl6naEewLTX/bjArCkpFRgbYy+CzFX4R1lv 6ajUQtkywDlbzeQ3bOzT3rzdpl3isE8QbCyDMk+6xgd+ED2EY9Y8Pk0E9YNaLHm7KKKI gtB45qhawUa3wCQWq9M7WvlLzllEw9iJJewY3nde3UvkhW032pjU8NzJeSwaTA+Y9r8o AeDX0L4XVdEjwHhZRHT5Lbzyy6d8FAcrhGY5eWz1lwvtuDLkBGPXAcsqBcasvIf2Hdhj aIdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=RgfrtBj6saJEPQY1qJEclkcFsm/IEepd/IbEMlZzF8w=; b=UXKN8RodoT1ZzoA0j9Ipcofh9zLVfRs/Nn+HoPUeTjKw4PItVAulisFl/uQasshrNk jTZgAzSnSQ2PKRFepAn1vaXhnPvkdq5t8cgwJ42HR4cFMWVGniU7B5kutY5AQJwGC5SY +bK4txy7bccCa0fa39kTz0BiUGoURvA7A9hN12hMFt0K58/YqHRQ+s0j5OVLZUXNpTvq q6jHGE0Mfedk2q/GC7qbvLG6Nc8JlOGJT4Fj9SJFCdMNCd2tEYRs2HAO8lrnSLDCvzqm 8Nca/4cBZT300vRPfhu57jVw3Y/oOg9V3ApW7E29kRruOQVxU5p1q7kOwa9NaKs78xPS mWPQ== X-Gm-Message-State: AOAM531FP8PQx2VnyDYCyx2rhLG9DS+yTSdE5ZCEg7sge5tZGzOtjv2j Md2nyQuBuVfpzG1uVZ5TPRpwifDvdU0= X-Google-Smtp-Source: ABdhPJypDdEapafa8oQ/3he24F2YZq04UXay/2nXiWz+VvlGtOAanWYAJMa4sQ3CXEn+Ek6vILpGGQ== X-Received: by 2002:a1c:4944:: with SMTP id w65mr14126741wma.169.1597670590838; Mon, 17 Aug 2020 06:23:10 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id q5sm30200405wrp.60.2020.08.17.06.23.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Aug 2020 06:23:10 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Phillip Wood via GitGitGadget" Date: Mon, 17 Aug 2020 13:23:07 +0000 Subject: [PATCH 1/2] add -p: use ALLOC_GROW_BY instead of ALLOW_GROW Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Johannes Schindelin , Phillip Wood , Phillip Wood Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood This simplifies the code slightly, especially the third case where hunk_nr was incremented a few lines before ALLOC_GROW(). Signed-off-by: Phillip Wood --- add-patch.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/add-patch.c b/add-patch.c index f899389e2c..a15fa407be 100644 --- a/add-patch.c +++ b/add-patch.c @@ -457,11 +457,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps) eol = pend; if (starts_with(p, "diff ")) { - s->file_diff_nr++; - ALLOC_GROW(s->file_diff, s->file_diff_nr, + ALLOC_GROW_BY(s->file_diff, s->file_diff_nr, 1, file_diff_alloc); file_diff = s->file_diff + s->file_diff_nr - 1; - memset(file_diff, 0, sizeof(*file_diff)); hunk = &file_diff->head; hunk->start = p - plain->buf; if (colored_p) @@ -483,11 +481,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps) */ hunk->splittable_into++; - file_diff->hunk_nr++; - ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr, + ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1, file_diff->hunk_alloc); hunk = file_diff->hunk + file_diff->hunk_nr - 1; - memset(hunk, 0, sizeof(*hunk)); hunk->start = p - plain->buf; if (colored) @@ -511,7 +507,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps) if (file_diff->mode_change) BUG("double mode change?\n\n%.*s", (int)(eol - plain->buf), plain->buf); - if (file_diff->hunk_nr++) + if (file_diff->hunk_nr) BUG("mode change in the middle?\n\n%.*s", (int)(eol - plain->buf), plain->buf); @@ -520,9 +516,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps) * is _part of_ the header "hunk". */ file_diff->mode_change = 1; - ALLOC_GROW(file_diff->hunk, file_diff->hunk_nr, + ALLOC_GROW_BY(file_diff->hunk, file_diff->hunk_nr, 1, file_diff->hunk_alloc); - memset(file_diff->hunk, 0, sizeof(struct hunk)); file_diff->hunk->start = p - plain->buf; if (colored_p) file_diff->hunk->colored_start = From patchwork Mon Aug 17 13:23:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Jean-No=C3=ABl_Avila_via_GitGitGadget?= X-Patchwork-Id: 11718249 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 D6D14739 for ; Mon, 17 Aug 2020 13:23:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B61182076E for ; Mon, 17 Aug 2020 13:23:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SSDbPxza" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728582AbgHQNXR (ORCPT ); Mon, 17 Aug 2020 09:23:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728533AbgHQNXO (ORCPT ); Mon, 17 Aug 2020 09:23:14 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D0D2C061389 for ; Mon, 17 Aug 2020 06:23:13 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id y3so15000374wrl.4 for ; Mon, 17 Aug 2020 06:23:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=bJ7GEKpVNH4KMjkH/NQAU3zjbetFVi2nhDRYtm2Nwto=; b=SSDbPxzaTOO+50/V6dr0rquUEDZLLkvAKXDd4u7i7eI73CvGxBW1qc5iiSsS1lqiYV qoVw71vV6LR6KCH8ip6tydMxaFYw0B3VaUSfRyO5XVex59I10BnqE5ZfH/fchffnSW0o zbHta/6S5+2nCqwKnX51hqrWJetotPZoXaOD4xHUfZSMaTtk1Rf2mxoqHL5SbcUpueBL LjcgZS7YBgkpd885pn7lST15Cmg7JpCDEEfmVAHgmXDbfebSgC7INOERY3ZwBs0jEFMZ t7Ib58obCL5KU47kdgJG+EJiA71xozBIWYBy/SJrKZOqU58dRBpDudHxRaE0tYnM4xPW 4pFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=bJ7GEKpVNH4KMjkH/NQAU3zjbetFVi2nhDRYtm2Nwto=; b=V3GiqMhHw9+qQAlwi8sytKziDHqQkfLybLqqWbdNYNN6pavqUmpM7Kb2Q+U6Ce3xSn sz2K8UPAPMCn8wJJezN0JKxoh3IoPHx3ddp6zVd8gs+UuUvyyIcdxx6iiBY38sqPIhid fgD9xV9z5/Twu6CiGAQIe7zVJNALhXqci3Te2YI613mjyGydJX/xNa7fAV1OpkOp0zt6 gqkGevL1T7GMS37rXTnfxD+IipWWlapjCjQ7Dd9oH020GyD3mq1QdrOJqSUyeSueHL/y K6D5AiAAw/0/p/67nTaEVa7NcJZZFv61ebAdQgzmqLNwfaLxpEgibAo6rfLz6iPF8Crs /9rA== X-Gm-Message-State: AOAM533cHEgwuK09TMijRaSpz92a9/61/WnJhDAvIWNOydkoBwYtrpAo 2f7ixtC6GEs+Ju0YSpnfT8c2hS4Aip8= X-Google-Smtp-Source: ABdhPJzYXyny7hQl27lDJwg/M9fT0/RE+WuMVu3X1tPeskLTFrPYP44e8A1tuka1ojwmsBHe2GSWCA== X-Received: by 2002:adf:fbc6:: with SMTP id d6mr15854950wrs.189.1597670591704; Mon, 17 Aug 2020 06:23:11 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b77sm56807753wmb.3.2020.08.17.06.23.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Aug 2020 06:23:11 -0700 (PDT) Message-Id: <752e13fb9fd9fd7930d83a0915dbbc0274a99908.1597670589.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Phillip Wood via GitGitGadget" Date: Mon, 17 Aug 2020 13:23:08 +0000 Subject: [PATCH 2/2] add -p: fix checking of user input Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Johannes Schindelin , Phillip Wood , Phillip Wood Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood When a file has been deleted the C version of add -p allows the user to edit a hunk even though 'e' is not in the list of allowed responses. (I think 'e' is disallowed because if the file is edited it is no longer a deletion and we're not set up to rewrite the diff header). The invalid response was allowed because the test that determines whether to display 'e' was not duplicated correctly in the code that processes the user's choice. Fix this by using flags that are set when constructing the prompt and checked when processing the user's choice rather than repeating the check itself. Signed-off-by: Phillip Wood --- add-patch.c | 54 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/add-patch.c b/add-patch.c index a15fa407be..907c05b3c1 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1352,6 +1352,15 @@ static int patch_update_file(struct add_p_state *s, struct child_process cp = CHILD_PROCESS_INIT; int colored = !!s->colored.len, quit = 0; enum prompt_mode_type prompt_mode_type; + enum { + ALLOW_GOTO_PREVIOUS_HUNK = 1 << 0, + ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK = 1 << 1, + ALLOW_GOTO_NEXT_HUNK = 1 << 2, + ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3, + ALLOW_SEARCH_AND_GOTO = 1 << 4, + ALLOW_SPLIT = 1 << 5, + ALLOW_EDIT = 1 << 6 + } permitted = 0; if (!file_diff->hunk_nr) return 0; @@ -1388,22 +1397,35 @@ static int patch_update_file(struct add_p_state *s, fputs(s->buf.buf, stdout); strbuf_reset(&s->buf); - if (undecided_previous >= 0) + if (undecided_previous >= 0) { + permitted |= ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK; strbuf_addstr(&s->buf, ",k"); - if (hunk_index) + } + if (hunk_index) { + permitted |= ALLOW_GOTO_PREVIOUS_HUNK; strbuf_addstr(&s->buf, ",K"); - if (undecided_next >= 0) + } + if (undecided_next >= 0) { + permitted |= ALLOW_GOTO_NEXT_UNDECIDED_HUNK; strbuf_addstr(&s->buf, ",j"); - if (hunk_index + 1 < file_diff->hunk_nr) + } + if (hunk_index + 1 < file_diff->hunk_nr) { + permitted |= ALLOW_GOTO_NEXT_HUNK; strbuf_addstr(&s->buf, ",J"); - if (file_diff->hunk_nr > 1) + } + if (file_diff->hunk_nr > 1) { + permitted |= ALLOW_SEARCH_AND_GOTO; strbuf_addstr(&s->buf, ",g,/"); - if (hunk->splittable_into > 1) + } + if (hunk->splittable_into > 1) { + permitted |= ALLOW_SPLIT; strbuf_addstr(&s->buf, ",s"); + } if (hunk_index + 1 > file_diff->mode_change && - !file_diff->deleted) + !file_diff->deleted) { + permitted |= ALLOW_EDIT; strbuf_addstr(&s->buf, ",e"); - + } if (file_diff->deleted) prompt_mode_type = PROMPT_DELETION; else if (file_diff->added) @@ -1452,22 +1474,22 @@ static int patch_update_file(struct add_p_state *s, break; } } else if (s->answer.buf[0] == 'K') { - if (hunk_index) + if (permitted & ALLOW_GOTO_PREVIOUS_HUNK) hunk_index--; else err(s, _("No previous hunk")); } else if (s->answer.buf[0] == 'J') { - if (hunk_index + 1 < file_diff->hunk_nr) + if (permitted & ALLOW_GOTO_NEXT_HUNK) hunk_index++; else err(s, _("No next hunk")); } else if (s->answer.buf[0] == 'k') { - if (undecided_previous >= 0) + if (permitted & ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK) hunk_index = undecided_previous; else err(s, _("No previous hunk")); } else if (s->answer.buf[0] == 'j') { - if (undecided_next >= 0) + if (permitted & ALLOW_GOTO_NEXT_UNDECIDED_HUNK) hunk_index = undecided_next; else err(s, _("No next hunk")); @@ -1475,7 +1497,7 @@ static int patch_update_file(struct add_p_state *s, char *pend; unsigned long response; - if (file_diff->hunk_nr < 2) { + if (!(permitted & ALLOW_SEARCH_AND_GOTO)) { err(s, _("No other hunks to goto")); continue; } @@ -1512,7 +1534,7 @@ static int patch_update_file(struct add_p_state *s, regex_t regex; int ret; - if (file_diff->hunk_nr < 2) { + if (!(permitted & ALLOW_SEARCH_AND_GOTO)) { err(s, _("No other hunks to search")); continue; } @@ -1557,7 +1579,7 @@ static int patch_update_file(struct add_p_state *s, hunk_index = i; } else if (s->answer.buf[0] == 's') { size_t splittable_into = hunk->splittable_into; - if (splittable_into < 2) + if (!(permitted & ALLOW_SPLIT)) err(s, _("Sorry, cannot split this hunk")); else if (!split_hunk(s, file_diff, hunk - file_diff->hunk)) @@ -1565,7 +1587,7 @@ static int patch_update_file(struct add_p_state *s, _("Split into %d hunks."), (int)splittable_into); } else if (s->answer.buf[0] == 'e') { - if (hunk_index + 1 == file_diff->mode_change) + if (!(permitted & ALLOW_EDIT)) err(s, _("Sorry, cannot edit this hunk")); else if (edit_hunk_loop(s, file_diff, hunk) >= 0) { hunk->use = USE_HUNK;