-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathgit_test.rs
More file actions
477 lines (397 loc) · 17.9 KB
/
git_test.rs
File metadata and controls
477 lines (397 loc) · 17.9 KB
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
use code2prompt_core::git::{
get_git_diff, get_git_diff_between_branches, get_git_diff_file_paths, get_git_log,
};
#[cfg(test)]
mod tests {
use super::*;
use git2::{Repository, RepositoryInitOptions, Signature};
use std::fs;
use tempfile::TempDir;
#[test]
fn test_get_git_diff() {
// Create a temporary directory
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let repo_path = temp_dir.path();
// Initialize a new Git repository
let repo = Repository::init(repo_path).expect("Failed to initialize repository");
// Create a new file in the repository
let file_path = repo_path.join("test_file.txt");
fs::write(&file_path, "Initial content").expect("Failed to write to test file");
// Stage and commit the new file
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
let signature =
Signature::now("Test", "test@example.com").expect("Failed to create signature");
repo.commit(
Some("HEAD"),
&signature,
&signature,
"Initial commit",
&tree,
&[],
)
.expect("Failed to commit");
// Modify the file
fs::write(&file_path, "Modified content").expect("Failed to modify test file");
// Add the modified file to the index again
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
// Get the git diff using the function from the module
let diff = get_git_diff(repo_path).expect("Failed to get git diff");
// Print the diff for debugging
println!("Generated diff:\n{}", diff);
// Assert that the diff contains the expected content
assert!(diff.contains("Modified content"));
}
#[test]
fn test_get_git_diff_between_branches() {
// Create a temporary directory
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let repo_path = temp_dir.path();
// Initialize a new Git repository
let mut binding = RepositoryInitOptions::new();
let init_options = binding.initial_head("master");
let repo = Repository::init_opts(repo_path, init_options)
.expect("Failed to initialize repository");
// Create a new file in the repository
let file_path = repo_path.join("test_file.txt");
fs::write(&file_path, "Initial content").expect("Failed to write to test file");
// Stage and commit the new file
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
let signature =
Signature::now("Test", "test@example.com").expect("Failed to create signature");
let master_commit = repo
.commit(
Some("HEAD"),
&signature,
&signature,
"Initial commit in master branch",
&tree,
&[],
)
.expect("Failed to commit");
// Create a new branch and make a commit on the master branch
repo.branch(
"development",
&repo
.find_commit(master_commit)
.expect("Failed to find commit"),
false,
)
.expect("Failed to create new branch");
// Modify the file in the new branch
repo.set_head("refs/heads/development")
.expect("Failed to set HEAD");
repo.checkout_head(None).expect("Failed to checkout HEAD");
fs::write(&file_path, "Content in new branch")
.expect("Failed to modify test file in new branch");
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
repo.commit(
Some("HEAD"),
&signature,
&signature,
"New commit in branch development",
&tree,
&[&repo
.find_commit(master_commit)
.expect("Failed to find commit")],
)
.expect("Failed to commit in new branch");
// Get the git diff between branches
let diff = get_git_diff_between_branches(repo_path, "master", "development")
.expect("Failed to get git diff between branches");
// Print the diff for debugging
println!("Generated diff between branches:\n{}", diff);
// Assert that the diff contains the expected content
assert!(diff.contains("Initial content"));
assert!(diff.contains("Content in new branch"));
}
#[test]
fn test_get_git_log() {
// Create a temporary directory
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let repo_path = temp_dir.path();
// Initialize a new Git repository
let mut binding = RepositoryInitOptions::new();
let init_options = binding.initial_head("master");
let repo = Repository::init_opts(repo_path, init_options)
.expect("Failed to initialize repository");
// Create a new file in the repository
let file_path = repo_path.join("test_file.txt");
fs::write(&file_path, "Initial content").expect("Failed to write to test file");
// Stage and commit the new file
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
let signature =
Signature::now("Test", "test@example.com").expect("Failed to create signature");
let master_commit = repo
.commit(
Some("HEAD"),
&signature,
&signature,
"Initial commit in branch master",
&tree,
&[],
)
.expect("Failed to commit");
// Create a new branch and make a commit on the master branch
repo.branch(
"development",
&repo
.find_commit(master_commit)
.expect("Failed to find commit"),
false,
)
.expect("Failed to create new branch");
// Modify the file in the new branch
repo.set_head("refs/heads/development")
.expect("Failed to set HEAD");
repo.checkout_head(None).expect("Failed to checkout HEAD");
fs::write(&file_path, "Content in development")
.expect("Failed to modify test file in new branch");
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
repo.commit(
Some("HEAD"),
&signature,
&signature,
"First commit in development",
&tree,
&[&repo
.find_commit(master_commit)
.expect("Failed to find commit")],
)
.expect("Failed to commit in new branch");
// Make a second commit in the development branch
fs::write(&file_path, "Second content in development")
.expect("Failed to modify test file in new branch");
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
repo.commit(
Some("HEAD"),
&signature,
&signature,
"Second commit in development",
&tree,
&[&repo
.find_commit(repo.head().unwrap().target().unwrap())
.expect("Failed to find commit")],
)
.expect("Failed to commit second change in new branch");
// Get the git log between branches
let log = get_git_log(repo_path, "master", "development")
.expect("Failed to get git log between branches");
// Print the log for debugging
println!("Generated git log:\n{}", log);
// Assert that the log contains the expected content
assert!(log.contains("First commit in development"));
assert!(log.contains("Second commit in development"));
}
#[test]
fn test_get_git_diff_file_paths() {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let repo_path = temp_dir.path();
let mut binding = RepositoryInitOptions::new();
let init_options = binding.initial_head("master");
let repo = Repository::init_opts(repo_path, init_options)
.expect("Failed to initialize repository");
// Create two files on master
let file_a = repo_path.join("file_a.txt");
let file_b = repo_path.join("file_b.txt");
fs::write(&file_a, "content a").expect("Failed to write file_a");
fs::write(&file_b, "content b").expect("Failed to write file_b");
let mut index = repo.index().expect("Failed to get index");
index
.add_path(std::path::Path::new("file_a.txt"))
.expect("Failed to add file_a");
index
.add_path(std::path::Path::new("file_b.txt"))
.expect("Failed to add file_b");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
let sig = Signature::now("Test", "test@example.com").expect("Failed to create signature");
let master_commit = repo
.commit(Some("HEAD"), &sig, &sig, "initial commit", &tree, &[])
.expect("Failed to commit");
// Create dev branch, modify file_a and add file_c
repo.branch(
"dev",
&repo.find_commit(master_commit).expect("Failed to find commit"),
false,
)
.expect("Failed to create branch");
repo.set_head("refs/heads/dev").expect("Failed to set HEAD");
repo.checkout_head(None).expect("Failed to checkout");
fs::write(&file_a, "modified content a").expect("Failed to modify file_a");
let file_c = repo_path.join("file_c.txt");
fs::write(&file_c, "content c").expect("Failed to write file_c");
let mut index = repo.index().expect("Failed to get index");
index
.add_path(std::path::Path::new("file_a.txt"))
.expect("Failed to add file_a");
index
.add_path(std::path::Path::new("file_c.txt"))
.expect("Failed to add file_c");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
repo.commit(
Some("HEAD"),
&sig,
&sig,
"dev changes",
&tree,
&[&repo.find_commit(master_commit).expect("Failed to find commit")],
)
.expect("Failed to commit on dev");
let paths = get_git_diff_file_paths(repo_path, "master", "dev")
.expect("Failed to get diff file paths");
assert!(
paths.contains(&std::path::PathBuf::from("file_a.txt")),
"should contain modified file_a.txt"
);
assert!(
paths.contains(&std::path::PathBuf::from("file_c.txt")),
"should contain added file_c.txt"
);
assert!(
!paths.contains(&std::path::PathBuf::from("file_b.txt")),
"should not contain unchanged file_b.txt"
);
}
#[test]
fn test_git_diff_with_commit_hashes_and_tags() {
// Create a temporary directory
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let repo_path = temp_dir.path();
// Initialize a new Git repository
let mut binding = RepositoryInitOptions::new();
let init_options = binding.initial_head("master");
let repo = Repository::init_opts(repo_path, init_options)
.expect("Failed to initialize repository");
// Create a new file in the repository
let file_path = repo_path.join("test_file.txt");
fs::write(&file_path, "Initial content").expect("Failed to write to test file");
// Stage and commit the new file
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
let signature =
Signature::now("Test", "test@example.com").expect("Failed to create signature");
let first_commit_id = repo
.commit(
Some("HEAD"),
&signature,
&signature,
"First commit",
&tree,
&[],
)
.expect("Failed to commit");
// Create a tag for the first commit
let first_commit = repo
.find_commit(first_commit_id)
.expect("Failed to find first commit");
repo.tag(
"v1.0.0",
first_commit.as_object(),
&signature,
"Version 1.0.0",
false,
)
.expect("Failed to create tag");
// Make a second commit
fs::write(&file_path, "Modified content").expect("Failed to modify test file");
let mut index = repo.index().expect("Failed to get repository index");
index
.add_path(file_path.strip_prefix(repo_path).unwrap())
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
let second_commit_id = repo
.commit(
Some("HEAD"),
&signature,
&signature,
"Second commit",
&tree,
&[&first_commit],
)
.expect("Failed to commit second change");
// Test 1: Diff between commit hashes (full hash)
let first_commit_hash = first_commit_id.to_string();
let second_commit_hash = second_commit_id.to_string();
let diff_full_hash =
get_git_diff_between_branches(repo_path, &first_commit_hash, &second_commit_hash)
.expect("Failed to get git diff between full commit hashes");
assert!(diff_full_hash.contains("Initial content"));
assert!(diff_full_hash.contains("Modified content"));
// Test 2: Diff between abbreviated commit hashes
let first_commit_short = &first_commit_hash[..7];
let second_commit_short = &second_commit_hash[..7];
let diff_short_hash =
get_git_diff_between_branches(repo_path, first_commit_short, second_commit_short)
.expect("Failed to get git diff between abbreviated commit hashes");
assert!(diff_short_hash.contains("Initial content"));
assert!(diff_short_hash.contains("Modified content"));
// Test 3: Diff between tag and commit hash
let diff_tag_to_hash =
get_git_diff_between_branches(repo_path, "v1.0.0", &second_commit_hash)
.expect("Failed to get git diff between tag and commit hash");
assert!(diff_tag_to_hash.contains("Initial content"));
assert!(diff_tag_to_hash.contains("Modified content"));
// Test 4: Diff between tag and HEAD
let diff_tag_to_head = get_git_diff_between_branches(repo_path, "v1.0.0", "HEAD")
.expect("Failed to get git diff between tag and HEAD");
assert!(diff_tag_to_head.contains("Initial content"));
assert!(diff_tag_to_head.contains("Modified content"));
// Test 5: Error case - invalid reference should still fail
let result = get_git_diff_between_branches(repo_path, "nonexistent_reference", "HEAD");
assert!(result.is_err());
assert!(result
.unwrap_err()
.to_string()
.contains("Branch nonexistent_reference doesn't exist!"));
}
}