-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgc-smart
executable file
·124 lines (103 loc) · 4.13 KB
/
gc-smart
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
#!/bin/bash
# ==============================================================================
# gc-smart
# ==============================================================================
#
# Filename : gc-smart
# Author(s) : David Schmid (david.schmid@mailbox.org)
# Version : 0.4.1
#
# ------------------------------ Description -----------------------------------
# Leverages gpt-commit-prompter to auto-generate commit messages for staged
# changes in a Git repository. A diff named staged_changes.diff is produced and
# fed to the AI helper. The result is saved to tmp_commit_msg.txt, which is then
# used as a template for the "git commit" command, permitting user review.
#
# ---------------------------- Prerequisites -----------------------------------
# - Staged changes should be in the repo.
# - gpt-commit-prompter should be in the current directory or in the PATH.
# - tmp_commit_msg.txt will be overwritten each run; backup crucial data.
# - Set tmp_commit_msg.txt as commit template via:
# git config --global commit.template tmp_commit_msg.txt
#
# -------------------------------- Options -------------------------------------
# For options, execute "gc-smart --help" or refer to the print_help function.
#
# ==============================================================================
# Ignoring false positives, see: https://github.com/koalaman/shellcheck/issues/2660
# shellcheck disable=SC2317
cleanup() {
# Deactivate the script's virtual environment if it was activated
if [ -n "$VIRTUAL_ENV" ] && [ "$VIRTUAL_ENV" != "$original_venv" ]; then
deactivate
fi
# Reactivate the original virtual environment if there was one
if [ -n "$original_venv" ] && [ "$VIRTUAL_ENV" != "$original_venv" ]; then
# shellcheck disable=SC1091
source "$original_venv/bin/activate"
fi
# Restore the original commit template
restore_original_commit_template
# Additional cleanup logic if needed
if ! $KEEP_FILES; then
rm -f "$GIT_ROOT/git_error.log"
rm -f "$GIT_ROOT/staged_changes.diff"
rm -f "$GIT_ROOT/tmp_commit_msg.txt"
fi
}
# Set trap to call cleanup function upon script exit
trap cleanup EXIT
# Determine the root directory of the gc-smart script
GCS_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Store the current virtual environment (if any)
original_venv="$VIRTUAL_ENV"
# Path to the virtual environment for the gpt-commit-prompter script
VENV_PATH="$GCS_ROOT/python/venv"
# Source additional script files for modular functionality
source "$GCS_ROOT/bash/arg-checks.sh"
source "$GCS_ROOT/bash/help-message.sh"
source "$GCS_ROOT/bash/error-handling.sh"
source "$GCS_ROOT/bash/commit-preview-handler.sh"
source "$GCS_ROOT/bash/template_handler.sh"
# Set the git command, either default or as specified by the user
set_git_cmd "$@"
# Determine the root directory of the current Git repository
GIT_ROOT=$(${GIT_CMD} rev-parse --show-toplevel)
# Parse and handle script arguments
check_for_help "$@"
set_keep_files_flag "$@"
set_preview_flag "$@"
set_instruction "$@"
set_style_option "$@"
# Check for prerequisites and conditions
if [[ "$GIT_CMD" == "git" ]]; then
# Only perform this check if using the default git command
check_git_repo
fi
# Check for staged changes, AI helper script, and commit template
check_staged_changes
check_ai_helper
# Create a diff file of the staged changes
$GIT_CMD diff --cached > "$GIT_ROOT/staged_changes.diff"
# Activate the virtual environment
# shellcheck disable=SC1091
source "$VENV_PATH/bin/activate"
# Backup existing commit template and set new one
backup_and_set_commit_template
# Generate the commit message using the AI helper
generate_commit_message
# If preview is enabled, handle the preview and interaction process
if $PREVIEW; then
handle_preview
fi
# Commit the changes to the Git repository and check the result
if ! $GIT_CMD commit 2> "$GIT_ROOT/git_error.log"; then
# Handle any commit errors
handle_git_commit_error
else
# Indicate successful commit
echo "Commit successful."
fi
# Normal exit after successful execution.
echo "Execution of gc-smart completed successfully."
exit 0