Skip to content
This repository was archived by the owner on Mar 27, 2026. It is now read-only.

Commit 8135636

Browse files
committed
update instructions
1 parent 1ba1d8a commit 8135636

5 files changed

Lines changed: 114 additions & 99 deletions

File tree

instructions.txt

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/filters/filter_manager.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use fnmatch_regex::glob_to_regex;
33
use crate::utils::config_manager::FilterRule;
44
use crate::utils::diff_parser::Hunk;
55
use crate::filters::csharp_parser::{CSharpParser, CSharpMethod};
6+
use serde_json;
67

78
/// Manages file pattern filters for controlling context lines in git diffs
89
pub struct FilterManager {
@@ -295,4 +296,26 @@ impl FilterManager {
295296
}
296297
content
297298
}
299+
300+
/// Get the include_method_body value from the first filter rule
301+
///
302+
/// Returns None if there are no filter rules
303+
pub fn get_include_method_body(&self) -> Option<bool> {
304+
if self.filters.is_empty() {
305+
return None;
306+
}
307+
// Return true if any filter has include_method_body set to true
308+
let result = self.filters.iter().any(|rule| rule.include_method_body);
309+
Some(result)
310+
}
311+
312+
/// Get the filters as a JSON string
313+
///
314+
/// Returns None if there are no filter rules
315+
pub fn get_filters_json(&self) -> Option<String> {
316+
if self.filters.is_empty() {
317+
return None;
318+
}
319+
serde_json::to_string_pretty(&self.filters).ok()
320+
}
298321
}

src/repodiff.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ impl RepoDiff {
5555
// Parse and process the diff
5656
let patch_dict = DiffParser::parse_unified_diff(&raw_diff)?;
5757
let processed_dict = self.filter_manager.post_process_files(&patch_dict);
58-
let final_output = DiffParser::reconstruct_patch(&processed_dict);
58+
59+
// Get filters as JSON if available
60+
let filters_json = self.filter_manager.get_filters_json();
61+
62+
let final_output = DiffParser::reconstruct_patch(
63+
&processed_dict,
64+
filters_json.as_deref()
65+
);
5966

6067
// Create output directory if it doesn't exist
6168
if let Some(parent) = Path::new(output_file).parent() {

src/utils/diff_parser.rs

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,19 @@ impl DiffParser {
129129
}
130130

131131
/// Get the instructions for interpreting git diff output
132-
fn get_diff_instructions() -> Vec<String> {
133-
let instructions = r#"**Instructions for Interpreting Git Diff Output**
134-
135-
This document provides a guide to understanding the diff output generated by RepoDiff.
136-
137-
**Important Note:** The diff output in `repodiff_output.txt` has been sanitized to focus what's relevant for understanding the diffs.
138-
Real-world Git diff output may contain more details.
132+
///
133+
/// # Arguments
134+
///
135+
/// * `filters_json` - JSON string containing the file filters configuration
136+
pub fn get_diff_instructions(filters_json: Option<&str>) -> Vec<String> {
137+
let mut instructions = String::from("This file provides a guide to understanding the diff output generated by RepoDiff, a simplified and context-aware unified diff designed for code reviews.
138+
RepoDiff processes a single `git diff` output and applies user-defined rules to tailor the content, with special handling for C# files.
139139
140-
**1. Basic Structure:**
140+
# 1. Basic Structure:
141141
142142
A Git diff file describes the *differences* between two versions of a file. It's structured into *hunks*, which represent contiguous regions of change.
143143
144-
* `diff --git a/<path> b/<path>`: Indicates the file being compared. `a/` refers to the "old" version, and `b/` refers to the "new" version. (Note that paths always use forward slashes in Git diff output, even on Windows systems.)
144+
* `diff --git a/<path> b/<path>`: Indicates the file being compared. `a/` refers to the \"old\" version, and `b/` refers to the \"new\" version. (Note that paths always use forward slashes in Git diff output, even on Windows systems.)
145145
* `--- a/<path>`: Marks the beginning of the original file content.
146146
* `+++ b/<path>`: Marks the beginning of the modified file content.
147147
* `@@ -<start_line_old>,<num_lines_old> +<start_line_new>,<num_lines_new> @@ <section_header>`: This is the *hunk header*. (Optional in simplified output, but common in real diffs).
@@ -153,49 +153,64 @@ A Git diff file describes the *differences* between two versions of a file. It's
153153
* `-`: Line removed from the old version.
154154
* `+`: Line added to the new version.
155155
156-
**2. Simplified Example:**
156+
## Simplified Example:
157157
158-
```
158+
```diff
159159
diff --git a/MyFile.cs b/MyFile.cs
160160
--- a/MyFile.cs
161161
+++ b/MyFile.cs
162162
// Some code
163-
string oldValue = "old";
163+
string oldValue = \"old\";
164164
-// Removed line
165-
+string newValue = "new";
165+
+string newValue = \"new\";
166166
// More code
167167
```
168168
169169
**Explanation of the Example:**
170-
171170
* The file being changed is `MyFile.cs`.
172-
* `" string oldValue = "old";"`: This line is present in both versions.
171+
* `\" string oldValue = \"old\";\"`: This line is present in both versions.
173172
* `-// Removed line`: This line was removed from the old version.
174-
* `+string newValue = "new";`: This line was added to the new version.
175-
* `" // More code"`: This line is present in both versions.
173+
* `+string newValue = \"new\";`: This line was added to the new version.
174+
* `\" // More code\"`: This line is present in both versions.
176175
177-
**3. Key LLM Considerations:**
176+
# 2. Special Handling in RepoDiff
178177
179-
* **Focus on Content Lines:** The most important part for understanding changes is the content prefixed with ` `, `-`, or `+`.
180-
* **Context is Crucial:** Use the surrounding unchanged lines to understand the *purpose* of the change.
181-
* **File Paths:** Pay attention to the file paths (`a/<path>`, `b/<path>`) to understand which files are being modified.
178+
RepoDiff customizes the diff output using user-defined filters, with enhanced control for C# files (*.cs).
182179
183-
**4. Application to your File:**
180+
The following JSON filters are applied to the diff output:
184181
185-
* **".cs" Files:** Changes to C# source code. Focus on the addition (`+`) and removal (`-`) of code lines to understand logic changes.
186-
* **"Test*.cs" Files:** Changes to unit test files. These are often important for understanding how the functionality is being tested and whether the changes are robust.
187-
* **".xml" Files:** Changes to configuration or data files. Look for added, removed, or modified XML elements and attributes. Focus is usually on changes to properties.
182+
");
183+
184+
if let Some(filters) = filters_json {
185+
instructions.push_str(filters);
186+
}
187+
188+
instructions.push_str("
189+
190+
Each filter defines:
188191
189-
**5. Special Instructions for File Types based on the given filters:**
192+
* *`file_pattern`*: A glob pattern matching file names (e.g., \"*.cs\" for C# files).
193+
* *`context_lines`*: Number of unchanged lines shown before and after each change or hunk.
194+
* **For C# files only:**
195+
* *`include_method_body`*: If true, includes the entire body of methods with changes.
196+
* *`include_signatures`*: If true, includes signatures of methods within the context range of changes, with partial or full bodies based on size. It will always include namespace/class declarations enclosing changed methods. The placeholder `⋮----`* is used to omit code inside the hunk that is outside of the context range
190197
191-
* `.cs` code is assumed to not contain test code
192-
* `*Test*.cs` contain test code, which should be helpful for understanding functionality.
193-
* `*.xml` contains configuration.
198+
# 4. Usage Guidelines
199+
200+
* Focus on Content: Lines with ` `, `-`, or `+` show the actual changes.
201+
* Use Context: Unchanged lines provide purpose and structure.
202+
* Interpret Placeholders: `⋮----` signals omitted code; infer its presence simplifies analysis. Consider the context around the placeholder to understand what might have been omitted (e.g., method body, part of a method, etc.).
203+
* File Paths: Track `a/<path>` and `b/<path>` to identify modified files.
204+
* C# Specifics: Note method bodies and signatures in *.cs files are tailored by filters.
194205
195206
By focusing on these key elements, you can effectively extract meaningful information from Git diff output and summarize the changes made in a software project.
196207
197-
---
198-
"#;
208+
================================================================
209+
Diff Output
210+
================================================================
211+
212+
");
213+
199214
instructions.lines().map(|s| s.to_string()).collect()
200215
}
201216

@@ -204,12 +219,13 @@ By focusing on these key elements, you can effectively extract meaningful inform
204219
/// # Arguments
205220
///
206221
/// * `patch_dict` - Dictionary mapping filenames to lists of hunks
207-
pub fn reconstruct_patch(patch_dict: &HashMap<String, Vec<Hunk>>) -> String {
222+
/// * `filters_json` - JSON string containing the file filters configuration
223+
pub fn reconstruct_patch(patch_dict: &HashMap<String, Vec<Hunk>>, filters_json: Option<&str>) -> String {
208224
let mut output = Vec::new();
209225

210226
// Only add instructions if the patch dictionary is not empty
211227
if !patch_dict.is_empty() {
212-
output.extend(Self::get_diff_instructions());
228+
output.extend(Self::get_diff_instructions(filters_json));
213229
}
214230

215231
for (filename, hunks) in patch_dict {
@@ -250,4 +266,4 @@ By focusing on these key elements, you can effectively extract meaningful inform
250266

251267
output.join("\n")
252268
}
253-
}
269+
}

tests/diff_parser_test.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ rename to new_file.txt
133133
fn test_reconstruct_patch_empty() {
134134
// Test reconstructing an empty patch
135135
let patch_dict = std::collections::HashMap::new();
136-
let result = DiffParser::reconstruct_patch(&patch_dict);
136+
let result = DiffParser::reconstruct_patch(&patch_dict, None);
137137
assert_eq!(result, "");
138138
}
139139

@@ -195,4 +195,36 @@ fn test_filter_hunk_context_lines() {
195195
" line5".to_string(),
196196
" line6".to_string(),
197197
]);
198+
}
199+
200+
#[test]
201+
fn test_get_diff_instructions() {
202+
// Test case 1: No filters
203+
let result = DiffParser::get_diff_instructions(None);
204+
let result_str = result.join("\n");
205+
assert!(result_str.contains("This file provides a guide to understanding the diff output generated by RepoDiff"));
206+
207+
// Test case 2: With filters
208+
let filters_json = r#"[
209+
{
210+
"file_pattern": "*.cs",
211+
"context_lines": 3,
212+
"include_method_body": true,
213+
"include_signatures": false
214+
},
215+
{
216+
"file_pattern": "*.xml",
217+
"context_lines": 2,
218+
"include_method_body": false,
219+
"include_signatures": false
220+
}
221+
]"#;
222+
223+
let result = DiffParser::get_diff_instructions(Some(filters_json));
224+
let result_str = result.join("\n");
225+
assert!(result_str.contains("The following JSON filters are applied to the diff output:"));
226+
assert!(result_str.contains("*.cs"));
227+
assert!(result_str.contains("*.xml"));
228+
assert!(result_str.contains("include_method_body"));
229+
assert!(result_str.contains("include_signatures"));
198230
}

0 commit comments

Comments
 (0)