commit_format 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef4ace94c6a11e7172e7013ce1eebbccd4ab41e26d3e926b109f10af2e69bf39
4
+ data.tar.gz: b314bbcc2b661def7c107cf3ea2ea6d0d129695348618fefb9af79b1a20b3224
5
+ SHA512:
6
+ metadata.gz: 14701063826fe6467c185353da71aeb2bf7b92fe01fc94e34a1e4aa7f4ef6169e3b6f1f3f78b16113c81928eaf2f263d7183be38cf0d56564da099e84fdcd4b7
7
+ data.tar.gz: c6b176440cfc56cab8003151c41bf940f6ce67879ea7298827847c65eb6d5ad9b86eaf55c1a7b5e283571e55ee08c32f6deddd737622c2aa159b5ddb9782b88b
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_PATH: "vendor/.bundle"
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ Gemfile.lock
2
+ spec/examples.txt
3
+ tmp
4
+ vendor
5
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,69 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ UseCache: true
4
+ CacheRootDirectory: ./tmp
5
+ NewCops: enable
6
+ TargetRubyVersion: "2.7.0"
7
+
8
+ Style/Documentation:
9
+ Enabled: false
10
+
11
+ Style/StringLiterals:
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/HashSyntax:
18
+ EnforcedStyle: hash_rockets
19
+
20
+ Layout/HashAlignment:
21
+ EnforcedLastArgumentHashStyle: ignore_implicit
22
+
23
+ Layout/ArgumentAlignment:
24
+ EnforcedStyle: with_fixed_indentation
25
+
26
+ Layout/ParameterAlignment:
27
+ EnforcedStyle: with_fixed_indentation
28
+
29
+ Layout/MultilineMethodCallIndentation:
30
+ EnforcedStyle: indented
31
+
32
+ Layout/MultilineOperationIndentation:
33
+ EnforcedStyle: indented
34
+
35
+ Layout/FirstArrayElementIndentation:
36
+ EnforcedStyle: consistent
37
+
38
+ Layout/LineEndStringConcatenationIndentation:
39
+ EnforcedStyle: indented
40
+
41
+ Layout/LineLength:
42
+ Max: 80
43
+
44
+ Style/Lambda:
45
+ EnforcedStyle: lambda
46
+
47
+ Style/WordArray:
48
+ Enabled: false
49
+
50
+ Style/FrozenStringLiteralComment:
51
+ Enabled: true
52
+
53
+ Style/SymbolArray:
54
+ EnforcedStyle: brackets
55
+
56
+ Metrics/AbcSize:
57
+ Enabled: false
58
+
59
+ Metrics/BlockLength:
60
+ Exclude:
61
+ - Rakefile
62
+
63
+ Metrics/MethodLength:
64
+ Max: 15
65
+ Exclude:
66
+ - Rakefile
67
+
68
+ Bundler/OrderedGems:
69
+ Enabled: false
@@ -0,0 +1,58 @@
1
+ ---
2
+ version: v1.0
3
+ name: Commit Format
4
+ agent:
5
+ machine:
6
+ type: e1-standard-2
7
+ os_image: ubuntu1804
8
+ auto_cancel:
9
+ running:
10
+ when: branch != 'main' AND branch != 'develop'
11
+ global_job_config:
12
+ env_vars:
13
+ - name: _BUNDLER_CACHE
14
+ value: v1
15
+ prologue:
16
+ commands:
17
+ - checkout
18
+ - |
19
+ if [ -n "$RUBY_VERSION" ]; then
20
+ sem-version ruby $RUBY_VERSION
21
+ cache restore $_BUNDLER_CACHE-bundler-$RUBY_VERSION-$(checksum commit_format.gemspec)-$(checksum Gemfile)
22
+ bundle install --jobs=3 --retry=3
23
+ cache store $_BUNDLER_CACHE-bundler-$RUBY_VERSION-$(checksum commit_format.gemspec)-$(checksum Gemfile) vendor/.bundle
24
+ else
25
+ echo Skipping Ruby install
26
+ fi
27
+ blocks:
28
+ - name: Test
29
+ dependencies: []
30
+ task:
31
+ prologue:
32
+ commands:
33
+ - "git config --global user.email 'test@example.com'"
34
+ - "git config --global user.name 'John Doe'"
35
+ - "git config --global init.defaultBranch main"
36
+ jobs:
37
+ - name: Test
38
+ matrix:
39
+ - env_var: RUBY_VERSION
40
+ values:
41
+ - 3.1.0
42
+ - 3.0.3
43
+ - 2.7.5
44
+ commands:
45
+ - bundle exec rspec
46
+ - name: Linters
47
+ dependencies: []
48
+ task:
49
+ jobs:
50
+ - name: Ruby Lint (RuboCop)
51
+ env_vars:
52
+ - name: RUBY_VERSION
53
+ value: 3.1.0
54
+ commands:
55
+ - bundle exec rubocop
56
+ - name: Git Lint (Lintje)
57
+ commands:
58
+ - script/lint_git
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Commit Format
2
+
3
+ ## 0.1.0
4
+
5
+ Initial release.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,33 @@
1
+ Commit Format Copyright 2022 (Tom de Bruijn)(“Licensor”)
2
+
3
+ Hippocratic License Version Number: 2.1.
4
+
5
+ Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
6
+
7
+ Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
8
+
9
+ * Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
10
+
11
+ * Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
12
+
13
+ * Compliance with Human Rights Principles and Human Rights Laws.
14
+
15
+ 1. Human Rights Principles.
16
+
17
+ (a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
18
+
19
+ (b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
20
+
21
+ Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
22
+
23
+ 2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
24
+
25
+ 3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
26
+
27
+ * Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
28
+
29
+ * Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
30
+
31
+ * Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
32
+
33
+ This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Commit Format
2
+
3
+ Commit Format is a formatter for commits to paste into a Git Pull Request
4
+ description.
5
+
6
+ ## Installation
7
+
8
+ 1. Run `gem install commit_format`
9
+
10
+ ## Usage
11
+
12
+ ```
13
+ # Print all commits on feature branch since branching off the default branch
14
+ # Only works when not on the default branch
15
+ commit-format
16
+
17
+ # Copy the output directly onto the clipboard on macOS
18
+ commit-format | pbcopy
19
+
20
+ # Print the range of formatted commits
21
+ commit-format HEAD~1..HEAD
22
+
23
+ # Prints the commits new in the 'my-feature' branch
24
+ commit-format main..my-feature
25
+
26
+ # Print the last 5 commits
27
+ commit-format -n 5
28
+
29
+ # Print all new commits since selected base branch
30
+ commit-format -b main
31
+ commit-format -b feature-branch
32
+ ```
33
+
34
+ ## Features
35
+
36
+ - Turns each commit subject into a markdown heading.
37
+ - Indents all headings in the message body on level lower. Heading level 2
38
+ becomes heading level 3, etc.
39
+
40
+ ## Development
41
+
42
+ - Run `bundle install`
43
+ - Install [mono](https://github.com/appsignal/mono).
44
+
45
+ ### Tracking changes
46
+
47
+ Use mono to create a changeset per change.
48
+
49
+ ```
50
+ mono changeset add
51
+ ```
52
+
53
+ ## Testing
54
+
55
+ ```
56
+ bundle exec rspec
57
+ ```
58
+
59
+ ## Publishing
60
+
61
+ ```
62
+ mono publish
63
+ ```
64
+
65
+ ## Resources
66
+
67
+ - [Git is about communication](https://tomdebruijn.com/posts/git-is-about-communication/)
data/bin/commit-format ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.expand_path(File.join(__dir__, "..", "lib"))
5
+
6
+ require "commit_format"
7
+ require "commit_format/cli"
8
+
9
+ CommitFormat::Cli.new(ARGV).execute
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("lib/commit_format/version", __dir__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Tom de Bruijn"]
7
+ gem.email = ["tom@tomdebruijn.com"]
8
+ gem.summary = "Git commit formatter utility."
9
+ gem.description = "Commit-format is a utility to make it easier generating " \
10
+ "Pull Request descriptions from multiple commits."
11
+ gem.homepage = "https://github.com/tombruijn/commit-format"
12
+ gem.license = "Hippocratic-2.1"
13
+
14
+ gem.files = `git ls-files`
15
+ .split($\) # rubocop:disable Style/SpecialGlobalVars
16
+ .reject { |f| f.start_with?(".changesets/") }
17
+ gem.executables << "commit-format"
18
+ gem.name = "commit_format"
19
+ gem.require_paths = %w[lib]
20
+ gem.version = CommitFormat::VERSION
21
+ gem.required_ruby_version = ">= 2.7.0"
22
+
23
+ gem.metadata = {
24
+ "rubygems_mfa_required" => "true",
25
+ "bug_tracker_uri" => "https://github.com/tombruijn/commit-format/issues",
26
+ "changelog_uri" =>
27
+ "https://github.com/tombruijn/commit-format/blob/main/CHANGELOG.md",
28
+ "source_code_uri" => "https://github.com/tombruijn/commit-format"
29
+ }
30
+
31
+ gem.add_development_dependency "pry", "0.14.1"
32
+ gem.add_development_dependency "rspec", "3.10.0"
33
+ gem.add_development_dependency "rubocop", "1.25.0"
34
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ module CommitFormat
6
+ class Cli
7
+ def initialize(args)
8
+ @args = parse_args(args)
9
+ end
10
+
11
+ def execute
12
+ parser = Parser.new(@args)
13
+ commits = parser.commits
14
+
15
+ formatter = Formatter.new(commits)
16
+ formatted_commits = formatter.formatted_commits
17
+ print_formatted_commits(formatted_commits)
18
+ end
19
+
20
+ private
21
+
22
+ def print_formatted_commits(commits)
23
+ commits_length = commits.length - 1
24
+ commits.each_with_index do |commit, index|
25
+ puts commit
26
+
27
+ # Print empty line between commits
28
+ puts if index < commits_length
29
+ end
30
+ end
31
+
32
+ def parse_args(args) # rubocop:disable Metrics/MethodLength
33
+ options = {}
34
+ OptionParser.new do |parser|
35
+ document_cli(parser)
36
+
37
+ parser.separator "Options:"
38
+ parser.on(
39
+ "-n",
40
+ "--max-count <number>",
41
+ "Limit the number of commits to output"
42
+ ) do |count|
43
+ options[:limit] = count
44
+ end
45
+ parser.on(
46
+ "-b",
47
+ "--base-branch <branch>",
48
+ "Select all commits since selected base branch"
49
+ ) do |branch|
50
+ options[:base_branch] = branch
51
+ end
52
+ parser.on("-v", "--version", "Print version number") do
53
+ puts CommitFormat::VERSION
54
+ exit 0
55
+ end
56
+ parser.on_tail("-h", "--help", "Show this help message") do
57
+ puts parser
58
+ exit 0
59
+ end
60
+ end.parse!(args)
61
+
62
+ options[:init_default_branch] = `git config init.defaultbranch`.strip
63
+ options[:selector] = args.join if args.any?
64
+ options
65
+ end
66
+
67
+ def document_cli(cli) # rubocop:disable Metrics/MethodLength
68
+ cli.banner = "Usage: commit-format [options] [commit range]"
69
+ cli.separator ""
70
+ cli.separator "Examples:"
71
+ cli.separator " commit-format"
72
+ cli.separator " Print all commits on a feature branch since " \
73
+ "branching off the default branch"
74
+ cli.separator " Only works when not on the default branch"
75
+ cli.separator ""
76
+ cli.separator " commit-format | pbcopy"
77
+ cli.separator " Copy the commits to the clipboard on macOS"
78
+ cli.separator ""
79
+ cli.separator " commit-format HEAD~1..HEAD"
80
+ cli.separator " Prints the commits in the commit range"
81
+ cli.separator ""
82
+ cli.separator " commit-format main..my-feature"
83
+ cli.separator " Prints the commits new in the 'my-feature' branch"
84
+ cli.separator ""
85
+ cli.separator " commit-format -n 5"
86
+ cli.separator " Print the last 5 commits"
87
+ cli.separator ""
88
+ cli.separator " commit-format -b main"
89
+ cli.separator " commit-format -b intermediate-feature-branch"
90
+ cli.separator " Print all new commits since selected branch"
91
+ cli.separator ""
92
+ cli.separator "Arguments:"
93
+ cli.separator " <commit range> Range of commits to format."
94
+ cli.separator ""
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommitFormat
4
+ class Formatter
5
+ def initialize(commits)
6
+ @commits = commits
7
+ end
8
+
9
+ def formatted_commits
10
+ @commits.map { |commit| format_commit(commit) }
11
+ end
12
+
13
+ private
14
+
15
+ def format_commit(commit)
16
+ formatted_commit =
17
+ commit.lines.map do |line|
18
+ # Prepend another `#` characters to make headings one level lower
19
+ if line.start_with?("##")
20
+ "##{line}"
21
+ else
22
+ line
23
+ end
24
+ end.join
25
+ # Start subject with a heading
26
+ "## #{formatted_commit}"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommitFormat
4
+ class Parser
5
+ SEPARATOR = "# ------------------------ COMMIT >! ------------------------"
6
+
7
+ def initialize(options)
8
+ @options = options
9
+ end
10
+
11
+ def commits
12
+ log_output =
13
+ `git log --reverse --pretty="#{SEPARATOR}%n%B" #{arguments(@options)}`
14
+ log_output
15
+ .split(SEPARATOR)
16
+ .map(&:strip)
17
+ .reject { |commit| commit.length.zero? }
18
+ end
19
+
20
+ private
21
+
22
+ def arguments(options)
23
+ args = []
24
+ args << "--max-count=#{options[:limit]}" if options.key?(:limit)
25
+ args << main_argument(options)
26
+ args.compact.join(" ")
27
+ end
28
+
29
+ def main_argument(options)
30
+ return options[:selector] if options.key?(:selector)
31
+ return if options.key?(:limit)
32
+
33
+ if options.key?(:base_branch)
34
+ "#{options[:base_branch]}..HEAD"
35
+ else
36
+ "#{options[:init_default_branch]}..HEAD"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommitFormat
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommitFormat
4
+ end
5
+
6
+ require "commit_format/parser"
7
+ require "commit_format/formatter"
data/mono.yml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ language: ruby
3
+ repo: "https://github.com/tombruijn/commit-format"
data/script/lint_git ADDED
@@ -0,0 +1,22 @@
1
+ #!/bin/bash
2
+
3
+ set -eu
4
+
5
+ LINTJE_VERSION="0.6.1"
6
+
7
+ mkdir -p $HOME/bin
8
+ cache_key=v1-lintje-$LINTJE_VERSION
9
+ cache restore $cache_key
10
+
11
+ # File exists and is executable
12
+ if [ -x "$HOME/bin/lintje" ]; then
13
+ echo "Restored Lintje $LINTJE_VERSION from cache"
14
+ else
15
+ echo "Downloading Lintje $LINTJE_VERSION"
16
+ curl -L \
17
+ https://github.com/tombruijn/lintje/releases/download/v$LINTJE_VERSION/x86_64-unknown-linux-gnu.tar.gz | \
18
+ tar -xz --directory $HOME/bin
19
+ cache store $cache_key $HOME/bin/lintje
20
+ fi
21
+
22
+ $HOME/bin/lintje $SEMAPHORE_GIT_COMMIT_RANGE
data/spec/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ inherit_from: ../.rubocop.yml
2
+
3
+ Metrics/BlockLength:
4
+ Enabled: false
5
+
6
+ Layout/LineLength:
7
+ Max: 100
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe CommitFormat::Cli do
4
+ describe "with --version flag" do
5
+ it "prints the version number" do
6
+ output = run_version
7
+ expect(output).to eql(CommitFormat::VERSION)
8
+ end
9
+ end
10
+
11
+ describe "without an argument" do
12
+ it "formats all commits to main branch" do
13
+ test_dir = "without_argument"
14
+ prepare_repository test_dir do
15
+ create_commit "Commit subject 1", "Commit message body.\nSecond line."
16
+ checkout_branch "my-branch"
17
+ create_commit "Commit subject 2", "Commit message body.\nSecond line."
18
+ create_commit "Commit subject 3", "Commit message body.\nSecond line."
19
+ end
20
+
21
+ output = run(test_dir)
22
+ expect(output).to eql(<<~OUTPUT)
23
+ ## Commit subject 2
24
+
25
+ Commit message body.
26
+ Second line.
27
+
28
+ ## Commit subject 3
29
+
30
+ Commit message body.
31
+ Second line.
32
+ OUTPUT
33
+ end
34
+ end
35
+
36
+ context "with --base-branch argument" do
37
+ it "formats all commits since the base branch" do
38
+ test_dir = "base_branch"
39
+ prepare_repository test_dir do
40
+ create_commit "Commit subject 1", "Commit message body.\nSecond line."
41
+ checkout_branch "branch1"
42
+ create_commit "Commit subject 2", "Commit message body.\nSecond line."
43
+ checkout_branch "branch2"
44
+ create_commit "Commit subject 3", "Commit message body.\nSecond line."
45
+ create_commit "Commit subject 4", "Commit message body.\nSecond line."
46
+ end
47
+
48
+ output = run(test_dir, ["--base-branch=branch1"])
49
+ expect(output).to eql(<<~OUTPUT)
50
+ ## Commit subject 3
51
+
52
+ Commit message body.
53
+ Second line.
54
+
55
+ ## Commit subject 4
56
+
57
+ Commit message body.
58
+ Second line.
59
+ OUTPUT
60
+ end
61
+ end
62
+
63
+ describe "with --max-count argument" do
64
+ it "--max-count 1 formats a single commit" do
65
+ test_dir = "option_max_count_1"
66
+ prepare_repository test_dir do
67
+ create_commit "Commit subject 1", "Commit message body.\nSecond line."
68
+ create_commit "Commit subject 2", "Commit message body.\nSecond line."
69
+ end
70
+
71
+ output = run(test_dir, ["--max-count=1"])
72
+ expect(output).to eql(<<~OUTPUT)
73
+ ## Commit subject 2
74
+
75
+ Commit message body.
76
+ Second line.
77
+ OUTPUT
78
+ end
79
+
80
+ it "--max-count 2 formats multiple commits" do
81
+ test_dir = "option_max_count_2"
82
+ prepare_repository test_dir do
83
+ create_commit "Commit subject 1", "Commit message body.\nSecond line."
84
+ create_commit "Commit subject 2", "Commit message body.\nSecond line."
85
+ end
86
+
87
+ output = run(test_dir, ["--max-count=2"])
88
+ expect(output).to eql(<<~OUTPUT)
89
+ ## Commit subject 1
90
+
91
+ Commit message body.
92
+ Second line.
93
+
94
+ ## Commit subject 2
95
+
96
+ Commit message body.
97
+ Second line.
98
+ OUTPUT
99
+ end
100
+ end
101
+
102
+ describe "with a single commit argument" do
103
+ it "formats a single commit" do
104
+ test_dir = "single_commit"
105
+ prepare_repository test_dir do
106
+ create_commit "Commit subject 1", "Commit message body.\nSecond line."
107
+ create_commit "Commit subject 2", "Commit message body.\nSecond line."
108
+ end
109
+
110
+ output = run(test_dir, ["HEAD~1..HEAD"])
111
+ expect(output).to eql(<<~OUTPUT)
112
+ ## Commit subject 2
113
+
114
+ Commit message body.
115
+ Second line.
116
+ OUTPUT
117
+ end
118
+ end
119
+
120
+ describe "with multiple commits argument" do
121
+ it "formats multiple commits" do
122
+ test_dir = "multiple_commits"
123
+ prepare_repository test_dir do
124
+ create_commit "Commit subject 1", "Commit message body.\nSecond line."
125
+ checkout_branch "my-branch"
126
+ create_commit "Commit subject 2", "Commit message body.\nSecond line."
127
+ create_commit "Commit subject 3", "Commit message body.\nSecond line."
128
+ end
129
+
130
+ output = run(test_dir, ["main...HEAD"])
131
+ expect(output).to eql(<<~OUTPUT)
132
+ ## Commit subject 2
133
+
134
+ Commit message body.
135
+ Second line.
136
+
137
+ ## Commit subject 3
138
+
139
+ Commit message body.
140
+ Second line.
141
+ OUTPUT
142
+ end
143
+
144
+ it "indents headings by one level" do
145
+ test_dir = "multiple_commits"
146
+ message_body = <<~MESSAGE
147
+ Commit message body.
148
+
149
+ ## Sub heading 1
150
+
151
+ First section.
152
+
153
+ ## Sub heading 2
154
+
155
+ Second section.
156
+ MESSAGE
157
+ prepare_repository test_dir do
158
+ create_commit "Commit subject 1", message_body
159
+ create_commit "Commit subject 2", message_body
160
+ create_commit "Commit subject 3", message_body
161
+ end
162
+
163
+ output = run(test_dir, ["HEAD~2...HEAD"])
164
+ expect(output).to eql(<<~OUTPUT)
165
+ ## Commit subject 2
166
+
167
+ Commit message body.
168
+
169
+ ### Sub heading 1
170
+
171
+ First section.
172
+
173
+ ### Sub heading 2
174
+
175
+ Second section.
176
+
177
+ ## Commit subject 3
178
+
179
+ Commit message body.
180
+
181
+ ### Sub heading 1
182
+
183
+ First section.
184
+
185
+ ### Sub heading 2
186
+
187
+ Second section.
188
+ OUTPUT
189
+ end
190
+ end
191
+
192
+ def run(dir_name, args = [])
193
+ capture_stdout do
194
+ in_repository dir_name do
195
+ CommitFormat::Cli.new(args).execute
196
+ end
197
+ end
198
+ end
199
+
200
+ def run_version
201
+ capture_stdout do
202
+ CommitFormat::Cli.new(["--version"]).execute
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "commit_format"
4
+ require "commit_format/cli"
5
+
6
+ ROOT_DIR = File.expand_path("..", __dir__)
7
+ SPEC_DIR = File.expand_path(__dir__)
8
+ Dir.glob("support/**/*.rb", :base => SPEC_DIR).sort.each do |file|
9
+ require file
10
+ end
11
+
12
+ RSpec.configure do |config|
13
+ config.expect_with :rspec do |expectations|
14
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
15
+ end
16
+ config.mock_with :rspec do |mocks|
17
+ mocks.verify_partial_doubles = true
18
+ end
19
+ config.shared_context_metadata_behavior = :apply_to_host_groups
20
+ config.example_status_persistence_file_path = "spec/examples.txt"
21
+ config.disable_monkey_patching!
22
+ config.warnings = true
23
+ config.order = :random
24
+ Kernel.srand config.seed
25
+
26
+ config.include OutputHelper
27
+ config.include RepositoryHelper
28
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tempfile"
4
+ require "securerandom"
5
+
6
+ module OutputHelper
7
+ def std_stream
8
+ Tempfile.new SecureRandom.uuid
9
+ end
10
+
11
+ # Capture STDOUT in a variable
12
+ #
13
+ # Given tempfiles are rewinded and unlinked after yield, so no cleanup
14
+ # required. You can read from the stream using `stdout.read`.
15
+ #
16
+ # Usage
17
+ #
18
+ # out_stream = Tempfile.new
19
+ # capture_stdout(out_stream) { do_something }
20
+ def capture_stdout(stdout = std_stream)
21
+ original_stdout = $stdout.dup
22
+ $stdout.reopen stdout
23
+
24
+ begin
25
+ yield
26
+ ensure
27
+ $stdout.reopen original_stdout
28
+ stdout.rewind
29
+ stdout.unlink
30
+ end
31
+ stdout.read
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RepositoryHelper
4
+ REPOSITORY_TEST_DIR = File.join(ROOT_DIR, "tmp/examples")
5
+
6
+ def prepare_repository(dir_name)
7
+ directory = directory_path(dir_name)
8
+ FileUtils.rm_rf directory
9
+ FileUtils.mkdir_p directory
10
+ Dir.chdir directory do
11
+ `git init .`
12
+ `git branch -m main`
13
+ create_commit "Initial commit", ""
14
+ yield
15
+ end
16
+ end
17
+
18
+ def in_repository(dir_name, &block)
19
+ directory = directory_path(dir_name)
20
+ Dir.chdir(directory, &block)
21
+ end
22
+
23
+ def directory_path(dir_name)
24
+ File.join(REPOSITORY_TEST_DIR, dir_name)
25
+ end
26
+
27
+ def checkout_branch(name)
28
+ `git checkout -b "#{name}"`
29
+ end
30
+
31
+ def create_commit(subject, message)
32
+ `git commit --allow-empty -m "#{subject}" -m "#{message}"`
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commit_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom de Bruijn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.14.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.14.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.10.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.10.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.25.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.25.0
55
+ description: Commit-format is a utility to make it easier generating Pull Request
56
+ descriptions from multiple commits.
57
+ email:
58
+ - tom@tomdebruijn.com
59
+ executables:
60
+ - commit-format
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".bundle/config"
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".rubocop.yml"
68
+ - ".semaphore/semaphore.yml"
69
+ - CHANGELOG.md
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - bin/commit-format
74
+ - commit_format.gemspec
75
+ - lib/commit_format.rb
76
+ - lib/commit_format/cli.rb
77
+ - lib/commit_format/formatter.rb
78
+ - lib/commit_format/parser.rb
79
+ - lib/commit_format/version.rb
80
+ - mono.yml
81
+ - script/lint_git
82
+ - spec/.rubocop.yml
83
+ - spec/lib/commit_format/cli_spec.rb
84
+ - spec/spec_helper.rb
85
+ - spec/support/output_helper.rb
86
+ - spec/support/repository_helper.rb
87
+ homepage: https://github.com/tombruijn/commit-format
88
+ licenses:
89
+ - Hippocratic-2.1
90
+ metadata:
91
+ rubygems_mfa_required: 'true'
92
+ bug_tracker_uri: https://github.com/tombruijn/commit-format/issues
93
+ changelog_uri: https://github.com/tombruijn/commit-format/blob/main/CHANGELOG.md
94
+ source_code_uri: https://github.com/tombruijn/commit-format
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.7.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.3.4
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Git commit formatter utility.
114
+ test_files: []