|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Git |
| 4 | + module Pkgs |
| 5 | + module Commands |
| 6 | + class Where |
| 7 | + include Output |
| 8 | + |
| 9 | + def initialize(args) |
| 10 | + @args = args |
| 11 | + @options = parse_options |
| 12 | + end |
| 13 | + |
| 14 | + def run |
| 15 | + name = @args.first |
| 16 | + |
| 17 | + error "Usage: git pkgs where <package-name>" unless name |
| 18 | + |
| 19 | + repo = Repository.new |
| 20 | + require_database(repo) |
| 21 | + |
| 22 | + Database.connect(repo.git_dir) |
| 23 | + |
| 24 | + workdir = File.dirname(repo.git_dir) |
| 25 | + branch = Models::Branch.find_by(name: @options[:branch] || repo.default_branch) |
| 26 | + |
| 27 | + unless branch |
| 28 | + error "Branch not found. Run 'git pkgs init' first." |
| 29 | + end |
| 30 | + |
| 31 | + snapshots = Models::DependencySnapshot.current_for_branch(branch) |
| 32 | + snapshots = snapshots.where(ecosystem: @options[:ecosystem]) if @options[:ecosystem] |
| 33 | + |
| 34 | + manifest_paths = snapshots.for_package(name).joins(:manifest).pluck("manifests.path").uniq |
| 35 | + |
| 36 | + if manifest_paths.empty? |
| 37 | + empty_result "Package '#{name}' not found in current dependencies" |
| 38 | + return |
| 39 | + end |
| 40 | + |
| 41 | + results = manifest_paths.flat_map do |path| |
| 42 | + find_in_manifest(name, File.join(workdir, path), path) |
| 43 | + end |
| 44 | + |
| 45 | + if results.empty? |
| 46 | + empty_result "Package '#{name}' tracked but not found in current files" |
| 47 | + return |
| 48 | + end |
| 49 | + |
| 50 | + if @options[:format] == "json" |
| 51 | + output_json(results) |
| 52 | + else |
| 53 | + paginate { output_text(results, name) } |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + def find_in_manifest(name, full_path, display_path) |
| 58 | + return [] unless File.exist?(full_path) |
| 59 | + |
| 60 | + lines = File.readlines(full_path) |
| 61 | + matches = [] |
| 62 | + |
| 63 | + lines.each_with_index do |line, idx| |
| 64 | + next unless line.include?(name) |
| 65 | + |
| 66 | + match = { path: display_path, line: idx + 1, content: line.rstrip } |
| 67 | + |
| 68 | + if context_lines > 0 |
| 69 | + match[:before] = context_before(lines, idx) |
| 70 | + match[:after] = context_after(lines, idx) |
| 71 | + end |
| 72 | + |
| 73 | + matches << match |
| 74 | + end |
| 75 | + |
| 76 | + matches |
| 77 | + end |
| 78 | + |
| 79 | + def context_lines |
| 80 | + @options[:context] || 0 |
| 81 | + end |
| 82 | + |
| 83 | + def context_before(lines, idx) |
| 84 | + start_idx = [0, idx - context_lines].max |
| 85 | + (start_idx...idx).map { |i| { line: i + 1, content: lines[i].rstrip } } |
| 86 | + end |
| 87 | + |
| 88 | + def context_after(lines, idx) |
| 89 | + end_idx = [lines.length - 1, idx + context_lines].min |
| 90 | + ((idx + 1)..end_idx).map { |i| { line: i + 1, content: lines[i].rstrip } } |
| 91 | + end |
| 92 | + |
| 93 | + def output_text(results, name) |
| 94 | + results.each_with_index do |result, i| |
| 95 | + puts "--" if i > 0 && context_lines > 0 |
| 96 | + |
| 97 | + result[:before]&.each do |ctx| |
| 98 | + puts format_context_line(result[:path], ctx[:line], ctx[:content]) |
| 99 | + end |
| 100 | + |
| 101 | + puts format_match_line(result[:path], result[:line], result[:content], name) |
| 102 | + |
| 103 | + result[:after]&.each do |ctx| |
| 104 | + puts format_context_line(result[:path], ctx[:line], ctx[:content]) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def format_match_line(path, line_num, content, name) |
| 110 | + path_str = Color.magenta(path) |
| 111 | + line_str = Color.green(line_num.to_s) |
| 112 | + highlighted = content.gsub(name, Color.red(name)) |
| 113 | + "#{path_str}:#{line_str}:#{highlighted}" |
| 114 | + end |
| 115 | + |
| 116 | + def format_context_line(path, line_num, content) |
| 117 | + path_str = Color.magenta(path) |
| 118 | + line_str = Color.green(line_num.to_s) |
| 119 | + content_str = Color.dim(content) |
| 120 | + "#{path_str}-#{line_str}-#{content_str}" |
| 121 | + end |
| 122 | + |
| 123 | + def output_json(results) |
| 124 | + require "json" |
| 125 | + puts JSON.pretty_generate(results) |
| 126 | + end |
| 127 | + |
| 128 | + def parse_options |
| 129 | + options = {} |
| 130 | + |
| 131 | + parser = OptionParser.new do |opts| |
| 132 | + opts.banner = "Usage: git pkgs where <package-name> [options]" |
| 133 | + |
| 134 | + opts.on("-b", "--branch=NAME", "Branch to search (default: current)") do |v| |
| 135 | + options[:branch] = v |
| 136 | + end |
| 137 | + |
| 138 | + opts.on("-e", "--ecosystem=NAME", "Filter by ecosystem") do |v| |
| 139 | + options[:ecosystem] = v |
| 140 | + end |
| 141 | + |
| 142 | + opts.on("-C", "--context=NUM", Integer, "Show NUM lines of context") do |v| |
| 143 | + options[:context] = v |
| 144 | + end |
| 145 | + |
| 146 | + opts.on("-f", "--format=FORMAT", "Output format (text, json)") do |v| |
| 147 | + options[:format] = v |
| 148 | + end |
| 149 | + |
| 150 | + opts.on("--no-pager", "Do not pipe output into a pager") do |
| 151 | + options[:no_pager] = true |
| 152 | + end |
| 153 | + |
| 154 | + opts.on("-h", "--help", "Show this help") do |
| 155 | + puts opts |
| 156 | + exit |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + parser.parse!(@args) |
| 161 | + options |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments