Class: GitCommitMailer

Inherits:
Object
  • Object
show all
Defined in:
lib/git-commit-mailer/info.rb,
lib/git-commit-mailer.rb,
lib/git-commit-mailer/version.rb,
lib/git-commit-mailer/file-diff.rb,
lib/git-commit-mailer/push-info.rb,
lib/git-commit-mailer/commit-info.rb,
lib/git-commit-mailer/mail-body-formatter.rb,
lib/git-commit-mailer/html-mail-body-formatter.rb,
lib/git-commit-mailer/text-mail-body-formatter.rb

Overview

Copyright © 2009 Ryo Onodera <[email protected]> Copyright © 2012-2014 Kouhei Sutou <[email protected]>

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Classes: CommitInfo, FileDiff, HTMLMailBodyFormatter, Info, MailBodyFormatter, NoParentCommit, PushInfo, TextMailBodyFormatter

Constant Summary collapse

KILO_SIZE =
1000
DEFAULT_MAX_SIZE =
"100M"
VERSION =
"1.0.4"
URL =
"https://github.com/clear-code/git-commit-mailer"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to) ⇒ GitCommitMailer



523
524
525
# File 'lib/git-commit-mailer.rb', line 523

def initialize(to)
  @to = to
end

Instance Attribute Details

#add_diff=(value) ⇒ Object (writeonly)

Sets the attribute add_diff



513
514
515
# File 'lib/git-commit-mailer.rb', line 513

def add_diff=(value)
  @add_diff = value
end

#add_html=(value) ⇒ Object (writeonly)

Sets the attribute add_html



513
514
515
# File 'lib/git-commit-mailer.rb', line 513

def add_html=(value)
  @add_html = value
end

#dateObject



583
584
585
# File 'lib/git-commit-mailer.rb', line 583

def date
  @date || Time.now
end

#from(info) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
# File 'lib/git-commit-mailer.rb', line 555

def from(info)
  if @from
    if /\A[^\s<]+@[^\s>]\z/ =~ @from
      @from
    else
      "#{format_name(info.author_name)} <#{@from}>"
    end
  else
    "#{format_name(info.author_name)} <#{info.author_email}>"
  end
end

#from_domainObject

Returns the value of attribute from_domain.



515
516
517
# File 'lib/git-commit-mailer.rb', line 515

def from_domain
  @from_domain
end

#git_bin_pathObject



587
588
589
# File 'lib/git-commit-mailer.rb', line 587

def git_bin_path
  ENV['GIT_BIN_PATH'] || @git_bin_path
end

#github_base_urlObject

Returns the value of attribute github_base_url.



518
519
520
# File 'lib/git-commit-mailer.rb', line 518

def github_base_url
  @github_base_url
end

#github_repositoryObject

Returns the value of attribute github_repository.



518
519
520
# File 'lib/git-commit-mailer.rb', line 518

def github_repository
  @github_repository
end

#github_userObject

Returns the value of attribute github_user.



518
519
520
# File 'lib/git-commit-mailer.rb', line 518

def github_user
  @github_user
end

#gitlab_project_uriObject

Returns the value of attribute gitlab_project_uri.



519
520
521
# File 'lib/git-commit-mailer.rb', line 519

def gitlab_project_uri
  @gitlab_project_uri
end

#max_diff_sizeObject

Returns the value of attribute max_diff_size.



515
516
517
# File 'lib/git-commit-mailer.rb', line 515

def max_diff_size
  @max_diff_size
end

#max_sizeObject

Returns the value of attribute max_size.



515
516
517
# File 'lib/git-commit-mailer.rb', line 515

def max_size
  @max_size
end

#name=(value) ⇒ Object

Sets the attribute name



520
521
522
# File 'lib/git-commit-mailer.rb', line 520

def name=(value)
  @name = value
end

#new_revisionObject (readonly)

Returns the value of attribute new_revision.



511
512
513
# File 'lib/git-commit-mailer.rb', line 511

def new_revision
  @new_revision
end

#old_revisionObject (readonly)

Returns the value of attribute old_revision.



511
512
513
# File 'lib/git-commit-mailer.rb', line 511

def old_revision
  @old_revision
end

#portObject

Returns the value of attribute port.



516
517
518
# File 'lib/git-commit-mailer.rb', line 516

def port
  @port
end

#referenceObject (readonly)

Returns the value of attribute reference.



511
512
513
# File 'lib/git-commit-mailer.rb', line 511

def reference
  @reference
end

#repositoryObject



579
580
581
# File 'lib/git-commit-mailer.rb', line 579

def repository
  @repository || Dir.pwd
end

#repository_browserObject

Returns the value of attribute repository_browser.



517
518
519
# File 'lib/git-commit-mailer.rb', line 517

def repository_browser
  @repository_browser
end

#repository_uriObject

Returns the value of attribute repository_uri.



515
516
517
# File 'lib/git-commit-mailer.rb', line 515

def repository_uri
  @repository_uri
end

#rss_pathObject

Returns the value of attribute rss_path.



516
517
518
# File 'lib/git-commit-mailer.rb', line 516

def rss_path
  @rss_path
end

#rss_uriObject

Returns the value of attribute rss_uri.



516
517
518
# File 'lib/git-commit-mailer.rb', line 516

def rss_uri
  @rss_uri
end

#send_per_to=(value) ⇒ Object (writeonly)

Sets the attribute send_per_to



512
513
514
# File 'lib/git-commit-mailer.rb', line 512

def send_per_to=(value)
  @send_per_to = value
end

#send_push_mail=(value) ⇒ Object (writeonly)

Sets the attribute send_push_mail



513
514
515
# File 'lib/git-commit-mailer.rb', line 513

def send_push_mail=(value)
  @send_push_mail = value
end

#senderObject

Returns the value of attribute sender.



515
516
517
# File 'lib/git-commit-mailer.rb', line 515

def sender
  @sender
end

#serverObject

Returns the value of attribute server.



516
517
518
# File 'lib/git-commit-mailer.rb', line 516

def server
  @server
end

#show_path=(value) ⇒ Object (writeonly)

Sets the attribute show_path



513
514
515
# File 'lib/git-commit-mailer.rb', line 513

def show_path=(value)
  @show_path = value
end

#sleep_per_mailObject

Returns the value of attribute sleep_per_mail.



521
522
523
# File 'lib/git-commit-mailer.rb', line 521

def sleep_per_mail
  @sleep_per_mail
end

#toObject (readonly)

Returns the value of attribute to.



511
512
513
# File 'lib/git-commit-mailer.rb', line 511

def to
  @to
end

#track_remote=(value) ⇒ Object (writeonly)

Sets the attribute track_remote



514
515
516
# File 'lib/git-commit-mailer.rb', line 514

def track_remote=(value)
  @track_remote = value
end

#verbose=(value) ⇒ Object (writeonly)

Sets the attribute verbose



520
521
522
# File 'lib/git-commit-mailer.rb', line 520

def verbose=(value)
  @verbose = value
end

Class Method Details

.execute(command, working_directory = nil, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/git-commit-mailer.rb', line 63

def execute(command, working_directory=nil, &block)
  if ENV["DEBUG"]
    suppress_stderr = ""
  else
    suppress_stderr = " 2> /dev/null"
  end

  script = "#{command} #{suppress_stderr}"
  puts script if ENV["DEBUG"]
  result = nil
  with_working_direcotry(working_directory) do
    if block_given?
      IO.popen(script, "w+", &block)
    else
      result = `#{script} 2>&1`
    end
  end
  raise "execute failed: #{command}\n#{result}" unless $?.exitstatus.zero?
  result.force_encoding("UTF-8") if result.respond_to?(:force_encoding)
  result
end

.extract_email_address(address) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/git-commit-mailer.rb', line 117

def extract_email_address(address)
  if /<(.+?)>/ =~ address
    $1
  else
    address
  end
end

.extract_email_address_from_mail(mail) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/git-commit-mailer.rb', line 125

def extract_email_address_from_mail(mail)
  begin
    from_header = mail.lines.grep(/\AFrom: .*\Z/)[0]
    extract_email_address(from_header.rstrip.sub(/From: /, ""))
  rescue
    raise '"From:" header is not found in mail.'
  end
end

.extract_to_addresses(mail) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/git-commit-mailer.rb', line 134

def extract_to_addresses(mail)
  to_value = nil
  if /^To:(.*\r?\n(?:^\s+.*)*)/ni =~ mail
    to_value = $1
  else
    raise "'To:' header is not found in mail:\n#{mail}"
  end
  to_value_without_comment = to_value.gsub(/".*?"/n, "")
  to_value_without_comment.split(/\s*,\s*/n).collect do |address|
    extract_email_address(address.strip)
  end
end

.format_size(size) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/git-commit-mailer.rb', line 178

def format_size(size)
  return "no limit" if size.nil?
  return "#{size}B" if size < KILO_SIZE
  size /= KILO_SIZE.to_f
  return "#{size}KB" if size < KILO_SIZE
  size /= KILO_SIZE.to_f
  return "#{size}MB" if size < KILO_SIZE
  size /= KILO_SIZE.to_f
  "#{size}GB"
end

.git(git_bin_path, repository, command, &block) ⇒ Object



106
107
108
109
110
111
# File 'lib/git-commit-mailer.rb', line 106

def git(git_bin_path, repository, command, &block)
  $executing_git ||= SpentTime.new("executing git commands")
  $executing_git.spend do
    execute("#{git_bin_path} --git-dir=#{shell_escape(repository)} #{command}", &block)
  end
end

.parse(argv) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/git-commit-mailer.rb', line 167

def parse(argv)
  options = make_options

  parser = make_parser(options)
  argv = argv.dup
  parser.parse!(argv)
  to = argv

  [to, options]
end

.parse_options_and_create(argv = nil) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/git-commit-mailer.rb', line 158

def parse_options_and_create(argv=nil)
  argv ||= ARGV
  to, options = parse(argv)
  to += options.to
  mailer = new(to.compact)
  apply_options(mailer, options)
  mailer
end

.send_mail(server, port, from, to, mail) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/git-commit-mailer.rb', line 147

def send_mail(server, port, from, to, mail)
  $sending_mail ||= SpentTime.new("sending mails")
  $sending_mail.spend do
    Net::SMTP.start(server, port) do |smtp|
      smtp.open_message_stream(from, to) do |f|
        f.print(mail)
      end
    end
  end
end

.shell_escape(string) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/git-commit-mailer.rb', line 95

def shell_escape(string)
  # To suppress warnings from Shellwords::escape.
  if string.respond_to? :force_encoding
    bytes = string.dup.force_encoding("ascii-8bit")
  else
    bytes = string
  end

  Shellwords.escape(bytes)
end

.short_revision(revision) ⇒ Object



113
114
115
# File 'lib/git-commit-mailer.rb', line 113

def short_revision(revision)
  revision[0, 7]
end

.with_working_direcotry(working_directory) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/git-commit-mailer.rb', line 85

def with_working_direcotry(working_directory)
  if working_directory
    Dir.chdir(working_directory) do
      yield
    end
  else
    yield
  end
end

.x_mailerObject



59
60
61
# File 'lib/git-commit-mailer.rb', line 59

def x_mailer
  "#{name} #{VERSION}; #{URL}"
end

Instance Method Details

#add_diff?Boolean



1152
1153
1154
# File 'lib/git-commit-mailer.rb', line 1152

def add_diff?
  @add_diff
end

#add_html?Boolean



1156
1157
1158
# File 'lib/git-commit-mailer.rb', line 1156

def add_html?
  @add_html
end

#annotated_tag_contentObject



948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'lib/git-commit-mailer.rb', line 948

def annotated_tag_content
  message = ''
  tagger = git("for-each-ref --format='%(taggername)' #{@reference}").strip
  tagged = git("for-each-ref --format='%(taggerdate:rfc2822)' #{@reference}").strip
  message << " tagged by  #{tagger}\n"
  message << "        on  #{format_time(Time.rfc2822(tagged))}\n\n"

  # Show the content of the tag message; this might contain a change
  # log or release notes so is worth displaying.
  tag_content = git("cat-file tag #{@new_revision}").split("\n")
  #skips header section
  tag_content.shift while not tag_content.first.empty?
  #skips the empty line indicating the end of header section
  tag_content.shift

  message << tag_content.join("\n") + "\n"
  message
end

#collect_new_commitsObject



845
846
847
848
849
850
851
852
# File 'lib/git-commit-mailer.rb', line 845

def collect_new_commits
  commits = []
  git("rev-list #{@old_revision}..#{@new_revision} #{excluded_revisions}").lines.
  reverse_each do |revision|
    commits << revision.strip
  end
  commits
end

#collect_push_informationObject



708
709
710
711
712
713
714
715
# File 'lib/git-commit-mailer.rb', line 708

def collect_push_information
  change_type = detect_change_type
  revision_type = detect_revision_type(change_type)
  reference_type = detect_reference_type(revision_type)
  messsage, commits = make_push_message(reference_type, change_type)

  [reference_type, change_type, messsage, commits]
end

#create_commit_info(*args) ⇒ Object



531
532
533
# File 'lib/git-commit-mailer.rb', line 531

def create_commit_info(*args)
  CommitInfo.new(self, *args)
end

#create_push_info(*args) ⇒ Object



527
528
529
# File 'lib/git-commit-mailer.rb', line 527

def create_push_info(*args)
  PushInfo.new(self, *args)
end

#delete_tagsObject



617
618
619
620
621
622
# File 'lib/git-commit-mailer.rb', line 617

def delete_tags
  git("rev-parse --symbolic --tags").lines.each do |reference|
    reference.rstrip!
    git("tag -d %s" % GitCommitMailer.shell_escape(reference))
  end
end

#detect_change_typeObject



647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/git-commit-mailer.rb', line 647

def detect_change_type
  if old_revision =~ /0{40}/ and new_revision =~ /0{40}/
    raise "Invalid revision hash"
  elsif old_revision !~ /0{40}/ and new_revision !~ /0{40}/
    :update
  elsif old_revision =~ /0{40}/
    :create
  elsif new_revision =~ /0{40}/
    :delete
  else
    raise "Invalid revision hash"
  end
end

#detect_object_type(object_name) ⇒ Object



661
662
663
# File 'lib/git-commit-mailer.rb', line 661

def detect_object_type(object_name)
  git("cat-file -t #{object_name}").strip
end

#detect_reference_type(revision_type) ⇒ Object



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/git-commit-mailer.rb', line 674

def detect_reference_type(revision_type)
  if reference =~ /refs\/tags\/.*/ and revision_type == "commit"
    :unannotated_tag
  elsif reference =~ /refs\/tags\/.*/ and revision_type == "tag"
    # change recipients
    #if [ -n "$announcerecipients" ]; then
    #  recipients="$announcerecipients"
    #fi
    :annotated_tag
  elsif reference =~ /refs\/(heads|remotes\/origin)\/.*/ and revision_type == "commit"
    :branch
  elsif reference =~ /refs\/remotes\/.*/ and revision_type == "commit"
    # tracking branch
    # Push-update of tracking branch.
    # no email generated.
    throw :no_email
  else
    # Anything else (is there anything else?)
    raise "Unknown type of update to #@reference (#{revision_type})"
  end
end

#detect_revision_type(change_type) ⇒ Object



665
666
667
668
669
670
671
672
# File 'lib/git-commit-mailer.rb', line 665

def detect_revision_type(change_type)
  case change_type
  when :create, :update
    detect_object_type(new_revision)
  when :delete
    detect_object_type(old_revision)
  end
end

#determine_prominent_authorObject



1074
1075
1076
1077
1078
1079
# File 'lib/git-commit-mailer.rb', line 1074

def determine_prominent_author
  #if @commit_infos.length > 0
  #
  #else
  #   @push_info
end

#excluded_revisionsObject



717
718
719
720
721
722
723
724
725
726
727
# File 'lib/git-commit-mailer.rb', line 717

def excluded_revisions
   # refer to the long comment located at the top of this file for the
   # explanation of this command.
   current_reference_revision = git("rev-parse #@reference").strip
   git("rev-parse --not --branches --remotes").lines.find_all do |line|
     line.strip!
     not line.index(current_reference_revision)
   end.collect do |line|
     GitCommitMailer.shell_escape(line)
   end.join(' ')
end

#explain_rewindObject



750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/git-commit-mailer.rb', line 750

def explain_rewind
"This update discarded existing revisions and left the branch pointing at\na previous point in the repository history.\n\n * -- * -- N (\#{short_new_revision})\n          \\\\\n           O <- O <- O (\#{short_old_revision})\n\nThe removed revisions are not necessarilly gone - if another reference\nstill refers to them they will stay in the repository.\n"
end

#explain_rewind_and_new_commitsObject



764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/git-commit-mailer.rb', line 764

def explain_rewind_and_new_commits
"This update added new revisions after undoing existing revisions.  That is\nto say, the old revision is not a strict subset of the new revision.  This\nsituation occurs when you --force push a change and generate a repository\ncontaining something like this:\n\n * -- * -- B <- O <- O <- O (\#{short_old_revision})\n          \\\\\n           N -> N -> N (\#{short_new_revision})\n\nWhen this happens we assume that you've already had alert emails for all\nof the O revisions, and so we here report only the revisions in the N\nbranch from the common base, B.\n"
end

#explain_special_caseObject



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/git-commit-mailer.rb', line 821

def explain_special_case
  #  1. Existing revisions were removed.  In this case new_revision
  #     is a subset of old_revision - this is the reverse of a
  #     fast-forward, a rewind
  #  2. New revisions were added on top of an old revision,
  #     this is a rewind and addition.

  # (1) certainly happened, (2) possibly.  When (2) hasn't
  # happened, we set a flag to indicate that no log printout
  # is required.

  # Find the common ancestor of the old and new revisions and
  # compare it with new_revision
  baserev = git("merge-base #{@old_revision} #{@new_revision}").strip
  rewind_only = false
  if baserev == new_revision
    explanation = explain_rewind
    rewind_only = true
  else
    explanation = explain_rewind_and_new_commits
  end
  [rewind_only, explanation]
end

#fetchObject



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/git-commit-mailer.rb', line 624

def fetch
  updated_references = []
  old_references = origin_references
  delete_tags
  git("fetch --force --tags")
  git("fetch --force")
  new_references = origin_references

  old_references.each do |reference, revision|
    if revision != new_references[reference]
      updated_references << [revision, new_references[reference], reference]
    end
  end
  new_references.each do |reference, revision|
    if revision != old_references[reference]#.sub(/remotes\/origin/, 'heads')
      updated_references << [old_references[reference], revision, reference]
    end
  end
  updated_references.sort do |reference_change1, reference_change2|
    reference_change1.last <=> reference_change2.last
  end.uniq
end

#find_branch_name_from_its_descendant_revision(revision) ⇒ Object



1019
1020
1021
1022
1023
1024
1025
# File 'lib/git-commit-mailer.rb', line 1019

def find_branch_name_from_its_descendant_revision(revision)
  begin
    name = git("name-rev --name-only --refs refs/heads/* #{revision}").strip
    revision = parent_commit(revision)
  end until name.sub(/([~^][0-9]+)*\z/, '') == name
  name
end

#format_name(name) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
# File 'lib/git-commit-mailer.rb', line 567

def format_name(name)
  case name
  when /[,"\\]/
    escaped_name = name.gsub(/["\\]/) do |special_character|
      "\\#{special_character}"
    end
    "\"#{escaped_name}\""
  else
    name
  end
end

#format_time(time) ⇒ Object



1168
1169
1170
# File 'lib/git-commit-mailer.rb', line 1168

def format_time(time)
  time.strftime('%Y-%m-%d %X %z (%a, %d %b %Y)')
end

#get_record(revision, record) ⇒ Object



539
540
541
# File 'lib/git-commit-mailer.rb', line 539

def get_record(revision, record)
  get_records(revision, [record]).first
end

#get_records(revision, records) ⇒ Object



543
544
545
546
547
548
549
# File 'lib/git-commit-mailer.rb', line 543

def get_records(revision, records)
  GitCommitMailer.git(git_bin_path, @repository,
                      "log -n 1 --pretty=format:'#{records.join('%n')}%n' " +
                      "#{revision}").lines.collect do |line|
    line.strip
  end
end

#git(command, &block) ⇒ Object



535
536
537
# File 'lib/git-commit-mailer.rb', line 535

def git(command, &block)
  GitCommitMailer.git(git_bin_path, @repository, command, &block)
end

#make_infosObject



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/git-commit-mailer.rb', line 1091

def make_infos
  catch(:no_email) do
    @push_info = create_push_info(old_revision, new_revision, reference,
                                  *collect_push_information)
    if @push_info.branch_changed?
      @push_info.commits.each do |revision|
        commit_info = create_commit_info(reference, revision)
        @commit_infos << commit_info
        @commit_info_map[revision] = commit_info
      end
    end
  end

  post_process_infos
end

#make_mailsObject



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/git-commit-mailer.rb', line 1107

def make_mails
  if send_per_to?
    @push_mails = @to.collect do |to|
      make_mail(@push_info, [to])
    end
  else
    @push_mails = [make_mail(@push_info, @to)]
  end

  @commit_mails = []
  @commit_infos.each do |info|
    if send_per_to?
      @to.each do |to|
        @commit_mails << make_mail(info, [to])
      end
    else
      @commit_mails << make_mail(info, @to)
    end
  end
end

#make_push_message(reference_type, change_type) ⇒ Object



696
697
698
699
700
701
702
703
704
705
706
# File 'lib/git-commit-mailer.rb', line 696

def make_push_message(reference_type, change_type)
  unless [:branch, :annotated_tag, :unannotated_tag].include?(reference_type)
    raise "unexpected reference_type"
  end
  unless [:update, :create, :delete].include?(change_type)
    raise "unexpected change_type"
  end

  method_name = "process_#{change_type}_#{reference_type}"
  __send__(method_name)
end

#origin_referencesObject



607
608
609
610
611
612
613
614
615
# File 'lib/git-commit-mailer.rb', line 607

def origin_references
  references = Hash.new("0" * 40)
  git("rev-parse --symbolic-full-name --tags --remotes").lines.each do |reference|
    reference.rstrip!
    next if reference =~ %r!\Arefs/remotes! and reference !~ %r!\Arefs/remotes/origin!
    references[reference] = git("rev-parse %s" % GitCommitMailer.shell_escape(reference)).rstrip
  end
  references
end

#parent_commit(revision) ⇒ Object



930
931
932
933
934
935
936
# File 'lib/git-commit-mailer.rb', line 930

def parent_commit(revision)
  begin
    git("rev-parse #{revision}^").strip
  rescue
    raise NoParentCommit
  end
end

#post_process_infosObject



1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/git-commit-mailer.rb', line 1064

def post_process_infos
  # @push_info.author_name = determine_prominent_author
  commit_infos = @commit_infos.dup
  # @commit_infos may be altered and I don't know any sensible behavior of ruby
  # in such cases. Take the safety measure at the moment...
  commit_infos.reverse_each do |commit_info|
    traverse_merge_commit(commit_info) if commit_info.merge?
  end
end

#previous_tag_by_revision(revision) ⇒ Object



938
939
940
941
942
943
944
945
946
# File 'lib/git-commit-mailer.rb', line 938

def previous_tag_by_revision(revision)
  # If the tagged object is a commit, then we assume this is a
  # release, and so we calculate which tag this tag is
  # replacing
  begin
    git("describe --abbrev=0 #{parent_commit(revision)}").strip
  rescue NoParentCommit
  end
end

#process_annotated_tagObject



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# File 'lib/git-commit-mailer.rb', line 967

def process_annotated_tag
  message = ''
  # Use git for-each-ref to pull out the individual fields from the tag
  tag_object = git("for-each-ref --format='%(*objectname)' #{@reference}").strip
  tag_type = git("for-each-ref --format='%(*objecttype)' #{@reference}").strip

  case tag_type
  when "commit"
    message << "   tagging  #{tag_object} (#{tag_type})\n"
    previous_tag = previous_tag_by_revision(@new_revision)
    message << "  replaces  #{previous_tag}\n" if previous_tag
    message << annotated_tag_content
    message << short_log_from_previous_tag(previous_tag)
  else
    message << "   tagging  #{tag_object} (#{tag_type})\n"
    message << "    length  #{git("cat-file -s #{tag_object}").strip} bytes\n"
    message << annotated_tag_content
  end

  message
end

#process_backward_updateObject



781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
# File 'lib/git-commit-mailer.rb', line 781

def process_backward_update
  # List all of the revisions that were removed by this update, in a
  # fast forward update, this list will be empty, because rev-list O
  # ^N is empty.  For a non fast forward, O ^N is the list of removed
  # revisions
  fast_forward = false
  revision_found = false
  commits_summary = []
  git("rev-list #{@new_revision}..#{@old_revision}").lines.each do |revision|
    revision_found ||= true
    revision.strip!
    short_revision = GitCommitMailer.short_revision(revision)
    subject = get_record(revision, '%s')
    commits_summary << "discards  #{short_revision} #{subject}\n"
  end
  unless revision_found
    fast_forward = true
    subject = get_record(old_revision, '%s')
    commits_summary << "    from  #{short_old_revision} #{subject}\n"
  end
  [fast_forward, commits_summary]
end

#process_create_annotated_tagObject



887
888
889
890
891
# File 'lib/git-commit-mailer.rb', line 887

def process_create_annotated_tag
  "Annotated tag (#{@reference}) is created.\n" +
  "        at  #{@new_revision} (tag)\n" +
  process_annotated_tag
end

#process_create_branchObject



729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
# File 'lib/git-commit-mailer.rb', line 729

def process_create_branch
  message = "Branch (#{@reference}) is created.\n"
  commits = []

  commit_list = []
  git("rev-list #{@new_revision} #{excluded_revisions}").lines.
  reverse_each do |revision|
    revision.strip!
    short_revision = GitCommitMailer.short_revision(revision)
    commits << revision
    subject = get_record(revision, '%s')
    commit_list << "     via  #{short_revision} #{subject}\n"
  end
  if commit_list.length > 0
    commit_list[-1].sub!(/\A     via  /, '     at   ')
    message << commit_list.join
  end

  [message, commits]
end

#process_create_unannotated_tagObject



989
990
991
992
993
994
995
# File 'lib/git-commit-mailer.rb', line 989

def process_create_unannotated_tag
  raise "unexpected" unless detect_object_type(@new_revision) == "commit"

  "Unannotated tag (#{@reference}) is created.\n" +
  "        at  #{@new_revision} (commit)\n\n" +
  process_unannotated_tag(@new_revision)
end

#process_delete_annotated_tagObject



900
901
902
903
904
905
906
# File 'lib/git-commit-mailer.rb', line 900

def process_delete_annotated_tag
  "Annotated tag (#{@reference}) is deleted.\n" +
  "       was  #{@old_revision}\n\n" +
  git("show -s --pretty=oneline #{@old_revision}").sub(/^Tagger.*$/, '').
                                                   sub(/^Date.*$/, '').
                                                   sub(/\n{2,}/, "\n\n")
end

#process_delete_branchObject



881
882
883
884
885
# File 'lib/git-commit-mailer.rb', line 881

def process_delete_branch
  "Branch (#{@reference}) is deleted.\n" +
  "       was  #{@old_revision}\n\n" +
  git("show -s --pretty=oneline #{@old_revision}")
end

#process_delete_unannotated_tagObject



1007
1008
1009
1010
1011
1012
1013
# File 'lib/git-commit-mailer.rb', line 1007

def process_delete_unannotated_tag
  raise "unexpected" unless detect_object_type(@old_revision) == "commit"

  "Unannotated tag (#{@reference}) is deleted.\n" +
  "       was  #{@old_revision} (commit)\n\n" +
  process_unannotated_tag(@old_revision)
end

#process_forward_updateObject



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# File 'lib/git-commit-mailer.rb', line 804

def process_forward_update
  # List all the revisions from baserev to new_revision in a kind of
  # "table-of-contents"; note this list can include revisions that
  # have already had notification emails and is present to show the
  # full detail of the change from rolling back the old revision to
  # the base revision and then forward to the new revision
  commits_summary = []
  git("rev-list #{@old_revision}..#{@new_revision}").lines.each do |revision|
    revision.strip!
    short_revision = GitCommitMailer.short_revision(revision)

    subject = get_record(revision, '%s')
    commits_summary << "     via  #{short_revision} #{subject}\n"
  end
  commits_summary
end

#process_reference_change(old_revision, new_revision, reference) ⇒ Object



1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
# File 'lib/git-commit-mailer.rb', line 1128

def process_reference_change(old_revision, new_revision, reference)
  reset(old_revision, new_revision, reference)

  make_infos
  make_mails
  if rss_output_available?
    output_rss
  end

  [@push_mails, @commit_mails]
end

#process_unannotated_tag(revision) ⇒ Object



1015
1016
1017
# File 'lib/git-commit-mailer.rb', line 1015

def process_unannotated_tag(revision)
  git("show --no-color --root -s --pretty=short #{revision}")
end

#process_update_annotated_tagObject



893
894
895
896
897
898
# File 'lib/git-commit-mailer.rb', line 893

def process_update_annotated_tag
  "Annotated tag (#{@reference}) is updated.\n" +
  "        to  #{@new_revision} (tag)\n" +
  "      from  #{@old_revision} (which is now obsolete)\n" +
  process_annotated_tag
end

#process_update_branchObject



854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
# File 'lib/git-commit-mailer.rb', line 854

def process_update_branch
  message = "Branch (#{@reference}) is updated.\n"

  fast_forward, backward_commits_summary = process_backward_update
  forward_commits_summary = process_forward_update

  commits_summary = backward_commits_summary + forward_commits_summary.reverse

  unless fast_forward
    rewind_only, explanation = explain_special_case
    message << explanation
  end

  message << "\n"
  message << commits_summary.join

  unless rewind_only
    new_commits = collect_new_commits
  end
  if rewind_only or new_commits.empty?
    message << "\n"
    message << "No new revisions were added by this update.\n"
  end

  [message, new_commits]
end

#process_update_unannotated_tagObject



997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/git-commit-mailer.rb', line 997

def process_update_unannotated_tag
  raise "unexpected" unless detect_object_type(@new_revision) == "commit"
  raise "unexpected" unless detect_object_type(@old_revision) == "commit"

  "Unannotated tag (#{@reference}) is updated.\n" +
  "        to  #{@new_revision} (commit)\n" +
  "      from  #{@old_revision} (commit)\n\n" +
  process_unannotated_tag(@new_revision)
end

#reset(old_revision, new_revision, reference) ⇒ Object



1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/git-commit-mailer.rb', line 1081

def reset(old_revision, new_revision, reference)
  @old_revision = old_revision
  @new_revision = new_revision
  @reference = reference

  @push_info = nil
  @commit_infos = []
  @commit_info_map = {}
end

#send_all_mailsObject



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
# File 'lib/git-commit-mailer.rb', line 1140

def send_all_mails
  if send_push_mail?
    @push_mails.each do |mail|
      send_mail(mail)
    end
  end

  @commit_mails.each do |mail|
    send_mail(mail)
  end
end

#send_per_to?Boolean



551
552
553
# File 'lib/git-commit-mailer.rb', line 551

def send_per_to?
  @send_per_to
end

#send_push_mail?Boolean



1164
1165
1166
# File 'lib/git-commit-mailer.rb', line 1164

def send_push_mail?
  @send_push_mail
end

#short_log(revision_specifier) ⇒ Object



908
909
910
911
912
913
914
915
# File 'lib/git-commit-mailer.rb', line 908

def short_log(revision_specifier)
  log = git("rev-list --pretty=short #{GitCommitMailer.shell_escape(revision_specifier)}")
  git("shortlog") do |git|
    git.write(log)
    git.close_write
    return git.read
  end
end

#short_log_from_previous_tag(previous_tag) ⇒ Object



917
918
919
920
921
922
923
924
925
# File 'lib/git-commit-mailer.rb', line 917

def short_log_from_previous_tag(previous_tag)
  if previous_tag
    # Show changes since the previous release
    short_log("#{previous_tag}..#{@new_revision}")
  else
    # No previous tag, show all the changes since time began
    short_log(@new_revision)
  end
end

#short_new_revisionObject



599
600
601
# File 'lib/git-commit-mailer.rb', line 599

def short_new_revision
  GitCommitMailer.short_revision(@new_revision)
end

#short_old_revisionObject



603
604
605
# File 'lib/git-commit-mailer.rb', line 603

def short_old_revision
  GitCommitMailer.short_revision(@old_revision)
end

#show_path?Boolean



1160
1161
1162
# File 'lib/git-commit-mailer.rb', line 1160

def show_path?
  @show_path
end

#track_remote?Boolean



591
592
593
# File 'lib/git-commit-mailer.rb', line 591

def track_remote?
  @track_remote
end

#traverse_merge_commit(merge_commit) ⇒ Object



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/git-commit-mailer.rb', line 1027

def traverse_merge_commit(merge_commit)
  first_grand_parent = parent_commit(merge_commit.first_parent)

  [merge_commit.first_parent, *merge_commit.other_parents].each do |revision|
    is_traversing_first_parent = (revision == merge_commit.first_parent)
    base_revision = git("merge-base #{first_grand_parent} #{revision}").strip
    base_revisions = [@old_revision, base_revision]
    #branch_name = find_branch_name_from_its_descendant_revision(revision)
    descendant_revision = merge_commit.revision

    until base_revisions.index(revision)
      commit_info = @commit_info_map[revision]
      if commit_info
        commit_info.reference = @reference
      else
        commit_info = create_commit_info(@reference, revision)
        index = @commit_infos.index(@commit_info_map[descendant_revision])
        @commit_infos.insert(index, commit_info)
        @commit_info_map[revision] = commit_info
      end

      merge_message = "Merged #{merge_commit.short_revision}: #{merge_commit.subject}"
      if not is_traversing_first_parent and not commit_info.merge_messages.index(merge_message)
        commit_info.merge_messages << merge_message
        commit_info.merge_commits << merge_commit
      end

      if commit_info.merge?
        traverse_merge_commit(commit_info)
        base_revision = git("merge-base #{first_grand_parent} #{commit_info.first_parent}").strip
        base_revisions << base_revision unless base_revisions.index(base_revision)
      end
      descendant_revision, revision = revision, commit_info.first_parent
    end
  end
end

#verbose?Boolean



595
596
597
# File 'lib/git-commit-mailer.rb', line 595

def verbose?
  @verbose
end