git-editor 2.1.0

A command-line tool to edit git commit timestamps, messages, and author information
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
use crate::rewrite::engine::{self, CommitterMode, Edit, Plan};
use crate::rewrite::report::print_outcome;
use crate::utils::dates::{format_git_time, parse_git_time};
use crate::utils::prompt::{cancelled, confirm};
use crate::utils::prompt::{read_prompted_line, read_prompted_line_raw};
use crate::utils::simulation::{report_simulation, simulation_from_plan};
use crate::utils::types::Result;
use crate::utils::types::{CommitInfo, EditOptions};
use crate::utils::validator::{is_valid_email, validate_identity_part};
use crate::{args::Args, utils::commit_history::get_commit_history};
use colored::Colorize;
use git2::Repository;
use std::io::{self, Write};

pub fn select_commit(commits: &[CommitInfo]) -> Result<usize> {
    crate::say!("\n{}", "Commit History:".bold().green());
    crate::say!("{}", "-".repeat(80).cyan());

    for (i, commit) in commits.iter().enumerate() {
        crate::say!(
            "{:3}. {} {} {} {}",
            i + 1,
            commit.short_hash.yellow().bold(),
            commit
                .timestamp
                .format("%Y-%m-%d %H:%M:%S")
                .to_string()
                .blue(),
            crate::utils::sanitize::safe(&commit.author_name).magenta(),
            commit
                .message
                .lines()
                .next()
                .unwrap_or("(no message)")
                .white()
        );
    }

    crate::say!("{}", "-".repeat(80).cyan());
    crate::say_inline!(
        "\n{} {} ",
        "Select commit number to edit:".bold().green(),
        "(Esc to cancel)".bright_black()
    );
    io::stdout().flush()?;

    let input = read_prompted_line()?;

    let selection = input.parse::<usize>().map_err(|_| "Invalid number")?;

    if selection < 1 || selection > commits.len() {
        return Err("Selection out of range".into());
    }

    Ok(selection - 1)
}

pub fn show_commit_details(commit: &CommitInfo, repo: &Repository) -> Result<()> {
    crate::say!("\n{}", "Selected Commit Details:".bold().green());
    crate::say!("{}", "=".repeat(80).cyan());

    crate::say!("{}: {}", "Hash".bold(), commit.oid.to_string().yellow());
    crate::say!("{}: {}", "Short Hash".bold(), commit.short_hash.yellow());
    crate::say!(
        "{}: {}",
        "Author".bold(),
        format!("{} <{}>", commit.author_name, commit.author_email).magenta()
    );
    crate::say!(
        "{}: {}",
        "Date".bold(),
        commit
            .timestamp
            .format("%Y-%m-%d %H:%M:%S")
            .to_string()
            .blue()
    );
    crate::say!(
        "{}: {}",
        "Parent Count".bold(),
        commit.parent_count.to_string().white()
    );

    crate::say!("\n{}", "Message:".bold());
    crate::say!("{}", commit.message.white());

    // Show parent commits
    if commit.parent_count > 0 {
        let git_commit = repo.find_commit(commit.oid)?;
        crate::say!("\n{}", "Parent Commits:".bold());
        for (i, parent_id) in git_commit.parent_ids().enumerate() {
            let parent = repo.find_commit(parent_id)?;
            crate::say!(
                "  {}: {} - {}",
                i + 1,
                parent_id.to_string()[..8].to_string().yellow(),
                parent
                    .summary()
                    .ok()
                    .flatten()
                    .unwrap_or("(no message)")
                    .white()
            );
        }
    }

    crate::say!("{}", "=".repeat(80).cyan());
    Ok(())
}

/// Parse a comma-separated menu selection; `5` means "all of the above".
fn parse_edit_selection(input: &str) -> Result<Vec<usize>> {
    let mut selected = Vec::new();
    for token in input.split(',').map(str::trim).filter(|t| !t.is_empty()) {
        match token.parse::<usize>() {
            Ok(5) => selected.extend(1..=4),
            Ok(n @ 1..=4) => selected.push(n),
            _ => return Err(format!("Invalid option '{token}' (choose 1-5)").into()),
        }
    }
    selected.sort_unstable();
    selected.dedup();
    if selected.is_empty() {
        return Err("No option selected; nothing to edit".into());
    }
    Ok(selected)
}

fn prompt_line(label: &str) -> Result<String> {
    crate::say_inline!("{} {} ", label.bold(), "(Esc to cancel)".bright_black());
    io::stdout().flush()?;
    read_prompted_line()
}

/// Read a multi-line message; a line containing only `.` ends it.
fn prompt_message() -> Result<String> {
    crate::say!(
        "{} {} ",
        "New commit message (finish with a line containing only '.'):".bold(),
        "(Esc to cancel)".bright_black()
    );
    let mut lines = Vec::new();
    loop {
        let line = read_prompted_line_raw()?;
        if line.trim() == "." {
            break;
        }
        lines.push(line);
    }
    build_message(&lines)
}

/// Join entered lines into a message, dropping leading/trailing blank lines.
fn build_message(lines: &[String]) -> Result<String> {
    let start = lines.iter().position(|l| !l.trim().is_empty());
    let end = lines.iter().rposition(|l| !l.trim().is_empty());
    match (start, end) {
        (Some(start), Some(end)) => Ok(lines[start..=end].join("\n") + "\n"),
        _ => Err("Commit message cannot be empty".into()),
    }
}

// Get user input for what to change
pub fn get_edit_options() -> Result<EditOptions> {
    crate::say!("\n{}", "What would you like to edit?".bold().green());
    crate::say!("1. Author name");
    crate::say!("2. Author email");
    crate::say!("3. Commit timestamp");
    crate::say!("4. Commit message");
    crate::say!("5. All of the above");

    let selections = parse_edit_selection(&prompt_line("Select option(s) (comma-separated):")?)?;
    let mut options = EditOptions::default();

    for selection in selections {
        match selection {
            1 => {
                let name = prompt_line("New author name:")?;
                validate_identity_part(&name, "Author name")?;
                options.author_name = Some(name);
            }
            2 => {
                let email = prompt_line("New author email:")?;
                if !is_valid_email(&email) {
                    return Err(format!("Invalid email format: {email}").into());
                }
                options.author_email = Some(email);
            }
            3 => {
                let timestamp =
                    prompt_line("New timestamp (YYYY-MM-DD HH:MM:SS [+HH:MM], default UTC):")?;
                options.timestamp = Some(parse_git_time(&timestamp)?);
            }
            4 => options.message = Some(prompt_message()?),
            _ => unreachable!("parse_edit_selection only returns 1-4"),
        }
    }

    Ok(options)
}

/// Find a commit by unique hash prefix (4+ hex digits) or by 1-based list
/// number (1 = newest, as shown by `-s`). A hash prefix wins when both match.
fn resolve_commit<'a>(commits: &'a [CommitInfo], reference: &str) -> Result<&'a CommitInfo> {
    let reference = reference.trim().to_ascii_lowercase();
    if reference.len() >= 4 && reference.chars().all(|c| c.is_ascii_hexdigit()) {
        let mut matches = commits
            .iter()
            .filter(|c| c.oid.to_string().starts_with(&reference));
        match (matches.next(), matches.next()) {
            (Some(commit), None) => return Ok(commit),
            (Some(_), Some(_)) => {
                return Err(format!("--commit '{reference}' matches several commits").into())
            }
            (None, _) => {}
        }
    }
    match reference.parse::<usize>() {
        Ok(number) if (1..=commits.len()).contains(&number) => Ok(&commits[number - 1]),
        _ => Err(format!(
            "--commit '{reference}' matches no commit hash and is not a number between 1 and {}",
            commits.len()
        )
        .into()),
    }
}

/// Edits requested with --name/--email/--set-date/--set-message.
fn edit_options_from_args(args: &Args) -> Result<EditOptions> {
    if let Some(name) = &args.name {
        validate_identity_part(name, "Author name")?;
    }
    if let Some(email) = &args.email {
        if !is_valid_email(email) {
            return Err(format!("Invalid email format: {email}").into());
        }
    }
    let options = EditOptions {
        author_name: args.name.clone(),
        author_email: args.email.clone(),
        timestamp: args.set_date.as_deref().map(parse_git_time).transpose()?,
        message: args.set_message.clone(),
    };
    if options.author_name.is_none()
        && options.author_email.is_none()
        && options.timestamp.is_none()
        && options.message.is_none()
    {
        return Err("Nothing to change: pass --name, --email, --set-date and/or --set-message with --commit".into());
    }
    if options
        .message
        .as_deref()
        .is_some_and(|m| m.trim().is_empty())
    {
        return Err("Commit message cannot be empty".into());
    }
    Ok(options)
}

pub fn rewrite_specific_commits(args: &Args) -> Result<()> {
    let commits = get_commit_history(args, false)?;

    if commits.is_empty() {
        crate::say!("{}", "No commits found!".red());
        return Ok(());
    }

    let repo = Repository::open(args.repo_path.as_ref().unwrap())?;
    // Fail early on a detached/unborn HEAD and remember the tip the edit is based on.
    let head = engine::current_branch(&repo)?.head;

    let (selected_commit, edit_options) = match &args.commit {
        // Flag-driven (non-interactive) edit.
        Some(reference) => (
            resolve_commit(&commits, reference)?,
            edit_options_from_args(args)?,
        ),
        None => {
            let selected = &commits[select_commit(&commits)?];
            show_commit_details(selected, &repo)?;
            (selected, get_edit_options()?)
        }
    };

    // Confirm changes
    crate::say!("\n{}", "Planned changes:".bold().yellow());
    if let Some(ref name) = edit_options.author_name {
        crate::say!(
            "  Author name: {} -> {}",
            crate::utils::sanitize::safe(&selected_commit.author_name).red(),
            name.green()
        );
    }
    if let Some(ref email) = edit_options.author_email {
        crate::say!(
            "  Author email: {} -> {}",
            crate::utils::sanitize::safe(&selected_commit.author_email).red(),
            email.green()
        );
    }
    if let Some(ref timestamp) = edit_options.timestamp {
        crate::say!(
            "  Timestamp: {} -> {}",
            selected_commit
                .timestamp
                .format("%Y-%m-%d %H:%M:%S")
                .to_string()
                .red(),
            format_git_time(*timestamp).green()
        );
    }
    if let Some(ref message) = edit_options.message {
        crate::say!(
            "  Message: {} -> {}",
            crate::utils::sanitize::safe(selected_commit.message.lines().next().unwrap_or(""))
                .red(),
            crate::utils::sanitize::safe(message.lines().next().unwrap_or("")).green()
        );
    }

    if args.simulate {
        let plan = Plan {
            edits: [(selected_commit.oid, edit_from_options(&edit_options))]
                .into_iter()
                .collect(),
            committer: args.committer.into(),
        };
        let preview = simulation_from_plan(&commits, &plan, "Specific Commit Edit");
        report_simulation(&preview, args.show_diff, true);
        crate::say!("{}", "Simulation only: nothing was written.".cyan());
        return Ok(());
    }

    if !confirm(&format!("\n{}", "Proceed with changes?".bold()), args.yes)? {
        return Err(cancelled());
    }

    // Apply changes
    apply_commit_changes(
        &repo,
        selected_commit,
        &edit_options,
        head,
        args.committer.into(),
    )?;

    if args.show_history {
        get_commit_history(args, true)?;
    }

    Ok(())
}

fn edit_from_options(options: &EditOptions) -> Edit {
    Edit {
        name: options.author_name.clone(),
        email: options.author_email.clone(),
        time: options.timestamp,
        message: options.message.clone(),
    }
}

/// Rewrite only the selected commit (descendants are re-parented, ancestors reused).
fn apply_commit_changes(
    repo: &Repository,
    target_commit: &CommitInfo,
    options: &EditOptions,
    expected_head: git2::Oid,
    committer: CommitterMode,
) -> Result<()> {
    let plan = Plan {
        edits: [(target_commit.oid, edit_from_options(options))]
            .into_iter()
            .collect(),
        committer,
    };
    let outcome = engine::apply(repo, &plan, expected_head)?;
    print_outcome(&outcome);
    Ok(())
}

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

    fn create_test_repo_with_commits() -> (TempDir, String) {
        let temp_dir = TempDir::new().unwrap();
        let repo_path = temp_dir.path().to_str().unwrap().to_string();

        // Initialize git repo
        let repo = git2::Repository::init(&repo_path).unwrap();

        // Create multiple commits
        for i in 1..=3 {
            let file_path = temp_dir.path().join(format!("test{i}.txt"));
            fs::write(&file_path, format!("test content {i}")).unwrap();

            let mut index = repo.index().unwrap();
            index
                .add_path(std::path::Path::new(&format!("test{i}.txt")))
                .unwrap();
            index.write().unwrap();

            let tree_id = index.write_tree().unwrap();
            let tree = repo.find_tree(tree_id).unwrap();

            let sig = git2::Signature::new(
                "Test User",
                "test@example.com",
                &git2::Time::new(1234567890 + i as i64 * 3600, 0),
            )
            .unwrap();

            let parents = if i == 1 {
                vec![]
            } else {
                let head = repo.head().unwrap();
                let parent_commit = head.peel_to_commit().unwrap();
                vec![parent_commit]
            };

            repo.commit(
                Some("HEAD"),
                &sig,
                &sig,
                &format!("Commit {i}"),
                &tree,
                &parents.iter().collect::<Vec<_>>(),
            )
            .unwrap();
        }

        (temp_dir, repo_path)
    }

    #[test]
    fn test_show_commit_details() {
        let (_temp_dir, repo_path) = create_test_repo_with_commits();
        let repo = Repository::open(&repo_path).unwrap();

        // Get commit info
        let args = Args {
            repo_path: Some(repo_path),
            ..Default::default()
        };

        let commits = get_commit_history(&args, false).unwrap();
        let commit = &commits[0];

        // Test that show_commit_details doesn't crash
        let result = show_commit_details(commit, &repo);
        assert!(result.is_ok());
    }

    #[test]
    fn test_build_message_keeps_body_structure() {
        let lines: Vec<String> = ["", "subject", "", "  indented body", ""]
            .iter()
            .map(|s| s.to_string())
            .collect();
        assert_eq!(
            build_message(&lines).unwrap(),
            "subject\n\n  indented body\n"
        );
        assert!(build_message(&["  ".to_string()]).is_err());
    }

    #[test]
    fn test_resolve_commit_by_number_and_prefix() {
        let make = |hex: &str| CommitInfo {
            oid: git2::Oid::from_str(hex).unwrap(),
            ..Default::default()
        };
        let commits = vec![
            make("abcdef0000000000000000000000000000000000"),
            make("abcd120000000000000000000000000000000000"),
            make("1234560000000000000000000000000000000000"),
        ];
        assert_eq!(resolve_commit(&commits, "1").unwrap().oid, commits[0].oid);
        assert_eq!(resolve_commit(&commits, "3").unwrap().oid, commits[2].oid);
        assert_eq!(
            resolve_commit(&commits, "1234").unwrap().oid,
            commits[2].oid
        );
        assert_eq!(
            resolve_commit(&commits, "ABCDEF").unwrap().oid,
            commits[0].oid
        );
        assert!(resolve_commit(&commits, "abcd").is_err()); // ambiguous
        assert!(resolve_commit(&commits, "4").is_err());
        assert!(resolve_commit(&commits, "ffff").is_err());
    }

    #[test]
    fn test_edit_options_from_args_requires_a_change() {
        let args = Args {
            pick_specific_commits: true,
            commit: Some("1".into()),
            ..Default::default()
        };
        assert!(edit_options_from_args(&args).is_err());
        let args = Args {
            email: Some("not-an-email".into()),
            ..args
        };
        assert!(edit_options_from_args(&args).is_err());
    }

    #[test]
    fn test_parse_edit_selection() {
        assert_eq!(parse_edit_selection("1").unwrap(), vec![1]);
        assert_eq!(parse_edit_selection(" 3, 1 ,3").unwrap(), vec![1, 3]);
        assert_eq!(parse_edit_selection("5").unwrap(), vec![1, 2, 3, 4]);
        assert!(parse_edit_selection("9").is_err());
        assert!(parse_edit_selection("1,x").is_err());
        assert!(parse_edit_selection("").is_err());
    }
}