Skip to content

Commit

Permalink
feat: Implement diff content and content search functions in GitQL App
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Dec 25, 2024
1 parent 9172b72 commit 83cbab5
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 12 deletions.
17 changes: 12 additions & 5 deletions docs/gitql_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ those functions are available only in the gitql application.

### GitQL Diffs functions

| Name | Parameters | Return | Description |
| ------------------------ | ----------------- | ------- | --------------------------------------------------------- |
| DIFF_CHANGES_CONTENT | DiffChanges | Text | Return the full content of all changes appended together. |
| DIFF_CHANGES_FILES_COUNT | DiffChanges | Integer | Return number of unique files changes in this commit. |
| IS_DIFF_CHANGES_HAS_FILE | DiffChanges, Text | Boolean | Return true if this diff changes contains file. |
| Name | Parameters | Return | Description |
| ---------------------------------- | ----------------- | ------- | ------------------------------------------------------------------------ |
| DIFF_CONTENT | DiffChanges | Text | Return the full content of all changes appended together. |
| DIFF_ADDED_CONTENT | DiffChanges | Text | Return the added content of all changes appended together. |
| DIFF_DELETED_CONTENT | DiffChanges | Text | Return the deleted content of all changes appended together. |
| DIFF_MODIFIED_CONTENT | DiffChanges | Text | Return the modified content of all changes appended together. |
| DIFF_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the all content of changes contains second argument. |
| DIFF_ADDED_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the added content of changes contains second argument. |
| DIFF_DELETED_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the deleted content of changes contains second argument. |
| DIFF_MODIFICATION_CONTENT_CONTAINS | DiffChanges, Text | Text | Return true if the modified content of changes contains second argument. |
| DIFF_FILES_COUNT | DiffChanges | Integer | Return number of unique files changes in this commit. |
| IS_DIFF_HAS_FILE | DiffChanges, Text | Boolean | Return true if this diff changes contains file. |
199 changes: 193 additions & 6 deletions src/gitql/functions/diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,111 @@ use gitql_core::values::integer::IntValue;
use gitql_core::values::text::TextValue;

use crate::gitql::types::diff_changes::DiffChangesType;
use crate::gitql::values::diff_changes::DiffChangeKind;
use crate::gitql::values::diff_changes::DiffChangesValue;

#[inline(always)]
pub(crate) fn register_diffs_functions(map: &mut HashMap<&'static str, StandardFunction>) {
map.insert("diff_changes_content", diff_changes_full_content);
map.insert("diff_content", diff_changes_full_content);
map.insert("diff_added_content", diff_changes_added_content);
map.insert("diff_deleted_content", diff_changes_deleted_content);
map.insert("diff_modified_content", diff_changes_modified_content);

map.insert("diff_changes_files_count", diff_changes_files_count);
map.insert("diff_content_contains", diff_changes_full_content_contains);
map.insert(
"diff_added_content_contains",
diff_changes_added_content_contains,
);
map.insert(
"diff_deleted_content_contains",
diff_changes_deleted_content_contains,
);
map.insert(
"diff_modified_content_contains",
diff_changes_modified_content_contains,
);

map.insert("diff_files_count", diff_changes_files_count);

map.insert("is_diff_changes_has_file", diff_changes_contains_file);
map.insert("is_diff_has_file", diff_changes_contains_file);
}

#[inline(always)]
pub(crate) fn register_diffs_function_signatures(map: &mut HashMap<&'static str, Signature>) {
map.insert(
"diff_changes_content",
"diff_content",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_added_content",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_deleted_content",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_changes_files_count",
"diff_modified_content",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_content_contains",
Signature {
parameters: vec![Box::new(DiffChangesType), Box::new(TextType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_added_content_contains",
Signature {
parameters: vec![Box::new(DiffChangesType), Box::new(TextType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_deleted_content_contains",
Signature {
parameters: vec![Box::new(DiffChangesType), Box::new(TextType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_modified_content_contains",
Signature {
parameters: vec![Box::new(DiffChangesType), Box::new(TextType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_files_count",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(IntType),
},
);

map.insert(
"is_diff_changes_has_file",
"is_diff_has_file",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(BoolType),
Expand All @@ -64,6 +138,119 @@ fn diff_changes_full_content(values: &[Box<dyn Value>]) -> Box<dyn Value> {
})
}

fn diff_changes_added_content(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let mut content = String::new();
for change in changes.changes.iter() {
if change.kind == DiffChangeKind::Addition {
content += &String::from_utf8_lossy(&change.content);
}
}

return Box::new(TextValue { value: content });
}

Box::new(TextValue {
value: String::default(),
})
}

fn diff_changes_deleted_content(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let mut content = String::new();
for change in changes.changes.iter() {
if change.kind == DiffChangeKind::Deletion {
content += &String::from_utf8_lossy(&change.content);
}
}

return Box::new(TextValue { value: content });
}

Box::new(TextValue {
value: String::default(),
})
}

fn diff_changes_modified_content(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let mut content = String::new();
for change in changes.changes.iter() {
if change.kind == DiffChangeKind::Modification {
content += &String::from_utf8_lossy(&change.content);
}
}

return Box::new(TextValue { value: content });
}

Box::new(TextValue {
value: String::default(),
})
}

fn diff_changes_full_content_contains(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let str = values[1].as_text().unwrap();
let mut content = String::new();
for change in changes.changes.iter() {
content += &String::from_utf8_lossy(&change.content);
}

let is_contains = content.contains(&str);
return Box::new(BoolValue { value: is_contains });
}
Box::new(BoolValue { value: false })
}

fn diff_changes_added_content_contains(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let str = values[1].as_text().unwrap();
let mut content = String::new();
for change in changes.changes.iter() {
if change.kind == DiffChangeKind::Addition {
content += &String::from_utf8_lossy(&change.content);
}
}

let is_contains = content.contains(&str);
return Box::new(BoolValue { value: is_contains });
}
Box::new(BoolValue { value: false })
}

fn diff_changes_deleted_content_contains(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let str = values[1].as_text().unwrap();
let mut content = String::new();
for change in changes.changes.iter() {
if change.kind == DiffChangeKind::Deletion {
content += &String::from_utf8_lossy(&change.content);
}
}

let is_contains = content.contains(&str);
return Box::new(BoolValue { value: is_contains });
}
Box::new(BoolValue { value: false })
}

fn diff_changes_modified_content_contains(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let str = values[1].as_text().unwrap();
let mut content = String::new();
for change in changes.changes.iter() {
if change.kind == DiffChangeKind::Modification {
content += &String::from_utf8_lossy(&change.content);
}
}

let is_contains = content.contains(&str);
return Box::new(BoolValue { value: is_contains });
}
Box::new(BoolValue { value: false })
}

fn diff_changes_files_count(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let mut unique_files: HashSet<&String> = HashSet::new();
Expand Down
2 changes: 1 addition & 1 deletion src/gitql/values/diff_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gitql_core::values::base::Value;

use crate::gitql::types::diff_changes::DiffChangesType;

#[derive(Clone)]
#[derive(PartialEq, Clone)]
pub enum DiffChangeKind {
Addition,
Deletion,
Expand Down

0 comments on commit 83cbab5

Please sign in to comment.