Skip to content

Commit

Permalink
Issue #10: Add repository path as repo column to all data tables
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Aug 31, 2023
1 parent cbd1bc4 commit 9f7541b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 22 deletions.
57 changes: 55 additions & 2 deletions crates/gitql-engine/src/engine_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn select_references(
fields: Vec<String>,
alias_table: HashMap<String, String>,
) -> Vec<GQLObject> {
let repo_path = repo.path().to_str().unwrap().to_string();
let mut gql_references: Vec<GQLObject> = Vec::new();
let git_references = repo.references();
if git_references.is_err() {
Expand Down Expand Up @@ -75,6 +76,15 @@ fn select_references(
}
}

if is_limit_fields_empty || fields.contains(&String::from("repo")) {
let key = alias_table
.get("repo")
.unwrap_or(&"repo".to_string())
.to_string();

attributes.insert(key, repo_path.to_string());
}

let gql_reference = GQLObject { attributes };
gql_references.push(gql_reference);
}
Expand All @@ -87,6 +97,8 @@ fn select_commits(
fields: Vec<String>,
alias_table: HashMap<String, String>,
) -> Vec<GQLObject> {
let repo_path = repo.path().to_str().unwrap().to_string();

let mut commits: Vec<GQLObject> = Vec::new();
let mut revwalk = repo.revwalk().unwrap();
revwalk.push_head().unwrap();
Expand Down Expand Up @@ -145,6 +157,15 @@ fn select_commits(
attributes.insert(key, commit.time().seconds().to_string());
}

if is_limit_fields_empty || fields.contains(&String::from("repo")) {
let key = alias_table
.get("repo")
.unwrap_or(&"repo".to_string())
.to_string();

attributes.insert(key, repo_path.to_string());
}

let gql_commit = GQLObject { attributes };
commits.push(gql_commit);
}
Expand All @@ -165,6 +186,7 @@ fn select_diffs(
let select_insertions = fields.contains(&String::from("insertions"));
let select_deletions = fields.contains(&String::from("deletions"));
let select_file_changed = fields.contains(&String::from("files_changed"));
let repo_path = repo.path().to_str().unwrap().to_string();

for commit_id in revwalk {
let commit = repo.find_commit(commit_id.unwrap()).unwrap();
Expand Down Expand Up @@ -194,6 +216,15 @@ fn select_diffs(
attributes.insert(key, commit.author().email().unwrap_or("").to_string());
}

if is_limit_fields_empty || fields.contains(&String::from("repo")) {
let key = alias_table
.get("repo")
.unwrap_or(&"repo".to_string())
.to_string();

attributes.insert(key, repo_path.to_string());
}

if is_limit_fields_empty || select_insertions || select_deletions || select_file_changed {
let diff = if commit.parents().len() > 0 {
repo.diff_tree_to_tree(
Expand Down Expand Up @@ -247,6 +278,7 @@ fn select_branches(
let mut branches: Vec<GQLObject> = Vec::new();
let local_branches = repo.branches(None).unwrap();
let is_limit_fields_empty = fields.is_empty();
let repo_path = repo.path().to_str().unwrap().to_string();

for branch in local_branches {
let (branch, _) = branch.unwrap();
Expand Down Expand Up @@ -288,6 +320,15 @@ fn select_branches(
attributes.insert(key, branch.get().is_remote().to_string());
}

if is_limit_fields_empty || fields.contains(&String::from("repo")) {
let key = alias_table
.get("repo")
.unwrap_or(&"repo".to_string())
.to_string();

attributes.insert(key, repo_path.to_string());
}

let gql_branch = GQLObject { attributes };
branches.push(gql_branch);
}
Expand All @@ -303,20 +344,32 @@ fn select_tags(
let mut tags: Vec<GQLObject> = Vec::new();
let tag_names = repo.tag_names(None).unwrap();
let is_limit_fields_empty = fields.is_empty();
let repo_path = repo.path().to_str().unwrap().to_string();

for tag_name in tag_names.iter() {
match tag_name {
Some(name) => {
let mut attributes: HashMap<String, String> = HashMap::new();

if is_limit_fields_empty || fields.contains(&String::from("name")) {
let key = alias_table
.get("name")
.unwrap_or(&"name".to_string())
.to_string();
attributes.insert(key, name.to_string());
let gql_tag = GQLObject { attributes };
tags.push(gql_tag);
}

if is_limit_fields_empty || fields.contains(&String::from("repo")) {
let key = alias_table
.get("repo")
.unwrap_or(&"repo".to_string())
.to_string();

attributes.insert(key, repo_path.to_string());
}

let gql_tag = GQLObject { attributes };
tags.push(gql_tag);
}
None => {}
}
Expand Down
17 changes: 13 additions & 4 deletions crates/gitql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@ use gitql_ast::types::TABLES_FIELDS_TYPES;
lazy_static! {
static ref TABLES_FIELDS_NAMES: HashMap<&'static str, Vec<&'static str>> = {
let mut map = HashMap::new();
map.insert("refs", vec!["name", "full_name", "type"]);
map.insert("refs", vec!["name", "full_name", "type", "repo"]);
map.insert(
"commits",
vec!["commit_id", "title", "message", "name", "email", "time"],
vec![
"commit_id",
"title",
"message",
"name",
"email",
"time",
"repo",
],
);
map.insert(
"branches",
vec!["name", "commit_count", "is_head", "is_remote"],
vec!["name", "commit_count", "is_head", "is_remote", "repo"],
);
map.insert(
"diffs",
Expand All @@ -36,9 +44,10 @@ lazy_static! {
"insertions",
"deletions",
"files_changed",
"repo",
],
);
map.insert("tags", vec!["name"]);
map.insert("tags", vec!["name", "repo"]);
map
};
}
Expand Down
37 changes: 21 additions & 16 deletions docs/structure/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@

### References table

| Name | Type | Description |
| --------- | ---- | ------------------- |
| name | Text | Reference name |
| full_name | Text | Reference full name |
| type | Text | Reference type |
| Name | Type | Description |
| --------- | ---- | -------------------- |
| name | Text | Reference name |
| full_name | Text | Reference full name |
| type | Text | Reference type |
| repo | Text | Repository full path |

### Commits table

---

| Name | Type | Description |
| --------- | ---- | ------------------- |
| commit_id | Text | Commit id |
| title | Text | Commit title |
| message | Text | Commit full message |
| name | Text | Author name |
| email | Text | Author email |
| time | Date | Commit date |
| Name | Type | Description |
| --------- | ---- | -------------------- |
| commit_id | Text | Commit id |
| title | Text | Commit title |
| message | Text | Commit full message |
| name | Text | Author name |
| email | Text | Author email |
| time | Date | Commit date |
| repo | Text | Repository full path |

---

Expand All @@ -34,6 +36,7 @@
| insertions | Number | Number of inserted lines |
| deletions | Number | Number of deleted lines |
| files_changed | Number | Number of file changed |
| repo | Text | Repository full path |

---

Expand All @@ -45,11 +48,13 @@
| commit_count | Number | Number of commits in this branch |
| is_head | Bool | Is the head branch |
| is_remote | Bool | Is a remote branch |
| repo | Text | Repository full path |

---

### Tags table

| Name | Type | Description |
| ---- | ---- | ----------- |
| name | Text | Tag name |
| Name | Type | Description |
| ---- | ---- | -------------------- |
| name | Text | Tag name |
| repo | Text | Repository full path |

0 comments on commit 9f7541b

Please sign in to comment.