Skip to content

feat: libra add reset subcommand.#1261

Merged
genedna merged 1 commit into
gitmono-dev:mainfrom
yyjeqhc:feat_libra_reset
Jul 26, 2025
Merged

feat: libra add reset subcommand.#1261
genedna merged 1 commit into
gitmono-dev:mainfrom
yyjeqhc:feat_libra_reset

Conversation

@yyjeqhc

@yyjeqhc yyjeqhc commented Jul 25, 2025

Copy link
Copy Markdown
Contributor

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

hard reset

root@MSI:~/724/2# libra init

# 第一次提交:1.txt,分支 1
echo "content 1" > 1.txt
libra add 1.txt
libra commit -m "commit 1: add 1.txt"
libra branch 1

# 第二次提交:2.txt,分支 2
echo "content 2" > 2.txt
libra add 2.txt
libra commit -m "commit 2: add 2.txt"
libra branch 2

# 第三次提交:3.txt,分支 3
echo "content 3" > 3.txt
libra add 3.txt
libra commit -m "commit 3: add 3.txt"
libra branch 3

# 第四次提交:4.txt,分支 4
echo "content 4" > 4.txt
libra add 4.txt
libra commit -m "commit 4: add 4.txt"
libra branch 4

echo "eee">>3.txt
echo "eee">>4.txt
echo "eee">>5.txt
libra add 3.txt
Initializing empty Libra repository in ./.libra
root@MSI:~/724/2# libra status
On branch master
Changes to be committed:
  use "libra restore --staged <file>..." to unstage
        modified: 3.txt
Changes not staged for commit:
  use "libra add <file>..." to update what will be committed
  use "libra restore <file>..." to discard changes in working directory
        modified: 4.txt
Untracked files:
  use "libra add <file>..." to include in what will be committed
        5.txt
root@MSI:~/724/2# libra reset --hard 1
HEAD is now at 5ae57f8
root@MSI:~/724/2# ls
1.txt  5.txt

mixed reset

#创建仓库的操作同上

root@MSI:~/724/2# libra reset 1
HEAD is now at 594ebc7
root@MSI:~/724/2# ls
1.txt  2.txt  3.txt  4.txt  5.txt
root@MSI:~/724/2# cat 3.txt
content 3
eee
root@MSI:~/724/2# cat 4.txt
content 4
eee
root@MSI:~/724/2# cat 5.txt
eee
root@MSI:~/724/2# libra status
On branch master
Untracked files:
  use "libra add <file>..." to include in what will be committed
        3.txt
        4.txt
        5.txt
        2.txt

soft reset

#创建仓库的操作同上
root@MSI:~/724/2# libra reset --soft 1
HEAD is now at 2f229d7
root@MSI:~/724/2# libra status
On branch master
Changes to be committed:
  use "libra restore --staged <file>..." to unstage
        new file: 2.txt
        new file: 3.txt
        new file: 4.txt
Changes not staged for commit:
  use "libra add <file>..." to update what will be committed
  use "libra restore <file>..." to discard changes in working directory
        modified: 4.txt
Untracked files:
  use "libra add <file>..." to include in what will be committed
        5.txt
#经检查,文件内容正常

综上,3种reset以后工作区/暂存区文件列表和文件内容,表现和git一致。

Copilot AI review requested due to automatic review settings July 25, 2025 09:58
@vercel

vercel Bot commented Jul 25, 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 25, 2025 2:01pm

This comment was marked as outdated.

@genedna
genedna requested a review from Copilot July 25, 2025 11:48

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 reset subcommand, adding the ability to reset the current HEAD to a specified state with three different modes (soft, mixed, hard). This is part of issue #1239 and provides Git-compatible reset functionality.

  • Implements three reset modes: soft (HEAD only), mixed (HEAD + index), and hard (HEAD + index + working directory)
  • Adds comprehensive test coverage for all reset modes and edge cases
  • Integrates reset command into the CLI interface with proper argument parsing

Reviewed Changes

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

Show a summary per file
File Description
libra/src/command/reset.rs Core implementation of reset functionality with all three modes
libra/tests/command/reset_test.rs Comprehensive test suite covering soft, mixed, hard reset scenarios
libra/src/command/mod.rs Module declaration for reset command
libra/src/cli.rs CLI integration and command parsing for reset
libra/tests/command/mod.rs Test module declaration

// Add file to index - but don't modify working directory files
// Use the blob hash from the tree, not from working directory
// Get blob size for IndexEntry
let blob = mercury::internal::object::blob::Blob::load(&item.id);

Copilot AI Jul 25, 2025

Copy link

Choose a reason for hiding this comment

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

The load method call doesn't handle potential errors. This could panic if the blob object doesn't exist or is corrupted. Use load_object function instead which returns a Result that can be properly handled.

Suggested change
let blob = mercury::internal::object::blob::Blob::load(&item.id);
let blob = load_object(&item.id)
.map_err(|e| format!("failed to load blob: {e}"))?;

Copilot uses AI. Check for mistakes.
Comment thread libra/src/command/reset.rs Outdated
assert!(staged.is_empty(), "Index should be reset in mixed reset");

// Verify unstaged changes exist (2.txt, 3.txt, 4.txt should be untracked/modified)
let unstaged = changes_to_be_staged();

Copilot AI Jul 25, 2025

Copy link

Choose a reason for hiding this comment

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

The function name changes_to_be_staged() suggests it returns staged changes, but it's being used to check for unstaged changes. This appears to be a semantic mismatch that could lead to incorrect test behavior.

Suggested change
let unstaged = changes_to_be_staged();
let unstaged = unstaged_changes();

Copilot uses AI. Check for mistakes.
assert!(staged.is_empty(), "Index should be reset in hard reset");

// Verify only untracked files remain
let unstaged = changes_to_be_staged();

Copilot AI Jul 25, 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 above - changes_to_be_staged() function name suggests staged changes but is being used to verify unstaged changes after hard reset.

Suggested change
let unstaged = changes_to_be_staged();
let unstaged = unstaged_changes();

Copilot uses AI. Check for mistakes.
assert!(fs::metadata("4.txt").is_ok());

// Verify index was reset (4.txt should be untracked)
let unstaged = changes_to_be_staged();

Copilot AI Jul 25, 2025

Copy link

Choose a reason for hiding this comment

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

Consistent with previous issues - the function name and usage don't align semantically, which could lead to test failures or incorrect assertions.

Suggested change
let unstaged = changes_to_be_staged();
let unstaged = unstaged_changes();

Copilot uses AI. Check for mistakes.
@genedna

genedna commented Jul 25, 2025

Copy link
Copy Markdown
Collaborator

@yyjeqhc , 请在 https://github.com/web3infra-foundation/mega/tree/main/aria/contents/docs/libra/command 这里参照其它命令,添加 reset 的文档

@genedna
genedna added this pull request to the merge queue Jul 26, 2025
Merged via the queue into gitmono-dev:main with commit 7289166 Jul 26, 2025
13 checks passed
@yyjeqhc
yyjeqhc deleted the feat_libra_reset 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