git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Igor Djordjevic BugA <igor.d.djordjevic@gmail.com>
To: Tushar Kapila <tgkprog@gmail.com>, git@vger.kernel.org
Subject: Re: feature request: user email config per domain
Date: Thu, 23 Feb 2017 02:30:00 +0100	[thread overview]
Message-ID: <5e25e9d5-ca52-2b32-3701-4826599774ef@gmail.com> (raw)
In-Reply-To: <CAN0Skmmjd5Y0uWz_WC69mAStucZ6nR0mjdp4-ODJz2UnTaB-eQ@mail.gmail.com>

Hi Tushar,

On 22/02/2017 14:12, Tushar Kapila <tgkprog@gmail.com> wrote:
> So when remote URL has github.com push as tgkprog@search.com but for
> testing.abc.doman:8080 use tgkprog@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

  parent reply	other threads:[~2017-02-23  1:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-22 13:12 feature request: user email config per domain Tushar Kapila
2017-02-22 13:51 ` Pranit Bauva
2017-02-22 23:21 ` Andrew Ardill
2017-02-23  0:42 ` Jeff King
2017-02-23  1:30 ` Igor Djordjevic BugA [this message]
2017-02-23 19:29   ` Fwd: " Igor Djordjevic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5e25e9d5-ca52-2b32-3701-4826599774ef@gmail.com \
    --to=igor.d.djordjevic@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=tgkprog@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).