tsk-bin 0.25.0

tsk — work with a clear context
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::collections::hash_map::RandomState;
use std::ffi::OsString;
use std::hash::{BuildHasher, Hasher};
use std::path::{Path, PathBuf};

use super::git::Git;
use super::refs::{LedgerRefs, RefsSource};

pub const IN_REPO_REMOTE: &str = "origin";
pub const DOCS_DIR_NAME: &str = "docs";
pub const DOLT_DIR_NAME: &str = "ledger-db";
pub const CLONE_ID_FILE: &str = "tsk-clone-id";
const CLONE_ID_SUFFIX_LEN: usize = 8;

pub const REPO_ID_FILE: &str = "tsk-repo-id";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LedgerLocation {
    remote: String,
    ref_name: String,
    repo_id: Option<String>,
    refs: LedgerRefs,
    source: RefsSource,
}

impl LedgerLocation {
    /// The location of a repo's own ledger on branches. Resolution builds the location in
    /// the mode in force with [`LedgerLocation::with_refs`].
    pub fn in_repo() -> LedgerLocation {
        LedgerLocation {
            remote: IN_REPO_REMOTE.to_string(),
            ref_name: LedgerRefs::Branches.ledger_ref().to_string(),
            repo_id: None,
            refs: LedgerRefs::Branches,
            source: RefsSource::Default,
        }
    }

    /// The location of a nexus-held ledger on branches.
    pub fn nexus(nexus_url: &str, repo_id: &str) -> LedgerLocation {
        LedgerLocation {
            remote: nexus_url.to_string(),
            ref_name: format!("{}{}", LedgerRefs::Branches.nexus_prefix(), repo_id),
            repo_id: Some(repo_id.to_string()),
            refs: LedgerRefs::Branches,
            source: RefsSource::Default,
        }
    }

    /// The same ledger in `refs` mode: the data ref and the documents ref change, the remote
    /// and the repo id do not.
    pub fn with_refs(&self, refs: LedgerRefs, source: RefsSource) -> LedgerLocation {
        LedgerLocation {
            remote: self.remote.clone(),
            ref_name: match &self.repo_id {
                Some(id) => format!("{}{}", refs.nexus_prefix(), id),
                None => refs.ledger_ref().to_string(),
            },
            repo_id: self.repo_id.clone(),
            refs,
            source,
        }
    }

    pub fn refs(&self) -> LedgerRefs {
        self.refs
    }

    pub fn source(&self) -> RefsSource {
        self.source
    }

    pub fn remote(&self) -> &str {
        &self.remote
    }

    pub fn ref_name(&self) -> &str {
        &self.ref_name
    }

    pub fn repo_id(&self) -> Option<&str> {
        self.repo_id.as_deref()
    }

    /// The ref that holds the ledger's documents, on the same remote. A nexus ref ends in
    /// `-docs` rather than `/docs`, because git cannot hold both `ledgers/<id>` and
    /// `ledgers/<id>/docs` as refs: one would be a file and the other a directory.
    pub fn docs_ref_name(&self) -> String {
        match &self.repo_id {
            Some(id) => format!("{}{}-docs", self.refs.nexus_prefix(), id),
            None => self.refs.docs_ref().to_string(),
        }
    }

    /// The location of the documents branch: this location's remote and label, with the
    /// docs ref in place of the ledger ref.
    pub fn docs(&self) -> LedgerLocation {
        LedgerLocation {
            remote: self.remote.clone(),
            ref_name: self.docs_ref_name(),
            repo_id: self.repo_id.clone(),
            refs: self.refs,
            source: self.source,
        }
    }

    pub fn label(&self) -> &str {
        if self.repo_id.is_some() {
            "the nexus"
        } else {
            &self.remote
        }
    }
}

/// What every command that needs the repository says, once, when it runs outside one.
pub const NOT_IN_REPO: &str =
    "error: not inside a git repository. Run tsk from inside the repository whose ledger you want.";

#[derive(Clone)]
pub struct Repo {
    pub git: Git,
    pub common_dir: PathBuf,
}

impl Repo {
    pub fn discover(dir: &Path) -> Result<Repo, String> {
        let git = Git::new(dir);
        let (args, output) =
            git.output(["rev-parse", "--path-format=absolute", "--git-common-dir"])?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if stderr.contains("not a git repository") {
                return Err(NOT_IN_REPO.to_string());
            }
            return Err(super::git::failure_message(&args, &output));
        }
        let common_dir = String::from_utf8_lossy(&output.stdout)
            .trim_end_matches(['\n', '\r'])
            .to_string();
        Ok(Repo {
            git,
            common_dir: PathBuf::from(common_dir),
        })
    }

    /// The ledger's location in the mode in force. Resolved once per process: the nexus
    /// index, the repo config and discovery are read the first time.
    pub fn location(&self, state_root: &Path) -> Result<LedgerLocation, String> {
        if let Some(known) = super::refs::memo_get(self, state_root) {
            return Ok(known);
        }
        let location = super::nexus::resolve(
            self,
            state_root,
            crate::config::config_file_from_env().as_deref(),
        )?;
        super::refs::memo_set(self, state_root, &location);
        Ok(location)
    }

    pub fn repo_id_file(&self) -> PathBuf {
        self.common_dir.join(REPO_ID_FILE)
    }

    pub fn read_repo_id(&self) -> Result<Option<String>, String> {
        read_clone_id(&self.repo_id_file())
    }

    pub fn write_repo_id(&self, id: &str) -> Result<(), String> {
        std::fs::write(self.repo_id_file(), format!("{}\n", id)).map_err(|e| {
            format!(
                "error: could not write the repo id to {}: {}",
                self.repo_id_file().display(),
                e
            )
        })
    }

    pub fn raw_origin_url(&self) -> Result<Option<String>, String> {
        let (args, output) = self.git.output(["config", "--get", "remote.origin.url"])?;
        match output.status.code() {
            Some(0) => {
                let url = String::from_utf8_lossy(&output.stdout).trim().to_string();
                Ok(if url.is_empty() { None } else { Some(url) })
            }
            Some(1) => Ok(None),
            _ => Err(super::git::failure_message(&args, &output)),
        }
    }

    pub fn clone_id_file(&self) -> PathBuf {
        self.common_dir.join(CLONE_ID_FILE)
    }

    pub fn read_clone_id(&self) -> Result<Option<String>, String> {
        read_clone_id(&self.clone_id_file())
    }

    pub fn ensure_clone_id(&self) -> Result<String, String> {
        if let Some(id) = self.read_clone_id()? {
            return Ok(id);
        }
        let id = format!(
            "{}-{}",
            clone_id_name(&self.common_dir),
            mint_token(CLONE_ID_SUFFIX_LEN, &self.common_dir.to_string_lossy())
        );
        std::fs::write(self.clone_id_file(), format!("{}\n", id)).map_err(|e| {
            format!(
                "error: could not write the clone id to {}: {}",
                self.clone_id_file().display(),
                e
            )
        })?;
        Ok(id)
    }
}

pub fn read_clone_id(file: &Path) -> Result<Option<String>, String> {
    match std::fs::read_to_string(file) {
        Ok(text) => {
            let id: String = text.chars().filter(|c| !c.is_whitespace()).collect();
            Ok(if id.is_empty() { None } else { Some(id) })
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(e) => Err(format!("error: could not read {}: {}", file.display(), e)),
    }
}

pub fn clone_id_name(common_dir: &Path) -> String {
    let name: String = common_dir
        .parent()
        .and_then(|p| p.file_name())
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default()
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
        .collect();
    if name.is_empty() {
        "repo".to_string()
    } else {
        name
    }
}

pub fn mint_token(length: usize, extra_seed: &str) -> String {
    let mut token = String::new();
    let mut round: u64 = 0;
    while token.len() < length {
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);
        let mut hasher = RandomState::new().build_hasher();
        hasher.write_u128(nanos);
        hasher.write_u32(std::process::id());
        hasher.write_u64(round);
        hasher.write(extra_seed.as_bytes());
        token.push_str(&format!("{:016x}", hasher.finish()));
        round += 1;
    }
    token.truncate(length);
    token
}

pub fn state_root(
    xdg_state_home: Option<OsString>,
    home: Option<OsString>,
) -> Result<PathBuf, String> {
    let base = match xdg_state_home.filter(|v| !v.is_empty()) {
        Some(xdg) => PathBuf::from(xdg),
        None => match home.filter(|v| !v.is_empty()) {
            Some(home) => PathBuf::from(home).join(".local").join("state"),
            None => return Err(
                "error: neither XDG_STATE_HOME nor HOME is set; cannot locate the ledger worktree"
                    .to_string(),
            ),
        },
    };
    Ok(base.join("tsk"))
}

pub fn state_root_from_env() -> Result<PathBuf, String> {
    state_root(std::env::var_os("XDG_STATE_HOME"), std::env::var_os("HOME"))
}

/// Where the Dolt backend keeps the worktree of its documents branch.
pub fn docs_worktree_path(state_root: &Path, clone_id: &str) -> PathBuf {
    state_root.join("repos").join(clone_id).join(DOCS_DIR_NAME)
}

/// Where the Dolt backend keeps its database.
pub fn dolt_db_path(state_root: &Path, clone_id: &str) -> PathBuf {
    state_root.join("repos").join(clone_id).join(DOLT_DIR_NAME)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn in_repo_location_uses_full_ref_name_on_origin() {
        let location = LedgerLocation::in_repo();
        assert_eq!(location.remote(), "origin");
        assert_eq!(location.ref_name(), "refs/heads/tsk/ledger");
        assert_eq!(location.label(), "origin");
        assert_eq!(location.repo_id(), None);
        assert!(location.ref_name().starts_with("refs/heads/"));
    }

    #[test]
    fn nexus_location_uses_the_repo_id_branch_on_the_nexus_url() {
        let location = LedgerLocation::nexus("https://example.test/o/nexus", "work-api");
        assert_eq!(location.remote(), "https://example.test/o/nexus");
        assert_eq!(location.ref_name(), "refs/heads/ledgers/work-api");
        assert_eq!(location.label(), "the nexus");
        assert_eq!(location.repo_id(), Some("work-api"));
    }

    #[test]
    fn the_docs_ref_is_tsk_docs_in_the_repo_and_id_docs_on_the_nexus() {
        let in_repo = LedgerLocation::in_repo();
        assert_eq!(in_repo.docs_ref_name(), "refs/heads/tsk/docs");
        assert_eq!(in_repo.docs().ref_name(), "refs/heads/tsk/docs");
        assert_eq!(in_repo.docs().remote(), "origin");

        let nexus = LedgerLocation::nexus("https://example.test/o/nexus", "work-api");
        assert_eq!(nexus.docs_ref_name(), "refs/heads/ledgers/work-api-docs");
        let docs = nexus.docs();
        assert_eq!(docs.ref_name(), "refs/heads/ledgers/work-api-docs");
        assert_eq!(docs.remote(), "https://example.test/o/nexus");
        assert_eq!(docs.label(), "the nexus");
        // The two refs can coexist: neither is a path prefix of the other.
        assert!(!docs
            .ref_name()
            .starts_with(&format!("{}/", nexus.ref_name())));
    }

    #[test]
    fn a_custom_ref_location_names_every_ref_in_full() {
        use crate::ledger::refs::{LedgerRefs, RefsSource};
        let in_repo = LedgerLocation::in_repo().with_refs(LedgerRefs::Custom, RefsSource::Default);
        assert_eq!(in_repo.ref_name(), "refs/tsk/ledger");
        assert_eq!(in_repo.docs_ref_name(), "refs/tsk/docs");
        assert_eq!(in_repo.docs().ref_name(), "refs/tsk/docs");
        assert_eq!(in_repo.remote(), "origin");
        assert_eq!(in_repo.refs(), LedgerRefs::Custom);
        assert_eq!(in_repo.source(), RefsSource::Default);

        let nexus = LedgerLocation::nexus("https://example.test/o/nexus", "work-api")
            .with_refs(LedgerRefs::Custom, RefsSource::Nexus);
        assert_eq!(nexus.ref_name(), "refs/tsk/ledgers/work-api");
        assert_eq!(nexus.docs_ref_name(), "refs/tsk/ledgers/work-api-docs");
        assert_eq!(nexus.docs().label(), "the nexus");
        assert_eq!(nexus.repo_id(), Some("work-api"));
        // The two refs can coexist: neither is a path prefix of the other.
        assert!(!nexus
            .docs_ref_name()
            .starts_with(&format!("{}/", nexus.ref_name())));
    }

    #[test]
    fn with_refs_round_trips_to_the_branch_location() {
        use crate::ledger::refs::{LedgerRefs, RefsSource};
        let branches = LedgerLocation::nexus("n", "x");
        let custom = branches.with_refs(LedgerRefs::Custom, RefsSource::Default);
        assert_ne!(branches, custom);
        assert_eq!(
            custom.with_refs(LedgerRefs::Branches, RefsSource::Default),
            branches
        );
    }

    #[test]
    fn docs_worktree_path_is_docs_under_clone_id() {
        assert_eq!(
            docs_worktree_path(Path::new("/s/tsk"), "tsk-180ae86e"),
            PathBuf::from("/s/tsk/repos/tsk-180ae86e/docs")
        );
    }

    #[test]
    fn state_root_prefers_xdg_state_home() {
        let root = state_root(Some("/x/state".into()), Some("/home/u".into())).unwrap();
        assert_eq!(root, PathBuf::from("/x/state/tsk"));
    }

    #[test]
    fn state_root_treats_empty_xdg_as_unset() {
        let root = state_root(Some("".into()), Some("/home/u".into())).unwrap();
        assert_eq!(root, PathBuf::from("/home/u/.local/state/tsk"));
    }

    #[test]
    fn state_root_falls_back_to_home() {
        let root = state_root(None, Some("/home/u".into())).unwrap();
        assert_eq!(root, PathBuf::from("/home/u/.local/state/tsk"));
    }

    #[test]
    fn state_root_fails_without_xdg_or_home() {
        assert!(state_root(None, None).is_err());
    }

    #[test]
    fn clone_id_name_is_the_clone_directory_name() {
        assert_eq!(clone_id_name(Path::new("/code/tsk/.git")), "tsk");
    }

    #[test]
    fn clone_id_name_strips_unsafe_characters() {
        assert_eq!(
            clone_id_name(Path::new("/code/my repo!@v1.2_x/.git")),
            "myrepov1.2_x"
        );
    }

    #[test]
    fn clone_id_name_falls_back_to_repo() {
        assert_eq!(clone_id_name(Path::new("/code/***/.git")), "repo");
        assert_eq!(clone_id_name(Path::new("/")), "repo");
    }

    #[test]
    fn mint_token_is_lowercase_hex_of_requested_length() {
        for length in [1, 8, 16, 20] {
            let token = mint_token(length, "seed");
            assert_eq!(token.len(), length);
            assert!(token
                .chars()
                .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()));
        }
    }

    #[test]
    fn mint_token_differs_between_calls() {
        assert_ne!(mint_token(8, "seed"), mint_token(8, "seed"));
    }

    #[test]
    fn read_clone_id_strips_whitespace_and_treats_empty_as_absent() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join(CLONE_ID_FILE);
        assert_eq!(read_clone_id(&file).unwrap(), None);
        std::fs::write(&file, "  \n").unwrap();
        assert_eq!(read_clone_id(&file).unwrap(), None);
        std::fs::write(&file, " tsk-1234abcd \n").unwrap();
        assert_eq!(
            read_clone_id(&file).unwrap(),
            Some("tsk-1234abcd".to_string())
        );
    }
}