使用switch+restore替代checkout

This commit is contained in:
HouXiaoxuan
2023-12-22 03:34:58 +08:00
parent f48585a380
commit b0172211a0
6 changed files with 62 additions and 4 deletions

View File

@@ -11,10 +11,19 @@ Git in Rust. 用 Rust 编写的简易 Git
- [x] rm
- [x] commit
- 支持分支 git branch, git checkout
- 支持分支 git branch, git checkout
- [x] branch
- [ ] Checkout
- [x] branch
- [ ] switch
- [ ] restore
```bash
# 撤销未暂存的文件更改不涉及un trached file)
git restore path
git restore . # 全部
```
- 支持简单的合并 git merge

View File

@@ -6,6 +6,7 @@ use mit::commands::init::init;
use mit::commands::log::log;
use mit::commands::remove::remove;
use mit::commands::status::status;
use mit::commands::switch::switch;
/// Rust实现的简易版本的Git用于学习Rust语言
#[derive(Parser)]
@@ -86,6 +87,28 @@ enum Command {
#[clap(long, action, group = "sub")]
show_current: bool,
},
/// 切换分支
Switch {
/// 要切换的分支
#[clap(required_unless_present("create"))]
branch: Option<String>,
/// 创建并切换到新分支
#[clap(long, short)]
create: Option<String>,
},
/// restore
Restore {
// TODO 行为不确定
/// 要恢复的文件
#[clap(required = true)]
files: Vec<String>,
/// source
#[clap(long, short)]
source: Option<String>,
},
}
pub fn handle_command() {
let cli = Cli::parse();
@@ -111,5 +134,12 @@ pub fn handle_command() {
Command::Branch { list, delete, new_branch, commit_hash, show_current } => {
branch(new_branch, commit_hash, list, delete, show_current);
}
Command::Switch { branch, create } => {
switch(branch, create);
}
Command::Restore { files, source } => {
println!("files: {:?}, source: {:?}", files, source);
}
}
}

View File

@@ -38,6 +38,7 @@ pub fn __log(all: bool, number: Option<usize>) -> usize {
log_count += 1;
let commit = Commit::load(&head_commit);
if first {
// TODO: (HEAD -> ttt, ad2)
first = false;
print!(
"{}{}{}{}",

View File

@@ -4,4 +4,6 @@ pub mod init;
pub mod remove;
pub mod status;
pub mod log;
pub mod branch;
pub mod branch;
pub mod switch;
pub mod restore;

0
src/commands/restore.rs Normal file
View File

16
src/commands/switch.rs Normal file
View File

@@ -0,0 +1,16 @@
pub fn switch(branch: Option<String>, create: Option<String>) {
// TODO
match create {
Some(branch_name) => match branch {
Some(branch) => {
println!("craete and switch to branch: {:?} base on {:?}", branch_name, branch);
}
None => {
println!("create and switch to branch: {:?}", branch_name);
}
},
None => {
println!("switch to branch: {:?}", branch.unwrap());
}
}
}