Skip to content

Commit

Permalink
refactor git and git-prompt modules in sugar to allow testing (#…
Browse files Browse the repository at this point in the history
…121)

related to
- #24

## description
the `./src/nu-git-manager-sugar/` directory structure changed from
```
.
|-- extra.nu
|-- git-prompt.nu
|-- git.nu
|-- github.nu
`-- mod.nu
```
to
```
.
|-- extra.nu
|-- git
|   |-- lib
|   |   |-- lib.nu
|   |   `-- prompt.nu
|   |-- mod.nu
|   `-- prompt.nu
|-- github.nu
`-- mod.nu
```

that is
- all the Git commands, e.g. `gm repo ...`, are still exposed as before
from `git/mod.nu`
- `git-prompt` has been moved to `git/prompt.nu` and the main _setup_
command is now called `nu-git-manager-sugar git prompt setup`
- the commands from past `git-prompt.nu` that would need to be tested
have been moved to `git/lib/` which is a _private_ module
  • Loading branch information
amtoine authored Dec 3, 2023
1 parent ad37e5b commit 9359ac6
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 257 deletions.
257 changes: 0 additions & 257 deletions src/nu-git-manager-sugar/git-prompt.nu

This file was deleted.

111 changes: 111 additions & 0 deletions src/nu-git-manager-sugar/git/lib/lib.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 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 │
# ╰──────┴──────────╯
# TODO: write a test
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
# TODO: write a test
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
}
}
}

Loading

0 comments on commit 9359ac6

Please sign in to comment.