Skip to content

Commit 8204a1d

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 8204a1d

File tree

5 files changed

+302
-75
lines changed

5 files changed

+302
-75
lines changed

src/ext/repository.rs

Lines changed: 21 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,17 @@ 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+
// fetch the comment symbol from the config
345+
.string("core.commentChar")
346+
// convert the value to a string and trim it
347+
.and_then(|value| value.to_str().ok().map(|s| s.trim().to_string()))
348+
// filter out empty values and "auto"
349+
.filter(|value_str| !value_str.is_empty() && value_str != "auto")
350+
// if no value is found, return "#"
351+
.unwrap_or("#".to_string())
352+
}
333353
}

0 commit comments

Comments
 (0)