|
1 | 1 | namespace :dojos do |
2 | 2 | desc 'Git履歴からinactivated_at日付を抽出してYAMLファイルに反映(引数でDojo IDを指定可能)' |
3 | 3 | task :extract_inactivated_at_from_git, [:dojo_id] => :environment do |t, args| |
4 | | - require 'git' |
5 | | - |
6 | 4 | yaml_path = Rails.root.join('db', 'dojos.yaml') |
7 | | - git = Git.open(Rails.root) |
8 | 5 |
|
9 | 6 | # YAMLファイルの内容を行番号付きで読み込む |
10 | 7 | yaml_lines = File.readlines(yaml_path) |
@@ -50,23 +47,32 @@ namespace :dojos do |
50 | 47 | if target_line_number |
51 | 48 | # git blame を使って該当行の最新コミット情報を取得 |
52 | 49 | # --porcelain で解析しやすい形式で出力 |
53 | | - blame_cmd = "git blame #{yaml_path} -L #{target_line_number},+1 --porcelain" |
| 50 | + blame_cmd = "git blame #{yaml_path} -L #{target_line_number},+1 --porcelain 2>&1" |
54 | 51 | blame_output = `#{blame_cmd}`.strip |
55 | 52 |
|
| 53 | + # エラーチェック |
| 54 | + if blame_output.include?("fatal:") || blame_output.empty? |
| 55 | + puts " ✗ Git blameエラー: #{blame_output}" |
| 56 | + next |
| 57 | + end |
| 58 | + |
56 | 59 | # コミットIDを抽出(最初の行の最初の要素) |
57 | | - commit_id = blame_output.lines[0].split.first |
| 60 | + commit_id = blame_output.lines[0]&.split&.first |
58 | 61 |
|
59 | 62 | if commit_id && commit_id.match?(/^[0-9a-f]{40}$/) |
60 | 63 | # コミット情報を取得 |
61 | | - commit = git.gcommit(commit_id) |
62 | | - inactivated_date = commit.author_date |
| 64 | + commit_info = `git show --no-patch --format='%at%n%an%n%s' #{commit_id}`.strip.lines |
| 65 | + timestamp = commit_info[0].to_i |
| 66 | + author_name = commit_info[1] |
| 67 | + commit_message = commit_info[2] |
| 68 | + inactivated_date = Time.at(timestamp) |
63 | 69 |
|
64 | 70 | # 特定Dojoモードの場合は情報表示のみ |
65 | 71 | if args[:dojo_id] |
66 | 72 | puts "✓ is_active: false に設定された日時: #{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}" |
67 | 73 | puts " コミット: #{commit_id[0..7]}" |
68 | | - puts " 作者: #{commit.author.name}" |
69 | | - puts " メッセージ: #{commit.message.lines.first.strip}" |
| 74 | + puts " 作者: #{author_name}" |
| 75 | + puts " メッセージ: #{commit_message}" |
70 | 76 | next |
71 | 77 | end |
72 | 78 |
|
@@ -100,7 +106,7 @@ namespace :dojos do |
100 | 106 | if yaml_updated |
101 | 107 | updated_count += 1 |
102 | 108 | puts " ✓ inactivated_at を追加: #{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}" |
103 | | - puts " コミット: #{commit_id[0..7]} by #{commit.author.name}" |
| 109 | + puts " コミット: #{commit_id[0..7]} by #{author_name}" |
104 | 110 | elsif !args[:dojo_id] |
105 | 111 | puts " - スキップ(既に設定済みまたは更新失敗)" |
106 | 112 | end |
|
0 commit comments