diff --git a/modules/scripts/default.nix b/modules/scripts/default.nix index 54d59f0..980a369 100644 --- a/modules/scripts/default.nix +++ b/modules/scripts/default.nix @@ -1,7 +1,8 @@ { ... }: +let + entries = builtins.readDir ./.; + dirs = builtins.filter (name: entries.${name} == "directory") (builtins.attrNames entries); +in { - imports = [ - ./switch-branch-with-refresh.nix - ./nix-init-env.nix - ]; + imports = map (d: ./. + "/${d}") dirs; } diff --git a/modules/scripts/nix-init-env.nix b/modules/scripts/nix-init-env/default.nix similarity index 80% rename from modules/scripts/nix-init-env.nix rename to modules/scripts/nix-init-env/default.nix index f41fd3b..3e96627 100644 --- a/modules/scripts/nix-init-env.nix +++ b/modules/scripts/nix-init-env/default.nix @@ -8,7 +8,7 @@ pkgs.jq pkgs.coreutils ]; - text = builtins.readFile ./nix-init-env.sh; + text = builtins.readFile ./script.sh; }) ]; } diff --git a/modules/scripts/nix-init-env.sh b/modules/scripts/nix-init-env/script.sh similarity index 100% rename from modules/scripts/nix-init-env.sh rename to modules/scripts/nix-init-env/script.sh diff --git a/modules/scripts/switch-branch-with-refresh.nix b/modules/scripts/switch-branch-with-refresh.nix deleted file mode 100644 index bcdfba8..0000000 --- a/modules/scripts/switch-branch-with-refresh.nix +++ /dev/null @@ -1,44 +0,0 @@ -{ pkgs, ... }: -{ - home.packages = [ - (pkgs.writeShellApplication { - name = "switch-branch-with-refresh"; - runtimeInputs = [ pkgs.git ]; - text = '' - if [ -z "''${1:-}" ]; then - echo "Usage: switch-branch-with-refresh " >&2 - exit 1 - fi - - current_branch=$(git branch --show-current) - - if [ -z "$current_branch" ]; then - echo "Error: Not on a branch (detached HEAD?)" >&2 - exit 1 - fi - - # 新ブランチを作成(現在のコミットをそのまま引き継ぐ) - if ! git switch -c "$1"; then - echo "Error: Failed to create branch '$1'." >&2 - exit 1 - fi - - # 元ブランチをリモートの状態にリセット - git fetch origin - git switch "$current_branch" - echo "Warning: This will reset '$current_branch' to origin/$current_branch with --hard." - read -r -p "Continue? [y/N] " confirm - if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then - echo "Aborted. Switching back to '$1'." - git switch "$1" - exit 1 - fi - git reset --hard "origin/$current_branch" - - # 新ブランチに移動して完了 - git switch "$1" - echo "Done. Commits are on '$1'. '$current_branch' reset to origin/$current_branch." - ''; - }) - ]; -} diff --git a/modules/scripts/switch-branch-with-refresh/default.nix b/modules/scripts/switch-branch-with-refresh/default.nix new file mode 100644 index 0000000..75d6be3 --- /dev/null +++ b/modules/scripts/switch-branch-with-refresh/default.nix @@ -0,0 +1,10 @@ +{ pkgs, ... }: +{ + home.packages = [ + (pkgs.writeShellApplication { + name = "switch-branch-with-refresh"; + runtimeInputs = [ pkgs.git ]; + text = builtins.readFile ./script.sh; + }) + ]; +} diff --git a/modules/scripts/switch-branch-with-refresh/script.sh b/modules/scripts/switch-branch-with-refresh/script.sh new file mode 100755 index 0000000..fb8899d --- /dev/null +++ b/modules/scripts/switch-branch-with-refresh/script.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +if [ -z "${1:-}" ]; then + echo "Usage: switch-branch-with-refresh " >&2 + exit 1 +fi + +current_branch=$(git branch --show-current) + +if [ -z "$current_branch" ]; then + echo "Error: Not on a branch (detached HEAD?)" >&2 + exit 1 +fi + +# 新ブランチを作成(現在のコミットをそのまま引き継ぐ) +if ! git switch -c "$1"; then + echo "Error: Failed to create branch '$1'." >&2 + exit 1 +fi + +# 元ブランチをリモートの状態にリセット +git fetch origin +git switch "$current_branch" +echo "Warning: This will reset '$current_branch' to origin/$current_branch with --hard." +read -r -p "Continue? [y/N] " confirm +if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then + echo "Aborted. Switching back to '$1'." + git switch "$1" + exit 1 +fi +git reset --hard "origin/$current_branch" + +# 新ブランチに移動して完了 +git switch "$1" +echo "Done. Commits are on '$1'. '$current_branch' reset to origin/$current_branch."