From patchwork Sun Mar 17 10:16:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 10856273 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ABD3115AC for ; Sun, 17 Mar 2019 10:16:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 94AE229736 for ; Sun, 17 Mar 2019 10:16:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 88BCE29741; Sun, 17 Mar 2019 10:16:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 194C129736 for ; Sun, 17 Mar 2019 10:16:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727148AbfCQKQP (ORCPT ); Sun, 17 Mar 2019 06:16:15 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:43635 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725903AbfCQKQO (ORCPT ); Sun, 17 Mar 2019 06:16:14 -0400 Received: by mail-pf1-f194.google.com with SMTP id c8so2628044pfd.10 for ; Sun, 17 Mar 2019 03:16:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=/hOMBbmx3PDgGx7271Y1+Yx9WvP5sR1bSossW4Q0tac=; b=dfZ2wamlbeEbH+sCLs6OGQHeQWHdiizq+QF454XWR2OB2+LhLiHqPTQb+gp7Ayy1Wz CClrv0m8bWpAbEMAgpufJB3XVYuYOQIbXZp/hq0TAw547//L1hmX3AYRgvXVeFNeFioO RqfBHy/JodwcwIClqmSZT8gtUW73pYj4Sj9cBuxqc58cqpzOIXvMEryjOWlKq2yHdsN/ JGYX5OQG/6GPzaKXk7RghgsA1x50bMEAATLsQVeMlIYR9LFaYqHs1KUsXTHjEZHqZzHf RR1Cu1cRtC1PhpBXf1oz6Igo6LGyX9wDqbe9Z0cn/6n7y5PDCyUtfz0sB/KdyiH0EyXS n2Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=/hOMBbmx3PDgGx7271Y1+Yx9WvP5sR1bSossW4Q0tac=; b=KVpq19q33x8qYrXatmFmT4VyGxcQYulzJaKfJrG3SZRfYXacxDV/31Gsv3WADbTgjC kK/cnWdfnjPMBUFpBYEyh7v3JRIyCgXigzUSq9esiMjJAcvH72L/7IolAqOO6B1vx+wm bCDU30pJAYlgAblGUbG08+3fEw2SY1mJ0vzMaIO4auC/BoE/dfXrFrjz6r/MAlfDSUm1 BZsQxvLO4KZgHUNJugt97wAWFuqhhDZZLT/KWFjiaWkwNlJgUW9b1p8u6CXMiPZTuvlZ pYSCZhX0b8RvtcGjl9akiCuNjO8jtoSGXO9ETjq8MxeDyLlRxiUMBK/v344+CbZ3ChYN XdXg== X-Gm-Message-State: APjAAAX+Dm8J+uHJKjtPYzO94oePiUqtajtGtWNfJbRAqUC9gfLzXVtF MIEF6ZMF1VencW1rBYw0ORYabdBp X-Google-Smtp-Source: APXvYqyv/BfMI1PJ0IOBvEHNzC08slaNya0+mGct1zlOg94THd8HcmKMCXRSpmd7+mrAUpu8gtJGHA== X-Received: by 2002:a17:902:e50b:: with SMTP id ck11mr14224803plb.25.1552817773244; Sun, 17 Mar 2019 03:16:13 -0700 (PDT) Received: from archbookpro.localdomain (c-73-222-73-77.hsd1.ca.comcast.net. [73.222.73.77]) by smtp.gmail.com with ESMTPSA id p7sm5131375pgs.42.2019.03.17.03.16.12 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 17 Mar 2019 03:16:12 -0700 (PDT) Date: Sun, 17 Mar 2019 03:16:10 -0700 From: Denton Liu To: Git Mailing List Cc: Denton Liu , Eric Sunshine , Junio C Hamano , Phillip Wood , Ramsay Jones , SZEDER =?iso-8859-1?q?G=E1bor?= Subject: [PATCH v8 07/11] sequencer.c: remove duplicate code Message-ID: <5cab2c3ed2085b7c706682d3d9878b6bf807d2f7.1552817044.git.liu.denton@gmail.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.11.3 (2019-02-01) Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since we implemented get_cleanup_mode, we had some duplicate code in git_sequencer_config which essentially performed the same operations. Refactor git_sequencer_config to take advantage of the logic already in get_cleanup_mode. Note that we had to introduce a separate argument to get_cleanup_mode indicating whether to die or not on an invalid value. This is because if we are parsing a config, we do not want to die but instead, we only want to warn the user, whereas if we are parsing a command-line option, we would like to actually die. Finally, this is almost a no-op refactor but not quite. Previously, in the case that an invalid value is presented, default_msg_cleanup would not be set. We change the behaviour so that default_msg_cleanup will now take on the value as if "default" were provided as the cleanup_arg. Also, we lowercase some user-facing strings. Reviewed-by: Phillip Wood Signed-off-by: Denton Liu --- builtin/commit.c | 2 +- sequencer.c | 22 ++++++++-------------- sequencer.h | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 8277da8474..ba1e6027ba 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1172,7 +1172,7 @@ static int parse_and_validate_options(int argc, const char *argv[], die(_("Only one of --include/--only/--all/--interactive/--patch can be used.")); if (argc == 0 && (also || (only && !amend && !allow_empty))) die(_("No paths with --include/--only does not make sense.")); - cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor); + cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor, 1); handle_untracked_files_arg(s); diff --git a/sequencer.c b/sequencer.c index 224c823b43..2cbfb848dd 100644 --- a/sequencer.c +++ b/sequencer.c @@ -172,17 +172,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb) if (status) return status; - if (!strcmp(s, "verbatim")) - opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; - else if (!strcmp(s, "whitespace")) - opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; - else if (!strcmp(s, "strip")) - opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL; - else if (!strcmp(s, "scissors")) - opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; - else - warning(_("invalid commit message cleanup mode '%s'"), - s); + opts->default_msg_cleanup = get_cleanup_mode(s, 0, 0); free((char *)s); return status; @@ -512,7 +502,7 @@ static int fast_forward_to(struct repository *r, } enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, - int use_editor) + int use_editor, int die_on_error) { if (!cleanup_arg || !strcmp(cleanup_arg, "default")) return use_editor ? COMMIT_MSG_CLEANUP_ALL : @@ -526,8 +516,12 @@ enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, else if (!strcmp(cleanup_arg, "scissors")) return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS : COMMIT_MSG_CLEANUP_SPACE; - else - die(_("Invalid cleanup mode %s"), cleanup_arg); + else if (!die_on_error) { + warning(_("invalid cleanup mode %s, falling back to default"), cleanup_arg); + return use_editor ? COMMIT_MSG_CLEANUP_ALL : + COMMIT_MSG_CLEANUP_SPACE; + } else + die(_("invalid cleanup mode %s"), cleanup_arg); } void append_conflicts_hint(struct index_state *istate, diff --git a/sequencer.h b/sequencer.h index eb9bd97ef3..e7908f558e 100644 --- a/sequencer.h +++ b/sequencer.h @@ -117,7 +117,7 @@ void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag); void append_conflicts_hint(struct index_state *istate, struct strbuf *msgbuf); enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, - int use_editor); + int use_editor, int die_on_error); void cleanup_message(struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode, int verbose);