From 32dd23f42f3268124ef7b10309013fc85ac16586 Mon Sep 17 00:00:00 2001 From: Daichi Toyooka Date: Wed, 19 Nov 2025 15:46:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20`git=20back`=20=E3=82=B3=E3=83=9E?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=82=92=E5=89=8A=E9=99=A4=E3=81=97=E3=80=81?= =?UTF-8?q?=E9=96=A2=E9=80=A3=E3=81=99=E3=82=8B=E3=83=89=E3=82=AD=E3=83=A5?= =?UTF-8?q?=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 - cmd/branch/back.go | 55 ------------- cmd/branch/back_test.go | 168 ---------------------------------------- doc/commands/branch.md | 9 --- setup.ps1 | 1 - setup.sh | 1 - 6 files changed, 237 deletions(-) delete mode 100644 cmd/branch/back.go delete mode 100644 cmd/branch/back_test.go diff --git a/README.md b/README.md index 5d7b8e1..16aeec1 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,6 @@ Git の日常操作を少しだけ楽にするための拡張コマンド集で - `git rename-branch` - 現在のブランチ名を安全に変更し、--push でリモートも更新 - `git delete-local-branches` - マージ済みローカルブランチをまとめて削除 - `git recent` - 最近使用したブランチを時系列で表示して切り替え -- `git back` - 前のブランチやタグに戻る(`git checkout -` のショートカット) - `git sync` - リモートのデフォルトブランチと同期(rebase使用) - `git abort` - 進行中の rebase / merge / cherry-pick / revert を自動判定して中止 @@ -202,7 +201,6 @@ ln -s git-plus git-browse ln -s git-plus git-pr-checkout ln -s git-plus git-clone-org ln -s git-plus git-batch-clone -ln -s git-plus git-back ln -s git-plus git-issue-list ln -s git-plus git-issue-create ln -s git-plus git-issue-edit @@ -260,7 +258,6 @@ Copy-Item "$binPath\git-plus.exe" "$binPath\git-browse.exe" Copy-Item "$binPath\git-plus.exe" "$binPath\git-pr-checkout.exe" Copy-Item "$binPath\git-plus.exe" "$binPath\git-clone-org.exe" Copy-Item "$binPath\git-plus.exe" "$binPath\git-batch-clone.exe" -Copy-Item "$binPath\git-plus.exe" "$binPath\git-back.exe" Copy-Item "$binPath\git-plus.exe" "$binPath\git-issue-list.exe" Copy-Item "$binPath\git-plus.exe" "$binPath\git-issue-create.exe" Copy-Item "$binPath\git-plus.exe" "$binPath\git-issue-edit.exe" diff --git a/cmd/branch/back.go b/cmd/branch/back.go deleted file mode 100644 index 655878f..0000000 --- a/cmd/branch/back.go +++ /dev/null @@ -1,55 +0,0 @@ -/* -Package branch は git の拡張コマンドのうち、ブランチ操作に関するコマンドを定義します。 - -このファイル (back.go) は、前のブランチやタグに戻るコマンドを提供します。 -git checkout - のショートカットとして機能し、 -直前にいたブランチやタグに素早く戻ることができます。 - -主な機能: - - 前のブランチ・タグへの切り替え(git checkout -) - - ブランチやタグ間の素早い移動 - - 作業ブランチとメインブランチの往復、タグの確認などに便利 - -使用例: - git back # 前のブランチ/タグに戻る -*/ -package branch - -import ( - "fmt" - - "github.com/spf13/cobra" - "github.com/tonbiattack/git-plus/cmd" - "github.com/tonbiattack/git-plus/internal/gitcmd" -) - -// backCmd は前のブランチやタグに戻るコマンドです。 -// git checkout - を実行し、直前にチェックアウトしていたブランチやタグに切り替えます。 -var backCmd = &cobra.Command{ - Use: "back", - Short: "前のブランチ/タグに戻る", - Long: `git checkout - を実行し、直前にいたブランチやタグに戻ります。 - -このコマンドは、ブランチやタグ間を頻繁に移動する際に便利です。 -例えば、作業ブランチとメインブランチの往復、タグの確認などに使用できます。 - -注意: - - 少なくとも一度はブランチやタグを切り替えている必要があります - - 最初のチェックアウトでは使用できません`, - Example: ` git back # 前のブランチ/タグに戻る - git back # git へのエイリアスがあれば使用可能`, - RunE: func(cmd *cobra.Command, args []string) error { - // git checkout - を実行して前のブランチ/タグに切り替え - // "-" は直前にチェックアウトしていたブランチ/タグを示す特殊な記号 - if err := gitcmd.RunWithIO("checkout", "-"); err != nil { - return fmt.Errorf("前のブランチ/タグへの切り替えに失敗しました: %w", err) - } - return nil - }, -} - -// init はコマンドの初期化を行います。 -// backCmd を RootCmd に登録することで、CLI から実行可能にします。 -func init() { - cmd.RootCmd.AddCommand(backCmd) -} diff --git a/cmd/branch/back_test.go b/cmd/branch/back_test.go deleted file mode 100644 index eab19be..0000000 --- a/cmd/branch/back_test.go +++ /dev/null @@ -1,168 +0,0 @@ -package branch - -import ( - "os" - "testing" - - "github.com/tonbiattack/git-plus/cmd" - "github.com/tonbiattack/git-plus/internal/testutil" -) - -// TestBackCmd_CommandSetup はbackコマンドの設定をテストします -func TestBackCmd_CommandSetup(t *testing.T) { - if backCmd.Use != "back" { - t.Errorf("backCmd.Use = %q, want %q", backCmd.Use, "back") - } - - if backCmd.Short == "" { - t.Error("backCmd.Short should not be empty") - } - - if backCmd.Long == "" { - t.Error("backCmd.Long should not be empty") - } - - if backCmd.Example == "" { - t.Error("backCmd.Example should not be empty") - } -} - -// TestBackCmd_InRootCmd はbackコマンドがRootCmdに登録されていることを確認します -func TestBackCmd_InRootCmd(t *testing.T) { - found := false - for _, c := range cmd.RootCmd.Commands() { - if c.Use == "back" { - found = true - break - } - } - - if !found { - t.Error("backCmd should be registered in RootCmd") - } -} - -// TestBackCmd_HasRunE はRunE関数が設定されていることを確認します -func TestBackCmd_HasRunE(t *testing.T) { - if backCmd.RunE == nil { - t.Error("backCmd.RunE should not be nil") - } -} - -// TestBackCmd_SwitchBranch はブランチ切り替え機能をテストします -func TestBackCmd_SwitchBranch(t *testing.T) { - repo := testutil.NewGitRepo(t) - - // 初期コミットを作成 - repo.CreateFile("README.md", "# Test") - repo.Commit("Initial commit") - - // 現在のブランチ名を取得(環境によって異なる可能性がある) - defaultBranch := repo.CurrentBranch() - - // 新しいブランチを作成して切り替え - repo.CreateAndCheckoutBranch("feature/test") - - // デフォルトブランチに戻る - repo.CheckoutBranch(defaultBranch) - - // 現在の作業ディレクトリを取得します。 - // - `os.Getwd()` は現在のカレントワーキングディレクトリの絶対パスを返します。 - // - 戻り値: (string, error) - // - string: カレントディレクトリのパス - // - error: 取得に失敗した場合のエラー(例: 権限がない、パス不在など) - // このテストでは、テスト実行中にカレントディレクトリを一時的にリポジトリのディレクトリに変更するため、 - // 終了時に `defer` で元のディレクトリへ戻す目的で保存しています。 - // 現在の作業ディレクトリを取得します。 - // - `os.Getwd()` は現在のカレントワーキングディレクトリの絶対パスを返します。 - // - 戻り値: (string, error) - // - string: カレントディレクトリのパス - // - error: 取得に失敗した場合のエラー(例: 権限がない、パス不在など) - // このテストでは、テスト実行中にカレントディレクトリを一時的にリポジトリのディレクトリに変更するため、 - // 終了時に `defer` で元のディレクトリへ戻す目的で保存しています。 - oldDir, err := os.Getwd() - if err != nil { - t.Fatalf("Failed to get current directory: %v", err) - } - defer func() { _ = os.Chdir(oldDir) }() - - if err := os.Chdir(repo.Dir); err != nil { - t.Fatalf("Failed to change directory: %v", err) - } - - // backコマンドを実行(feature/testブランチに戻るはず) - err = backCmd.RunE(backCmd, []string{}) - if err != nil { - t.Errorf("backCmd.RunE returned error: %v", err) - } - - // feature/testブランチに戻っていることを確認 - currentBranch := repo.CurrentBranch() - if currentBranch != "feature/test" { - t.Errorf("Current branch = %q, want %q", currentBranch, "feature/test") - } -} - -// TestBackCmd_MultipleSwitches は複数回の切り替えをテストします -func TestBackCmd_MultipleSwitches(t *testing.T) { - repo := testutil.NewGitRepo(t) - - // 初期コミットを作成 - repo.CreateFile("README.md", "# Test") - repo.Commit("Initial commit") - - // 複数のブランチを作成 - repo.CreateBranch("branch-a") - repo.CreateBranch("branch-b") - - // branch-aに切り替え - repo.CheckoutBranch("branch-a") - // branch-bに切り替え - repo.CheckoutBranch("branch-b") - - oldDir, err := os.Getwd() - if err != nil { - t.Fatalf("Failed to get current directory: %v", err) - } - defer func() { _ = os.Chdir(oldDir) }() - - if err := os.Chdir(repo.Dir); err != nil { - t.Fatalf("Failed to change directory: %v", err) - } - - // backコマンドを実行(branch-aに戻る) - err = backCmd.RunE(backCmd, []string{}) - if err != nil { - t.Errorf("backCmd.RunE returned error: %v", err) - } - - currentBranch := repo.CurrentBranch() - if currentBranch != "branch-a" { - t.Errorf("Current branch = %q, want %q", currentBranch, "branch-a") - } -} - -// TestBackCmd_NoHistory は履歴がない場合のテスト -func TestBackCmd_NoHistory(t *testing.T) { - repo := testutil.NewGitRepo(t) - - // 初期コミットを作成 - repo.CreateFile("README.md", "# Test") - repo.Commit("Initial commit") - - oldDir, err := os.Getwd() - if err != nil { - t.Fatalf("Failed to get current directory: %v", err) - } - defer func() { _ = os.Chdir(oldDir) }() - - if err := os.Chdir(repo.Dir); err != nil { - t.Fatalf("Failed to change directory: %v", err) - } - - // 履歴がない場合はエラーになるはず - err = backCmd.RunE(backCmd, []string{}) - if err == nil { - t.Error("backCmd.RunE should return error when there's no checkout history") - } -} diff --git a/doc/commands/branch.md b/doc/commands/branch.md index 12b6c94..5c70d4a 100644 --- a/doc/commands/branch.md +++ b/doc/commands/branch.md @@ -78,15 +78,6 @@ git recent -h # ヘルプを表示 引数は不要です。頻繁に複数のブランチを行き来する場合や、最近作業していたブランチ名を思い出せない場合に便利です。 -## git back - -前のブランチやタグに戻ります。`git checkout -` のショートカットで、ブランチやタグ間の素早い移動に便利です。 - -```bash -git back -git back -h # ヘルプを表示 -``` - ## git sync 現在のブランチをリモートのデフォルトブランチ(main/master)の最新状態と同期します。 diff --git a/setup.ps1 b/setup.ps1 index 7cb5cae..0662622 100644 --- a/setup.ps1 +++ b/setup.ps1 @@ -94,7 +94,6 @@ $commands = @( "git-pr-checkout", "git-clone-org", "git-batch-clone", - "git-back", "git-abort", "git-issue-list", "git-issue-create", diff --git a/setup.sh b/setup.sh index a5118a8..fe19dfe 100644 --- a/setup.sh +++ b/setup.sh @@ -97,7 +97,6 @@ commands=( "git-pr-checkout" "git-clone-org" "git-batch-clone" - "git-back" "git-abort" "git-issue-list" "git-issue-create"