You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
alpha(update): add --squash, --preserve-path, --output-branch for PR-friendly upgrades
This change makes `kubebuilder alpha update` produce a PR-ready branch and a
single squashed commit when requested, improving automation and review UX.
Key changes
-----------
• New `--squash` flag:
- Snapshots the exact tree of the temporary merge branch into ONE commit on a
stable branch: `kubebuilder-alpha-update-to-<to-version>`.
- Intended for opening/refreshing idempotent PRs.
- Gracefully handles "no changes" (git commit exits 1 → treated as no-op).
• New `--preserve-path` (repeatable):
- When squashing, restore given paths from the base branch (e.g.
`.github/workflows`) so CI/config files are kept as-is on the PR branch.
• New `--output-branch`:
- Overrides the default PR branch name created by `--squash`.
• Commit message used by `--squash`:
- `[kubebuilder-automated-update]: update scaffold from <from> to <to>; (squashed 3-way merge)`
• Behavior/ergonomics:
- Without `--force`: stops on conflicts on the temporary merge branch.
- With `--force`: commits conflict markers on the merge branch (automation-friendly).
- After merge, still best-effort run: `make manifests generate fmt vet lint-fix`.
Defaults / Compatibility
------------------------
- `--squash` is off by default (no behavior change unless opted-in).
- `--from-branch` defaults to `main`.
- `--preserve-path` is empty by default (no restores).
- Safe to run on projects scaffolded with v4.5.0+ (uses `alpha generate`).
Motivation
----------
Make upgrades PR-centric and automation-ready by producing a deterministic,
reviewable branch and a single squashed commit that mirrors the merge result.
Assisted-by: ChatGPT (OpenAI)
It then commits **once**. If there were conflicts, the single commit will contain conflict markers.
54
+
55
+
You can push either `tmp-merge-*` (no squashing) or the output branch (with `--squash`) to your remote and open a PR.
46
56
47
57
## How to Use It
48
58
49
-
Run the command from your project directory:
59
+
Run from your project root:
50
60
51
61
```sh
52
62
kubebuilder alpha update
53
63
```
54
64
55
-
If needed, set a specific version or branch:
65
+
Pin versions and base branch:
56
66
57
67
```sh
58
68
kubebuilder alpha update \
59
-
--from-version=v4.5.2 \
60
-
--to-version=v4.6.0 \
61
-
--from-branch=main
69
+
--from-versionv4.5.2 \
70
+
--to-versionv4.6.0 \
71
+
--from-branchmain
62
72
```
63
73
64
-
Force update even with merge conflicts:
74
+
Automation-friendly: proceed even with conflicts:
65
75
66
76
```sh
67
77
kubebuilder alpha update --force
68
78
```
69
79
70
-
<asideclass="note warning">
71
-
<h1>You might need to upgrade your project first</h1>
80
+
Create a **single squashed commit** on a stable PR branch:
81
+
82
+
```sh
83
+
kubebuilder alpha update --force --squash
84
+
```
85
+
86
+
Squash while **preserving** paths from your base branch (keep CI/workflows, etc.):
87
+
88
+
```sh
89
+
kubebuilder alpha update --force --squash \
90
+
--preserve-path .github/workflows \
91
+
--preserve-path docs
92
+
```
93
+
94
+
Use a **custom output branch** name:
95
+
96
+
```sh
97
+
kubebuilder alpha update --force --squash \
98
+
--output-branch upgrade/kb-to-v4.7.0
99
+
```
72
100
73
-
This command uses `kubebuilder alpha generate` internally.
74
-
As a result, the version of the CLI originally used to create your project must support `alpha generate`.
101
+
### Commit message used in `--squash` mode
75
102
76
-
This command has only been tested with projects created using **v4.5.0** or later.
77
-
It might not work with projects that were initially created using a Kubebuilder version older than **v4.5.0**.
103
+
```
104
+
[kubebuilder-automated-update]: update scaffold from <from> to <to>; (squashed 3-way merge)
105
+
```
78
106
79
-
If your project was created with an older version, run `kubebuilder alpha generate` first to re-scaffold it.
80
-
Once updated, you can use `kubebuilder alpha update` for future upgrades.
107
+
<asideclass="note warning">
108
+
<h1>You might need to upgrade your project first</h1>
109
+
This command uses <code>kubebuilder alpha generate</code> internally. The Kubebuilder version originally used to create your project must support <code>alpha generate</code>.<br><br>
110
+
This command has been tested with projects created using <strong>v4.5.0+</strong>. If your project predates that, first run <code>kubebuilder alpha generate</code> once to modernize the scaffold. After that, you can use <code>kubebuilder alpha update</code> for future upgrades.
|`--from-version`|**Required for projects initialized with versions earlier than v4.6.0.** Kubebuilder version your project was created with. If unset, uses the `PROJECT` file. |
88
-
|`--to-version`| Version to upgrade to. Defaults to the latest version. |
89
-
|`--from-branch`| Git branch that contains your current project code. Defaults to `main`. |
90
-
|`--force`| Force the update even if conflicts occur. Conflicted files will include conflict markers, and a commit will be created automatically. Ideal for automation (e.g., cronjobs, CI). |
|`--from-version`|**Required for projects initialized before v4.6.0.** Kubebuilder version your project was created with. If unset, uses the `PROJECT` file. |
118
+
|`--to-version`| Version to upgrade to. Defaults to the latest release. |
119
+
|`--from-branch`| Git branch that contains your current project code. Defaults to `main`. |
120
+
|`--force`| Continue even if there are merge conflicts. Conflicted files are committed with conflict markers (great for CI/cron). |
121
+
|`--squash`| Write the merge result as a **single commit** on a stable output branch. |
122
+
|`--preserve-path`| Repeatable. When squashing, restore these paths from the base branch (e.g., `--preserve-path .github/workflows`). |
123
+
|`--output-branch`| Custom branch name for the squashed commit (defaults to `kubebuilder-alpha-update-to-<to-version>`). |
124
+
|`-h, --help`| Show help for this command. |
125
+
92
126
<asideclass="note">
93
-
Projects generated with **Kubebuilder v4.6.0** or later include the `cliVersion` field in the `PROJECT` file.
94
-
This field is used by `kubebuilder alpha update` to determine the correct CLI
95
-
version for upgrading your project.
127
+
Projects generated with **Kubebuilder v4.6.0+** include <code>cliVersion</code> in the <code>PROJECT</code> file. `kubebuilder alpha update` uses this to pick the correct CLI for re-scaffolding.
96
128
</aside>
97
129
98
130
## Merge Conflicts with `--force`
99
131
100
-
When you use the `--force` flag with `kubebuilder alpha update`, Git will complete the merge even if there are conflicts. The resulting commit will include conflict markers like:
132
+
When you use `--force`, Git completes the merge even if there are conflicts. The resulting commit includes conflict markers like:
133
+
101
134
```
102
135
<<<<<<< HEAD
103
136
Your changes
104
137
=======
105
138
Incoming changes
106
-
>>>>>>> branch-name
139
+
>>>>>>> tmp-original-…
107
140
```
108
-
These conflicts will be committed in the
109
-
`tmp-kb-update-merge` branch.
110
141
111
-
<asideclass="note warning">
112
-
You must manually resolve these conflicts before merging into your main branch.
142
+
-**Without `--force`**: the command stops on `tmp-merge-*` and prints guidance; no commit is created.
143
+
-**With `--force`**: the merge is committed (on `tmp-merge-*`, or on the output branch if using `--squash`) and includes the markers.
113
144
114
145
<asideclass="note warning">
115
-
<H1>If you face conflicts (using or not the --force flag) </H1>
116
-
If the merge introduces conflicts, you must resolve them and **ensure** you execute the following command to regenerate the manifests and organise the files properly:
146
+
You must manually resolve these conflicts before merging into your main branch.<br><br>
- Automated environments (e.g., CI pipelines or cron jobs)
133
-
- When you need to create a PR even if conflicts are present
134
-
- When a human will resolve the conflicts later
135
-
`kubebuilder alpha update --force`
136
157
137
-
This ensures the update proceeds without manual blocking but shifts responsibility for conflict resolution to a follow-up manual step.
158
+
Use `--force` when:
159
+
160
+
- Running in automated environments (CI/cron) and you want a PR/branch even if conflicts occur
161
+
- A human will resolve conflicts later
162
+
- You prefer the tool to be non-blocking
138
163
139
-
This approach is typically used in automation workflows where conflict markers are later addressed by a human, or where preserving the conflicting changes is acceptable for follow-up processing.
164
+
This ensures the update proceeds without manual blocking but
165
+
shifts responsibility for conflict resolution to a follow-up step.
140
166
141
-
## Requirements
167
+
## Demonstration
142
168
143
-
- A valid [PROJECT][project-config] file at the root of your project
144
-
- A clean Git working directory (no uncommitted changes)
145
-
- Git must be installed and available
169
+
<iframewidth="560"height="315"src="https://www.youtube.com/embed/J8zonID__8k?si=WC-FXOHX0mCjph71"title="YouTube video player"frameborder="0"allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"referrerpolicy="strict-origin-when-cross-origin"allowfullscreen></iframe>
0 commit comments