シェルスクリプトで以下の実装をしてください: - ./run.sh abcdef3 のようにshort commit hashを引数に取って実行します -…
- User
シェルスクリプトで以下の実装をしてください:
- ./run.sh abcdef3 のようにshort commit hashを引数に取って実行します
- カレントディレクトリが所属するgitレポジトリのリモート含む全てのブランチを最終コミットの日時順にソートして表示します
- それぞれのブランチに引数で指定されたコミットハッシュが存在するかどうかを確認します
- 存在する場合はブランチ名の前に"* "をつけて表示します
- 各ブランチは
最終コミット日時(JST) ブランチ名の形式で表示します - 使用するコマンドはMacOS向けに実装してください
Assistant
以下のシェルスクリプト
run.shを作成します:#!/bin/bash # 引数チェック if [ $# -ne 1 ]; then echo "Usage: $0 <commit_hash>" exit 1 fi commit_hash="$1" # gitレポジトリかどうかチェック if ! git rev-parse --git-dir > /dev/null 2>&1; then echo "Error: Not a git repository" exit 1 fi # 指定されたコミットハッシュが存在するかチェック if ! git rev-parse --quiet --verify "${commit_hash}" > /dev/null 2>&1; then echo "Error: Commit hash '${commit_hash}' not found" exit 1 fi # 一時ファイルを作成 temp_file=$(mktemp) # 全ブランチを取得(ローカル + リモート) git for-each-ref --format='%(refname:short)' refs/heads/ refs/remotes/ | grep -v "HEAD" | while read branch; do # 最終コミットのタイムスタンプを取得 timestamp=$(git log -1 --format="%ct" "$branch" 2>/dev/null) if [ -z "$timestamp" ]; then continue fi # コミットハッシュがこのブランチに存在するかチェック prefix="" if git merge-base --is-ancestor "$commit_hash" "$branch" 2>/dev/null; then prefix="* " fi # MacOSでJST時刻に変換 date_str=$(TZ=JST-9 date -r "$timestamp" "+%Y-%m-%d %H:%M:%S") # 結果を一時ファイルに書き込み echo "${timestamp} ${date_str} ${prefix}${branch}" >> "$temp_file" done # タイムスタンプ順にソートして表示(新しい順) sort -nr "$temp_file" | cut -d' ' -f2- # 一時ファイルを削除 rm -f "$temp_file"このスクリプトの実行手順:
- ファイルに実行権限を付与:
chmod +x run.sh- 実行:
./run.sh abcdef3スクリプトの動作説明:
- 引数として指定されたコミットハッシュの存在確認
git for-each-refでローカルとリモートの全ブランチを取得- 各ブランチの最終コミット日時を Unix タイムスタンプで取得
git merge-base --is-ancestorで指定されたコミットがブランチに含まれているかチェック- MacOS の
dateコマンドとTZ=JST-9を使ってJST時刻に変換 - 最終コミット日時の降順(新しい順)でソートして表示
出力例:
2024-01-15 14:30:25 * origin/main 2024-01-14 16:45:12 * main 2024-01-13 10:20:33 origin/feature-branch 2024-01-12 09:15:44 development
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.