-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
127 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/git.nu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<name: string, hash: string, type: string> { | ||
let tag = do -i { | ||
^git describe HEAD --tags | ||
} | complete | ||
let is_tag = $tag.exit_code == 0 and ( | ||
$tag.stdout | ||
| str trim | ||
| parse --regex '(?<tag>.*)-(?<n>\d+)-(?<hash>[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<staged: list<string>, unstaged: list<string>, untracked: list<string>> { | ||
let status = ^git -C $repo status --short | lines | ||
{ | ||
staged: ($status | parse --regex '^\w. (?<file>.*)' | get file), | ||
unstaged: ($status | parse --regex '^.\w (?<file>.*)' | get file), | ||
untracked: ($status | parse --regex '^\?\? (?<file>.*)' | get file), | ||
} | ||
} | ||
|
124 changes: 2 additions & 122 deletions
124
pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/mod.nu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<name: string, hash: string, type: string> { | ||
let tag = do -i { | ||
^git describe HEAD --tags | ||
} | complete | ||
let is_tag = $tag.exit_code == 0 and ( | ||
$tag.stdout | ||
| str trim | ||
| parse --regex '(?<tag>.*)-(?<n>\d+)-(?<hash>[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<staged: list<string>, unstaged: list<string>, untracked: list<string>> { | ||
let status = ^git -C $repo status --short | lines | ||
{ | ||
staged: ($status | parse --regex '^\w. (?<file>.*)' | get file), | ||
unstaged: ($status | parse --regex '^.\w (?<file>.*)' | get file), | ||
untracked: ($status | parse --regex '^\?\? (?<file>.*)' | get file), | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/prompt.nu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters