Skip to content

Instantly share code, notes, and snippets.

@andycandrea
Created September 29, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andycandrea/bba8ed82ea85389a6554fa530d5c5c60 to your computer and use it in GitHub Desktop.
Save andycandrea/bba8ed82ea85389a6554fa530d5c5c60 to your computer and use it in GitHub Desktop.
Compares the git log of two branches to find tasks represented in one but not the other
# Very rough draft
# TODO: Clean up, extract into multiple objects and then convert to a
# gem/executable.
class TaskFinder
def initialize(outdated_branch, updated_branch)
@outdated_branch = outdated_branch
@updated_branch = updated_branch
end
def updated_tasks_and_branches
all_identifiers = task_number_messages + octothorpe_messages + unmerged_branches
all_identifiers.uniq.sort_by { |identifier| [identifier.to_i, identifier.to_s] |
end
# Looks through commit messages that include the word "Task" and attempts
# to extract a task number from those messages.
private def task_number_messages
messages = find_commit_messages('task')
messages.reject! { |message| message.downcase.include?('merge') }
messages.map! { |message| message[/\d+/] }
messages.reject!(&:nil?)
messages.uniq
end
# Looks through commit messages containing an octothorpe/pound
# sign/hashtag/"#" and attempts to extract a task number from them.
private def octothorpe_messages
messages = find_commit_messages('\#')
messages.map! { |message| message[/#\d+/] }
messages.reject!(&:nil?)
messages.map! { |message| message.delete('#') }
messages.uniq
end
# Looks through merge commits (excluding those marked as merged to or from the
# outdated_branch and those involving a remote branch) and extracts the origin
# branch name. When the branch name includes 'task' or 'hotfix', it attempts
# to extract a task number from the branch name and returns that instead.
private def unmerged_branches
messages = find_commit_messages('Merge branch')
messages.reject! do |message|
/Merge branch '.+' of .+ into .+/ === message || message.include?(outdated_branch)
end
messages.map! do |message|
branch_name = message[/(?<=Merge branch ').+(?=')/].to_s
task_number = ''
if branch_name.downcase.include?('task') || branch_name.downcase.include?('hotfix')
task_number = branch_name[/\d+/].to_s
end
if task_number.length > 0
task_number
else
branch_name
end
end
messages.uniq
end
private def find_commit_messages(grep_criterion)
`#{log_command} | grep -i #{grep_criterion}`.split("\n").map(&:strip).freeze
end
private def log_command
"git log #{outdated_branch}..#{updated_branch}".freeze
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment