summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/branch/models.rs2
-rw-r--r--src/commit/models.rs9
-rw-r--r--src/git.rs18
-rw-r--r--src/repository/models.rs28
4 files changed, 37 insertions, 20 deletions
diff --git a/src/branch/models.rs b/src/branch/models.rs
index c8a8d65..3d57537 100644
--- a/src/branch/models.rs
+++ b/src/branch/models.rs
@@ -119,7 +119,7 @@ impl Branch {
let statement = client
.prepare(
- "INSERT INTO repository(id, name, repository_id, head)
+ "INSERT INTO branch(id, name, repository_id, head)
VALUES($1, $2, $3, $4)
RETURNING *",
)
diff --git a/src/commit/models.rs b/src/commit/models.rs
index 2f1536b..b2aa2d2 100644
--- a/src/commit/models.rs
+++ b/src/commit/models.rs
@@ -108,21 +108,22 @@ impl Commit {
None => "NULL".to_string(),
};
raw_query += &format!(
- "('{}', {}, '{}', '{}', '{}', '{}', '{}', '{}', '{}'),",
+ "('{}', {}, E'{}', '{}', '{}', E'{}', '{}', E'{}', '{}'),",
commit.hash,
tree,
- commit.text,
+ commit.text.replace("'", "\\'"),
commit.date,
commit.author_email,
- commit.author_name,
+ commit.author_name.replace("'", "\\'"),
commit.committer_email,
- commit.committer_name,
+ commit.committer_name.replace("'", "\\'"),
commit.repository_url
)[..]
}
// Remove the last `,`
let _ = raw_query.pop();
+ raw_query += " RETURNING *";
// TODO: write query with &commits and parameter. Need to implement
// ToSql trait for `Commit` model
diff --git a/src/git.rs b/src/git.rs
index 76f9a4e..2a8749c 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -103,7 +103,13 @@ fn get_commit(gcommit: &git2::Commit, repo_name: &String) -> Commit {
/// Then, open the repository.
/// Then, get commits
/// Finally, remove the temporary folder
-pub fn repo_commits(repo_name: &String) -> Result<Vec<Commit>, Error> {
+pub fn repo_commits(
+ repo_name: &String,
+ branch: &String,
+) -> Result<Vec<Commit>, Error> {
+ // Remove a possible already cloned repository
+ let _ = remove_dir_all(get_tmp_dir(&repo_name));
+
// Try to clone the repo. If it returns an error, it's useless to go ahead:
// raises an error.
let repo = match clone_repo(&repo_name) {
@@ -113,7 +119,7 @@ pub fn repo_commits(repo_name: &String) -> Result<Vec<Commit>, Error> {
}
};
- if let Err(e) = get_branch(&repo, "main") {
+ if let Err(e) = get_branch(&repo, branch) {
return Err(e);
}
@@ -130,13 +136,5 @@ pub fn repo_commits(repo_name: &String) -> Result<Vec<Commit>, Error> {
commits.push(get_commit(&hash, &repo_name));
}
- if let Err(_) = remove_dir_all(get_tmp_dir(&repo_name)) {
- return Err(git2::Error::new(
- git2::ErrorCode::GenericError,
- git2::ErrorClass::Os,
- "Temporary clone not deleted",
- ));
- }
-
Ok(commits)
}
diff --git a/src/repository/models.rs b/src/repository/models.rs
index de82d45..16cbb98 100644
--- a/src/repository/models.rs
+++ b/src/repository/models.rs
@@ -1,3 +1,4 @@
+use crate::branch::models::{Branch, BranchData};
use crate::commit::models::Commit;
use crate::db::get_client;
use crate::email::models::{Email, EmailData};
@@ -30,6 +31,7 @@ pub struct Repository {
#[derive(Serialize, Deserialize)]
pub struct RepositoryData {
pub url: String,
+ pub branch: String,
}
impl Repository {
@@ -190,9 +192,13 @@ impl Repository {
match repo {
Some(repo) => {
- let commits = match git::repo_commits(&repo_name) {
+ let commits = match git::repo_commits(&repo_name, &data.branch)
+ {
Ok(c) => c,
Err(e) => {
+ // It also need to remove the repository from the db
+ let _ =
+ Repository::delete(pool.clone(), &repo.id).await;
return Err(AppError {
message: Some(
format!(
@@ -203,7 +209,7 @@ impl Repository {
),
cause: Some("Repository clone".to_string()),
error_type: AppErrorType::GitError,
- })
+ });
}
};
@@ -224,9 +230,21 @@ impl Repository {
let commits_result =
Commit::create(pool.clone(), commits).await;
- if let Err(e) = commits_result {
- return Err(e);
- }
+
+ match commits_result {
+ Ok(commits_res_vec) => {
+ let branch_data = BranchData {
+ name: data.branch.clone(),
+ repository_id: repo.id,
+ head: commits_res_vec[0].hash.clone(),
+ };
+ let _ =
+ Branch::create(pool.clone(), &branch_data).await;
+ }
+ Err(e) => {
+ return Err(e);
+ }
+ };
Ok(repo)
}