Adding Git Remote Repo to Network Drive

remote_path <- normalizePath(
  path = file.path(Sys.getenv()[["OneDrive"]], "Desktop\\test_repo"),
  # path = file.path(Sys.getenv()[["OneDrive"]], "Desktop\\test_repo"),
  winslash = "/",
  mustWork = FALSE
)

local_path <- normalizePath(
  path = file.path(Sys.getenv()[["USERPROFILE"]], "Software\\Git Local\\test_repo"),
  # path = file.path(Sys.getenv()[["USERPROFILE"]], "Software\\Git Local\\test_repo"),
  winslash = "/",
  mustWork = FALSE
)

files_to_copy_path <- list.files("    ", full.names = TRUE)

if (!dir.exists(remote_path)) {
  dir.create(remote_path)
  cli::cli_alert_success(paste0(remote_path, " created!"))
} else {
  cli::cli_alert_info(paste0(remote_path, " already exists!"))
}

remote_terminal_id <- rstudioapi::terminalExecute(
  command = "git init --bare",
  workingDir = remote_path
)

while (is.null(rstudioapi::terminalExitCode(remote_terminal_id))) {
  Sys.sleep(0.1)
}

if (!dir.exists(local_path)) {
  dir.create(local_path)
  cli::cli_alert_success(paste0(local_path, " created!"))
} else {
  cli::cli_alert_info(paste0(local_path, " already exists!"))
}

local_terminal_id_init <- rstudioapi::terminalExecute(
  command = "git init",
  workingDir = local_path
)

while (is.null(rstudioapi::terminalExitCode(local_terminal_id_init))) {
  Sys.sleep(0.1)
}

rstudioapi::initializeProject(local_path)
purrr::walk(rstudioapi::terminalList(), rstudioapi::terminalKill)

file.copy(
  from = files_to_copy_path,
  to = local_path
)
git_all_commit_push <- paste(
  paste0("cd '", local_path, "'"),
  "git add .",
  'git commit -m "Initial commit"',
  paste0("git remote add origin '", remote_path, "'"),
  "git push origin master",
  "",
  sep = "\n"
)

new_term <- rstudioapi::terminalCreate()

local_terminal_id <- purrr::map(
  .x = git_all_commit_push,
  .f = ~ {
    rstudioapi::terminalSend(new_term, text = paste0(.x))
  }
)

purrr::walk(
  rstudioapi::terminalList(),
  rstudioapi::terminalKill
)

rstudioapi::openProject(
  path = local_path,
  newSession = TRUE
)
git_ignore_commit_push <- paste(
  paste0("cd '", local_path, "'"),
  "git add .",
  'git commit -m ".gitignore commit"',
  "git push --set-upstream origin master",
  "",
  sep = "\n"
)

new_term2 <- rstudioapi::terminalCreate()

gitignore_terminal_id <- purrr::map(
  .x = git_ignore_commit_push,
  .f = ~ {
    
    
    rstudioapi::terminalSend(
      id = new_term2,
      text = paste0(.x)
    )
  }
)

purrr::walk(
  rstudioapi::terminalList(),
  rstudioapi::terminalKill
)