-
Notifications
You must be signed in to change notification settings - Fork 726
Implement reword_commit as an example of rebase engine usage #11452
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
Open
Caleb-T-Owens
wants to merge
1
commit into
graph-based-rebasing
Choose a base branch
from
reimplement-commit-reword
base: graph-based-rebasing
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−25
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| use bstr::{BString, ByteSlice}; | ||
| use but_api_macros::but_api; | ||
| use but_oplog::legacy::{OperationKind, SnapshotDetails}; | ||
| use tracing::instrument; | ||
|
|
||
| /// Rewords a commit, but without updating the oplog. | ||
| /// | ||
| /// Returns the ID of the newly renamed commit | ||
| #[but_api] | ||
| #[instrument(err(Debug))] | ||
| pub fn reword_commit_only( | ||
| ctx: &but_ctx::Context, | ||
| commit_id: gix::ObjectId, | ||
| message: BString, | ||
| ) -> anyhow::Result<gix::ObjectId> { | ||
| let mut guard = ctx.exclusive_worktree_access(); | ||
| let (repo, _, graph) = ctx.graph_and_meta_mut_and_repo_from_head(guard.write_permission())?; | ||
|
|
||
| but_workspace::commit::reword(&graph, &repo, commit_id, message.as_bstr()) | ||
| } | ||
|
|
||
| /// Apply `existing_branch` to the workspace in the repository that `ctx` refers to, or create the workspace with default name. | ||
| /// | ||
| /// Returns the ID of the newly renamed commit | ||
| #[but_api] | ||
| #[instrument(err(Debug))] | ||
| pub fn reword_commit( | ||
| ctx: &but_ctx::Context, | ||
| commit_id: gix::ObjectId, | ||
| message: BString, | ||
| ) -> anyhow::Result<gix::ObjectId> { | ||
| // NOTE: since this is optional by nature, the same would be true if snapshotting/undo would be disabled via `ctx` app settings, for instance. | ||
| let maybe_oplog_entry = but_oplog::UnmaterializedOplogSnapshot::from_details( | ||
| ctx, | ||
| SnapshotDetails::new(OperationKind::UpdateCommitMessage), | ||
| ) | ||
| .ok(); | ||
|
|
||
| let res = reword_commit_only(ctx, commit_id, message); | ||
| if let Some(snapshot) = maybe_oplog_entry.filter(|_| res.is_ok()) { | ||
| snapshot.commit(ctx).ok(); | ||
| }; | ||
| res | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //! An action to perform a reword of a commit | ||
|
|
||
| pub(crate) mod function { | ||
| use anyhow::Result; | ||
| use bstr::BStr; | ||
| use but_rebase::{ | ||
| commit::DateMode, | ||
| graph_rebase::{GraphExt, Step}, | ||
| }; | ||
|
|
||
| /// This action will rewrite a commit and any relevant history so it uses | ||
| /// the new name. | ||
| /// | ||
| /// Returns the ID of the newly renamed commit | ||
| pub fn reword( | ||
| graph: &but_graph::Graph, | ||
| repo: &gix::Repository, | ||
| commit_id: gix::ObjectId, | ||
| new_message: &BStr, | ||
| ) -> Result<gix::ObjectId> { | ||
| let mut editor = graph.to_editor(repo)?; | ||
| let target_selector = editor.select_commit(commit_id)?; | ||
|
|
||
| let mut commit = editor.find_commit(commit_id)?; | ||
| commit.message = new_message.to_owned(); | ||
| let new_id = editor.write_commit(commit, DateMode::CommitterUpdateAuthorKeep)?; | ||
|
|
||
| editor.replace(&target_selector, Step::new_pick(new_id)); | ||
|
|
||
| let outcome = editor.rebase()?; | ||
| outcome.materialize()?; | ||
|
|
||
| Ok(new_id) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
crates/but-workspace/tests/fixtures/scenario/reword-three-commits.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -eu -o pipefail | ||
|
|
||
| echo "/remote/" > .gitignore | ||
| mkdir remote | ||
| pushd remote | ||
| git init | ||
| popd | ||
|
|
||
| git init | ||
|
|
||
| git remote add origin ./remote | ||
|
|
||
| git checkout -b one | ||
| echo "foo" > one.txt | ||
| git add . | ||
| git commit -m "commit one" | ||
|
|
||
| git checkout -b two | ||
| echo "foo" > two.txt | ||
| git add . | ||
| git commit -m "commit two" | ||
|
|
||
| git push -u origin two | ||
|
|
||
| git checkout -b three | ||
| echo "foo" > three.txt | ||
| git add . | ||
| git commit -m "commit three" |
2 changes: 2 additions & 0 deletions
2
...s/but-workspace/tests/workspace/commit.rs → ...t-workspace/tests/workspace/commit/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| mod reword; | ||
|
|
||
| mod from_new_merge_with_metadata { | ||
| use bstr::ByteSlice; | ||
| use but_graph::init::{Options, Overlay}; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Copy-paste error in documentation: This docstring describes "Apply
existing_branchto the workspace..." which is unrelated to rewording commits. This appears to be copied from another function and not updated.The documentation should describe what
reword_commitactually does, for example: