repolens 2.0.0

A CLI tool to audit and prepare repositories for open source or enterprise standards
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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Git operations for creating branches, commits, and pushing changes

use crate::error::{ActionError, RepoLensError};
use chrono::Local;
use std::path::Path;
use std::process::Command;

/// Create a new git branch with a timestamp-based name
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
///
/// # Returns
///
/// The name of the created branch
///
/// # Errors
///
/// Returns an error if git operations fail
pub fn create_branch(root: &Path) -> Result<String, RepoLensError> {
    let timestamp = Local::now().format("%Y%m%d-%H%M%S");
    let branch_name = format!("repolens/apply-{}", timestamp);

    // Create and checkout the new branch
    let output = Command::new("git")
        .args(["checkout", "-b", &branch_name])
        .current_dir(root)
        .output()
        .map_err(|e| {
            RepoLensError::Action(ActionError::ExecutionFailed {
                message: format!("Failed to create branch: {}", e),
            })
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(RepoLensError::Action(ActionError::ExecutionFailed {
            message: format!("Failed to create branch '{}': {}", branch_name, stderr),
        }));
    }

    Ok(branch_name)
}

/// Check if there are any uncommitted changes in the repository
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
///
/// # Returns
///
/// `true` if there are uncommitted changes, `false` otherwise
pub fn has_changes(root: &Path) -> Result<bool, RepoLensError> {
    // Check if there are any changes (staged or unstaged)
    let status_output = Command::new("git")
        .args(["status", "--porcelain"])
        .current_dir(root)
        .output()
        .map_err(|e| {
            RepoLensError::Action(ActionError::ExecutionFailed {
                message: format!("Failed to check git status: {}", e),
            })
        })?;

    if !status_output.status.success() {
        return Err(RepoLensError::Action(ActionError::ExecutionFailed {
            message: "Failed to check git status".to_string(),
        }));
    }

    let status = String::from_utf8_lossy(&status_output.stdout);
    Ok(!status.trim().is_empty())
}

/// Stage all changes in the repository.
///
/// Part of the public API - provides a convenient way to stage
/// all changes when applying fixes programmatically.
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
///
/// # Errors
///
/// Returns an error if git add fails
#[allow(dead_code)]
pub fn stage_all_changes(root: &Path) -> Result<(), RepoLensError> {
    let output = Command::new("git")
        .args(["add", "-A"])
        .current_dir(root)
        .output()
        .map_err(|e| {
            RepoLensError::Action(ActionError::ExecutionFailed {
                message: format!("Failed to stage changes: {}", e),
            })
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(RepoLensError::Action(ActionError::ExecutionFailed {
            message: format!("Failed to stage changes: {}", stderr),
        }));
    }

    Ok(())
}

/// Stage specific files in the repository
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
/// * `files` - The list of file paths to stage (relative to the repository root)
///
/// # Errors
///
/// Returns an error if git add fails
pub fn stage_files(root: &Path, files: &[String]) -> Result<(), RepoLensError> {
    if files.is_empty() {
        return Ok(());
    }

    let mut args = vec!["add", "--"];
    let file_refs: Vec<&str> = files.iter().map(|s| s.as_str()).collect();
    args.extend(file_refs);

    let output = Command::new("git")
        .args(&args)
        .current_dir(root)
        .output()
        .map_err(|e| {
            RepoLensError::Action(ActionError::ExecutionFailed {
                message: format!("Failed to stage files: {}", e),
            })
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(RepoLensError::Action(ActionError::ExecutionFailed {
            message: format!("Failed to stage files: {}", stderr),
        }));
    }

    Ok(())
}

/// Create a commit with the given message
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
/// * `message` - The commit message
///
/// # Errors
///
/// Returns an error if git commit fails
pub fn create_commit(root: &Path, message: &str) -> Result<(), RepoLensError> {
    let output = Command::new("git")
        .args(["commit", "-m", message])
        .current_dir(root)
        .output()
        .map_err(|e| {
            RepoLensError::Action(ActionError::ExecutionFailed {
                message: format!("Failed to create commit: {}", e),
            })
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(RepoLensError::Action(ActionError::ExecutionFailed {
            message: format!("Failed to create commit: {}", stderr),
        }));
    }

    Ok(())
}

/// Push the current branch to the remote repository
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
/// * `branch_name` - The name of the branch to push
///
/// # Errors
///
/// Returns an error if git push fails
pub fn push_branch(root: &Path, branch_name: &str) -> Result<(), RepoLensError> {
    let output = Command::new("git")
        .args(["push", "-u", "origin", branch_name])
        .current_dir(root)
        .output()
        .map_err(|e| {
            RepoLensError::Action(ActionError::ExecutionFailed {
                message: format!("Failed to push branch: {}", e),
            })
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(RepoLensError::Action(ActionError::ExecutionFailed {
            message: format!("Failed to push branch '{}': {}", branch_name, stderr),
        }));
    }

    Ok(())
}

/// Get the current branch name.
///
/// Part of the public API - useful for external code that needs
/// to determine the current working branch.
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
///
/// # Returns
///
/// The current branch name, or `None` if not in a git repository
#[allow(dead_code)]
pub fn get_current_branch(root: &Path) -> Option<String> {
    let output = Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .current_dir(root)
        .output()
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if branch.is_empty() {
        None
    } else {
        Some(branch)
    }
}

/// Get the default branch (main or master)
///
/// # Arguments
///
/// * `root` - The root directory of the git repository
///
/// # Returns
///
/// The default branch name, or `None` if not found
pub fn get_default_branch(root: &Path) -> Option<String> {
    // Try to get from git symbolic-ref
    let output = Command::new("git")
        .args(["symbolic-ref", "refs/remotes/origin/HEAD", "--short"])
        .current_dir(root)
        .output()
        .ok()?;

    if output.status.success() {
        let branch = String::from_utf8_lossy(&output.stdout)
            .trim()
            .trim_start_matches("origin/")
            .to_string();
        if !branch.is_empty() {
            return Some(branch);
        }
    }

    // Fall back to checking for main or master
    let branches = ["main", "master"];
    for branch in branches {
        let output = Command::new("git")
            .args([
                "show-ref",
                "--verify",
                "--quiet",
                &format!("refs/heads/{}", branch),
            ])
            .current_dir(root)
            .output()
            .ok()?;

        if output.status.success() {
            return Some(branch.to_string());
        }
    }

    None
}

/// Check if the current directory is a git repository
///
/// # Arguments
///
/// * `root` - The directory to check
///
/// # Returns
///
/// `true` if it's a git repository, `false` otherwise
pub fn is_git_repository(root: &Path) -> bool {
    root.join(".git").exists()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use std::fs;
    use std::process::Command;
    use tempfile::TempDir;

    fn init_git_repo(root: &Path) -> Result<(), Box<dyn std::error::Error>> {
        Command::new("git")
            .args(["init"])
            .current_dir(root)
            .output()?;

        // Configure git user (required for commits)
        Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(root)
            .output()?;

        Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(root)
            .output()?;

        // Create initial commit
        fs::write(root.join("README.md"), "# Test Repo")?;
        Command::new("git")
            .args(["add", "README.md"])
            .current_dir(root)
            .output()?;

        Command::new("git")
            .args(["commit", "-m", "Initial commit"])
            .current_dir(root)
            .output()?;

        Ok(())
    }

    #[test]
    fn test_is_git_repository() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Test with non-git directory
        assert!(!is_git_repository(root));

        // Test with git directory
        fs::create_dir(root.join(".git")).unwrap();
        assert!(is_git_repository(root));
    }

    #[test]
    #[serial]
    fn test_create_branch() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Create branch
        let branch_name = create_branch(&root_abs).expect("Failed to create branch");

        // Verify branch name format
        assert!(branch_name.starts_with("repolens/apply-"));

        // Verify we're on the new branch
        let output = Command::new("git")
            .args(["rev-parse", "--abbrev-ref", "HEAD"])
            .current_dir(&root_abs)
            .output()
            .unwrap();

        let current_branch = String::from_utf8_lossy(&output.stdout).trim().to_string();
        assert_eq!(current_branch, branch_name);

        let _ = std::env::set_current_dir(&original_dir);
    }

    #[test]
    #[serial]
    fn test_has_changes() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Initially no changes
        assert!(!has_changes(&root_abs).expect("Failed to check changes"));

        // Create a new file
        fs::write(root_abs.join("test.txt"), "test content").unwrap();

        // Now there should be changes
        assert!(has_changes(&root_abs).expect("Failed to check changes"));

        let _ = std::env::set_current_dir(&original_dir);
    }

    #[test]
    #[serial]
    fn test_stage_all_changes() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Create a new file
        fs::write(root_abs.join("test.txt"), "test content").unwrap();

        // Stage changes
        stage_all_changes(&root_abs).expect("Failed to stage changes");

        // Verify file is staged
        let output = Command::new("git")
            .args(["status", "--porcelain"])
            .current_dir(&root_abs)
            .output()
            .unwrap();

        let status = String::from_utf8_lossy(&output.stdout);
        assert!(status.contains("test.txt"));
        assert!(status.starts_with("A")); // A = Added

        let _ = std::env::set_current_dir(&original_dir);
    }

    #[test]
    #[serial]
    fn test_create_commit() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Create and stage a file
        fs::write(root_abs.join("test.txt"), "test content").unwrap();
        stage_all_changes(&root_abs).expect("Failed to stage changes");

        // Create commit
        let commit_message = "Test commit";
        create_commit(&root_abs, commit_message).expect("Failed to create commit");

        // Verify commit was created
        let output = Command::new("git")
            .args(["log", "--oneline", "-1"])
            .current_dir(&root_abs)
            .output()
            .unwrap();

        let log = String::from_utf8_lossy(&output.stdout);
        assert!(log.contains(commit_message));

        let _ = std::env::set_current_dir(&original_dir);
    }

    #[test]
    #[serial]
    fn test_stage_files() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Create multiple files
        fs::write(root_abs.join("file1.txt"), "content 1").unwrap();
        fs::write(root_abs.join("file2.txt"), "content 2").unwrap();
        fs::write(root_abs.join("file3.txt"), "content 3").unwrap();

        // Stage only specific files
        let files = vec!["file1.txt".to_string(), "file2.txt".to_string()];
        stage_files(&root_abs, &files).expect("Failed to stage files");

        // Verify only file1.txt and file2.txt are staged, file3.txt is not
        let output = Command::new("git")
            .args(["status", "--porcelain"])
            .current_dir(&root_abs)
            .output()
            .unwrap();

        let status = String::from_utf8_lossy(&output.stdout);
        assert!(
            status.contains("A  file1.txt"),
            "file1.txt should be staged"
        );
        assert!(
            status.contains("A  file2.txt"),
            "file2.txt should be staged"
        );
        assert!(
            status.contains("?? file3.txt"),
            "file3.txt should be untracked, not staged"
        );

        let _ = std::env::set_current_dir(&original_dir);
    }

    #[test]
    #[serial]
    fn test_stage_files_empty() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Stage empty list should succeed without error
        let files: Vec<String> = vec![];
        stage_files(&root_abs, &files).expect("Staging empty file list should succeed");

        let _ = std::env::set_current_dir(&original_dir);
    }

    #[test]
    #[serial]
    fn test_get_default_branch() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Get default branch (should be main or master)
        let default_branch = get_default_branch(&root_abs);

        // Should find main or master
        assert!(default_branch.is_some());
        let branch = default_branch.unwrap();
        assert!(branch == "main" || branch == "master");

        let _ = std::env::set_current_dir(&original_dir);
    }

    #[test]
    #[serial]
    fn test_get_current_branch() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        let root_abs = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

        let original_dir =
            std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("/tmp"));

        if std::env::current_dir().is_err() {
            let _ = std::env::set_current_dir("/tmp");
        }

        std::env::set_current_dir(&root_abs).expect("Failed to change to temp directory");

        // Initialize git repo
        init_git_repo(&root_abs).expect("Failed to init git repo");

        // Get current branch
        let current_branch = get_current_branch(&root_abs);

        // Should find a branch (main or master)
        assert!(current_branch.is_some());
        let branch = current_branch.unwrap();
        assert!(branch == "main" || branch == "master");

        let _ = std::env::set_current_dir(&original_dir);
    }
}