-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,123 @@ | ||
// Copyright 2024 The Jujutsu Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use itertools::Itertools; | ||
use jj_lib::op_walk; | ||
|
||
use super::diff::show_op_diff; | ||
use crate::cli_util::{CommandHelper, LogContentFormat}; | ||
use crate::command_error::{user_error, CommandError}; | ||
use crate::diff_util::DiffFormatArgs; | ||
use crate::operation_templater::OperationTemplateLanguage; | ||
use crate::ui::Ui; | ||
|
||
/// Show changes to the repository in an operation | ||
#[derive(clap::Args, Clone, Debug)] | ||
pub struct OperationShowArgs { | ||
/// Show repository changes in this operation, compared to its parent(s) | ||
#[arg(visible_alias = "op", default_value = "@")] | ||
operation: String, | ||
/// Don't show the graph, show a flat list of modified changes | ||
#[arg(long)] | ||
no_graph: bool, | ||
/// Show patch of modifications to changes | ||
/// | ||
/// If the previous version has different parents, it will be temporarily | ||
/// rebased to the parents of the new version, so the diff is not | ||
/// contaminated by unrelated changes. | ||
#[arg(long, short = 'p')] | ||
patch: bool, | ||
#[command(flatten)] | ||
diff_format: DiffFormatArgs, | ||
} | ||
|
||
pub fn cmd_op_show( | ||
ui: &mut Ui, | ||
command: &CommandHelper, | ||
args: &OperationShowArgs, | ||
) -> Result<(), CommandError> { | ||
let workspace_command = command.workspace_helper(ui)?; | ||
let repo = workspace_command.repo(); | ||
let repo_loader = &repo.loader(); | ||
let head_op_str = &command.global_args().at_operation; | ||
let head_ops = if head_op_str == "@" { | ||
// If multiple head ops can't be resolved without merging, let the | ||
// current op be empty. Beware that resolve_op_for_load() will eliminate | ||
// redundant heads whereas get_current_head_ops() won't. | ||
let current_op = op_walk::resolve_op_for_load(repo_loader, head_op_str).ok(); | ||
if let Some(op) = current_op { | ||
vec![op] | ||
} else { | ||
op_walk::get_current_head_ops( | ||
repo_loader.op_store(), | ||
repo_loader.op_heads_store().as_ref(), | ||
)? | ||
} | ||
} else { | ||
vec![op_walk::resolve_op_for_load(repo_loader, head_op_str)?] | ||
}; | ||
let current_op_id = match &*head_ops { | ||
[op] => Some(op.id()), | ||
_ => None, | ||
}; | ||
let op = op_walk::resolve_op_for_load(repo_loader, &args.operation)?; | ||
let parents: Vec<_> = op.parents().try_collect()?; | ||
if parents.is_empty() { | ||
return Err(user_error("Cannot show the root operation")); | ||
} | ||
let parent_op = repo_loader.merge_operations(command.settings(), parents, None)?; | ||
let parent_repo = repo_loader.load_at(&parent_op)?; | ||
let repo = repo_loader.load_at(&op)?; | ||
|
||
// Create a new transaction beginning from `repo`. | ||
let mut workspace_command = | ||
command.for_loaded_repo(ui, command.load_workspace()?, repo.clone())?; | ||
let tx = workspace_command.start_transaction(); | ||
|
||
let with_content_format = LogContentFormat::new(ui, command.settings())?; | ||
|
||
// TODO: Should we make this customizable via clap arg? | ||
let template; | ||
{ | ||
let language = OperationTemplateLanguage::new( | ||
repo_loader.op_store().root_operation_id(), | ||
current_op_id, | ||
command.operation_template_extensions(), | ||
); | ||
let text = command.settings().config().get_string("templates.op_log")?; | ||
template = command | ||
.parse_template( | ||
ui, | ||
&language, | ||
&text, | ||
OperationTemplateLanguage::wrap_operation, | ||
)? | ||
.labeled("op_log"); | ||
} | ||
|
||
ui.request_pager(); | ||
template.format(&op, ui.stdout_formatter().as_mut())?; | ||
|
||
show_op_diff( | ||
ui, | ||
command, | ||
tx, | ||
&parent_repo, | ||
&repo, | ||
!args.no_graph, | ||
&with_content_format, | ||
&args.diff_format, | ||
args.patch, | ||
) | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -900,6 +900,160 @@ fn test_op_diff() { | |
"###); | ||
} | ||
|
||
#[test] | ||
fn test_op_show() { | ||
let test_env = TestEnvironment::default(); | ||
let git_repo_path = test_env.env_root().join("git-repo"); | ||
let git_repo = init_bare_git_repo(&git_repo_path); | ||
test_env.jj_cmd_ok(test_env.env_root(), &["git", "clone", "git-repo", "repo"]); | ||
let repo_path = test_env.env_root().join("repo"); | ||
|
||
// Overview of op log. | ||
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]); | ||
insta::assert_snapshot!(&stdout, @r###" | ||
@ 984d5ceb039f [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 | ||
│ check out git remote's default branch | ||
│ args: jj git clone git-repo repo | ||
◉ 817baaeefcbb [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 | ||
│ fetch from git remote into empty repo | ||
│ args: jj git clone git-repo repo | ||
◉ b51416386f26 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 | ||
│ add workspace 'default' | ||
◉ 9a7d829846af [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 | ||
│ initialize repo | ||
◉ 000000000000 root() | ||
"###); | ||
|
||
// The root operation is empty. | ||
let stderr = test_env.jj_cmd_failure(&repo_path, &["op", "show", "0000000"]); | ||
insta::assert_snapshot!(&stderr, @r###" | ||
Error: Cannot show the root operation | ||
"###); | ||
|
||
// Showing the latest operation. | ||
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "show", "@"]); | ||
insta::assert_snapshot!(&stdout, @r###" | ||
984d5ceb039f [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 | ||
check out git remote's default branch | ||
args: jj git clone git-repo repo | ||
Changed commits: | ||
○ Change sqpuoqvxutmz | ||
+sqpuoqvx 9708515f (empty) (no description set) | ||
○ Change qpvuntsmwlqt | ||
-qpvuntsm hidden 230dd059 (empty) (no description set) | ||
Changed local branches: | ||
branch-1: | ||
+ ulyvmwyz 1d843d1f branch-1 | Commit 1 | ||
- (absent) | ||
Changed remote branches: | ||
branch-1@origin: | ||
+ (tracked) ulyvmwyz 1d843d1f branch-1 | Commit 1 | ||
- (untracked) ulyvmwyz 1d843d1f branch-1 | Commit 1 | ||
"###); | ||
// `jj op show @` should behave identically to `jj op show`. | ||
let stdout_without_op_id = test_env.jj_cmd_success(&repo_path, &["op", "show"]); | ||
assert_eq!(stdout, stdout_without_op_id); | ||
|
||
// Showing a given operation. | ||
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "show", "@-"]); | ||
insta::assert_snapshot!(&stdout, @r###" | ||
817baaeefcbb [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 | ||
fetch from git remote into empty repo | ||
args: jj git clone git-repo repo | ||
Changed commits: | ||
○ Change tqyxmsztkvot | ||
+tqyxmszt 3e785984 branch-3@origin | Commit 3 | ||
○ Change yuvsmzqkmpws | ||
+yuvsmzqk 3d9189bc branch-2@origin | Commit 2 | ||
○ Change ulyvmwyzwuwt | ||
+ulyvmwyz 1d843d1f branch-1@origin | Commit 1 | ||
Changed remote branches: | ||
branch-1@origin: | ||
+ (untracked) ulyvmwyz 1d843d1f branch-1@origin | Commit 1 | ||
- (untracked) (absent) | ||
branch-2@origin: | ||
+ (untracked) yuvsmzqk 3d9189bc branch-2@origin | Commit 2 | ||
- (untracked) (absent) | ||
branch-3@origin: | ||
+ (untracked) tqyxmszt 3e785984 branch-3@origin | Commit 3 | ||
- (untracked) (absent) | ||
"###); | ||
|
||
// Create a conflicted branch using a concurrent operation. | ||
test_env.jj_cmd_ok( | ||
&repo_path, | ||
&[ | ||
"branch", | ||
"set", | ||
"branch-1", | ||
"-r", | ||
"branch-2@origin", | ||
"--at-op", | ||
"@-", | ||
], | ||
); | ||
let (_, stderr) = test_env.jj_cmd_ok(&repo_path, &["log"]); | ||
insta::assert_snapshot!(&stderr, @r###" | ||
Concurrent modification detected, resolving automatically. | ||
"###); | ||
// Showing a merge operation is empty. | ||
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "show"]); | ||
insta::assert_snapshot!(&stdout, @r###" | ||
6c131cd79314 [email protected] 2001-02-03 04:05:14.000 +07:00 - 2001-02-03 04:05:14.000 +07:00 | ||
resolve concurrent operations | ||
args: jj log | ||
"###); | ||
|
||
// Test fetching from git remote. | ||
modify_git_repo(git_repo); | ||
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "fetch"]); | ||
insta::assert_snapshot!(&stdout, @r###" | ||
"###); | ||
insta::assert_snapshot!(&stderr, @r###" | ||
branch: branch-1@origin [updated] tracked | ||
branch: branch-2@origin [updated] untracked | ||
branch: branch-3@origin [deleted] untracked | ||
Abandoned 1 commits that are no longer reachable. | ||
"###); | ||
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "show"]); | ||
insta::assert_snapshot!(&stdout, @r###" | ||
84466f397d80 [email protected] 2001-02-03 04:05:16.000 +07:00 - 2001-02-03 04:05:16.000 +07:00 | ||
fetch from git remote(s) origin | ||
args: jj git fetch | ||
Changed commits: | ||
○ Change qzxslznxxpoz | ||
+qzxslznx d487febd branch-2@origin | Commit 5 | ||
○ Change slvtnnzxztqy | ||
+slvtnnzx 4f856199 branch-1?? branch-1@origin | Commit 4 | ||
○ Change tqyxmsztkvot | ||
-tqyxmszt hidden 3e785984 Commit 3 | ||
Changed local branches: | ||
branch-1: | ||
+ (added) slvtnnzx 4f856199 branch-1?? branch-1@origin | Commit 4 | ||
+ (added) yuvsmzqk 3d9189bc branch-1?? | Commit 2 | ||
- (added) ulyvmwyz 1d843d1f Commit 1 | ||
- (added) yuvsmzqk 3d9189bc branch-1?? | Commit 2 | ||
Changed remote branches: | ||
branch-1@origin: | ||
+ (tracked) slvtnnzx 4f856199 branch-1?? branch-1@origin | Commit 4 | ||
- (tracked) ulyvmwyz 1d843d1f Commit 1 | ||
branch-2@origin: | ||
+ (untracked) qzxslznx d487febd branch-2@origin | Commit 5 | ||
- (untracked) yuvsmzqk 3d9189bc branch-1?? | Commit 2 | ||
branch-3@origin: | ||
+ (untracked) (absent) | ||
- (untracked) tqyxmszt hidden 3e785984 Commit 3 | ||
"###); | ||
} | ||
|
||
fn init_bare_git_repo(git_repo_path: &Path) -> git2::Repository { | ||
let git_repo = git2::Repository::init_bare(git_repo_path).unwrap(); | ||
let git_blob_oid = git_repo.blob(b"some content").unwrap(); | ||
|