bendis 0.5.6

A patch tool for Bender to work better in HERIS project
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
use anyhow::{bail, Context, Result};
use colored::Colorize;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::process::{Command, Stdio};

use super::hardening::{self, HardeningStatus};
use crate::converter::format;
use crate::utils::config::{self, BendisConfig};

pub fn run(hard: bool) -> Result<()> {
    // Check for legacy structure and offer migration
    if config::check_and_migrate_if_needed()? {
        // Migration was performed or user declined
        // If migration was successful, continue with update
        // If user declined, exit
        if !config::get_bendis_dir().exists() {
            // User declined migration and no new structure exists
            bail!(
                "error: bendis_workspace directory not found\nrun `bendis init` to migrate or initialize the project"
            );
        }
    }

    println!("Updating dependencies...");

    // Load configuration
    let cfg = BendisConfig::load()?;

    let bendis_dir = config::get_bendis_dir();
    let root_dir = config::get_root_dir();

    // Check if bendis_workspace directory exists
    if !bendis_dir.exists() {
        bail!(
            "error: bendis_workspace directory not found\nrun `bendis init` first to initialize the project"
        );
    }

    // Check bendis_workspace/.gitignore if enabled in config
    let mut gitignore_warning = None;
    if cfg.gitignore_check == 1 {
        if let Err(e) = config::check_bendis_gitignore() {
            gitignore_warning = Some(e.to_string());
        }
    }

    // Step 1: Copy hw/ and target/ directories from root to bendis_workspace/
    println!("  Syncing hw/ and target/ directories to bendis_workspace/...");
    config::copy_root_dirs_to_bendis_workspace()?;

    // Restore the authoritative workspace override before Bender searches parent configs.
    restore_root_override_for_workspace_update(&bendis_dir, &root_dir)?;

    // Step 2: Run bender update in bendis_workspace directory
    println!("  Preparing cache files (it may take long)...");
    run_bender_update_in_bendis(&bendis_dir, cfg.silent_mode == 1)?;

    // Step 3: Run format converter
    println!("  Converting URLs...");
    let configs_changed = format::convert(&bendis_dir, &root_dir)?;

    // Step 4: Ensure bendis_workspace/.gitignore has required entries (hw/, target/)
    println!("  Updating bendis_workspace/.gitignore...");
    config::ensure_bendis_workspace_gitignore_entries()?;

    // Step 5: Ensure root .gitignore does not contain hw/, target/
    println!("  Updating root .gitignore...");
    config::ensure_root_gitignore_excludes_hw_target()?;

    // Step 6: Materialize the converted exact lock without resolving versions again.
    println!("  Checking for changes...");
    if configs_changed {
        println!("  Detected changes, checking out dependencies...");
    } else {
        println!("  No changes detected, checking local dependency cache...");
    }
    run_bender_checkout_in_root(&root_dir, Path::new("bender"))?;

    // Step 7: Clean up based on storage_saving_mode
    println!("  Cleaning up cache...");
    cleanup_bendis_workspace_bender_dir(&bendis_dir, cfg.storage_saving_mode == 1)?;

    // Step 8: Verify
    verify_completion(&root_dir)?;

    // Display gitignore warning if there was an issue
    if let Some(warning_msg) = gitignore_warning {
        eprintln!("\n{}", "warning:".yellow().bold());
        eprintln!("  {}", warning_msg);
        eprintln!("  Some files in bendis_workspace/ may be tracked by git unexpectedly.");
        eprintln!("  Please review bendis_workspace/.gitignore and ensure it contains the required entries.");
        eprintln!(
            "  To disable this check, set 'gitignore_check = 0' in: {}",
            "bendis config".cyan()
        );
    }

    if hard {
        println!("Preparing AegisRTL workspace...");
        let result = match hardening::run(&root_dir) {
            Ok(result) => result,
            Err(error) => {
                eprintln!("Hardening failed; local RTL dependencies were not activated");
                return Err(error);
            }
        };
        for message in hardening_completion_messages(&result) {
            println!("{message}");
        }
    }

    println!("Done");

    Ok(())
}

fn hardening_completion_messages(result: &hardening::HardeningResult) -> Vec<String> {
    match result.status {
        HardeningStatus::Completed => vec![
            format!(
                "Hardening completed. Changed RTL files: {}",
                result.changed_files.len()
            ),
            "Hardened RTL dependencies activated".to_string(),
        ],
        HardeningStatus::Skipped => vec![
            "Hardening skipped: aegisrtl/scripts/harden.sh was not found".to_string(),
            "Local RTL dependencies activated without hardening".to_string(),
        ],
    }
}

fn restore_root_override_for_workspace_update(bendis_dir: &Path, root_dir: &Path) -> Result<()> {
    let source = bendis_dir.join(".bender.yml");
    if !source.is_file() {
        bail!("error: bendis_workspace/.bender.yml not found");
    }
    fs::copy(&source, root_dir.join(".bender.yml"))
        .context("Failed to restore root .bender.yml from bendis_workspace")?;
    Ok(())
}

fn run_bender_update_in_bendis(bendis_dir: &Path, silent: bool) -> Result<()> {
    let mut cmd = Command::new("bender");
    cmd.args(&["-d", "./bendis_workspace", "update"]);

    // Always capture output for error logging
    cmd.stdout(Stdio::piped());
    cmd.stderr(Stdio::piped());

    let mut child = cmd
        .spawn()
        .context("Failed to run bender. Is bender installed and in PATH?")?;

    // Capture stdout and stderr
    let stdout = child.stdout.take().unwrap();
    let stderr = child.stderr.take().unwrap();

    let mut stdout_reader = BufReader::new(stdout);
    let mut stderr_reader = BufReader::new(stderr);

    let mut log_buffer = Vec::new();
    let mut line = String::new();

    // Read stdout
    while stdout_reader.read_line(&mut line).unwrap_or(0) > 0 {
        if !silent {
            print!("{}", line);
        }
        log_buffer.push(line.clone());
        line.clear();
    }

    // Read stderr
    while stderr_reader.read_line(&mut line).unwrap_or(0) > 0 {
        if !silent {
            eprint!("{}", line);
        }
        log_buffer.push(line.clone());
        line.clear();
    }

    let status = child.wait().context("Failed to wait for bender process")?;

    if !status.success() {
        // Always display full log on error, regardless of silent mode
        eprintln!(
            "\n{}",
            "Error: bender update failed in bendis_workspace/".red()
        );
        eprintln!("\nCommand output:");
        for log_line in &log_buffer {
            eprint!("{}", log_line);
        }

        // Check for specific fetch/version error pattern
        let has_fetch_error = log_buffer.iter().any(|line| {
            line.contains("Fetching Dependency")
                && line.contains("cannot satisfy requirement")
                && line.contains("may need fetch")
        });

        if has_fetch_error {
            eprintln!("\n{}", "Possible solution:".yellow());
            eprintln!(
                "  1. Delete {} directory",
                "bendis_workspace/.bender/".cyan()
            );
            eprintln!("  2. Check your internet connection");
            eprintln!("  3. Run {} again", "bendis update".cyan());
        }

        bail!("error: bender update in bendis_workspace/ failed");
    }

    // Check if Bender.lock was created
    let lock_file = bendis_dir.join("Bender.lock");
    if !lock_file.exists() {
        bail!("error: failed to generate bendis_workspace/Bender.lock");
    }

    Ok(())
}

fn run_bender_checkout_in_root(root_dir: &Path, bender: &Path) -> Result<()> {
    let local = Command::new(bender)
        .args(["--local", "--git-submodules", "false", "checkout"])
        .current_dir(root_dir)
        .output()
        .context("Failed to run bender. Is bender installed and in PATH?")?;
    if local.status.success() && checkouts_have_complete_submodules(root_dir)? {
        return Ok(());
    }

    println!("  Local checkout could not be completed; retrying with remote access");
    let remote = Command::new(bender)
        .arg("checkout")
        .current_dir(root_dir)
        .output()
        .context("Failed to retry bender checkout with remote access")?;
    if !remote.status.success() {
        eprint!("{}", String::from_utf8_lossy(&remote.stdout));
        eprint!("{}", String::from_utf8_lossy(&remote.stderr));
        bail!("bender checkout failed in root directory");
    }
    if !checkouts_have_complete_submodules(root_dir)? {
        bail!("bender checkout did not produce complete dependency submodules");
    }
    Ok(())
}

fn checkouts_have_complete_submodules(root_dir: &Path) -> Result<bool> {
    let checkouts = root_dir.join(".bender/git/checkouts");
    if !checkouts.is_dir() {
        return Ok(true);
    }
    for entry in fs::read_dir(&checkouts)
        .with_context(|| format!("Failed to read {}", checkouts.display()))?
    {
        let path = entry?.path();
        if !path.is_dir() || !path.join(".git").exists() {
            continue;
        }
        let submodules = Command::new("git")
            .args(["submodule", "status", "--recursive"])
            .current_dir(&path)
            .output()
            .with_context(|| format!("Failed to inspect submodules in {}", path.display()))?;
        if !submodules.status.success()
            || submodules
                .stdout
                .split(|byte| *byte == b'\n')
                .filter(|line| !line.is_empty())
                .any(|line| line.first() != Some(&b' '))
        {
            return Ok(false);
        }
    }
    Ok(true)
}

fn cleanup_bendis_workspace_bender_dir(bendis_dir: &Path, full_cleanup: bool) -> Result<()> {
    if full_cleanup {
        // storage_saving_mode = 1: delete entire bendis_workspace/.bender/
        let bender_dir = bendis_dir.join(".bender");
        if bender_dir.exists() {
            fs::remove_dir_all(&bender_dir)
                .context("Failed to remove bendis_workspace/.bender/")?;
        }
    } else {
        // storage_saving_mode = 0: don't delete anything (keep cache)
        // Do nothing
    }

    Ok(())
}

fn verify_completion(root_dir: &Path) -> Result<()> {
    let mut missing = Vec::new();

    // Check required files
    if !root_dir.join("Bender.yml").exists() {
        missing.push("Bender.yml");
    }
    if !root_dir.join(".bender.yml").exists() {
        missing.push(".bender.yml");
    }
    if !root_dir.join("Bender.lock").exists() {
        missing.push("Bender.lock");
    }
    if !root_dir.join(".bender").exists() {
        missing.push(".bender/");
    }

    if !missing.is_empty() {
        bail!(
            "error: verification failed, missing: {}",
            missing.join(", ")
        );
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{
        hardening_completion_messages, restore_root_override_for_workspace_update,
        run_bender_checkout_in_root,
    };
    use crate::commands::hardening::{HardeningResult, HardeningStatus};
    use std::fs;
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn workspace_update_restores_root_override_before_running_bender() {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root: PathBuf = std::env::temp_dir().join(format!("bendis-restore-{nonce}"));
        let workspace = root.join("bendis_workspace");
        fs::create_dir_all(&workspace).unwrap();
        fs::write(
            root.join(".bender.yml"),
            "overrides:\n  core: { path: '../aegisrtl/core' }\n",
        )
        .unwrap();
        fs::write(
            workspace.join(".bender.yml"),
            "overrides:\n  core: { git: 'ssh://ihep/core.git', rev: abc }\n",
        )
        .unwrap();

        restore_root_override_for_workspace_update(&workspace, &root).unwrap();

        assert_eq!(
            fs::read_to_string(root.join(".bender.yml")).unwrap(),
            fs::read_to_string(workspace.join(".bender.yml")).unwrap()
        );
        fs::remove_dir_all(root).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn root_checkout_uses_local_cache_without_removing_it() {
        use std::os::unix::fs::PermissionsExt;

        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = std::env::temp_dir().join(format!("bendis-root-checkout-{nonce}"));
        let bin = root.join("fake-bender");
        let log = root.join("calls.log");
        fs::create_dir_all(root.join(".bender/git/db/core")).unwrap();
        fs::write(root.join(".bender/git/db/core/marker"), "keep\n").unwrap();
        fs::write(
            &bin,
            format!("#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{}'\n", log.display()),
        )
        .unwrap();
        let mut permissions = fs::metadata(&bin).unwrap().permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&bin, permissions).unwrap();

        run_bender_checkout_in_root(&root, &bin).unwrap();

        assert_eq!(
            fs::read_to_string(&log).unwrap(),
            "--local --git-submodules false checkout\n"
        );
        assert!(root.join(".bender/git/db/core/marker").is_file());
        fs::remove_dir_all(root).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn root_checkout_retries_once_with_remote_access() {
        use std::os::unix::fs::PermissionsExt;

        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = std::env::temp_dir().join(format!("bendis-root-retry-{nonce}"));
        let bin = root.join("fake-bender");
        let log = root.join("calls.log");
        fs::create_dir_all(&root).unwrap();
        fs::write(
            &bin,
            format!(
                "#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{}'\n[ \"$1\" != \"--local\" ]\n",
                log.display()
            ),
        )
        .unwrap();
        let mut permissions = fs::metadata(&bin).unwrap().permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&bin, permissions).unwrap();

        run_bender_checkout_in_root(&root, &bin).unwrap();

        assert_eq!(
            fs::read_to_string(&log).unwrap(),
            "--local --git-submodules false checkout\ncheckout\n"
        );
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn skipped_hardening_reports_unmodified_local_rtl_activation() {
        let result = HardeningResult {
            status: HardeningStatus::Skipped,
            changed_files: Vec::new(),
        };

        assert_eq!(
            hardening_completion_messages(&result),
            vec![
                "Hardening skipped: aegisrtl/scripts/harden.sh was not found".to_string(),
                "Local RTL dependencies activated without hardening".to_string(),
            ]
        );
    }

}