Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
ec34a47faf | |||
85d80db364 | |||
4f44862160 | |||
25d43765d9 | |||
88680da7d5 | |||
2a46b7b170 | |||
f9513fe051 | |||
09cfa5b88f | |||
88b602a124 | |||
674266664d | |||
39dc4ae15c | |||
55a5a9a1e1 | |||
92a0e3b1b3 | |||
b167b3ebfa | |||
b78631d6ac | |||
00a249978e |
18
CHANGELOG.md
18
CHANGELOG.md
@ -6,6 +6,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [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
|
||||
|
102
Cargo.lock
generated
102
Cargo.lock
generated
@ -60,6 +60,12 @@ dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
@ -68,18 +74,18 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.23"
|
||||
version = "4.5.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84"
|
||||
checksum = "769b0145982b4b48713e01ec42d61614425f27b7058bda7180a3a41f30104796"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.23"
|
||||
version = "4.5.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838"
|
||||
checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
@ -123,6 +129,27 @@ dependencies = [
|
||||
"powerfmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"option-ext",
|
||||
"redox_users",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "1.0.0"
|
||||
@ -139,6 +166,17 @@ dependencies = [
|
||||
"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]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
@ -159,10 +197,11 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
|
||||
|
||||
[[package]]
|
||||
name = "kignore"
|
||||
version = "0.2.0"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"console",
|
||||
"dirs",
|
||||
"eyre",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
@ -180,6 +219,16 @@ version = "0.2.169"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "log"
|
||||
version = "0.4.22"
|
||||
@ -232,6 +281,12 @@ version = "1.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||
|
||||
[[package]]
|
||||
name = "option-ext"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
@ -268,6 +323,17 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"libredox",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
@ -364,6 +430,26 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "2.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.8"
|
||||
@ -500,6 +586,12 @@ version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
|
11
Cargo.toml
11
Cargo.toml
@ -3,4 +3,13 @@ members = ["crates/*"]
|
||||
resolver = "2"
|
||||
|
||||
[workspace.package]
|
||||
version = "0.2.3"
|
||||
version = "0.4.0"
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
debug = false
|
||||
panic = "abort"
|
||||
overflow-checks = false
|
||||
strip = true
|
||||
|
24
README.md
24
README.md
@ -27,24 +27,22 @@ Cargo will only pull the `kignore` command and won't add a subcommand to `git.
|
||||
|
||||
```bash
|
||||
$ 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
|
||||
|
||||
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-alias/git-ignore` as a template or:
|
||||
git-ignore available on your path
|
||||
|
||||
```
|
||||
git clone https://github.com/kjuulh/gitignore
|
||||
./scripts/install-git-alias.sh # only tested on mac and linux
|
||||
```
|
||||
|
||||
### Homebrew
|
||||
|
||||
Added in HomebrewFormula
|
||||
|
||||
```bash
|
||||
$ brew tap kjuulh/gitignore https://github.com/kjuulh/gitignore
|
||||
$ brew install kjuulh/gitignore/kignore-bin
|
||||
# zsh
|
||||
eval "kignore init zsh"
|
||||
|
||||
# shell
|
||||
eval "kignore init sh"
|
||||
|
||||
# bash
|
||||
eval "kignore init bash"
|
||||
```
|
||||
|
@ -15,9 +15,11 @@ edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.5.4", features = ["env", "unicode", "string"] }
|
||||
console = "0.15.8"
|
||||
dirs = "6.0.0"
|
||||
eyre = "0.6.12"
|
||||
tracing = { version = "0.1.40", features = ["log"] }
|
||||
tracing-subscriber = { version = "0.3.18", features = [
|
||||
|
@ -1,28 +1,76 @@
|
||||
use clap::{Arg, Command};
|
||||
use eyre::{Context, ContextCompat};
|
||||
use console::style;
|
||||
use eyre::{Context, ContextCompat, OptionExt};
|
||||
use std::io::prelude::*;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::{env::current_dir, io::Read, path::PathBuf};
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
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<()> {
|
||||
let matches = Command::new("gitignore")
|
||||
.version("0.1")
|
||||
.author("Kasper J. Hermansen <contact@kjuulh.io>")
|
||||
.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.
|
||||
Easily add patterns using `git ignore <pattern>` this will by default also help you remove committed code violating these patterns
|
||||
")
|
||||
.long_about(
|
||||
"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)
|
||||
.arg(
|
||||
Arg::new("pattern")
|
||||
.help("the pattern you want to ignore")
|
||||
.long_help("the pattern you want to ignore in the nearest .gitignore file")
|
||||
.required(true),
|
||||
).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]"))
|
||||
.long_help("the pattern you want to ignore in the nearest .gitignore file"),
|
||||
)
|
||||
.arg(
|
||||
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();
|
||||
|
||||
match matches.subcommand() {
|
||||
Some(("init", args)) => match args
|
||||
.subcommand()
|
||||
.expect("should never be able to call on init")
|
||||
{
|
||||
("zsh", _) => init_script(ShellType::Zsh),
|
||||
("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",
|
||||
@ -47,6 +95,8 @@ Easily add patterns using `git ignore <pattern>` this will by default also help
|
||||
.context("missing [pattern]")?;
|
||||
|
||||
add_gitignore_pattern(term, pattern)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum GitActions {
|
||||
@ -55,7 +105,7 @@ enum GitActions {
|
||||
}
|
||||
|
||||
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(
|
||||
"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 {
|
||||
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_content = String::new();
|
||||
gitignore_file
|
||||
@ -85,22 +135,26 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
|
||||
gitignore_path.to_string_lossy()
|
||||
))?;
|
||||
if gitignore_content.contains(pattern) {
|
||||
term.write_line(".gitignore already contains pattern, skipping")?;
|
||||
println!(
|
||||
".gitignore already contains pattern, {}",
|
||||
style("skipping...").blue()
|
||||
);
|
||||
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")?;
|
||||
gitignore_file
|
||||
.sync_all()
|
||||
.context("failed to write data to disk")?;
|
||||
}
|
||||
GitActions::CreateIgnoreAndAddPattern { git_path } => {
|
||||
term.write_line(
|
||||
"could not find .gitignore file, creating one in the root of the git repository",
|
||||
)?;
|
||||
println!(
|
||||
"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)?;
|
||||
term.write_line("adding pattern to file")?;
|
||||
println!("adding pattern to file");
|
||||
writeln!(gitignore_file, "{}", pattern).context("could not write contents to file")?;
|
||||
gitignore_file
|
||||
.sync_all()
|
||||
@ -121,8 +175,19 @@ fn add_gitignore_pattern(term: console::Term, pattern: &String) -> eyre::Result<
|
||||
String::from_utf8(output.stdout)?
|
||||
.lines()
|
||||
.chain(String::from_utf8(output.stderr)?.lines())
|
||||
.try_for_each(|l| term.write_line(l))
|
||||
.context("could not print all output to terminal")?;
|
||||
.map(|l| {
|
||||
// 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() {
|
||||
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)
|
||||
}
|
||||
|
||||
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(())
|
||||
}
|
||||
|
@ -6,6 +6,9 @@ vars:
|
||||
service: "kignore"
|
||||
registry: kasperhermansen
|
||||
|
||||
rust:
|
||||
publish: {}
|
||||
|
||||
please:
|
||||
project:
|
||||
owner: kjuulh
|
||||
|
Loading…
Reference in New Issue
Block a user