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
//! What:
//! Ask real Git,
//! in one query,
//! which repository an invocation selects and where
//! inside its worktree the command runs.
//! Why:
//! The configuration root,
//! the require-root policy and the worktree policies all need
//! answers Git itself computes from `-C`,
//! `--git-dir`,
//! `--work-tree` and its
//! environment.
//! One `git rev-parse` prints all of them,
//! so an invocation starts one
//! Git process for these facts instead of one per fact,
//! and the wrapper never
//! re-implements Git's repository discovery or its `-C` chaining.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // const { identity, prefix } = parseLocationOutput(await git(locationQueryArguments(globalPrefix)));
//! ```
/// What:
/// `use` brings names from sibling files into this file;
/// `super::` means "the parent
/// module",
/// where every sibling file of this crate is declared.
/// Why:
/// The identity part of the answer is read by the existing identity reader;
/// this
/// module adds the one extra line.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// import { parseIdentityOutput } from './worktree_identity.ts';
/// ```
use crate;
use crate;
/// What:
/// `OsString` is owned operating-system text of raw bytes.
/// Sibling the reader might
/// expect:
/// `String`,
/// which must be valid UTF-8.
/// Why:
/// The caller's global options are replayed to Git unchanged.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// // string[] argv, but byte-preserving.
/// ```
use OsString;
/// `Path`/`PathBuf` are borrowed/owned filesystem paths of raw bytes.
use ;
/// What:
/// The `rev-parse` option that prints the path from the worktree top level to the
/// directory the command runs in.
/// `&str` is borrowed text baked into the program.
/// Why:
/// Git prints an empty line at the top level,
/// so "is the command run from the
/// root" is Git's own answer,
/// with every `-C` and symbolic link already applied.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const PREFIX_OPTION = '--show-prefix';
/// ```
pub const PREFIX_OPTION: &str = "--show-prefix";
/// What:
/// Everything the one query answers.
/// A `struct` is a record with named fields;
/// `Vec<u8>` is an owned list of bytes (`u8` is one byte;
/// sibling `String` would
/// require UTF-8).
/// `#[derive(...)]` asks the compiler to generate cloning,
/// debug
/// printing and `==`.
/// Why:
/// The prefix is kept as the bytes Git printed,
/// because a directory name need not
/// be UTF-8.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type RepositoryLocation = { identity: WorktreeIdentity; prefix: Buffer };
/// ```
/// Named predicate for finding the last line break;
/// `&u8` borrows one byte.
/// What:
/// Interpret the location query's exit state and output.
/// `&[u8]` borrows the raw
/// output;
/// `std::io::Result<T>` is `Result<T, std::io::Error>`,
/// "a value or an
/// input/output error".
/// Why:
/// On success the last line is the prefix and everything before it is the identity
/// answer.
/// On failure Git stopped before the prefix,
/// so the output is the identity
/// answer of a place without a worktree.
/// Any other shape is rejected,
/// not guessed.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function parseLocationOutput(success: boolean, stdout: Buffer): RepositoryLocation; // throws
/// ```
/// What:
/// Build the real-Git argument list of the location query:
/// the caller's global
/// options,
/// the identity options,
/// then the prefix option.
/// `&[OsString]` borrows the
/// arguments before the subcommand;
/// `Vec<OsString>` is the owned result.
/// Why:
/// Replaying the complete global prefix lets Git apply `-C` chains through symbolic
/// links,
/// `--git-dir`,
/// `--work-tree` and its environment exactly as it will for the
/// forwarded command.
/// The prefix option is last because Git stops at
/// `--show-toplevel` where there is no worktree,
/// before it would print a prefix.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const args = [...globalPrefix, ...IDENTITY_QUERY, '--show-prefix'];
/// ```
/// What:
/// The directory the command runs in:
/// the worktree top level plus the prefix.
/// `&Path` borrows the top level;
/// the result is an owned path.
/// Why:
/// Diagnostics name the directory,
/// and the require-root decision compares it with
/// the top level.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const effectiveDirectory = prefix === '' ? root : join(root, prefix);
/// ```
/// Output-shape and disposable-repository controls stay out of the release executable.