From patchwork Sun Mar 17 10:16:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denton Liu X-Patchwork-Id: 10856279 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 3B24D13B5 for ; Sun, 17 Mar 2019 10:16:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2327D29736 for ; Sun, 17 Mar 2019 10:16:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 174D429741; Sun, 17 Mar 2019 10:16:25 +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 64F5B29736 for ; Sun, 17 Mar 2019 10:16:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727206AbfCQKQX (ORCPT ); Sun, 17 Mar 2019 06:16:23 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:45533 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727194AbfCQKQX (ORCPT ); Sun, 17 Mar 2019 06:16:23 -0400 Received: by mail-pg1-f195.google.com with SMTP id y3so151210pgk.12 for ; Sun, 17 Mar 2019 03:16:22 -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=epOOx5qmhX9XAzSIHFLPF2H7cgbOFAXzQxVq89PKzho=; b=nBpIT/AtfmwMfiidYThurTnsm1jIpVK0mpGPs7pdCPY9ptzuw0QALppZE1E2Cu90bd uIPWMmYKHfP/lSlOLHfj+srUup/YCGBa5jOji06qD8wacLNa4Yn1BcxkatOG7sDsrPQL 66P1s5K7VIL6LVfFlaFR09FE+fOndy/6ILp5cnk1SW8lZdu4KeKQrGnqMCknm8ulgGUh wUz4grx3KYgDw8EIF/XiU9ZNeMuFsY8QFP301KlBaSdz+8gNCHq5OoU/Lg018PERa99N KdZsNDRnJikQMUcPVLAaGN82Bx57XX7/IteaASwI4bvYkvLv1VvkeQgYBUmjmHwZt/zK pOqg== 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=epOOx5qmhX9XAzSIHFLPF2H7cgbOFAXzQxVq89PKzho=; b=bu/QgLbCQMeDpfY0fnkv4gWo/B/kOzr3Iw2nPlj32Nt4Q57iqYgWYBQCjS4FR3VJMN SRFcBtDdwwH6aripUEfAv8n81iVvd0/g9MYQZaZdgGlRggH/uEQdWehkDyggWhaHORIy uLFnNAsOvdG1/n8DkUowZvAHN63rxEYWRduObrC30bIN0YWcc6FK4g2r/306cIbDntwS JMlflJW6pptRsMHDWIIIKiLlKEHcXzZordSqeHdMeVR6NgbETL44IBwAtouWAwPPQ7XV k+9+5Mr4NDXiVFo7DH3mmPB1+0/9/a6E7L5xdibad6xCyldQmE2wiSdkrl2pytpjNU29 hQCA== X-Gm-Message-State: APjAAAUlDQwpYtLUhRovpKTnoFrYgfmSo0YDCRwq6vDK+0kkJWCWFn2A myhuUWKt4kBb9XN5ADHbBvy1Dbz4 X-Google-Smtp-Source: APXvYqxDoQt4rCKuAi1rbEX8eNpV/RKOnhzsVYalHol6VCwxbjNzgfwy5W/8UXReeCal16v8sqzS9w== X-Received: by 2002:a17:902:834b:: with SMTP id z11mr14009128pln.257.1552817781784; Sun, 17 Mar 2019 03:16:21 -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 r10sm1964302pfn.188.2019.03.17.03.16.20 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 17 Mar 2019 03:16:21 -0700 (PDT) Date: Sun, 17 Mar 2019 03:16:19 -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 10/11] sequencer.c: define describe_cleanup_mode Message-ID: <4edca9db0b2a32621a7622b5b709db23b2512d1e.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 Define a function which allows us to get the string configuration value of a enum commit_msg_cleanup_mode. This is done by refactoring get_cleanup_mode such that it uses a lookup table to find the mappings between string and enum and then using the same LUT in reverse to define describe_cleanup_mode. Reviewed-by: Eric Sunshine Reviewed-by: Junio C Hamano Signed-off-by: Ramsay Jones Signed-off-by: Denton Liu --- sequencer.c | 62 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/sequencer.c b/sequencer.c index 2cbfb848dd..f782e8bab2 100644 --- a/sequencer.c +++ b/sequencer.c @@ -160,6 +160,22 @@ static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") static GIT_PATH_FUNC(rebase_path_reschedule_failed_exec, "rebase-merge/reschedule-failed-exec") +struct cleanup_config_mapping { + const char *value; + enum commit_msg_cleanup_mode editor; + enum commit_msg_cleanup_mode no_editor; +}; + +/* the 0th element of this array must be the "default" */ +static struct cleanup_config_mapping cleanup_config_mapping[] = { + { "default", COMMIT_MSG_CLEANUP_ALL, COMMIT_MSG_CLEANUP_SPACE }, + { "verbatim", COMMIT_MSG_CLEANUP_NONE, COMMIT_MSG_CLEANUP_NONE }, + { "whitespace", COMMIT_MSG_CLEANUP_SPACE, COMMIT_MSG_CLEANUP_SPACE }, + { "strip", COMMIT_MSG_CLEANUP_ALL, COMMIT_MSG_CLEANUP_ALL }, + { "scissors", COMMIT_MSG_CLEANUP_SCISSORS, COMMIT_MSG_CLEANUP_SPACE }, + { NULL, 0, 0 } +}; + static int git_sequencer_config(const char *k, const char *v, void *cb) { struct replay_opts *opts = cb; @@ -504,26 +520,37 @@ static int fast_forward_to(struct repository *r, enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg, int use_editor, int die_on_error) { - if (!cleanup_arg || !strcmp(cleanup_arg, "default")) - return use_editor ? COMMIT_MSG_CLEANUP_ALL : - COMMIT_MSG_CLEANUP_SPACE; - else if (!strcmp(cleanup_arg, "verbatim")) - return COMMIT_MSG_CLEANUP_NONE; - else if (!strcmp(cleanup_arg, "whitespace")) - return COMMIT_MSG_CLEANUP_SPACE; - else if (!strcmp(cleanup_arg, "strip")) - return COMMIT_MSG_CLEANUP_ALL; - else if (!strcmp(cleanup_arg, "scissors")) - return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS : - COMMIT_MSG_CLEANUP_SPACE; - else if (!die_on_error) { + struct cleanup_config_mapping *def = &cleanup_config_mapping[0]; + struct cleanup_config_mapping *p; + + if (!cleanup_arg) + return use_editor ? def->editor : + def->no_editor; + + for (p = cleanup_config_mapping; p->value; p++) + if (!strcmp(cleanup_arg, p->value)) + return use_editor ? p->editor : + p->no_editor; + + 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; + return use_editor ? def->editor : + def->no_editor; } else die(_("invalid cleanup mode %s"), cleanup_arg); } +static const char *describe_cleanup_mode(enum commit_msg_cleanup_mode cleanup_mode) +{ + struct cleanup_config_mapping *curr; + + for (curr = &cleanup_config_mapping[1]; curr->value; curr++) + if (cleanup_mode == curr->editor) + return curr->value; + + BUG("invalid cleanup_mode provided (%d)", cleanup_mode); +} + void append_conflicts_hint(struct index_state *istate, struct strbuf *msgbuf) { @@ -2350,6 +2377,8 @@ static int populate_opts_cb(const char *key, const char *value, void *data) opts->allow_rerere_auto = git_config_bool_or_int(key, value, &error_flag) ? RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; + else if (!strcmp(key, "options.default-msg-cleanup")) + opts->default_msg_cleanup = get_cleanup_mode(value, 1, 1); else return error(_("invalid key: %s"), key); @@ -2754,6 +2783,9 @@ static int save_opts(struct replay_opts *opts) res |= git_config_set_in_file_gently(opts_file, "options.allow-rerere-auto", opts->allow_rerere_auto == RERERE_AUTOUPDATE ? "true" : "false"); + + res |= git_config_set_in_file_gently(opts_file, "options.default-msg-cleanup", + describe_cleanup_mode(opts->default_msg_cleanup)); return res; }