Skip to content

Commit 68351c5

Browse files
authored
explicit git init to master, error handling (#32)
1 parent 4e5cdf7 commit 68351c5

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

src/git.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use anyhow::{Context, Result};
44
use git2::{DiffOptions, Repository};
5-
use log::{info, error};
5+
use log::info;
66
use std::path::Path;
77

88
/// Generates a git diff for the repository at the provided path
@@ -50,14 +50,17 @@ pub fn get_git_diff(repo_path: &Path) -> Result<String> {
5050
/// # Returns
5151
///
5252
/// * `Result<String, git2::Error>` - The generated git diff as a string or an error
53-
pub fn get_git_diff_between_branches(repo_path: &Path, branch1: &str, branch2: &str) -> Result<String> {
53+
pub fn get_git_diff_between_branches(
54+
repo_path: &Path,
55+
branch1: &str,
56+
branch2: &str,
57+
) -> Result<String> {
5458
info!("Opening repository at path: {:?}", repo_path);
5559
let repo = Repository::open(repo_path).context("Failed to open repository")?;
5660

5761
for branch in [branch1, branch2].iter() {
5862
if !branch_exists(&repo, branch) {
59-
error!("{}",format!("Branch {} doesn't exist!", branch));
60-
std::process::exit(1);
63+
return Err(anyhow::anyhow!("Branch {} doesn't exist!", branch));
6164
}
6265
}
6366

@@ -103,17 +106,20 @@ pub fn get_git_log(repo_path: &Path, branch1: &str, branch2: &str) -> Result<Str
103106

104107
for branch in [branch1, branch2].iter() {
105108
if !branch_exists(&repo, branch) {
106-
error!("{}",format!("Branch {} doesn't exist!", branch));
107-
std::process::exit(1);
109+
return Err(anyhow::anyhow!("Branch {} doesn't exist!", branch));
108110
}
109111
}
110112

111113
let branch1_commit = repo.revparse_single(branch1)?.peel_to_commit()?;
112114
let branch2_commit = repo.revparse_single(branch2)?.peel_to_commit()?;
113115

114116
let mut revwalk = repo.revwalk().context("Failed to create revwalk")?;
115-
revwalk.push(branch2_commit.id()).context("Failed to push branch2 commit to revwalk")?;
116-
revwalk.hide(branch1_commit.id()).context("Failed to hide branch1 commit from revwalk")?;
117+
revwalk
118+
.push(branch2_commit.id())
119+
.context("Failed to push branch2 commit to revwalk")?;
120+
revwalk
121+
.hide(branch1_commit.id())
122+
.context("Failed to hide branch1 commit from revwalk")?;
117123
revwalk.set_sorting(git2::Sort::REVERSE)?;
118124

119125
let mut log_text = String::new();

tests/git_test.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use code2prompt::git::{get_git_diff, get_git_diff_between_branches, get_git_log}
33
#[cfg(test)]
44
mod tests {
55
use super::*;
6-
use git2::{Repository, Signature};
6+
use git2::{Repository, RepositoryInitOptions, Signature};
77
use std::fs;
88
use tempfile::TempDir;
99

@@ -69,7 +69,10 @@ mod tests {
6969
let repo_path = temp_dir.path();
7070

7171
// Initialize a new Git repository
72-
let repo = Repository::init(repo_path).expect("Failed to initialize repository");
72+
let mut binding = RepositoryInitOptions::new();
73+
let init_options = binding.initial_head("master");
74+
let repo = Repository::init_opts(repo_path, init_options)
75+
.expect("Failed to initialize repository");
7376

7477
// Create a new file in the repository
7578
let file_path = repo_path.join("test_file.txt");
@@ -99,13 +102,21 @@ mod tests {
99102
.expect("Failed to commit");
100103

101104
// Create a new branch and make a commit on the master branch
102-
repo.branch("development", &repo.find_commit(master_commit).expect("Failed to find commit"), false)
103-
.expect("Failed to create new branch");
105+
repo.branch(
106+
"development",
107+
&repo
108+
.find_commit(master_commit)
109+
.expect("Failed to find commit"),
110+
false,
111+
)
112+
.expect("Failed to create new branch");
104113

105114
// Modify the file in the new branch
106-
repo.set_head("refs/heads/development").expect("Failed to set HEAD");
115+
repo.set_head("refs/heads/development")
116+
.expect("Failed to set HEAD");
107117
repo.checkout_head(None).expect("Failed to checkout HEAD");
108-
fs::write(&file_path, "Content in new branch").expect("Failed to modify test file in new branch");
118+
fs::write(&file_path, "Content in new branch")
119+
.expect("Failed to modify test file in new branch");
109120

110121
let mut index = repo.index().expect("Failed to get repository index");
111122
index
@@ -122,7 +133,9 @@ mod tests {
122133
&signature,
123134
"New commit in branch development",
124135
&tree,
125-
&[&repo.find_commit(master_commit).expect("Failed to find commit")],
136+
&[&repo
137+
.find_commit(master_commit)
138+
.expect("Failed to find commit")],
126139
)
127140
.expect("Failed to commit in new branch");
128141

@@ -145,7 +158,10 @@ mod tests {
145158
let repo_path = temp_dir.path();
146159

147160
// Initialize a new Git repository
148-
let repo = Repository::init(repo_path).expect("Failed to initialize repository");
161+
let mut binding = RepositoryInitOptions::new();
162+
let init_options = binding.initial_head("master");
163+
let repo = Repository::init_opts(repo_path, init_options)
164+
.expect("Failed to initialize repository");
149165

150166
// Create a new file in the repository
151167
let file_path = repo_path.join("test_file.txt");
@@ -175,13 +191,21 @@ mod tests {
175191
.expect("Failed to commit");
176192

177193
// Create a new branch and make a commit on the master branch
178-
repo.branch("development", &repo.find_commit(master_commit).expect("Failed to find commit"), false)
179-
.expect("Failed to create new branch");
194+
repo.branch(
195+
"development",
196+
&repo
197+
.find_commit(master_commit)
198+
.expect("Failed to find commit"),
199+
false,
200+
)
201+
.expect("Failed to create new branch");
180202

181203
// Modify the file in the new branch
182-
repo.set_head("refs/heads/development").expect("Failed to set HEAD");
204+
repo.set_head("refs/heads/development")
205+
.expect("Failed to set HEAD");
183206
repo.checkout_head(None).expect("Failed to checkout HEAD");
184-
fs::write(&file_path, "Content in development").expect("Failed to modify test file in new branch");
207+
fs::write(&file_path, "Content in development")
208+
.expect("Failed to modify test file in new branch");
185209

186210
let mut index = repo.index().expect("Failed to get repository index");
187211
index
@@ -198,12 +222,15 @@ mod tests {
198222
&signature,
199223
"First commit in development",
200224
&tree,
201-
&[&repo.find_commit(master_commit).expect("Failed to find commit")],
225+
&[&repo
226+
.find_commit(master_commit)
227+
.expect("Failed to find commit")],
202228
)
203229
.expect("Failed to commit in new branch");
204230

205231
// Make a second commit in the development branch
206-
fs::write(&file_path, "Second content in development").expect("Failed to modify test file in new branch");
232+
fs::write(&file_path, "Second content in development")
233+
.expect("Failed to modify test file in new branch");
207234

208235
let mut index = repo.index().expect("Failed to get repository index");
209236
index
@@ -220,7 +247,9 @@ mod tests {
220247
&signature,
221248
"Second commit in development",
222249
&tree,
223-
&[&repo.find_commit(repo.head().unwrap().target().unwrap()).expect("Failed to find commit")],
250+
&[&repo
251+
.find_commit(repo.head().unwrap().target().unwrap())
252+
.expect("Failed to find commit")],
224253
)
225254
.expect("Failed to commit second change in new branch");
226255

0 commit comments

Comments
 (0)