commit 6af34f3d351bf0d8902d07d5d5f1fb05f9935903 Author: Trevor Vallender Date: Mon Sep 25 11:10:44 2023 +0100 Initial commit diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..ee560cc --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,30 @@ +AllCops: + NewCops: enable + Exclude: + - 'bin/*' + - 'db/migrate/*' + - 'db/schema.rb' + +Style/Documentation: + Enabled: false + +Style/StringLiterals: + EnforcedStyle: double_quotes + +Style/HashSyntax: + Enabled: false + +Style/SymbolArray: + Enabled: false + +Style/TrailingCommaInArguments: + EnforcedStyleForMultiline: consistent_comma + +Style/TrailingCommaInArrayLiteral: + EnforcedStyleForMultiline: consistent_comma + +Style/TrailingCommaInHashLiteral: + EnforcedStyleForMultiline: consistent_comma + +Style/WordArray: + Enabled: false diff --git a/git-ticket.rb b/git-ticket.rb new file mode 100755 index 0000000..ed4acfe --- /dev/null +++ b/git-ticket.rb @@ -0,0 +1,91 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "open3" +require "thor" + +## +# CLI interface to the GitTicket class below +class GitTicketCli < Thor + desc "shas", "show SHAs for a given ticket ref" + def shas(ticket_ref) + git_ticket = GitTicket.new(ticket_ref) + + git_ticket.shas.each { |sha| puts sha } + end + + desc "commits", "show commits for a given ticket ref" + def commits(ticket_ref) + git_ticket = GitTicket.new(ticket_ref) + + git_ticket.commit_messages.each { |message| puts "#{message}\n" } + end + + desc "patches", "show patches for a given ticket ref" + def patches(ticket_ref) + git_ticket = GitTicket.new(ticket_ref) + + git_ticket.patches.each { |patch| puts patch } + 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