From patchwork Mon Feb 25 23:16:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gummerer X-Patchwork-Id: 10829261 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 37A1E1390 for ; Mon, 25 Feb 2019 23:17:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2713C2B864 for ; Mon, 25 Feb 2019 23:17:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1B69A2BD35; Mon, 25 Feb 2019 23:17:00 +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 AF4732B864 for ; Mon, 25 Feb 2019 23:16:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728957AbfBYXQ5 (ORCPT ); Mon, 25 Feb 2019 18:16:57 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:35318 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727421AbfBYXQz (ORCPT ); Mon, 25 Feb 2019 18:16:55 -0500 Received: by mail-wm1-f66.google.com with SMTP id y15so576316wma.0 for ; Mon, 25 Feb 2019 15:16:53 -0800 (PST) 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=JlD68eCgrUfwauFpzHbRnXcwN5b8qnBUaZwPTeb7llI=; b=FAXnub9rWXa4AYfPADm22S7DtdyU5ujFmgpEcOultojeF490fGXxqjsO73T4J4AXHK ZUfeh0Sg+07zt1314lstII1uWEY+04yxflZIkS9V7p9h3TAf/ovqmo1AUfwULEY1Oouu qpZj4BCZkr6hfGphw62OgmlteGH4H3BeNHPi/O7teuuHiD/LPwgcOBS/9xXHvFd+JEDf SVa+NxqwRXxRErQoNxClg+7yirOGTAvMsgsoMytyX44KMcDMjt/EMbuwAZiyUr/ig08z y82NIXqBnjVny9gCbmOnf5rwrdJMA9KsQ/PF+AQFfY3rNay0Yk1nYV4ubtGFq6YV8BOZ eHkQ== 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=JlD68eCgrUfwauFpzHbRnXcwN5b8qnBUaZwPTeb7llI=; b=mJfdF/90UvUAZ9rmLe3seaXdvFzehj602FulxBlKzI9GEEb1kouJkY+0p/suPx6kHQ SwkJYuiDjvKyQ1kUiU/3Jopw3PXMO9sL9psDe0fVxuUXeLCISBhKYaSY9iwXuTRJLQaX 8Am/Tm6NLsY1/if7ODW0ooHQ1AgTKFo0H5djC2p0GIRk6uf59pANjtn4iHPMbVJ0OMxG dW7NzARNruSZX0QhUNeQkcpcyTr1gxHYvPJgi4/bDq4cvOAnH6jMp3IQCiYxan41Sh+J 5S+V0Nxyl0sQZWVLn4ZJ5rIiFlLOHWrRV8swVR5i0PPDkYY3WubFbt5doAgyHisGqCMC 7Qbg== X-Gm-Message-State: AHQUAuYHzovovfQYAP7lt++7nBaM4H7lZpqN1Bgl+EJnsO4yWwWVquFV 7LqlZHYW7AckGXMtM0w36JB/De8U X-Google-Smtp-Source: AHgI3Ibw4Gbmd854a7Ww8MT/eaXlLiuIhKbDEGKal2fprZlMilDy4HyhdN9FEP62ALWbzoqpyo25Kw== X-Received: by 2002:a1c:9aca:: with SMTP id c193mr668721wme.2.1551136612237; Mon, 25 Feb 2019 15:16:52 -0800 (PST) Received: from localhost ([95.149.189.205]) by smtp.gmail.com with ESMTPSA id j13sm105876wrx.74.2019.02.25.15.16.50 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 25 Feb 2019 15:16:51 -0800 (PST) From: Thomas Gummerer To: git@vger.kernel.org Cc: Junio C Hamano , Johannes Schindelin , Paul-Sebastian Ungureanu , =?utf-8?q?SZED?= =?utf-8?q?ER_G=C3=A1bor?= , Johannes Schindelin , Thomas Gummerer Subject: [PATCH v13 04/27] ident: add the ability to provide a "fallback identity" Date: Mon, 25 Feb 2019 23:16:08 +0000 Message-Id: <20190225231631.30507-5-t.gummerer@gmail.com> X-Mailer: git-send-email 2.21.0.rc2.291.g17236886c5 In-Reply-To: <20190225231631.30507-1-t.gummerer@gmail.com> References: <20190225231631.30507-1-t.gummerer@gmail.com> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Johannes Schindelin In 3bc2111fc2e9 (stash: tolerate missing user identity, 2018-11-18), `git stash` learned to provide a fallback identity for the case that no proper name/email was given (and `git stash` does not really care about a correct identity anyway, but it does want to create a commit object). In preparation for the same functionality in the upcoming built-in version of `git stash`, let's offer the same functionality as an API function. Signed-off-by: Johannes Schindelin [tg: add docs; make it a bug to call the function before other functions in the ident API] Signed-off-by: Thomas Gummerer --- cache.h | 5 +++++ ident.c | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/cache.h b/cache.h index 7b6b89fc4c..611e554dea 100644 --- a/cache.h +++ b/cache.h @@ -1491,6 +1491,11 @@ extern const char *git_sequence_editor(void); extern const char *git_pager(int stdout_is_tty); extern int is_terminal_dumb(void); extern int git_ident_config(const char *, const char *, void *); +/* + * Prepare an ident to fall back on if the user didn't configure it. + * Must be called before any other function from the ident API. + */ +void prepare_fallback_ident(const char *name, const char *email); extern void reset_ident_date(void); struct ident_split { diff --git a/ident.c b/ident.c index 33bcf40644..f30bd623f0 100644 --- a/ident.c +++ b/ident.c @@ -505,6 +505,28 @@ int git_ident_config(const char *var, const char *value, void *data) return 0; } +static void set_env_if(const char *key, const char *value, int *given, int bit) +{ + if (*given & bit) + BUG("%s was checked before prepare_fallback got called", key); + if (getenv(key)) + return; /* nothing to do */ + setenv(key, value, 0); + *given |= bit; +} + +void prepare_fallback_ident(const char *name, const char *email) +{ + set_env_if("GIT_AUTHOR_NAME", name, + &author_ident_explicitly_given, IDENT_NAME_GIVEN); + set_env_if("GIT_AUTHOR_EMAIL", email, + &author_ident_explicitly_given, IDENT_MAIL_GIVEN); + set_env_if("GIT_COMMITTER_NAME", name, + &committer_ident_explicitly_given, IDENT_NAME_GIVEN); + set_env_if("GIT_COMMITTER_EMAIL", email, + &committer_ident_explicitly_given, IDENT_MAIL_GIVEN); +} + static int buf_cmp(const char *a_begin, const char *a_end, const char *b_begin, const char *b_end) {