Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ai commit summary creation using chat-gpt #1979

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ easy-cast = "0.5"
filetreelist = { path = "./filetreelist", version = "0.5" }
fuzzy-matcher = "0.3"
gh-emoji = { version = "1.0", optional = true }
git2-summarize = { path = "./git2-summarize", version = "0.1" }
indexmap = "1.9"
itertools = "0.12"
log = "0.4"
Expand Down Expand Up @@ -69,7 +70,7 @@ trace-libgit = ["asyncgit/trace-libgit"]
vendor-openssl = ["asyncgit/vendor-openssl"]

[workspace]
members = ["asyncgit", "filetreelist", "git2-hooks", "git2-testing", "scopetime"]
members = ["asyncgit", "filetreelist", "git2-hooks", "git2-summarize", "git2-testing", "scopetime"]

[profile.release]
lto = true
Expand Down
29 changes: 28 additions & 1 deletion asyncgit/src/sync/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
error::Error,
error::Result,
hash,
sync::{get_stashes, repository::repo},
sync::{get_stashes, repository::repo, utils::bytes2string},
};
use easy_cast::Conv;
use git2::{
Expand Down Expand Up @@ -398,6 +398,33 @@ fn raw_diff_to_file_diff(
Ok(res.into_inner())
}

///
pub fn unified_stage_diff(repo_path: &RepoPath) -> Result<String> {
scope_time!("unified_stage_diff");

let repo = repo(repo_path)?;

let diff = get_diff_raw(&repo, "*", true, false, None)?;

let mut output = String::with_capacity(32);

diff.print(DiffFormat::Patch, |_delta, _hunk, line| {
let prefix = match line.origin_value() {
git2::DiffLineType::Addition => "+",
git2::DiffLineType::Deletion => "-",
_ => "",
};

if let Ok(line) = bytes2string(line.content()) {
output.push_str(&format!("{prefix}{line}"));
}

true
})?;

Ok(output)
}

const fn is_newline(c: char) -> bool {
c == '\n' || c == '\r'
}
Expand Down
2 changes: 1 addition & 1 deletion asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub use config::{
get_config_string, untracked_files_config,
ShowUntrackedFilesConfig,
};
pub use diff::get_diff_commit;
pub use diff::{get_diff_commit, unified_stage_diff};
pub use git2::BranchType;
pub use hooks::{
hooks_commit_msg, hooks_post_commit, hooks_pre_commit,
Expand Down
12 changes: 8 additions & 4 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ allow = [
"BSD-3-Clause",
"CC0-1.0",
"ISC",
"MPL-2.0"
"MPL-2.0",
"OpenSSL",
]
copyleft = "warn"
allow-osi-fsf-free = "neither"
Expand All @@ -19,8 +20,11 @@ allow = ["Unicode-DFS-2016"]
name = "unicode-ident"
version = "1.0.3"

[[licenses.clarify]]
name = "ring"
expression = "MIT AND ISC AND OpenSSL"
license-files = [{ path = "LICENSE", hash = 0xbd0eed23 }]

[bans]
multiple-versions = "deny"
skip-tree = [
{ name = "windows-sys" }
]
skip-tree = [{ name = "windows-sys" }]
18 changes: 18 additions & 0 deletions git2-summarize/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "git2-summarize"
version = "0.1.0"
authors = ["extrawurst <[email protected]>"]
edition = "2021"
description = "use openai gpt to summarize git2 diffs"
homepage = "https://github.com/extrawurst/gitui"
repository = "https://github.com/extrawurst/gitui"
documentation = "https://docs.rs/git2-summarize/"
readme = "README.md"
license = "MIT"
categories = ["development-tools"]
keywords = ["git"]

[dependencies]
git2 = ">=0.17"
log = "0.4"
openai-api-rs = "2.1"
1 change: 1 addition & 0 deletions git2-summarize/LICENSE.md
3 changes: 3 additions & 0 deletions git2-summarize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# git2-summarize

this uses open ai chat-gpt to summarize a git diff
49 changes: 49 additions & 0 deletions git2-summarize/examples/simple.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
diff --git a/git2-hooks/src/lib.rs b/git2-hooks/src/lib.rs
index 0cf5ac8..042ac87 100644
--- a/git2-hooks/src/lib.rs
+++ b/git2-hooks/src/lib.rs
@@ -27,6 +27,7 @@ use git2::Repository;
pub const HOOK_POST_COMMIT: &str = "post-commit";
pub const HOOK_PRE_COMMIT: &str = "pre-commit";
pub const HOOK_COMMIT_MSG: &str = "commit-msg";
+pub const HOOK_PRE_PUSH: &str = "pre-push";

const HOOK_COMMIT_MSG_TEMP_FILE: &str = "COMMIT_EDITMSG";

@@ -152,6 +153,36 @@ pub fn hooks_post_commit(
hook.run_hook(&[])
}

+/// see [`hooks_pre_push`]
+pub enum PrePushHookLocalRef<'a> {
+ Delete,
+ Ref {
+ local_ref: &'a str,
+ local_obj_name: &'a str,
+ },
+}
+
+/// see https://git-scm.com/docs/githooks#_pre_push
+///
+/// # Arguments
+///
+/// * `remote_obj_name` - pass `None` if foreign ref not yet exists
+pub fn hooks_pre_push(
+ repo: &Repository,
+ other_paths: Option<&[&str]>,
+ local_ref: PrePushHookLocalRef,
+ remote_ref: &str,
+ remote_obj_name: Option<&str>,
+) -> Result<HookResult> {
+ let hook = HookPaths::new(repo, other_paths, HOOK_PRE_PUSH)?;
+
+ if !hook.found() {
+ return Ok(HookResult::NoHookFound);
+ }
+
+ hook.run_hook(&[])
+}
+
#[cfg(test)]
mod tests {
use super::*;
14 changes: 14 additions & 0 deletions git2-summarize/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::env;

fn main() {
let diff = include_str!("simple.diff");

let summary = git2_summarize::git_diff_summarize(
&env::var("OPENAI_API_KEY").unwrap(),
diff,
50,
)
.unwrap();

println!("{summary}");
}
Loading