Skip to content

Commit c87162a

Browse files
author
Ned Batchelder
committed
feat: collect --title=TEXT #48
1 parent 52800b0 commit c87162a

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Added
2+
.....
3+
4+
- The ``collect`` command now has a ``--title=TEXT`` option to provide the
5+
exact text to use as the title of the new changelog entry. Finishes `issue
6+
48`_.
7+
8+
.. _issue 48: https://github.com/nedbat/scriv/issues/48

docs/commands.rst

+10-4
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ scriv collect
107107
Collect fragments and produce a combined entry in the CHANGELOG file.
108108
109109
Options:
110-
--add / --no-add 'git add' the updated changelog file.
110+
--add / --no-add 'git add' the updated changelog file and removed
111+
fragments.
111112
--edit / --no-edit Open the changelog file in your text editor.
113+
--title TEXT The title text to use for this entry.
112114
--keep Keep the fragment files that are collected.
113115
--version TEXT The version name to use for this entry.
114116
-v, --verbosity LVL Either CRITICAL, ERROR, WARNING, INFO or DEBUG
115117
--help Show this message and exit.
116-
.. [[[end]]] (checksum: 5a37816dcb12829eca58f1ccd42a8e62)
118+
.. [[[end]]] (checksum: 3f25ae739160c63a0685e98f88681927)
117119
118120
The collect command aggregates all the current fragments into the changelog
119121
file.
@@ -124,8 +126,11 @@ Entry Creation
124126
All of the .rst or .md files in the fragment directory are read, parsed, and
125127
re-assembled into a changelog entry. The entry's title is determined by the
126128
:ref:`config_entry_title_template` setting. The default uses the version string
127-
(if one is specified in the from the :ref:`config_version` setting) and the
128-
current date.
129+
(if one is specified in the :ref:`config_version` setting) and the current
130+
date.
131+
132+
Instead of using the title template, you can provide an exact title to use for
133+
the new entry with the ``--title`` option.
129134

130135
The output file is specified by the :ref:`config_output_file` setting. Scriv
131136
looks in the file for a special marker (usually in a comment) to determine
@@ -135,6 +140,7 @@ this, you can have your changelog be just part of a larger README file. If
135140
there is no marker in the file, the new entry is inserted at the top of the
136141
file.
137142

143+
138144
Fragment Deletion
139145
-----------------
140146

src/scriv/collect.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Collecting fragments."""
22

33
import logging
4+
import sys
45
from typing import Optional
56

67
import click
@@ -14,13 +15,18 @@
1415

1516
@click.command()
1617
@click.option(
17-
"--add/--no-add", default=None, help="'git add' the updated changelog file."
18+
"--add/--no-add",
19+
default=None,
20+
help="'git add' the updated changelog file and removed fragments.",
1821
)
1922
@click.option(
2023
"--edit/--no-edit",
2124
default=None,
2225
help="Open the changelog file in your text editor.",
2326
)
27+
@click.option(
28+
"--title", default=None, help="The title text to use for this entry."
29+
)
2430
@click.option(
2531
"--keep", is_flag=True, help="Keep the fragment files that are collected."
2632
)
@@ -29,11 +35,18 @@
2935
)
3036
@click_log.simple_verbosity_option(logger)
3137
def collect(
32-
add: Optional[bool], edit: Optional[bool], keep: bool, version: str
38+
add: Optional[bool],
39+
edit: Optional[bool],
40+
title: str,
41+
keep: bool,
42+
version: str,
3343
) -> None:
3444
"""
3545
Collect fragments and produce a combined entry in the CHANGELOG file.
3646
"""
47+
if title is not None and version is not None:
48+
sys.exit("Can't provide both --title and --version.")
49+
3750
if add is None:
3851
add = git_config_bool("scriv.collect.add")
3952
if edit is None:
@@ -49,7 +62,10 @@ def collect(
4962
changelog = scriv.changelog()
5063
changelog.read()
5164

52-
new_header = changelog.entry_header(version=version)
65+
if title is None:
66+
new_header = changelog.entry_header(version=version)
67+
else:
68+
new_header = changelog.format_tools().format_header(title)
5369
new_text = changelog.entry_text(scriv.combine_fragments(frags))
5470
changelog.add_entry(new_header, new_text)
5571
changelog.write()

tests/test_collect.py

+21
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,24 @@ def test_no_fragments(cli_invoke, changelog_d, temp_dir, caplog):
437437
changelog_text = (temp_dir / "CHANGELOG.rst").read_text()
438438
assert changelog_text == "Not much\n"
439439
assert "No changelog fragments to collect" in caplog.text
440+
441+
442+
def test_title_provided(cli_invoke, changelog_d, temp_dir):
443+
(changelog_d / "20170616_nedbat.rst").write_text(COMMENT + FRAG1 + COMMENT)
444+
(changelog_d / "20170617_nedbat.rst").write_text(COMMENT + FRAG2)
445+
title = "This is the Header"
446+
cli_invoke(["collect", "--title", title])
447+
changelog_text = (temp_dir / "CHANGELOG.rst").read_text()
448+
# With --title provided, the first header is literally what was provided.
449+
lines = CHANGELOG_1_2.splitlines()
450+
lines[1] = title
451+
lines[2] = len(title) * "="
452+
assert changelog_text == "\n".join(lines) + "\n"
453+
454+
455+
def test_title_and_version_clash(cli_invoke):
456+
result = cli_invoke(
457+
["collect", "--title", "xx", "--version", "1.2"], expect_ok=False
458+
)
459+
assert result.exit_code == 1
460+
assert str(result.exception) == "Can't provide both --title and --version."

0 commit comments

Comments
 (0)