Skip to content

Commit f53b841

Browse files
committed
Add file_ignore setting
1 parent accd4d6 commit f53b841

File tree

6 files changed

+139
-18
lines changed

6 files changed

+139
-18
lines changed

e2e/test_githook_file_ignore.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
DIFF_CONTENT_PATH="$(pwd)/tests/data/example_2.diff"
5+
6+
export TEMPDIR=$(mktemp -d)
7+
(
8+
cd "${TEMPDIR}"
9+
git init
10+
11+
export TEMPFILE=$(mktemp)
12+
echo "foo" > $TEMPFILE
13+
14+
GPTCOMMIT__MODEL_PROVIDER="tester-foobar" \
15+
gptcommit prepare-commit-msg \
16+
--git-diff-content "${DIFF_CONTENT_PATH}" \
17+
--commit-msg-file "${TEMPFILE}" \
18+
--commit-source ""
19+
20+
cat $TEMPFILE
21+
)
22+
rm -rf "${TEMPDIR}"

src/actions/prepare_commit_msg.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ pub(crate) async fn main(settings: Settings, args: PrepareCommitMsgArgs) -> Resu
9292
};
9393

9494
let client = get_llm_client(&settings);
95-
let summarization_client =
96-
SummarizationClient::new(settings.prompt.unwrap(), settings.output.unwrap(), client)?;
95+
let summarization_client = SummarizationClient::new(settings.to_owned(), client)?;
9796

9897
println!("{}", "🤖 Asking GPT-3 to summarize diffs...".green().bold());
9998

src/settings.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ use crate::{
1919
},
2020
};
2121

22+
static DEFAULT_FILES_TO_IGNORE: &[&str; 4] = &[
23+
"package-lock.json",
24+
"yarn.lock",
25+
"pnpm-lock.yaml",
26+
"Cargo.lock",
27+
];
28+
2229
#[derive(Debug, Clone, Display, Serialize, Default, EnumString)]
2330
pub(crate) enum ModelProvider {
2431
#[default]
@@ -143,7 +150,10 @@ pub(crate) struct Settings {
143150
pub openai: Option<OpenAISettings>,
144151
pub prompt: Option<PromptSettings>,
145152
pub output: Option<OutputSettings>,
153+
/// Whether to run githook when amending the commit
146154
pub allow_amend: Option<bool>,
155+
/// Files to ignore, format similar to gitignore
156+
pub file_ignore: Option<Vec<String>>,
147157
}
148158

149159
impl Settings {
@@ -170,6 +180,14 @@ impl Settings {
170180
fn get_config_builder() -> Result<ConfigBuilder<DefaultState>, ConfigError> {
171181
let mut settings = Config::builder()
172182
.set_default("allow_amend", false)?
183+
.set_default(
184+
"file_ignore",
185+
DEFAULT_FILES_TO_IGNORE
186+
.to_vec()
187+
.iter()
188+
.map(|s| s.to_string())
189+
.collect::<Vec<_>>(),
190+
)?
173191
.set_default("model_provider", ModelProvider::OpenAI)?
174192
.set_default(
175193
"openai",

src/summarize.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ use std::str::FromStr;
33
use std::sync::Arc;
44

55
use crate::llms::llm_client::LlmClient;
6+
use crate::settings::Settings;
67
use crate::util;
7-
use crate::{
8-
prompt::format_prompt,
9-
settings::PromptSettings,
10-
settings::{Language, OutputSettings},
11-
};
8+
use crate::{prompt::format_prompt, settings::Language};
129
use anyhow::Result;
1310
use tokio::task::JoinSet;
1411
use tokio::try_join;
1512
#[derive(Debug, Clone)]
1613
pub(crate) struct SummarizationClient {
1714
client: Arc<dyn LlmClient>,
1815

16+
file_ignore: Vec<String>,
1917
prompt_file_diff: String,
2018
prompt_commit_summary: String,
2119
prompt_commit_title: String,
@@ -24,21 +22,20 @@ pub(crate) struct SummarizationClient {
2422
}
2523

2624
impl SummarizationClient {
27-
pub(crate) fn new(
28-
prompt_settings: PromptSettings,
29-
output_settings: OutputSettings,
30-
client: Box<dyn LlmClient>,
31-
) -> Result<Self> {
32-
let prompt_file_diff = prompt_settings.file_diff.unwrap_or_default();
33-
let prompt_commit_summary = prompt_settings.commit_summary.unwrap_or_default();
34-
let prompt_commit_title = prompt_settings.commit_title.unwrap_or_default();
35-
let prompt_translation = prompt_settings.translation.unwrap_or_default();
36-
let prompt_lang = Language::from_str(&output_settings.lang.unwrap_or_default())
25+
pub(crate) fn new(settings: Settings, client: Box<dyn LlmClient>) -> Result<Self> {
26+
let prompt = settings.prompt.unwrap();
27+
28+
let prompt_file_diff = prompt.file_diff.unwrap_or_default();
29+
let prompt_commit_summary = prompt.commit_summary.unwrap_or_default();
30+
let prompt_commit_title = prompt.commit_title.unwrap_or_default();
31+
let prompt_translation = prompt.translation.unwrap_or_default();
32+
let prompt_lang = Language::from_str(&settings.output.unwrap().lang.unwrap_or_default())
3733
.unwrap()
3834
.to_string();
39-
35+
let file_ignore = settings.file_ignore.unwrap_or_default();
4036
Ok(Self {
4137
client: client.into(),
38+
file_ignore,
4239
prompt_file_diff,
4340
prompt_commit_summary,
4441
prompt_commit_title,
@@ -103,6 +100,15 @@ impl SummarizationClient {
103100
/// https://git-scm.com/docs/git-diff
104101
async fn process_file_diff(&self, file_diff: &str) -> Option<(String, String)> {
105102
if let Some(file_name) = util::get_file_name_from_diff(file_diff) {
103+
if self
104+
.file_ignore
105+
.iter()
106+
.any(|ignore| file_name.contains(ignore))
107+
{
108+
warn!("skipping {file_name} due to file_ignore setting");
109+
110+
return None;
111+
}
106112
let completion = self.diff_summary(file_name, file_diff).await;
107113
Some((
108114
file_name.to_string(),

src/toml.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ impl DeepKeysCollector<'_> {
1212
let mut visitor = DeepKeysCollector::default();
1313
visitor.visit_document(&document);
1414

15+
visitor.keys.dedup();
1516
visitor.keys.sort();
1617
visitor.keys
1718
}
@@ -76,11 +77,13 @@ the-force = { value = "surrounds-you" }
7677
visitor.visit_document(&document);
7778

7879
assert_eq!(visitor.current_path, Vec::<&str>::new());
80+
visitor.keys.dedup();
7981
visitor.keys.sort();
8082
assert_eq!(
8183
visitor.keys,
8284
vec![
8385
"allow_amend",
86+
"file_ignore",
8487
"model_provider",
8588
"openai.api_key",
8689
"openai.model",
@@ -101,6 +104,7 @@ the-force = { value = "surrounds-you" }
101104
DeepKeysCollector::get_keys(input),
102105
vec![
103106
"allow_amend",
107+
"file_ignore",
104108
"model_provider",
105109
"openai.api_key",
106110
"openai.model",

tests/data/example_2.diff

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
diff --git a/.vscode/launch.json b/.vscode/launch.json
2+
new file mode 100644
3+
index 0000000..f26eafc
4+
--- /dev/null
5+
+++ b/.vscode/launch.json
6+
@@ -0,0 +1,10 @@
7+
+{
8+
+ // Use IntelliSense to learn about possible attributes.
9+
+ // Hover to view descriptions of existing attributes.
10+
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
11+
+ "version": "0.2.0",
12+
+ "configurations": [
13+
+ rust
14+
+
15+
+ ]
16+
+}
17+
diff --git a/yarn.lock b/yarn.lock
18+
new file mode 100644
19+
index 0000000..a51b2a6
20+
--- /dev/null
21+
+++ b/yarn.lock
22+
@@ -0,0 +1 @@
23+
+sadasdas
24+
diff --git a/src/main.rs b/src/main.rs
25+
index 46d98b0..b1dd2af 100644
26+
--- a/src/main.rs
27+
+++ b/src/main.rs
28+
@@ -1,10 +1,7 @@
29+
use std::process::Command;
30+
31+
fn run_command(cmd: &str, args: &[&str]) -> Result<String, String> {
32+
- let output = match Command::new(cmd)
33+
- .args(args)
34+
- .output()
35+
- {
36+
+ let output = match Command::new(cmd).args(args).output() {
37+
Ok(o) => o,
38+
Err(e) => return Err(format!("Failed to execute process: {}", e)),
39+
};
40+
@@ -19,9 +16,28 @@ fn run_command(cmd: &str, args: &[&str]) -> Result<String, String> {
41+
}
42+
43+
fn main() {
44+
- let result = run_command("git", &["diff", "--staged"]);
45+
- match result {
46+
- Ok(output) => println!("{}", output),
47+
- Err(error) => eprintln!("{}", error),
48+
+ let output = run_command("git", &["diff", "--staged"]).unwrap();
49+
+ let file_diffs = split_string_with_pattern(&output, "diff --git ");
50+
+ println!("{:?}", file_diffs);
51+
+}
52+
+
53+
+fn split_string_with_pattern<'a>(string: &'a str, pattern: &'a str) -> Vec<&'a str> {
54+
+ let mut start = dbg!(pattern.len());
55+
+ let mut substrings = Vec::new();
56+
+
57+
+ while let Some(i) = string[start..].find(pattern) {
58+
+ substrings.push(&string[start..start + i]);
59+
+ start += i;
60+
}
61+
+ substrings.push(&string[start..]);
62+
+
63+
+ substrings
64+
+}
65+
+
66+
+#[test]
67+
+fn test_split_string_with_pattern() {
68+
+ let string = include_str!("../test_data/diff.txt");
69+
+ let pattern = "diff --git ";
70+
+ println!("testing");
71+
+ assert!(split_string_with_pattern(string, pattern).len() == 2);
72+
}

0 commit comments

Comments
 (0)