From 7c2ea8115d4dd6b1ca976c438a95d79d9b906207 Mon Sep 17 00:00:00 2001 From: sg Date: Tue, 22 Oct 2024 12:20:21 +0100 Subject: [PATCH] changelog binary that allows to generate a changelog consisting of commit messages between current HEAD and the last tag --- cmd/changelog/README.md | 16 ++ cmd/changelog/changelog.tmpl.md | 6 + cmd/changelog/go.mod | 31 +++ cmd/changelog/go.sum | 146 +++++++++++ cmd/changelog/main.go | 221 ++++++++++++++++ cmd/changelog/main_test.go | 446 ++++++++++++++++++++++++++++++++ 6 files changed, 866 insertions(+) create mode 100644 cmd/changelog/README.md create mode 100644 cmd/changelog/changelog.tmpl.md create mode 100644 cmd/changelog/go.mod create mode 100644 cmd/changelog/go.sum create mode 100644 cmd/changelog/main.go create mode 100644 cmd/changelog/main_test.go diff --git a/cmd/changelog/README.md b/cmd/changelog/README.md new file mode 100644 index 000000000..2adba0f41 --- /dev/null +++ b/cmd/changelog/README.md @@ -0,0 +1,16 @@ +# Changelog Section Generator + +This binary is pointed to a repository and it generates a changelog based on the commit messages +between the latest tag and HEAD. + +## Use Cases + +### Tag first, then generate Changelog + +If a user tags HEAD first then calls this binary, latest tag will point to the HEAD commit. +If HEAD and latest tag point to the same commit. The binary will produce a changelog for the previous tag and HEAD. + +### Generate Changelog then tag + +The default mode for this project is to run it before you tag a commit. +In this case you need to provide the name of the new tag and the message of the new tag in order to generate a correct changelog entry. diff --git a/cmd/changelog/changelog.tmpl.md b/cmd/changelog/changelog.tmpl.md new file mode 100644 index 000000000..ed9429341 --- /dev/null +++ b/cmd/changelog/changelog.tmpl.md @@ -0,0 +1,6 @@ +## {{.Tag}} + +**{{.TagMessage}}** + +{{range $val := .Messages}}\* {{$val}} +{{end}} diff --git a/cmd/changelog/go.mod b/cmd/changelog/go.mod new file mode 100644 index 000000000..d20f10503 --- /dev/null +++ b/cmd/changelog/go.mod @@ -0,0 +1,31 @@ +module github.com/smithy-security/smithy/cmd/changelog + +go 1.22.2 + +require github.com/go-git/go-git v4.7.0+incompatible + +require ( + dario.cat/mergo v1.0.0 // indirect + github.com/Microsoft/go-winio v0.6.1 // indirect + github.com/ProtonMail/go-crypto v1.0.0 // indirect + github.com/cloudflare/circl v1.3.7 // indirect + github.com/cyphar/filepath-securejoin v0.2.4 // indirect + github.com/emirpasic/gods v1.18.1 // indirect + github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect + github.com/go-git/go-billy/v5 v5.5.0 // indirect + github.com/go-git/go-git/v5 v5.12.0 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect + github.com/kevinburke/ssh_config v1.2.0 // indirect + github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect + github.com/skeema/knownhosts v1.2.2 // indirect + github.com/xanzy/ssh-agent v0.3.3 // indirect + golang.org/x/crypto v0.21.0 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/net v0.22.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/tools v0.13.0 // indirect + gopkg.in/src-d/go-git.v4 v4.13.1 // indirect + gopkg.in/warnings.v0 v0.1.2 // indirect +) diff --git a/cmd/changelog/go.sum b/cmd/changelog/go.sum new file mode 100644 index 000000000..b514a0fb1 --- /dev/null +++ b/cmd/changelog/go.sum @@ -0,0 +1,146 @@ +dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= +dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= +github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= +github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= +github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78= +github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= +github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= +github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= +github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= +github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= +github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg= +github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= +github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= +github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= +github.com/go-git/go-git v4.7.0+incompatible h1:+W9rgGY4DOKKdX2x6HxSR7HNeTxqiKrOvKnuittYVdA= +github.com/go-git/go-git v4.7.0+incompatible/go.mod h1:6+421e08gnZWn30y26Vchf7efgYLe4dl5OQbBSUXShE= +github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys= +github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= +github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo= +github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= +github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= +github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= +github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= +github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= +github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= +golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190729092621-ff9f1409240a/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98= +gopkg.in/src-d/go-git-fixtures.v3 v3.5.0/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g= +gopkg.in/src-d/go-git.v4 v4.13.1 h1:SRtFyV8Kxc0UP7aCHcijOMQGPxHSmMOPrzulQWolkYE= +gopkg.in/src-d/go-git.v4 v4.13.1/go.mod h1:nx5NYcxdKxq5fpltdHnPa2Exj4Sx0EclMWZQbYDu2z8= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/cmd/changelog/main.go b/cmd/changelog/main.go new file mode 100644 index 000000000..620e3e363 --- /dev/null +++ b/cmd/changelog/main.go @@ -0,0 +1,221 @@ +package main + +import ( + "bytes" + _ "embed" + "flag" + "fmt" + "log" + "log/slog" + "os" + "strings" + "time" + + "text/template" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" +) + +type changelog struct { + Tag string + TagMessage string + Messages []string + Timestamp string +} + +type annotatedTag struct { + Name string + Message string + Timestamp string + previousTag *annotatedTag +} + +var ( + //go:embed changelog.tmpl.md + defaultTemplate string + errNoAnnotatedTags = fmt.Errorf("no annotated tags found") + errFailedToGetHead = fmt.Errorf("failed to get HEAD") +) + +// getPreviousTag returns the previous tag based on the current tag. +func getPreviousTag(tags []*plumbing.Reference, currentTag *plumbing.Reference) *plumbing.Reference { + for i, tag := range tags { + if tag.Hash() == currentTag.Hash() && i > 0 { + return tags[i-1] // Return the previous tag + } + } + return nil +} + +// getLatestAnnotatedTag retrieves the tag name and the message of the latest annotated tag. +func getLatestAnnotatedTag(repo *git.Repository) (annotatedTag, error) { + // Get the tag references from the repository + tags, err := repo.Tags() + if err != nil { + return annotatedTag{}, fmt.Errorf("failed to get tags: %v", err) + } + + var latestTagObject *object.Tag + var previousTagObject *object.Tag + // Iterate through the tags to find the latest annotated tag + err = tags.ForEach(func(tagRef *plumbing.Reference) error { + // Try to get the tag object (only annotated tags have this) + tagObj, err := repo.TagObject(tagRef.Hash()) + if err != nil { + // Skip lightweight tags, which do not have messages + return nil + } + // Compare tag creation dates to find the latest tag + if latestTagObject == nil || tagObj.Tagger.When.After(latestTagObject.Tagger.When) { + previousTagObject = latestTagObject + latestTagObject = tagObj + } + return nil + }) + + if err != nil { + return annotatedTag{}, err + } + + if latestTagObject == nil { + return annotatedTag{}, errNoAnnotatedTags + } + + var previousTag annotatedTag + if previousTagObject != nil { + previousTag = annotatedTag{ + Name: strings.TrimSpace(previousTagObject.Name), + Message: strings.TrimSpace(previousTagObject.Message), + Timestamp: strings.TrimSpace(previousTagObject.Tagger.When.Format(time.RFC3339)), + } + } + + annotatedTag := annotatedTag{ + Name: strings.TrimSpace(latestTagObject.Name), + Message: strings.TrimSpace(latestTagObject.Message), + Timestamp: strings.TrimSpace(latestTagObject.Tagger.When.Format(time.RFC3339)), + previousTag: &previousTag, + } + // Return the message of the latest annotated tag + return annotatedTag, nil +} + +func getCommitMessagesUntilHead(from string, repo *git.Repository) ([]string, error) { + rangeMsgs := strings.TrimSpace(from) + + // Resolve HEAD reference + headRef, err := repo.Head() + if err != nil { + return nil, errFailedToGetHead + } + + // Get the latest tag's commit hash + tagRef, err := repo.ResolveRevision(plumbing.Revision(rangeMsgs)) + if err != nil { + return nil, fmt.Errorf("failed to resolve tag reference: %v", err) + } + + // Get the commit iterator between the latest tag and HEAD + commitIter, err := repo.Log(&git.LogOptions{ + From: headRef.Hash(), + Order: git.LogOrderCommitterTime, + }) + if err != nil { + return nil, fmt.Errorf("failed to get commit logs: %v", err) + } + + var logOutput []string + err = commitIter.ForEach(func(c *object.Commit) error { + // Format the commit message: "commit message title" + msg := strings.Split(c.Message, "\n\n") + logOutput = append(logOutput, msg[0]) + + // Stop when the commit hash matches the tag commit + if c.Hash == *tagRef { + return fmt.Errorf("reached the tag commit, stop iterating") + } + + return nil + }) + + if err != nil && err.Error() != "reached the tag commit, stop iterating" { + return nil, err + } + + return logOutput, nil +} + +// generateChangelog generates a changelog between the latest tag and HEAD. +func generateChangelog(repo *git.Repository, newTag annotatedTag, changelogTemplate string) (string, error) { + tag, err := getLatestAnnotatedTag(repo) + if err != nil { + return "", err + } + + commitMsgs, err := getCommitMessagesUntilHead(tag.Name, repo) + if err != nil { + return "", err + } + + if len(commitMsgs) == 1 { // HEAD has 1 commit since the last tag, the tagged one + slog.Info("Head is on the latest annotated tag, creating a changelog between the previous tag and head") + commitMsgs, err = getCommitMessagesUntilHead(tag.previousTag.Name, repo) + if err != nil { + return "", err + } + newTag = tag + } + change := changelog{ + Tag: newTag.Name, + TagMessage: newTag.Message, + Messages: commitMsgs, + } + + // Format the changelog + tmpl, err := template.New("changelog").Parse(changelogTemplate) + if err != nil { + return "", err + } + buf := new(bytes.Buffer) + tmpl.Execute(buf, change) + + return buf.String(), nil +} + +type conf struct { + RepoPath string + ChangelogTmplPath string + NewTagName string + NewTagMessage string +} + +var config = conf{} + +func main() { + flag.StringVar(&config.RepoPath, "repo-path", "", "path to the repository you want to generate a changelog for") + flag.StringVar(&config.ChangelogTmplPath, "changelog-template-path", "", "path to the go-template you want to use for templating your changelog") + flag.StringVar(&config.NewTagName, "new-tag-name", "", "name of the new tag, usually a semver like v1.2.3") + flag.StringVar(&config.NewTagMessage, "new-tag-message", "", "message of the new tag, usually a summary of the changes") + flag.Parse() + repo, err := git.PlainOpen(config.RepoPath) + if err != nil { + log.Fatalf("Failed to open repository '%s': %v", config.RepoPath, err) + } + + changelogTemplate := defaultTemplate + if config.ChangelogTmplPath != "" { + changelogBytes, err := os.ReadFile(config.ChangelogTmplPath) + if err != nil { + log.Fatal(err) + } + changelogTemplate = string(changelogBytes) + } + changelog, err := generateChangelog(repo, annotatedTag{Name: config.NewTagName, Message: config.NewTagMessage}, changelogTemplate) + if err != nil { + log.Fatalf("Error generating changelog: %v", err) + } + + fmt.Println(changelog) +} diff --git a/cmd/changelog/main_test.go b/cmd/changelog/main_test.go new file mode 100644 index 000000000..856adb8b3 --- /dev/null +++ b/cmd/changelog/main_test.go @@ -0,0 +1,446 @@ +package main + +import ( + _ "embed" + "fmt" + "os" + "path/filepath" + "reflect" + "testing" + "time" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" +) + +type gitObj struct { + msg string + name string + timestamp string + tagCommitNum int // for tags only, tag specific commit hash +} + +// createCommit creates a new commit in the repository. +func createCommit(repo *git.Repository, workspace, message, timestamp string) (plumbing.Hash, error) { + // Get the working tree + worktree, err := repo.Worktree() + if err != nil { + return plumbing.ZeroHash, err + } + + // Write a dummy file (if you're working with a non-bare repository) + filename := "dummyfile.txt" + err = os.WriteFile(filepath.Join(workspace, filename), []byte("Hello, world!"), 0644) + if err != nil { + return plumbing.ZeroHash, fmt.Errorf("failed to write dummy file: %v", err) + } + + // Add the file to the staging area + _, err = worktree.Add(filename) + if err != nil { + return plumbing.ZeroHash, fmt.Errorf("failed to add file: %v", err) + } + + when, err := time.Parse(time.RFC3339, timestamp) + if err != nil { + return plumbing.ZeroHash, err + } + // Commit the changes + commitHash, err := worktree.Commit(message, &git.CommitOptions{ + Author: &object.Signature{ + Name: "foobar", + Email: "foobar@example.com", + When: when, + }, + }) + if err != nil { + return plumbing.ZeroHash, fmt.Errorf("failed to create commit: %v", err) + } + + return commitHash, nil +} + +// createAnnotatedTag creates an annotated tag for the given commit. +func createAnnotatedTag(repo *git.Repository, commitHash plumbing.Hash, tagName, tagMessage, tagTimestamp string) (*plumbing.Reference, error) { + when, err := time.Parse(time.RFC3339, tagTimestamp) + if err != nil { + return nil, err + } + // Create the annotated tag + tagHash, err := repo.CreateTag(tagName, commitHash, &git.CreateTagOptions{ + Message: tagMessage, + Tagger: &object.Signature{ + Name: "Jane Doe", + Email: "janedoe@example.com", + When: when, + }, + }) + if err != nil { + return nil, fmt.Errorf("failed to create annotated tag: %v", err) + } + + return tagHash, nil +} + +// createInMemoryRepo creates an empty Git repository in memory. +func mustCreateRepo(workspace string, commits, tags []gitObj) *git.Repository { + repo, err := git.PlainInit(workspace, false) + if err != nil { + panic(err) + } + + commitHashes := make([]plumbing.Hash, 0, len(commits)) + for _, c := range commits { + cHash, err := createCommit(repo, workspace, c.msg, c.timestamp) + if err != nil { + panic(err) + } + commitHashes = append(commitHashes, cHash) + } + + for i, t := range tags { + commitNum := i % len(commitHashes) + if t.tagCommitNum != 0 { + commitNum = t.tagCommitNum + } + if _, err := createAnnotatedTag(repo, + commitHashes[commitNum], + t.name, t.msg, t.timestamp); err != nil { + panic(err) + } + } + return repo +} + +func Test_getLatestAnnotatedTag(t *testing.T) { + workspace := "" + dummyTimestamp := "2023-01-19T18:09:06Z" + + type args struct { + repo func() *git.Repository + } + tests := []struct { + name string + args args + want annotatedTag + wantErr error + }{ + { + name: "no tags, empty repo", + args: args{ + repo: func() *git.Repository { + workspace := t.TempDir() + return mustCreateRepo(workspace, []gitObj{}, []gitObj{}) + }, + }, + want: annotatedTag{}, + wantErr: errNoAnnotatedTags, + }, + { + name: "1 tag", + args: args{ + repo: func() *git.Repository { + os.RemoveAll(workspace) + workspace = t.TempDir() + return mustCreateRepo(workspace, + []gitObj{ + {msg: "foo", timestamp: dummyTimestamp}, + {msg: "bar", timestamp: dummyTimestamp}, + }, + []gitObj{ + { + name: "v0.0", + msg: "init", + timestamp: dummyTimestamp, + }, + }) + }, + }, + want: annotatedTag{ + Name: "v0.0", + Message: "init", + Timestamp: dummyTimestamp, + }, + wantErr: nil, + }, + { + name: "2 tags", + args: args{ + repo: func() *git.Repository { + os.RemoveAll(workspace) + workspace = t.TempDir() + return mustCreateRepo(workspace, + []gitObj{ + {msg: "foo", timestamp: dummyTimestamp}, + {msg: "bar", timestamp: dummyTimestamp}, + {msg: "baz", timestamp: dummyTimestamp}, + }, + []gitObj{ + { + name: "v0.0", + msg: "init", + timestamp: dummyTimestamp, + }, + { + name: "v0.0.1", + msg: "feature1", + timestamp: "2024-01-19T18:09:06Z", + }, + }) + + }, + }, + want: annotatedTag{ + Name: "v0.0.1", + Message: "feature1", + Timestamp: "2024-01-19T18:09:06Z", + }, + wantErr: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getLatestAnnotatedTag(tt.args.repo()) + if (err != nil) && err != tt.wantErr { + t.Errorf("getLatestAnnotatedTag() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("getLatestAnnotatedTag() = %v, want %v", got, tt.want) + } + }) + } + os.RemoveAll(workspace) +} + +func Test_getCommitMessagesUntilHead(t *testing.T) { + workspace := "" + dummyTimestamp := "2023-01-19T18:09:06Z" + + type args struct { + repo func() *git.Repository + from string + } + tests := []struct { + name string + args args + want []string + wantErr error + }{ + { + name: "empty repo", + args: args{ + repo: func() *git.Repository { + workspace := t.TempDir() + return mustCreateRepo(workspace, []gitObj{}, []gitObj{}) + }, + from: "", + }, + want: nil, + wantErr: errFailedToGetHead, + }, + { + name: "2 commits", + args: args{ + repo: func() *git.Repository { + os.RemoveAll(workspace) + workspace = t.TempDir() + return mustCreateRepo(workspace, + []gitObj{ + {msg: "foo", timestamp: dummyTimestamp}, + {msg: "bar", timestamp: dummyTimestamp}, + }, + []gitObj{}) + }, + from: "HEAD~1", + }, + want: []string{"bar", "foo"}, + wantErr: nil, + }, + { + name: "3 commits, from is HEAD-1", + args: args{ + repo: func() *git.Repository { + os.RemoveAll(workspace) + workspace = t.TempDir() + return mustCreateRepo(workspace, + []gitObj{ + {msg: "foobar", timestamp: dummyTimestamp}, + {msg: "foo", timestamp: dummyTimestamp}, + {msg: "bar", timestamp: dummyTimestamp}, + {msg: "baz", timestamp: dummyTimestamp}, + }, []gitObj{}) + + }, + from: "HEAD~2", + }, + + want: []string{"baz", "bar", "foo"}, + wantErr: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getCommitMessagesUntilHead(tt.args.from, tt.args.repo()) + if (err != nil) && err != tt.wantErr { + t.Errorf("getCommitMessagesUntilHead() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("getCommitMessagesUntilHead() = %v, want %v", got, tt.want) + } + }) + } + os.RemoveAll(workspace) +} + +func Test_generateChangelog(t *testing.T) { + workspace := "" + dummyTimestamp := "2023-01-19T18:09:06Z" + + type args struct { + repo func() *git.Repository + template string + newTag annotatedTag + } + tests := []struct { + name string + args args + want string + wantErr error + }{ + { + name: "empty repo, default template", + args: args{ + repo: func() *git.Repository { + workspace := t.TempDir() + return mustCreateRepo(workspace, []gitObj{}, []gitObj{}) + }, + template: defaultTemplate, + newTag: annotatedTag{Name: "a", Message: "b"}, + }, + want: "", + wantErr: errNoAnnotatedTags, + }, + { + name: "2 commits, 1 tag, default template", + args: args{ + newTag: annotatedTag{Name: "v0.7.6", Message: "this is the new tag msg"}, + repo: func() *git.Repository { + os.RemoveAll(workspace) + workspace = t.TempDir() + return mustCreateRepo(workspace, + []gitObj{ + {msg: "foo", timestamp: dummyTimestamp}, + {msg: "bar", timestamp: dummyTimestamp}, + }, + []gitObj{ + { + msg: "this is my tag msg", + name: "v0.7.5", + timestamp: dummyTimestamp, + }, + }) + }, + template: defaultTemplate, + }, + want: "## v0.7.6\n**this is the new tag msg**\n\n* bar\n* foo\n\n", + wantErr: nil, + }, + { + name: "3 commits,2 tags, custom template", + args: args{ + newTag: annotatedTag{Name: "v0.7.6", Message: "this is the new tag msg"}, + repo: func() *git.Repository { + os.RemoveAll(workspace) + workspace = t.TempDir() + return mustCreateRepo(workspace, + []gitObj{ + {msg: "foo", timestamp: dummyTimestamp}, + {msg: "bar", timestamp: dummyTimestamp}, + {msg: "baz", timestamp: dummyTimestamp}, + }, []gitObj{ + { + msg: "this is my tag msg", + name: "v0.7.1", + timestamp: dummyTimestamp, + tagCommitNum: 0, + }, + { + msg: "this is my other tag msg", + name: "v0.7.5", + timestamp: "2024-01-19T18:09:06Z", + tagCommitNum: 1, + }, + }) + + }, + template: "{{.Tag}}", + }, + + want: "v0.7.6", + wantErr: nil, + }, + { + name: "3 commits,2 tags, HEAD is on the latest tag so we are generating a changelog for the previous", + args: args{ + newTag: annotatedTag{}, + repo: func() *git.Repository { + os.RemoveAll(workspace) + workspace = t.TempDir() + return mustCreateRepo(workspace, + []gitObj{ + {msg: "foo", timestamp: dummyTimestamp}, + {msg: "bar", timestamp: dummyTimestamp}, + {msg: "baz", timestamp: dummyTimestamp}, + }, []gitObj{ + { + msg: "this is my other tag msg", + name: "v0.7.5", + timestamp: "2024-01-19T18:09:06Z", + tagCommitNum: 1, + }, + { + msg: "this is my HEAD tag msg", + name: "v0.7.7", + timestamp: "2024-02-19T18:09:06Z", + tagCommitNum: 2, + }, + }) + + }, + template: "{{.Tag}}", + }, + + want: "v0.7.7", + wantErr: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := generateChangelog(tt.args.repo(), tt.args.newTag, tt.args.template) + if (err != nil) && err != tt.wantErr { + t.Errorf("generateChangelog() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("generateChangelog() = '%v', want '%v'", got, tt.want) + } + }) + } + os.RemoveAll(workspace) +} + +// func Test_main(t *testing.T) { +// tests := []struct { +// name string +// }{ +// // TODO: Add test cases. +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// main() +// }) +// } +// }