Skip to content

Commit d3058d0

Browse files
Copilotoleander
andauthored
[Refactor] Remove dead code markers and fix all clippy warnings (#64)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: oleander <220827+oleander@users.noreply.github.com>
1 parent 607f3ab commit d3058d0

File tree

2 files changed

+1
-134
lines changed

2 files changed

+1
-134
lines changed

src/bin/hook.rs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(dead_code)]
2-
31
// .git/hooks/prepare-commit-msg
42
//
53
// git commit --amend --no-edit
@@ -54,7 +52,6 @@ use anyhow::{bail, Context, Result};
5452
use git2::{Oid, Repository};
5553
use ai::{commit, config, debug_output};
5654
use ai::hook::*;
57-
use ai::model::Model;
5855

5956
#[derive(Debug, PartialEq)]
6057
enum Source {
@@ -90,44 +87,6 @@ struct Args {
9087
}
9188

9289
impl Args {
93-
async fn handle_commit(&self, repo: &Repository, pb: &ProgressBar, model: Model, remaining_tokens: usize) -> Result<()> {
94-
let tree = match self.sha1.as_deref() {
95-
Some("HEAD") | None => repo.head().ok().and_then(|head| head.peel_to_tree().ok()),
96-
Some(sha1) => {
97-
// Try to resolve the reference first
98-
if let Ok(obj) = repo.revparse_single(sha1) {
99-
obj.peel_to_tree().ok()
100-
} else {
101-
// If not a reference, try as direct OID
102-
repo
103-
.find_object(Oid::from_str(sha1)?, None)
104-
.ok()
105-
.and_then(|obj| obj.peel_to_tree().ok())
106-
}
107-
}
108-
};
109-
110-
let diff = repo.to_diff(tree.clone())?;
111-
if diff.is_empty()? {
112-
if self.sha1.as_deref() == Some("HEAD") {
113-
// For amend operations, we want to keep the existing message
114-
return Ok(());
115-
}
116-
bail!("No changes to commit");
117-
}
118-
119-
let patch = repo
120-
.to_patch(tree, remaining_tokens, model)
121-
.context("Failed to get patch")?;
122-
123-
let response = commit::generate(patch.to_string(), remaining_tokens, model, None).await?;
124-
std::fs::write(&self.commit_msg_file, response.response.trim())?;
125-
126-
pb.finish_and_clear();
127-
128-
Ok(())
129-
}
130-
13190
async fn execute(&self) -> Result<()> {
13291
use Source::*;
13392

src/hook.rs

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(dead_code)]
21
use std::collections::HashMap;
32
use std::io::{Read, Write};
43
use std::path::PathBuf;
@@ -7,7 +6,6 @@ use std::sync::Arc;
76
use std::sync::atomic::{AtomicUsize, Ordering};
87

98
use rayon::prelude::*;
10-
use structopt::StructOpt;
119
use git2::{Diff, DiffFormat, DiffOptions, Repository, Tree};
1210
use anyhow::{Context, Result};
1311
use thiserror::Error;
@@ -17,7 +15,7 @@ use crate::model::Model;
1715
use crate::profile;
1816

1917
// Constants
20-
const MAX_POOL_SIZE: usize = 1000;
18+
2119
const DEFAULT_STRING_CAPACITY: usize = 8192;
2220
const PARALLEL_CHUNK_SIZE: usize = 25;
2321
const ESTIMATED_FILES_COUNT: usize = 100;
@@ -44,46 +42,6 @@ pub enum HookError {
4442
Anyhow(#[from] anyhow::Error)
4543
}
4644

47-
// CLI Arguments
48-
#[derive(StructOpt, Debug)]
49-
#[structopt(name = "commit-msg-hook", about = "A tool for generating commit messages.")]
50-
pub struct Args {
51-
pub commit_msg_file: PathBuf,
52-
53-
#[structopt(short = "t", long = "type")]
54-
pub commit_type: Option<String>,
55-
56-
#[structopt(short = "s", long = "sha1")]
57-
pub sha1: Option<String>
58-
}
59-
60-
// Memory management
61-
#[derive(Debug)]
62-
struct StringPool {
63-
strings: Vec<String>,
64-
capacity: usize
65-
}
66-
67-
impl StringPool {
68-
fn new(capacity: usize) -> Self {
69-
Self { strings: Vec::with_capacity(capacity), capacity }
70-
}
71-
72-
fn get(&mut self) -> String {
73-
self
74-
.strings
75-
.pop()
76-
.unwrap_or_else(|| String::with_capacity(self.capacity))
77-
}
78-
79-
fn put(&mut self, mut string: String) {
80-
string.clear();
81-
if self.strings.len() < MAX_POOL_SIZE {
82-
self.strings.push(string);
83-
}
84-
}
85-
}
86-
8745
// File operations traits
8846
pub trait FilePath {
8947
fn is_empty(&self) -> Result<bool> {
@@ -364,10 +322,6 @@ impl PatchDiff for Diff<'_> {
364322
// Pre-allocate HashMap with estimated capacity
365323
let mut files = HashMap::with_capacity(ESTIMATED_FILES_COUNT);
366324

367-
// Use pre-sized buffers to avoid reallocations
368-
const BUFFER_SIZE: usize = 64; // Hold context prefix strings
369-
static CONTEXT_PREFIX: &str = "context: ";
370-
371325
// Create thread-local cache for paths to avoid allocations
372326
thread_local! {
373327
static PATH_CACHE: std::cell::RefCell<HashMap<PathBuf, ()>> =
@@ -676,49 +630,3 @@ impl PatchRepository for Repository {
676630
.minimal(true);
677631
}
678632
}
679-
680-
#[cfg(test)]
681-
mod tests {
682-
use super::*;
683-
684-
#[test]
685-
fn test_string_pool_new() {
686-
let pool = StringPool::new(100);
687-
assert_eq!(pool.strings.len(), 0);
688-
assert_eq!(pool.capacity, 100);
689-
}
690-
691-
#[test]
692-
fn test_string_pool_put_and_get() {
693-
let mut pool = StringPool::new(10);
694-
let mut s1 = String::with_capacity(10);
695-
s1.push_str("test");
696-
pool.put(s1);
697-
698-
assert_eq!(pool.strings.len(), 1);
699-
700-
let s2 = pool.get();
701-
assert_eq!(s2.capacity(), 10);
702-
assert_eq!(s2.len(), 0);
703-
assert_eq!(pool.strings.len(), 0);
704-
}
705-
706-
#[test]
707-
fn test_string_pool_limit() {
708-
let mut pool = StringPool::new(10);
709-
710-
for _ in 0..150 {
711-
pool.put(String::with_capacity(10));
712-
}
713-
714-
assert_eq!(pool.strings.len(), 150);
715-
}
716-
}
717-
718-
#[test]
719-
fn test_string_pool_get() {
720-
let mut pool = StringPool::new(10);
721-
let s1 = pool.get();
722-
assert_eq!(s1.capacity(), 10);
723-
assert_eq!(s1.len(), 0);
724-
}

0 commit comments

Comments
 (0)