|
1 | 1 | use std::path::{self, Path, PathBuf};
|
2 | 2 |
|
3 |
| -use git2::{ |
4 |
| - FetchOptions, ObjectType, Oid, Repository, RepositoryInitOptions, |
5 |
| - WorktreeAddOptions, |
6 |
| -}; |
| 3 | +use git2::{FetchOptions, ObjectType, Oid, Repository, RepositoryInitOptions, WorktreeAddOptions}; |
7 | 4 | use snafu::{ResultExt, Snafu};
|
8 | 5 |
|
9 | 6 | use crate::{
|
@@ -215,6 +212,7 @@ pub fn ensure_worktree_is_at(
|
215 | 212 | worktree_name: &str,
|
216 | 213 | worktree_root: &Path,
|
217 | 214 | branch: &str,
|
| 215 | + base_branch: Option<&str>, |
218 | 216 | commit: Oid,
|
219 | 217 | ) -> Result<()> {
|
220 | 218 | tracing::info!("checking out worktree");
|
@@ -242,29 +240,41 @@ pub fn ensure_worktree_is_at(
|
242 | 240 | commit: head_commit,
|
243 | 241 | })?;
|
244 | 242 | }
|
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 { |
248 | 264 | 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 | + })?; |
257 | 267 | worktree
|
258 | 268 | .checkout_tree(&commit, None)
|
259 | 269 | .context(CheckoutWorktreeSnafu {
|
260 | 270 | worktree: &worktree,
|
261 | 271 | commit: &commit,
|
262 | 272 | })?;
|
263 | 273 | worktree
|
264 |
| - .set_head_bytes(branch.name_bytes()) |
| 274 | + .set_head_bytes(branch_ref.name_bytes()) |
265 | 275 | .context(UpdateWorktreeHeadSnafu {
|
266 | 276 | worktree: &worktree,
|
267 |
| - target: &branch, |
| 277 | + target: &branch_ref, |
268 | 278 | })?;
|
269 | 279 | Ok(())
|
270 | 280 | }
|
|
0 commit comments