使用git和curl命令行

时间:2013-02-22 22:44:38

标签: windows r git curl

我正在尝试编写一个函数来将项目推送到github而不首先在云中创建项目。目前,您可以使用this question中的信息从RStudio中的git命令行执行此操作。

现在我正在尝试将其包装成一个函数,我可以使用system从本地仓库创建云中的repo。我正在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放在前面。我目前正在使用win 7机器

1 个答案:

答案 0 :(得分: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\"}\'") )
相关问题