-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-search
executable file
·104 lines (95 loc) · 2.96 KB
/
git-search
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
#!/bin/bash
search_by_commit() {
git log --grep="$1" $2
}
search_by_content() {
git log -S "$1" $2
}
search_by_content_regex() {
git log -G "$1" $2
}
interactive_search() {
local options=("Search by commit message" "Search by content (string match)" "Search by content (regex)")
local choice=$(printf '%s\n' "${options[@]}" | fzf --prompt="Select search type: ")
case "$choice" in
"Search by commit message")
read -p "Enter commit message to search: " message
search_by_commit "$message" "$1"
;;
"Search by content (string match)")
read -p "Enter content to search (string match): " content
search_by_content "$content" "$1"
;;
"Search by content (regex)")
read -p "Enter content to search (regex): " regex
search_by_content_regex "$regex" "$1"
;;
esac
}
display_help() {
echo "Usage: git-search [options]"
echo
echo "Options:"
echo " -h, --help Display this help message"
echo " -p, --patch Show the actual content of the changes"
echo " --by-commit <message> Search by commit message"
echo " --by-content <string> Search by content using string match"
echo " --by-content-regex <regex> Search by content using regex"
echo " --since <date> Search for commits since a specific date"
echo " --until <date> Search for commits until a specific date"
echo
echo "Examples:"
echo " git-search Start interactive search"
echo " git-search -p Start interactive search and show content"
echo " git-search --by-commit \"Fix bug\""
echo " git-search --by-content \"function_name\" -p"
echo " git-search --by-content-regex \"function_\w+\" -p"
echo " git-search --since 2024-03-08"
echo " git-search --since 2024-03-01 --until 2024-03-05"
}
patch_option=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
display_help
exit 0
;;
-p|--patch)
patch_option="-p"
shift
;;
--by-commit)
shift
search_by_commit "$1" "$patch_option"
exit 0
;;
--by-content)
shift
search_by_content "$1" "$patch_option"
exit 0
;;
--by-content-regex)
shift
search_by_content_regex "$1" "$patch_option"
exit 0
;;
--since)
since="--since $2"
shift 2
;;
--until)
until="--until $2"
shift 2
;;
*)
echo "Unknown option: $1"
display_help
exit 1
;;
esac
done
if [[ -z "$since" && -z "$until" ]]; then
interactive_search "$patch_option"
else
git log $since $until $patch_option
fi