Skip to content

Commit 893ec03

Browse files
committed
Added documentation for replacing strings in different files.
Fixes #6
1 parent 9388b64 commit 893ec03

File tree

1 file changed

+92
-23
lines changed

1 file changed

+92
-23
lines changed

docsrc/search-and-replace.md

+92-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,59 @@
11
# Searching and replacing versions
22

3-
Given this `requirements.txt`:
3+
Bump-my-version uses [template strings](https://docs.python.org/3/library/string.html#format-string-syntax) to search the configured files for the old or current version and replace the text with the new version.
4+
5+
You can configure the search or replace templates globally and within each `tool.bumpversion.files` entry in your configuration.
6+
7+
The default search template is `{current_version}` to find the version string within the file and replace it with `{new_version}`.
8+
9+
The search and replace templates can be multiple lines, like so:
10+
11+
```toml
12+
[tool.bumpversion]
13+
current_version = "1.2.3"
14+
15+
[[tool.bumpversion.files]]
16+
filename = "config.ini"
17+
search = "[myproject]\nversion={current_version}"
18+
replace = "[myproject]\nversion={new_version}"
19+
```
20+
21+
Alternatively, using [TOML's multiline strings](https://toml.io/en/v1.0.0#string):
22+
23+
```toml
24+
[tool.bumpversion]
25+
current_version = "1.2.3"
26+
27+
[[tool.bumpversion.files]]
28+
filename = "config.ini"
29+
search = """
30+
[myproject]
31+
version={current_version}"""
32+
33+
replace = """
34+
[myproject]
35+
version={new_version}"""
36+
```
37+
38+
## Avoiding incorrect replacements
39+
40+
In files that have multiple version strings, bump-my-version may find the wrong string and replace it. Given this `requirements.txt` for `MyProject`:
441

542
```text
643
Django>=1.5.6,<1.6
744
MyProject==1.5.6
845
```
946

10-
using this `.bumpversion.toml` will ensure only the line containing `MyProject` will be changed:
47+
The default search and replace templates will replace the wrong text. Instead of changing `MyProject`'s version from `1.5.6` to `1.6.0`, it changes `Django`'s version:
48+
49+
```text
50+
Django>=1.6.0,<1.6
51+
MyProject==1.5.6
52+
```
53+
54+
Providing search and replace templates for the `requirements.txt` file will avoid this.
55+
56+
This `.bumpversion.toml` will ensure only the line containing `MyProject` will be changed:
1157

1258
```toml
1359
[tool.bumpversion]
@@ -19,7 +65,7 @@ search = "MyProject=={current_version}"
1965
replace = "MyProject=={new_version}"
2066
```
2167

22-
It can be multiple lines, templated using [Python Format String Syntax](https://docs.python.org/3/library/string.html#format-string-syntax).
68+
2369

2470
If the string to be replaced includes literal quotes, the search and replace patterns must include them to match. Given the file `version.sh`:
2571

@@ -34,49 +80,72 @@ search = "MY_VERSION=\"{current_version}\""
3480
replace = "MY_VERSION=\"{new_version}\""
3581
```
3682

37-
---
83+
## Custom version formats in different files
84+
85+
You can use file configurations to replace the version in multiple files, even if each file has the version formatted differently.
86+
87+
In a module-aware Go project, when you create a major version of your module beyond `v1`, your module name must include the major version number (e.g., `github.com/myorg/myproject/v2`). However, you also have the full version in a YAML file named `release-channels.yaml`.
88+
89+
`go.mod` file:
3890

39-
## Using bumpversion to maintain a go mod file within a Go project
91+
```go
92+
module github.com/myorg/myproject/v2
93+
94+
go 1.12
95+
96+
require (
97+
...
98+
)
99+
```
40100

41-
In a module-aware Go project, when you create a major version of your module beyond v1, your module name will need to include the major version number (e.g., `github.com/myorg/myproject/v2`).
101+
`release-channels.yaml` file:
102+
103+
```yaml
104+
stable: "v2.21.4"
105+
```
42106
43107
You can use bump-my-version to maintain the major version number within the `go.mod` file by using the `parse` and `serialize` options, as in this example:
44108

45-
- Example `.bumpversion.toml` file:
109+
`.bumpversion.toml` file:
46110

47111
```toml
48112
[tool.bumpversion]
49-
current_version = 2.0.0
50-
commit = True
113+
current_version = 2.21.4
51114
52115
[[tool.bumpversion.files]]
53116
filename = "go.mod"
54117
parse = "(?P<major>\\d+)"
55118
serialize = "{major}"
56119
search = "module github.com/myorg/myproject/v{current_version}"
57120
replace = "module github.com/myorg/myproject/v{new_version}"
121+
122+
[[tool.bumpversion.files]]
123+
filename = "release-channels.yaml"
58124
```
59125

60-
- Example `go.mod` file:
126+
While all the version bumps are `minor` or `patch`, the `go.mod` file doesn't change, while the `release-channels.yaml` file will. As soon as you do a `major` version bump, the `go.mod` file now contains this module directive:
61127

62128
```go
63-
module github.com/myorg/myproject/v2
129+
module github.com/myorg/myproject/v3
130+
```
64131

65-
go 1.12
132+
## Multiple replacements within the same file
66133

67-
require (
68-
...
69-
)
70-
```
134+
To make several replacements in the same file, you must configure multiple `[[tool.bumpversion.files]]` sections for the same file with different configuration options.
71135

72-
Then run this command to create version 3.0.0 of your project:
136+
In this example, the changelog is generated before the version bump. It uses `Unreleased` as the heading and includes a link to GitHub to compare this version (`HEAD`) with the previous version.
73137

74-
```console
75-
bump-my-version --new-version 3.0.0 major
76-
```
138+
To change `Unreleased ` to the current version, we have an entry with `search` set to `Unreleased`. The default `replace` value is `{new_version}`, so changing it is unnecessary.
77139

78-
Your `go.mod` file now contains this module directive:
140+
To change the link, another entry has its `search` set to `{current_version}...HEAD` and the `replace` set to `{current_version}...{new_version}`.
79141

80-
```go
81-
module github.com/myorg/myproject/v3
142+
```toml
143+
[[tool.bumpversion.files]]
144+
filename = "CHANGELOG.md"
145+
search = "Unreleased"
146+
147+
[[tool.bumpversion.files]]
148+
filename = "CHANGELOG.md"
149+
search = "{current_version}...HEAD"
150+
replace = "{current_version}...{new_version}"
82151
```

0 commit comments

Comments
 (0)