create_changelog 1.3.2 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,58 +0,0 @@
1
- # changelog_filter.rb, part of Create-changelog
2
- # Copyright 2015 Daniel Kraus
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- require_relative 'array'
16
-
17
- # Filters a text or array for changelog entries.
18
- class ChangelogFilter
19
- # Factory method that creates an instance given a text string
20
- def self.FromString(string)
21
- unless string.is_a?(String)
22
- fail "Must call this factory with String, not " + string.class.to_s
23
- end
24
- self.FromArray(string.chomp.split("\n"))
25
- end
26
-
27
- # Factory method that creates an instance given an array of strings
28
- def self.FromArray(ary)
29
- unless ary.is_a?(Array)
30
- fail "Must call this factory with Array, not " + ary.class.to_s
31
- end
32
- filter = ChangelogFilter.new
33
- log, text = ary.partition do |line|
34
- line.match(pattern)
35
- end
36
- filter.changelog = log.uniq.sort.remove_indent if log.length > 0
37
- filter.other_text = text if text.length > 0
38
- filter
39
- end
40
-
41
- # Returns the grep string that matches changelog entries.
42
- def self.pattern
43
- '\s*[*-]\s+[^:]+:\s'
44
- end
45
-
46
- # An array of changelog entries.
47
- attr_accessor :changelog
48
-
49
- # An array of text lines that are not changelog entries.
50
- attr_accessor :other_text
51
-
52
- private
53
-
54
- def initialize
55
- end
56
- end
57
-
58
- # vim: nospell
@@ -1,64 +0,0 @@
1
- # commit_changelog.rb, part of Create-changelog
2
- # Copyright 2015 Daniel Kraus
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- require_relative 'changelog_filter'
15
-
16
- # Filters commit messages for changelog entries.
17
- class CommitChangelog
18
-
19
- # Contains changelog entries of the commits.
20
- attr_reader :changelog
21
-
22
- # Instantiates an object containing changelog entries between
23
- # two git commits.
24
- #
25
- # @param [String] to_commit
26
- # Most recent commit whose changelog lines to include.
27
- #
28
- # @param [String] from_commit
29
- # Earlier commit whose changelog lines will _not_ be included.
30
- #
31
- # @return [Array]
32
- # Array of changelog lines, or nil if none were found.
33
- #
34
- def initialize(to_commit, from_commit)
35
- pattern = ChangelogFilter.pattern
36
- messages = Git.get_filtered_messages(from_commit, to_commit, pattern)
37
- filter = ChangelogFilter.FromString(messages)
38
- @changelog = filter.changelog
39
- end
40
-
41
- # Adds changelog information contained in a specific commit message. This
42
- # method is typically used to parse the initial commit's commit message.
43
- #
44
- # @param [String] commit
45
- # Sha-1 of the commit whose commit message to filter for changelog lines.
46
- #
47
- # @return
48
- # Undefined
49
- #
50
- def add_commit(commit)
51
- pattern = ChangelogFilter.pattern
52
- filtered_text = Git.get_filtered_message(commit, pattern)
53
- if filtered_text
54
- filtered_lines = filtered_text.split("\n").uniq
55
- if @changelog
56
- @changelog = @changelog.concat(filtered_lines).uniq
57
- else
58
- @changelog = filtered_lines
59
- end
60
- end
61
- end
62
- end
63
-
64
- # vim: nospell
@@ -1,147 +0,0 @@
1
- # git.rb, part of Create-changelog
2
- # Copyright 2015 Daniel Kraus
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- require_relative 'tag_list'
15
-
16
- # A static wrapper class for git
17
- class Git
18
-
19
- # Determines whether Git is installed
20
- #
21
- # @return [bool]
22
- # True if Git is installed, false if not.
23
- #
24
- def self.is_installed?
25
- `git --version`
26
- $? == 0
27
- end
28
-
29
- # Determines whether the (current) directory is a git repository
30
- #
31
- # @param [String] dir
32
- # Directory to check; if nil, uses the current directory.
33
- #
34
- # @return [bool]
35
- # True if the directory is a Git repository, false if not.
36
- def self.is_git_repository?(dir = nil)
37
- dir = Dir.pwd if dir.nil?
38
- system("git status > /dev/null 2>&1")
39
- $? == 0
40
- end
41
-
42
- # Determines if the repository in the current directory is empty.
43
- #
44
- def self.is_empty_repository?
45
- `git show HEAD > /dev/null 2>&1`
46
- $? != 0
47
- end
48
-
49
- # Retrieves the name of the current branch.
50
- #
51
- # @return [String]
52
- # Current branch.
53
- def self.current_branch
54
- `git branch`.rstrip
55
- end
56
-
57
- # Launches the text editor that Git uses for commit messages,
58
- # and passes file as a command line argument to it.
59
- #
60
- # @see https://github.com/git/git/blob/master/editor.c
61
- # Git's editor.c on GitHub
62
- #
63
- # @param [String] file
64
- # Filename to pass to the editor.
65
- #
66
- # @return [int]
67
- # Exit code of the editor process, or false if no editor found.
68
- #
69
- def self.launch_editor(file)
70
- # const char *editor = getenv("GIT_EDITOR");
71
- editor = ENV['GIT_EDITOR']
72
-
73
- # const char *terminal = getenv("TERM");
74
- terminal = ENV['TERM'];
75
-
76
- # int terminal_is_dumb = !terminal || !strcmp(terminal, "dumb");
77
- terminal_is_dumb = !terminal || terminal == 'dumb'
78
-
79
- # if (!editor && editor_program)
80
- editor = `git config --get core.editor`.rstrip if editor.nil? || editor.empty?
81
-
82
- # if (!editor && !terminal_is_dumb)
83
- # editor = getenv("VISUAL");
84
- editor = ENV['VISUAL'] if (editor.nil? || editor.empty?) && !terminal_is_dumb
85
-
86
- # if (!editor)
87
- # editor = getenv("EDITOR");
88
- editor = ENV['EDITOR'] if (editor.nil? || editor.empty?)
89
-
90
- # if (!editor && terminal_is_dumb)
91
- # return NULL;
92
- # if (!editor)
93
- # editor = DEFAULT_EDITOR;
94
- # Use vi, Git's hard-coded default
95
- editor = 'vi' if (editor.nil? || editor.empty?) && !terminal_is_dumb
96
-
97
- if editor && !editor.empty?
98
- system "#{editor} '#{file}'"
99
- $?
100
- else
101
- false
102
- end
103
- end
104
-
105
- # Retrieves the first 99 lines of the annotation of a tag.
106
- #
107
- def self.get_tag_annotation(tag)
108
- test_tag tag
109
- `git tag -l -n99 #{tag}`.rstrip
110
- end
111
-
112
- # Retrieves the author date of a tag
113
- #
114
- def self.get_tag_date(tag)
115
- test_tag tag
116
- `git log -1 --format=format:%ai #{tag}`
117
- end
118
-
119
- # Retrieves commit messages and filters them
120
- # Todo: Armor this against code injection!
121
- def self.get_filtered_messages(from_commit, to_commit, filter)
122
- `git log #{from_commit}..#{to_commit} -E --grep='#{filter}' --format=%b`
123
- end
124
-
125
- # Retrieves one commit message and filters it
126
- # Todo: Armor this against code injection!
127
- def self.get_filtered_message(commit, filter)
128
- `git log #{commit} -E --grep='#{filter}' --format=%b`
129
- end
130
-
131
- @@tags = nil
132
-
133
- # Ensures lazy loading of the tag list to enable calling code
134
- # to change the working directory first.
135
- def self.tags
136
- @@tags = TagList.new unless @@tags
137
- @@tags
138
- end
139
-
140
- # Tests if the given tag exists and fails if it doesn't
141
- def self.test_tag(tag)
142
- fail "Invalid tag: #{tag}" unless tags.list.include?(tag)
143
- end
144
- private_class_method :test_tag, :tags
145
- end
146
-
147
- # vim: nospell
@@ -1,50 +0,0 @@
1
- # tag.rb, part of Create-changelog
2
- # Copyright 2015 Daniel Kraus
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- require 'date'
15
- require_relative 'git'
16
- require_relative 'changelog_filter'
17
-
18
- # Represents a Git tag and its annotation.
19
- class Tag
20
- # The heading of the tag annotation.
21
- attr_accessor :heading
22
-
23
- # Array of lines in the tag annotation that are not changelog entries.
24
- attr_reader :text
25
-
26
- # Array of lines in the tag annotation that are changelog entries.
27
- attr_reader :changelog
28
-
29
- # Author commit date of the tag
30
- attr_reader :date
31
-
32
- # Gets change information for a specific tagged version.
33
- #
34
- # @param [String] tag
35
- # Tag for which to instantiate the class.
36
- def initialize(tag)
37
- annotation = Git.get_tag_annotation(tag)
38
- @date = Date.parse(Git.get_tag_date(tag))
39
- if annotation
40
- annotation = annotation.split("\n")
41
- @heading = annotation.shift
42
- @heading = @heading.split(' ')[1..-1].join(' ') if @heading
43
- filter = ChangelogFilter.FromArray(annotation)
44
- @text = filter.other_text.remove_indent if filter.other_text
45
- @changelog = filter.changelog
46
- end
47
- end
48
- end
49
-
50
- # vim: nospell
@@ -1,73 +0,0 @@
1
- # TagList, part of Create-changelog
2
- # Copyright 2015 Daniel Kraus
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- # Builds a list of tags in the current git repository.
16
- # The tags are enclosed by the sha1 of the first commit
17
- # and optionally "HEAD" to allow traversing the list
18
- # with each_con to obtain start and end points of
19
- # developmental epochs.
20
- class TagList
21
- # Returns an array of tag names surrounded by HEAD
22
- # at the top and the sha1 of the first commit at the
23
- # bottom.
24
- attr_reader :list
25
-
26
- # Instantiates the tag list.
27
- #
28
- # @param [bool] include_head
29
- # Indicates whether or not to include the most recent changes.
30
- #
31
- def initialize(include_head = true)
32
- @include_head = include_head
33
- @list = build_list
34
- end
35
-
36
- # Returns the most recent tag in the git repository,
37
- # or the sha1 of the initial commit if there is no tag.
38
- #
39
- # @return [String]
40
- #
41
- def latest_tag
42
- # Index 0 is HEAD
43
- # Index 1 is most recent tag or first commit
44
- @list[1]
45
- end
46
-
47
- private
48
-
49
- # Returns the sha1 of the initial commit.
50
- # In fact, this function returns all parentless commits
51
- # of the repository. Usually there should be not more than
52
- # one such commit.
53
- # See http://stackoverflow.com/a/1007545/270712
54
- #
55
- def get_initial_commit
56
- `git rev-list --max-parents=0 HEAD`.chomp
57
- end
58
-
59
- # Builds a list of Git tags and encloses it with HEAD and the
60
- # Sha-1 of the initial commit.
61
- #
62
- # @return [Array]
63
- # Array of tags, surrounded by HEAD and the Sha-1 of the initial commit.
64
- def build_list
65
- tags = []
66
- tags << get_initial_commit
67
- tags += `git tag | sort -V`.split("\n").map { |s| s.rstrip }
68
- tags << "HEAD" if @include_head
69
- tags.reverse
70
- end
71
- end
72
-
73
- # vim: nospell
@@ -1,19 +0,0 @@
1
- require 'test/unit'
2
- require_relative '../lib/changelog_filter'
3
-
4
- class TestChangelogFilter < Test::Unit::TestCase
5
- def test_filter_from_string
6
- log_entry = "- TEST: This is a test log entry"
7
- string = <<-EOF
8
- This is a multiline
9
- test string
10
- #{log_entry}
11
- and more text
12
- EOF
13
- filter = ChangelogFilter.FromString(string)
14
- assert(filter.changelog.length == 1,
15
- 'Filter should contain one changelog entry.')
16
- assert(filter.changelog[0].lstrip == log_entry,
17
- 'Filtered changelog did not contain test entry.')
18
- end
19
- end
@@ -1,12 +0,0 @@
1
- require 'test/unit'
2
- require_relative '../lib/tag_list'
3
-
4
- class TestTagList < Test::Unit::TestCase
5
- def test_list_is_enclosed
6
- tags = TagList.new
7
- assert(tags.list[0] == 'HEAd',
8
- 'First element in list should be HEAD')
9
- assert(tags.list[-1].length == 40,
10
- 'Last element in list should be Sha-1 hash')
11
- end
12
- end