Skip to main content

git_policy_cli/
git_builtins.rs

1//! What:
2//!  The names of the commands built into Git 2.56.0,
3//!  and the test "is this word one
4//!       of them".
5//! Why:
6//!  Git never expands an alias whose name is a built-in command,
7//!  so a built-in word
8//!      is the command that runs.
9//!  Any other word may be an alias for anything,
10//!  including
11//!      `worktree add`,
12//!  and resolving aliases is not ported;
13//!  the caller needs to know
14//!      which of the two it is looking at without starting Git.
15//!
16//! In TS you'd write (pseudocode):
17//! ```ts
18//! // GIT_BUILTIN_COMMANDS.has('status') === true; GIT_BUILTIN_COMMANDS.has('st') === false
19//! ```
20
21/// What:
22///  Every name `git --list-cmds=builtins` prints for Git 2.56.0,
23///  in that order.
24///       `&[&[u8]]` is a borrowed list of byte spellings baked into the program.
25///  Sibling
26///       the reader might expect:
27///  `&[&str]`,
28///  a list of UTF-8 texts.
29/// Why:
30///   Command words arrive as raw bytes and are compared without decoding,
31///  so the
32///       table holds bytes.
33///  A test compares the whole table with real Git's own answer.
34///
35/// In TS you'd write (pseudocode):
36/// ```ts
37/// const GIT_BUILTIN_COMMANDS: ReadonlySet<string> = new Set(['add', 'am', /* ... */]);
38/// ```
39pub const GIT_BUILTIN_COMMANDS: &[&[u8]] = &[
40    b"add",
41    b"am",
42    b"annotate",
43    b"apply",
44    b"archive",
45    b"backfill",
46    b"bisect",
47    b"blame",
48    b"branch",
49    b"bugreport",
50    b"bundle",
51    b"cat-file",
52    b"check-attr",
53    b"check-ignore",
54    b"check-mailmap",
55    b"check-ref-format",
56    b"checkout",
57    b"checkout--worker",
58    b"checkout-index",
59    b"cherry",
60    b"cherry-pick",
61    b"clean",
62    b"clone",
63    b"column",
64    b"commit",
65    b"commit-graph",
66    b"commit-tree",
67    b"config",
68    b"count-objects",
69    b"credential",
70    b"credential-cache",
71    b"credential-cache--daemon",
72    b"credential-store",
73    b"describe",
74    b"diagnose",
75    b"diff",
76    b"diff-files",
77    b"diff-index",
78    b"diff-pairs",
79    b"diff-tree",
80    b"difftool",
81    b"fast-export",
82    b"fast-import",
83    b"fetch",
84    b"fetch-pack",
85    b"fmt-merge-msg",
86    b"for-each-ref",
87    b"for-each-repo",
88    b"format-patch",
89    b"format-rev",
90    b"fsck",
91    b"fsck-objects",
92    b"fsmonitor--daemon",
93    b"gc",
94    b"get-tar-commit-id",
95    b"grep",
96    b"hash-object",
97    b"help",
98    b"history",
99    b"hook",
100    b"index-pack",
101    b"init",
102    b"init-db",
103    b"interpret-trailers",
104    b"last-modified",
105    b"log",
106    b"ls-files",
107    b"ls-remote",
108    b"ls-tree",
109    b"mailinfo",
110    b"mailsplit",
111    b"maintenance",
112    b"merge",
113    b"merge-base",
114    b"merge-file",
115    b"merge-index",
116    b"merge-ours",
117    b"merge-recursive",
118    b"merge-recursive-ours",
119    b"merge-recursive-theirs",
120    b"merge-subtree",
121    b"merge-tree",
122    b"mktag",
123    b"mktree",
124    b"multi-pack-index",
125    b"mv",
126    b"name-rev",
127    b"notes",
128    b"pack-objects",
129    b"pack-redundant",
130    b"pack-refs",
131    b"patch-id",
132    b"pickaxe",
133    b"prune",
134    b"prune-packed",
135    b"pull",
136    b"push",
137    b"range-diff",
138    b"read-tree",
139    b"rebase",
140    b"receive-pack",
141    b"reflog",
142    b"refs",
143    b"remote",
144    b"remote-ext",
145    b"remote-fd",
146    b"repack",
147    b"replace",
148    b"replay",
149    b"repo",
150    b"rerere",
151    b"reset",
152    b"restore",
153    b"rev-list",
154    b"rev-parse",
155    b"revert",
156    b"rm",
157    b"send-pack",
158    b"shortlog",
159    b"show",
160    b"show-branch",
161    b"show-index",
162    b"show-ref",
163    b"sparse-checkout",
164    b"stage",
165    b"stash",
166    b"status",
167    b"stripspace",
168    b"submodule--helper",
169    b"switch",
170    b"symbolic-ref",
171    b"tag",
172    b"unpack-file",
173    b"unpack-objects",
174    b"update-index",
175    b"update-ref",
176    b"update-server-info",
177    b"upload-archive",
178    b"upload-archive--writer",
179    b"upload-pack",
180    b"url-parse",
181    b"var",
182    b"verify-commit",
183    b"verify-pack",
184    b"verify-tag",
185    b"version",
186    b"whatchanged",
187    b"worktree",
188    b"write-tree",
189];
190
191/// What:
192///  Whether `word` is a command built into Git 2.56.0.
193///  `&[u8]` borrows the word's
194///       raw bytes;
195///  `bool` is true or false.
196/// Why:
197///   A built-in word cannot be an alias,
198///  so what the word says is what Git runs.
199///
200/// In TS you'd write (pseudocode):
201/// ```ts
202/// function isGitBuiltin(word: string): boolean { return GIT_BUILTIN_COMMANDS.has(word); }
203/// ```
204pub fn is_git_builtin(word: &[u8]) -> bool {
205    // `.contains(&word)` borrows the word for the lookup.
206    return GIT_BUILTIN_COMMANDS.contains(&word);
207}
208
209/// The comparison with real Git stays out of the release executable.
210#[cfg(test)]
211#[path = "git_builtins_tests.rs"]
212mod tests;