使用R将本地repo推送到Windows上的github [英] Use R to push local repo to github on Windows

查看:105
本文介绍了使用R将本地repo推送到Windows上的github的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经问过一个非常类似的问题,并得到了一个从命令行,但我现在想使用R来自动化从Windows进程(Linux更容易)。



以下是我正在尝试做的事:


  1. 创建本地目录(或已经存在)

  2. 在云中生成一个与local相同的新github仓库(

  3. 添加一个.git到本地存储库

  4. 进行初始提交

  5. 在云仓库和本地仓库之间建立链接

  6. 将提交和本地仓库中的文件to github

我相信根据输出,我在失败之前一路进入第5步(因为本地目录中的提交和文件永远不会转到云中的github)。我知道第2步的工作原理是因为空的回购是在此处创建的。我不知道如何测试第5步。最后一步 shell(cmd6,intern = T) RGui和RStudio会导致永恒的死亡螺旋。问题是:如何将提交和本地回购推送到云端。

这是我的更新代码(第三个代码块中的用户名和密码):

  ##创建目录
回购< - foo5
dir.create(回购)
project.dir< - file.path(getwd(),回购)

##在目录
cat(This is a test,file = file.path(project.dir,READ.ME))中抛出一个READ.ME

## Github信息(这将改变每个用户)
密码< - pass
github.user< - trinker

##获取git位置$ (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]

##下载curl并设置github api
wincurl< - http://curl.askapache.com/download / CUR l-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我们希望格式化
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)

##更改工作目录
wd< - getwd()
setwd(project.dir)

##设置.git目录
cmd2< - paste0(shQuote(gitpath),init)
shell(cmd2,intern = T)

##添加所有
cmd3< - paste0(shQuote(gitpath),add。)
shell(cmd3,intern = T)

cmdStat< - paste0(shQuote(gitpath),status)
shell(cmdStat,intern = T)

##设置电子邮件(可能不需要)
修剪< - 函数(x)gsub(^ \\ s + | \\s + $,,x)#remove结尾/领导白色

x< - 文件如果(file.exists(x)){
y< - readLines(x)
email< - 修剪(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)

##初始提交
cmd4 < - paste0(shQuote(gitpath),'commit -m初始提交')
系统(cmd4,实习生= T)

# #建立本地和远程
cmd5 < - paste0(shQuote(gitpath),remote add origin https://github.com/,
github.user,/,repo, .git)
shell(cmd5,intern = T)

本地到远程
cmd6 < - paste0(shQuote(gitpath),push -u origin master)
shell(cmd6,intern = T)

setwd(wd)

我知道脚本稍微长一些,但重新创建问题并复制问题都是必需的:


$ b 注意我根据Simon的回答更新了这个问题,因为他是正确的,并且接近了推动。原始问题的内容可以在这里找到。




  • 环境变量<$ h2_lin>解决方案

c $ c>%HOME%已定义

  • a _netrc 文件存在于正确的凭据中推回您的回购



  • 该文件包含:

      machine github.com 
    登录用户名
    密码xxxx
    协议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

     如果不存在%HOME%@set HOME =%HOMEDRIVE% %HOMEPATH%
    @if如果不存在%HOME%@set HOME =%USERPROFILE%



    <但你也可以手动完成。


    I once asked a very similar question and got a response that worked from the command line but am now wanting to use R to automate the process from Windows (Linux is much easier).

    Here is what I'm trying to do:

    1. Create a local directory (or it exists already)
    2. Generate a new github repo in the cloud by the same name as local (based on this answer)
    3. Add a .git to the local repo
    4. Make an initial commit
    5. Establish link between cloud repo and local repo
    6. Push the commit and the files in the local repo to github

    I believe based on the output that I get all the way to step 5 before I fail (as the commit and files from the local directory never go to github in the cloud). I know step 2 works because the empty repo is created here. I don't know how to test step 5. At the last step shell(cmd6, intern = T) RGui and RStudio result in an eternal death spiral. The question is: How can I push the commit and local repo to the cloud.

    Here is my updated code (the only thing that is user specific is username and password in third code chunk):

    ## 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)
    

    I know the script is a bit longer but it's all necessary to recreate the problem and replicate the issue:

    Note I updated the question in light of Simon's response as he was correct and got closer to the push. The content of the original question can be found here.

    解决方案

    If are using https address, then make sure that:

    • the environment variable %HOME% is defined
    • a _netrc file exists in it with the right credential to push back to your repo

    That file shoud contains:

    machine github.com
    login username
    password xxxx
    protocol https
    

    That works even if you have activated the recent two-factor authentication on GitHub.

    Then your push won't time out:

    cmd6 <- paste0(shQuote(gitpath), " push -u origin master")  
    shell(cmd6, intern = T) 
    

    This is easier than setting public/private ssh keys.


    As the OP Tyler Rinker commented, setting %HOME% is illustrated in my other answer "Git - How to use .netrc file on windows to save user and password".
    This is normally done by git-cmd.bat:

    if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
    @if not exist "%HOME%" @set HOME=%USERPROFILE%
    

    But you can do it manually as well.

    这篇关于使用R将本地repo推送到Windows上的github的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆