Kiedyś zapytałem bardzo similar question i otrzymałem odpowiedź, która działała z linii poleceń, ale teraz chcę użyć R do zautomatyzowania procesu z systemu Windows (Linux jest znacznie łatwiejszy).Użyj R, aby uruchomić lokalne repo na github na Windows
Oto, co usiłuję zrobić:
- Utwórz katalog lokalny (lub już istnieje)
- wygenerować nowy GitHub repo w chmurze o nazwie takiej samej jak miejscowy (based on this answer)
- Dodaj .git do lokalnego repo
- dokonać wstępnej popełnić
- nawiązać połączenie między chmurą lokalnego repo i repo
- Pchnąć popełnić i pliki w lokalnym repo do GitHub
wierzę based on the output że dostanę całą drogę do kroku 5, zanim nie (jak popełnić i pliki z katalogu lokalnego nigdy iść do github w Chmura). Wiem, że krok 2 działa, ponieważ puste repo jest tworzone here. Nie wiem, jak przetestować krok 5. W ostatnim kroku RGui i RStudio prowadzą do spirali śmierci wiecznej. Pytanie brzmi: Jak mogę przekazać zatwierdzenie i lokalne ponowne otwarcie do chmury.
Tu jest mój zaktualizowany kod (jedyną rzeczą, która jest specyficzna użytkownika jest nazwa użytkownika i hasło w trzecim kodu fragmencie):
## 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)
wiem, że skrypt jest nieco dłużej, ale to wszystko konieczny do odtworzenia problemu i powtórzenia problemu:
UwagaUaktualniłem pytanie w świetle odpowiedzi Simona, ponieważ był poprawny i zbliżył się do poparcia. Treść pierwotnego pytania można znaleźć pod adresem here.
Czy transakcja repo wygląda poprawnie? Może to być problem z blokowaniem plików systemu Windows i nie pozwalaniem na odejście. Być może spróbuj zmienić 'cmd3' na' paste0 (shQuote (gitpath), "add -all") ', a następnie uruchom' paste0 (shQuote (gitpath), "status") 'zaraz potem. ** AND ** możesz chcieć umieścić przełącznik, aby określić platformę i jeśli w systemie Windows użyjesz 'powłoki', a nie' systemu'. Uważam, że 'shell' jest nieco bardziej przyjazny w' Windows'. –
Z mojego doświadczenia wynika, że prawie zawsze lepiej jest używać 'system', a nie' powłoki'. – hadley