Skip to content

Commit 3bab236

Browse files
committed
Issue #139: Add support for Copy mode and improve the structure of DataProvider
1 parent b30ee49 commit 3bab236

File tree

3 files changed

+106
-228
lines changed

3 files changed

+106
-228
lines changed

docs/structure/tables.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ show tables
5555

5656
## Diffs Changes table
5757

58-
| Name | Type | Description |
59-
| ---------- | ------- | ------------------------------------------------------------------------ |
60-
| commit_id | Text | Commit id |
61-
| insertions | Integer | Number of inserted lines in one change |
62-
| removals | Integer | Number of deleted lines in one change |
63-
| mode | Text | Change mode A for Add, D for Delete, M for Modification or R for Rewrite |
64-
| path | Text | Location of the change |
65-
| repo | Text | Repository full path |
58+
| Name | Type | Description |
59+
| ---------- | ------- | ------------------------------------------------------------------------------------ |
60+
| commit_id | Text | Commit id |
61+
| insertions | Integer | Number of inserted lines in one change |
62+
| removals | Integer | Number of deleted lines in one change |
63+
| mode | Text | Change mode A for Add, D for Delete, M for Modification, C for Copy or R for Rewrite |
64+
| path | Text | Location of the change |
65+
| repo | Text | Repository full path |
6666

6767
---
6868

src/gitql/gitql_data_provider.rs

+10-209
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ use gitql_core::values::text::TextValue;
1010
use gitql_engine::data_provider::DataProvider;
1111

1212
use gix::diff::blob::pipeline::Mode;
13-
use gix::object::tree::diff::Change;
1413
use gix::refs::Category;
1514

1615
use super::values::diff_changes::DiffChange;
17-
use super::values::diff_changes::DiffChangeInfo;
18-
use super::values::diff_changes::DiffChangeKind;
1916
use super::values::diff_changes::DiffChangesValue;
2017

2118
pub struct GitQLDataProvider {
@@ -341,115 +338,11 @@ fn select_diffs(repo: &gix::Repository, selected_columns: &[String]) -> Result<V
341338
&mut rewrite_cache,
342339
|change| {
343340
files_changed += usize::from(change.entry_mode().is_no_tree());
344-
match change {
345-
Change::Addition {
346-
location,
347-
entry_mode: _,
348-
relation: _,
349-
id,
350-
} => {
351-
let mut diff_change = DiffChange::new(DiffChangeKind::Addition);
352-
diff_change.location = location.to_string();
353-
if let Ok(object) = repo.find_object(id) {
354-
if let Ok(blob) = object.try_into_blob() {
355-
diff_change.content = blob.data.clone();
356-
}
357-
}
358-
359-
if let Ok(mut platform) = change.diff(&mut diff_cache) {
360-
if let Ok(Some(counts)) = platform.line_counts() {
361-
diff_change.insertions += counts.insertions;
362-
diff_change.removals += counts.removals;
363-
}
364-
}
365-
366-
insertions += diff_change.insertions;
367-
removals += diff_change.removals;
368-
diff_changes.push(diff_change);
369-
}
370-
Change::Deletion {
371-
location,
372-
entry_mode: _,
373-
relation: _,
374-
id,
375-
} => {
376-
let mut diff_change = DiffChange::new(DiffChangeKind::Deletion);
377-
diff_change.location = location.to_string();
378-
if let Ok(object) = repo.find_object(id) {
379-
if let Ok(blob) = object.try_into_blob() {
380-
diff_change.content = blob.data.clone();
381-
}
382-
}
383-
384-
if let Ok(mut platform) = change.diff(&mut diff_cache) {
385-
if let Ok(Some(counts)) = platform.line_counts() {
386-
diff_change.insertions += counts.insertions;
387-
diff_change.removals += counts.removals;
388-
}
389-
}
390-
391-
insertions += diff_change.insertions;
392-
removals += diff_change.removals;
393-
diff_changes.push(diff_change);
394-
}
395-
Change::Modification {
396-
location,
397-
previous_entry_mode: _,
398-
previous_id: _,
399-
entry_mode: _,
400-
id,
401-
} => {
402-
let mut diff_change =
403-
DiffChange::new(DiffChangeKind::Modification);
404-
diff_change.location = location.to_string();
405-
if let Ok(object) = repo.find_object(id) {
406-
if let Ok(blob) = object.try_into_blob() {
407-
diff_change.content = blob.data.clone();
408-
}
409-
}
410-
411-
if let Ok(mut platform) = change.diff(&mut diff_cache) {
412-
if let Ok(Some(counts)) = platform.line_counts() {
413-
diff_change.insertions += counts.insertions;
414-
diff_change.removals += counts.removals;
415-
}
416-
}
417-
418-
insertions += diff_change.insertions;
419-
removals += diff_change.removals;
420-
diff_changes.push(diff_change);
421-
}
422-
Change::Rewrite {
423-
source_location: _,
424-
source_relation: _,
425-
source_entry_mode: _,
426-
source_id: _,
427-
diff,
428-
entry_mode: _,
429-
location,
430-
id,
431-
relation: _,
432-
copy: _,
433-
} => {
434-
let mut diff_change = DiffChange::new(DiffChangeKind::Rewrite);
435-
diff_change.location = location.to_string();
436-
if let Ok(object) = repo.find_object(id) {
437-
if let Ok(blob) = object.try_into_blob() {
438-
diff_change.content = blob.data.clone();
439-
}
440-
}
441-
442-
if let Some(diff_line_stats) = diff {
443-
diff_change.insertions += diff_line_stats.insertions;
444-
diff_change.removals += diff_line_stats.removals;
445-
446-
insertions += diff_line_stats.insertions;
447-
removals += diff_line_stats.removals;
448-
}
449-
450-
diff_changes.push(diff_change);
451-
}
452-
}
341+
let diff_change =
342+
DiffChange::new_with_content(&change, &mut diff_cache, &repo);
343+
insertions += diff_change.insertions;
344+
removals += diff_change.removals;
345+
diff_changes.push(diff_change);
453346
Ok::<_, Infallible>(Default::default())
454347
},
455348
);
@@ -559,101 +452,7 @@ fn select_diffs_changes(
559452
&parent,
560453
&mut rewrite_cache,
561454
|change| {
562-
let diff_change = match change {
563-
Change::Addition {
564-
location,
565-
entry_mode: _,
566-
relation: _,
567-
id: _,
568-
} => {
569-
let mut change_info = DiffChangeInfo {
570-
path: location.to_string(),
571-
insertions: 0,
572-
removals: 0,
573-
mode: 'A',
574-
};
575-
576-
if let Ok(mut platform) = change.diff(&mut diff_cache) {
577-
if let Ok(Some(counts)) = platform.line_counts() {
578-
change_info.insertions += counts.insertions;
579-
change_info.removals += counts.removals;
580-
}
581-
}
582-
583-
change_info
584-
}
585-
Change::Deletion {
586-
location,
587-
entry_mode: _,
588-
relation: _,
589-
id: _,
590-
} => {
591-
let mut change_info = DiffChangeInfo {
592-
path: location.to_string(),
593-
insertions: 0,
594-
removals: 0,
595-
mode: 'D',
596-
};
597-
598-
if let Ok(mut platform) = change.diff(&mut diff_cache) {
599-
if let Ok(Some(counts)) = platform.line_counts() {
600-
change_info.insertions += counts.insertions;
601-
change_info.removals += counts.removals;
602-
}
603-
}
604-
605-
change_info
606-
}
607-
Change::Modification {
608-
location,
609-
previous_entry_mode: _,
610-
previous_id: _,
611-
entry_mode: _,
612-
id: _,
613-
} => {
614-
let mut change_info = DiffChangeInfo {
615-
path: location.to_string(),
616-
insertions: 0,
617-
removals: 0,
618-
mode: 'M',
619-
};
620-
621-
if let Ok(mut platform) = change.diff(&mut diff_cache) {
622-
if let Ok(Some(counts)) = platform.line_counts() {
623-
change_info.insertions += counts.insertions;
624-
change_info.removals += counts.removals;
625-
}
626-
}
627-
628-
change_info
629-
}
630-
Change::Rewrite {
631-
source_location: _,
632-
source_relation: _,
633-
source_entry_mode: _,
634-
source_id: _,
635-
diff,
636-
entry_mode: _,
637-
location,
638-
id: _,
639-
relation: _,
640-
copy: _,
641-
} => {
642-
let mut change_info = DiffChangeInfo {
643-
path: location.to_string(),
644-
insertions: 0,
645-
removals: 0,
646-
mode: 'R',
647-
};
648-
649-
if let Some(diff_line_stats) = diff {
650-
change_info.insertions += diff_line_stats.insertions;
651-
change_info.removals += diff_line_stats.removals;
652-
}
653-
654-
change_info
655-
}
656-
};
455+
let diff_change = DiffChange::new_without_content(&change, &mut diff_cache);
657456

658457
let mut values: Vec<Box<dyn Value>> =
659458
Vec::with_capacity(selected_columns_len);
@@ -674,12 +473,14 @@ fn select_diffs_changes(
674473
}
675474

676475
if column_name == "mode" {
677-
values.push(Box::new(TextValue::new(diff_change.mode.to_string())));
476+
let mode = diff_change.kind.mode().to_string();
477+
values.push(Box::new(TextValue::new(mode)));
678478
continue;
679479
}
680480

681481
if column_name == "path" {
682-
values.push(Box::new(TextValue::new(diff_change.path.to_string())));
482+
let path = diff_change.location.to_string();
483+
values.push(Box::new(TextValue::new(path)));
683484
continue;
684485
}
685486

0 commit comments

Comments
 (0)