declutter-diff 0.1.3

Review AI-generated diffs with comments, tests, imports, logging and formatting toggled in and out
Documentation
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
use std::collections::HashSet;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

use crate::diff::RowKind;
use crate::git::git;
use crate::review::{DiffModes, FileReview};

/// Which changes the reviewer has marked as reviewed. A mark belongs to a file's exact
/// change — its path and both versions — so it lapses as soon as either version moves,
/// and it carries over between ranges that contain the same change.
///
/// Kept in `<git common dir>/declutter/reviewed.tsv`: private to this clone, shared by its
/// worktrees, never committed.
pub struct ReviewStore {
    path: Option<PathBuf>,
    marks: HashSet<(String, u64)>,
}

impl ReviewStore {
    /// Opens the store of the repository containing `dir`, or an in-memory one if the
    /// repository's git directory can't be found.
    pub fn open(dir: &Path) -> ReviewStore {
        match state_dir(dir) {
            Some(state) => ReviewStore::at(state.join("reviewed.tsv")),
            None => ReviewStore::in_memory(),
        }
    }

    pub fn at(path: PathBuf) -> ReviewStore {
        let marks = fs::read_to_string(&path)
            .unwrap_or_default()
            .lines()
            .filter_map(|line| {
                let (hash, file) = line.split_once('\t')?;
                Some((file.to_string(), u64::from_str_radix(hash, 16).ok()?))
            })
            .collect();
        ReviewStore {
            path: Some(path),
            marks,
        }
    }

    pub fn in_memory() -> ReviewStore {
        ReviewStore {
            path: None,
            marks: HashSet::new(),
        }
    }

    pub fn is_reviewed(&self, file: &FileReview) -> bool {
        self.marks.contains(&key(file))
    }

    pub fn set_reviewed(&mut self, file: &FileReview, reviewed: bool) -> Result<()> {
        if reviewed {
            self.marks.insert(key(file));
        } else {
            self.marks.remove(&key(file));
        }
        self.save()
    }

    fn save(&self) -> Result<()> {
        let Some(path) = &self.path else {
            return Ok(());
        };
        let mut lines: Vec<String> = self
            .marks
            .iter()
            .map(|(file, hash)| format!("{hash:016x}\t{file}"))
            .collect();
        lines.sort();
        write_atomically(path, &(lines.join("\n") + "\n"))
    }
}

/// `<git common dir>/declutter`: per-clone state, shared by worktrees, never committed.
pub fn state_dir(dir: &Path) -> Option<PathBuf> {
    let common = git(
        dir,
        &["rev-parse", "--path-format=absolute", "--git-common-dir"],
    )
    .ok()?;
    Some(PathBuf::from(String::from_utf8(common).ok()?.trim()).join("declutter"))
}

fn write_atomically(path: &Path, contents: &str) -> Result<()> {
    let dir = path.parent().context("state file has no directory")?;
    fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
    let tmp = path.with_extension("tmp");
    fs::write(&tmp, contents).with_context(|| format!("writing {}", tmp.display()))?;
    fs::rename(&tmp, path).with_context(|| format!("writing {}", path.display()))
}

/// Which version of the file a note's line number refers to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum NoteSide {
    /// A removed line: the number is in the old version.
    Old,
    New,
}

/// A reviewer's note on one line of a change, or — with an empty path — on the change
/// as a whole.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Note {
    pub path: String,
    pub side: NoteSide,
    pub line: usize,
    /// The line the note is about, so the note still makes sense once lines move.
    pub code: String,
    pub text: String,
    /// The review the note was left in — a pull request, or a range such as
    /// `origin/main...feature` — so it only shows up, and is only posted, there.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub review: Option<String>,
    /// Added from outside the viewer (`declutter notes add`) and not yet opened by the
    /// reviewer. Drafts are never posted: opening one with `m` makes it the reviewer's.
    #[serde(default, skip_serializing_if = "is_false")]
    pub draft: bool,
}

fn is_false(value: &bool) -> bool {
    !value
}

/// What [`NoteStore::add`] did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Added {
    New,
    /// The line already had a note; the text went under it.
    Appended,
    /// The line's note already says this.
    Duplicate,
}

/// Where a posted note ended up on the host.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Link {
    pub id: String,
    pub url: String,
}

/// A note as it was posted, kept in `posted.jsonl` next to the notes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Posted {
    #[serde(flatten)]
    pub note: Note,
    /// Seconds since the Unix epoch.
    pub posted_at: u64,
    #[serde(flatten)]
    pub link: Link,
}

/// Review notes, kept in `<git common dir>/declutter/notes.json` until cleared or
/// posted. A store can be scoped to one review; it then reads and writes only that
/// review's notes, and leaves the others alone.
pub struct NoteStore {
    path: Option<PathBuf>,
    notes: Vec<Note>,
    scope: Option<String>,
    /// What an in-memory store has posted; a stored one keeps it in `posted.jsonl`.
    posted: Vec<Posted>,
}

impl NoteStore {
    pub fn open(dir: &Path) -> NoteStore {
        match state_dir(dir) {
            Some(state) => NoteStore::at(state.join("notes.json")),
            None => NoteStore::in_memory(),
        }
    }

    pub fn at(path: PathBuf) -> NoteStore {
        let notes = fs::read(&path)
            .ok()
            .and_then(|bytes| serde_json::from_slice(&bytes).ok())
            .unwrap_or_default();
        NoteStore {
            path: Some(path),
            notes,
            scope: None,
            posted: Vec::new(),
        }
    }

    pub fn in_memory() -> NoteStore {
        NoteStore {
            path: None,
            notes: Vec::new(),
            scope: None,
            posted: Vec::new(),
        }
    }

    /// The same store, seeing only the notes of `review`.
    pub fn scoped(mut self, review: impl Into<String>) -> NoteStore {
        self.scope = Some(review.into());
        self
    }

    fn in_scope(&self, note: &Note) -> bool {
        self.scope.is_none() || note.review == self.scope
    }

    pub fn notes(&self) -> Vec<&Note> {
        self.notes
            .iter()
            .filter(|note| self.in_scope(note))
            .collect()
    }

    /// Where exported prompts are written, next to the notes themselves.
    pub fn export_path(&self) -> Option<PathBuf> {
        Some(self.path.as_ref()?.with_file_name("review-notes.md"))
    }

    pub fn find(&self, path: &str, side: NoteSide, line: usize) -> Option<&Note> {
        self.notes.iter().find(|note| {
            self.in_scope(note) && note.path == path && note.side == side && note.line == line
        })
    }

    /// The note on the change as a whole, if there is one.
    pub fn general(&self) -> Option<&Note> {
        self.find("", NoteSide::New, 0)
    }

    /// Adds or replaces the note on the same line of this review; an empty text removes it.
    pub fn set(&mut self, mut note: Note) -> Result<()> {
        note.review = self.scope.clone();
        self.notes.retain(|n| !n.same_place(&note));
        if !note.text.trim().is_empty() {
            self.notes.push(note);
            self.notes
                .sort_by(|a, b| (&a.path, a.line).cmp(&(&b.path, b.line)));
        }
        self.save()
    }

    /// Adds a note, putting its text under any note already on that line rather than
    /// replacing it. A note that gains text it didn't have takes the new note's draft
    /// state, so text nobody has read is never posted as read.
    pub fn add(&mut self, mut note: Note) -> Result<Added> {
        note.review = self.scope.clone();
        let text = note.text.trim().to_string();
        let added = match self.notes.iter_mut().find(|n| n.same_place(&note)) {
            Some(existing) if existing.text.contains(&text) => return Ok(Added::Duplicate),
            Some(existing) => {
                existing.text = format!("{}\n\n{text}", existing.text.trim_end());
                existing.draft |= note.draft;
                Added::Appended
            }
            None => {
                note.text = text;
                self.notes.push(note);
                self.notes
                    .sort_by(|a, b| (&a.path, a.line).cmp(&(&b.path, b.line)));
                Added::New
            }
        };
        self.save()?;
        Ok(added)
    }

    /// Logs these notes as posted, each with where it landed, and drops them from the store.
    pub fn record_posted(&mut self, posted: &[(Note, Link)]) -> Result<()> {
        let posted_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_or(0, |since| since.as_secs());
        let records: Vec<Posted> = posted
            .iter()
            .map(|(note, link)| Posted {
                note: Note {
                    review: self.scope.clone().or_else(|| note.review.clone()),
                    ..note.clone()
                },
                posted_at,
                link: link.clone(),
            })
            .collect();
        match self.log_path() {
            Some(log) => {
                let mut lines = String::new();
                for record in &records {
                    lines.push_str(&serde_json::to_string(record)?);
                    lines.push('\n');
                }
                fs::OpenOptions::new()
                    .create(true)
                    .append(true)
                    .open(&log)
                    .and_then(|mut file| file.write_all(lines.as_bytes()))
                    .with_context(|| format!("writing {}", log.display()))?;
            }
            None => self.posted.extend(records),
        }
        let gone: Vec<Note> = posted.iter().map(|(note, _)| note.clone()).collect();
        self.remove(&gone)
    }

    /// This review's posted notes, oldest first.
    pub fn posted(&self) -> Vec<Posted> {
        let all = match self.log_path() {
            Some(log) => fs::read_to_string(log)
                .unwrap_or_default()
                .lines()
                .filter_map(|line| serde_json::from_str(line).ok())
                .collect(),
            None => self.posted.clone(),
        };
        all.into_iter()
            .filter(|posted: &Posted| self.in_scope(&posted.note))
            .collect()
    }

    fn log_path(&self) -> Option<PathBuf> {
        Some(self.path.as_ref()?.with_file_name("posted.jsonl"))
    }

    /// Deletes this review's notes (every note, when the store is not scoped).
    pub fn clear(&mut self) -> Result<()> {
        let scope = self.scope.clone();
        self.notes
            .retain(|note| scope.is_some() && note.review != scope);
        self.save()
    }

    /// Deletes exactly these notes, as after posting them.
    pub fn remove(&mut self, gone: &[Note]) -> Result<()> {
        self.notes.retain(|note| !gone.contains(note));
        self.save()
    }

    /// The notes as one prompt to hand to a coding agent. Drafts are left out: nobody
    /// has read them, so they are no one's review comments yet.
    pub fn prompt(&self) -> String {
        let mut out = String::from(
            "Please address these review comments. Line numbers refer to the version under review.\n",
        );
        let opened = self.notes().into_iter().filter(|note| !note.draft);
        for (i, note) in opened.enumerate() {
            let text = note.text.trim().replace('\n', "\n   ");
            if note.is_general() {
                out.push_str(&format!("\n{}. On the change as a whole: {text}\n", i + 1));
            } else {
                out.push_str(&format!(
                    "\n{}. {}: {text}\n   ```\n   {}\n   ```\n",
                    i + 1,
                    note.place(),
                    note.code.trim()
                ));
            }
        }
        out
    }

    fn save(&self) -> Result<()> {
        let Some(path) = &self.path else {
            return Ok(());
        };
        write_atomically(path, &serde_json::to_string_pretty(&self.notes)?)
    }
}

impl Note {
    /// A draft note on the change as a whole rather than on a line.
    pub fn on_pull_request(text: impl Into<String>) -> Note {
        Note {
            path: String::new(),
            side: NoteSide::New,
            line: 0,
            code: String::new(),
            text: text.into(),
            review: None,
            draft: true,
        }
    }

    /// A draft note on `line` of `path`, which must be a row of the change's diff —
    /// with every layer shown — since that is where the viewer can show it. A removed
    /// line is on the old side; added and unchanged lines are on the new side.
    pub fn on_line(
        files: &[FileReview],
        path: &str,
        side: NoteSide,
        line: usize,
        text: impl Into<String>,
    ) -> Result<Note> {
        let Some(file) = files.iter().find(|file| file.path == path) else {
            bail!("`{path}` is not part of this change");
        };
        let view = file.view(DiffModes::SHOWN);
        let rows = view.hunks.iter().flat_map(|hunk| &hunk.rows);
        let lines: Vec<(usize, &str)> = rows
            .filter_map(|row| match (side, row.kind) {
                (NoteSide::Old, RowKind::Removed) => Some((row.old_line?, row.text.as_str())),
                (NoteSide::New, RowKind::Added | RowKind::Context) => {
                    Some((row.new_line?, row.text.as_str()))
                }
                _ => None,
            })
            .collect();
        let Some((_, code)) = lines.iter().find(|(n, _)| *n == line) else {
            let (place, which) = match side {
                NoteSide::New => (format!("`{path}:{line}`"), "the lines"),
                NoteSide::Old => (
                    format!("removed line {line} of `{path}`"),
                    "the removed lines",
                ),
            };
            if lines.is_empty() {
                bail!("{place} is not in the diff, which has no such lines");
            }
            let numbers: Vec<usize> = lines.iter().map(|(n, _)| *n).collect();
            bail!(
                "{place} is not in the diff; {which} in it are {}",
                ranges(&numbers)
            );
        };
        Ok(Note {
            path: path.to_string(),
            side,
            line,
            code: code.to_string(),
            text: text.into().trim().to_string(),
            review: None,
            draft: true,
        })
    }

    pub fn is_general(&self) -> bool {
        self.path.is_empty()
    }

    fn same_place(&self, other: &Note) -> bool {
        self.review == other.review
            && self.path == other.path
            && self.side == other.side
            && self.line == other.line
    }

    /// "`Cart.swift:12`", or "`Cart.swift` (removed line 12)".
    pub fn place(&self) -> String {
        if self.is_general() {
            return "the change as a whole".to_string();
        }
        match self.side {
            NoteSide::New => format!("`{}:{}`", self.path, self.line),
            NoteSide::Old => format!("`{}` (removed line {})", self.path, self.line),
        }
    }
}

/// "3, 12–18, 40–52" for sorted line numbers.
fn ranges(numbers: &[usize]) -> String {
    let mut sorted = numbers.to_vec();
    sorted.sort_unstable();
    sorted.dedup();
    let mut parts: Vec<String> = Vec::new();
    let mut start = 0;
    for i in 0..sorted.len() {
        if i + 1 == sorted.len() || sorted[i + 1] != sorted[i] + 1 {
            parts.push(if start == i {
                sorted[i].to_string()
            } else {
                format!("{}–{}", sorted[start], sorted[i])
            });
            start = i + 1;
        }
    }
    parts.join(", ")
}

fn key(file: &FileReview) -> (String, u64) {
    (file.path.clone(), file.fingerprint)
}

/// FNV-1a, 64-bit: stable across Rust releases, unlike the standard library's hasher.
pub(crate) fn fingerprint(parts: &[&str]) -> u64 {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for (i, part) in parts.iter().enumerate() {
        if i > 0 {
            hash ^= 0xff;
            hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
        }
        for byte in part.bytes() {
            hash ^= u64::from(byte);
            hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
        }
    }
    hash
}