sennit 0.21.0

A dotfiles manager that keeps symlink semantics, and adds templating and drift detection
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use crate::manifest::Manifest;
use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// 1 つのリンクについて、あるべき姿と現状の差。
#[derive(Debug, Clone)]
pub struct Entry {
    /// リポジトリルートからの相対パス。$HOME からの相対パスでもある。
    pub rel: PathBuf,
    pub src: PathBuf,
    pub dest: PathBuf,
    pub state: State,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum State {
    /// 既に正しい symlink が張られている
    Linked,
    /// 何も無い
    Missing,
    /// 別の場所を指す symlink
    Wrong { current: PathBuf },
    /// symlink ではない実体(ファイル/ディレクトリ)が居座っている
    Occupied,
    /// dest の祖先に symlink がある。向き先はリポジトリの中でも外でもよい。
    ///
    /// 旧い配置でディレクトリごと張ったものが典型。dest はその symlink を
    /// 通ってリポジトリの実ファイルに着くので、そのまま進めるとリポジトリの
    /// ファイルを退避するか、自分自身を指すリンクを作る。祖先の symlink だけを
    /// 外して実ディレクトリにし、その下に 1 ファイルずつ張り直す。
    Ancestor { link: PathBuf },
}

impl State {
    pub fn needs_change(&self) -> bool {
        !matches!(self, State::Linked)
    }
}

pub struct Plan {
    pub entries: Vec<Entry>,
}

impl Plan {
    /// マニフェストとリポジトリの実体から、あるべきリンク一覧を組み立てる。
    ///
    /// ディレクトリはその下のファイル単位で symlink する。ディレクトリごと
    /// symlink すると、他のツールがそこへ書き込んだファイルまでリポジトリに
    /// 現れてしまうため。
    pub fn build(root: &Path, home: &Path, manifest: &Manifest) -> Result<Self> {
        let mut entries = Vec::new();

        for target in manifest.targets() {
            let abs = root.join(target);
            // exists() は EACCES も「無い」と答える。読めなくなっただけの
            // ディレクトリを宣言から外れたものとして扱うと、その下のリンクが
            // すべて stale になり $HOME から消える。無いのか読めないのかを
            // 区別する — WalkDir の側と同じ理由。
            match std::fs::metadata(&abs) {
                Ok(_) => {}
                Err(err) if err.kind() == std::io::ErrorKind::NotFound => continue,
                Err(err) => {
                    return Err(anyhow::Error::new(err))
                        .with_context(|| format!("failed to read {}", abs.display()))
                }
            }

            if abs.is_file() {
                Self::push(&mut entries, root, home, manifest, Path::new(target))?;
                continue;
            }

            // 読めなかったディレクトリを黙って飛ばさない。飛ばすとその下の
            // パスが「今回の対象」から丸ごと落ち、apply は前回の記録と
            // 突き合わせて $HOME 側のリンクを prune しにかかる。権限を
            // 1 つ間違えただけで利用者の設定が外れる。
            //
            // ignore のディレクトリには入らない。入ると、無関係な木(作業ツリーや
            // node_modules)を全部歩くうえ、その中の読めないディレクトリで落ちる。
            let walker = WalkDir::new(&abs).into_iter().filter_entry(|e| {
                e.path()
                    .strip_prefix(root)
                    .map(|rel| !manifest.is_ignored(rel))
                    .unwrap_or(true)
            });
            for e in walker {
                let e = e.with_context(|| format!("failed to read {}", abs.display()))?;
                if !e.file_type().is_file() {
                    continue;
                }
                let rel = e
                    .path()
                    .strip_prefix(root)
                    .context("path escaped repository root")?;
                Self::push(&mut entries, root, home, manifest, rel)?;
            }
        }

        // 生成物はまだディスクに無いことがある。クローン直後の diff / list が
        // 「これから作られて張られるもの」を数えないと、apply の結果と食い違う。
        // 宣言されている出力は、実体の有無に関わらず対象として数える。
        for out in manifest.render.keys().chain(manifest.encrypted.keys()) {
            let rel = Path::new(out);
            if entries.iter().any(|e| e.rel == rel) {
                continue;
            }
            if !manifest.targets().iter().any(|t| {
                let t = Path::new(t);
                rel == t || rel.starts_with(t)
            }) {
                continue;
            }
            Self::push(&mut entries, root, home, manifest, rel)?;
        }

        entries.sort_by(|a, b| a.rel.cmp(&b.rel));
        Ok(Plan { entries })
    }

    fn push(
        entries: &mut Vec<Entry>,
        root: &Path,
        home: &Path,
        manifest: &Manifest,
        rel: &Path,
    ) -> Result<()> {
        // [link] の対象が重なっていると同じパスが二度入り、二度目の
        // symlink が EEXIST で落ちる。diff も --dry-run も何も言わないまま、
        // 最初の apply だけが途中で止まる。
        if entries.iter().any(|e| e.rel == rel) {
            return Ok(());
        }
        // テンプレートは生成の入力であって配置対象ではない。ignore の宣言に
        // 頼ると書き忘れたときに、未展開の {{ }} を含むファイルがそのまま
        // $HOME に置かれる。[render] の入力は自明に対象外なので自動で外す。
        //
        // [merge] の入力も同じ。共有設定そのものを $HOME に出す理由が無い。
        if manifest.is_template(rel)
            || manifest.is_ciphertext(rel)
            || manifest.is_merge_source(rel)
            || manifest.is_ignored(rel)
        {
            return Ok(());
        }
        // [merge] の出力は symlink にしない。リポジトリに同じパスのファイルが
        // あると、link が張った symlink を merge が JSON として書き直す。
        if manifest.is_merge_output(rel) {
            anyhow::bail!(
                "`{}` is a [merge] output and also a file under [link]; \
                 remove it from the repository or from [link]",
                rel.display()
            );
        }
        let src = crate::platform::native(&root.join(rel));
        let dest = crate::platform::native(&home.join(rel));
        let state = match ancestor_link(root, home, rel, manifest) {
            Some(link) => State::Ancestor { link },
            None => inspect(&src, &dest),
        };
        entries.push(Entry {
            rel: rel.to_path_buf(),
            src,
            dest,
            state,
        });
        Ok(())
    }

    pub fn changes(&self) -> impl Iterator<Item = &Entry> {
        self.entries.iter().filter(|e| e.state.needs_change())
    }
}

/// `rel` の祖先(home の直下から dest の親まで)にある symlink のうち、
/// いちばん外側のもの。向き先は問わない。
fn first_symlink(home: &Path, rel: &Path) -> Option<PathBuf> {
    let mut cur = home.to_path_buf();
    let comps: Vec<_> = rel.components().collect();
    for c in comps.iter().take(comps.len().saturating_sub(1)) {
        cur.push(c);
        let Ok(meta) = std::fs::symlink_metadata(&cur) else {
            // ここから先は無い。無いものは symlink ではありえない
            return None;
        };
        if meta.file_type().is_symlink() {
            return Some(cur);
        }
    }
    None
}

/// 張り替える祖先 symlink。`rel` の祖先のうちいちばん外側の symlink で、
///
/// - 向き先がリポジトリの中なら、どこにあっても張り替える。そのまま進むと
///   リポジトリ自身のファイルを退避し、自分を指すリンクを作る。
/// - 向き先が外なら、manifest に書かれたトップレベルのパスより**深い**ものだけ
///   張り替える。旧い配置が別のリポジトリへ向けていたディレクトリが典型で、
///   symlink は中身を持たないので、外せば向き先には触れずに済む。
///   トップレベルそのもの(`.config -> ~/Dropbox/config`)や、その上のリンクは
///   利用者が意図した配置なので触らず、0.19 までと同じく向き先の中に置く。
pub fn ancestor_link(root: &Path, home: &Path, rel: &Path, manifest: &Manifest) -> Option<PathBuf> {
    let link = first_symlink(home, rel)?;
    if link_into_repo(root, &link) {
        return Some(link);
    }
    let at = link.strip_prefix(home).ok()?;
    manifest
        .targets()
        .iter()
        .any(|t| at.starts_with(t) && at != Path::new(t))
        .then_some(link)
}

/// manifest のトップレベルのパスのうち、それ自身が外を指す symlink のもの。
/// 配置はその向き先の中に行われる。
pub fn outside_top_levels(
    root: &Path,
    home: &Path,
    manifest: &Manifest,
) -> Vec<(PathBuf, PathBuf)> {
    let mut out = Vec::new();
    for t in manifest.targets() {
        let path = home.join(t);
        let is_link = std::fs::symlink_metadata(&path)
            .map(|m| m.file_type().is_symlink())
            .unwrap_or(false);
        if is_link && !link_into_repo(root, &path) {
            if let Ok(to) = std::fs::read_link(&path) {
                out.push((PathBuf::from(t), to));
            }
        }
    }
    out
}

/// 上の一覧を 1 行ずつ知らせる。
pub fn note_outside_top_levels(root: &Path, home: &Path, manifest: &Manifest) {
    for (rel, to) in outside_top_levels(root, home, manifest) {
        println!(
            "note: ~/{} points to {}, outside the repository; files are placed inside it",
            rel.display(),
            to.display()
        );
    }
}

/// その symlink の向き先がリポジトリの中か。
///
/// 向き先を解決できればその実体で、できなければ(リンク先が消えている)
/// 字句で比べる。`repo/../other` は文字としては `repo` で始まるが外なので、
/// 字句の比較の前に `..` を畳む。
pub fn link_into_repo(root: &Path, link: &Path) -> bool {
    let Ok(target) = std::fs::read_link(link) else {
        return false;
    };
    let target = if target.is_absolute() {
        target
    } else {
        match link.parent() {
            Some(p) => p.join(target),
            None => return false,
        }
    };
    match crate::platform::canonicalize(&target) {
        Ok(real) => crate::platform::path_starts_with(&real, root),
        Err(_) => crate::platform::path_starts_with(&collapse(&target), root),
    }
}

/// `.` と `..` を字句で畳む。symlink は解決しない。
fn collapse(p: &Path) -> PathBuf {
    use std::path::Component;
    let mut out = PathBuf::new();
    for c in p.components() {
        match c {
            Component::CurDir => {}
            Component::ParentDir => {
                if !out.pop() {
                    out.push("..");
                }
            }
            other => out.push(other),
        }
    }
    out
}

/// 祖先の symlink のうち、リポジトリの中を指すものだけ。
///
/// [merge] の出力が使う。出力は plan の対象ではなく、利用者が自分で外へ
/// 向けたリンクの先に書くのは従来どおりでよい。中を指すリンクを通すと
/// リポジトリを書き換えてしまうので、それだけを見つける。
pub fn repo_ancestor_link(root: &Path, home: &Path, rel: &Path) -> Option<PathBuf> {
    first_symlink(home, rel).filter(|l| link_into_repo(root, l))
}

/// dest の現状を分類する。symlink_metadata なので、リンク先の有無に
/// 引きずられずリンクそのものを見る。
fn inspect(src: &Path, dest: &Path) -> State {
    match std::fs::symlink_metadata(dest) {
        Err(_) => State::Missing,
        Ok(meta) => {
            if !meta.file_type().is_symlink() {
                return State::Occupied;
            }
            match std::fs::read_link(dest) {
                Ok(current) if crate::platform::same_path(&current, src) => State::Linked,
                Ok(current) => State::Wrong { current },
                Err(_) => State::Occupied,
            }
        }
    }
}

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

    fn scratch(name: &str) -> PathBuf {
        let d = std::env::temp_dir().join(format!("sennit-plan-{name}"));
        let _ = std::fs::remove_dir_all(&d);
        std::fs::create_dir_all(&d).unwrap();
        d
    }

    fn write(path: &Path, body: &str) {
        if let Some(p) = path.parent() {
            std::fs::create_dir_all(p).unwrap();
        }
        std::fs::write(path, body).unwrap();
    }

    #[test]
    fn inspect_tells_the_four_states_apart() {
        let d = scratch("inspect");
        let src = d.join("src.conf");
        write(&src, "x");

        let missing = d.join("missing.conf");
        assert_eq!(inspect(&src, &missing), State::Missing);

        let occupied = d.join("occupied.conf");
        write(&occupied, "mine");
        assert_eq!(inspect(&src, &occupied), State::Occupied);

        let linked = d.join("linked.conf");
        symlink(&src, &linked).unwrap();
        assert_eq!(inspect(&src, &linked), State::Linked);

        let elsewhere = d.join("elsewhere.conf");
        write(&elsewhere, "y");
        let wrong = d.join("wrong.conf");
        symlink(&elsewhere, &wrong).unwrap();
        assert_eq!(inspect(&src, &wrong), State::Wrong { current: elsewhere });
    }

    /// リンク先が消えていても、リンクそのものは張られている。
    /// メタデータを追いかけると Missing に見えてしまう。
    #[test]
    fn a_link_to_a_deleted_file_is_still_a_link() {
        let d = scratch("broken");
        let src = d.join("src.conf");
        write(&src, "x");
        let dest = d.join("dest.conf");
        symlink(&src, &dest).unwrap();
        std::fs::remove_file(&src).unwrap();

        assert_eq!(inspect(&src, &dest), State::Linked);
    }

    fn manifest_from(toml: &str) -> Manifest {
        toml::from_str(toml).unwrap()
    }

    /// クローン直後は生成物がまだ無い。それを数えないと diff / list が
    /// apply より少ない件数を出す。
    #[test]
    fn a_declared_output_is_planned_before_it_exists() {
        let d = scratch("planned-output");
        let root = d.join("repo");
        let home = d.join("home");
        std::fs::create_dir_all(&home).unwrap();
        write(&root.join("conf/app.conf.tmpl"), "x = {{ ui.bg }}\n");

        let m = manifest_from(
            r#"
[link]
common = ["conf"]
ignore = ["*.tmpl"]

[render]
"conf/app.conf" = "conf/app.conf.tmpl"
"#,
        );
        let plan = Plan::build(&root, &home, &m).unwrap();
        let rels: Vec<_> = plan.entries.iter().map(|e| e.rel.clone()).collect();
        assert_eq!(rels, vec![PathBuf::from("conf/app.conf")]);
        assert_eq!(plan.entries[0].state, State::Missing);
    }

    /// 出力が [link] の対象外なら、宣言されていても配置しない。
    #[test]
    fn an_output_outside_the_link_targets_is_not_planned() {
        let d = scratch("planned-outside");
        let root = d.join("repo");
        let home = d.join("home");
        std::fs::create_dir_all(&home).unwrap();
        write(&root.join("elsewhere/app.conf.tmpl"), "x\n");

        let m = manifest_from(
            r#"
[link]
common = ["conf"]

[render]
"elsewhere/app.conf" = "elsewhere/app.conf.tmpl"
"#,
        );
        let plan = Plan::build(&root, &home, &m).unwrap();
        assert!(plan.entries.is_empty());
    }

    /// ignore のディレクトリには入らない。その中が読めなくても落ちない。
    #[cfg(unix)]
    #[test]
    fn an_ignored_directory_is_not_walked() {
        use std::os::unix::fs::PermissionsExt;
        let d = scratch("pruned-walk");
        let root = d.join("repo");
        let home = d.join("home");
        std::fs::create_dir_all(&home).unwrap();
        write(&root.join("conf/a.conf"), "a\n");
        write(&root.join("conf/worktrees/x/secret.txt"), "x\n");
        write(&root.join("conf/hooks/node_modules/dep/index.js"), "x\n");
        write(&root.join("conf/hooks/a.test.ts"), "x\n");
        let locked = root.join("conf/worktrees");
        std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o000)).unwrap();

        let m = manifest_from(
            r#"
[link]
common = ["conf"]
ignore = ["conf/worktrees", "**/node_modules", "*.test.ts"]
"#,
        );
        let plan = Plan::build(&root, &home, &m);
        std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap();
        // root can read anything, which is fine: the point is that nothing fails
        let plan = plan.unwrap();
        let rels: Vec<_> = plan.entries.iter().map(|e| e.rel.clone()).collect();
        assert_eq!(rels, vec![PathBuf::from("conf/a.conf")]);
    }

    #[test]
    fn a_link_into_the_repository_above_the_destination_is_found() {
        let d = scratch("ancestor");
        let root = d.join("repo");
        let home = d.join("home");
        write(&root.join("skills/gog/SKILL.md"), "x\n");
        std::fs::create_dir_all(home.join("skills")).unwrap();
        symlink(root.join("skills/gog"), home.join("skills/gog")).unwrap();

        let rel = Path::new("skills/gog/SKILL.md");
        let m = manifest_from("[link]\ncommon = [\"skills\"]\n");
        assert_eq!(
            ancestor_link(&root, &home, rel, &m),
            Some(home.join("skills/gog"))
        );
        // a path with no link above it
        assert_eq!(
            ancestor_link(&root, &home, Path::new("skills/other/a.md"), &m),
            None
        );
    }

    #[test]
    fn a_relative_link_into_the_repository_is_found_too() {
        let d = scratch("ancestor-relative");
        let root = d.join("repo");
        let home = d.join("repo/../home");
        write(&root.join("skills/gog/SKILL.md"), "x\n");
        std::fs::create_dir_all(home.join("skills")).unwrap();
        symlink("../../repo/skills/gog", home.join("skills/gog")).unwrap();
        let root = crate::platform::canonicalize(&root).unwrap();
        assert!(repo_ancestor_link(&root, &home, Path::new("skills/gog/SKILL.md")).is_some());
    }

    /// A link is found wherever it leads; only the ones into the repository
    /// count for `repo_ancestor_link`.
    #[test]
    fn a_link_outside_the_repository_is_found_but_is_not_a_repository_link() {
        let d = scratch("ancestor-outside");
        let root = d.join("repo");
        let home = d.join("home");
        let elsewhere = d.join("life/skills/gog");
        write(&root.join("skills/gog/SKILL.md"), "x\n");
        write(&elsewhere.join("SKILL.md"), "old\n");
        std::fs::create_dir_all(home.join("skills")).unwrap();
        symlink(&elsewhere, home.join("skills/gog")).unwrap();

        let rel = Path::new("skills/gog/SKILL.md");
        assert!(!link_into_repo(&root, &home.join("skills/gog")));
        assert_eq!(repo_ancestor_link(&root, &home, rel), None);
        // below a declared top-level path, it is replaced ...
        let m = manifest_from("[link]\ncommon = [\"skills\"]\n");
        assert_eq!(
            ancestor_link(&root, &home, rel, &m),
            Some(home.join("skills/gog"))
        );
        // ... but the top-level path itself, or a link above it, is not
        let m = manifest_from("[link]\ncommon = [\"skills/gog\"]\n");
        assert_eq!(ancestor_link(&root, &home, rel, &m), None);
        let m = manifest_from("[link]\ncommon = [\"skills/gog/SKILL.md\"]\n");
        assert_eq!(ancestor_link(&root, &home, rel, &m), None);
    }

    /// `repo/../other` starts with `repo` as text but is not inside it.
    #[test]
    fn a_target_that_climbs_out_of_the_repository_is_outside() {
        let d = scratch("dotdot");
        let root = crate::platform::canonicalize(&d).unwrap().join("repo");
        let home = d.join("home");
        write(&root.join("a"), "x\n");
        write(&d.join("other/p"), "x\n");
        std::fs::create_dir_all(&home).unwrap();

        // the target exists, so it can be resolved
        let to_existing = root.join("../other/p");
        symlink(&to_existing, home.join("existing")).unwrap();
        assert!(!link_into_repo(&root, &home.join("existing")));
        // the target is gone, so only the text is left; `..` still has to count
        let to_missing = root.join("../nowhere/p");
        symlink(&to_missing, home.join("dangling")).unwrap();
        assert!(!link_into_repo(&root, &home.join("dangling")));
        // a `..` that stays inside is inside
        let inside = root.join("sub/../a");
        symlink(&inside, home.join("inside")).unwrap();
        assert!(link_into_repo(&root, &home.join("inside")));
        let gone = root.join("sub/../gone");
        symlink(&gone, home.join("inside-gone")).unwrap();
        assert!(link_into_repo(&root, &home.join("inside-gone")));
    }
}