wsb 0.153.17

Workspace - A tool suite for file operations, version management, and development workflow automation
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
use crate::fleet::config::GitModules;
use crate::fleet::error::{Result, SuperGitError};
use crate::fleet::output::Formatter;
use crate::git_backend;
use gix::bstr::ByteSlice;
use std::fs;
use std::path::{Path, PathBuf};

/// Generate per-file diff files for a single git repo.
///
/// Behavior mirrors the bash `gitdiff_per_file` function:
/// - No base: diffs unstaged changes. With `--staged`, also appends staged diffs.
/// - With base: diffs against the given base ref (commit, branch, tag).
///
/// Each changed file `path/to/file.rs` gets a corresponding `outdir/path/to/file.rs.diff`.
pub fn generate_per_file_diffs(
    repo_path: &Path,
    base: Option<&str>,
    outdir: &Path,
    include_staged: bool,
) -> Result<Vec<String>> {
    let repo = git_backend::open_repo(repo_path).map_err(|err| SuperGitError::Other(err.to_string()))?;
    let mut written_files: Vec<String> = Vec::new();

    match base {
        Some(base_ref) => {
            let names = changed_files_against_base(&repo, base_ref)?;
            for file in &names {
                let diff = diff_against_base(&repo, base_ref, file)?;
                if !diff.is_empty() {
                    write_diff_file(outdir, file, &diff, false)?;
                    written_files.push(file.clone());
                }
            }
        }
        None => {
            let names = unstaged_changed_files(&repo)?;
            for file in &names {
                let diff = diff_index_to_worktree(&repo, file)?;
                if !diff.is_empty() {
                    write_diff_file(outdir, file, &diff, false)?;
                    written_files.push(file.clone());
                }
            }

            if include_staged {
                let staged_names = staged_changed_files(&repo)?;
                for file in &staged_names {
                    let diff = diff_head_to_index(&repo, file)?;
                    if !diff.is_empty() {
                        let is_append = written_files.contains(file);
                        write_diff_file(outdir, file, &diff, is_append)?;
                        if !written_files.contains(file) {
                            written_files.push(file.clone());
                        }
                    }
                }
            }
        }
    }

    Ok(written_files)
}

fn unstaged_changed_files(repo: &gix::Repository) -> Result<Vec<String>> {
    let mut files = Vec::new();
    for item in repo
        .status(gix::progress::Discard)
        .map_err(|err| SuperGitError::Other(err.to_string()))?
        .into_iter(Vec::<gix::bstr::BString>::new())
        .map_err(|err| SuperGitError::Other(err.to_string()))?
    {
        match item.map_err(|err| SuperGitError::Other(err.to_string()))? {
            gix::status::Item::IndexWorktree(entry) => {
                let path = match entry {
                    gix::status::index_worktree::Item::Modification { rela_path, .. } => {
                        rela_path.to_string()
                    }
                    gix::status::index_worktree::Item::DirectoryContents { entry, .. } => {
                        entry.rela_path.to_string()
                    }
                    gix::status::index_worktree::Item::Rewrite {
                        dirwalk_entry, ..
                    } => dirwalk_entry.rela_path.to_string(),
                };
                if !files.contains(&path) {
                    files.push(path);
                }
            }
            gix::status::Item::TreeIndex(_) => {}
        }
    }
    Ok(files)
}

fn staged_changed_files(repo: &gix::Repository) -> Result<Vec<String>> {
    let mut files = Vec::new();
    for item in repo
        .status(gix::progress::Discard)
        .map_err(|err| SuperGitError::Other(err.to_string()))?
        .into_iter(Vec::<gix::bstr::BString>::new())
        .map_err(|err| SuperGitError::Other(err.to_string()))?
    {
        if let gix::status::Item::TreeIndex(change) =
            item.map_err(|err| SuperGitError::Other(err.to_string()))?
        {
            let path = change.location().to_string();
            if !files.contains(&path) {
                files.push(path);
            }
        }
    }
    Ok(files)
}

fn changed_files_against_base(repo: &gix::Repository, base_ref: &str) -> Result<Vec<String>> {
    let base_commit = repo
        .rev_parse_single(base_ref)
        .map_err(|err| SuperGitError::Other(err.to_string()))?
        .object()
        .map_err(|err| SuperGitError::Other(err.to_string()))?
        .peel_to_commit()
        .map_err(|err| SuperGitError::Other(err.to_string()))?;
    let head_commit = repo
        .head_commit()
        .map_err(|err| SuperGitError::Other(err.to_string()))?;
    let base_tree = base_commit
        .tree()
        .map_err(|err| SuperGitError::Other(err.to_string()))?;
    let head_tree = head_commit
        .tree()
        .map_err(|err| SuperGitError::Other(err.to_string()))?;
    let changes = repo
        .diff_tree_to_tree(&base_tree, &head_tree, None)
        .map_err(|err| SuperGitError::Other(err.to_string()))?;

    let mut files = Vec::new();
    for change in changes {
        let path = match change {
            gix::diff::tree_with_rewrites::Change::Addition { location, .. }
            | gix::diff::tree_with_rewrites::Change::Deletion { location, .. }
            | gix::diff::tree_with_rewrites::Change::Modification { location, .. }
            | gix::diff::tree_with_rewrites::Change::Rewrite { location, .. } => {
                location.to_string()
            }
        };
        if !files.contains(&path) {
            files.push(path);
        }
    }
    Ok(files)
}

fn diff_against_base(repo: &gix::Repository, base_ref: &str, file: &str) -> Result<String> {
    diff_between_sources(
        repo,
        &DiffSource::TreePath {
            treeish: base_ref.to_string(),
            path: file.to_string(),
        },
        &DiffSource::TreePath {
            treeish: "HEAD".to_string(),
            path: file.to_string(),
        },
    )
}

fn diff_head_to_index(repo: &gix::Repository, file: &str) -> Result<String> {
    diff_between_sources(
        repo,
        &DiffSource::TreePath {
            treeish: "HEAD".to_string(),
            path: file.to_string(),
        },
        &DiffSource::IndexPath(file.to_string()),
    )
}

fn diff_index_to_worktree(repo: &gix::Repository, file: &str) -> Result<String> {
    diff_between_sources(
        repo,
        &DiffSource::IndexPath(file.to_string()),
        &DiffSource::WorktreePath(file.to_string()),
    )
}

enum DiffSource {
    TreePath { treeish: String, path: String },
    IndexPath(String),
    WorktreePath(String),
}

fn diff_between_sources(repo: &gix::Repository, old: &DiffSource, new: &DiffSource) -> Result<String> {
    let old_data = read_diff_source(repo, old)?;
    let new_data = read_diff_source(repo, new)?;

    let interner = gix::diff::blob::intern::InternedInput::new(
        gix::diff::blob::sources::byte_lines(&old_data),
        gix::diff::blob::sources::byte_lines(&new_data),
    );
    let unified = gix::diff::blob::UnifiedDiff::new(
        &interner,
        gix::diff::blob::unified_diff::ConsumeBinaryHunk::new(gix::bstr::BString::default(), "\n"),
        gix::diff::blob::unified_diff::ContextSize::symmetrical(3),
    );
    let diff = gix::diff::blob::diff(gix::diff::blob::Algorithm::Histogram, &interner, unified)
        .map_err(|err| SuperGitError::Other(err.to_string()))?;
    Ok(String::from_utf8_lossy(diff.as_bytes()).into_owned())
}

fn read_diff_source(repo: &gix::Repository, source: &DiffSource) -> Result<Vec<u8>> {
    match source {
        DiffSource::WorktreePath(path) => {
            let full_path = repo
                .workdir()
                .ok_or_else(|| SuperGitError::Other("Bare repositories are not supported".to_string()))?
                .join(path);
            match fs::read(&full_path) {
                Ok(bytes) => Ok(bytes),
                Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()),
                Err(err) => Err(SuperGitError::Other(format!(
                    "Failed to read '{}': {}",
                    full_path.display(),
                    err
                ))),
            }
        }
        DiffSource::IndexPath(path) => {
            let index = repo
                .index_or_load_from_head_or_empty()
                .map_err(|err| SuperGitError::Other(err.to_string()))?
                .into_owned();
            let entry = index
                .entry_by_path(path.as_bytes().as_bstr())
                .ok_or_else(|| SuperGitError::Other(format!("Path '{}' not found in index", path)))?;
            if entry.id.is_null() {
                return Ok(Vec::new());
            }
            repo.find_object(entry.id)
                .map_err(|err| SuperGitError::Other(err.to_string()))
                .map(|obj| obj.data.clone())
        }
        DiffSource::TreePath { treeish, path } => {
            let tree = repo
                .rev_parse_single(treeish.as_str())
                .map_err(|err| SuperGitError::Other(err.to_string()))?
                .object()
                .map_err(|err| SuperGitError::Other(err.to_string()))?
                .peel_to_tree()
                .map_err(|err| SuperGitError::Other(err.to_string()))?;
            let entry = tree
                .lookup_entry_by_path(PathBuf::from(path))
                .map_err(|err| SuperGitError::Other(err.to_string()))?;
            let Some(entry) = entry else {
                return Ok(Vec::new());
            };
            repo.find_object(entry.id())
                .map_err(|err| SuperGitError::Other(err.to_string()))
                .map(|obj| obj.data.clone())
        }
    }
}

fn write_diff_file(outdir: &Path, file: &str, diff: &str, append: bool) -> Result<()> {
    let diff_path = outdir.join(format!("{}.diff", file));
    if let Some(parent) = diff_path.parent() {
        fs::create_dir_all(parent).map_err(|e| {
            SuperGitError::Other(format!(
                "Failed to create directory '{}': {}",
                parent.display(),
                e
            ))
        })?;
    }

    if append {
        use std::io::Write;
        let mut f = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&diff_path)
            .map_err(|e| {
                SuperGitError::Other(format!(
                    "Failed to open '{}' for appending: {}",
                    diff_path.display(),
                    e
                ))
            })?;
        f.write_all(diff.as_bytes()).map_err(|e| {
            SuperGitError::Other(format!(
                "Failed to write to '{}': {}",
                diff_path.display(),
                e
            ))
        })?;
    } else {
        fs::write(&diff_path, diff).map_err(|e| {
            SuperGitError::Other(format!("Failed to write '{}': {}", diff_path.display(), e))
        })?;
    }

    Ok(())
}

/// Fleet diff: generate per-file diffs for superproject + all submodules.
/// Each repo's diffs go into `outdir/<repo_name>/`.
pub fn execute<P: AsRef<Path>>(
    repo_root: P,
    base: Option<&str>,
    outdir: &str,
    include_staged: bool,
    verbose: bool,
) -> Result<()> {
    let repo_root = repo_root.as_ref();
    let gitmodules = match GitModules::parse(repo_root) {
        Ok(gitmodules) => gitmodules,
        Err(SuperGitError::NoSubmodules) => GitModules {
            entries: Vec::new(),
            submodules: Vec::new(),
        },
        Err(err) => return Err(err),
    };
    let outdir_path = Path::new(outdir);

    Formatter::print_info(&format!(
        "Generating per-file diffs for superproject + {} submodule(s) into '{}'",
        gitmodules.submodules.len(),
        outdir
    ));

    let mut total_files = 0;
    let mut failed = 0;

    // Superproject
    {
        let super_outdir = outdir_path.join("_superproject");
        if verbose {
            Formatter::print_submodule_header("[SUPERPROJECT]");
        }
        match generate_per_file_diffs(repo_root, base, &super_outdir, include_staged) {
            Ok(files) => {
                if files.is_empty() {
                    if verbose {
                        Formatter::print_info("No changes");
                    }
                } else {
                    Formatter::print_success(&format!(
                        "Superproject: {} diff file(s) written",
                        files.len()
                    ));
                    total_files += files.len();
                }
            }
            Err(e) => {
                Formatter::print_error(&format!("Superproject diff failed: {}", e));
                failed += 1;
            }
        }
    }

    // Submodules
    for submodule in &gitmodules.submodules {
        let submodule_path = repo_root.join(&submodule.path);

        if !submodule_path.exists() {
            Formatter::print_warning(&format!(
                "Submodule '{}' not initialized, skipping",
                submodule.name
            ));
            continue;
        }

        let sub_outdir = outdir_path.join(&submodule.name);
        if verbose {
            Formatter::print_submodule_header(&submodule.name);
        }

        match generate_per_file_diffs(&submodule_path, base, &sub_outdir, include_staged) {
            Ok(files) => {
                if files.is_empty() {
                    if verbose {
                        Formatter::print_info(&format!("No changes in '{}'", submodule.name));
                    }
                } else {
                    Formatter::print_success(&format!(
                        "'{}': {} diff file(s) written",
                        submodule.name,
                        files.len()
                    ));
                    total_files += files.len();
                }
            }
            Err(e) => {
                Formatter::print_error(&format!("Diff failed for '{}': {}", submodule.name, e));
                failed += 1;
            }
        }
    }

    if total_files > 0 || failed == 0 {
        Formatter::print_success(&format!(
            "Total: {} diff file(s) written to '{}'",
            total_files, outdir
        ));
    }

    if failed > 0 {
        return Err(SuperGitError::Other(format!(
            "{} repo(s) failed to generate diffs",
            failed
        )));
    }

    Ok(())
}