trinker / reports

An R package to assist in the workflow of writing academic articles and other reports

Home Page:http://cran.us.r-project.org/web/packages/reports/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

repo2github broken

trinker opened this issue · comments

git add . on windows appears not to work anymore. I may give up on this as it seems to be finicky (don't know of this is an API change or what).

Problem occurs on Linux too. Maybe something with git add .??? Doesn't seem to be adding anything.

The problem occurs as seen below:

repo2github2()
repo2github2 <- 
function(password, project.dir = getwd(), 
        repo = basename(getwd()), github.user = getOption("github.user"), 
        gitpath = NULL, readme = TRUE) {

    #check for github user name
    if (is.null(github.user)) { 
        message("Enter [GitHub username] and press [Enter] to continue")
        github.user <- readLines(n=1)
    }   

    #check for password
    if (missing(password)) {    
        message("Enter [GitHub password] and press [Enter] to continue")
        password <- readLines(n=1)
    }

    OSiswin <- Sys.info()["sysname"] != "Windows"

    ## .gitignore content
    GI <- paste("# History files\n.Rhistory\n\n# Example code in package build",
        sprintf("process\n*-Ex.R\n\n.Rprofile\n.Rproj.user\n%s.Rproj\n", repo))

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

        ## Make .gitignore
        cat(GI, file=file.path(project.dir, ".gitignore"))
        cat(sprintf("%s\n===", repo), file=file.path(project.dir, "README.md"))    

    } 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.exe",
                "C:/Program Files/Git/bin/git.exe")[test][1]
        }
        url <- "http://curl.askapache.com/download/curl-7.23.1-win64-ssl-sspi.zip"
        tmp <- tempfile( fileext = ".zip" )
        download.file(url,tmp)
        unzip(tmp, exdir = tempdir())       
        system(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 )

        ## Make .gitignore and README
        cat(GI, file=file.path(project.dir, ".gitignore"))      
        cat(sprintf("%s\n===", repo), file=file.path(project.dir, "README.md"))           

    }
    system(cmd1)  

    ## push the directory to github
    if (is.null(project.dir)) stop("\"project.dir\" must be supplied")


    if (!OSiswin) {    

        wd <- getwd()
        setwd(project.dir)
        cmd2 <- paste0(shQuote(gitpath), " init")
        system(cmd2, intern = T)
        cmd3 <- paste0(shQuote(gitpath), " add .")  
        system(cmd3, intern = T)     
browser()   
cmdStat <- paste0(shQuote(gitpath), " status")  
shell(cmdStat, intern = T)        
        ## Set email
        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)

        ## Initial commit
        cmd4 <- paste0(shQuote(gitpath), ' commit -m "Initial commit"')  
        system(cmd4, intern = T) 

        ## add a new remote
        cmd5 <- paste0(shQuote(gitpath), " remote add origin https://github.com/",
            github.user, "/", repo, ".git")  
        system(cmd5, intern = T) 

        #Make a temp _netrc file
        home <- Sys.getenv()["HOME"]
        newhome <- file.path(home, paste0("DELETE_ME_", gsub(":", "\\.", UF(Sys.time()))))
        dir.create(newhome)
        loc <- file.path(newhome, "_netrc")
        on.exit(Sys.setenv(HOME = home))
        Sys.setenv(HOME = newhome)
        netrc <- sprintf("machine github.com\nlogin %s\npassword %s\nprotocol https", 
            github.user, password)
        cat(netrc, file=loc)

        ## Push the repo to github
        cmd6 <- paste0(shQuote(gitpath), " push -u origin master")  
        system(cmd6, intern = T) 

        setwd(wd)

        ## Delete the _netrc file
        unlink(loc, recursive = TRUE, force = FALSE)
        unlink(newhome, recursive = TRUE, force = FALSE)

        if (file.exists(loc)) {
            warn <- paste("For Windows users this function creates a temporary", 
                "_netrc\nfile in the temp directory and attempts to delete this", 
                "file.\nThe _netrc contains username and password information for", 
                "github.\n\nThis file was created:\n", loc, "\n\nThe results of",
                "file.exists(loc) is:", file.exists(loc), "\n\nIf TRUE delete file",
                "manually.\n\nThe file can be found via:\n", loc)
            warning(warn)
        }
    } else {
        system( paste0( "cd ", project.dir , " && " , gitpath , " init" ) )
        system( paste0( "cd ", project.dir , " && " , gitpath , " add \\." ) )
        system( paste0( "cd ", project.dir , " && " , gitpath , 
            " commit -m \"Initial commit\"" ) )
        system( paste0( "cd ", project.dir , " && " , gitpath, 
            " remote add origin https://github.com/", github.user, "/", repo, ".git") ) 
    }
    message(sprintf("%s pushed to github", repo))
}

Link to what it should look like (at least the first part): https://dl.dropboxusercontent.com/u/61803503/Errors/github_push.txt

What I'm supposed to use:

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/trinker/new_guy.git
git push -u origin master


## Push an existing repository from the command line

git remote add origin https://github.com/trinker/new_guy.git
git push -u origin master

Oddly enough I just tried this and it worked ????

I'm cautiously closing this as it appears to have worked twice. Not sure what's going on. I'm on a slower Internet connection at the momment????