Skip to content

Commit 1ccbeac

Browse files
committed
feat: respect core.commentChar setting for escaping comment characters in commit messages
Bring StGit in parity with git's behavior for comment characters. Setting `core.commentChar` to a value other than '#' allows the use of certain markup like markdown in commit messages. Git for that reason allows overriding the comment character to any string value. This change incurs a small amount of extra allocations for constructing strings at runtime taking into account the user's choice of comment character. issue: 600
1 parent f88c7f0 commit 1ccbeac

File tree

5 files changed

+298
-68
lines changed

5 files changed

+298
-68
lines changed

src/ext/repository.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::borrow::Cow;
44

55
use anyhow::{anyhow, Result};
6-
use bstr::BStr;
6+
use bstr::{BStr, ByteSlice};
77

88
use crate::{
99
stupid::Stupid,
@@ -78,6 +78,13 @@ pub(crate) trait RepositoryExtended {
7878

7979
/// [`gix::Repository::rev_parse_single()`] with StGit-specific error mapping.
8080
fn rev_parse_single_ex(&self, spec: &str) -> Result<gix::Id<'_>>;
81+
82+
/// Get the comment symbol from git config.
83+
///
84+
/// Returns the configured `core.commentChar` value, defaulting to "#" if not set.
85+
/// Git's default is '#' and it also supports 'auto' which we treat as '#'.
86+
/// Note: git allows this to be a string, not just a single character.
87+
fn get_comment_symbol(&self) -> String;
8188
}
8289

8390
/// Options for creating a git commit object.
@@ -330,4 +337,21 @@ impl RepositoryExtended for gix::Repository {
330337
}
331338
})
332339
}
340+
341+
fn get_comment_symbol(&self) -> String {
342+
let config = self.config_snapshot();
343+
config
344+
.string("core.commentChar")
345+
.and_then(|value| value.to_str().ok().map(|s| s.to_string()))
346+
.map(|value_str| {
347+
// Git supports 'auto' which means it will pick a character that doesn't
348+
// conflict with the message. We just use '#' for 'auto'.
349+
if value_str == "auto" || value_str.is_empty() {
350+
"#".to_string()
351+
} else {
352+
value_str
353+
}
354+
})
355+
.unwrap_or("#".to_string())
356+
}
333357
}

0 commit comments

Comments
 (0)