|
2 | 2 |
|
3 | 3 | use anyhow::{Context, Result};
|
4 | 4 | use git2::{DiffOptions, Repository};
|
5 |
| -use log::info; |
| 5 | +use log::{info, error}; |
6 | 6 | use std::path::Path;
|
7 | 7 |
|
8 | 8 | /// Generates a git diff for the repository at the provided path
|
@@ -38,3 +38,112 @@ pub fn get_git_diff(repo_path: &Path) -> Result<String> {
|
38 | 38 | info!("Generated git diff successfully");
|
39 | 39 | Ok(String::from_utf8_lossy(&diff_text).into_owned())
|
40 | 40 | }
|
| 41 | + |
| 42 | +/// Generates a git diff between two branches for the repository at the provided path |
| 43 | +/// |
| 44 | +/// # Arguments |
| 45 | +/// |
| 46 | +/// * `repo_path` - A reference to the path of the git repository |
| 47 | +/// * `branch1` - The name of the first branch |
| 48 | +/// * `branch2` - The name of the second branch |
| 49 | +/// |
| 50 | +/// # Returns |
| 51 | +/// |
| 52 | +/// * `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> { |
| 54 | + info!("Opening repository at path: {:?}", repo_path); |
| 55 | + let repo = Repository::open(repo_path).context("Failed to open repository")?; |
| 56 | + |
| 57 | + for branch in [branch1, branch2].iter() { |
| 58 | + if !branch_exists(&repo, branch) { |
| 59 | + error!("{}",format!("Branch {} doesn't exist!", branch)); |
| 60 | + std::process::exit(1); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + let branch1_commit = repo.revparse_single(branch1)?.peel_to_commit()?; |
| 65 | + let branch2_commit = repo.revparse_single(branch2)?.peel_to_commit()?; |
| 66 | + |
| 67 | + let branch1_tree = branch1_commit.tree()?; |
| 68 | + let branch2_tree = branch2_commit.tree()?; |
| 69 | + |
| 70 | + let diff = repo |
| 71 | + .diff_tree_to_tree( |
| 72 | + Some(&branch1_tree), |
| 73 | + Some(&branch2_tree), |
| 74 | + Some(DiffOptions::new().ignore_whitespace(true)), |
| 75 | + ) |
| 76 | + .context("Failed to generate diff between branches")?; |
| 77 | + |
| 78 | + let mut diff_text = Vec::new(); |
| 79 | + diff.print(git2::DiffFormat::Patch, |_delta, _hunk, line| { |
| 80 | + diff_text.extend_from_slice(line.content()); |
| 81 | + true |
| 82 | + }) |
| 83 | + .context("Failed to print diff")?; |
| 84 | + |
| 85 | + info!("Generated git diff between branches successfully"); |
| 86 | + Ok(String::from_utf8_lossy(&diff_text).into_owned()) |
| 87 | +} |
| 88 | + |
| 89 | +/// Retrieves the git log between two branches for the repository at the provided path |
| 90 | +/// |
| 91 | +/// # Arguments |
| 92 | +/// |
| 93 | +/// * `repo_path` - A reference to the path of the git repository |
| 94 | +/// * `branch1` - The name of the first branch (e.g., "master") |
| 95 | +/// * `branch2` - The name of the second branch (e.g., "migrate-manifest-v3") |
| 96 | +/// |
| 97 | +/// # Returns |
| 98 | +/// |
| 99 | +/// * `Result<String, git2::Error>` - The git log as a string or an error |
| 100 | +pub fn get_git_log(repo_path: &Path, branch1: &str, branch2: &str) -> Result<String> { |
| 101 | + info!("Opening repository at path: {:?}", repo_path); |
| 102 | + let repo = Repository::open(repo_path).context("Failed to open repository")?; |
| 103 | + |
| 104 | + for branch in [branch1, branch2].iter() { |
| 105 | + if !branch_exists(&repo, branch) { |
| 106 | + error!("{}",format!("Branch {} doesn't exist!", branch)); |
| 107 | + std::process::exit(1); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + let branch1_commit = repo.revparse_single(branch1)?.peel_to_commit()?; |
| 112 | + let branch2_commit = repo.revparse_single(branch2)?.peel_to_commit()?; |
| 113 | + |
| 114 | + 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.set_sorting(git2::Sort::REVERSE)?; |
| 118 | + |
| 119 | + let mut log_text = String::new(); |
| 120 | + for oid in revwalk { |
| 121 | + let oid = oid.context("Failed to get OID from revwalk")?; |
| 122 | + let commit = repo.find_commit(oid).context("Failed to find commit")?; |
| 123 | + log_text.push_str(&format!( |
| 124 | + "{} - {}\n", |
| 125 | + &commit.id().to_string()[..7], |
| 126 | + commit.summary().unwrap_or("No commit message") |
| 127 | + )); |
| 128 | + } |
| 129 | + |
| 130 | + info!("Retrieved git log successfully"); |
| 131 | + Ok(log_text) |
| 132 | +} |
| 133 | + |
| 134 | +/// Checks if a local branch exists in the given repository |
| 135 | +/// |
| 136 | +/// # Arguments |
| 137 | +/// |
| 138 | +/// * `repo` - A reference to the `Repository` where the branch should be checked |
| 139 | +/// * `branch_name` - A string slice that holds the name of the branch to check |
| 140 | +/// |
| 141 | +/// # Returns |
| 142 | +/// |
| 143 | +/// * `bool` - `true` if the branch exists, `false` otherwise |
| 144 | +fn branch_exists(repo: &Repository, branch_name: &str) -> bool { |
| 145 | + match repo.find_branch(branch_name, git2::BranchType::Local) { |
| 146 | + Ok(_) => true, |
| 147 | + Err(_) => false, |
| 148 | + } |
| 149 | +} |
0 commit comments