[英]Use R to push local repo to github on Windows
我曾經問過一個非常相似的問題並得到了一個從命令行起作用的響應,但我現在想用R來自動化Windows的過程(Linux更容易)。
這是我正在嘗試做的事情:
我相信基於輸出 ,我在失敗之前一直到第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)
我知道腳本有點長,但重新創建問題並復制問題都是必要的:
注意我根據Simon的回答更新了這個問題,因為他是正確的,並且更接近推動。 原始問題的內容可以在這里找到。
如果使用https地址,請確保:
%HOME%
已定義 _netrc
文件存在於其中,並具有正確的憑據以推回到您的_netrc
該文件包含:
machine github.com
login username
password xxxx
protocol https
即使您已在GitHub上激活了最近的雙因素身份驗證,這仍然有效 。
然后你的推動不會超時:
cmd6 <- paste0(shQuote(gitpath), " push -u origin master")
shell(cmd6, intern = T)
這比設置公共/私有ssh密鑰更容易。
正如OP Tyler Rinker 評論的那樣 ,設置%HOME%
在我的另一個答案“ Git - 如何在Windows上使用.netrc
文件以保存用戶和密碼 ”中說明。
這通常由git-cmd.bat完成 :
if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%
但你也可以手動完成。
問題似乎只是混淆了ssh
和https
協議。
請注意,網址應為:
# https:
"https://github.com/<username>/<myrepo>.git"
# ssh:
"git@github.com:<username>/<repo>.git"
你有:
cmd5 <- paste0(shQuote(gitpath), " remote add origin https://github.com:",
github.user, "/", repo, ".git")
cat( cmd5 )
"... remote add origin https://github.com:trinker/foo2.git"
只需將cmd5
更改為
# Note the forward slash at EOL in place of the colon
cmd5 <- paste0(shQuote(gitpath), " remote add origin https://github.com/",
github.user, "/", repo, ".git")
"... remote add origin https://github.com/trinker/foo2.git"
git add .
之后立即運行它也不會有什么壞處git add .
:
cmdStat <- paste0(shQuote(gitpath), " status")
shell(cmdStat, intern = T)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.