Skip to content

feat: libra add revert subcommand.#1266

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

feat: libra add revert subcommand.#1266
genedna merged 1 commit into
gitmono-dev:mainfrom
yyjeqhc:feat_libra_revert

Conversation

@yyjeqhc

@yyjeqhc yyjeqhc commented Jul 26, 2025

Copy link
Copy Markdown
Contributor

Part of #1239
实现libra revert子命令的基本功能

#!/bin/bash

# --- 环境准备 ---
# 清理旧环境
rm -rf .libra *.txt

# 初始化仓库
libra init
echo "✅ 仓库已初始化"

# 创建一系列提交
echo "content a" > a.txt
libra add a.txt
libra commit -m "commit 1: add a.txt"

echo "content b" > b.txt
libra add b.txt
libra commit -m "commit 2: add b.txt"

echo "content c" > c.txt
libra add c.txt
libra commit -m "commit 3: add c.txt"

echo "d.txt will be untracked" > d_untracked.txt

echo "✅ 准备工作完成,当前状态如下:"
libra log
root@MSI:~/724/2# libra revert HEAD~
[fb62b48] Revert commit bd0032e
root@MSI:~/724/2# ls
a.txt  c.txt  d_untracked.txt
root@MSI:~/724/2# libra log
commit fb62b481596df7236b63de6d4061dfef81616304 (HEAD -> master)
Author: mega <admin@mega.org>
Date: 2025-07-26 10:52:05 UTC +0800

Revert ""

This reverts commit bd0032ead734fec9835156f1f2d4f4558fd1a812.

commit bd43015230c3dd1c76222940ea35633bc16e0786
Author: mega <admin@mega.org>
Date: 2025-07-26 10:51:58 UTC +0800

commit 3: add c.txt

commit bd0032ead734fec9835156f1f2d4f4558fd1a812
Author: mega <admin@mega.org>
Date: 2025-07-26 10:51:58 UTC +0800

commit 2: add b.txt

commit 7715d01d9610950b2824460151968ea9f7128763
Author: mega <admin@mega.org>
Date: 2025-07-26 10:51:58 UTC +0800

commit 1: add a.txt

Copilot AI review requested due to automatic review settings July 26, 2025 10:53
@vercel

vercel Bot commented Jul 26, 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 4:32am

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 revert subcommand to create new commits that undo changes from existing commits, following standard Git revert behavior. It provides a safe way to undo changes without altering project history.

Key changes include:

  • Core revert implementation with support for regular commits and root commits
  • --no-commit flag to stage changes without committing
  • Comprehensive test coverage for various revert scenarios

Reviewed Changes

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

Show a summary per file
File Description
libra/src/command/revert.rs Main implementation of revert functionality with commit resolution, tree manipulation, and conflict detection
libra/src/command/mod.rs Module registration and typo fix in save_object parameter name
libra/src/cli.rs CLI integration for the new revert command
libra/tests/command/revert_test.rs Comprehensive test suite covering basic revert, no-commit flag, root commits, and error cases
libra/tests/command/mod.rs Test module registration
aria/contents/docs/libra/command/revert/index.mdx Documentation for the revert command usage and examples
Comments suppressed due to low confidence (1)

libra/tests/command/revert_test.rs:302

  • The error test doesn't verify that the operation actually fails or check the error message. It should assert that the revert operation returns an error and verify the working directory remains unchanged.
    // Test reverting non-existent commit should fail gracefully

Comment thread libra/src/command/revert.rs
Comment on lines +129 to +136
if current_files.get(path) != Some(&reverted_hash) && current_files.contains_key(path) {
return Err(format!(
"conflict: file '{}' was modified in a later commit",
path.display()
));

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 conflict detection logic is flawed. This condition will trigger false conflicts when the current file state legitimately differs from the reverted commit due to other changes. The check should compare if the current state conflicts with applying the revert, not just if it differs from the original commit.

Suggested change
if current_files.get(path) != Some(&reverted_hash) && current_files.contains_key(path) {
return Err(format!(
"conflict: file '{}' was modified in a later commit",
path.display()
));
if let Some(current_hash) = current_files.get(path) {
if let Some(parent_hash) = parent_hash {
// Check if applying the revert would overwrite legitimate changes
if current_hash != parent_hash {
return Err(format!(
"conflict: file '{}' was modified in a later commit",
path.display()
));
}
} else {
// File was newly added in the reverted commit, check if it still exists
return Err(format!(
"conflict: file '{}' was added in a later commit",
path.display()
));
}

Copilot uses AI. Check for mistakes.
Comment thread libra/src/command/revert.rs Outdated
Comment thread libra/src/command/revert.rs Outdated
if let Some(entry) = temp_index.get(path_str, 0) {
tree_items.push(mercury::internal::object::tree::TreeItem {
mode: mercury::internal::object::tree::TreeItemMode::Blob,
name: path_buf.file_name().unwrap().to_str().unwrap().to_string(),

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.

Using only file_name() will cause issues with files in subdirectories as they will lose their directory structure. Files like 'dir/file.txt' will be stored as just 'file.txt' at the root level.

Suggested change
name: path_buf.file_name().unwrap().to_str().unwrap().to_string(),
name: path_buf.to_str().unwrap().to_string(), // Use full relative path

Copilot uses AI. Check for mistakes.
Comment thread libra/src/command/revert.rs Outdated
// Simplified implementation: create a temporary index, then create tree based on this index
let mut temp_index = Index::new();
for (path, hash) in files {
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.

Loading blob objects just to get their size is inefficient. The size information should be available from the index entry or object metadata without loading the entire blob content.

Copilot uses AI. Check for mistakes.
Comment thread libra/src/command/revert.rs Outdated
@genedna
genedna added this pull request to the merge queue Jul 27, 2025
Merged via the queue into gitmono-dev:main with commit 45e9851 Jul 27, 2025
13 checks passed
@yyjeqhc
yyjeqhc deleted the feat_libra_revert 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