Home > Software engineering >  how can I make `git` fail instead of asking for credentials
how can I make `git` fail instead of asking for credentials

Time:07-05

I am trying to use git commands inside my script, and I want git to fail in any case so that I can check for errors my users supplied in their requests.

The current behavior of git is like this:

  • the address is not a git repo, it fails.
  • user space does not exist, it asks for a username/password
  • repo does not exist or is possibly private, it asks for a username/password
  • given username/password/token is wrong, it asks for a username/password.

But this is not just a simple "ask for credentials" function executed, but rather git spawns many other processes all with different process IDs, and also for some reason kills the original spawned process without notifying my spawner script. (Node.js' spawn)

At first, I tried using a timeout to kill the main spawned process if it does not write a response to its stdout but since there is no process with that ID anymore, I fail to check further for errors. I had to give up trying to clone a repo unless supplied address can be fetched from a public api.

All my searches ended up on the other side of the coin, "make it to ask for a password". I wonder if I failed to think of correct search terms for my purpose, or if it is just that there is no such way.

Please enlighten me if there is a flag I have missed, or some other way to do the trick.

PS: I hesitated but I have added node.js tag, but this might be a problem in any language that can spawn child processes.

CodePudding user response:

If you want to avoid Git prompting for credentials, you can set GIT_TERMINAL_PROMPT=0. That will prevent Git itself from prompting using a credential helper or making any other terminal requests, but it will not prevent other tools it spawns, such as OpenSSH, from prompting. There is no way to do that.

Thus, if you're looking to clone from most major hosting sites, you'll be better off using HTTPS URLs because in that case OpenSSH isn't used and all authentication prompts are handled by Git. In such a case, if authentication is required, it will simply fail.

  • Related