-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgw.v
135 lines (120 loc) · 3.4 KB
/
gw.v
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
module main
import os
import cli
import chalk
import spinner
struct ExecuteCommand {
command string
filter string
sort bool = true
}
fn main() {
mut cmd := cli.Command{
name: 'gw'
description: 'Where is my commit?\nUsage:\ngw search -help\ngw diff -help'
version: '1.3.3'
}
mut search_cmd := cli.Command{
name: 'search'
description: 'Searches the commit message in branches and tags'
usage: '<commit message>'
required_args: 1
execute: search
}
search_cmd.add_flag(cli.Flag{
flag: .string
name: 'branch'
abbrev: 'b'
default_value: ['']
description: 'Containing branch name that you want to filter'
})
search_cmd.add_flag(cli.Flag{
flag: .string
name: 'tag'
abbrev: 't'
default_value: ['']
description: 'Containing tag name that you want to filter'
})
mut diff_cmd := cli.Command{
name: 'diff'
description: 'Shows the commits between 2 tags'
usage: '<tag1> <tag2>'
required_args: 2
execute: diff
}
cmd.add_command(search_cmd)
cmd.add_command(diff_cmd)
cmd.setup()
cmd.parse(os.args)
}
fn search(cli_command cli.Command) ! {
branch := cli_command.flags.get_string('branch') or {
panic('Failed to get `branch` flag: ${err}')
}
tag := cli_command.flags.get_string('tag') or { panic('Failed to get `tag` flag: ${err}') }
message := cli_command.args[0]
println('Searching for ${chalk.style(message, 'bold')} in branches ${chalk.style(branch,
'bold')} and tags ${chalk.style(tag, 'bold')}')
branches := search_branches(message, branch)
tags := search_tag(message, tag)
println(chalk.fg('Branches:', 'green'))
for b in branches {
println(b)
}
println(chalk.fg('Tags:', 'green'))
for t in tags {
println(t)
}
}
fn search_branches(commit_message string, branch string) []string {
git_command := git_log(commit_message, 'git branch -r ')
return execute_command(ExecuteCommand{ command: git_command, filter: branch })
}
fn search_tag(commit_message string, tag string) []string {
git_command := git_log(commit_message, 'git tag')
return execute_command(ExecuteCommand{ command: git_command, filter: tag })
}
fn git_log(commit_message string, branch_or_tag_command string) string {
return '
YELLOW="\033[0;33m"
CYAN="\033[0;36m"
GREEN="\033[0;34m"
NC="\033[0m"
git log --format="%h %cd %an" --date=short --all --grep "${commit_message}" | while read -r sha1 date author; do
${branch_or_tag_command} --contains \$sha1 | xargs -I {} echo {} "\${YELLOW}\$sha1\$NC - \$CYAN\$author\$NC \$GREEN(\$date)\$NC";
done
'
}
fn diff(cli_command cli.Command) ! {
tag1 := cli_command.args[0]
tag2 := cli_command.args[1]
git_command := 'git log --oneline ${tag1}..${tag2}'
diff_commits := execute_command(ExecuteCommand{ command: git_command, filter: '', sort: false })
for d in diff_commits {
commit_id := d.all_before(' ')
commit_message := d.after_char(` `)
println('${chalk.fg(commit_id, 'green')} ${commit_message}')
}
}
fn execute_command(execute_command ExecuteCommand) []string {
mut sp := spinner.Spinner{}
sp.start()
mut s := []string{}
mut cmd := os.Command{
path: execute_command.command
redirect_stdout: false
}
cmd.start() or { panic('Failed to start git command: ${err}') }
for !cmd.eof {
line := cmd.read_line().trim_space()
if line.len > 0 && line.contains(execute_command.filter) {
s << line
}
}
cmd.close() or { panic('Failed to stop git command: ${err}') }
if execute_command.sort {
s.sort()
}
sp.stop()
return s
}