Skip to content

Commit 1f8995e

Browse files
committed
Take default branch into account for changelog link
1 parent 8183ba7 commit 1f8995e

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v0.3.0 (in development)
22
-----------------------
33
- Switch from nom to winnow
4+
- `release`: Take name of repository's default branch into account when adding
5+
changelog link to README
46

57
v0.2.0 (2023-11-22)
68
-------------------

src/commands/release.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ impl Release {
4343
.context("Could not determine GitHub repository for local repository")?;
4444
let is_lib = package.is_lib()?;
4545
let publish = metadata.publish.as_deref() != Some(&[]);
46+
let Some(default_branch) = git.default_branch()? else {
47+
bail!("Could not determine repository's default branch");
48+
};
4649

4750
// Determine new version
4851
let new_version = if let Some(v) = self.version {
@@ -270,7 +273,7 @@ impl Release {
270273
let Some(mut readme) = readme_file.get()? else {
271274
bail!("README.md suddenly disappeared!");
272275
};
273-
if readme.ensure_changelog_link(&ghrepo) {
276+
if readme.ensure_changelog_link(&ghrepo, &default_branch) {
274277
log::info!("Adding Changelog link to README.md ...");
275278
readme_file.set(readme)?;
276279
}

src/git.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ impl<'a> Git<'a> {
136136
}
137137
Ok(PathBuf::from(s))
138138
}
139+
140+
// Returns None if the default branch could not be determined
141+
pub(crate) fn default_branch(&self) -> Result<Option<String>, CommandOutputError> {
142+
let branches = self
143+
.readlines("branch", ["--format=%(refname:short)"])?
144+
.collect::<HashSet<_>>();
145+
for guess in ["main", "master"] {
146+
if branches.contains(guess) {
147+
return Ok(Some(guess.to_owned()));
148+
}
149+
}
150+
Ok(None)
151+
}
139152
}
140153

141154
#[cfg(test)]

src/readme.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ impl Readme {
103103
}
104104

105105
// Returns `true` if changed
106-
pub(crate) fn ensure_changelog_link(&mut self, repo: &GHRepo) -> bool {
106+
pub(crate) fn ensure_changelog_link(&mut self, repo: &GHRepo, default_branch: &str) -> bool {
107107
if self.links.iter().any(|lnk| lnk.text == "Changelog") {
108108
false
109109
} else {
110110
self.links.push(Link {
111-
url: format!("https://github.com/{repo}/blob/master/CHANGELOG.md"),
111+
url: format!("https://github.com/{repo}/blob/{default_branch}/CHANGELOG.md"),
112112
text: "Changelog".into(),
113113
});
114114
true

0 commit comments

Comments
 (0)