File: status.R

package info (click to toggle)
r-cran-git2r 0.35.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,956 kB
  • sloc: ansic: 8,203; sh: 4,104; makefile: 7
file content (134 lines) | stat: -rw-r--r-- 5,167 bytes parent folder | download
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
## git2r, R bindings to the libgit2 library.
## Copyright (C) 2013-2023 The git2r contributors
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License, version 2,
## as published by the Free Software Foundation.
##
## git2r is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

library(git2r)
source("util/check.R")

## For debugging
sessionInfo()
libgit2_version()
libgit2_features()


## Create a directory in tempdir
path <- tempfile(pattern = "git2r-")
dir.create(path)

## Initialize a repository
repo <- init(path)
config(repo, user.name = "Alice", user.email = "alice@example.org")

## Status case 1
status_exp_1 <- structure(list(staged = empty_named_list(),
                               unstaged = empty_named_list(),
                               untracked = empty_named_list()),
                          class = "git_status")
status_obs_1 <- status(repo)
stopifnot(identical(print(status_obs_1), status_obs_1))
str(status_exp_1)
str(status_obs_1)
stopifnot(identical(status_obs_1, status_exp_1))
stopifnot(identical(capture.output(status(repo)),
                    "working directory clean"))

## Status case 2, include ignored files
status_exp_2 <- structure(list(staged = empty_named_list(),
                               unstaged = empty_named_list(),
                               untracked = empty_named_list(),
                               ignored = empty_named_list()),
                          class = "git_status")
status_obs_2 <- status(repo, ignored = TRUE)
status_obs_2
str(status_exp_2)
str(status_obs_2)
stopifnot(identical(status_obs_2, status_exp_2))
stopifnot(identical(capture.output(status(repo, ignored = TRUE)),
                    "working directory clean"))

## Create 4 files
writeLines("File-1", file.path(path, "test-1.txt"))
writeLines("File-2", file.path(path, "test-2.txt"))
writeLines("File-3", file.path(path, "test-3.txt"))
writeLines("File-4", file.path(path, "test-4.txt"))

## Status case 3: 4 untracked files
status_exp_3 <- structure(list(staged = empty_named_list(),
                               unstaged = empty_named_list(),
                               untracked = list(untracked = "test-1.txt",
                                                untracked = "test-2.txt",
                                                untracked = "test-3.txt",
                                                untracked = "test-4.txt")),
                          class = "git_status")
status_obs_3 <- status(repo)
status_obs_3
str(status_exp_3)
str(status_obs_3)
stopifnot(identical(status_obs_3, status_exp_3))

## Add file 1 and 2 to the repository and commit
add(repo, c("test-1.txt", "test-2.txt"))
commit(repo, "Commit message")

## Status case 4: 2 untracked files
status_exp_4 <- structure(list(staged = empty_named_list(),
                               unstaged = empty_named_list(),
                               untracked = list(untracked = "test-3.txt",
                                                untracked = "test-4.txt")),
                          class = "git_status")
status_obs_4 <- status(repo)
status_obs_4
str(status_exp_4)
str(status_obs_4)
stopifnot(identical(status_obs_4, status_exp_4))

## Update file 1 & 2
writeLines(c("File-1", "Hello world"), file.path(path, "test-1.txt"))
writeLines(c("File-2", "Hello world"), file.path(path, "test-2.txt"))

## Add file 1
add(repo, "test-1.txt")

## Status case 5: 1 staged file, 1 unstaged file and 2 untracked files
status_exp_5 <- structure(list(staged = list(modified = "test-1.txt"),
                               unstaged = list(modified = "test-2.txt"),
                               untracked = list(untracked = "test-3.txt",
                                                untracked = "test-4.txt")),
                          class = "git_status")
status_obs_5 <- status(repo)
status_obs_5
str(status_exp_5)
str(status_obs_5)
stopifnot(identical(status_obs_5, status_exp_5))

## Add .gitignore file with file test-4.txt
writeLines("test-4.txt", file.path(path, ".gitignore"))

## Status case 6: 1 staged file, 1 unstaged file, 2 untracked files
## and 1 ignored file
status_exp_6 <- structure(list(staged = list(modified = "test-1.txt"),
                               unstaged = list(modified = "test-2.txt"),
                               untracked = list(untracked = ".gitignore",
                                                untracked = "test-3.txt"),
                               ignored = list(ignored = "test-4.txt")),
                          class = "git_status")
status_obs_6 <- status(repo, ignore = TRUE)
status_obs_6
str(status_exp_6)
str(status_obs_6)
stopifnot(identical(status_obs_6, status_exp_6))

## Cleanup
unlink(path, recursive = TRUE)