0

I am trying to add a file using git add. Note: I am very new to git.

Using Ubuntu Linux

From a terminal prompt, this works.

/usr/bin/git add static/fb/img/gic1-5_yield_curve_5bd11b935bd72745.png

I can confirm that it works by doing git status and seeing the filename under, Changes to be committed:

I want to be able to do the same thing from an R script. This does not work:

system2(command = "/usr/bin/git", args = c("add", "static/fb/img/gic1-5_yield_curve_5bd11b935bd72745.png"))

Why is this?

ixodid
  • 2,180
  • 1
  • 19
  • 46
  • Are you certain that R is working in the same directory that you tested in the terminal shell? `getwd()` will tell you that. – r2evans Dec 23 '20 at 01:37
  • getwd() shows me I'm in an unhelpful directory. I didn't think that matters if I'm using these long, absolute path names. The command to system2 starts at root. The 1st arg is "add" and second argument has a full path name that begins at root. It's a little different that what I put in the question. – ixodid Dec 23 '20 at 01:41
  • `"static/fb/img/gic1-5_yield_curve_5bd11b935bd72745.png"` is relative, not absolute. Unless your root directory is a git repo (unlikely and strongly discouraged), then `git` will fail with something like `fatal: not a git repository`. You can either `setwd(...)` somewhere within the git repo, or add `-C /path/to/gitrepo` to the command line. – r2evans Dec 23 '20 at 02:00
  • I understand. I think I confused the issue my not posting the actual path names that look like: /home/me/website/static/fb/img/gic1-5_yield_curve_5bd11b935bd72745.png – ixodid Dec 23 '20 at 02:02
  • Okay, I understand. But ... if R is not in the git repo directory or a sub-dir of the directory, then you must either `setwd(..)` before calling `git` or add `-C ...`. This is a limitation of `git`, not R. – r2evans Dec 23 '20 at 02:03
  • R is definitely NOT in the git repo directory or a sub-dir of the directory. So before I do the system2 call I need to set my working directory to where? R(usr/bin/R) , the head of the local repo? – ixodid Dec 23 '20 at 02:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226334/discussion-between-ixodid-and-r2evans). – ixodid Dec 23 '20 at 02:10
  • 1
    If your git repo is `/home/me/website/`, then one of: (1) `setwd("/home/me/website"); system2(asbefore)`; or (2) `system2("git",c("-C","/home/me/website","add",...))`. – r2evans Dec 23 '20 at 02:10
  • 1
    setwd("/home/me/website"); system2(asbefore) This worked. Thank-you very much for your patient help r2evans. – ixodid Dec 23 '20 at 02:15

0 Answers0