Skip to content

Commit 3d6f961

Browse files
patchable: Set the base branch as the git upstream branch (#1131)
* patchable: Set the base branch as the git upstream branch This helps some git GUIs (like magit) narrow in on prioritizing our patch commits, rather than just "the last N commits". * Changelog --------- Co-authored-by: Nick <10092581+NickLarsenNZ@users.noreply.github.com>
1 parent 586b4d0 commit 3d6f961

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ All notable changes to this project will be documented in this file.
6868
- BREAKING: kcat: Stop building kcat image ([#1124]).
6969
- containerdebug updated to 0.2.0 ([#1128])
7070
- Build Hadoop as `stackable` and configure the Stackable Nexus build-repo for the `root` user ([#1133])
71+
- patchable: The base branch is now configured as the git upstream branch ([#1131]).
7172

7273
### Fixed
7374

@@ -151,6 +152,7 @@ All notable changes to this project will be documented in this file.
151152
[#1126]: https://github.com/stackabletech/docker-images/pull/1126
152153
[#1127]: https://github.com/stackabletech/docker-images/pull/1127
153154
[#1128]: https://github.com/stackabletech/docker-images/pull/1128
155+
[#1131]: https://github.com/stackabletech/docker-images/pull/1131
154156
[#1133]: https://github.com/stackabletech/docker-images/pull/1133
155157

156158
## [25.3.0] - 2025-03-21

rust/patchable/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ fn main() -> Result<()> {
426426
&ctx.pv.version,
427427
&product_worktree_root,
428428
&worktree_branch,
429+
base_branch.as_deref(),
429430
patched_commit,
430431
)
431432
.context(CheckoutProductWorktreeSnafu)?;

rust/patchable/src/repo.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use std::path::{self, Path, PathBuf};
22

3-
use git2::{
4-
FetchOptions, ObjectType, Oid, Repository, RepositoryInitOptions,
5-
WorktreeAddOptions,
6-
};
3+
use git2::{FetchOptions, ObjectType, Oid, Repository, RepositoryInitOptions, WorktreeAddOptions};
74
use snafu::{ResultExt, Snafu};
85

96
use crate::{
@@ -215,6 +212,7 @@ pub fn ensure_worktree_is_at(
215212
worktree_name: &str,
216213
worktree_root: &Path,
217214
branch: &str,
215+
base_branch: Option<&str>,
218216
commit: Oid,
219217
) -> Result<()> {
220218
tracing::info!("checking out worktree");
@@ -242,29 +240,41 @@ pub fn ensure_worktree_is_at(
242240
commit: head_commit,
243241
})?;
244242
}
245-
let branch = worktree
246-
.branch(branch, &commit_obj, true)
247-
.context(CreateWorktreeBranchSnafu {
243+
let mut branch =
244+
worktree
245+
.branch(branch, &commit_obj, true)
246+
.context(CreateWorktreeBranchSnafu {
247+
repo: &worktree,
248+
branch,
249+
commit,
250+
})?;
251+
// Set the base branch as the patch branch's upstream, this helps some git GUIs (like magit)
252+
// visualize the difference between upstream and our patchset.
253+
if let Err(err) = branch.set_upstream(base_branch) {
254+
tracing::warn!(
255+
error = &err as &dyn std::error::Error,
256+
branch.base = base_branch,
257+
"failed to set upstream branch, ignoring..."
258+
);
259+
}
260+
let branch_ref = branch.into_reference();
261+
let commit = branch_ref
262+
.peel(ObjectType::Commit)
263+
.context(FindCommitSnafu {
248264
repo: &worktree,
249-
branch,
250-
commit,
251-
})?
252-
.into_reference();
253-
let commit = branch.peel(ObjectType::Commit).context(FindCommitSnafu {
254-
repo: &worktree,
255-
commit: &branch,
256-
})?;
265+
commit: &branch_ref,
266+
})?;
257267
worktree
258268
.checkout_tree(&commit, None)
259269
.context(CheckoutWorktreeSnafu {
260270
worktree: &worktree,
261271
commit: &commit,
262272
})?;
263273
worktree
264-
.set_head_bytes(branch.name_bytes())
274+
.set_head_bytes(branch_ref.name_bytes())
265275
.context(UpdateWorktreeHeadSnafu {
266276
worktree: &worktree,
267-
target: &branch,
277+
target: &branch_ref,
268278
})?;
269279
Ok(())
270280
}

0 commit comments

Comments
 (0)