jonschlinkert / git-username

Extract the username from a git remote origin URL.
https://github.com/jonschlinkert
MIT License
18 stars 3 forks source link

Wrong behavior? #4

Open tunnckoCore opened 5 years ago

tunnckoCore commented 5 years ago

I think it should defaults to the ~/.gitconfig's user.username?

Most of others git-* packages are based on it, not on the remote.

Scenario: I have @tunnckoCoreLabs, and the git username with which you actually push is @tunnckoCore. Instead, it gives me tunnckoCoreLabs.

I think it should default to the global one, if not then get it from the remote.

Damn, how many commits i did before i realize that problem :D

doowb commented 5 years ago

I think you should be using git-user-name to get your username that you'll be pushing with. This library returns the username/owner of the repository.

tunnckoCore commented 5 years ago

I want both: the name which is Charlike Mike Reagent and it is used for signing commits too and GPG; and username which actually probably can be anything, but most common case is to be your Github user/personal profile.

Probably may be better to rename this package to git-user-remote or git-remote-owner or git-owner.

That's it.

[user]
    name = Charlike Mike Reagent
    username = tunnckoCore
    email = mameto2011@gmail.com
    signingkey = XXX

If i change the user.name it will mess with the signing and gpg, and also not make much sense to me to be your github profile.

The only thing is that i'm not sure if user.username is standardized or used.. and is it what it sounds it should be?

edit: Hm. Or what about to create git-user-login which will get user.login :D following the GitHub's naming.

tunnckoCore commented 5 years ago

Before few years i created:

And thought to use the global .gitconfig for storing github's meta data. But.. yea, it was while ago and probably not make much sense.

edit: Haha, it's so ridiculous to look at some of yourself's old code. :D

tunnckoCore commented 5 years ago

But currently i workaround it like that

// Workaround for the https://github.com/jonschlinkert/git-username/issues/4
export function gitUsername(options) {
  const opt = Object.assign({ type: 'global' }, options && options.gitconfig);
  const gc = gitconfig(opt);
  const opts = Object.assign({ cwd: '/', path: gc }, options);
  const config = parse.sync(opts) || {};
  return config.user && config.user.username
    ? config.user.username
    : gitRemoteOwner();
}

because later

    const author = {
      url: 'https://tunnckocore.com',
      name: gitName(),
      email: gitEmail() || 'mameto2011@gmail.com',
      twitter: 'tunnckoCore',
      username: gitUsername(),
    };
doowb commented 5 years ago

https://github.com/jonschlinkert/git-user-name does basically what that gitUsername function does but it returns the config.user.name property... it will also use the local config first, then fallback to global if necessary.

Since this library has different intentions, and git-user-name returns the user.name, I think you can use parse-git-config directly (like you're doing) but simplify it to this:

export function gitUsername(options = {}) {
  const opts = Object.assign({ type: 'global' }, options.gitconfig);
  const config = parse.sync(opts) || {};
  return config.user && config.user.username ? config.user.username : gitRemoteOwner();
};

Haha, it's so ridiculous to look at some of yourself's old code. :D

Lol... I know what you mean... and it's great that one of those libraries you link to is just an empty function :)

tunnckoCore commented 5 years ago

it will also use the local config first, then fallback to global if necessary.

I don't see how it ever will fallback, since we always have local .git and remote? Meeh, or almost always.

but simplify it to this:

Good catch, i'll try. I probably will externalize it as module, just need to think of good name. For now git-user-login sounds best to me.

and it's great that one of those libraries you link to is just an empty function :)

Hahaha yea really. Yea, right? :laughing: Best module ever.