gitcreds icon indicating copy to clipboard operation
gitcreds copied to clipboard

gitcreds should check git version

Open MyKo101 opened this issue 3 years ago • 9 comments

I was trying to follow along with the Github credentials guide on usethis and when I got to the point of running gitcreds::gitcreds_set() I got the following error:

> gitcreds::gitcreds_set()
Error in throw(new_error("git_error", args = args, stdout = out, status = attr(out,  : 
  System git failed

In this functions, there's a tryCatch() intending to grab gitcreds_no_credentials and return NULL, however the error being thrown here is not a gitcreds_no_credentials condition and so the error was seeping through. The code goes on to run gitcreds_set_new(url) if the condition is caught and I was getting the same error response.

It took me way too long to realise my git version was out of date and after updating the error went away.

It would be useful for gitcreds to check a user's git version at some point in the process because the error I was receiving was very uninformative.

MyKo101 avatar Jan 14 '21 11:01 MyKo101

Hi, which version of gitcreds is this? I think the most recent version prints the output from git at least. Also, what was your git version before the upgrade?

gaborcsardi avatar Jan 25 '21 09:01 gaborcsardi

Unfortunately since I've updated my git version, I don't know which version of git I was actually using. Do you know if there is any way to find out which version I was using? Would it be in any of the .git/ files of an old repo?

I'm on gitcred version 0.1.0.

I think that the throw(new_error(...)) line is supposed to be doing as you say, output the git output when an error is hit. But since it's hitting an unexpected git error (because of my ancient version), it's not being caught in the right way.

MyKo101 avatar Jan 25 '21 09:01 MyKo101

gitcreds 0.1.1 prints the output of git, I believe.

gaborcsardi avatar Jan 25 '21 09:01 gaborcsardi

This is what I get for git 1.7.7:

Error in new_git_error("git_error", args = args, stdout = out, status = attr(out,  :
  System git failed: git: 'credential' is not a git command. See 'git --help'.

Is this better?

gaborcsardi avatar Jan 27 '21 11:01 gaborcsardi

Checking the git version programmatically seems hard, because the git version strings are quite creative. E.g. my 1.7.7 prints this:

> system("git --version")
git version 1.7.7.GIT

So I am afraid that we would do more harm with trying to parse these strings.

gaborcsardi avatar Jan 27 '21 11:01 gaborcsardi

Looking at the documentation for git, I believe that the credential command was introduced in version 2.25. Anything older than this wouldn't work with gitcreds, right?

I think the git versions also all have the three standard parts (major.minor.patch) followed by other system-type versions (mine is now 2.30.0.windows.1

Something like this should work as a check:


git_version <- function(){
  gv_raw <- system("git --version",intern=T)
  v_location <- regexpr("([0-9]*?\\.[0-9]*?\\.[0-9]*\\.)",gv_raw)
  gv_str <- regmatches(gv_raw,v_location)
  as.numeric_version(substr(gv_str,1,nchar(gv_str)-1))
}

git_version()

if(git_version() < "2.25")
  stop("git credentials only available in ")

I don't know a way to test this function with other versions of git though so the results might be unpredictable so I understand your apprehension.

MyKo101 avatar Jan 27 '21 16:01 MyKo101

No, it is much older than that:

❯ git --version
git version 2.9.2

❯ git credential
usage: git credential [fill|approve|reject]

gaborcsardi avatar Jan 27 '21 17:01 gaborcsardi

I've just had a quick look at the archive and it looks like credentials was available at least as far back as 1.8.0, if your example of 1.7.7 is correct then it must have been introduced between those.

This is what I get for git 1.7.7:

Error in new_git_error("git_error", args = args, stdout = out, status = attr(out,  :
  System git failed: git: 'credential' is not a git command. See 'git --help'.

Is this better?

Is this error just forwarding on the error from git? Would it be possible to put in a little bit that says "Try updating git" or something to that effect?

MyKo101 avatar Jan 27 '21 19:01 MyKo101

I am not sure if it is possible to determine if the problem is an old git version. git credential can fail various ways.

gaborcsardi avatar Jan 27 '21 19:01 gaborcsardi