Skip to content
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

bookmark: Make moving a bookmark onto a hidden commit a warning #5050

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cli/src/commands/bookmark/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops::Deref;

use clap::builder::NonEmptyStringValueParser;
use jj_lib::object_id::ObjectId as _;
use jj_lib::op_store::RefTarget;
Expand Down Expand Up @@ -46,7 +48,9 @@ pub fn cmd_bookmark_create(
let mut workspace_command = command.workspace_helper(ui)?;
let target_commit = workspace_command
.resolve_single_rev(ui, args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
let repo = workspace_command.repo();
let view = workspace_command.repo().view();
let is_hidden = target_commit.is_hidden(repo.deref())?;
Copy link
Collaborator

@yuja yuja Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: .as_ref() (or something like &*repo.)

let bookmark_names = &args.names;
for name in bookmark_names {
if view.get_local_bookmark(name).is_present() {
Expand Down Expand Up @@ -87,6 +91,14 @@ pub fn cmd_bookmark_create(
writeln!(ui.hint_default(), "Use -r to specify the target revision.")?;
}

if is_hidden {
writeln!(
ui.status(),
"Created {} bookmarks onto a hidden commit, use `jj new` to unhide it.",
bookmark_names.len()
)?;
}

tx.finish(
ui,
format!(
Expand Down
10 changes: 10 additions & 0 deletions cli/src/commands/bookmark/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops::Deref;

use clap_complete::ArgValueCandidates;
use itertools::Itertools as _;
use jj_lib::object_id::ObjectId as _;
Expand Down Expand Up @@ -91,6 +93,7 @@ pub fn cmd_bookmark_move(
let repo = workspace_command.repo().clone();

let target_commit = workspace_command.resolve_single_rev(ui, &args.to)?;
let is_hidden = target_commit.is_hidden(repo.deref())?;
let matched_bookmarks = {
let is_source_ref: Box<dyn Fn(&RefTarget) -> _> = if !args.from.is_empty() {
let is_source_commit = workspace_command
Expand Down Expand Up @@ -168,6 +171,13 @@ pub fn cmd_bookmark_move(
)?;
}

if is_hidden {
writeln!(
ui.hint_default(),
"Moved a bookmark onto a hidden commit, use `jj new` to unhide it"
)?;
}

tx.finish(
ui,
format!(
Expand Down
9 changes: 9 additions & 0 deletions cli/src/commands/bookmark/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
.resolve_single_rev(ui, args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
let repo = workspace_command.repo().as_ref();
let bookmark_names = &args.names;
let is_hidden = target_commit.is_hidden(repo)?;
let mut new_bookmark_count = 0;
let mut moved_bookmark_count = 0;
for name in bookmark_names {
Expand Down Expand Up @@ -101,6 +102,14 @@
if bookmark_names.len() > 1 && args.revision.is_none() {
writeln!(ui.hint_default(), "Use -r to specify the target revision.")?;
}
// TODO: Emit a warning here to `jj new -r <old>` to revive the commmit if it

Check failure on line 105 in cli/src/commands/bookmark/set.rs

View workflow job for this annotation

GitHub Actions / Codespell

commmit ==> commit
// was hidden.
if is_hidden {
writeln!(
ui.hint_default(),
"Moved a bookmark onto a hidden commit, use `jj new` to unhide it."
)?;
}
if new_bookmark_count > 0 {
// TODO: delete this hint in jj 0.25+
writeln!(
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commit_templater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,8 @@ fn builtin_commit_methods<'repo>() -> CommitTemplateBuildMethodFnMap<'repo, Comm
function.expect_no_arguments()?;
let repo = language.repo;
let out_property = self_property.map(|commit| {
let maybe_entries = repo.resolve_change_id(commit.change_id());
maybe_entries.map_or(true, |entries| !entries.contains(commit.id()))
let value = commit.is_hidden(repo);
value.unwrap_or_default()
});
Ok(L::wrap_boolean(out_property))
},
Expand Down
15 changes: 15 additions & 0 deletions cli/tests/test_bookmark_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,21 @@ fn test_bookmark_list_conflicted() {
"###);
}

#[test]
fn test_bookmark_create_onto_hidden_warning() {
// TODO: write
}

#[test]
fn test_bookmark_move_onto_hidden_warning() {
// TODO: write
}

#[test]
fn test_bookmark_set_onto_hidden_warning() {
// TODO: write
}

fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
let template = r#"bookmarks ++ " " ++ commit_id.short()"#;
test_env.jj_cmd_success(cwd, &["log", "-T", template])
Expand Down
6 changes: 6 additions & 0 deletions lib/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ impl Commit {
&self.data.committer
}

/// A commit is hidden, if its commit id is not in the predecessor set.
pub fn is_hidden(&self, repo: &dyn Repo) -> BackendResult<bool> {
let maybe_entries = repo.resolve_change_id(self.change_id());
Ok(maybe_entries.map_or(true, |entries| !entries.contains(&self.id)))
}

/// A commit is discardable if it has no change from its parent, and an
/// empty description.
pub fn is_discardable(&self, repo: &dyn Repo) -> BackendResult<bool> {
Expand Down
Loading