Class: GitTreeWalker

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/util/git_tree_walker.rb,
lib/util/git_tree_walker_private.rb

Constant Summary collapse

IGNORED_DIRECTORIES =
['.', '..', '.venv'].freeze

Constants included from Logging

Logging::DEBUG, Logging::NORMAL, Logging::QUIET, Logging::VERBOSE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #log_stdout, verbosity, verbosity=

Constructor Details

#initialize(args = ARGV, options: {}) ⇒ GitTreeWalker

Returns a new instance of GitTreeWalker.



17
18
19
20
21
22
23
24
# File 'lib/util/git_tree_walker.rb', line 17

def initialize(args = ARGV, options: {})
  @options = options
  @root_map = {}
  @display_roots = []
  determine_roots(args)
  @config = GitTree::Config.new
  log Logging::VERBOSE, "GitTreeWalker#initialize: verbosity is #{Logging.verbosity}"
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/util/git_tree_walker.rb', line 13

def config
  @config
end

#display_roots ⇒ Object (readonly)

Returns the value of attribute display_roots.



13
14
15
# File 'lib/util/git_tree_walker.rb', line 13

def display_roots
  @display_roots
end

#root_map ⇒ Object (readonly)

Returns the value of attribute root_map.



13
14
15
# File 'lib/util/git_tree_walker.rb', line 13

def root_map
  @root_map
end

Instance Method Details

#abbreviate_path(dir) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/util/git_tree_walker.rb', line 26

def abbreviate_path(dir)
  @root_map.each do |display_root, expanded_paths|
    expanded_paths.each do |expanded_path|
      return dir.sub(expanded_path, display_root) if dir.start_with?(expanded_path)
    end
  end
  dir # Return original if no match
end

#find_and_process_repos ⇒ Object

Finds git repos and yields them to the block. Does not use thread pool.



66
67
68
69
70
71
72
73
# File 'lib/util/git_tree_walker.rb', line 66

def find_and_process_repos(&)
  visited = Set.new
  @root_map.each do |root_arg, paths|
    paths.sort.each do |root_path|
      find_git_repos_recursive(root_path, visited) { |dir| yield(dir, root_arg) }
    end
  end
end

#process ⇒ Object

Accepts a block



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/util/git_tree_walker.rb', line 35

def process(&) # Accepts a block
  log Logging::VERBOSE, "Processing #{@display_roots.join(' ')}", :green
  if @options[:serial]
    log Logging::VERBOSE, "Running in serial mode.", :yellow
    find_and_process_repos do |dir, _root_arg|
      yield(dir, 0, self) # task, thread_id, walker
    end
  else
    pool = FixedThreadPoolManager.new(0.75)
    # The block passed to pool.start now receives the walker instance (self)
    pool.start do |_worker, dir, thread_id|
      yield(dir, thread_id, self)
    end
    # Run the directory scanning in a separate thread so the main thread can handle interrupts.
    producer_thread = Thread.new do
      find_and_process_repos do |dir, _root_arg|
        pool.add_task(dir)
      end
    end

    # Wait for the producer to finish, then wait for the pool to complete.
    producer_thread.join
    pool.wait_for_completion
  end
rescue Interrupt
  # If interrupted, ensure the pool is shut down and then let the main command handle the exit.
  pool&.shutdown
  raise
end