11 Commits

Author SHA1 Message Date
674266664d chore(release): v0.4.0 (#90)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.4.0

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/gitignore/pulls/90
2024-12-22 14:53:58 +01:00
39dc4ae15c chore: make variant for the other shell commands
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-12-22 14:52:05 +01:00
55a5a9a1e1 feat: make init zsh
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-12-22 13:33:46 +01:00
92a0e3b1b3 chore(release): v0.3.0 (#89)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.3.0

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/gitignore/pulls/89
2024-12-22 13:01:48 +01:00
b167b3ebfa feat: make cli look nice
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-12-22 13:00:58 +01:00
b78631d6ac chore(release): v0.2.4 (#88)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore(release): 0.2.4

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/gitignore/pulls/88
2024-12-22 12:44:31 +01:00
00a249978e chore: trigger commit
All checks were successful
continuous-integration/drone/push Build is passing
2024-12-22 12:43:57 +01:00
a86a4197f7 chore(release): v0.2.3 (#87)
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is failing
chore(release): 0.2.3

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/gitignore/pulls/87
2024-12-22 12:37:08 +01:00
0397b64e28 chore: trigger commit
All checks were successful
continuous-integration/drone/push Build is passing
2024-12-22 12:36:39 +01:00
77901c6ac9 chore(release): v0.2.2 (#86)
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is failing
chore(release): 0.2.2

Co-authored-by: cuddle-please <bot@cuddle.sh>
Reviewed-on: https://git.front.kjuulh.io/kjuulh/gitignore/pulls/86
2024-12-22 12:34:09 +01:00
a1eadd67e1 feat: trigger commit
All checks were successful
continuous-integration/drone/push Build is passing
2024-12-22 12:33:24 +01:00
6 changed files with 361 additions and 64 deletions

View File

@@ -6,6 +6,34 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [0.4.0] - 2024-12-22
### Added
- make init zsh
### Other
- make variant for the other shell commands
## [0.3.0] - 2024-12-22
### Added
- make cli look nice
## [0.2.4] - 2024-12-22
### Other
- trigger commit
## [0.2.3] - 2024-12-22
### Other
- trigger commit
## [0.2.2] - 2024-12-22
### Added
- trigger commit
## [0.2.1] - 2024-12-22 ## [0.2.1] - 2024-12-22
### Other ### Other

182
Cargo.lock generated
View File

@@ -47,7 +47,7 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [ dependencies = [
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@@ -57,9 +57,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@@ -111,7 +117,7 @@ dependencies = [
"libc", "libc",
"once_cell", "once_cell",
"unicode-width", "unicode-width",
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@@ -123,6 +129,27 @@ dependencies = [
"powerfmt", "powerfmt",
] ]
[[package]]
name = "dirs"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "encode_unicode" name = "encode_unicode"
version = "1.0.0" version = "1.0.0"
@@ -139,6 +166,17 @@ dependencies = [
"once_cell", "once_cell",
] ]
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]] [[package]]
name = "indenter" name = "indenter"
version = "0.3.3" version = "0.3.3"
@@ -159,10 +197,11 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]] [[package]]
name = "kignore" name = "kignore"
version = "0.2.0" version = "0.3.0"
dependencies = [ dependencies = [
"clap", "clap",
"console", "console",
"dirs",
"eyre", "eyre",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@@ -180,6 +219,16 @@ version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "libredox"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
dependencies = [
"bitflags",
"libc",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.22" version = "0.4.22"
@@ -232,6 +281,12 @@ version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]] [[package]]
name = "overload" name = "overload"
version = "0.1.1" version = "0.1.1"
@@ -268,6 +323,17 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "redox_users"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
dependencies = [
"getrandom",
"libredox",
"thiserror",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.11.1" version = "1.11.1"
@@ -364,6 +430,26 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "thiserror"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "thread_local" name = "thread_local"
version = "1.1.8" version = "1.1.8"
@@ -500,6 +586,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"
@@ -522,13 +614,37 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.59.0" version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [ dependencies = [
"windows-targets", "windows-targets 0.52.6",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
] ]
[[package]] [[package]]
@@ -537,28 +653,46 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.52.6",
"windows_i686_gnu", "windows_i686_gnu 0.52.6",
"windows_i686_gnullvm", "windows_i686_gnullvm",
"windows_i686_msvc", "windows_i686_msvc 0.52.6",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc", "windows_x86_64_msvc 0.52.6",
] ]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.52.6" version = "0.52.6"
@@ -571,24 +705,48 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"

View File

@@ -3,4 +3,4 @@ members = ["crates/*"]
resolver = "2" resolver = "2"
[workspace.package] [workspace.package]
version = "0.2.1" version = "0.4.0"

View File

@@ -27,24 +27,22 @@ Cargo will only pull the `kignore` command and won't add a subcommand to `git.
```bash ```bash
$ cargo install kignore $ cargo install kignore
$ cargo binstall kignore # binstall will warn that git.front.kjuulh.io isn't a valid repo, it is still installable though
``` ```
#### Post install #### Post install
To get the `git ignore` subcommand working you will need to have the file To get the `git ignore` subcommand working you will need to have the file
git-ignore available on your path, either add it yourself using git-ignore available on your path
`git-alias/git-ignore` as a template or:
``` ```
git clone https://github.com/kjuulh/gitignore # zsh
./scripts/install-git-alias.sh # only tested on mac and linux eval "kignore init zsh"
```
# shell
### Homebrew eval "kignore init sh"
Added in HomebrewFormula # bash
eval "kignore init bash"
```bash
$ brew tap kjuulh/gitignore https://github.com/kjuulh/gitignore
$ brew install kjuulh/gitignore/kignore-bin
``` ```

View File

@@ -18,6 +18,7 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.4", features = ["env", "unicode", "string"] } clap = { version = "4.5.4", features = ["env", "unicode", "string"] }
console = "0.15.8" console = "0.15.8"
dirs = "5.0.1"
eyre = "0.6.12" eyre = "0.6.12"
tracing = { version = "0.1.40", features = ["log"] } tracing = { version = "0.1.40", features = ["log"] }
tracing-subscriber = { version = "0.3.18", features = [ tracing-subscriber = { version = "0.3.18", features = [

View File

@@ -1,52 +1,102 @@
use clap::{Arg, Command}; use clap::{Arg, Command};
use eyre::{Context, ContextCompat}; use console::style;
use eyre::{Context, ContextCompat, OptionExt};
use std::io::prelude::*; use std::io::prelude::*;
use std::os::unix::fs::PermissionsExt;
use std::{env::current_dir, io::Read, path::PathBuf}; use std::{env::current_dir, io::Read, path::PathBuf};
use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::util::SubscriberInitExt;
const ZSH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env zsh
set -e
kignore $@
";
const SH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env sh
set -e
kignore $@
";
const BASH_FILE_CONTENTS: &[u8] = b"#!/usr/bin/env bash
set -e
kignore $@
";
pub fn main() -> eyre::Result<()> { pub fn main() -> eyre::Result<()> {
let matches = Command::new("gitignore") let matches = Command::new("gitignore")
.version("0.1") .version("0.1")
.author("Kasper J. Hermansen <contact@kjuulh.io>") .author("Kasper J. Hermansen <contact@kjuulh.io>")
.about("Easily ignore items and remove from git state") .about("Easily ignore items and remove from git state")
.long_about("git ignore is a utility tool for easily adding patterns to your .gitignore file. .long_about(
Easily add patterns using `git ignore <pattern>` this will by default also help you remove committed code violating these patterns "git ignore is a utility tool for easily adding patterns to your .gitignore file.
") Easily add patterns using `git ignore <pattern>` this will by default
also help you remove committed code violating these patterns
",
)
.propagate_version(true) .propagate_version(true)
.arg( .arg(
Arg::new("pattern") Arg::new("pattern")
.help("the pattern you want to ignore") .help("the pattern you want to ignore")
.long_help("the pattern you want to ignore in the nearest .gitignore file") .long_help("the pattern you want to ignore in the nearest .gitignore file"),
.required(true), )
).arg( .arg(
Arg::new("log-level").long("log-level").help("choose a log level and get more messages").long_help("Choose a log level and get more message, defaults to [fatal]")) Arg::new("log-level")
.long("log-level")
.default_value("warn")
.help("choose a log level and get more messages")
.long_help("Choose a log level and get more message, defaults to [warn]"),
)
.subcommand(
clap::Command::new("init")
.subcommand_required(true)
.subcommand(Command::new("zsh"))
.subcommand(Command::new("sh"))
.subcommand(Command::new("bash")),
)
.get_matches(); .get_matches();
let log_level = match matches.get_one::<String>("log-level").map(|f| f.as_str()) { match matches.subcommand() {
Some("off") => "off", Some(("init", args)) => match args
Some("info") => "info", .subcommand()
Some("debug") => "debug", .expect("should never be able to call on init")
Some("warn") => "warn", {
Some("error") => "error", ("zsh", _) => init_script(ShellType::Zsh),
_ => "error", ("bash", _) => init_script(ShellType::Bash),
}; ("sh", _) => init_script(ShellType::Shell),
(subcommand, _) => {
panic!("cannot call on subcommand: {}", subcommand);
}
},
_ => {
let log_level = match matches.get_one::<String>("log-level").map(|f| f.as_str()) {
Some("off") => "off",
Some("info") => "info",
Some("debug") => "debug",
Some("warn") => "warn",
Some("error") => "error",
_ => "error",
};
tracing_subscriber::registry() tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(format!( .with(tracing_subscriber::EnvFilter::new(format!(
"gitignore={}", "gitignore={}",
log_level log_level
))) )))
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
let term = console::Term::stdout(); let term = console::Term::stdout();
let pattern = matches let pattern = matches
.get_one::<String>("pattern") .get_one::<String>("pattern")
.context("missing [pattern]")?; .context("missing [pattern]")?;
add_gitignore_pattern(term, pattern) add_gitignore_pattern(term, pattern)
}
}
} }
enum GitActions { enum GitActions {
@@ -55,7 +105,7 @@ enum GitActions {
} }
fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<()> { fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<()> {
term.write_line("git ignore: Add pattern")?; println!("git ignore: Add pattern");
let curdir = current_dir().context( let curdir = current_dir().context(
"could not find current_dir, you may not have permission to access that directory", "could not find current_dir, you may not have permission to access that directory",
)?; )?;
@@ -75,7 +125,7 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
match actions { match actions {
GitActions::AddPattern { gitignore_path } => { GitActions::AddPattern { gitignore_path } => {
term.write_line("Found existing .gitignore")?; println!("Found existing {}", style(".gitignore").green());
let mut gitignore_file = open_gitignore_file(&gitignore_path)?; let mut gitignore_file = open_gitignore_file(&gitignore_path)?;
let mut gitignore_content = String::new(); let mut gitignore_content = String::new();
gitignore_file gitignore_file
@@ -85,22 +135,26 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
gitignore_path.to_string_lossy() gitignore_path.to_string_lossy()
))?; ))?;
if gitignore_content.contains(pattern) { if gitignore_content.contains(pattern) {
term.write_line(".gitignore already contains pattern, skipping")?; println!(
".gitignore already contains pattern, {}",
style("skipping...").blue()
);
return Ok(()); return Ok(());
} }
term.write_line("adding pattern to file")?; println!("adding pattern to file");
writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?; writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?;
gitignore_file gitignore_file
.sync_all() .sync_all()
.context("failed to write data to disk")?; .context("failed to write data to disk")?;
} }
GitActions::CreateIgnoreAndAddPattern { git_path } => { GitActions::CreateIgnoreAndAddPattern { git_path } => {
term.write_line( println!(
"could not find .gitignore file, creating one in the root of the git repository", "could not find {} file, creating one in the root of the git repository",
)?; style(".gitignore").yellow()
);
let mut gitignore_file = create_gitignore_file(git_path)?; let mut gitignore_file = create_gitignore_file(git_path)?;
term.write_line("adding pattern to file")?; println!("adding pattern to file");
writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?; writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?;
gitignore_file gitignore_file
.sync_all() .sync_all()
@@ -121,8 +175,19 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
String::from_utf8(output.stdout)? String::from_utf8(output.stdout)?
.lines() .lines()
.chain(String::from_utf8(output.stderr)?.lines()) .chain(String::from_utf8(output.stderr)?.lines())
.try_for_each(|l| term.write_line(l)) .map(|l| {
.context("could not print all output to terminal")?; // make rm 'path' look nice
if l.contains("rm") {
if let Some((_, pruned_first)) = l.split_once("'") {
if let Some((content, _)) = pruned_first.rsplit_once("'") {
return content;
}
}
}
l
})
.for_each(|l| println!("removed from git history: {}", style(l).yellow()));
if !output.status.success() { if !output.status.success() {
return Err(eyre::anyhow!("failed to run git index command")); return Err(eyre::anyhow!("failed to run git index command"));
@@ -235,3 +300,50 @@ fn search_for_dotgitignore(path: &PathBuf) -> eyre::Result<GitSearchResult> {
search_for_dotgitignore(&upwards_par) search_for_dotgitignore(&upwards_par)
} }
enum ShellType {
Bash,
Shell,
Zsh,
}
fn init_script(shell: ShellType) -> eyre::Result<()> {
let bin_dir = dirs::executable_dir().ok_or_eyre("failed to find executable dir")?;
let script = match shell {
ShellType::Bash => BASH_FILE_CONTENTS,
ShellType::Shell => SH_FILE_CONTENTS,
ShellType::Zsh => ZSH_FILE_CONTENTS,
};
let alias_script = bin_dir.join("git-ignore");
if let Ok(existing_file) = std::fs::read(&alias_script) {
if existing_file == script {
return Ok(());
}
} else {
std::fs::create_dir_all(&bin_dir).context("failed to create bin dir")?;
}
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&alias_script)?;
file.write_all(script)?;
file.flush()?;
// Set the file to be executable
let metadata = file.metadata()?;
let mut permissions = metadata.permissions();
permissions.set_mode(0o755); // rwxr-xr-x
file.set_permissions(permissions)?;
println!(
"successfully wrote alias to {}",
style(alias_script.display()).green()
);
Ok(())
}