{"thread":{"id":"45194","subject":"feature request: user email config per domain","startedAt":"2017-02-22T13:12:46Z","lastAt":"2017-02-23T19:29:37Z","messageCount":6,"participants":["Tushar Kapila","Pranit Bauva","Andrew Ardill","Jeff King","Igor Djordjevic BugA","Igor Djordjevic"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"312281","messageId":"CAN0Skmmjd5Y0uWz_WC69mAStucZ6nR0mjdp4-ODJz2UnTaB-eQ@mail.gmail.com","threadId":"45194","inReplyTo":null,"subject":"feature request: user email config per domain","fromName":"Tushar Kapila","fromEmail":"tgkprog@gmail.com","sentAt":"2017-02-22T13:12:02Z","receivedAt":"2017-02-22T13:12:46Z","isPatch":false,"sender":{"key":"tgkprog@gmail.com","avatar":null},"body":"I can set my email via:\ngit config --global user.email tgkprog@xyz.dom\n\nthis is dangerous when I use this my office or in a multi repository\nprovider environment where my email is different for a few (like\ntgkprog@search.com for github and tushar@mycompany.com for my company\nprivate repo). I know I can over ride it per repository, but sometimes\nforget to do that. And even if I unset it, it inadvertantly gets set\nelsewhere when I make a repo and the site 'helps' me by showing me the\ncommands to init and clone my new repo.\nI did an analysis on a bunch of company git repositories using jgit\n(only master branch), and we have 57 emails out of 346 which are not\nthe company email. Also in there are cases when name is the same but\nsome commits are by email 1 and others by email 2, because of this\nglobal config. As some of us work on open source and company repos on\nthe same computer.\n\nFeature request :  can we have a config for email per repo domain ?\nSomething like:\n\ngit config --global domain.user.email tgkprog@test.xyz.com\ntesting.abc.doman:8080\n\ngit config --global domain.user.email tgkprog@xyz.com abc.doman:80\n\ngit config --global domain.user.email tgkprog@search.com github.com\n\nSo when remote URL has github.com push as tgkprog@search.com but for\ntesting.abc.doman:8080 use tgkprog@test.xyz.com ?\n\nFor me one name is enough. But can do the same for name if others need it?\n\nThank you.\n\nRegards\nTushar Kapila\n"},{"id":"312282","messageId":"CAFZEwPNEeCyVUbaU2J_9CuqCnvRjMR0DuHjz9cM81DzN0MYN8g@mail.gmail.com","threadId":"45194","inReplyTo":"CAN0Skmmjd5Y0uWz_WC69mAStucZ6nR0mjdp4-ODJz2UnTaB-eQ@mail.gmail.com","subject":"Re: feature request: user email config per domain","fromName":"Pranit Bauva","fromEmail":"pranit.bauva@gmail.com","sentAt":"2017-02-22T13:51:35Z","receivedAt":"2017-02-22T13:51:41Z","isPatch":false,"sender":{"key":"pranit.bauva@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2959938?v=4"},"body":"Hey Tushar,\n\nWhen you run `git config --global user.email abc@xyz.com` it writes\nout this setting in ~/.gitconfig which is then considered to be\n\"global\" ie. this same email will be used for every repo, but ...\n\nYou can always override this. Let's say there is a repo named \"foo\" in\nwhich you want the email \"xyz@abc.com\", then you go inside that folder\nand type `git config user.email xyz@abc.com`. Note: the option\n`--global` is skipped here. This creates a file `.gitconfig` in the\nfolder \"foo\" and now whenever you commit, the .gitconfig file inside\nthe repo will get the first preference and then the global\n~/.gitconfig.\n\nThis will work for you assuming that you have different repos for your\ncompany and for your open source work. Will this solve your problem?\n\nRegards,\nPranit Bauva\n"},{"id":"312351","messageId":"CAH5451=AqQ3+sE9P6njXdrFWcgbQoECJX2PXmL1b3xoKX6wv8A@mail.gmail.com","threadId":"45194","inReplyTo":"CAN0Skmmjd5Y0uWz_WC69mAStucZ6nR0mjdp4-ODJz2UnTaB-eQ@mail.gmail.com","subject":"Re: feature request: user email config per domain","fromName":"Andrew Ardill","fromEmail":"andrew.ardill@gmail.com","sentAt":"2017-02-22T23:21:12Z","receivedAt":"2017-02-22T23:23:30Z","isPatch":false,"sender":{"key":"andrew.ardill@gmail.com","avatar":"https://gravatar.com/avatar/da14cb7c091dd44dc6c63a4d3361b149acaf25226dc78eb4131a17b93d9b0993?d=mp&s=160"},"body":"On 23 February 2017 at 00:12, Tushar Kapila <tgkprog@gmail.com> wrote:\n> I can set my email via:\n> git config --global user.email tgkprog@xyz.dom\n>\n> this is dangerous when I use this my office or in a multi repository\n> provider environment where my email is different for a few (like\n> tgkprog@search.com for github and tushar@mycompany.com for my company\n> private repo).\n\nThere has been a large discussion around this idea before, see this\nthread for example [0].\n\nThe proposed idea was to have something set in your global config that\nwould only be turned on if you were within a given directory. This\nwould allow you to have two root directories, one for your work\nprojects and one for open source projects (for example) and any git\nrepositories within those folders would each would have their own\nconfig options automatically applied based on their location.\n\nThere was a patch suggested, and it worked quite well, but nothing\nfurther has been done to my knowledge.\n\n[0] http://public-inbox.org/git/7vvc3d1o01.fsf@alter.siamese.dyndns.org/\n\nRegards,\n\nAndrew Ardill\n"},{"id":"312364","messageId":"20170223004235.lsfyboofmqg6btt7@sigill.intra.peff.net","threadId":"45194","inReplyTo":"CAN0Skmmjd5Y0uWz_WC69mAStucZ6nR0mjdp4-ODJz2UnTaB-eQ@mail.gmail.com","subject":"Re: feature request: user email config per domain","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-02-23T00:42:35Z","receivedAt":"2017-02-23T00:49:30Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Feb 22, 2017 at 06:42:02PM +0530, Tushar Kapila wrote:\n\n> Feature request :  can we have a config for email per repo domain ?\n> Something like:\n> \n> git config --global domain.user.email tgkprog@test.xyz.com\n> testing.abc.doman:8080\n> \n> git config --global domain.user.email tgkprog@xyz.com abc.doman:80\n> \n> git config --global domain.user.email tgkprog@search.com github.com\n> \n> So when remote URL has github.com push as tgkprog@search.com but for\n> testing.abc.doman:8080 use tgkprog@test.xyz.com ?\n\nThe idea of \"the domain for this repo\" doesn't really match Git's\ndistributed model. A repo may have no remotes at all, or multiple\nremotes with different domains (or even remotes which do not have a\ndomain associated with them, like local files, or remote helpers which\nobscure the domain name).\n\nIt sounds like you're assuming that the domain would come from looking\nat remote.origin.url. Which I agree would work in the common case of\n\"git clone https://example.com\", but I'm not comfortable with the number\nof corner cases the feature has.\n\nI'd much rather see something based on the working tree path, like Duy's\nconditional config includes. Then you write something in your\n~/.gitconfig like:\n\n  [include \"gitdir:/home/peff/work/\"]\n  path = .gitconfig-work\n\n  [include \"gitdir:/home/peff/play/\"]\n  path = .gitconfig-play\n\nand set user.email as appropriate in each.\n\nYou may also set user.useConfigOnly in your ~/.gitconfig. Then you'd\nhave to set user.email in each individual repository, but at least Git\nwould complain and remind you when you forget to do so, rather than\nsilently using the wrong email.\n\n-Peff\n"},{"id":"312367","messageId":"5e25e9d5-ca52-2b32-3701-4826599774ef@gmail.com","threadId":"45194","inReplyTo":"CAN0Skmmjd5Y0uWz_WC69mAStucZ6nR0mjdp4-ODJz2UnTaB-eQ@mail.gmail.com","subject":"Re: feature request: user email config per domain","fromName":"Igor Djordjevic BugA","fromEmail":"igor.d.djordjevic@gmail.com","sentAt":"2017-02-23T01:30:00Z","receivedAt":"2017-02-23T01:30:30Z","isPatch":false,"sender":{"key":"igor.d.djordjevic@gmail.com","avatar":null},"body":"Hi Tushar,\n\nOn 22/02/2017 14:12, Tushar Kapila <tgkprog@gmail.com> wrote:\n> So when remote URL has github.com push as tgkprog@search.com but for\n> testing.abc.doman:8080 use tgkprog@test.xyz.com ?\n\nI`m not sure if this is sensible, as authorship information is baked\ninto the commit at the time of committing, which (usually ;) happens\nbefore you get to 'git push' to the other repo.\n\nIf possible, changing this info after the fact, on 'git push', would\ninfluence the existing commit you`re trying to send over, so your\n'git-push' would have a surprising consequence of not actually\npushing your desired commit at all, but creating a totally new commit\ninside the other repo -- this new commit would be exactly the same\npatch-wise (in regards to differences introduced), but because of the\nchanged user info it would be considered a different commit\nnonetheless (different hash).\n\n> ... I know I can over ride it per repository, but sometimes\n> forget to do that. And even if I unset it, it inadvertantly gets set\n> elsewhere when I make a repo and the site 'helps' me by showing me the\n> commands to init and clone my new repo.\n\nOtherwise, as you already stated that you find the current local (per\nrepo) user settings override logic inconvenient (error-prone), you\nmight be interested in approach described in this[1] Stack Overflow\npost.\n\nIn short, it uses a template-injected 'post-checkout' hook (triggered\non 'git clone' as well) alongside '.gitconfig' (global) settings to\nachieve what seems to be pretty similar to what you asked for (but\nmight be a bit more sensible), where you may fine-tune it further to\nbetter suit your needs.\n\nOn 20/02/2017 21:12, Grant Humphries[2] wrote[1]:\n> This answer is partially inspired by the post by @Saucier, but I was\n> looking for an automated way to set user.name and user.email on a per\n> repo basis, based on the remote, that was a little more light weight\n> than the git-passport package that he developed. Also h/t to @John\n> for the useConfigOnly setting. Here is my solution:\n>\n> .gitconfig changes:\n>\n> \t[github]\n> \t\tname = <github username>\n> \t\temail = <github email>\n> \t[gitlab]\n> \t\tname = <gitlab username>\n> \t\temail = <gitlab email>\n> \t[init]\n> \t\ttemplatedir = ~/.git-templates\n> \t[user]\n> \t\tuseConfigOnly = true\n>\n> post-checkout hook which should be saved to the following path:\n> ~/.git-templates/hooks/post-checkout:\n>\n> \t#!/usr/bin/env bash\n> \t\n> \t# make regex matching below case insensitive\n> \tshopt -s nocasematch\n> \t\n> \t# values in the services array should have a corresponding section in\n> \t# .gitconfig where the 'name' and 'email' for that service are specified\n> \tremote_url=\"$( git config --get --local remote.origin.url )\"\n> \tservices=(\n> \t\t'github'\n> \t\t'gitlab'\n> \t)\n> \t\n> \tset_local_user_config() {\n> \t\tlocal service=\"${1}\"\n> \t\tlocal config=\"${2}\"\n> \t\tlocal service_config=\"$( git config --get ${service}.${config} )\"\n> \t\tlocal local_config=\"$( git config --get --local user.${config} )\"\n> \t\n> \t\tif [[ \"${local_config}\" != \"${service_config}\" ]]; then\n> \t\t\tgit config --local \"user.${config}\" \"${service_config}\"\n> \t\t\techo \"repo 'user.${config}' has been set to '${service_config}'\"\n> \t\tfi\n> \t}\n> \t\n> \t# if remote_url doesn't contain the any of the values in the services\n> \t# array the user name and email will remain unset and the\n> \t# user.useConfigOnly = true setting in .gitconfig will prompt for those\n> \t# credentials and prevent commits until they are defined\n> \tfor s in \"${services[@]}\"; do\n> \t\tif [[ \"${remote_url}\" =~ \"${s}\" ]]; then\n> \t\t\tset_local_user_config \"${s}\" 'name'\n> \t\t\tset_local_user_config \"${s}\" 'email'\n> \t\t\tbreak\n> \t\tfi\n> \tdone\n>\n> I use different credentials for github and gitlab, but those\n> references in the code above could be replaced or augmented with any\n> service that you use. In order to have the post-checkout hook\n> automatically set the user name and email locally for a repo after a\n> checkout make sure the service name appears in the remote url, add it\n> to the services array in the post-checkout script and create a\n> section for it in your .gitconfig that contains your user name and\n> email for that service.\n>\n> If none of the service names appear in the remote url or the repo\n> doesn't have a remote the user name and email will not be set\n> locally. In these cases the user.useConfigOnly setting will be in\n> play which will not allow you to make commits until the user name and\n> email are set at the repo level, and will prompt the user to\n> configure that information.\n\nRegards,\nBuga\n\n*P.S.* For the purpose of completeness and archiving I copied the\nStack Overflow post[1] here as well, but all the credits go to its\nauthor[2] (you may upvote the linked post[1] if you find it helpful).\nPlease feel free let me know if this practice is otherwise to be\navoided.\n\n[1] http://stackoverflow.com/a/42354282\n[2] http://stackoverflow.com/users/2167004\n"},{"id":"312422","messageId":"00803225-db9e-9ba0-fe37-8977ec611d61@gmail.com","threadId":"45194","inReplyTo":"5e25e9d5-ca52-2b32-3701-4826599774ef@gmail.com","subject":"Fwd: Re: feature request: user email config per domain","fromName":"Igor Djordjevic","fromEmail":"igor.d.djordjevic@gmail.com","sentAt":"2017-02-23T19:29:26Z","receivedAt":"2017-02-23T19:29:37Z","isPatch":false,"sender":{"key":"igor.d.djordjevic@gmail.com","avatar":null},"body":"Forwarding a message that ended on my end only, probably by accident.\n\n-------- Forwarded Message --------\nSubject: Re: feature request: user email config per domain\nDate: Thu, 23 Feb 2017 13:32:56 +0530\nFrom: Tushar Kapila <tgkprog@gmail.com>\nTo: Igor Djordjevic BugA <igor.d.djordjevic@gmail.com>\n\nHello All,\n> I'd much rather see something based on the working tree path, like\nDuy's conditional config includes. Then you write something in your\n~/.gitconfig\n\n> This would allow you to have two root directories, one for your work\nprojects and one for open source projects (for example).\n\nI guess this can be extended for any number of root directories. Like,\nwhen a consultant has multiple employer email ids.\n\nThis sounds great and it would enforce at commit time, which as\npointed out, is the correct time to do it. If for some reason it is\nnot adopted I at least hope that we have a simple global config which\nspecifies that user must set email for each repo and to ignore any\nglobal config.\n\nAm sure this can be done via hooks, but I would like something that is\nreally simple for newbies and companies to enforce with minimal\ninstruction.\n\n* Will try what Igor and Grant Humphries suggests.\n\n* Thank you all for your replies, and a big thank you for git - its a\ngreat tool. I used to dream of something like it when I was stuck with\nsvn (pre 2010 I was introduced to git late.)\n\n\n[1] http://stackoverflow.com/a/42354282\n[2] http://stackoverflow.com/users/2167004\n"}]}