Created
April 9, 2025 02:01
-
-
Save barelyknown/d449db2fd22805936423f3101006d527 to your computer and use it in GitHub Desktop.
To find the most recent files in a directory, run: ./scripts/list_recent_files.rb [directory_path] or specify the number with -n [count].
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'json' | |
require 'time' | |
require 'open3' | |
require 'fileutils' | |
require 'optparse' | |
# Parse command line options | |
options = { limit: 5 } | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{File.basename(__FILE__)} [options] DIRECTORY" | |
opts.on("-n", "--limit NUMBER", Integer, "Number of files to show (default: 5)") do |n| | |
options[:limit] = n | |
end | |
opts.on("-h", "--help", "Show this help message") do | |
puts opts | |
exit | |
end | |
end.parse! | |
target_dir = ARGV[0] | |
if target_dir.nil? || target_dir.empty? | |
puts "Error: Please specify a directory" | |
exit 1 | |
end | |
target_dir = File.expand_path(target_dir) | |
unless File.directory?(target_dir) | |
puts "Error: '#{target_dir}' is not a directory" | |
exit 1 | |
end | |
# Convert to relative path within the repo | |
git_root_cmd = "git -C #{target_dir} rev-parse --show-toplevel" | |
git_root, _stderr, status = Open3.capture3(git_root_cmd) | |
unless status.success? | |
puts "Error: #{target_dir} is not within a git repository" | |
exit 1 | |
end | |
git_root = git_root.strip | |
relative_path = target_dir.sub("#{git_root}/", '') | |
relative_path = '.' if relative_path == git_root || relative_path.empty? | |
# Find all files in the directory with creation timestamps | |
Dir.chdir(git_root) do | |
# Get all files with their creation dates using git | |
files_with_timestamps = {} | |
# This command gets when each file in the target directory was first added | |
git_cmd = "git log --diff-filter=A --format='%at %H' --name-status -- #{relative_path}" | |
stdout, stderr, status = Open3.capture3(git_cmd) | |
unless status.success? | |
warn "Error running git log: #{stderr}" | |
exit 1 | |
end | |
current_timestamp = nil | |
stdout.each_line do |line| | |
line.strip! | |
next if line.empty? | |
# Timestamp line looks like: 1617304800 abcdef12345... | |
if line.match?(/^\d+ [a-f0-9]+$/) | |
timestamp_str, _commit_hash = line.split(' ', 2) | |
current_timestamp = timestamp_str.to_i | |
# Added file line looks like: A path/to/file.rb | |
elsif line.start_with?('A') && current_timestamp | |
_, file_path = line.split(/\s+/, 2) | |
# Only include files directly in the target directory | |
file_dir = File.dirname(file_path) | |
if file_dir == relative_path || (relative_path == '.' && file_dir == '') | |
# Only record the first time we see a file (earliest creation) | |
files_with_timestamps[file_path] ||= current_timestamp | |
end | |
end | |
end | |
# Handle untracked files in the directory | |
git_cmd = "git ls-files --others --exclude-standard #{relative_path}" | |
stdout, stderr, status = Open3.capture3(git_cmd) | |
unless status.success? | |
warn "Error listing untracked files: #{stderr}" | |
exit 1 | |
end | |
current_time = Time.now.to_i | |
stdout.each_line do |line| | |
file_path = line.strip | |
next if file_path.empty? | |
# Only include files directly in the target directory | |
file_dir = File.dirname(file_path) | |
if file_dir == relative_path || (relative_path == '.' && file_dir == '') | |
files_with_timestamps[file_path] = current_time | |
end | |
end | |
# Sort by timestamp (newest first) and take top N | |
sorted_files = files_with_timestamps.sort_by { |_path, timestamp| -timestamp } | |
# Output results | |
if sorted_files.empty? | |
puts "No files found in #{target_dir}" | |
else | |
# Just print the relative paths without timestamps | |
sorted_files.first(options[:limit]).each do |path, _timestamp| | |
puts path | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment