diff --git a/README.md b/README.md index 3800020..09b3c49 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/cli.rs b/src/cli.rs index 7317099..d926092 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, + + /// 创建并切换到新分支 + #[clap(long, short)] + create: Option, + }, + /// restore + Restore { + // TODO 行为不确定 + /// 要恢复的文件 + #[clap(required = true)] + files: Vec, + + /// source + #[clap(long, short)] + source: Option, + }, } 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); + } } } diff --git a/src/commands/log.rs b/src/commands/log.rs index e0dcd35..59e8264 100644 --- a/src/commands/log.rs +++ b/src/commands/log.rs @@ -38,6 +38,7 @@ pub fn __log(all: bool, number: Option) -> usize { log_count += 1; let commit = Commit::load(&head_commit); if first { + // TODO: (HEAD -> ttt, ad2) first = false; print!( "{}{}{}{}", diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a3986cb..674f706 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -4,4 +4,6 @@ pub mod init; pub mod remove; pub mod status; pub mod log; -pub mod branch; \ No newline at end of file +pub mod branch; +pub mod switch; +pub mod restore; \ No newline at end of file diff --git a/src/commands/restore.rs b/src/commands/restore.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/commands/switch.rs b/src/commands/switch.rs new file mode 100644 index 0000000..757790c --- /dev/null +++ b/src/commands/switch.rs @@ -0,0 +1,16 @@ +pub fn switch(branch: Option, create: Option) { + // 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()); + } + } +}