forked from r-lib/gert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.R
More file actions
359 lines (336 loc) · 9.96 KB
/
Copy pathcommit.R
File metadata and controls
359 lines (336 loc) · 9.96 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#' Stage and commit changes
#'
#' @description
#' To commit changes, start by *staging* the files to be included in the commit
#' using `git_add()` or `git_rm()`. Use `git_status()` to see an overview of
#' staged and unstaged changes, and finally `git_commit()` creates a new commit
#' with currently staged files.
#'
#' `git_commit_all()` is a convenience function that automatically stages and
#' commits all modified files. Note that `git_commit_all()` does **not** add
#' new, untracked files to the repository. You need to make an explicit call to
#' `git_add()` to start tracking new files.
#'
#' @export
#' @family git
#' @inheritParams git_open
#' @param message a commit message
#' @param ref revision string with a branch/tag/commit value
#' @param author A [git_signature] value, default is [git_signature_default()].
#' @param committer A [git_signature] value, default is same as `author`
#' @return
#' * `git_status()`, `git_ls()`: A data frame with one row per file
#' * `git_commit()`, `git_commit_all()`: A SHA
#' @useDynLib gert R_git_commit_create
#' @git commit index status
#' @examples
#' oldwd <- getwd()
#' repo <- file.path(tempdir(), "myrepo")
#' git_init(repo)
#' setwd(repo)
#'
#' # Set a user if no default
#' if(!user_is_configured()){
#' git_config_set("user.name", "Jerry")
#' git_config_set("user.email", "jerry@gmail.com")
#' }
#'
#' writeLines(letters[1:6], "alphabet.txt")
#' git_status()
#'
#' git_add("alphabet.txt")
#' git_status()
#'
#' git_commit("Start alphabet file")
#' git_status()
#'
#' git_ls()
#'
#' git_log()
#'
#' cat(letters[7:9], file = "alphabet.txt", sep = "\n", append = TRUE)
#' git_status()
#'
#' git_commit_all("Add more letters")
#'
#' # cleanup
#' setwd(oldwd)
#' unlink(repo, recursive = TRUE)
git_commit <- function(message, author = NULL, committer = NULL, repo = '.') {
repo <- git_open(repo)
if (!length(author)) {
author <- git_signature_default(repo = repo)
} else {
git_signature_parse(author) #validate
}
if (!length(committer)) {
committer <- author
} else {
git_signature_parse(committer) #validate
}
stopifnot(is.character(message), length(message) == 1)
status <- git_status(repo = repo)
if (!any(status$staged)) {
stop("No staged files to commit. Run git_add() to select files.")
}
merge_parents <- git_merge_parent_heads(repo = repo)
.Call(R_git_commit_create, repo, message, author, committer, merge_parents)
}
#' @export
#' @rdname git_commit
git_commit_all <- function(
message,
author = NULL,
committer = NULL,
repo = '.'
) {
repo <- git_open(repo)
unstaged <- git_status(staged = FALSE, repo = repo)
changes <- unstaged$file[
unstaged$status %in% c("modified", "renamed", "typechange")
]
if (length(changes)) {
git_add(changes, repo = repo)
}
deleted <- unstaged$file[unstaged$status == "deleted"]
if (length(deleted)) {
git_rm(deleted, repo = repo)
}
git_commit(
message = message,
author = author,
committer = committer,
repo = repo
)
}
#' View commit history
#'
#' @description
#'
#' * `git_commit_stats()` returns information about commit insertion and deletion
#' * `git_commit_info()` a list of commit info
#' * `git_commit_id()` is a shortcut for `git_commit_info()$id`
#' * `git_log()` shows the most recent commits
#' * `git_ls()` lists all the files that are being tracked in the repository.
#' * `git_stat_files()` shows information of when `files` was last modified.
#'
#' @export
#' @inheritParams git_commit
#' @family git
#' @name git_history
#' @returns
#' * `git_commit_info()` and `git_commit_stats()` return a list.
#' @useDynLib gert R_git_commit_info
#' @git commit
git_commit_info <- function(ref = "HEAD", repo = '.') {
repo <- git_open(repo)
.Call(R_git_commit_info, repo, ref)
}
#' @export
#' @rdname git_history
#' @useDynLib gert R_git_commit_id
git_commit_id <- function(ref = "HEAD", repo = '.') {
repo <- git_open(repo)
.Call(R_git_commit_id, repo, ref)
}
#' @export
#' @rdname git_history
#' @useDynLib gert R_git_commit_stats
git_commit_stats <- function(ref = "HEAD", repo = '.') {
repo <- git_open(repo)
.Call(R_git_commit_stats, repo, ref)
}
#' @export
#' @rdname git_merge
#' @param ancestor a reference to a potential ancestor commit
#' @useDynLib gert R_git_commit_descendant
git_commit_descendant_of <- function(ancestor, ref = 'HEAD', repo = '.') {
repo <- git_open(repo)
.Call(R_git_commit_descendant, repo, ref, ancestor)
}
#' @export
#' @order 1
#' @rdname git_commit
#' @name git_commit
#' @param files vector of paths relative to the git root directory.
#' Use `"."` to stage all changed files.
#' @param force add files even if in gitignore
#' @useDynLib gert R_git_repository_add
git_add <- function(files, force = FALSE, repo = '.') {
repo <- git_open(repo)
info <- git_info(repo)
normalizePath(file.path(info$path, files), mustWork = FALSE)
force <- as.logical(force)
.Call(R_git_repository_add, repo, files, force)
git_status(repo = repo)
}
#' @export
#' @order 2
#' @rdname git_commit
#' @useDynLib gert R_git_repository_rm
git_rm <- function(files, repo = '.') {
repo <- git_open(repo)
info <- git_info(repo)
normalizePath(file.path(info$path, files), mustWork = FALSE)
.Call(R_git_repository_rm, repo, files)
git_status(repo = repo)
}
#' @export
#' @rdname git_commit
#' @useDynLib gert R_git_status_list
#' @param staged return only staged (TRUE) or unstaged files (FALSE).
#' Use `NULL` or `NA` to show both (default).
#' @param pathspec character vector with paths to match
git_status <- function(staged = NULL, pathspec = NULL, repo = '.') {
repo <- git_open(repo)
staged <- as.logical(staged)
pathspec <- as.character(pathspec)
df <- .Call(R_git_status_list, repo, staged, pathspec)
df[order(df$file), , drop = FALSE]
}
#' @export
#' @rdname git_merge
#' @useDynLib gert R_git_conflict_list
git_conflicts <- function(repo = '.') {
repo <- git_open(repo)
.Call(R_git_conflict_list, repo)
}
#' @export
#' @rdname git_commit
#' @useDynLib gert R_git_repository_ls
git_ls <- function(repo = '.', ref = NULL) {
repo <- git_open(repo)
if (!length(ref) && isTRUE(git_info(repo = repo)$bare)) {
ref <- 'HEAD'
}
.Call(R_git_repository_ls, repo, ref = ref)
}
#' @export
#' @rdname git_history
#' @useDynLib gert R_git_commit_log
#' @param max lookup at most latest n parent commits
#' @param after date or timestamp: only include commits starting this date
#' @param path character vector with paths to filter on; only commits that
#' touch these paths are included
git_log <- function(
ref = "HEAD",
max = 100,
after = NULL,
path = NULL,
repo = "."
) {
repo <- git_open(repo)
ref <- as.character(ref)
max <- as.integer(max)
if (length(after)) {
after <- as.POSIXct(after)
}
path <- as.character(path)
.Call(R_git_commit_log, repo, ref, max, after, path)
}
#' @export
#' @rdname git_history
#' @useDynLib gert R_git_stat_files
git_stat_files <- function(files, ref = "HEAD", max = NULL, repo = '.') {
repo <- git_open(repo)
files <- as.character(files)
max <- as.integer(max)
.Call(R_git_stat_files, repo, files, ref, max)
}
#' Revert a commit
#'
#' Applies the inverse of the changes introduced by a given commit, equivalent
#' to `git revert <commit>`. The commit must be reachable from the current HEAD.
#'
#' By default, a new revert commit is created immediately. Set `commit = FALSE`
#' to only stage the reverted changes without committing, leaving you free to
#' amend or combine them before calling [git_commit()].
#'
#' @export
#' @name git_revert
#' @rdname git_revert
#' @family git
#' @inheritParams git_open
#' @inheritParams git_history
#' @param commit if `FALSE`, stage the reverted changes without creating a
#' commit. Default is `TRUE`, that is to say, by default a commit is made.
#' @param ... parameters passed to `git_commit` such as `message` or `author`
#' @return The SHA of the new revert commit (invisibly), or `NULL` when
#' `commit = FALSE`.
#' @examplesIf interactive()
#' repo <- file.path(tempdir(), "myrepo")
#' git_init(repo)
#'
#' # Set a user if no default
#' if (!user_is_configured()) {
#' git_config_set("user.name", "Jerry")
#' git_config_set("user.email", "jerry@gmail.com")
#' }
#'
#' writeLines("hello", file.path(repo, "hello.txt"))
#' git_add("hello.txt", repo = repo)
#' git_commit("First commit", repo = repo)
#'
#' writeLines("world", file.path(repo, "hello.txt"))
#' git_add("hello.txt", repo = repo)
#' bad_commit <- git_commit("Second commit", repo = repo)
#'
#' # Default: revert and commit with an auto-generated message
#' git_revert(bad_commit, repo = repo)
#' git_log(repo = repo)
#'
#' # Revert with a custom message
#' writeLines("oops", file.path(repo, "hello.txt"))
#' git_add("hello.txt", repo = repo)
#' bad_commit2 <- git_commit("Third commit", repo = repo)
#' git_revert(bad_commit2, message = "Undo third commit\n", repo = repo)
#' git_log(repo = repo)
#'
#' # Stage the revert without committing
#' writeLines("again", file.path(repo, "hello.txt"))
#' git_add("hello.txt", repo = repo)
#' bad_commit3 <- git_commit("Fourth commit", repo = repo)
#' git_revert(bad_commit3, commit = FALSE, repo = repo)
#' git_status(repo = repo)
#'
#' unlink(repo, recursive = TRUE)
#' @useDynLib gert R_git_revert
#' @git revert
git_revert <- function(
ref,
commit = TRUE,
...,
repo = '.'
) {
repo <- git_open(repo)
assert_string(ref)
stopifnot(is.logical(commit), length(commit) == 1)
sha <- check_ref_in_history(ref, repo)
.Call(R_git_revert, repo, sha)
if (isTRUE(commit)) {
git_revert_commit(repo = repo, sha = sha, ...)
}
}
git_revert_commit <- function(
sha,
message = revert_message(sha, repo),
...,
repo
) {
git_commit(message, ..., repo = repo)
}
assert_string <- function(x) {
if (!is.character(x) || !length(x)) {
stop("Argument must be a string of length 1")
}
}
revert_message <- function(sha, repo) {
info <- git_commit_info(sha, repo = repo)
subject <- strsplit(info$message, "\n")[[1]][1]
subject <- sub("\\s+$", "", subject)
sprintf(
'Revert "%s"\n\nThis reverts commit %s.\n',
subject,
info$id
)
}