Skip to content

Commit

Permalink
fix: escape commit messages in openpr
Browse files Browse the repository at this point in the history
  • Loading branch information
xtexx committed Jan 19, 2025
1 parent 43a86fc commit 7471d97
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions buildit-utils/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ fn handle_commits(commits: &[Commit]) -> anyhow::Result<String> {
break;
}

s.push_str(&format!("- {}\n", c.msg.0.trim()));
s.push_str(&format!("- {}\n", escape(c.msg.0.trim())));
if let Some(body) = &c.msg.1 {
let body = body.split('\n');
for line in body {
let line = line.trim();
if !line.is_empty() {
s.push_str(&format!(" {line}\n"));
s.push_str(&format!(" {}\n", escape(line.trim())));
}
}
}
Expand All @@ -254,6 +254,22 @@ fn handle_commits(commits: &[Commit]) -> anyhow::Result<String> {
Ok(s)
}

fn escape(text: &str) -> String {
// the escaped strings only live for a short time, and they are short
// so the waste of memeory are ignorable
let mut result = String::with_capacity(text.len() * 2);
for char in text.chars() {
if matches!(
char,
'*' | '~' | '`' | '[' | ']' | '(' | ')' | '_' | '-' | '\\'
) {
result.push('\\');
}
result.push(char);
}
result
}

struct Commit {
_id: String,
msg: (String, Option<String>),
Expand Down

0 comments on commit 7471d97

Please sign in to comment.