Skip to content

Commit

Permalink
git update
Browse files Browse the repository at this point in the history
  • Loading branch information
ODAncona committed Feb 24, 2025
1 parent ba2af2d commit e89af5c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 29 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/default_template_md.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ Source Tree:
{{code}}

{{/if}}
{{/each}}
{{/each}}

{{#if git_diff}}
Git Diff:
{{ git_diff }}
{{/if}}
8 changes: 7 additions & 1 deletion src/default_template_xml.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
</file>
{{/if}}
{{/each}}
</files>
</files>

{{#if git_diff}}
<git-diff>
{{ git_diff }}
</git-diff>
{{/if}}
63 changes: 51 additions & 12 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,77 @@ use git2::{DiffOptions, Repository};
use log::info;
use std::path::Path;

/// Generates a git diff for the repository at the provided path
/// Generates a git diff for the repository at the provided path.
///
/// This function compares the repository's HEAD tree with the index to produce a diff of staged changes.
/// It also checks for unstaged changes (differences between the index and the working directory) and,
/// if found, appends a notification to the output.
///
/// If there are no staged changes, the function returns a message in the format:
/// `"no diff between HEAD and index"`.
///
/// # Arguments
///
/// * `repo_path` - A reference to the path of the git repository
/// * `repo_path` - A reference to the path of the git repository.
///
/// # Returns
///
/// * `Result<String, git2::Error>` - The generated git diff as a string or an error
/// * `Result<String>` - On success, returns either the diff (with an appended note if unstaged changes exist)
/// or a message indicating that there is no diff between the compared git objects.
/// In case of error, returns an appropriate error.
pub fn get_git_diff(repo_path: &Path) -> Result<String> {
info!("Opening repository at path: {:?}", repo_path);
let repo = Repository::open(repo_path).context("Failed to open repository")?;

let head = repo.head().context("Failed to get repository head")?;
let head_tree = head.peel_to_tree().context("Failed to peel to tree")?;

let diff = repo
// Generate diff for staged changes (HEAD vs. index)
let staged_diff = repo
.diff_tree_to_index(
Some(&head_tree),
None,
Some(DiffOptions::new().ignore_whitespace(true)),
)
.context("Failed to generate diff")?;
.context("Failed to generate diff for staged changes")?;

let mut diff_text = Vec::new();
diff.print(git2::DiffFormat::Patch, |_delta, _hunk, line| {
diff_text.extend_from_slice(line.content());
true
})
.context("Failed to print diff")?;
let mut staged_diff_text = Vec::new();
staged_diff
.print(git2::DiffFormat::Patch, |_delta, _hunk, line| {
staged_diff_text.extend_from_slice(line.content());
true
})
.context("Failed to print staged diff")?;

let staged_diff_output = String::from_utf8_lossy(&staged_diff_text).into_owned();

// If there is no staged diff, return a message indicating so.
if staged_diff_output.trim().is_empty() {
return Ok("no diff between HEAD and index".to_string());
}

// Generate diff for unstaged changes (index vs. working directory)
let unstaged_diff = repo
.diff_index_to_workdir(None, Some(DiffOptions::new().ignore_whitespace(true)))
.context("Failed to generate diff for unstaged changes")?;

let mut unstaged_diff_text = Vec::new();
unstaged_diff
.print(git2::DiffFormat::Patch, |_delta, _hunk, line| {
unstaged_diff_text.extend_from_slice(line.content());
true
})
.context("Failed to print unstaged diff")?;

let unstaged_diff_output = String::from_utf8_lossy(&unstaged_diff_text).into_owned();

let mut output = staged_diff_output;
if !unstaged_diff_output.trim().is_empty() {
output.push_str("\nNote: Some changes are not staged.");
}

info!("Generated git diff successfully");
Ok(String::from_utf8_lossy(&diff_text).into_owned())
Ok(output)
}

/// Generates a git diff between two branches for the repository at the provided path
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ fn main() -> Result<()> {
};

// ~~~ Traverse the directory ~~~

let create_tree = traverse_directory(
&args.path,
&include_patterns,
Expand Down Expand Up @@ -192,10 +193,10 @@ fn main() -> Result<()> {
spinner.set_message("Generating git diff...");
get_git_diff(&args.path).unwrap_or_default()
} else {
String::new()
String::from("no git diff")
};

// git diff between two branches
// Git diff between two branches
let mut git_diff_branch: String = String::new();
if let Some(branches) = &args.git_diff_branch {
spinner.set_message("Generating git diff between two branches...");
Expand All @@ -208,7 +209,7 @@ fn main() -> Result<()> {
.unwrap_or_default()
}

// git log between two branches
// Git log between two branches
let mut git_log_branch: String = String::new();
if let Some(branches) = &args.git_log_branch {
spinner.set_message("Generating git log between two branches...");
Expand Down

0 comments on commit e89af5c

Please sign in to comment.