-
Notifications
You must be signed in to change notification settings - Fork 532
git: export git ref to parent branch when possible #6546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -950,7 +950,7 @@ pub fn export_some_refs( | |
update_git_head( | ||
&git_repo, | ||
gix::refs::transaction::PreviousValue::MustExistAndMatch(old_target), | ||
current_oid, | ||
current_oid.map(gix::refs::Target::Object), | ||
) | ||
.map_err(GitExportError::from_git)?; | ||
} | ||
|
@@ -1191,11 +1191,11 @@ fn update_git_ref( | |
fn update_git_head( | ||
git_repo: &gix::Repository, | ||
expected_ref: gix::refs::transaction::PreviousValue, | ||
new_oid: Option<gix::ObjectId>, | ||
new_target: Option<gix::refs::Target>, | ||
) -> Result<(), gix::reference::edit::Error> { | ||
let mut ref_edits = Vec::new(); | ||
let new_target = if let Some(oid) = new_oid { | ||
gix::refs::Target::Object(oid) | ||
let new_target = if let Some(target) = new_target { | ||
target | ||
} else { | ||
// Can't detach HEAD without a commit. Use placeholder ref to nullify | ||
// the HEAD. The placeholder ref isn't a normal branch ref. Git CLI | ||
|
@@ -1259,7 +1259,7 @@ pub fn reset_head(mut_repo: &mut MutableRepo, wc_commit: &Commit) -> Result<(), | |
|
||
// If the first parent of the working copy has changed, reset the Git HEAD. | ||
let old_head_target = mut_repo.git_head(); | ||
if old_head_target != new_head_target { | ||
if old_head_target != new_head_target || true { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you intend to leave the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that was just for testing. I'll remove it |
||
let expected_ref = if let Some(id) = old_head_target.as_normal() { | ||
// We have to check the actual HEAD state because we don't record a | ||
// symbolic ref as such. | ||
|
@@ -1276,9 +1276,38 @@ pub fn reset_head(mut_repo: &mut MutableRepo, wc_commit: &Commit) -> Result<(), | |
// Just overwrite if unborn (or conflict), which is also unusual. | ||
gix::refs::transaction::PreviousValue::MustExist | ||
}; | ||
let new_oid = new_head_target | ||
.as_normal() | ||
.map(|id| gix::ObjectId::from_bytes_or_panic(id.as_bytes())); | ||
|
||
let new_oid = new_head_target.as_normal().map(|id| { | ||
let single_parent_bookmark = wc_commit | ||
.single_parent() | ||
.and_then(|_| { | ||
mut_repo | ||
.view() | ||
.bookmarks() | ||
.filter(|(_, bookmark)| bookmark.local_target == &new_head_target) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a scan over all bookmarks. The commit template uses an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can use |
||
.exactly_one() | ||
.ok() | ||
}) | ||
.and_then(|(name, _)| { | ||
to_git_ref_name( | ||
GitRefKind::Bookmark, | ||
name.to_remote_symbol(REMOTE_NAME_FOR_LOCAL_GIT_REPO), | ||
) | ||
.and_then(|name| { | ||
gix::refs::FullName::try_from(BString::from(name.into_string())).ok() | ||
}) | ||
.map(gix::refs::Target::Symbolic) | ||
Comment on lines
+1292
to
+1299
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is the best way to go from bookmark to Also I don't think it it guaranteed that the bookmarks have been exported to git, so the code would have to check if the bookmark exists in git as well? Alternatively, instead of checking if the parent jj commit has a jj bookmark, we could check if the parent git commit has a branch and check that out using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, we can check if |
||
}); | ||
|
||
match single_parent_bookmark { | ||
Some(target) => target, | ||
None => { | ||
let id = gix::ObjectId::from_bytes_or_panic(id.as_bytes()); | ||
gix::refs::Target::Object(id) | ||
} | ||
} | ||
}); | ||
|
||
update_git_head(&git_repo, expected_ref, new_oid) | ||
.map_err(|err| GitResetHeadError::UpdateHeadRef(err.into()))?; | ||
mut_repo.set_git_head_target(new_head_target); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
.as_slice()
then pattern match?