Skip to content

Commit

Permalink
split git lib
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Dec 17, 2023
1 parent a167362 commit 77596ff
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 127 deletions.
122 changes: 122 additions & 0 deletions pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/git.nu
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 pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/lib/mod.nu
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),
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/nu-git-manager-sugar/nu-git-manager-sugar/git/mod.nu
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkgs/nu-git-manager-sugar/tests/git.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down

0 comments on commit 77596ff

Please sign in to comment.