Repository navigation
Expand file tree
/
Copy pathgit_tools.nu
More file actions
89 lines (72 loc) · 2.22 KB
/
Copy pathgit_tools.nu
File metadata and controls
89 lines (72 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Git metrics helper for prompt
# Parse git status --porcelain=v2 --branch output
export def parse-git-status-v2 [input: string] {
let lines = $input | lines
let branch_head = $lines
| where $it =~ "^# branch.head"
| get -o 0
| default ""
| parse --regex "# branch.head (?P<name>.+)"
| get -o 0
| get -o name
| default "no-branch"
let branch_ab = $lines
| where $it =~ "^# branch.ab"
| get -o 0
| default ""
| parse --regex "# branch.ab \\+(?P<ahead>\\d+) -(?P<behind>\\d+)"
| get -o 0
| default {ahead: "0", behind: "0"}
# 1 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <path>
# X = staged, Y = unstaged
let changed_lines = $lines | where $it =~ "^[12] "
let untracked_files = $lines | where $it =~ "^\\? " | length
# Staged: X column (index 2) is not '.'
let staged_files = $changed_lines
| where ($it | str substring 2..2) != "."
| length
# Modified: Y column (index 3) is 'M'
let modified_files = $changed_lines
| where ($it | str substring 3..3) == "M"
| length
# Deleted: Y column (index 3) is 'D'
let deleted_files = $changed_lines
| where ($it | str substring 3..3) == "D"
| length
{
branch: $branch_head
ahead: ($branch_ab.ahead | into int)
behind: ($branch_ab.behind | into int)
staged: $staged_files
modified: $modified_files
deleted: $deleted_files
untracked: $untracked_files
}
}
# Extract git metrics using porcelain v2
export def get-git-metrics [] {
if not (".git" | path exists) {
return {
branch: ""
ahead: 0
behind: 0
staged: 0
modified: 0
deleted: 0
untracked: 0
}
}
let status = do -i { ^git status --porcelain=v2 --branch } | complete
if $status.exit_code != 0 {
return {
branch: ""
ahead: 0
behind: 0
staged: 0
modified: 0
deleted: 0
untracked: 0
}
}
parse-git-status-v2 $status.stdout
}