我曾经问过一个非常
similar question,并得到了从命令行工作的响应,但现在我想使用R来自Windows的进程自动化(Linux更容易).
这是我要做的:
>创建本地目录(或已存在)
>在云中生成与本地(based on this answer)相同名称的新github回购
>添加一个.git到本地的repo
>进行初始提交
>建立云回购与本地回购之间的联系
将commit和本地备份中的文件推送到github
我相信based on the output,在我失败之前,我得到了所有的方法到第5步(因为本地目录中的提交和文件不会在云中的github).我知道步骤2的工作原因是空的repo被创建here.我不知道如何测试步骤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 = ","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/","/",".git") shell(cmd5,intern = T) ## push local to remote cmd6 <- paste0(shQuote(gitpath)," push -u origin master") shell(cmd6,intern = T) setwd(wd)
我知道脚本有点长,但是重新创建问题和复制问题都是必要的:
如果使用https地址,请确保:
>定义环境变量%HOME%
>一个_netrc文件存在于其中,具有正确的凭据以推回到您的回购
该文件包含:
machine github.com login username password xxxx protocol https
即使你有activated the recent two-factor authentication on GitHub也是如此.
那么你的推动不会超时:
cmd6 <- paste0(shQuote(gitpath),intern = T)
这比setting public/private ssh keys更容易.
作为OP Tyler Rinker commented,设置%HOME%在我的其他答案“Git – How to use .netrc
file on windows to save user and password”中显示.
这通常在git-cmd.bat之前完成:
if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH% @if not exist "%HOME%" @set HOME=%USERPROFILE%
但是您也可以手动进行操作.