Skip to content

Commit 95a9cba

Browse files
committed
Issue #139: Implement DIFF_CHANGED_FILES diff function
1 parent a9ecd9e commit 95a9cba

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

crates/gitql-core/src/values/array.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ impl ArrayValue {
3535
impl Value for ArrayValue {
3636
fn literal(&self) -> String {
3737
let mut str = String::new();
38-
let last_position = self.values.len() - 1;
38+
let elements = &self.values;
39+
if elements.is_empty() {
40+
return "[]".to_string();
41+
}
42+
3943
str += "[";
40-
for (pos, element) in self.values.iter().enumerate() {
44+
for (pos, element) in elements.iter().enumerate() {
4145
str += &element.literal();
42-
if pos != last_position {
46+
if pos + 1 != elements.len() {
4347
str += ", ";
4448
}
4549
}

docs/gitql_functions.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ those functions are available only in the gitql application.
1111

1212
### GitQL Diffs functions
1313

14-
| Name | Parameters | Return | Description |
15-
| ---------------------------------- | ----------------- | ------- | ------------------------------------------------------------------------ |
16-
| DIFF_CONTENT | DiffChanges | Text | Return the full content of all changes appended together. |
17-
| DIFF_ADDED_CONTENT | DiffChanges | Text | Return the added content of all changes appended together. |
18-
| DIFF_DELETED_CONTENT | DiffChanges | Text | Return the deleted content of all changes appended together. |
19-
| DIFF_MODIFIED_CONTENT | DiffChanges | Text | Return the modified content of all changes appended together. |
20-
| DIFF_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the all content of changes contains second argument. |
21-
| DIFF_ADDED_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the added content of changes contains second argument. |
22-
| DIFF_DELETED_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the deleted content of changes contains second argument. |
23-
| DIFF_MODIFICATION_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the modified content of changes contains second argument. |
24-
| DIFF_FILES_COUNT | DiffChanges | Integer | Return number of unique files changes in this commit. |
25-
| IS_DIFF_HAS_FILE | DiffChanges, Text | Boolean | Return true if this diff changes contains file. |
14+
| Name | Parameters | Return | Description |
15+
| ---------------------------------- | ----------------- | ----------- | ------------------------------------------------------------------------ |
16+
| DIFF_CONTENT | DiffChanges | Text | Return the full content of all changes appended together. |
17+
| DIFF_ADDED_CONTENT | DiffChanges | Text | Return the added content of all changes appended together. |
18+
| DIFF_DELETED_CONTENT | DiffChanges | Text | Return the deleted content of all changes appended together. |
19+
| DIFF_MODIFIED_CONTENT | DiffChanges | Text | Return the modified content of all changes appended together. |
20+
| DIFF_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the all content of changes contains second argument. |
21+
| DIFF_ADDED_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the added content of changes contains second argument. |
22+
| DIFF_DELETED_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the deleted content of changes contains second argument. |
23+
| DIFF_MODIFICATION_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the modified content of changes contains second argument. |
24+
| DIFF_CHANGED_FILES | DiffChanges | Array<Text> | Return changes files in this change as array of strings. |
25+
| DIFF_FILES_COUNT | DiffChanges | Integer | Return number of unique files changes in this commit. |
26+
| IS_DIFF_HAS_FILE | DiffChanges, Text | Boolean | Return true if this diff changes contains file. |

src/gitql/functions/diffs.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::collections::HashMap;
22
use std::collections::HashSet;
33

4+
use gitql_ast::types::array::ArrayType;
45
use gitql_ast::types::boolean::BoolType;
56
use gitql_ast::types::integer::IntType;
67
use gitql_ast::types::text::TextType;
78
use gitql_core::signature::Signature;
89
use gitql_core::signature::StandardFunction;
10+
use gitql_core::values::array::ArrayValue;
911
use gitql_core::values::base::Value;
1012
use gitql_core::values::boolean::BoolValue;
1113
use gitql_core::values::integer::IntValue;
@@ -36,6 +38,7 @@ pub(crate) fn register_diffs_functions(map: &mut HashMap<&'static str, StandardF
3638
diff_changes_modified_content_contains,
3739
);
3840

41+
map.insert("diff_changed_files", diff_changed_files);
3942
map.insert("diff_files_count", diff_changes_files_count);
4043

4144
map.insert("is_diff_has_file", diff_changes_contains_file);
@@ -91,6 +94,12 @@ pub(crate) fn register_diffs_function_signatures(map: &mut HashMap<&'static str,
9194
.add_parameter(Box::new(TextType)),
9295
);
9396

97+
map.insert(
98+
"diff_changed_files",
99+
Signature::with_return(Box::new(ArrayType::new(Box::new(TextType))))
100+
.add_parameter(Box::new(DiffChangesType)),
101+
);
102+
94103
map.insert(
95104
"diff_files_count",
96105
Signature::with_return(Box::new(IntType)).add_parameter(Box::new(DiffChangesType)),
@@ -217,6 +226,17 @@ fn diff_changes_modified_content_contains(values: &[Box<dyn Value>]) -> Box<dyn
217226
Box::new(BoolValue::new_false())
218227
}
219228

229+
fn diff_changed_files(values: &[Box<dyn Value>]) -> Box<dyn Value> {
230+
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
231+
let mut elements: Vec<Box<dyn Value>> = vec![];
232+
for change in changes.changes.iter() {
233+
elements.push(Box::new(TextValue::new(change.location.to_string())));
234+
}
235+
return Box::new(ArrayValue::new(elements, Box::new(TextType)));
236+
}
237+
Box::new(ArrayValue::empty(Box::new(TextType)))
238+
}
239+
220240
fn diff_changes_files_count(values: &[Box<dyn Value>]) -> Box<dyn Value> {
221241
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
222242
let mut unique_files: HashSet<&String> = HashSet::new();

0 commit comments

Comments
 (0)