-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathassert.git.bash
More file actions
166 lines (136 loc) · 4.19 KB
/
Copy pathassert.git.bash
File metadata and controls
166 lines (136 loc) · 4.19 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
#!/usr/bin/env bash
##
# @file
# Assertions for git repositories.
#
##
# Asserts that a directory is a git repository.
#
# Arguments:
# 1. dir: Directory to check. Optional, defaults to the current directory.
##
assert_git_repo() {
local dir="${1:-$(pwd)}"
assert_dir_exists "${dir}" || return 1
if [ -d "${dir}/.git" ]; then
local message
message="$(git --work-tree="${dir}" --git-dir="${dir}/.git" status 2>&1)"
if string_match "${message}" "not a git repository" "literal" 0 "anywhere"; then
format_error "Directory is not a git repository" "directory" "${dir}" | flunk
return 1
fi
return 0
else
format_error "Directory is not a git repository" "directory" "${dir}" | flunk
return 1
fi
}
##
# Asserts that a directory is not a git repository.
#
# Arguments:
# 1. dir: Directory to check. Optional, defaults to the current directory.
##
assert_git_not_repo() {
local dir="${1:-$(pwd)}"
assert_dir_exists "${dir}" || return 1
if [ -d "${dir}/.git" ]; then
format_error "Directory is a git repository, but should not be" "directory" "${dir}" | flunk
else
return 0
fi
}
##
# Asserts that a file is tracked in a git repository.
#
# Arguments:
# 1. file: File to check.
# 2. dir: Repository directory. Optional, defaults to the current directory.
#
# Returns:
# 0 if the file is tracked, 1 otherwise. Unlike the other assertions in this
# library, this one reports the outcome through the exit status alone and
# prints no message.
##
assert_git_file_tracked() {
local file="${1-}"
local dir="${2:-$(pwd)}"
if [ ! -d "${dir}/.git" ]; then
return 1
fi
git --work-tree="${dir}" --git-dir="${dir}/.git" ls-files --error-unmatch "${file}" &>/dev/null
return $?
}
##
# Asserts that a file is not tracked in a git repository.
#
# Arguments:
# 1. file: File to check.
# 2. dir: Repository directory. Optional, defaults to the current directory.
#
# Returns:
# 0 if the file is not tracked, 1 otherwise. Unlike the other assertions in
# this library, this one reports the outcome through the exit status alone and
# prints no message.
##
assert_git_file_not_tracked() {
local file="${1-}"
local dir="${2:-$(pwd)}"
if [ ! -d "${dir}/.git" ]; then
return 1
fi
if git --work-tree="${dir}" --git-dir="${dir}/.git" ls-files --error-unmatch "${file}" &>/dev/null; then
return 1
else
return 0
fi
}
##
# Asserts that a git repository has no uncommitted changes.
#
# Arguments:
# 1. dir: Repository directory. Optional, defaults to the current directory.
##
assert_git_clean() {
local dir="${1:-$(pwd)}"
local message
assert_git_repo "${dir}" || return 1
message="$(git --work-tree="${dir}" --git-dir="${dir}/.git" status)"
if string_match "${message}" "nothing to commit" "literal" 0 "anywhere"; then
return 0
fi
format_error "Repository has uncommitted changes" "directory" "${dir}" "status" "${message}" | flunk
return 1
}
##
# Asserts that a git repository has uncommitted changes.
#
# Arguments:
# 1. dir: Repository directory. Optional, defaults to the current directory.
##
assert_git_not_clean() {
local dir="${1:-$(pwd)}"
local message
assert_git_repo "${dir}" || return 1
message="$(git --work-tree="${dir}" --git-dir="${dir}/.git" status)"
if ! string_match "${message}" "nothing to commit" "literal" 0 "anywhere"; then
return 0
fi
format_error "Repository has no uncommitted changes, but should have" "directory" "${dir}" "status" "${message}" | flunk
return 1
}
##
## Deprecated aliases, removed in v2.1.
##
assert_not_git_repo() {
[ -n "${BATS_HELPERS_DEPRECATION_QUIET-}" ] || echo "Deprecated: 'assert_not_git_repo' will be removed in v2.1. Use 'assert_git_not_repo' instead." >&3
assert_git_not_repo "$@"
}
assert_git_file_is_tracked() {
[ -n "${BATS_HELPERS_DEPRECATION_QUIET-}" ] || echo "Deprecated: 'assert_git_file_is_tracked' will be removed in v2.1. Use 'assert_git_file_tracked' instead." >&3
assert_git_file_tracked "$@"
}
assert_git_file_is_not_tracked() {
[ -n "${BATS_HELPERS_DEPRECATION_QUIET-}" ] || echo "Deprecated: 'assert_git_file_is_not_tracked' will be removed in v2.1. Use 'assert_git_file_not_tracked' instead." >&3
assert_git_file_not_tracked "$@"
}