Hi Tushar,

On 22/02/2017 14:12, Tushar Kapila <tgkp...@gmail.com> wrote:
> So when remote URL has github.com push as tgkp...@search.com but for
> testing.abc.doman:8080 use tgkp...@test.xyz.com ?

I`m not sure if this is sensible, as authorship information is baked
into the commit at the time of committing, which (usually ;) happens
before you get to 'git push' to the other repo.

If possible, changing this info after the fact, on 'git push', would
influence the existing commit you`re trying to send over, so your
'git-push' would have a surprising consequence of not actually
pushing your desired commit at all, but creating a totally new commit
inside the other repo -- this new commit would be exactly the same
patch-wise (in regards to differences introduced), but because of the
changed user info it would be considered a different commit
nonetheless (different hash).

> ... I know I can over ride it per repository, but sometimes
> forget to do that. And even if I unset it, it inadvertantly gets set
> elsewhere when I make a repo and the site 'helps' me by showing me the
> commands to init and clone my new repo.

Otherwise, as you already stated that you find the current local (per
repo) user settings override logic inconvenient (error-prone), you
might be interested in approach described in this[1] Stack Overflow
post.

In short, it uses a template-injected 'post-checkout' hook (triggered
on 'git clone' as well) alongside '.gitconfig' (global) settings to
achieve what seems to be pretty similar to what you asked for (but
might be a bit more sensible), where you may fine-tune it further to
better suit your needs.

On 20/02/2017 21:12, Grant Humphries[2] wrote[1]:
> This answer is partially inspired by the post by @Saucier, but I was
> looking for an automated way to set user.name and user.email on a per
> repo basis, based on the remote, that was a little more light weight
> than the git-passport package that he developed. Also h/t to @John
> for the useConfigOnly setting. Here is my solution:
>
> .gitconfig changes:
>
>       [github]
>               name = <github username>
>               email = <github email>
>       [gitlab]
>               name = <gitlab username>
>               email = <gitlab email>
>       [init]
>               templatedir = ~/.git-templates
>       [user]
>               useConfigOnly = true
>
> post-checkout hook which should be saved to the following path:
> ~/.git-templates/hooks/post-checkout:
>
>       #!/usr/bin/env bash
>       
>       # make regex matching below case insensitive
>       shopt -s nocasematch
>       
>       # values in the services array should have a corresponding section in
>       # .gitconfig where the 'name' and 'email' for that service are specified
>       remote_url="$( git config --get --local remote.origin.url )"
>       services=(
>               'github'
>               'gitlab'
>       )
>       
>       set_local_user_config() {
>               local service="${1}"
>               local config="${2}"
>               local service_config="$( git config --get ${service}.${config} 
> )"
>               local local_config="$( git config --get --local user.${config} 
> )"
>       
>               if [[ "${local_config}" != "${service_config}" ]]; then
>                       git config --local "user.${config}" "${service_config}"
>                       echo "repo 'user.${config}' has been set to 
> '${service_config}'"
>               fi
>       }
>       
>       # if remote_url doesn't contain the any of the values in the services
>       # array the user name and email will remain unset and the
>       # user.useConfigOnly = true setting in .gitconfig will prompt for those
>       # credentials and prevent commits until they are defined
>       for s in "${services[@]}"; do
>               if [[ "${remote_url}" =~ "${s}" ]]; then
>                       set_local_user_config "${s}" 'name'
>                       set_local_user_config "${s}" 'email'
>                       break
>               fi
>       done
>
> I use different credentials for github and gitlab, but those
> references in the code above could be replaced or augmented with any
> service that you use. In order to have the post-checkout hook
> automatically set the user name and email locally for a repo after a
> checkout make sure the service name appears in the remote url, add it
> to the services array in the post-checkout script and create a
> section for it in your .gitconfig that contains your user name and
> email for that service.
>
> If none of the service names appear in the remote url or the repo
> doesn't have a remote the user name and email will not be set
> locally. In these cases the user.useConfigOnly setting will be in
> play which will not allow you to make commits until the user name and
> email are set at the repo level, and will prompt the user to
> configure that information.

Regards,
Buga

*P.S.* For the purpose of completeness and archiving I copied the
Stack Overflow post[1] here as well, but all the credits go to its
author[2] (you may upvote the linked post[1] if you find it helpful).
Please feel free let me know if this practice is otherwise to be
avoided.

[1] http://stackoverflow.com/a/42354282
[2] http://stackoverflow.com/users/2167004

Reply via email to