-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.rs
More file actions
151 lines (121 loc) · 3.78 KB
/
git.rs
File metadata and controls
151 lines (121 loc) · 3.78 KB
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
use std::{
fs::File,
io::{Read, Write},
path::PathBuf,
process::Command,
};
use anyhow::{Context, Result};
use regex::Regex;
use crate::buddy::Buddies;
// Markers for the git-squad section in the commit template
const BEGIN_MARKER: &str = "# BEGIN GIT-SQUAD";
const END_MARKER: &str = "# END GIT-SQUAD";
pub fn get_commit_template_path() -> Result<PathBuf> {
let output = Command::new("git")
.args(["config", "--get", "commit.template"])
.output()
.context("Failed to execute git command")?;
if !output.status.success() {
anyhow::bail!(
"No template file set. Configure one using `git config --set \
commit.template /path/to/template/file"
)
}
let path_str = String::from_utf8(output.stdout)
.context("Failed to parse git output")?
.trim()
.to_string();
Ok(PathBuf::from(path_str))
}
pub fn get_active_buddies(buddies: &Buddies) -> Result<Buddies> {
let template_path = get_commit_template_path()?;
if !template_path.exists() {
return Ok(Buddies::default());
}
let mut file = File::open(&template_path)
.context("Failed to open commit template file")?;
let mut contents = String::new();
file
.read_to_string(&mut contents)
.context("Failed to read commit template file")?;
let co_author_regex = Regex::new(r"Co-authored-by: .* <(.+)>").unwrap();
let mut active_buddies = Vec::new();
let mut in_squad_section = false;
for line in contents.lines() {
if line.trim() == BEGIN_MARKER {
in_squad_section = true;
continue;
}
if line.trim() == END_MARKER {
in_squad_section = false;
continue;
}
if in_squad_section {
if let Some(captures) = co_author_regex.captures(line) {
if let Some(email_match) = captures.get(1) {
let email = email_match.as_str();
if let Some(buddy) = buddies.get_buddy_by_email(email) {
active_buddies.push(buddy.clone());
}
}
}
}
}
Ok(Buddies::new(active_buddies))
}
pub fn update_commit_template(active_buddies: &Buddies) -> Result<()> {
let template_path = get_commit_template_path()?;
if !template_path.exists() {
let template_dir = template_path.parent().unwrap();
if !template_dir.exists() {
anyhow::bail!("Template dir '{}' doens't exist", template_dir.display());
}
File::create(&template_path)
.context("Failed to create commit template file")?;
}
let mut file = File::open(&template_path)
.context("Failed to open commit template file")?;
let mut contents = String::new();
file
.read_to_string(&mut contents)
.context("Failed to read commit template file")?;
// Extract the content excluding our section
let mut new_content = String::new();
let mut skipping = false;
// Parse the file line by line, skipping our section
for line in contents.lines() {
if line.trim() == BEGIN_MARKER {
skipping = true;
continue;
}
if line.trim() == END_MARKER {
skipping = false;
continue;
}
if !skipping {
new_content.push_str(line);
new_content.push('\n');
}
}
// Trim trailing whitespace
new_content = new_content.trim_end().to_string();
// Add our section with co-authors if needed
if !active_buddies.buddies.is_empty() {
new_content.push_str("\n\n");
new_content.push_str(BEGIN_MARKER);
new_content.push('\n');
for buddy in &active_buddies.buddies {
new_content.push_str(&buddy.format_co_author());
new_content.push('\n');
}
new_content.push_str(END_MARKER);
new_content.push('\n');
}
// Write back to file
let mut file = File::create(&template_path)
.context("Failed to open commit template file for writing")?;
file
.write_all(new_content.as_bytes())
.context("Failed to write to commit template file")?;
Ok(())
}