使用git和curl命令行

Tyl*_*ker 6 windows git curl r

我正在尝试编写一个函数来将项目推送到github而不首先在云中创建项目.目前,您可以使用此问题中的信息从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]
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试用system:

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

> system(paste(gitpath, "--version"))
git version 1.7.11.msysgit.1
Run Code Online (Sandbox Code Playgroud)

看起来不错.但后来我尝试了一个真正的代码块:

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

system(cmd1)
Run Code Online (Sandbox Code Playgroud)

并得到消息:

> 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 
Run Code Online (Sandbox Code Playgroud)

我该如何运行此命令:

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

我也试过跑,而不先把git放在前面.我目前正在使用win 7机器

Sim*_*lon 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\"}\'")
Run Code Online (Sandbox Code Playgroud)

请记住转义字符串中多余的引号。

我想您甚至可以下载已编译的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\"}\'") )
Run Code Online (Sandbox Code Playgroud)