2013-02-22 189 views
6

我正在嘗試編寫一個函數來將項目推送到github,而無需先在雲中創建項目。目前,您可以使用來自this question的信息從RStudio中的git命令行執行此操作。使用git和curl命令行

現在我試圖將它包裝到一個函數中,我可以使用system在本地回購庫中創建雲中的回購。我正在通過Windows和Linux機器上的這個工作(所以不知道它在mac上的效果如何)。這裏是我的代碼迄今(檢測git的位置):

gitpath <- NULL 
    repo <- "New" 
    user <- "CantPostThat" 
    password <- "blargcats" 


if (Sys.info()["sysname"] != "Windows") { 
    gitpath <- "git" 
} else { 
    if (is.null(gitpath)){ 
     test <- c(file.exists("C:\\Program Files (x86)\\Git\\bin\\git.exe"), 
      file.exists("C:\\Program Files\\Git\\bin\\git.exe")) 
     if (sum(test) == 0) { 
      stop("Git not found. Supply path to 'gitpath'")  
     } 
     gitpath <- c("\"C:\\Program Files (x86)\\Git\\bin\\git\"", 
      "\"C:\\Program Files\\Git\\bin\\git\"")[test][1] 
    } 
} 

然後,我system嘗試:

system(paste(gitpath, "--version")) 

> system(paste(gitpath, "--version")) 
git version 1.7.11.msysgit.1 

看起來不錯。但後來我嘗試在一個真正的代碼塊:

cmd1 <- paste(gitpath, paste0("curl -u '", user, ":", password, 
    "' https://api.github.com/user/repos -d '{\"name\":\"", repo, "\"}'")) 

system(cmd1) 

,看到消息:

> system(cmd1) 
git: 'curl' is not a git command. See 'git --help'. 

Did you mean this? 
    pull 
Warning message: 
running command '"C:\Program Files (x86)\Git\bin\git" curl -u ' trinker : PASSWORD ' https://api.github.com/user/repos -d '{"name":" three "}'' had status 1 

我怎樣才能運行此命令:

curl -u 'USER:PASS' https://api.github.com/user/repos -d '{"name":"REPO"}' 從控制檯。

我也試過沒有把git先放在前面。我目前在贏7機

+2

你有捲曲安裝? – hadley 2013-02-23 00:53:00

+0

我想我可以運行RCurl。還沒有嘗試過在Linux上。我想這個小故事會在windows上找到它。 – 2013-02-23 02:17:06

+0

@hadley我可以用git命令行來完成,所以必須安裝curl(儘管我找不到它)。 – 2013-02-23 02:53:03

回答

2

在我看來,你似乎試圖運行curl作爲git命令system("git curl"),這顯然不會工作。我想你需要在Windows上找到curl二進制文件的安裝路徑,其方式類似於上面對Git可執行文件所做的操作。在Mac OS X中,可以像這樣運行命令

system("curl -u \'USER:PASS\' https://api.github.com/user/repos -d \'{\"name\":\"REPO\"}\'") 

記住逃脫字符串中的多餘的引號。

我想你甚至可以下載編譯後的curl二進制文件並從下載位置運行它?我沒有訪問我的Win7盒子在工作測試這個運行從複製和粘貼,但你的想法...

url <- "http://curl.askapache.com/download/curl-7.23.1-win64-ssl-sspi.zip" 
tmp <- tempfile(fileext = ".zip") 
download.file(url,tmp) 
unzip(tmp) 
system(paste0(tempdir(),"/curl", " -u \'USER:PASS\' https://api.github.com/user/repos -d \'{\"name\":\"REPO\"}\'")) 
+0

這回答了這個問題,但我仍然無法使它工作。我標記爲正確的,[問一個更合適的問題 - - ](http://stackoverflow.com/questions/15042418/send-to-github-via-curl-command-line)(我甚至沒有在這個主題中提出正確的問題)。 – 2013-02-23 15:55:04