mirror of
https://github.com/Cian-H/dotfiles.git
synced 2025-12-26 20:51:56 +00:00
Changed . token to _dot
This change allows the dotfiles to work with chezmoi (e.g: on windows) and improves grepability with neovim/telescope
This commit is contained in:
14
dot_config/nushell/nu_scripts/modules/git/README.md
Normal file
14
dot_config/nushell/nu_scripts/modules/git/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Git Scripts
|
||||
|
||||
### Definition
|
||||
|
||||
These scripts should be used to demonstrate how combine the power of nushell with git commands.
|
||||
|
||||
### Aliases
|
||||
If you find some of these scripts useful you can add them as aliases in your git config file.
|
||||
|
||||
For instance you can create a nushell script in your computer and define in your git config file an alias that call that script:
|
||||
```
|
||||
[alias]
|
||||
bcl = !nu \"D:\\Tools\\gitalias_bcl.nu\"
|
||||
```
|
||||
146
dot_config/nushell/nu_scripts/modules/git/git.nu
Normal file
146
dot_config/nushell/nu_scripts/modules/git/git.nu
Normal file
@@ -0,0 +1,146 @@
|
||||
export def _git_stat [n] {
|
||||
do -i {
|
||||
git log -n $n --pretty=»¦«%h --stat
|
||||
| lines
|
||||
| reduce -f { c: '', r: [] } {|it, acc|
|
||||
if ($it | str starts-with '»¦«') {
|
||||
$acc | upsert c ($it | str substring 6.. )
|
||||
} else if ($it | find -r '[0-9]+ file.+change' | is-empty) {
|
||||
$acc
|
||||
} else {
|
||||
let x = (
|
||||
$it
|
||||
| split row ','
|
||||
| each {|x| $x
|
||||
| str trim
|
||||
| parse -r "(?P<num>[0-9]+) (?P<col>.+)"
|
||||
| get 0
|
||||
}
|
||||
| reduce -f {sha: $acc.c file:0 ins:0 del:0} {|i,a|
|
||||
let col = if ($i.col | str starts-with 'file') {
|
||||
'file'
|
||||
} else {
|
||||
$i.col | str substring ..3
|
||||
}
|
||||
let num = ($i.num | into int)
|
||||
$a | upsert $col $num
|
||||
}
|
||||
)
|
||||
$acc | upsert r ($acc.r | append $x)
|
||||
}
|
||||
}
|
||||
| get r
|
||||
}
|
||||
}
|
||||
|
||||
export def _git_log [v num] {
|
||||
let stat = if $v {
|
||||
_git_stat $num
|
||||
} else { {} }
|
||||
let r = (do -i {
|
||||
git log -n $num --pretty=%h»¦«%s»¦«%aN»¦«%aE»¦«%aD
|
||||
| lines
|
||||
| split column "»¦«" sha message author email date
|
||||
| each {|x| ($x| upsert date ($x.date | into datetime))}
|
||||
})
|
||||
if $v {
|
||||
$r | merge $stat | reverse
|
||||
} else {
|
||||
$r | reverse
|
||||
}
|
||||
}
|
||||
|
||||
def "nu-complete git log" [] {
|
||||
git log -n 32 --pretty=%h»¦«%s
|
||||
| lines
|
||||
| split column "»¦«" value description
|
||||
| each {|x| $x | update value $"($x.value)"}
|
||||
}
|
||||
|
||||
def "nu-complete git branches" [] {
|
||||
git branch
|
||||
| lines
|
||||
| filter {|x| not ($x | str starts-with '*')}
|
||||
| each {|x| $"($x|str trim)"}
|
||||
}
|
||||
|
||||
export def gl [
|
||||
commit?: string@"nu-complete git log"
|
||||
--verbose(-v)
|
||||
--num(-n):int=32
|
||||
] {
|
||||
if ($commit|is-empty) {
|
||||
_git_log $verbose $num
|
||||
} else {
|
||||
git log --stat -p -n 1 $commit
|
||||
}
|
||||
}
|
||||
|
||||
export def glv [
|
||||
commit?: string@"nu-complete git log"
|
||||
--num(-n):int=32
|
||||
] {
|
||||
if ($commit|is-empty) {
|
||||
_git_log true $num
|
||||
} else {
|
||||
git log --stat -p -n 1 $commit
|
||||
}
|
||||
}
|
||||
|
||||
export def gco [branch: string@"nu-complete git branches"] {
|
||||
git checkout $branch
|
||||
}
|
||||
|
||||
export def gbD [branch: string@"nu-complete git branches"] {
|
||||
git branch -D $branch
|
||||
}
|
||||
|
||||
export def gpp! [] {
|
||||
git pull
|
||||
git add --all
|
||||
git commit -v -a --no-edit --amend
|
||||
git push --force
|
||||
}
|
||||
|
||||
export def gha [] {
|
||||
git log --pretty=%h»¦«%aN»¦«%s»¦«%aD
|
||||
| lines
|
||||
| split column "»¦«" sha1 committer desc merged_at
|
||||
| histogram committer merger
|
||||
| sort-by merger
|
||||
| reverse
|
||||
}
|
||||
|
||||
export def gsq [] {
|
||||
git reflog expire --all --expire=now
|
||||
git gc --prune=now --aggressive
|
||||
}
|
||||
|
||||
def "nu-complete git remotes" [] {
|
||||
^git remote | lines | each { |line| $line | str trim }
|
||||
}
|
||||
|
||||
export def gr [remote?: string@"nu-complete git remotes"] {
|
||||
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
|
||||
git remote show $remote
|
||||
}
|
||||
|
||||
export def grh [commit: string@"nu-complete git log"] {
|
||||
git reset $commit
|
||||
}
|
||||
|
||||
export def gf [
|
||||
branch: string@"nu-complete git branches"
|
||||
remote?: string@"nu-complete git remotes"
|
||||
] {
|
||||
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
|
||||
git fetch $remote $branch
|
||||
}
|
||||
|
||||
export def gm [branch:string@"nu-complete git branches"] {
|
||||
git merge $branch
|
||||
}
|
||||
|
||||
export def grb [branch:string@"nu-complete git branches"] {
|
||||
git rebase (gstat).branch $branch
|
||||
}
|
||||
15
dot_config/nushell/nu_scripts/modules/git/git_branch_age.nu
Normal file
15
dot_config/nushell/nu_scripts/modules/git/git_branch_age.nu
Normal file
@@ -0,0 +1,15 @@
|
||||
# Creates a table listing the branches of a git repository and the day of the last commit
|
||||
export def "git age" [] {
|
||||
git branch |
|
||||
lines |
|
||||
str substring 2.. |
|
||||
wrap name |
|
||||
insert last_commit {
|
||||
get name |
|
||||
each {
|
||||
git show $in --no-patch --format=%as | into datetime
|
||||
}
|
||||
} |
|
||||
sort-by last_commit
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# Git branch cleanup
|
||||
|
||||
Remove any local git branches that have been merged and optionally remove any
|
||||
remote branches that have been merged.
|
||||
|
||||
Load with:
|
||||
```nushell
|
||||
source modules/git/git_branch_cleanup.nu
|
||||
```
|
||||
|
||||
To remove merged branches:
|
||||
```nushell
|
||||
git branch-cleanup
|
||||
```
|
||||
|
||||
To keep merged branches that start with "releases/":
|
||||
|
||||
```nushell
|
||||
git config --local --add branch-cleanup.keep 'releases/.*'
|
||||
```
|
||||
|
||||
Keep branch patterns are space-separated
|
||||
171
dot_config/nushell/nu_scripts/modules/git/git_branch_cleanup.nu
Normal file
171
dot_config/nushell/nu_scripts/modules/git/git_branch_cleanup.nu
Normal file
@@ -0,0 +1,171 @@
|
||||
# Adapted from original by Yorick Sijsling
|
||||
#
|
||||
# Adapted from bash version `git cleanup-repo` by Rob Miller <rob@bigfish.co.uk>
|
||||
# https://gist.github.com/robmiller/5133264
|
||||
|
||||
# Delete local (and optionally remote) merged branches
|
||||
#
|
||||
# Set branch-cleanup.keep locally to a space-separated list of branch patterns to keep
|
||||
export def "git branch-cleanup" [
|
||||
upstream: string@remotes = "origin" # Upstream remote repository
|
||||
] {
|
||||
let current_branch = current_branch
|
||||
let default_branch = get_default_branch $"refs/remotes/($upstream)/HEAD"
|
||||
let keep = get_keep
|
||||
|
||||
# Switch to the default branch
|
||||
switch_branch $default_branch
|
||||
|
||||
# Make sure we're working with the most up-to-date version of the default
|
||||
# branch.
|
||||
run-external "git" "fetch"
|
||||
|
||||
# Prune obsolete remote tracking branches. These are branches that we
|
||||
# once tracked, but have since been deleted on the remote.
|
||||
run-external "git" "remote" "prune" $upstream
|
||||
|
||||
# Delete local branches that have been fully merged into the default branch
|
||||
list_merged $upstream $default_branch $keep
|
||||
| each {|branch|
|
||||
delete_local $branch
|
||||
}
|
||||
|
||||
# Again with remote branches
|
||||
let merged_on_remote = list_merged --remote $upstream $default_branch $keep
|
||||
|
||||
if not ( $merged_on_remote | is-empty ) {
|
||||
print "The following remote branches are fully merged and will be removed:"
|
||||
|
||||
$merged_on_remote
|
||||
| each {||
|
||||
print $"\t($in)"
|
||||
}
|
||||
|
||||
print ""
|
||||
|
||||
if ( input --suppress-output "Continue (y/N)? " | str trim ) == "y" {
|
||||
$merged_on_remote
|
||||
| each {|branch|
|
||||
delete_remote $upstream $branch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch_branch $current_branch
|
||||
}
|
||||
|
||||
# The current branch name
|
||||
def current_branch [] {
|
||||
run-external "git" "branch" "--show-current"
|
||||
| into string
|
||||
| str trim
|
||||
}
|
||||
|
||||
# Delete a local branch
|
||||
def delete_local [
|
||||
branch: string # Branch to delete
|
||||
] {
|
||||
run-external "git" "branch" "--delete" $branch
|
||||
}
|
||||
|
||||
# Delete a remote branch
|
||||
def delete_remote [
|
||||
upstream: string # Repository to delete from
|
||||
branch: string # branch to delete
|
||||
] {
|
||||
run-external "git" "push" "--quiet" "--delete" $upstream $branch
|
||||
}
|
||||
|
||||
# Get the default branch
|
||||
def get_default_branch [
|
||||
upstream: string # Repository to get the upstream branch name from
|
||||
] {
|
||||
let args = [
|
||||
"--short"
|
||||
$upstream
|
||||
]
|
||||
|
||||
run-external "git" "symbolic-ref" ...$args
|
||||
| str trim
|
||||
| path basename
|
||||
}
|
||||
|
||||
# Get the local set of branches to always keep
|
||||
def get_keep [] {
|
||||
let keep = run-external "git" "config" "--local" "--get" "branch-cleanup.keep"
|
||||
|
||||
if ( $keep | is-empty ) {
|
||||
return []
|
||||
}
|
||||
|
||||
$keep
|
||||
| str trim
|
||||
| split column " "
|
||||
| get column1
|
||||
}
|
||||
|
||||
# List all the branches that have been merged fully into the default branch.
|
||||
#
|
||||
# We use the remote default branch here, just in case our local default branch is out of date.
|
||||
def list_merged [
|
||||
--remote # List remote branches (local default)
|
||||
upstream: string # Upstream repository
|
||||
branch: string # Default branch
|
||||
keep: list<string> # Patterns to keep. The default branch and HEAD will be added
|
||||
] {
|
||||
mut args = [
|
||||
"--list"
|
||||
"--merged" $"($upstream)/($branch)"
|
||||
]
|
||||
|
||||
if $remote {
|
||||
$args = ( $args | append [
|
||||
"--format" "%(refname:lstrip=3)"
|
||||
"--remote"
|
||||
])
|
||||
} else {
|
||||
$args = ( $args | append [
|
||||
"--format" "%(refname:lstrip=2)"
|
||||
])
|
||||
}
|
||||
|
||||
let args = $args
|
||||
|
||||
let keep = (
|
||||
$keep
|
||||
| append [
|
||||
"HEAD",
|
||||
$branch,
|
||||
]
|
||||
)
|
||||
|
||||
run-external "git" "branch" ...$args
|
||||
| lines
|
||||
| filter {|branch|
|
||||
$keep
|
||||
| all {|pattern|
|
||||
$branch !~ $'\A($pattern)\z'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def remotes [] {
|
||||
run-external "git" "remote" "-v"
|
||||
| parse "{value}\t{description} ({operation})"
|
||||
| select value description
|
||||
| uniq
|
||||
| sort
|
||||
}
|
||||
|
||||
# Switch to a different branch
|
||||
def switch_branch [
|
||||
branch: string
|
||||
] {
|
||||
let args = [
|
||||
"--quiet"
|
||||
"--no-guess"
|
||||
$branch
|
||||
]
|
||||
|
||||
run-external "git" "switch" ...$args
|
||||
}
|
||||
Reference in New Issue
Block a user