1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// use std::cell::RefCell;
// pub trait GitDiff {
// fn git_diff(
// repo: &git2::Repository,
// exclude_files: &[String],
// ) -> crate::error::ReviseResult<String> {
// let mut opts = git2::DiffOptions::new();
// // 设置 diff 选项以只包含暂存区的更改
// opts.include_untracked(false)
// .show_untracked_content(false)
// .include_ignored(false);
// // 获取 HEAD 指向的 tree
// let head = repo.head()?.peel_to_tree()?;
// // 获取暂存区的 index
// let index = repo.index()?;
// // 比较 HEAD 和暂存区的 index
// let diff = repo.diff_tree_to_index(
// Some(&head),
// Some(&index),
// Some(&mut opts),
// )?;
// let content = RefCell::new(String::new());
// let current_file_excluded = RefCell::new(false);
// diff.foreach(
// &mut |delta, _| {
// if let Some(path) = delta.new_file().path() {
// if let Some(file_name) = path.file_name() {
// if let Some(file_name_str) = file_name.to_str() {
// *current_file_excluded.borrow_mut() =
// exclude_files
// .contains(&file_name_str.to_string()); }
// }
// }
// true // 总是返回 true 以继续处理下一个文件
// },
// None,
// None,
// Some(&mut |_, _, line| {
// if !*current_file_excluded.borrow() {
// let prefix = match line.origin() {
// '+' => "+",
// '-' => "-",
// _ => " ",
// };
// content.borrow_mut().push_str(prefix);
// content
// .borrow_mut()
// .push_str(&String::from_utf8_lossy(line.content()));
// }
// true // 总是返回 true 以继续处理下一行
// }),
// )?;
// Ok(content.into_inner())
// }
// }
// // pub trait GitDiff {
// // fn git_diff(repo: &git2::Repository) ->
// // crate::error::ReviseResult<String> { let mut opts =
// // git2::DiffOptions::new(); // 创建一个新的DiffOptions实例
// // let head = repo.head()?.peel_to_tree()?; // 获取HEAD指向的tree
// // let diff = repo.diff_tree_to_workdir(Some(&head), Some(&mut
// opts))?; // // 获取当前工作目录与HEAD的差异
// // let mut content = String::new(); // 预分配内存
// // diff.foreach(
// // &mut |_, _| true,
// // None,
// // None,
// // Some(&mut |_, _, line| {
// // let prefix = match line.origin() {
// // '+' => "+",
// // '-' => "-",
// // _ => " ",
// // };
// // content.push_str(prefix);
// //
// content.push_str(&String::from_utf8_lossy(line.content())); //
// content.push('\n'); // true
// // }),
// // )?;
// // Ok(content)
// // }
// // }
// #[cfg(test)]
// mod tests {
// use super::*;
// #[test]
// fn test_git_diff() {
// struct GItDiffImpl;
// impl GitDiff for GItDiffImpl {}
// let _ =
// GItDiffImpl::git_diff(&git2::Repository::open(".").unwrap(), &[
// "README.md".to_string(),
// ])
// .unwrap();
// }
// }
use Command;
use crateReviseResult;