【问题标题】:How can I commit changes to GitHub from within a R script?如何从 R 脚本中将更改提交到 GitHub?
【发布时间】:2019-08-21 06:42:30
【问题描述】:

我正在尝试从 R 脚本中自动化一些基本的 git 操作。我在 Windows 操作系统上使用 Rstudio。例如,如果您希望在脚本完成某些自动化任务时更新 GitHub,这可能会有所帮助。

我编写了一些简单的函数,利用 R 的 shell() 函数和 Window 的 & 管道运算符向操作系统终端发送命令链:

# Git status.
gitstatus <- function(dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = "git status"
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

# Git add.
gitadd <- function(dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = "git add --all"
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

# Git commit.
gitcommit <- function(msg = "commit from Rstudio", dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = paste0("git commit -am ","'",msg,"'")
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

# Git push.
gitpush <- function(dir = getwd()){
  cmd_list <- list(
    cmd1 = tolower(substr(dir,1,2)),
    cmd2 = paste("cd",dir),
    cmd3 = "git push"
  )
  cmd <- paste(unlist(cmd_list),collapse = " & ")
  shell(cmd)
}

我的 gitstatusgitaddgitpush 函数有效。 gitcommit 函数不起作用。它会产生以下错误:

致命:带 -a 的路径没有意义。
警告信息:
在 shell(cmd) 中:'d: & cd D:/Documents/R/my_path & git commit -am 'commit from Rstudio'' 执行失败,错误代码为 128

gitpush 函数之所以有效,是因为如果您切换到终端或 Rstudio 中的 git,您可以提交更改,然后成功调用 gitpush

关于如何解决此问题的任何想法?

...

注意: 我已经安装了 Git bash,并且可以从 Windows 命令终端和 Rstudio 成功使用 git。我还尝试了另一种策略,即让 R 编写一个临时的 .bat 文件,然后执行它,但是这个策略也会在提交步骤中挂起。

【问题讨论】:

  • CRAN (!!) 上的几个包直接与 git API 接口,因此您不需要 shell -- git2r 就是其中之一。如果命令行程序git 可用,我的drat 包可以使用它或system() 命令。您可以检查(相当简单的)函数的来源at the git source repo of drat

标签: r git shell github rstudio


【解决方案1】:

解决方案

答案在Dirk Eddelbuettel's drat 包函数addrepo 中。还需要使用git2r'sconfig 函数来确保 git 识别 R。git2r 的函数可能会为将来从 R 脚本中使用 git 提供更强大的解决方案。同时,这是我解决问题的方法。

  • 安装 git2r。使用 git2r::config() 确保 git 识别 R。

  • 根据 Dirk 的代码,我修改了 gitcommit() 函数以利用 sprintf()system() 执行系统命令:

# Git commit.
gitcommit <- function(msg = "commit from Rstudio", dir = getwd()){
  cmd = sprintf("git commit -m\"%s\"",msg)
  system(cmd)
}

Sprintf 的输出如下所示:

[1] "git commit -m\"commit from Rstudio\""

示例

#install.packages("git2r")
library(git2r)

# Insure you have navigated to a directory with a git repo.
dir <- "mypath"
setwd(dir)

# Configure git.
git2r::config(user.name = "myusername",user.email = "myemail")

# Check git status.
gitstatus()

# Download a file.
url <- "https://i.kym-cdn.com/entries/icons/original/000/002/232/bullet_cat.jpg"
destfile <- "bullet_cat.jpg"
download.file(url,destfile)

# Add and commit changes. 
gitadd()
gitcommit()

# Push changes to github.
gitpush()

嗯,这张照片看起来很奇怪,但我想你明白了。

【讨论】:

    【解决方案2】:

    根据我在this Ask Ubuntu question 中阅读的内容,您应该使用&amp;&amp;,而不是&amp;,来分隔Git bash 中的多个命令。尝试这样做:

    gitcommit <- function(msg = "commit from Rstudio", dir = getwd()) {
        cmd_list <- list(
            cmd1 = tolower(substr(dir, 1, 2)),
            cmd2 = paste("cd", dir),
            cmd3 = paste0("git commit -am ", "'", msg, "'")
        )
        cmd <- paste(unlist(cmd_list),collapse = " && ")
        shell(cmd)
    }
    

    请注意,您的 gitcommit 函数将输出如下内容:

    /v & cd /var/www/service/usercode/255741827 & git commit -am 'first commit'"
    

    我不知道substr(dir, 1, 2) 部分的用途是什么,但如果我的建议仍然不起作用,请尝试将其删除,只留下cdgit commit 命令。

    【讨论】:

    • 感谢您的尝试。我试过 && ,这似乎并没有解决问题。 substr 行是为了确保 Windows 正确更改目录。例如,如果您在 D: 驱动器中,并且需要更改为 C: 驱动器,则需要在终端中输入 c:。键入 cd C:\my_path 不会带您到任何地方。你需要先用 d: 切换到 D: 盘。
    • @twb10 如果此答案解决了您的问题,请考虑通过单击左侧的绿色复选标记接受它。
    【解决方案3】:

    我个人喜欢 rOpenSci 的 gert 包的语法和简单性。具体来说,要提交一个 repo,你会这样做:

    git_add("test.txt")
    git_commit("Adding a file", author = "jerry <jerry@gmail.com>")
    

    顶推到远程:

    git_push(remote = "origin", repo = ".")
    

    并通过这种简单的语法查看所有其他有用的功能。

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 1970-01-01
      • 2011-08-20
      • 2018-02-09
      • 2020-12-04
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多