2022-04-09 01:02:28 +03:00
|
|
|
# panache-git
|
|
|
|
# An opinionated Git prompt for Nushell, styled after posh-git
|
|
|
|
#
|
|
|
|
# Quick Start:
|
2022-09-08 00:52:45 +03:00
|
|
|
# - Download this file (panache-git.nu)
|
2022-06-01 00:31:03 +03:00
|
|
|
# - In your Nushell config:
|
2024-04-11 16:00:04 +03:00
|
|
|
# - Import the main command from the panache-git.nu module file
|
2022-04-09 01:02:28 +03:00
|
|
|
# - Set panache-git as your prompt command
|
|
|
|
# - Disable the separate prompt indicator by setting it to an empty string
|
2022-09-08 00:52:45 +03:00
|
|
|
# - For example, with this file in your home directory:
|
2024-04-11 16:00:04 +03:00
|
|
|
# use ~/panache-git.nu main
|
|
|
|
# $env.PROMPT_COMMAND = {|| panache-git }
|
|
|
|
# $env.PROMPT_INDICATOR = {|| "" }
|
2022-04-09 01:02:28 +03:00
|
|
|
# - Restart Nushell
|
|
|
|
#
|
|
|
|
# For more documentation or to file an issue, see https://github.com/ehdevries/panache-git
|
|
|
|
|
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
# An opinionated Git prompt for Nushell, styled after posh-git
|
2024-04-11 16:00:04 +03:00
|
|
|
export def main [] {
|
2022-09-08 00:52:45 +03:00
|
|
|
let prompt = ($'(current-dir) (repo-styled)' | str trim)
|
|
|
|
$'($prompt)> '
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
# Get the current directory with home abbreviated
|
2022-09-29 15:38:35 +03:00
|
|
|
export def current-dir [] {
|
2022-09-08 00:52:45 +03:00
|
|
|
let current_dir = ($env.PWD)
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
let current_dir_relative_to_home = (
|
2022-09-29 15:38:35 +03:00
|
|
|
do --ignore-errors { $current_dir | path relative-to $nu.home-path } | str join
|
2022-09-08 00:52:45 +03:00
|
|
|
)
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2024-04-11 16:00:04 +03:00
|
|
|
let in_sub_dir_of_home = ($current_dir_relative_to_home | is-not-empty)
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
let current_dir_abbreviated = (if $in_sub_dir_of_home {
|
2023-08-18 02:10:20 +03:00
|
|
|
$'~(char separator)($current_dir_relative_to_home)' | str replace -ar '\\' '/'
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
2023-08-18 02:10:20 +03:00
|
|
|
$current_dir | str replace -ar '\\' '/'
|
2022-09-08 00:52:45 +03:00
|
|
|
})
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
$'(ansi reset)($current_dir_abbreviated)'
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
# Get repository status as structured data
|
2022-09-29 15:38:35 +03:00
|
|
|
export def repo-structured [] {
|
2024-04-11 16:00:04 +03:00
|
|
|
let in_git_repo = (do { git rev-parse --abbrev-ref HEAD } | complete | get stdout | is-not-empty)
|
2022-09-08 00:52:45 +03:00
|
|
|
|
|
|
|
let status = (if $in_git_repo {
|
|
|
|
git --no-optional-locks status --porcelain=2 --branch | lines
|
|
|
|
} else {
|
|
|
|
[]
|
|
|
|
})
|
|
|
|
|
|
|
|
let on_named_branch = (if $in_git_repo {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '# branch.head')
|
|
|
|
| first
|
2024-06-17 01:30:53 +03:00
|
|
|
| str contains '(detached)'
|
|
|
|
| not $in
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
|
|
|
|
let branch_name = (if $on_named_branch {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '# branch.head')
|
|
|
|
| split column ' ' col1 col2 branch
|
|
|
|
| get branch
|
|
|
|
| first
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let commit_hash = (if $in_git_repo {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '# branch.oid')
|
|
|
|
| split column ' ' col1 col2 full_hash
|
|
|
|
| get full_hash
|
|
|
|
| first
|
2023-04-16 15:15:37 +03:00
|
|
|
| str substring 0..7
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let tracking_upstream_branch = (if $in_git_repo {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '# branch.upstream')
|
2022-09-29 15:38:35 +03:00
|
|
|
| str join
|
2024-04-11 16:00:04 +03:00
|
|
|
| is-not-empty
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
|
|
|
|
let upstream_exists_on_remote = (if $in_git_repo {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '# branch.ab')
|
2022-09-29 15:38:35 +03:00
|
|
|
| str join
|
2024-04-11 16:00:04 +03:00
|
|
|
| is-not-empty
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
|
|
|
|
let ahead_behind_table = (if $upstream_exists_on_remote {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '# branch.ab')
|
|
|
|
| split column ' ' col1 col2 ahead behind
|
|
|
|
} else {
|
|
|
|
[[]]
|
|
|
|
})
|
|
|
|
|
|
|
|
let commits_ahead = (if $upstream_exists_on_remote {
|
|
|
|
$ahead_behind_table
|
|
|
|
| get ahead
|
|
|
|
| first
|
|
|
|
| into int
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let commits_behind = (if $upstream_exists_on_remote {
|
|
|
|
$ahead_behind_table
|
|
|
|
| get behind
|
|
|
|
| first
|
|
|
|
| into int
|
|
|
|
| math abs
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let has_staging_or_worktree_changes = (if $in_git_repo {
|
|
|
|
$status
|
2022-12-04 05:54:33 +03:00
|
|
|
| where ($it | str starts-with '1') or ($it | str starts-with '2')
|
2022-09-29 15:38:35 +03:00
|
|
|
| str join
|
2024-04-11 16:00:04 +03:00
|
|
|
| is-not-empty
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
|
|
|
|
let has_untracked_files = (if $in_git_repo {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '?')
|
2022-09-29 15:38:35 +03:00
|
|
|
| str join
|
2024-04-11 16:00:04 +03:00
|
|
|
| is-not-empty
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
|
|
|
|
let has_unresolved_merge_conflicts = (if $in_git_repo {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with 'u')
|
2022-09-29 15:38:35 +03:00
|
|
|
| str join
|
2024-04-11 16:00:04 +03:00
|
|
|
| is-not-empty
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
})
|
|
|
|
|
|
|
|
let staging_worktree_table = (if $has_staging_or_worktree_changes {
|
|
|
|
$status
|
2022-12-04 05:54:33 +03:00
|
|
|
| where ($it | str starts-with '1') or ($it | str starts-with '2')
|
2022-09-08 00:52:45 +03:00
|
|
|
| split column ' '
|
|
|
|
| get column2
|
|
|
|
| split column '' staging worktree --collapse-empty
|
|
|
|
} else {
|
|
|
|
[[]]
|
|
|
|
})
|
|
|
|
|
|
|
|
let staging_added_count = (if $has_staging_or_worktree_changes {
|
|
|
|
$staging_worktree_table
|
|
|
|
| where staging == 'A'
|
|
|
|
| length
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let staging_modified_count = (if $has_staging_or_worktree_changes {
|
|
|
|
$staging_worktree_table
|
|
|
|
| where staging in ['M', 'R']
|
|
|
|
| length
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let staging_deleted_count = (if $has_staging_or_worktree_changes {
|
|
|
|
$staging_worktree_table
|
|
|
|
| where staging == 'D'
|
|
|
|
| length
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let untracked_count = (if $has_untracked_files {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with '?')
|
|
|
|
| length
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let worktree_modified_count = (if $has_staging_or_worktree_changes {
|
|
|
|
$staging_worktree_table
|
|
|
|
| where worktree in ['M', 'R']
|
|
|
|
| length
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let worktree_deleted_count = (if $has_staging_or_worktree_changes {
|
|
|
|
$staging_worktree_table
|
|
|
|
| where worktree == 'D'
|
|
|
|
| length
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
let merge_conflict_count = (if $has_unresolved_merge_conflicts {
|
|
|
|
$status
|
|
|
|
| where ($it | str starts-with 'u')
|
|
|
|
| length
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
})
|
|
|
|
|
|
|
|
{
|
|
|
|
in_git_repo: $in_git_repo,
|
|
|
|
on_named_branch: $on_named_branch,
|
|
|
|
branch_name: $branch_name,
|
|
|
|
commit_hash: $commit_hash,
|
|
|
|
tracking_upstream_branch: $tracking_upstream_branch,
|
|
|
|
upstream_exists_on_remote: $upstream_exists_on_remote,
|
|
|
|
commits_ahead: $commits_ahead,
|
|
|
|
commits_behind: $commits_behind,
|
|
|
|
staging_added_count: $staging_added_count,
|
|
|
|
staging_modified_count: $staging_modified_count,
|
|
|
|
staging_deleted_count: $staging_deleted_count,
|
|
|
|
untracked_count: $untracked_count,
|
|
|
|
worktree_modified_count: $worktree_modified_count,
|
|
|
|
worktree_deleted_count: $worktree_deleted_count,
|
|
|
|
merge_conflict_count: $merge_conflict_count
|
2022-04-09 01:02:28 +03:00
|
|
|
}
|
2022-09-08 00:52:45 +03:00
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
# Get repository status as a styled string
|
2022-09-29 15:38:35 +03:00
|
|
|
export def repo-styled [] {
|
2022-09-08 00:52:45 +03:00
|
|
|
let status = (repo-structured)
|
|
|
|
|
|
|
|
let is_local_only = ($status.tracking_upstream_branch != true)
|
|
|
|
|
|
|
|
let upstream_deleted = (
|
2022-12-04 05:54:33 +03:00
|
|
|
$status.tracking_upstream_branch and
|
2022-09-08 00:52:45 +03:00
|
|
|
$status.upstream_exists_on_remote != true
|
|
|
|
)
|
|
|
|
|
|
|
|
let is_up_to_date = (
|
2022-12-04 05:54:33 +03:00
|
|
|
$status.upstream_exists_on_remote and
|
|
|
|
$status.commits_ahead == 0 and
|
2022-09-08 00:52:45 +03:00
|
|
|
$status.commits_behind == 0
|
|
|
|
)
|
|
|
|
|
|
|
|
let is_ahead = (
|
2022-12-04 05:54:33 +03:00
|
|
|
$status.upstream_exists_on_remote and
|
|
|
|
$status.commits_ahead > 0 and
|
2022-09-08 00:52:45 +03:00
|
|
|
$status.commits_behind == 0
|
|
|
|
)
|
|
|
|
|
|
|
|
let is_behind = (
|
2022-12-04 05:54:33 +03:00
|
|
|
$status.upstream_exists_on_remote and
|
|
|
|
$status.commits_ahead == 0 and
|
2022-09-08 00:52:45 +03:00
|
|
|
$status.commits_behind > 0
|
|
|
|
)
|
|
|
|
|
|
|
|
let is_ahead_and_behind = (
|
2022-12-04 05:54:33 +03:00
|
|
|
$status.upstream_exists_on_remote and
|
|
|
|
$status.commits_ahead > 0 and
|
2022-09-08 00:52:45 +03:00
|
|
|
$status.commits_behind > 0
|
|
|
|
)
|
|
|
|
|
|
|
|
let branch_name = (if $status.in_git_repo {
|
|
|
|
(if $status.on_named_branch {
|
|
|
|
$status.branch_name
|
2022-04-09 01:02:28 +03:00
|
|
|
} else {
|
2022-09-29 15:38:35 +03:00
|
|
|
['(' $status.commit_hash '...)'] | str join
|
2022-04-09 01:02:28 +03:00
|
|
|
})
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let branch_styled = (if $status.in_git_repo {
|
|
|
|
(if $is_local_only {
|
|
|
|
(branch-local-only $branch_name)
|
|
|
|
} else if $is_up_to_date {
|
|
|
|
(branch-up-to-date $branch_name)
|
|
|
|
} else if $is_ahead {
|
|
|
|
(branch-ahead $branch_name $status.commits_ahead)
|
|
|
|
} else if $is_behind {
|
|
|
|
(branch-behind $branch_name $status.commits_behind)
|
|
|
|
} else if $is_ahead_and_behind {
|
|
|
|
(branch-ahead-and-behind $branch_name $status.commits_ahead $status.commits_behind)
|
|
|
|
} else if $upstream_deleted {
|
|
|
|
(branch-upstream-deleted $branch_name)
|
2022-04-09 01:02:28 +03:00
|
|
|
} else {
|
2022-09-08 00:52:45 +03:00
|
|
|
$branch_name
|
2022-04-09 01:02:28 +03:00
|
|
|
})
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let has_staging_changes = (
|
2022-12-04 05:54:33 +03:00
|
|
|
$status.staging_added_count > 0 or
|
|
|
|
$status.staging_modified_count > 0 or
|
2022-09-08 00:52:45 +03:00
|
|
|
$status.staging_deleted_count > 0
|
|
|
|
)
|
|
|
|
|
|
|
|
let has_worktree_changes = (
|
2022-12-04 05:54:33 +03:00
|
|
|
$status.untracked_count > 0 or
|
|
|
|
$status.worktree_modified_count > 0 or
|
|
|
|
$status.worktree_deleted_count > 0 or
|
2022-09-08 00:52:45 +03:00
|
|
|
$status.merge_conflict_count > 0
|
|
|
|
)
|
|
|
|
|
|
|
|
let has_merge_conflicts = $status.merge_conflict_count > 0
|
|
|
|
|
|
|
|
let staging_summary = (if $has_staging_changes {
|
|
|
|
(staging-changes $status.staging_added_count $status.staging_modified_count $status.staging_deleted_count)
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let worktree_summary = (if $has_worktree_changes {
|
|
|
|
(worktree-changes $status.untracked_count $status.worktree_modified_count $status.worktree_deleted_count)
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let merge_conflict_summary = (if $has_merge_conflicts {
|
|
|
|
(unresolved-conflicts $status.merge_conflict_count)
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
2022-12-04 05:54:33 +03:00
|
|
|
let delimiter = (if ($has_staging_changes and $has_worktree_changes) {
|
2022-09-08 00:52:45 +03:00
|
|
|
('|' | bright-yellow)
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let local_summary = (
|
|
|
|
$'($staging_summary) ($delimiter) ($worktree_summary) ($merge_conflict_summary)' | str trim
|
|
|
|
)
|
|
|
|
|
|
|
|
let local_indicator = (if $status.in_git_repo {
|
|
|
|
(if $has_worktree_changes {
|
|
|
|
('!' | red)
|
|
|
|
} else if $has_staging_changes {
|
|
|
|
('~' | bright-cyan)
|
2022-04-09 01:02:28 +03:00
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
2022-09-08 00:52:45 +03:00
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
|
|
|
|
let repo_summary = (
|
|
|
|
$'($branch_styled) ($local_summary) ($local_indicator)' | str trim
|
|
|
|
)
|
|
|
|
|
|
|
|
let left_bracket = ('[' | bright-yellow)
|
|
|
|
let right_bracket = (']' | bright-yellow)
|
|
|
|
|
|
|
|
(if $status.in_git_repo {
|
|
|
|
$'($left_bracket)($repo_summary)($right_bracket)'
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
})
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
# Helper commands to encapsulate style and make everything else more readable
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def bright-cyan [] {
|
|
|
|
each { |it| $"(ansi -e '96m')($it)(ansi reset)" }
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def bright-green [] {
|
|
|
|
each { |it| $"(ansi -e '92m')($it)(ansi reset)" }
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def bright-red [] {
|
|
|
|
each { |it| $"(ansi -e '91m')($it)(ansi reset)" }
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def bright-yellow [] {
|
|
|
|
each { |it| $"(ansi -e '93m')($it)(ansi reset)" }
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def green [] {
|
|
|
|
each { |it| $"(ansi green)($it)(ansi reset)" }
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def red [] {
|
|
|
|
each { |it| $"(ansi red)($it)(ansi reset)" }
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def branch-local-only [
|
|
|
|
branch: string
|
|
|
|
] {
|
|
|
|
$branch | bright-cyan
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def branch-upstream-deleted [
|
|
|
|
branch: string
|
|
|
|
] {
|
|
|
|
$'($branch) (char failed)' | bright-cyan
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def branch-up-to-date [
|
|
|
|
branch: string
|
|
|
|
] {
|
|
|
|
$'($branch) (char identical_to)' | bright-cyan
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def branch-ahead [
|
|
|
|
branch: string
|
|
|
|
ahead: int
|
|
|
|
] {
|
|
|
|
$'($branch) (char branch_ahead)($ahead)' | bright-green
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def branch-behind [
|
|
|
|
branch: string
|
|
|
|
behind: int
|
|
|
|
] {
|
|
|
|
$'($branch) (char branch_behind)($behind)' | bright-red
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def branch-ahead-and-behind [
|
|
|
|
branch: string
|
|
|
|
ahead: int
|
|
|
|
behind: int
|
|
|
|
] {
|
|
|
|
$'($branch) (char branch_behind)($behind) (char branch_ahead)($ahead)' | bright-yellow
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def staging-changes [
|
|
|
|
added: int
|
|
|
|
modified: int
|
|
|
|
deleted: int
|
|
|
|
] {
|
|
|
|
$'+($added) ~($modified) -($deleted)' | green
|
|
|
|
}
|
2022-04-09 01:02:28 +03:00
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def worktree-changes [
|
|
|
|
added: int
|
|
|
|
modified: int
|
|
|
|
deleted: int
|
|
|
|
] {
|
|
|
|
$'+($added) ~($modified) -($deleted)' | red
|
2022-04-09 01:02:28 +03:00
|
|
|
}
|
|
|
|
|
2022-09-08 00:52:45 +03:00
|
|
|
def unresolved-conflicts [
|
|
|
|
conflicts: int
|
|
|
|
] {
|
|
|
|
$'!($conflicts)' | red
|
2022-04-09 01:02:28 +03:00
|
|
|
}
|