Skip to content

feat: libra add cherry-pick subcommand.#1270

Merged
genedna merged 1 commit into
gitmono-dev:mainfrom
yyjeqhc:feat_libra_cherry_pick
Jul 27, 2025
Merged

feat: libra add cherry-pick subcommand.#1270
genedna merged 1 commit into
gitmono-dev:mainfrom
yyjeqhc:feat_libra_cherry_pick

Conversation

@yyjeqhc

@yyjeqhc yyjeqhc commented Jul 27, 2025

Copy link
Copy Markdown
Contributor

Part of #1239
实现libra cherry-pick 子命令的基本功能

rm -rf *.txt .libra
libra init
echo "✅ Repo initialized."

# --- 2. 创建共同的祖先提交 (C1) ---
echo "base" > base.txt
libra add base.txt
libra commit -m "C1: Initial commit, our common ancestor"
echo "✅ C1: Created common ancestor."

# --- 3. 创建并切换到 feature 分支 ---
libra switch -c feature
echo "✅ Switched to new branch 'feature'."

# --- 4. 在 feature 分支上创建两个提交 ---
# Commit C2: 我们想要 cherry-pick 的第一个目标
echo "feature A" > feature_a.txt
libra add feature_a.txt
libra commit -m "C2: Add feature_a.txt"
echo "✅ C2: Added feature_a.txt on feature branch."

# Commit C3: 我们想要 cherry-pick 的第二个目标
echo "feature B" > feature_b.txt
libra add feature_b.txt
libra commit -m "C3: Add feature_b.txt"
echo "✅ C3: Added feature_b.txt on feature branch."

# --- 5. 切换回 master 分支 ---
libra switch master
echo "✅ Switched back to master."

# --- 6. 显示最终状态 ---
# 此时 master 分支只包含 C1,没有任何 feature 分支的更改
echo
echo "🎉 Cherry-pick test repo is ready. Current state:"
libra log

测试:

root@MSI:~/724/2# libra switch feature
root@MSI:~/724/2# libra log
commit 448700f1cee34c9322bf9bda5349e9e2760a4594 (HEAD -> feature)
Author: mega <admin@mega.org>
Date: 2025-07-27 13:43:28 UTC +0800

C3: Add feature_b.txt

commit cb58e6cc6f9493ebd61c4b381d78b1356cf73dac
Author: mega <admin@mega.org>
Date: 2025-07-27 13:43:28 UTC +0800

C2: Add feature_a.txt

commit 4b62bb5159debe2173f4da00a759b9bb95afddcd (master)
Author: mega <admin@mega.org>
Date: 2025-07-27 13:43:28 UTC +0800

C1: Initial commit, our common ancestor

root@MSI:~/724/2# libra switch master
root@MSI:~/724/2# libra cherry-pick 448700f1cee34c9322bf9bda5349e9e2760a4594 cb58e6cc6f9493ebd61c4b381d78b1356cf73dac
Cherry-picking commit 448700f...
Finished cherry-pick for 448700f in new commit dc8340a
Cherry-picking commit cb58e6c...
Finished cherry-pick for cb58e6c in new commit 50d7745
root@MSI:~/724/2# ls
base.txt  feature_a.txt  feature_b.txt
root@MSI:~/724/2# cat feature_a.txt
feature A
root@MSI:~/724/2# cat feature_b.txt
feature B

Copilot AI review requested due to automatic review settings July 27, 2025 13:46
@vercel

vercel Bot commented Jul 27, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
mega ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 27, 2025 2:28pm

This comment was marked as outdated.

This comment was marked as outdated.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements the libra cherry-pick subcommand to apply changes from existing commits onto the current branch. The feature allows selective application of commits from one branch to another without merging the entire branch.

Key Changes:

  • Added cherry-pick command with support for single/multiple commits and --no-commit option
  • Implemented three-way merge logic to apply commit changes
  • Added comprehensive test coverage for various cherry-pick scenarios

Reviewed Changes

Copilot reviewed 7 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
libra/src/command/cherry_pick.rs Core implementation of cherry-pick functionality with three-way merge logic
libra/src/command/mod.rs Exposed the new cherry_pick module
libra/src/cli.rs Added cherry-pick command to CLI interface
libra/tests/command/cherry_pick_test.rs Comprehensive test suite covering basic, multi-commit, and error scenarios
libra/tests/command/mod.rs Added cherry_pick_test module
libra/tests/command/revert_test.rs Updated CommitArgs with new disable_pre field
aria/contents/docs/libra/command/cherry-pick/index.mdx Documentation for the cherry-pick command
Comments suppressed due to low confidence (1)

libra/tests/command/cherry_pick_test.rs:434

  • The test executes cherry-pick with a non-existent commit but doesn't assert that it actually fails or check the error behavior. Add assertions to verify the expected error handling.
    // Test cherry-picking non-existent commit should fail gracefully

///
/// This is used when applying cherry-pick changes to update the index state.
fn update_index_entry(index: &mut Index, path: &Path, hash: SHA1) {
let blob = mercury::internal::object::blob::Blob::load(&hash);

Copilot AI Jul 27, 2025

Copy link

Choose a reason for hiding this comment

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

The Blob::load call doesn't handle potential errors. If the blob cannot be loaded, this will likely panic or return an invalid blob. Consider adding error handling with a Result return type.

Suggested change
let blob = mercury::internal::object::blob::Blob::load(&hash);
let blob = match mercury::internal::object::blob::Blob::load(&hash) {
Ok(blob) => blob,
Err(e) => {
eprintln!("Failed to load blob for hash {}: {}", hash, e);
return; // Exit the function if the blob cannot be loaded
}
};

Copilot uses AI. Check for mistakes.
for path_buf in &tracked_paths {
let path_str = path_buf.to_str().unwrap();
if let Some(entry) = index.get(path_str, 0) {
let blob = mercury::internal::object::blob::Blob::load(&entry.hash);

Copilot AI Jul 27, 2025

Copy link

Choose a reason for hiding this comment

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

Same issue as line 245 - Blob::load call lacks error handling. This could cause the function to panic if the blob cannot be loaded from storage.

Suggested change
let blob = mercury::internal::object::blob::Blob::load(&entry.hash);
let blob = mercury::internal::object::blob::Blob::load(&entry.hash)
.map_err(|e| format!("Failed to load blob: {}", e))?;

Copilot uses AI. Check for mistakes.
Comment on lines +53 to +57
if args.no_commit && args.commits.len() > 1 {
eprintln!("fatal: cannot cherry-pick multiple commits with --no-commit");
eprintln!("(use 'libra commit' to save the changes from the first cherry-pick)");
return;
}

Copilot AI Jul 27, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] This artificial limitation differs from Git's behavior and reduces functionality. Consider implementing proper multi-commit support with --no-commit or documenting this as a known limitation in the help text.

Suggested change
if args.no_commit && args.commits.len() > 1 {
eprintln!("fatal: cannot cherry-pick multiple commits with --no-commit");
eprintln!("(use 'libra commit' to save the changes from the first cherry-pick)");
return;
}
// TODO: Implement proper multi-commit support for --no-commit to align with Git's behavior.

Copilot uses AI. Check for mistakes.
Comment on lines +38 to +44
/// TODO: Now, it will apply the picked commits exactly as they are,
/// without resolving any conflicts, and it will not take the state of the staging area into account.
pub async fn execute(args: CherryPickArgs) {
if !util::check_repo_exist() {
return;
}

Copilot AI Jul 27, 2025

Copy link

Choose a reason for hiding this comment

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

The TODO comment indicates incomplete conflict resolution functionality. This is a significant limitation that should be documented in the command help or user documentation, not just in code comments.

Suggested change
/// TODO: Now, it will apply the picked commits exactly as they are,
/// without resolving any conflicts, and it will not take the state of the staging area into account.
pub async fn execute(args: CherryPickArgs) {
if !util::check_repo_exist() {
return;
}
///
/// Note: The cherry-pick operation applies commits exactly as they are, without resolving conflicts
/// or considering the state of the staging area.
pub async fn execute(args: CherryPickArgs) {
if !util::check_repo_exist() {
return;
}
println!("Note: Conflicts will not be resolved, and the staging area will not be considered during cherry-pick.");

Copilot uses AI. Check for mistakes.
@genedna
genedna added this pull request to the merge queue Jul 27, 2025
Merged via the queue into gitmono-dev:main with commit f31c5d8 Jul 27, 2025
18 of 20 checks passed
@yyjeqhc
yyjeqhc deleted the feat_libra_cherry_pick branch July 28, 2025 02:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants