3

I'm trying to use git from within R to send command to github via curl (i.e., I'm trying to make this question work as an R function). I can accomplish this if I can find the location of git.exe. I thought I could use Sys.which but it doesn't work.

> Sys.which("git")
git 
 "" 

But I know the file is there when I do:

> system('"C:\\Program Files (x86)\\Git\\bin\\git" --version')
git version 1.7.11.msysgit.1

How can I find git.exe on Windows? (or perhaps I'm going about the entire problem wrong as happened to me here)

Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 1
    `which` is a *nix command. `Sys.which` on Windows is only going to look in the environment path so it's not going to magically find files. Are you expecting to execute this on machines where git is installed in different places? – Andrew Mao Feb 21 '13 at 05:41
  • Maybe have a look at what hadley does in `devtools`: https://github.com/hadley/devtools/blob/master/R/install-git.r . Check the `git_path` function at the bottom. – Marius Feb 21 '13 at 05:42
  • @Andrew Mao chances are it would be in `Program Files` or `Programs Files (x86)` but it can't be certain the user would have places the files there. Maybe an `if` `else` that searchers those two places first and then if not found searches else where but I don't know how to find it if it's not in one of those two places. – Tyler Rinker Feb 21 '13 at 05:45
  • @Marius the `git_path()` function returns `Error: Git does not seem to be installed on your system.` – Tyler Rinker Feb 21 '13 at 05:48
  • @TylerRinker: Yeah, having a closer look, it doesn't seem to do much more than checking for `"git.exe"` rather than `"git"` on Windows, I just knew `devtools` had some git-related functions and figured hadley would have a clever solution. – Marius Feb 21 '13 at 05:51
  • Why not just run your git commands via git bash? –  Feb 21 '13 at 05:54
  • @Jack Maney the idea is to be more efficient. By the time I enter [the commands](http://stackoverflow.com/questions/14912161/repo-from-rstudio-to-github) into git bash I could have went to the net created the repo and dumped the necessary files there. The idea is you have an existing local repo and you can easily upload to GitHub. – Tyler Rinker Feb 21 '13 at 06:07
  • @TylerRinker you can run a batch using `shell` which list installed programs in windows... – agstudy Feb 21 '13 at 06:48
  • @agstudy can you give a demo as an answer? – Tyler Rinker Feb 21 '13 at 06:53
  • 4
    If you always want to find it, put the `bin` directory path in `%PATH%` (maybe in the startup link). That is even portable, since you can actually use `Sys.which` then. You could also hunt for the registry entries of the most common installations (Github fW, msysgit, Git for Windows). – filmor Feb 21 '13 at 06:58
  • 3
    Generally, it's the users responsibility to correctly set the path - if you are trying to do this by hand, you are entering a world of hurt. Sometimes that is worth doing, but it is really hard. For example, read the almost 260 lines of code devtools uses to find Rtools on windows: https://github.com/hadley/devtools/blob/master/R/rtools.r – hadley Feb 21 '13 at 15:06
  • 1
    @Hadley I think your advice is good. Everyone has alluded to this but you came out and said it :-) – Tyler Rinker Feb 21 '13 at 15:32

3 Answers3

5

You can configure list.files() to do a recursive search through the most likely folders:

list.files(
  path=c("c:/program files", "c:/program files (x86)"), 
  pattern="git.exe", 
  full.names=TRUE,
  recursive=TRUE
)

On my machine this results in:

[1] "c:/program files (x86)/Git/bin/git.exe"             
[2] "c:/program files (x86)/Git/cmd/git.exe"             
[3] "c:/program files (x86)/Git/libexec/git-core/git.exe"
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 1
    What if git is stored `"c:/Git/bin/git.exe"` – Tyler Rinker Feb 21 '13 at 06:52
  • 1
    @TylerRinker, the key phrase here is *most likely folders*, and I suspect that would be approach you would have to take unless you want to suffer performance hits of searching all drives and directories. For Windows (I think XP and above) you can also consider a system call to something like `wmic product get /format:csv` with `intern = TRUE`, do a `read.csv` on that, and find Git with `grep` and friends. But that'll probably be slower than checking the likely places and integrating a user prompt if Git's not found (like I [demonstrated yesterday](http://stackoverflow.com/a/14971683/1270695)) – A5C1D2H2I1M1N2O1R2T1 Feb 21 '13 at 08:53
1

You can type in the command line git --exec-path and this will give you the path to your git.exe

Eyal Gerber
  • 1,026
  • 10
  • 27
0

Hey not sure if anyone will need this now that it's been a while. I stumbledupon this from a Google search for a similar pain-point.

However, I found the path. It's a little tricky. I guess I installed this new GitHub software package and it put the exe in a different path than I expected.

C:\Users\your_user_name_here\AppData\Local\GitHub\PortableGit_c7e0cbde92ba565cb218a521411d0e854079a28c\cmd\git.exe

  • The only cavet here is I added the git.exe, because it will just show an image and cmd, so just know that it's an application and just add the .exe next to the git in your path and click test in PHP Storm or whatever IDE you're using.

Hope this helps someone!

Chad Buie
  • 13
  • 6