Skip to content

Commit

Permalink
fix: fix error: 'parent index out of bounds' (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Sep 27, 2020
1 parent 0db6a47 commit d2270fc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 43 deletions.
17 changes: 14 additions & 3 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,30 @@ impl Commit {
date,
}),
Err(err) => {
let message = git2_message.replace("\n", "");
let additional_info = if commit.parent_count() == 0 {
format!(
"{} Init commit or commit with no parent cannot be edited",
"warning:".yellow()
)
} else {
"".to_string()
};

let message = git2_message.trim_end();
let commit_message = if message.len() > 80 {
format!("{}{}", &message[0..80], "...").red()
} else {
git2_message.red()
message.red()
}
.to_string();
let cause = format!("{} {}", "cause:".red(), err);

let cause = format!("{} {}", "cause:".magenta(), err);
let level = "ERROR".red().bold().to_string();
Err(anyhow!(CommitFormat {
level,
shorthand,
commit_message,
additional_info,
cause
}))
}
Expand Down
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub(crate) enum ErrorKind {
#[error("{level} - {commit_message} - ({shorthand})\n\t{cause}\n")]
#[error("{level} - {commit_message} - ({shorthand})\n\t{cause}\n\t{additional_info}")]
CommitFormat {
level: String,
shorthand: String,
commit_message: String,
cause: String,
additional_info: String,
},
#[error("{level}:\n\t{cause}\n")]
Semver { level: String, cause: String },
Expand Down
90 changes: 51 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl CocoGitto {
let editor = std::env::var("EDITOR")?;
let dir = TempDir::new("cocogito")?;

let errored_commits: Vec<Oid> = commits
let mut errored_commits: Vec<Oid> = commits
.iter()
.map(|commit| {
let conv_commit = Commit::from_git_commit(&commit);
Expand All @@ -170,49 +170,61 @@ impl CocoGitto {
.map(|commit| commit.0)
.collect();

let last_errored_commit = errored_commits.last();
println!("{:?}", last_errored_commit);
let commit = self
.repository
.0
.find_commit(last_errored_commit.unwrap().to_owned())?;
let rebase_start = commit.parent_id(0)?;
let commit = self.repository.0.find_annotated_commit(rebase_start)?;
let mut options = RebaseOptions::new();
let mut rebase = self
.repository
.0
.rebase(None, Some(&commit), None, Some(&mut options))?;

while let Some(op) = rebase.next() {
if let Ok(rebase_operation) = op {
let oid = rebase_operation.id();
let original_commit = self.repository.0.find_commit(oid)?;
println!("rebasing {}", oid);
if errored_commits.contains(&oid) {
println!("\tmatch found in errored commits");
let file_path = dir.path().join(&commit.id().to_string());
let mut file = File::create(&file_path)?;
file.write_all(original_commit.message_bytes())?;

Command::new(&editor)
.arg(&file_path)
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;

let new_message = std::fs::read_to_string(&file_path)?;
rebase.commit(None, &original_commit.committer(), Some(&new_message))?;
// Get the last commit oid on the list as a starting point for our rebase
let last_errored_commit = errored_commits.pop();
if let Some(last_errored_commit) = last_errored_commit {
let commit = self
.repository
.0
.find_commit(last_errored_commit.to_owned())?;

let rebase_start = if commit.parent_count() == 0 {
commit.id()
} else {
commit.parent_id(0)?
};

let commit = self.repository.0.find_annotated_commit(rebase_start)?;
let mut options = RebaseOptions::new();
let mut rebase =
self.repository
.0
.rebase(None, Some(&commit), None, Some(&mut options))?;

while let Some(op) = rebase.next() {
if let Ok(rebase_operation) = op {
let oid = rebase_operation.id();
let original_commit = self.repository.0.find_commit(oid)?;
if errored_commits.contains(&oid) {
println!("Found errored commits : {}", &oid.to_string()[0..7]);
let file_path = dir.path().join(&commit.id().to_string());
let mut file = File::create(&file_path)?;
file.write_all(original_commit.message_bytes())?;

Command::new(&editor)
.arg(&file_path)
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?;

let new_message = std::fs::read_to_string(&file_path)?;
rebase.commit(None, &original_commit.committer(), Some(&new_message))?;
println!(
"Changed commit message to : \"{}\"",
&new_message.trim_end()
);
} else {
rebase.commit(None, &original_commit.committer(), None)?;
}
} else {
rebase.commit(None, &original_commit.committer(), None)?;
eprintln!("{:?}", op);
}
} else {
eprintln!("{:?}", op);
}

rebase.finish(None)?;
}

rebase.finish(None)?;
Ok(())
}

Expand Down

0 comments on commit d2270fc

Please sign in to comment.