diff --git a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/git.nu b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/git.nu new file mode 100644 index 0000000..d5ddf8a --- /dev/null +++ b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/git.nu @@ -0,0 +1,122 @@ +use style.nu [color] + +# give the revision of the repo you're in +# +# in the output, $.type is guaranteed to be one of +# - "branch" +# - "tag" +# - "detached" +# +# # Examples +# when on a branch +# > get-revision # would show the same even if the current branch commit is tagged +# ╭──────┬──────────────────────────────────────────╮ +# │ name │ main │ +# │ hash │ fa3c06510b3250f4a901db2e9a026a45c971b518 │ +# │ type │ branch │ +# ╰──────┴──────────────────────────────────────────╯ +# +# when on a tag +# > get-revision +# ╭──────┬──────────────────────────────────────────╮ +# │ name │ 1.2.3 │ +# │ hash │ fa3c06510b3250f4a901db2e9a026a45c971b518 │ +# │ type │ tag │ +# ╰──────┴──────────────────────────────────────────╯ +# +# when the HEAD is detached +# > get-revision +# ╭──────┬──────────────────────────────────────────╮ +# │ name │ │ +# │ hash │ fa3c06510b3250f4a901db2e9a026a45c971b518 │ +# │ type │ detached │ +# ╰──────┴──────────────────────────────────────────╯ +# +# when the HEAD is detached (short-version) +# > get-revision --short-hash +# ╭──────┬──────────╮ +# │ name │ │ +# │ hash │ fa3c0651 │ +# │ type │ detached │ +# ╰──────┴──────────╯ +export def get-revision [ + --short-hash: bool # print the hash of a detached HEAD in short format +]: nothing -> record { + let tag = do -i { + ^git describe HEAD --tags + } | complete + let is_tag = $tag.exit_code == 0 and ( + $tag.stdout + | str trim + | parse --regex '(?.*)-(?\d+)-(?[0-9a-fg]+)' + | is-empty + ) + + let branch = ^git branch --show-current + let hash = if $short_hash { + (^git rev-parse --short HEAD) + } else { + (^git rev-parse HEAD) + } + + if not ($branch | is-empty) { + {name: $branch, hash: $hash, type: "branch"} + } else if $is_tag { + {name: ($tag.stdout | str trim), hash: $hash, type: "tag"} + } else { + {name: null, hash: $hash, type: "detached"} + } +} + +# https://stackoverflow.com/questions/59603312/git-how-can-i-easily-tell-if-im-in-the-middle-of-a-rebase +export def git-action []: nothing -> string { + let git_dir = ^git rev-parse --git-dir | path expand + + def test-dir [target: string]: nothing -> bool { + ($git_dir | path join $target | path type) == "dir" + } + + def test-file [target: string]: nothing -> bool { + ($git_dir | path join $target | path type) == "file" + } + + if (test-dir "rebase-merge") { + if (test-file "rebase-merge/interactive") { + "REBASE-i" | color blue + } else { + "REBASE-m" | color magenta + } + } else { + if (test-dir "rebase-apply") { + if (test-file "rebase-apply/rebasing") { + "REBASE" | color cyan + } else if (test-file "rebase-apply/applying") { + "AM" | color cyan + } else { + "AM/REBASE" | color cyan + } + } else if (test-file "MERGE_HEAD") { + "MERGING" | color dark_gray + } else if (test-file "CHERRY_PICK_HEAD") { + "CHERRY-PICKING" | color green + } else if (test-file "REVERT_HEAD") { + "REVERTING" | color red + } else if (test-file "BISECT_LOG") { + "BISECTING" | color yellow + } else { + null + } + } +} + +export def get-status [ + repo: path, # the path to the repo +]: nothing -> record, unstaged: list, untracked: list> { + let status = ^git -C $repo status --short | lines + { + staged: ($status | parse --regex '^\w. (?.*)' | get file), + unstaged: ($status | parse --regex '^.\w (?.*)' | get file), + untracked: ($status | parse --regex '^\?\? (?.*)' | get file), + } +} + diff --git a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/mod.nu b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/mod.nu index b4a4bb5..6a62b94 100644 --- a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/mod.nu +++ b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/mod.nu @@ -1,123 +1,3 @@ +export module git.nu +export module prompt.nu export module style.nu - -use style [color] - -# give the revision of the repo you're in -# -# in the output, $.type is guaranteed to be one of -# - "branch" -# - "tag" -# - "detached" -# -# # Examples -# when on a branch -# > get-revision # would show the same even if the current branch commit is tagged -# ╭──────┬──────────────────────────────────────────╮ -# │ name │ main │ -# │ hash │ fa3c06510b3250f4a901db2e9a026a45c971b518 │ -# │ type │ branch │ -# ╰──────┴──────────────────────────────────────────╯ -# -# when on a tag -# > get-revision -# ╭──────┬──────────────────────────────────────────╮ -# │ name │ 1.2.3 │ -# │ hash │ fa3c06510b3250f4a901db2e9a026a45c971b518 │ -# │ type │ tag │ -# ╰──────┴──────────────────────────────────────────╯ -# -# when the HEAD is detached -# > get-revision -# ╭──────┬──────────────────────────────────────────╮ -# │ name │ │ -# │ hash │ fa3c06510b3250f4a901db2e9a026a45c971b518 │ -# │ type │ detached │ -# ╰──────┴──────────────────────────────────────────╯ -# -# when the HEAD is detached (short-version) -# > get-revision --short-hash -# ╭──────┬──────────╮ -# │ name │ │ -# │ hash │ fa3c0651 │ -# │ type │ detached │ -# ╰──────┴──────────╯ -export def get-revision [ - --short-hash: bool # print the hash of a detached HEAD in short format -]: nothing -> record { - let tag = do -i { - ^git describe HEAD --tags - } | complete - let is_tag = $tag.exit_code == 0 and ( - $tag.stdout - | str trim - | parse --regex '(?.*)-(?\d+)-(?[0-9a-fg]+)' - | is-empty - ) - - let branch = ^git branch --show-current - let hash = if $short_hash { - (^git rev-parse --short HEAD) - } else { - (^git rev-parse HEAD) - } - - if not ($branch | is-empty) { - {name: $branch, hash: $hash, type: "branch"} - } else if $is_tag { - {name: ($tag.stdout | str trim), hash: $hash, type: "tag"} - } else { - {name: null, hash: $hash, type: "detached"} - } -} - -# https://stackoverflow.com/questions/59603312/git-how-can-i-easily-tell-if-im-in-the-middle-of-a-rebase -export def git-action []: nothing -> string { - let git_dir = ^git rev-parse --git-dir | path expand - - def test-dir [target: string]: nothing -> bool { - ($git_dir | path join $target | path type) == "dir" - } - - def test-file [target: string]: nothing -> bool { - ($git_dir | path join $target | path type) == "file" - } - - if (test-dir "rebase-merge") { - if (test-file "rebase-merge/interactive") { - "REBASE-i" | color blue - } else { - "REBASE-m" | color magenta - } - } else { - if (test-dir "rebase-apply") { - if (test-file "rebase-apply/rebasing") { - "REBASE" | color cyan - } else if (test-file "rebase-apply/applying") { - "AM" | color cyan - } else { - "AM/REBASE" | color cyan - } - } else if (test-file "MERGE_HEAD") { - "MERGING" | color dark_gray - } else if (test-file "CHERRY_PICK_HEAD") { - "CHERRY-PICKING" | color green - } else if (test-file "REVERT_HEAD") { - "REVERTING" | color red - } else if (test-file "BISECT_LOG") { - "BISECTING" | color yellow - } else { - null - } - } -} - -export def get-status [ - repo: path, # the path to the repo -]: nothing -> record, unstaged: list, untracked: list> { - let status = ^git -C $repo status --short | lines - { - staged: ($status | parse --regex '^\w. (?.*)' | get file), - unstaged: ($status | parse --regex '^.\w (?.*)' | get file), - untracked: ($status | parse --regex '^\?\? (?.*)' | get file), - } -} diff --git a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/prompt.nu b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/prompt.nu index 0c494bf..02f9eeb 100644 --- a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/prompt.nu +++ b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/prompt.nu @@ -1,5 +1,5 @@ -use ../../git/lib [get-revision, git-action, get-status] -use ../../git/lib style [color, simplify-path] +use git.nu [get-revision, git-action, get-status] +use style.nu [color, simplify-path] # /!\ the PWD will be sanitized export def get-left-prompt [duration_threshold: duration]: nothing -> string { diff --git a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/mod.nu b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/mod.nu index de0b5dd..539cda1 100644 --- a/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/mod.nu +++ b/pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/mod.nu @@ -1,6 +1,6 @@ use std log -use ../git/lib [get-status] +use ../git/lib git [get-status] use ../completions.nu [ GIT_QUERY_TABLES, GIT_STRATEGIES, git-query-tables, get-remotes, get-branches, get-strategies diff --git a/pkgs/nu-git-manager-sugar/tests/git.nu b/pkgs/nu-git-manager-sugar/tests/git.nu index ce95437..051228c 100644 --- a/pkgs/nu-git-manager-sugar/tests/git.nu +++ b/pkgs/nu-git-manager-sugar/tests/git.nu @@ -354,10 +354,10 @@ export def branch-compare [] { } export module prompt { - use ../../../pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib [ + use ../../../pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib git [ get-revision, git-action ] - use ../../../pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/prompt.nu [ + use ../../../pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib prompt [ get-left-prompt ] use ../../../pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib style [