Module: Ace::Git::Atoms::DateResolver

Defined in:
lib/ace/git/atoms/date_resolver.rb

Overview

Pure functions for resolving dates to git commits Migrated from ace-git-diff

Class Method Summary collapse

Class Method Details

.commit_sha?(ref) ⇒ Boolean

Check if a string looks like a commit SHA

Parameters:

  • ref (String)

    Reference to check

Returns:

  • (Boolean)

    True if looks like a commit SHA



37
38
39
# File 'lib/ace/git/atoms/date_resolver.rb', line 37

def commit_sha?(ref)
  !!(ref =~ /^[0-9a-f]{7,40}$/i)
end

.format_since(since) ⇒ String

Format since parameter for git commands

Parameters:

  • since (String, Date, Time)

    Since parameter

Returns:

  • (String)

    Formatted since string



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ace/git/atoms/date_resolver.rb', line 83

def format_since(since)
  case since
  when Date
    since.strftime("%Y-%m-%d")
  when Time
    since.strftime("%Y-%m-%d")
  when String
    # Try to parse relative time
    parsed = parse_relative_time(since)
    parsed || since
  else
    (Date.today - 7).strftime("%Y-%m-%d")
  end
end

.git_ref?(ref) ⇒ Boolean

Check if a string looks like a git reference (branch, tag, etc)

Parameters:

  • ref (String)

    Reference to check

Returns:

  • (Boolean)

    True if looks like a git ref



44
45
46
47
48
49
50
51
# File 'lib/ace/git/atoms/date_resolver.rb', line 44

def git_ref?(ref)
  # Check for common ref patterns:
  # - refs/heads/*, refs/remotes/*, refs/tags/*
  # - origin/main, upstream/develop, etc
  # - HEAD, HEAD~1, HEAD^, etc
  !!(ref =~ %r{^(refs/|origin/|upstream/|HEAD)}) ||
    !!(ref =~ /^[A-Za-z][A-Za-z0-9_\-\/]*$/) # Branch or tag name
end

.parse_relative_time(time_str) ⇒ String?

Parse relative time strings (e.g., “7d”, “1 week ago”, “2 months”)

Parameters:

  • time_str (String)

    Time string to parse

Returns:

  • (String, nil)

    ISO date string or nil if can’t parse



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ace/git/atoms/date_resolver.rb', line 56

def parse_relative_time(time_str)
  case time_str
  when /^(\d+)d$/ # e.g., "7d"
    days = Regexp.last_match(1).to_i
    (Date.today - days).strftime("%Y-%m-%d")
  when /^(\d+)\s*days?\s*ago$/i # e.g., "7 days ago"
    days = Regexp.last_match(1).to_i
    (Date.today - days).strftime("%Y-%m-%d")
  when /^(\d+)\s*weeks?\s*ago$/i # e.g., "1 week ago"
    weeks = Regexp.last_match(1).to_i
    (Date.today - (weeks * 7)).strftime("%Y-%m-%d")
  when /^(\d+)\s*months?\s*ago$/i # e.g., "2 months ago"
    months = Regexp.last_match(1).to_i
    (Date.today << months).strftime("%Y-%m-%d")
  else
    # Try to parse as date string
    begin
      Date.parse(time_str).strftime("%Y-%m-%d")
    rescue ArgumentError
      nil
    end
  end
end

.resolve_since_to_commit(since, executor: CommandExecutor) ⇒ String

Resolve a since parameter to a git commit reference

Parameters:

  • since (String, Date)

    Date string, relative time, or commit SHA

  • executor (Module) (defaults to: CommandExecutor)

    Command executor module (default: CommandExecutor)

Returns:

  • (String)

    Git commit reference



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ace/git/atoms/date_resolver.rb', line 16

def resolve_since_to_commit(since, executor: CommandExecutor)
  return "HEAD" if since.nil? || since.empty?

  # If it looks like a commit SHA, use as-is
  return since if commit_sha?(since)

  # If it looks like a git ref (branch, tag), use as-is
  return since if git_ref?(since)

  # It's a date or relative time - find the first commit since that date
  first_commit = find_first_commit_since(since, executor)
  return since unless first_commit # Fallback to date string

  # Get parent of first commit to include all changes since date
  parent = get_parent_commit(first_commit, executor)
  parent || first_commit
end