Search code examples
c++libgit2

setting credentials just for the first time (libgit2)


So I use some libgit2 functions to connect to the remote and perform git push. I want to push to private repository, so I have to set credentials.

But for example, when I work with private repositories via git commands and I want to connect for the first time to this repository, I enter credentials...but after that, if I want to push for the second, third time or so, I don't have to write that credentials again, also when I restart computer. It seems to me that there is some place where "known repositories" are stored with their credentials.

And my question is, is there a way how to do that with libgit2? I mean, is there a way how to push to "known" private remote repositories without setting credentials? "Known" means repositories which I already worked with so I have already set credentials

I want to work with many different repositories, each of these repositories have their own credentials so I don't want to set credentials manually everytime I push.

When I perform push without setting credentials(push to "known" repo), this error message is displayed

Request failed with status code: 401

So is there a way, how not to set credentials every time I push?


Solution

  • Command line git has a set of credential helpers that securely store credentials using native platform APIs. There are credential helpers that use Keychain on Mac, Windows Credential Manager on Windows, and gnome-keyring and libsecret on Linux. See the chapter on credential helpers in the git book for more information.

    Libgit2 does not have a similar abstraction for securely storing passwords. Perhaps it should. Instead your application can adapt the different native APIs to store passwords then provide them to the libgit2 credential callback based on the username and hostname parameters.