{"thread":{"id":"56776","subject":"[PATCH] gpg-interface: allow selecting key id with env variable","startedAt":"2021-10-24T19:37:50Z","lastAt":"2021-11-07T16:27:49Z","messageCount":9,"participants":["Marco Beck","Junio C Hamano","Fabian Stelzer","Ævar Arnfjörð Bjarmason"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"439503","messageId":"20211024193625.148-1-mbeck@eaddrinuse.net","threadId":"56776","inReplyTo":null,"subject":"[PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Marco Beck","fromEmail":"mbeck@eaddrinuse.net","sentAt":"2021-10-24T19:36:25Z","receivedAt":"2021-10-24T19:37:50Z","isPatch":true,"sender":{"key":"mbeck@eaddrinuse.net","avatar":null},"body":"This allows selecting the gpg(1) key id to use for signing by setting the\nenvironment variable GIT_GPG_SIGNINGKEY.\n\nSigned-off-by: Marco Beck <mbeck@eaddrinuse.net>\n---\n Documentation/git.txt | 6 ++++++\n gpg-interface.c       | 5 +++++\n 2 files changed, 11 insertions(+)\n\ndiff --git Documentation/git.txt Documentation/git.txt\nindex d63c65e67d..ab032f2760 100644\n--- Documentation/git.txt\n+++ Documentation/git.txt\n@@ -941,6 +941,12 @@ corresponding standard handle, and if `GIT_REDIRECT_STDERR` is\n `2>&1`, standard error will be redirected to the same handle as\n standard output.\n \n+`GIT_GPG_SIGNINGKEY`::\n+\tIf this environment variable is set, then Git commands which use\n+\t`gpg(1)` for signing (e.g. linkgit:git-commit[1], linkgit:git-tag[1])\n+\twill use the specified key. See also the `user.signingkey` option in\n+\tlinkgit:git-config[1].\n+\n `GIT_PRINT_SHA1_ELLIPSIS` (deprecated)::\n \tIf set to `yes`, print an ellipsis following an\n \t(abbreviated) SHA-1 value.  This affects indications of\ndiff --git gpg-interface.c gpg-interface.c\nindex 127aecfc2b..10e923140c 100644\n--- gpg-interface.c\n+++ gpg-interface.c\n@@ -435,6 +435,11 @@ int git_gpg_config(const char *var, const char *value, void *cb)\n \n const char *get_signing_key(void)\n {\n+\tconst char *signing_key;\n+\n+\tif ((signing_key = getenv(\"GIT_GPG_SIGNINGKEY\")))\n+\t\treturn signing_key;\n+\n \tif (configured_signing_key)\n \t\treturn configured_signing_key;\n \treturn git_committer_info(IDENT_STRICT|IDENT_NO_DATE);\n-- \n2.32.0\n\n"},{"id":"439508","messageId":"xmqqsfwq40w7.fsf@gitster.g","threadId":"56776","inReplyTo":"20211024193625.148-1-mbeck@eaddrinuse.net","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-10-24T20:35:04Z","receivedAt":"2021-10-24T20:35:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Marco Beck <mbeck@eaddrinuse.net> writes:\n\n> This allows selecting the gpg(1) key id to use for signing by setting the\n> environment variable GIT_GPG_SIGNINGKEY.\n\nCan you explain why we want to add another way to specify the key?\nIt appears to me that user.signingkey is an adequate \"set it once,\nforget it, and keep using it\" way to do so.  \"I cannot write to the\nconfig file\" is unlikely, as the user must be capable of writing\ninto the repository to sign in the first place.\n\nIs the driving motivation \"I want to sign under a different identity\nfrom the one the repository specifies in its local configuration\"?\nWouldn't that be covered with a single-shot configuration syntax:\n\n    $ git -c user.signingkey=mbeck@eaddrinuse.net tag -s ...\n\nUnless there is a good reason to (and these days there aren't that\nmany), we prefer not to add new environment variables like this.\n\nThanks.\n"},{"id":"439510","messageId":"YXXMroOBvDERXIMP@octopus.int.carobme.de","threadId":"56776","inReplyTo":"xmqqsfwq40w7.fsf@gitster.g","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Marco Beck","fromEmail":"mbeck@eaddrinuse.net","sentAt":"2021-10-24T21:14:22Z","receivedAt":"2021-10-24T21:14:25Z","isPatch":true,"sender":{"key":"mbeck@eaddrinuse.net","avatar":null},"body":"On Sun, Oct 24, 2021 at 01:35:04PM -0700, Junio C Hamano wrote:\n\n>Can you explain why we want to add another way to specify the key?\n\nMotivation for that patch was that I usually use the same .gitconfig\n(all dotfiles actually) with multiple identities (e.g. company and private\nemail address). It's really convenient to switch to a different identity\nfor the rest of the session by just setting e.g. $GIT_AUTHOR_EMAIL and not\nneeding to remember adding --author to every invocation of git-commit(1)\netc. Thought it would be nice to have that convenience for selecting a\nsigning key as well.\n"},{"id":"439513","messageId":"8661a491-5ef1-a469-878f-7dd81d4a1aa2@gigacodes.de","threadId":"56776","inReplyTo":"YXXMroOBvDERXIMP@octopus.int.carobme.de","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Fabian Stelzer","fromEmail":"fs@gigacodes.de","sentAt":"2021-10-25T07:31:33Z","receivedAt":"2021-10-25T07:31:44Z","isPatch":true,"sender":{"key":"fs@gigacodes.de","avatar":"https://avatars.githubusercontent.com/u/564858?v=4"},"body":"On 24.10.21 23:14, Marco Beck wrote:\n> On Sun, Oct 24, 2021 at 01:35:04PM -0700, Junio C Hamano wrote:\n> \n>> Can you explain why we want to add another way to specify the key?\n> \n> Motivation for that patch was that I usually use the same .gitconfig\n> (all dotfiles actually) with multiple identities (e.g. company and private\n> email address). It's really convenient to switch to a different identity\n> for the rest of the session by just setting e.g. $GIT_AUTHOR_EMAIL and not\n> needing to remember adding --author to every invocation of git-commit(1)\n> etc. Thought it would be nice to have that convenience for selecting a\n> signing key as well.\n\nIf you only need to rarely change the key to sign with, the commands\nusually have a direct parameter to do so:\ngit tag -s -u <keyid>\ngit commit -S<keyid>\n\nOtherwise https://git-scm.com/docs/git-config#_conditional_includes\nmight be useful if you can store you private & work git in separate\ndirectories or only have a few selected repos per identity. Put all your\ndefaults in your .gitconfig and use sth like:\n\n[includeIf \"gitdir:~/projects/work/\"]\n  path = \".gitconfig.work\"\n\nto override them.\n\n\nKind regards,\nFabian\n"},{"id":"439515","messageId":"YXZpyWesc8VRWYwr@octopus.int.carobme.de","threadId":"56776","inReplyTo":"8661a491-5ef1-a469-878f-7dd81d4a1aa2@gigacodes.de","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Marco Beck","fromEmail":"mbeck@eaddrinuse.net","sentAt":"2021-10-25T08:24:41Z","receivedAt":"2021-10-25T08:25:14Z","isPatch":true,"sender":{"key":"mbeck@eaddrinuse.net","avatar":null},"body":"On Mon, Oct 25, 2021 at 09:31:33AM +0200, Fabian Stelzer wrote:\n\n>[includeIf \"gitdir:~/projects/work/\"]\n>  path = \".gitconfig.work\"\n\nYeah, that's what I added to my config recently just for signing key\nselection. I'm using direnv[1] for some time now to switch between\ndifferent \"profiles\" (more than just Git settings) by just setting\nsome environment variables and it was nice to have everything in one\nplace. That's why I disliked adding the includeIf to my .gitconfig.\n\nThanks anyway,\nMarco\n\n[1] https://direnv.net/\n"},{"id":"439519","messageId":"bc6b915a-7aa0-298b-e1ec-bf548e4dbdfc@gigacodes.de","threadId":"56776","inReplyTo":"YXZpyWesc8VRWYwr@octopus.int.carobme.de","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Fabian Stelzer","fromEmail":"fs@gigacodes.de","sentAt":"2021-10-25T08:46:56Z","receivedAt":"2021-10-25T08:47:04Z","isPatch":true,"sender":{"key":"fs@gigacodes.de","avatar":"https://avatars.githubusercontent.com/u/564858?v=4"},"body":"On 25.10.21 10:24, Marco Beck wrote:\n> On Mon, Oct 25, 2021 at 09:31:33AM +0200, Fabian Stelzer wrote:\n> \n>> [includeIf \"gitdir:~/projects/work/\"]\n>>  path = \".gitconfig.work\"\n> \n> Yeah, that's what I added to my config recently just for signing key\n> selection. I'm using direnv[1] for some time now to switch between\n> different \"profiles\" (more than just Git settings) by just setting\n> some environment variables and it was nice to have everything in one\n> place. That's why I disliked adding the includeIf to my .gitconfig.\n> \n> Thanks anyway,\n> Marco\n> \n> [1] https://direnv.net/\n\nIf you insist on using environment variables there is:\nGIT_CONFIG_KEY_<n>\nGIT_CONFIG_VALUE_<n>\n\nhttps://git-scm.com/docs/git-config\n\nA bit clunky in my opinion but you can set any config variable via env vars:\n\ne.g.:\nGIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.name GIT_CONFIG_VALUE_0=testing\n\n"},{"id":"439525","messageId":"xmqqwnm12xv7.fsf@gitster.g","threadId":"56776","inReplyTo":"YXXMroOBvDERXIMP@octopus.int.carobme.de","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-10-25T10:38:04Z","receivedAt":"2021-10-25T10:38:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Marco Beck <mbeck@eaddrinuse.net> writes:\n\n> On Sun, Oct 24, 2021 at 01:35:04PM -0700, Junio C Hamano wrote:\n>\n>>Can you explain why we want to add another way to specify the key?\n>\n> Motivation for that patch was that I usually use the same .gitconfig\n> (all dotfiles actually) with multiple identities (e.g. company and private\n> email address). It's really convenient to switch to a different identity\n> for the rest of the session by just setting e.g. $GIT_AUTHOR_EMAIL and not\n> needing to remember adding --author to every invocation of git-commit(1)\n> etc. Thought it would be nice to have that convenience for selecting a\n> signing key as well.\n\nIf you are flipping between two (or more) idents using GIT_AUTHOR_*\nbut not doing the same for GIT_COMMITTER_*, then your commits may be\nmade under the same committer identity for both work and hobby\nprojects, which may not be what you want.  And if you are flipping\nbetween multiple committer idents via GIT_COMMITTER_* environment\nvariables, the GPG signing key should follow the committer ident at\nno extra cost, I would think.\n\n\n"},{"id":"439547","messageId":"211025.86h7d5gobf.gmgdl@evledraar.gmail.com","threadId":"56776","inReplyTo":"bc6b915a-7aa0-298b-e1ec-bf548e4dbdfc@gigacodes.de","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-10-25T14:38:32Z","receivedAt":"2021-10-25T14:40:43Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Mon, Oct 25 2021, Fabian Stelzer wrote:\n\n> On 25.10.21 10:24, Marco Beck wrote:\n>> On Mon, Oct 25, 2021 at 09:31:33AM +0200, Fabian Stelzer wrote:\n>> \n>>> [includeIf \"gitdir:~/projects/work/\"]\n>>>  path = \".gitconfig.work\"\n>> \n>> Yeah, that's what I added to my config recently just for signing key\n>> selection. I'm using direnv[1] for some time now to switch between\n>> different \"profiles\" (more than just Git settings) by just setting\n>> some environment variables and it was nice to have everything in one\n>> place. That's why I disliked adding the includeIf to my .gitconfig.\n>> \n>> Thanks anyway,\n>> Marco\n>> \n>> [1] https://direnv.net/\n>\n> If you insist on using environment variables there is:\n> GIT_CONFIG_KEY_<n>\n> GIT_CONFIG_VALUE_<n>\n>\n> https://git-scm.com/docs/git-config\n>\n> A bit clunky in my opinion but you can set any config variable via env vars:\n>\n> e.g.:\n> GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.name GIT_CONFIG_VALUE_0=testing\n\nAlthough to be fair that's quite clunky if you're setting N number of\nthese.\n\nMarco: Would the \"include by env\" method discussed in the thread at\nhttps://lore.kernel.org/git/patch-1.1-1fe6f60d2bf-20210924T005553Z-avarab@gmail.com\nbe a better fit for what you're trying to do here, and eliminate the\nneed for you to have an env variable setting for this config key in\nparticular?\n\nAnyway, aside from that I wonder if we shouldn't just have this env\nvariable anyway for consistency with the user.name, user.email\netc. equivalents, which we probaby wouldn't add today if they didn't\nexist, but since we have them already...\n"},{"id":"440641","messageId":"YYf+f4DOtILHSNoX@octopus.int.carobme.de","threadId":"56776","inReplyTo":"bc6b915a-7aa0-298b-e1ec-bf548e4dbdfc@gigacodes.de","subject":"Re: [PATCH] gpg-interface: allow selecting key id with env variable","fromName":"Marco Beck","fromEmail":"mbeck@eaddrinuse.net","sentAt":"2021-11-07T16:27:43Z","receivedAt":"2021-11-07T16:27:49Z","isPatch":true,"sender":{"key":"mbeck@eaddrinuse.net","avatar":null},"body":"On Mon, Oct 25, 2021 at 10:46:56AM +0200, Fabian Stelzer wrote:\n>\n>If you insist on using environment variables there is:\n>GIT_CONFIG_KEY_<n>\n>GIT_CONFIG_VALUE_<n>\n>\n>https://git-scm.com/docs/git-config\n>\n>A bit clunky in my opinion but you can set any config variable via env vars:\n>\n>e.g.:\n>GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.name GIT_CONFIG_VALUE_0=testing\n\nWorks for me, thank you.\n\nMarco\n"}]}