-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_empty_branch.R
148 lines (129 loc) · 3.88 KB
/
create_empty_branch.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# create_empty_branch_ghpages --------------------------------------------------
#' Create Empty gh-pages branch
#'
#' @inheritParams create_empty_branch
#' @return create and push an empty gh-pages branch to org/repo
#' @export
create_empty_branch_ghpages <- function(
repo = NULL,
org = "KWB-R",
set_githubuser = TRUE,
git_exe = path_to_git(),
dest_dir = tempdir(),
execute = TRUE,
dbg = TRUE,
...
)
{
create_empty_branch(
repo = repo,
branch = "gh-pages",
org = org,
set_githubuser = set_githubuser,
git_exe = git_exe,
dest_dir = dest_dir,
execute = execute,
dbg = dbg,
...
)
}
# create_empty_branch ----------------------------------------------------------
#' Create Empty Branch From Github
#'
#' @param repo name of existing Github repository (default: NULL)
#' @param branch name of branch to be created (default: NULL)
#' @param org repo owner (default: "KWB-R")
#' @param set_githubuser should the Github user be set before executing the
#' branch creation (default: TRUE). In this case set_github_user() is run
#' @param git_exe path to GIT executable (default:
#' kwb.pkgbuild:::path_to_git()), only required on "windows". On all other
#' platforms it is assumed that "git" is sufficient!
#' @param dest_dir directory where the the repo should be checked out (default:
#' tempdir())
#' @param execute should a batch file be run?
#' @param dbg print debug messages (default: TRUE)
#' @param ... additional arguments passed to set_github_user
#' @return create empty branch (defined in 'branch') and push to org/repo on
#' Github
#' @export
#' @importFrom fs dir_create
create_empty_branch <- function(
repo = NULL,
branch = NULL,
org = "KWB-R",
set_githubuser = TRUE,
git_exe = path_to_git(),
dest_dir = tempdir(),
execute = TRUE,
dbg = TRUE,
...
)
{
if (is.null(repo)) clean_stop(
"Specify the name of the repo to be checked out with parameter 'repo'"
)
if (is.null(branch)) clean_stop(
"Specify the name of the branch to be created in parameter 'branch'"
)
git_exe <- git_check_if_windows(git_exe)
fs::dir_create(dest_dir)
cmd <- c(
sprintf('cd "%s"', dest_dir),
sprintf('"%s" clone https://github.com/%s/%s', git_exe, org, repo),
sprintf('cd "%s"', repo),
sprintf('"%s" checkout --orphan %s', git_exe, branch),
sprintf('"%s" rm -rf .', git_exe),
sprintf('"%s" commit --allow-empty -m "Initial gh-pages commit"', git_exe),
sprintf('"%s" push origin %s', git_exe, branch),
sprintf('"%s" checkout main', git_exe)
)
if (set_githubuser) cmd <- c(
set_github_user(git_exe = git_exe, dbg = dbg, ...), cmd
)
file <- file.path(dest_dir, sprintf("create_empty_%s_branch.bat", branch))
kwb.utils::catAndRun(
dbg = dbg,
messageText = paste("Writing batch file:", file),
expr = writeLines(cmd, file)
)
if (execute) kwb.utils::catAndRun(
dbg = dbg,
messageText = paste("Running batch file:", file),
expr = system(file)
)
git_exe <- git_check_if_windows(git_exe)
fs::dir_create(dest_dir)
cmd_checkout <- c(
sprintf('cd "%s"', dest_dir),
sprintf('"%s" clone https://github.com/%s/%s', git_exe, org, repo),
sprintf('"%s" checkout -b %s', git_exe, branch)
)
write_git_batch_and_execute(
cmd_checkout,
set_githubuser,
bat_name = sprintf("add_%s_branch.bat", branch),
dest_dir,
git_exe,
execute,
dbg,
...
)
checkout_path <- file.path(dest_dir, repo)
use_gitlab_ci_ghpages(dest_dir = checkout_path)
cmd_push <- c(
sprintf('cd "%s"', checkout_path),
sprintf('"%s" add %s', git_exe, ".gitlab-ci.yml"),
sprintf('"%s" commit -m "Add backup deploy config for GitLab"', git_exe),
sprintf('"%s" push origin %s', git_exe, branch)
)
write_git_batch_and_execute(
cmd_push,
set_githubuser,
bat_name = sprintf("add_%s_branch.bat", branch),
dest_dir,
git_exe,
execute,
dbg,
...
)
}