proj-janitor 0.4.0

Scan, back up, restore, prune, and clean old projects safely
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
use chrono::{Local, Utc};
use sha2::{Digest, Sha256};
use std::{
    collections::{HashMap, HashSet},
    fs,
    path::{Path, PathBuf},
    process::{Command, Stdio},
};

use crate::{
    Manifest, Record,
    archive::{
        archive_path_matches, copy_new_file, create_archive, create_private_dir,
        create_private_temp_dir, portable_path_key, remove_file_if_same, safe_windows_component,
        write_new_file,
    },
    expand, human,
    project::{discover, git_compatible_path, inspect_bundle_recovery, inspect_recovery},
    resolve_for_creation,
};

const VERSION: &str = env!("CARGO_PKG_VERSION");

fn blocked_backup_reason(reason: &str) -> bool {
    [
        "remote URL contains credentials or query data",
        "submodule URL contains credentials or query data",
        "Git config contains HTTP extraHeader",
        "Git config contains credential passwords",
        "Git config contains an inline credential helper",
        "Git config contains sensitive credential helper arguments",
        "Git config contains a custom SSH command",
        "Git config points to a credential file inside the project",
        "Git config contains credentials or query data in a service URL",
        "Git config contains credentials in a URL rewrite",
        "LFS config contains credentials or query data",
        "cannot safely inspect LFS config for credentials",
        "cannot safely inspect .gitmodules for credentials",
        "cannot safely inspect Git config for credentials",
    ]
    .iter()
    .any(|prefix| reason.starts_with(prefix))
}

fn create_git_bundle(
    project: &Path,
    bundle: &Path,
) -> anyhow::Result<(String, u64, same_file::Handle)> {
    let temporary = create_private_temp_dir(
        bundle
            .parent()
            .ok_or_else(|| anyhow::anyhow!("bundle has no parent"))?,
    )?;
    let temporary_bundle = temporary.join("bundle");
    let result = (|| -> anyhow::Result<_> {
        let output = Command::new("git")
            .arg("-C")
            .arg(project)
            .args(["bundle", "create"])
            .arg(git_compatible_path(&temporary_bundle))
            .arg("--all")
            .stdin(Stdio::null())
            .output()?;
        anyhow::ensure!(
            output.status.success(),
            "git bundle creation failed: {}",
            String::from_utf8_lossy(&output.stderr).trim()
        );
        Ok(copy_new_file(&temporary_bundle, bundle)?)
    })();
    let _ = fs::remove_dir_all(&temporary);
    result
}

pub(crate) fn backup_cmd(
    roots: &[PathBuf],
    dest: Option<PathBuf>,
    dry: bool,
    include_artifacts: bool,
    always: bool,
    exclude_git_metadata: bool,
) -> anyhow::Result<()> {
    let dest = dest
        .filter(|path| !path.as_os_str().is_empty())
        .ok_or_else(|| anyhow::anyhow!("set BACKUP_DEST or pass --backup-dest"))?;
    let dest = resolve_for_creation(&expand(&dest))?;
    if dest.exists() && !dest.is_dir() {
        anyhow::bail!("destination is not a directory: {}", dest.display())
    }
    let mut projects = Vec::new();
    let mut seen = HashSet::new();
    let mut failed = false;
    for root in roots {
        match discover(root, false) {
            Ok(ps) => {
                for p in ps {
                    if seen.insert(p.path.clone()) {
                        projects.push(p)
                    }
                }
            }
            Err(e) => {
                eprintln!("error: {e}");
                failed = true
            }
        }
    }
    if projects.is_empty() {
        anyhow::bail!("No projects found")
    }
    for project in &projects {
        anyhow::ensure!(
            safe_windows_component(&project.name),
            "unsafe project name: {}",
            project.name
        );
    }
    let mut name_counts = HashMap::new();
    for project in &projects {
        *name_counts
            .entry(portable_path_key(&project.name))
            .or_insert(0) += 1;
    }
    let project_dirs: Vec<_> = projects
        .iter()
        .map(|project| {
            if name_counts[&portable_path_key(&project.name)] > 1 {
                let path_hash = format!(
                    "{:x}",
                    Sha256::digest(project.path.to_string_lossy().as_bytes())
                );
                format!("{}-{}", project.name, &path_hash[..8])
            } else {
                project.name.clone()
            }
        })
        .collect();
    for (project, dir_name) in projects.iter().zip(&project_dirs) {
        let output_dir = resolve_for_creation(&dest.join(dir_name))?;
        anyhow::ensure!(
            !output_dir.starts_with(&project.path),
            "backup destination is inside project: {} -> {}",
            output_dir.display(),
            project.path.display()
        );
    }
    if !dry {
        create_private_dir(&dest)?;
    }
    let timestamp = Local::now().format("%Y%m%d-%H%M%S").to_string();
    for (p, dir_name) in projects.iter().zip(&project_dirs) {
        let dir = dest.join(dir_name);
        let mut run_stamp = timestamp.clone();
        let mut suffix = 2;
        while dir.join(format!("manifest-{run_stamp}.json")).exists()
            || dir.join(format!("{}-{run_stamp}.tar.gz", p.name)).exists()
            || dir.join(format!("{}-{run_stamp}.bundle", p.name)).exists()
        {
            run_stamp = format!("{timestamp}-{suffix}");
            suffix += 1;
        }
        let manifest_path = dir.join(format!("manifest-{run_stamp}.json"));
        let mut archive_path = dir.join(format!("{}-{run_stamp}.tar.gz", p.name));
        let bundle_path = dir.join(format!("{}-{run_stamp}.bundle", p.name));
        let (recovery, bundle_recovery, fallback_reason) = if include_artifacts {
            (None, None, Some("include_artifacts".to_owned()))
        } else if always {
            (None, None, Some("always_archive".to_owned()))
        } else {
            match inspect_recovery(p) {
                Ok(recovery) => (Some(recovery), None, None),
                Err(reason) => {
                    if exclude_git_metadata || blocked_backup_reason(&reason) {
                        (None, None, Some(reason))
                    } else {
                        match inspect_bundle_recovery(p) {
                            Ok(bundle) => (None, Some(bundle), Some(reason)),
                            Err(_) => (None, None, Some(reason)),
                        }
                    }
                }
            }
        };
        let blocked = !include_artifacts
            && !always
            && bundle_recovery.is_none()
            && fallback_reason
                .as_deref()
                .is_some_and(blocked_backup_reason);
        if blocked {
            let reason = fallback_reason.as_deref().unwrap_or_default();
            if dry {
                println!("[error] {}: {reason}", p.name);
            } else {
                eprintln!("error: {}: {reason}", p.name);
            }
            failed = true;
            continue;
        }
        if dry {
            if let Some(git) = &recovery {
                println!(
                    "[git] {}: {} @ {}",
                    p.name,
                    git.remote_url,
                    &git.commit[..12.min(git.commit.len())]
                );
            } else if let Some(bundle) = &bundle_recovery {
                let reason = fallback_reason
                    .as_deref()
                    .map(|reason| format!(" ({reason})"))
                    .unwrap_or_default();
                println!(
                    "[git-bundle] {}: {} -> {}{}",
                    p.name,
                    &bundle.commit[..12.min(bundle.commit.len())],
                    bundle_path.display(),
                    reason
                );
            } else {
                let reason = fallback_reason
                    .as_deref()
                    .map(|reason| format!(" ({reason})"))
                    .unwrap_or_default();
                println!(
                    "[archive] {}: {} -> {}{}{}",
                    p.name,
                    human(p.size_bytes),
                    archive_path.display(),
                    reason,
                    if exclude_git_metadata {
                        " [Git metadata exclusion enabled; restored archive has no Git metadata]"
                    } else {
                        ""
                    }
                );
            }
            continue;
        }
        let mut archive_identity = None;
        let mut manifest_identity = None;
        let result = (|| -> anyhow::Result<Record> {
            create_private_dir(&dir)?;
            if let Some(git) = &recovery {
                return Ok(Record {
                    project: p.name.clone(),
                    source: p.path.to_string_lossy().into(),
                    archive: None,
                    archive_bytes: 0,
                    source_bytes: p.size_bytes,
                    sha256: None,
                    strategy: "git".into(),
                    remote: Some(git.remote.clone()),
                    remote_url: Some(git.remote_url.clone()),
                    branch: Some(git.branch.clone()),
                    commit: Some(git.commit.clone()),
                    upstream: Some(git.upstream.clone()),
                    verified: Some("local-ref".into()),
                    fallback_reason: None,
                });
            }
            if let Some(bundle) = &bundle_recovery {
                let (hash, bytes, identity) = create_git_bundle(&p.path, &bundle_path)?;
                archive_identity = Some(identity);
                archive_path.clone_from(&bundle_path);
                Ok(Record {
                    project: p.name.clone(),
                    source: p.path.to_string_lossy().into(),
                    archive: Some(bundle_path.to_string_lossy().into()),
                    archive_bytes: bytes,
                    source_bytes: p.size_bytes,
                    sha256: Some(hash),
                    strategy: "git_bundle".into(),
                    remote: None,
                    remote_url: None,
                    branch: Some(bundle.branch.clone()),
                    commit: Some(bundle.commit.clone()),
                    upstream: None,
                    verified: Some("local-bundle".into()),
                    fallback_reason: fallback_reason.clone(),
                })
            } else {
                let (hash, bytes, identity) = create_archive(
                    &p.path,
                    &archive_path,
                    include_artifacts,
                    exclude_git_metadata,
                )?;
                archive_identity = Some(identity);
                Ok(Record {
                    project: p.name.clone(),
                    source: p.path.to_string_lossy().into(),
                    archive: Some(archive_path.to_string_lossy().into()),
                    archive_bytes: bytes,
                    source_bytes: p.size_bytes,
                    sha256: Some(hash),
                    strategy: "archive".into(),
                    remote: None,
                    remote_url: None,
                    branch: None,
                    commit: None,
                    upstream: None,
                    verified: None,
                    fallback_reason: fallback_reason.clone(),
                })
            }
        })();
        match result {
            Ok(record) => {
                let manifest = Manifest {
                    schema_version: 2,
                    created_at: Utc::now().to_rfc3339(),
                    tool: "proj-janitor".into(),
                    version: VERSION.into(),
                    include_artifacts,
                    always_archive: always,
                    exclude_git_metadata: exclude_git_metadata && record.strategy == "archive",
                    records: vec![record.clone()],
                };
                let write_result = (|| -> anyhow::Result<()> {
                    if let Some(identity) = &archive_identity {
                        anyhow::ensure!(
                            archive_path_matches(&archive_path, identity),
                            "archive path changed before manifest write: {}",
                            archive_path.display()
                        );
                    }
                    let mut bytes = serde_json::to_vec_pretty(&manifest)?;
                    bytes.push(b'\n');
                    manifest_identity = Some(write_new_file(&manifest_path, &bytes)?);
                    if let Some(identity) = &archive_identity {
                        anyhow::ensure!(
                            archive_path_matches(&archive_path, identity),
                            "archive path changed while manifest was written: {}",
                            archive_path.display()
                        );
                    }
                    Ok(())
                })();
                if let Err(e) = write_result {
                    if let Some(identity) = manifest_identity.take() {
                        remove_file_if_same(&manifest_path, identity);
                    }
                    if let Some(identity) = archive_identity.take() {
                        remove_file_if_same(&archive_path, identity);
                    }
                    eprintln!("error: {}: cannot write manifest: {e}", p.name);
                    failed = true;
                    continue;
                }
                drop(manifest_identity.take());
                if record.strategy == "git" {
                    println!(
                        "[git] {}: {} @ {}",
                        record.project,
                        record.remote_url.as_deref().unwrap_or("-"),
                        &record.commit.as_deref().unwrap_or_default()
                            [..12.min(record.commit.as_deref().unwrap_or_default().len())]
                    );
                } else if record.strategy == "git_bundle" {
                    let reason = record
                        .fallback_reason
                        .as_deref()
                        .map(|reason| format!(" ({reason})"))
                        .unwrap_or_default();
                    println!(
                        "[git-bundle] {}: {} {} sha256:{} {}{}",
                        record.project,
                        &record.commit.as_deref().unwrap_or_default()
                            [..12.min(record.commit.as_deref().unwrap_or_default().len())],
                        human(record.archive_bytes),
                        &record.sha256.as_deref().unwrap_or_default()[..12],
                        record.archive.as_deref().unwrap_or_default(),
                        reason
                    );
                } else {
                    let reason = record
                        .fallback_reason
                        .as_deref()
                        .map(|reason| format!(" ({reason})"))
                        .unwrap_or_default();
                    println!(
                        "[archive] {}: {} sha256:{}{}{}",
                        record.project,
                        human(record.archive_bytes),
                        &record.sha256.as_deref().unwrap_or_default()[..12],
                        reason,
                        if exclude_git_metadata {
                            " [Git metadata exclusion enabled; restored archive has no Git metadata]"
                        } else {
                            ""
                        }
                    );
                }
                println!("Manifest: {}", manifest_path.display());
            }
            Err(e) => {
                if let Some(identity) = archive_identity.take() {
                    remove_file_if_same(&archive_path, identity);
                }
                eprintln!("error: {}: {e}", p.name);
                failed = true
            }
        }
    }
    if failed {
        anyhow::bail!("one or more backup operations failed")
    }
    Ok(())
}