From 41a5c57d565fe454b8d39e2eb2b64148db308b6a Mon Sep 17 00:00:00 2001 From: Linus Oleander <220827+oleander@users.noreply.github.com> Date: Tue, 27 May 2025 23:22:17 +0200 Subject: [PATCH] Migrate from structopt to clap v4 with derive macros --- Cargo.lock | 339 ++++++++++++++++++++---------------------------- Cargo.toml | 2 +- src/bin/hook.rs | 36 ++--- src/hook.rs | 11 +- src/main.rs | 46 +++---- 5 files changed, 182 insertions(+), 252 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aef2369..81e55ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,12 +27,53 @@ dependencies = [ ] [[package]] -name = "ansi_term" -version = "0.12.1" +name = "anstream" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" dependencies = [ - "winapi", + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6680de5231bd6ee4c6191b8a1325daa282b415391ec9d3a37bd34f2060dc73fa" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.59.0", ] [[package]] @@ -78,7 +119,7 @@ checksum = "0289cba6d5143bfe8251d57b4a8cac036adf158525a76533a7082ba65ec76398" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -87,17 +128,6 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.4.0" @@ -154,12 +184,6 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.9.1" @@ -208,19 +232,50 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "2.34.0" +version = "4.5.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +checksum = "fd60e63e9be68e5fb56422e397cf9baddded06dae1d2e523401542383bc72a9f" dependencies = [ - "ansi_term", - "atty", - "bitflags 1.3.2", - "strsim 0.8.0", - "textwrap 0.11.0", - "unicode-width 0.1.14", - "vec_map", + "clap_builder", + "clap_derive", ] +[[package]] +name = "clap_builder" +version = "4.5.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89cc6392a1f72bbeb820d71f32108f61fdaf18bc526e1d23954168a67759ef51" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "colored" version = "3.0.0" @@ -341,8 +396,8 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim 0.11.1", - "syn 2.0.101", + "strsim", + "syn", ] [[package]] @@ -353,7 +408,7 @@ checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" dependencies = [ "darling_core", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -374,7 +429,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -384,7 +439,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab63b0e2bf4d5928aff72e83a7dace85d7bba5fe12dcc3c5a572d78caffd3f3c" dependencies = [ "derive_builder_core", - "syn 2.0.101", + "syn", ] [[package]] @@ -416,7 +471,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -604,7 +659,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -674,10 +729,11 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "git-ai" -version = "1.0.5" +version = "1.0.6" dependencies = [ "anyhow", "async-openai", + "clap", "colored", "config", "console", @@ -702,9 +758,8 @@ dependencies = [ "serde_derive", "serde_ini", "serde_json", - "structopt", "tempfile", - "textwrap 0.16.2", + "textwrap", "thiserror 2.0.12", "tiktoken-rs", "tokio", @@ -717,7 +772,7 @@ version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2deb07a133b1520dc1a5690e9bd08950108873d7ed5de38dcc74d3b5ebffa110" dependencies = [ - "bitflags 2.9.1", + "bitflags", "libc", "libgit2-sys", "log 0.4.27", @@ -757,21 +812,9 @@ checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" [[package]] name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - -[[package]] -name = "hermit-abi" -version = "0.1.19" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" @@ -1066,6 +1109,12 @@ dependencies = [ "serde", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itoa" version = "1.0.15" @@ -1122,7 +1171,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags 2.9.1", + "bitflags", "libc", ] @@ -1272,7 +1321,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi 0.3.9", + "hermit-abi", "libc", ] @@ -1297,13 +1346,19 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad" + [[package]] name = "openssl" version = "0.10.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fedfea7d58a1f73118430a55da6a286e7b044961736ce96a16a17068ea25e5da" dependencies = [ - "bitflags 2.9.1", + "bitflags", "cfg-if", "foreign-types", "libc", @@ -1320,7 +1375,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -1444,30 +1499,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.95" @@ -1577,7 +1608,7 @@ version = "0.5.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "928fca9cf2aa042393a8325b9ead81d2f0df4cb12e1e24cef072922ccd99c5af" dependencies = [ - "bitflags 2.9.1", + "bitflags", ] [[package]] @@ -1731,7 +1762,7 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" dependencies = [ - "bitflags 2.9.1", + "bitflags", "errno", "libc", "linux-raw-sys", @@ -1814,7 +1845,7 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" dependencies = [ - "bitflags 2.9.1", + "bitflags", "core-foundation", "core-foundation-sys", "libc", @@ -1848,7 +1879,7 @@ checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -1938,59 +1969,18 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "structopt" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" -dependencies = [ - "clap", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "subtle" version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.101" @@ -2019,7 +2009,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -2028,7 +2018,7 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" dependencies = [ - "bitflags 2.9.1", + "bitflags", "core-foundation", "system-configuration-sys", ] @@ -2056,15 +2046,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width 0.1.14", -] - [[package]] name = "textwrap" version = "0.16.2" @@ -2073,7 +2054,7 @@ checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" dependencies = [ "smawk", "unicode-linebreak", - "unicode-width 0.2.0", + "unicode-width", ] [[package]] @@ -2102,7 +2083,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -2113,7 +2094,7 @@ checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -2176,7 +2157,7 @@ checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -2244,7 +2225,7 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fdb0c213ca27a9f57ab69ddb290fd80d970922355b83ae380b395d3986b8a2e" dependencies = [ - "bitflags 2.9.1", + "bitflags", "bytes", "futures-util", "http", @@ -2287,7 +2268,7 @@ checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -2329,18 +2310,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" -[[package]] -name = "unicode-segmentation" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" - -[[package]] -name = "unicode-width" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" - [[package]] name = "unicode-width" version = "0.2.0" @@ -2371,22 +2340,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - -[[package]] -name = "vec_map" -version = "0.8.2" +name = "utf8parse" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] -name = "version_check" -version = "0.9.5" +name = "vcpkg" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] name = "void" @@ -2440,7 +2403,7 @@ dependencies = [ "log 0.4.27", "proc-macro2", "quote", - "syn 2.0.101", + "syn", "wasm-bindgen-shared", ] @@ -2475,7 +2438,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -2522,28 +2485,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - [[package]] name = "windows-link" version = "0.1.1" @@ -2740,7 +2681,7 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" dependencies = [ - "bitflags 2.9.1", + "bitflags", ] [[package]] @@ -2769,7 +2710,7 @@ checksum = "38da3c9736e16c5d3c8c597a9aaa5d1fa565d0532ae05e27c24aa62fb32c0ab6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", "synstructure", ] @@ -2790,7 +2731,7 @@ checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] [[package]] @@ -2810,7 +2751,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", "synstructure", ] @@ -2850,5 +2791,5 @@ checksum = "5b96237efa0c878c64bd89c436f661be4e46b2f3eff1ebb976f7ef2321d2f58f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.101", + "syn", ] diff --git a/Cargo.toml b/Cargo.toml index 1aeb208..81c849b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,7 +79,7 @@ dirs = "6.0" # Syntax highlighting and markdown rendering textwrap = "0.16" -structopt = "0.3.26" +clap = { version = "4.5", features = ["derive"] } mustache = "0.9.0" maplit = "1.0.2" diff --git a/src/bin/hook.rs b/src/bin/hook.rs index 6630c56..47f934c 100644 --- a/src/bin/hook.rs +++ b/src/bin/hook.rs @@ -42,12 +42,11 @@ // Args { commit_msg_file: PathBuf::from(".git/COMMIT_EDITMSG"), source: Some(Source::Commit), sha1: Some("HEAD") } // Outcome: Opens the default text editor to allow modification of the most recent commit message. No new commit message is generated automatically; it depends on user input. use std::process::exit; -use std::str::FromStr; use std::time::Duration; use std::path::PathBuf; use colored::Colorize; -use structopt::StructOpt; +use clap::{Parser, ValueEnum}; use indicatif::{ProgressBar, ProgressStyle}; use anyhow::{bail, Context, Result}; use git2::{Oid, Repository}; @@ -55,7 +54,7 @@ use ai::{commit, config, debug_output}; use ai::hook::*; use ai::model::Model; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone, ValueEnum)] enum Source { Message, Template, @@ -64,28 +63,17 @@ enum Source { Commit } -impl FromStr for Source { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - match s { - "message" => Ok(Source::Message), - "template" => Ok(Source::Template), - "merge" => Ok(Source::Merge), - "squash" => Ok(Source::Squash), - "commit" => Ok(Source::Commit), - other => bail!("{:?} is not a valid source", other) - } - } -} - -#[derive(StructOpt, Debug)] -#[structopt(name = "commit-msg-hook")] +#[derive(Parser, Debug)] +#[command(name = "commit-msg-hook", about = "A tool for generating commit messages.")] struct Args { - #[structopt(parse(from_os_str))] + #[arg(value_name = "COMMIT_MSG_FILE")] commit_msg_file: PathBuf, - source: Option, - sha1: Option + + #[arg(short = 't', long = "type", value_name = "SOURCE")] + source: Option, + + #[arg(short = 's', long = "sha1", value_name = "SHA1")] + sha1: Option } impl Args { @@ -221,7 +209,7 @@ async fn main() -> Result<()> { } let time = std::time::Instant::now(); - let args = Args::from_args(); + let args = Args::parse(); // Initialize debug session if in debug mode if log::log_enabled!(log::Level::Debug) { diff --git a/src/hook.rs b/src/hook.rs index c80116f..a43018c 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use rayon::prelude::*; -use structopt::StructOpt; +use clap::Parser; use git2::{Diff, DiffFormat, DiffOptions, Repository, Tree}; use anyhow::{Context, Result}; use thiserror::Error; @@ -45,15 +45,16 @@ pub enum HookError { } // CLI Arguments -#[derive(StructOpt, Debug)] -#[structopt(name = "commit-msg-hook", about = "A tool for generating commit messages.")] +#[derive(Parser, Debug)] +#[command(name = "commit-msg-hook", about = "A tool for generating commit messages.")] pub struct Args { + #[arg(value_name = "COMMIT_MSG_FILE")] pub commit_msg_file: PathBuf, - #[structopt(short = "t", long = "type")] + #[arg(short = 't', long = "type", value_name = "TYPE")] pub commit_type: Option, - #[structopt(short = "s", long = "sha1")] + #[arg(short = 's', long = "sha1", value_name = "SHA1")] pub sha1: Option } diff --git a/src/main.rs b/src/main.rs index 93bcf91..6a2708a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,68 +1,68 @@ mod config; mod filesystem; -use structopt::StructOpt; +use clap::{Parser, Subcommand}; use anyhow::Result; use dotenv::dotenv; use crate::config::App; use crate::filesystem::Filesystem; -#[derive(StructOpt)] -#[structopt(name = "git-ai", about = "A git extension that uses OpenAI to generate commit messages")] +#[derive(Parser)] +#[command(name = "git-ai", about = "A git extension that uses OpenAI to generate commit messages")] enum Cli { - #[structopt(about = "Installs the git-ai hook")] + #[command(subcommand)] Hook(HookSubcommand), - #[structopt(about = "Sets or gets configuration values")] + #[command(subcommand)] Config(ConfigSubcommand) } -#[derive(StructOpt)] +#[derive(Subcommand)] enum HookSubcommand { - #[structopt(about = "Installs the git-ai hook")] + #[command(about = "Installs the git-ai hook")] Install, - #[structopt(about = "Uninstalls the git-ai hook")] + #[command(about = "Uninstalls the git-ai hook")] Uninstall, - #[structopt(about = "Reinstalls the git-ai hook")] + #[command(about = "Reinstalls the git-ai hook")] Reinstall } -#[derive(StructOpt)] +#[derive(Subcommand)] enum ConfigSubcommand { - #[structopt(about = "Sets a configuration value")] + #[command(subcommand)] Set(SetSubcommand), - #[structopt(about = "Resets the internal configuration to the default values")] + #[command(about = "Resets the internal configuration to the default values")] Reset } -#[derive(StructOpt)] +#[derive(Subcommand)] enum SetSubcommand { - #[structopt(about = "Sets the model to use")] + #[command(about = "Sets the model to use")] Model(Model), - #[structopt(about = "Sets the maximum number of tokens to use for the diff")] + #[command(about = "Sets the maximum number of tokens to use for the diff")] MaxTokens { - #[structopt(help = "The maximum number of tokens", name = "max-tokens")] + #[arg(help = "The maximum number of tokens", value_name = "max-tokens")] max_tokens: usize }, - #[structopt(about = "Sets the maximum length of the commit message")] + #[command(about = "Sets the maximum length of the commit message")] MaxCommitLength { - #[structopt(help = "The maximum length of the commit message", name = "max-commit-length")] + #[arg(help = "The maximum length of the commit message", value_name = "max-commit-length")] max_commit_length: usize }, - #[structopt(about = "Sets the OpenAI API key")] + #[command(about = "Sets the OpenAI API key")] OpenaiApiKey { - #[structopt(help = "The OpenAI API key", name = "VALUE")] + #[arg(help = "The OpenAI API key", value_name = "VALUE")] value: String } } -#[derive(StructOpt)] +#[derive(Parser)] struct Model { - #[structopt(help = "The value to set", name = "VALUE")] + #[arg(help = "The value to set", value_name = "VALUE")] value: String } @@ -161,7 +161,7 @@ async fn main() -> Result<()> { println!("Debug build: Performance profiling enabled"); } - let args = Cli::from_args(); + let args = Cli::parse(); match args { Cli::Hook(sub) =>