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
use crate::args::Args;
use crate::utils::dates::parse_utc;
use crate::utils::types::Result;
use chrono::{Duration, NaiveDateTime};
use rand::Rng;

pub fn generate_timestamps(args: &Args) -> Result<Vec<NaiveDateTime>> {
    let start_dt = parse_utc(args.start.as_deref().ok_or("Missing --begin")?)?;
    let end_dt = parse_utc(args.end.as_deref().ok_or("Missing --end")?)?;

    if start_dt >= end_dt {
        return Err("Start datetime must be before end datetime".into());
    }

    let total_commits = count_commits(args.repo_path.as_ref().unwrap())?;
    if total_commits == 0 {
        return Err("No commits found in repository".into());
    }

    spread_timestamps(start_dt, end_dt, total_commits, args.skip_range_check)
}

/// Minimum gap between generated commits by default.
const DEFAULT_GAP_SECS: i64 = 3 * 3600;
/// Minimum gap with `--skip-range-check` when it fits.
const PACKED_GAP_SECS: i64 = 5 * 60;

/// `count` strictly increasing timestamps from `start` to exactly `end`.
///
/// Gaps are at least 3 hours (or, with `skip_range_check`, at least 5 minutes
/// when that fits and otherwise as even as possible, but never below one second),
/// with the remaining slack spread randomly. Everything is whole seconds, so the
/// last timestamp is `end` exactly.
pub fn spread_timestamps(
    start: NaiveDateTime,
    end: NaiveDateTime,
    count: usize,
    skip_range_check: bool,
) -> Result<Vec<NaiveDateTime>> {
    if count == 0 {
        return Ok(Vec::new());
    }
    if count == 1 {
        return Ok(vec![start]);
    }
    let gaps = (count - 1) as i64;
    let span = (end - start).num_seconds();

    let min_gap = if span >= DEFAULT_GAP_SECS * gaps {
        DEFAULT_GAP_SECS
    } else if !skip_range_check {
        return Err(format!(
            "Date range too small for {count} commits. Need at least {} hours between start and end dates.\n\
            Tip: Pass --skip-range-check to skip this validation and distribute commits evenly across the given range.",
            DEFAULT_GAP_SECS * gaps / 3600
        )
        .into());
    } else if span >= PACKED_GAP_SECS * gaps {
        PACKED_GAP_SECS
    } else if span >= gaps {
        // Too tight for random gaps: spread evenly, every gap at least one second.
        let gap_lengths = even_split(span, gaps as usize);
        return Ok(accumulate(start, &gap_lengths));
    } else {
        return Err(format!(
            "Date range too small for {count} commits: need at least {gaps} seconds so every commit gets a distinct timestamp"
        )
        .into());
    };

    let slack = span - min_gap * gaps;
    let mut rng = rand::rng();
    let weights: Vec<f64> = (0..gaps)
        .map(|_| rng.random::<f64>() + f64::EPSILON)
        .collect();
    let gap_lengths: Vec<i64> = random_split(slack, &weights)
        .into_iter()
        .map(|extra| extra + min_gap)
        .collect();
    Ok(accumulate(start, &gap_lengths))
}

/// Split `total` into `parts` integers that differ by at most one.
fn even_split(total: i64, parts: usize) -> Vec<i64> {
    let base = total / parts as i64;
    let remainder = (total % parts as i64) as usize;
    (0..parts)
        .map(|i| base + i64::from(i < remainder))
        .collect()
}

/// Split `total` proportionally to `weights` into integers that sum to `total` exactly.
fn random_split(total: i64, weights: &[f64]) -> Vec<i64> {
    let sum: f64 = weights.iter().sum();
    let shares: Vec<f64> = weights.iter().map(|w| w / sum * total as f64).collect();
    let mut parts: Vec<i64> = shares.iter().map(|s| s.floor() as i64).collect();
    let mut missing = total - parts.iter().sum::<i64>();
    // Hand out the rounding remainder to the largest fractional parts.
    let mut order: Vec<usize> = (0..parts.len()).collect();
    order.sort_by(|&a, &b| {
        let fa = shares[a] - shares[a].floor();
        let fb = shares[b] - shares[b].floor();
        fb.partial_cmp(&fa).unwrap_or(std::cmp::Ordering::Equal)
    });
    for i in order {
        if missing <= 0 {
            break;
        }
        parts[i] += 1;
        missing -= 1;
    }
    parts
}

fn accumulate(start: NaiveDateTime, gaps: &[i64]) -> Vec<NaiveDateTime> {
    let mut current = start;
    let mut out = Vec::with_capacity(gaps.len() + 1);
    out.push(current);
    for gap in gaps {
        current += Duration::seconds(*gap);
        out.push(current);
    }
    out
}

fn count_commits(repo_path: &str) -> Result<usize> {
    let repo = git2::Repository::open(repo_path)?;
    let mut revwalk = repo.revwalk()?;
    revwalk.push_head()?;
    revwalk.set_sorting(git2::Sort::TOPOLOGICAL | git2::Sort::TIME)?;
    Ok(revwalk.count())
}

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

    fn create_test_repo() -> (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 a test file
        let file_path = temp_dir.path().join("test.txt");
        fs::write(&file_path, "test content").unwrap();

        // Add and commit file
        let mut index = repo.index().unwrap();
        index.add_path(std::path::Path::new("test.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, 0),
        )
        .unwrap();
        repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();

        (temp_dir, repo_path)
    }

    #[test]
    fn test_count_commits() {
        let (_temp_dir, repo_path) = create_test_repo();
        let count = count_commits(&repo_path).unwrap();
        assert_eq!(count, 1);
    }

    #[test]
    fn test_generate_timestamps_invalid_date_format() {
        let (_temp_dir, repo_path) = create_test_repo();
        let args = Args {
            repo_path: Some(repo_path),
            email: Some("test@example.com".to_string()),
            name: Some("Test User".to_string()),
            start: Some("invalid-date".to_string()),
            end: Some("2023-01-02 00:00:00".to_string()),
            ..Default::default()
        };

        let result = generate_timestamps(&args);
        assert!(result.is_err());
    }

    #[test]
    fn test_generate_timestamps_valid_range() {
        let (_temp_dir, repo_path) = create_test_repo();
        let args = Args {
            repo_path: Some(repo_path),
            email: Some("test@example.com".to_string()),
            name: Some("Test User".to_string()),
            start: Some("2023-01-01 00:00:00".to_string()),
            end: Some("2023-01-10 00:00:00".to_string()),
            ..Default::default()
        };

        let result = generate_timestamps(&args);
        assert!(result.is_ok());

        let timestamps = result.unwrap();
        assert_eq!(timestamps.len(), 1); // One commit in test repo

        let start_dt =
            NaiveDateTime::parse_from_str("2023-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
        let end_dt =
            NaiveDateTime::parse_from_str("2023-01-10 00:00:00", "%Y-%m-%d %H:%M:%S").unwrap();

        assert!(timestamps[0] >= start_dt);
        assert!(timestamps[0] <= end_dt);
    }

    #[test]
    fn test_generate_timestamps_preserves_order() {
        let (_temp_dir, repo_path) = create_test_repo();
        let args = Args {
            repo_path: Some(repo_path),
            email: Some("test@example.com".to_string()),
            name: Some("Test User".to_string()),
            start: Some("2023-01-01 00:00:00".to_string()),
            end: Some("2023-01-10 00:00:00".to_string()),
            ..Default::default()
        };

        let result = generate_timestamps(&args);
        assert!(result.is_ok());

        let timestamps = result.unwrap();

        // Check that timestamps are in ascending order
        for i in 1..timestamps.len() {
            assert!(timestamps[i] >= timestamps[i - 1]);
        }
    }

    fn create_test_repo_with_n_commits(n: usize) -> (TempDir, String) {
        let temp_dir = TempDir::new().unwrap();
        let repo_path = temp_dir.path().to_str().unwrap().to_string();
        let repo = git2::Repository::init(&repo_path).unwrap();

        for i in 1..=n {
            let file_path = temp_dir.path().join(format!("file{i}.txt"));
            fs::write(&file_path, format!("content {i}")).unwrap();

            let mut index = repo.index().unwrap();
            index
                .add_path(std::path::Path::new(&format!("file{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_small_range_error_mentions_skip_flag() {
        let (_temp_dir, repo_path) = create_test_repo_with_n_commits(5);
        let args = Args {
            repo_path: Some(repo_path),
            email: Some("test@example.com".to_string()),
            name: Some("Test User".to_string()),
            start: Some("2023-01-01 00:00:00".to_string()),
            end: Some("2023-01-01 01:00:00".to_string()), // 1 hour for 5 commits
            ..Default::default()
        };

        let result = generate_timestamps(&args);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("--skip-range-check"),
            "Error message should mention --skip-range-check flag, got: {err_msg}"
        );
    }

    #[test]
    fn test_skip_range_check_produces_correct_count() {
        let (_temp_dir, repo_path) = create_test_repo_with_n_commits(5);
        let args = Args {
            repo_path: Some(repo_path),
            email: Some("test@example.com".to_string()),
            name: Some("Test User".to_string()),
            start: Some("2023-01-01 00:00:00".to_string()),
            end: Some("2023-01-01 01:00:00".to_string()), // 1 hour for 5 commits
            skip_range_check: true,
            ..Default::default()
        };

        let result = generate_timestamps(&args);
        assert!(
            result.is_ok(),
            "skip_range_check should succeed: {:?}",
            result.err()
        );

        let timestamps = result.unwrap();
        assert_eq!(timestamps.len(), 5);
    }

    #[test]
    fn test_skip_range_check_respects_5min_gap_and_bounds() {
        let (_temp_dir, repo_path) = create_test_repo_with_n_commits(3);
        let args = Args {
            repo_path: Some(repo_path),
            email: Some("test@example.com".to_string()),
            name: Some("Test User".to_string()),
            start: Some("2023-01-01 00:00:00".to_string()),
            end: Some("2023-01-01 02:00:00".to_string()), // 2 hours for 3 commits (enough for 5-min gaps)
            skip_range_check: true,
            ..Default::default()
        };

        let result = generate_timestamps(&args);
        assert!(result.is_ok());

        let timestamps = result.unwrap();
        let start_dt =
            NaiveDateTime::parse_from_str("2023-01-01 00:00:00", "%Y-%m-%d %H:%M:%S").unwrap();
        let end_dt =
            NaiveDateTime::parse_from_str("2023-01-01 02:00:00", "%Y-%m-%d %H:%M:%S").unwrap();

        // All timestamps should be within bounds
        for ts in &timestamps {
            assert!(*ts >= start_dt, "Timestamp {ts} is before start");
            assert!(*ts <= end_dt, "Timestamp {ts} is after end");
        }

        // Each consecutive gap should be >= 5 minutes
        for i in 1..timestamps.len() {
            let gap = timestamps[i] - timestamps[i - 1];
            assert!(
                gap >= Duration::minutes(5),
                "Gap between timestamps[{i}] and [{prev}] is {gap_min} mins, expected >= 5",
                prev = i - 1,
                gap_min = gap.num_minutes()
            );
        }

        // Timestamps should be in ascending order
        for i in 1..timestamps.len() {
            assert!(timestamps[i] >= timestamps[i - 1]);
        }
    }

    #[test]
    fn test_skip_range_check_single_commit() {
        let (_temp_dir, repo_path) = create_test_repo(); // single commit
        let args = Args {
            repo_path: Some(repo_path),
            email: Some("test@example.com".to_string()),
            name: Some("Test User".to_string()),
            start: Some("2023-01-01 00:00:00".to_string()),
            end: Some("2023-01-01 00:01:00".to_string()), // 1 minute
            skip_range_check: true,
            ..Default::default()
        };

        let result = generate_timestamps(&args);
        assert!(result.is_ok());

        let timestamps = result.unwrap();
        assert_eq!(timestamps.len(), 1);
    }

    fn dt(s: &str) -> NaiveDateTime {
        NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S").unwrap()
    }

    fn assert_strictly_increasing_within(
        ts: &[NaiveDateTime],
        start: NaiveDateTime,
        end: NaiveDateTime,
    ) {
        assert_eq!(ts.first(), Some(&start));
        assert_eq!(ts.last(), Some(&end));
        assert!(ts.windows(2).all(|w| w[0] < w[1]), "{ts:?}");
    }

    #[test]
    fn test_spread_hits_both_bounds_with_3h_gaps() {
        let (start, end) = (dt("2024-01-01 00:00:00"), dt("2024-01-03 00:00:00"));
        for _ in 0..50 {
            let ts = spread_timestamps(start, end, 7, false).unwrap();
            assert_strictly_increasing_within(&ts, start, end);
            assert!(ts
                .windows(2)
                .all(|w| (w[1] - w[0]).num_seconds() >= 3 * 3600));
        }
    }

    #[test]
    fn test_spread_tiny_range_never_duplicates() {
        let (start, end) = (dt("2024-01-01 00:00:00"), dt("2024-01-01 00:00:04"));
        let ts = spread_timestamps(start, end, 5, true).unwrap();
        assert_strictly_increasing_within(&ts, start, end);
        assert!(spread_timestamps(start, end, 6, true).is_err());
    }

    #[test]
    fn test_random_split_sums_exactly() {
        let parts = random_split(1001, &[0.3, 0.3, 0.4]);
        assert_eq!(parts.iter().sum::<i64>(), 1001);
        assert_eq!(even_split(10, 3), vec![4, 3, 3]);
    }
}