From patchwork Mon Mar 11 03:42:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 10846655 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 407D76C2 for ; Mon, 11 Mar 2019 03:42:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A3A728EF8 for ; Mon, 11 Mar 2019 03:42:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1B2D628F21; Mon, 11 Mar 2019 03:42:44 +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 A2B7828EF8 for ; Mon, 11 Mar 2019 03:42:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727339AbfCKDmm (ORCPT ); Sun, 10 Mar 2019 23:42:42 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:38772 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726933AbfCKDmm (ORCPT ); Sun, 10 Mar 2019 23:42:42 -0400 Received: by mail-pg1-f193.google.com with SMTP id m2so2960567pgl.5 for ; Sun, 10 Mar 2019 20:42:41 -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=bsd02n8xPECKi1/scXhJYBTSAoZKofufeyf8DYPbziA=; b=vKfPlc/43X1wpqa+seVvTAY6EwQMGsoUCiWXO9F03+Xx3F4nn53pOF4enE7fKGbD7m JTfzg1k64DdTQyXGh94rAuwy/gtIjv+QY4+p3L+wzQpuSn4gKdT/ZPaZMvb01lEjh5rE Dh6/QZEd4YiQztnzlXi4KBt4+HPjPA2p9gJq/UNwj7aDv2XzHMJRVtd0bCpfLcSNldkp TEUcm9voPd+iWFSH4rH6gLOwpaB3O+24s46/IKVJJbKSpsoieh56wjoPoGo3PYI0q4qq +Qwa0DOmy+cHiMns70wM/gcXbeMCJb1ywEE7GLqoNe3D7dl18tgIZBVn2hlFmm3CVV4J MmWw== 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=bsd02n8xPECKi1/scXhJYBTSAoZKofufeyf8DYPbziA=; b=gHWLl0dcM47TWMPrJg1zSVuE8n3R3TzfyeCrbR0cgfUWz+P2QVIEWgEL8RCbUGiXW3 0BgKTpD2e39NYyrBl/c+WE8dBRRqlQrJBY0zkNkRyDoUEwai7iAzsKlhaUdBu6j1WjmT 9yXwpeLda3Hc3PzXiNAhER058wLrBsA0qjON+qqKRwOyvXKf+JPOTfPGzWPyZtuRFOKe y5ZuHn+HgkPHleoIJB9ZMOOLJ06LfZ/KU92UjnNMjpHvhwn7b63GHG1Cq24tj8eemDsH lcGX3akQyY+RSOThSQKo2uTQCBH8KIs0y9XRXpZW8FiYzovsE5xl6IFU3xpj8s4zkzvp bA9Q== X-Gm-Message-State: APjAAAWOgo77dF7ScnXfm6aPnwXCGeZ+OJvv4q/qHuw8GQ5/MzMVDDet qBidRnMR94aZQYDOKl0gB6RoszDd X-Google-Smtp-Source: APXvYqzWtL/+4jaoNII10v8UBBSwIytkqYYqJDjjQzn+eunp2L4Chg41gikei4QEM2ExpQKJf8O+mw== X-Received: by 2002:a63:5b43:: with SMTP id l3mr27980656pgm.298.1552275760997; Sun, 10 Mar 2019 20:42:40 -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 g80sm6861367pfd.72.2019.03.10.20.42.40 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 10 Mar 2019 20:42:40 -0700 (PDT) Date: Sun, 10 Mar 2019 20:42:38 -0700 From: Denton Liu To: Git Mailing List Cc: Phillip Wood , Junio C Hamano Subject: [PATCH v7 4/8] sequencer.c: remove duplicate code Message-ID: <6d5ce3ffffe5ef6c23b12538650b2cdf86e4cac9.1552275703.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. Signed-off-by: Denton Liu --- builtin/commit.c | 2 +- sequencer.c | 20 +++++++------------- sequencer.h | 2 +- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 0df15e4851..81e3bd21ca 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..612621f221 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,7 +516,11 @@ 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 + 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); } 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);