Skip to content

Commit

Permalink
[feat][command] implements update-index
Browse files Browse the repository at this point in the history
  • Loading branch information
smd1121 committed Jan 5, 2024
1 parent c088e95 commit 725139a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion xgit/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typer

from xgit.commands import init, cat_file, ls_files, show_index, hash_object
from xgit.commands import init, cat_file, ls_files, show_index, hash_object, update_index

app = typer.Typer(add_completion=False, rich_markup_mode="markdown")

Expand All @@ -9,6 +9,8 @@
app.command()(init.init)
app.command()(cat_file.cat_file)
app.command()(ls_files.ls_files)
app.command()(update_index.update_index)

app.command(hidden=True)(show_index.show_index)


Expand Down
40 changes: 40 additions & 0 deletions xgit/commands/update_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
from pathlib import Path

import typer
from typer import Option, Argument
from typing_extensions import Optional, Annotated

from xgit.types.index import get_index
from xgit.utils.utils import find_repo


def update_index(
files: Annotated[Optional[list[str]], Argument(help="要更新的文件")] = None,
add: Annotated[bool, Option("--add", help="如果文件在暂存区中不存在,则加入暂存区")] = False,
remove: Annotated[bool, Option("--remove", help="如果文件在暂存区存在,但在本地不存在,则从暂存区移除")] = False,
force_remove: Annotated[bool, Option("--force-remove", help="从暂存区移除文件,即使文件在本地存在")] = False,
refresh: Annotated[bool, Option("--refresh", help="刷新暂存区中的文件状态")] = False,
verbose: Annotated[bool, Option("-v", help="详细输出添加和删除的文件")] = False,
cacheinfo: Annotated[
Optional[list[str]], Option("--cacheinfo", help="与 `--add` 一同使用,用 `<mode>,<object>,<path>` 指定一个 blob 加入暂存区")
] = None,
):
"""
更新暂存区
"""
# === 检查参数正确性 ===
# add, remove, force_remove, verbose 互斥
if add + remove + force_remove + refresh + verbose > 1:
typer.echo("fatal: only one of the options can be used", err=True)
sys.exit(1)

# refresh 不能与 files 同时使用
if refresh and files:
typer.echo("fatal: --refresh cannot be used with files", err=True)
sys.exit(1)

# cacheinfo 必须与 add 同时使用
if cacheinfo and not add:
typer.echo("fatal: --cacheinfo can only be used with --add", err=True)
sys.exit(1)

0 comments on commit 725139a

Please sign in to comment.