使用R将本地存储库推送到Windows上的github

Tyl*_*ker 15 windows git r github-api

我曾经问过一个非常相似的问题并得到了一个从命令行起作用的响应,但我现在想用R来自动化Windows的过程(Linux更容易).

这是我正在尝试做的事情:

  1. 创建本地目录(或已存在)
  2. 在云中生成一个与本地同名的新github仓库(基于此答案)
  3. 将.git添加到本地存储库
  4. 进行初始提交
  5. 建立云回购和本地回购之间的联系
  6. 将提交和本地存储库中的文件推送到github

我相信基于输出,我在失败之前一直到第5步(因为提交和本地目录中的文件永远不会转到云中的github).我知道第2步有效,因为这里创建了空仓库.我不知道如何测试第5步.在最后一步,shell(cmd6, intern = T)RGui和RStudio导致永恒的死亡螺旋.问题是:如何将提交和本地存储推送到云端.

这是我更新的代码(用户特定的唯一内容是第三代码块中的用户名和密码):

## Create Directory
repo <- "foo5"
dir.create(repo)
project.dir <- file.path(getwd(), repo) 

## Throw a READ.ME in the directory
cat("This is a test", file=file.path(project.dir, "READ.ME"))

## Github info (this will change per user)
password <-"pass" 
github.user <- "trinker"  

## Get git location
test <- c(file.exists("C:/Program Files (x86)/Git/bin/git.exe"),
    file.exists("C:/Program Files/Git/bin/git.exe"))
gitpath <- c("C:/Program Files (x86)/Git/bin/git.exe",
  "C:/Program Files/Git/bin/git.exe")[test][1]

## download curl and set up github api
wincurl <- "http://curl.askapache.com/download/curl-7.32.0-win64-ssl-sspi.zip"
url <- wincurl
tmp <- tempfile( fileext = ".zip" )
download.file(url,tmp)
unzip(tmp, exdir = tempdir())       
shell(paste0(tempdir(), "/curl http://curl.haxx.se/ca/cacert.pem -o " , 
    tempdir() , "/curl-ca-bundle.crt"))
json <- paste0(" { \"name\":\"" , repo , "\" } ") #string we desire formatting
json <- shQuote(json , type = "cmd" )
cmd1 <- paste0( tempdir() ,"/curl -i -u \"" , github.user , ":" , password , 
    "\" https://api.github.com/user/repos -d " , json )

shell(cmd1, intern = T)

## Change working directory
wd <- getwd()
setwd(project.dir)

## set up the .git directory
cmd2 <- paste0(shQuote(gitpath), " init")
shell(cmd2, intern = T)

## add all the contents of the directory for tracking
cmd3 <- paste0(shQuote(gitpath), " add .")  
shell(cmd3, intern = T)       

cmdStat <- paste0(shQuote(gitpath), " status")  
shell(cmdStat, intern = T)

## Set email (may not be needed)
Trim <- function (x) gsub("^\\s+|\\s+$", "", x) #remove trailing/leading white 

x <- file.path(path.expand("~"), ".gitconfig")
if (file.exists(x)) {
    y <- readLines(x)
    email <- Trim(unlist(strsplit(y[grepl("email = ", y)], "email ="))[2])
} else {
    z <- file.path(Sys.getenv("HOME"), ".gitconfig")
    if (file.exists(z)) {
        email <- Trim(unlist(strsplit(y[grepl("email = ", y)], "email ="))[2])
    } else {
        warning(paste("Set `email` in", x))
    }
}
cmdEM <- paste0(shQuote(gitpath), sprintf(" config --global user.email %s", email))        
system(cmdEM, intern = T)

## Initial commit
cmd4 <- paste0(shQuote(gitpath), ' commit -m "Initial commit"')  
system(cmd4, intern = T) 

## establish connection between local and remote
cmd5 <- paste0(shQuote(gitpath), " remote add origin https://github.com/",
    github.user, "/", repo, ".git")  
shell(cmd5, intern = T) 

## push local to remote 
cmd6 <- paste0(shQuote(gitpath), " push -u origin master")  
shell(cmd6, intern = T) 

setwd(wd)
Run Code Online (Sandbox Code Playgroud)

我知道脚本有点长,但重新创建问题并复制问题都是必要的:

注意我根据Simon的回答更新了这个问题,因为他是正确的,并且更接近推动.原始问题的内容可以在这里找到.

Von*_*onC 8

如果使用https地址,请确保:

  • 环境变量%HOME%已定义
  • 其中_netrc存在一个文件,其中包含正确的凭据以回送到您的仓库

该文件包含:

machine github.com
login username
password xxxx
protocol https
Run Code Online (Sandbox Code Playgroud)

即使您已在GitHub上激活了最近的双因素身份验证,这仍然有效.

然后你的推动不会超时:

cmd6 <- paste0(shQuote(gitpath), " push -u origin master")  
shell(cmd6, intern = T) 
Run Code Online (Sandbox Code Playgroud)

这比设置公共/私有ssh密钥更容易.


正如OP Tyler Rinker 评论的那样,设置%HOME%在我的另一个答案" Git - 如何.netrc在Windows上使用文件来保存用户和密码 "中说明.
这通常由git-cmd.bat完成:

if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%
Run Code Online (Sandbox Code Playgroud)

但你也可以手动完成.