git_editor/utils/
validator.rs

1use crate::args::Args;
2use crate::utils::types::Result;
3use regex::Regex;
4use url::Url;
5
6pub fn validate_inputs(args: &Args) -> Result<()> {
7    // Always validate repo_path since it's required for all operations
8    let repo_path = args.repo_path.as_ref().unwrap();
9
10    if repo_path.is_empty() {
11        return Err("Repository path cannot be empty".into());
12    }
13    if Url::parse(repo_path).is_err() && !std::path::Path::new(repo_path).exists() {
14        return Err(format!("Invalid repository path or URL: {repo_path}").into());
15    }
16    if std::path::Path::new(repo_path).exists() {
17        if !std::path::Path::new(repo_path).is_dir() {
18            return Err(format!("Repository path is not a directory: {repo_path}").into());
19        }
20        if !std::path::Path::new(repo_path).join(".git").exists() {
21            return Err(format!(
22                "Repository path does not contain a valid Git repository: {repo_path}"
23            )
24            .into());
25        }
26    }
27
28    // Skip validation for email, name, start, end if using show_history, pick_specific_commits, range, or simulate
29    if args.show_history || args.pick_specific_commits || args.range || args.simulate {
30        return Ok(());
31    }
32
33    // Validate email, name, start, end only for full rewrite operations
34    let email = args.email.as_ref().unwrap();
35    let name = args.name.as_ref().unwrap();
36    let start = args.start.as_ref().unwrap();
37    let end = args.end.as_ref().unwrap();
38
39    let email_re = Regex::new(r"(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$")?;
40    if !email_re.is_match(email) {
41        return Err(format!("Invalid email format: {email}").into());
42    }
43
44    if name.trim().is_empty() {
45        return Err("Name cannot be empty".into());
46    }
47
48    let start_re = Regex::new(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$")?;
49    if !start_re.is_match(start) {
50        return Err(
51            format!("Invalid start date format (expected YYYY-MM-DD HH:MM:SS): {start}").into(),
52        );
53    }
54
55    let end_re = Regex::new(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$")?;
56    if !end_re.is_match(end) {
57        return Err(
58            format!("Invalid end date format (expected YYYY-MM-DD HH:MM:SS): {end}").into(),
59        );
60    }
61
62    if start >= end {
63        return Err("Start date must be before end date".into());
64    }
65
66    Ok(())
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use std::fs;
73    use tempfile::TempDir;
74
75    fn create_test_repo() -> (TempDir, String) {
76        let temp_dir = TempDir::new().unwrap();
77        let repo_path = temp_dir.path().to_str().unwrap().to_string();
78
79        // Initialize git repo
80        let repo = git2::Repository::init(&repo_path).unwrap();
81
82        // Create a test file
83        let file_path = temp_dir.path().join("test.txt");
84        fs::write(&file_path, "test content").unwrap();
85
86        // Add and commit file
87        let mut index = repo.index().unwrap();
88        index.add_path(std::path::Path::new("test.txt")).unwrap();
89        index.write().unwrap();
90
91        let tree_id = index.write_tree().unwrap();
92        let tree = repo.find_tree(tree_id).unwrap();
93
94        let sig = git2::Signature::new(
95            "Test User",
96            "test@example.com",
97            &git2::Time::new(1234567890, 0),
98        )
99        .unwrap();
100        repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
101            .unwrap();
102
103        (temp_dir, repo_path)
104    }
105
106    #[test]
107    fn test_validate_inputs_show_history_mode() {
108        let (_temp_dir, repo_path) = create_test_repo();
109        let args = Args {
110            repo_path: Some(repo_path),
111            email: None,
112            name: None,
113            start: None,
114            end: None,
115            show_history: true,
116            pick_specific_commits: false,
117            range: false,
118            simulate: false,
119            show_diff: false,
120        };
121
122        let result = validate_inputs(&args);
123        assert!(result.is_ok());
124    }
125
126    #[test]
127    fn test_validate_inputs_pick_specific_commits_mode() {
128        let (_temp_dir, repo_path) = create_test_repo();
129        let args = Args {
130            repo_path: Some(repo_path),
131            email: None,
132            name: None,
133            start: None,
134            end: None,
135            show_history: false,
136            pick_specific_commits: true,
137            range: false,
138            simulate: false,
139            show_diff: false,
140        };
141
142        let result = validate_inputs(&args);
143        assert!(result.is_ok());
144    }
145
146    #[test]
147    fn test_validate_inputs_full_rewrite_valid() {
148        let (_temp_dir, repo_path) = create_test_repo();
149        let args = Args {
150            repo_path: Some(repo_path),
151            email: Some("test@example.com".to_string()),
152            name: Some("Test User".to_string()),
153            start: Some("2023-01-01 00:00:00".to_string()),
154            end: Some("2023-01-02 00:00:00".to_string()),
155            show_history: false,
156            pick_specific_commits: false,
157            range: false,
158            simulate: false,
159            show_diff: false,
160        };
161
162        let result = validate_inputs(&args);
163        assert!(result.is_ok());
164    }
165
166    #[test]
167    fn test_validate_inputs_invalid_email() {
168        let (_temp_dir, repo_path) = create_test_repo();
169        let _args = Args {
170            repo_path: Some(repo_path),
171            email: Some("invalid-email".to_string()),
172            name: Some("Test User".to_string()),
173            start: Some("2023-01-01 00:00:00".to_string()),
174            end: Some("2023-01-02 00:00:00".to_string()),
175            show_history: false,
176            pick_specific_commits: false,
177            range: false,
178            simulate: false,
179            show_diff: false,
180        };
181
182        // This test would normally call process::exit, so we can't test it directly
183        // without mocking. We'll test the regex separately.
184        let email_re = Regex::new(r"(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$").unwrap();
185        assert!(!email_re.is_match("invalid-email"));
186        assert!(email_re.is_match("test@example.com"));
187    }
188
189    #[test]
190    fn test_validate_inputs_invalid_date_format() {
191        let (_temp_dir, repo_path) = create_test_repo();
192        let _args = Args {
193            repo_path: Some(repo_path),
194            email: Some("test@example.com".to_string()),
195            name: Some("Test User".to_string()),
196            start: Some("invalid-date".to_string()),
197            end: Some("2023-01-02 00:00:00".to_string()),
198            show_history: false,
199            pick_specific_commits: false,
200            range: false,
201            simulate: false,
202            show_diff: false,
203        };
204
205        let start_re = Regex::new(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$").unwrap();
206        assert!(!start_re.is_match("invalid-date"));
207        assert!(start_re.is_match("2023-01-01 00:00:00"));
208    }
209
210    #[test]
211    fn test_validate_inputs_nonexistent_repo() {
212        let _args = Args {
213            repo_path: Some("/nonexistent/path".to_string()),
214            email: Some("test@example.com".to_string()),
215            name: Some("Test User".to_string()),
216            start: Some("2023-01-01 00:00:00".to_string()),
217            end: Some("2023-01-02 00:00:00".to_string()),
218            show_history: false,
219            pick_specific_commits: false,
220            range: false,
221            simulate: false,
222            show_diff: false,
223        };
224
225        // This would normally call process::exit, so we test the path validation logic
226        let repo_path = "/nonexistent/path";
227        assert!(!std::path::Path::new(repo_path).exists());
228    }
229
230    #[test]
231    fn test_email_regex_patterns() {
232        let email_re = Regex::new(r"(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$").unwrap();
233
234        // Valid emails
235        assert!(email_re.is_match("test@example.com"));
236        assert!(email_re.is_match("user.name@domain.org"));
237        assert!(email_re.is_match("user+tag@example.co.uk"));
238        assert!(email_re.is_match("123@test.io"));
239
240        // Invalid emails
241        assert!(!email_re.is_match("invalid-email"));
242        assert!(!email_re.is_match("@domain.com"));
243        assert!(!email_re.is_match("user@"));
244        assert!(!email_re.is_match("user@domain"));
245        assert!(!email_re.is_match("user@domain."));
246    }
247
248    #[test]
249    fn test_datetime_regex_patterns() {
250        let datetime_re = Regex::new(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$").unwrap();
251
252        // Valid datetime formats
253        assert!(datetime_re.is_match("2023-01-01 00:00:00"));
254        assert!(datetime_re.is_match("2023-12-31 23:59:59"));
255        assert!(datetime_re.is_match("2023-06-15 12:30:45"));
256
257        // Invalid datetime formats
258        assert!(!datetime_re.is_match("2023-1-1 0:0:0"));
259        assert!(!datetime_re.is_match("2023/01/01 00:00:00"));
260        assert!(!datetime_re.is_match("2023-01-01T00:00:00"));
261        assert!(!datetime_re.is_match("23-01-01 00:00:00"));
262        assert!(!datetime_re.is_match("2023-01-01 00:00"));
263    }
264
265    fn create_test_repo_with_commits() -> (TempDir, String) {
266        let temp_dir = TempDir::new().unwrap();
267        let repo_path = temp_dir.path().to_str().unwrap().to_string();
268
269        // Initialize git repo
270        let repo = git2::Repository::init(&repo_path).unwrap();
271
272        // Create multiple commits
273        for i in 1..=3 {
274            let file_path = temp_dir.path().join(format!("test{i}.txt"));
275            std::fs::write(&file_path, format!("test content {i}")).unwrap();
276
277            let mut index = repo.index().unwrap();
278            index
279                .add_path(std::path::Path::new(&format!("test{i}.txt")))
280                .unwrap();
281            index.write().unwrap();
282
283            let tree_id = index.write_tree().unwrap();
284            let tree = repo.find_tree(tree_id).unwrap();
285
286            let sig = git2::Signature::new(
287                "Test User",
288                "test@example.com",
289                &git2::Time::new(1234567890 + i as i64 * 3600, 0),
290            )
291            .unwrap();
292
293            let parents = if i == 1 {
294                vec![]
295            } else {
296                let head = repo.head().unwrap();
297                let parent_commit = head.peel_to_commit().unwrap();
298                vec![parent_commit]
299            };
300
301            repo.commit(
302                Some("HEAD"),
303                &sig,
304                &sig,
305                &format!("Commit {i}"),
306                &tree,
307                &parents.iter().collect::<Vec<_>>(),
308            )
309            .unwrap();
310        }
311
312        (temp_dir, repo_path)
313    }
314
315    #[test]
316    fn test_validate_inputs_range_mode() {
317        let (_temp_dir, repo_path) = create_test_repo_with_commits();
318        let args = Args {
319            repo_path: Some(repo_path),
320            email: None,
321            name: None,
322            start: None,
323            end: None,
324            show_history: false,
325            pick_specific_commits: false,
326            range: true,
327            simulate: false,
328            show_diff: false,
329        };
330
331        let result = validate_inputs(&args);
332        assert!(result.is_ok());
333    }
334}