From 7cf528700f4d2b3be992fd820670cda880c03c15 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Thu, 2 Nov 2023 17:53:54 +0000 Subject: [PATCH] Refactor to separate files --- git-ticket.rb | 60 +---------------------------------------------- lib/git_ticket.rb | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 59 deletions(-) create mode 100644 lib/git_ticket.rb diff --git a/git-ticket.rb b/git-ticket.rb index ed4acfe..1595834 100755 --- a/git-ticket.rb +++ b/git-ticket.rb @@ -3,6 +3,7 @@ require "open3" require "thor" +require_relative "./lib/git_ticket" ## # CLI interface to the GitTicket class below @@ -29,63 +30,4 @@ class GitTicketCli < Thor end end -## -# Provides an interface to the git commits related to a specific ticket -class GitTicket - attr_reader :ticket - attr_accessor :git_command - - ## - # Creates a new diff from all commits referencing a given ticket. - # - # @param [String] ticket_reference The ticket reference to use - # @param [String] diff_target The git reference to diff against. - def initialize(ticket_reference) - @ticket = ticket_reference - @git_command = "git -c color.ui=always" # So our output is colourised - end - - ## - # Sets and returns the commit messages for each commit - # @return [Array] The commit messages - def commit_messages - return @commit_messages if @commit_messages - - @commit_messages = [] - shas.each do |sha| - message, _status = Open3.capture2("#{git_command} show #{sha} -s") - @commit_messages.push(message) - end - - @commit_messages - end - - ## - # Sets and returns the patches for each commit. - # - # @return [Array] The diffs - def patches - return @patches if @patches - - @patches = [] - shas.each do |sha| - patch, _status = Open3.capture2("#{git_command} show #{sha} --patch") - @patches.push(patch) - end - - @patches - end - ## - - # Sets and returns the commit SHAs referenced. - # - # @return [Array] The commit SHAs - def shas - return @shas if @shas - - @shas, _status = Open3.capture2("#{git_command} log --all --grep #{@ticket} --format=%H") - @shas = @shas.split("\n") - end -end - GitTicketCli.start diff --git a/lib/git_ticket.rb b/lib/git_ticket.rb new file mode 100644 index 0000000..d387f42 --- /dev/null +++ b/lib/git_ticket.rb @@ -0,0 +1,59 @@ +## +# Provides an interface to the git commits related to a specific ticket +class GitTicket + attr_reader :ticket + attr_accessor :git_command + + ## + # Creates a new diff from all commits referencing a given ticket. + # + # @param [String] ticket_reference The ticket reference to use + # @param [String] diff_target The git reference to diff against. + def initialize(ticket_reference) + @ticket = ticket_reference + @git_command = "git -c color.ui=always" # So our output is colourised + end + + ## + # Sets and returns the commit messages for each commit + # @return [Array] The commit messages + def commit_messages + return @commit_messages if @commit_messages + + @commit_messages = [] + shas.each do |sha| + message, _status = Open3.capture2("#{git_command} show #{sha} -s") + @commit_messages.push(message) + end + + @commit_messages + end + + ## + # Sets and returns the patches for each commit. + # + # @return [Array] The diffs + def patches + return @patches if @patches + + @patches = [] + shas.each do |sha| + patch, _status = Open3.capture2("#{git_command} show #{sha} --patch") + @patches.push(patch) + end + + @patches + end + ## + + # Sets and returns the commit SHAs referenced. + # + # @return [Array] The commit SHAs + def shas + return @shas if @shas + + @shas, _status = Open3.capture2("#{git_command} log --all --grep #{@ticket} --format=%H") + @shas = @shas.split("\n") + end +end +