Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 9 additions & 55 deletions conf/bash/scripts/git-commit-chunk-text.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,29 @@

# This script will run the git commit command with input text
# Input text is long text with empty lines.
# First line is commit message, rest is description.
# to make long description with line break, we need to use multiple `-m`
# Eg: git commit -m "title" -m "description line 1" -m "description line 2" .........
# The entire input (from file or stdin) is used as the commit message.
# Uses 'git commit -F -' to read the message from stdin, which supports multi-line messages with blank lines.
# Eg: echo -e "Title\n\nDescription line 1\nDescription line 2" | $0
# $0 -f commit_message.txt
# $0 < commit_message.txt

set -euo pipefail

# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo "Commits changes using multi-line text input"
echo "Commits changes using multi-line text input (title and body)"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -f, --file Read commit message from file"
echo ""
echo "Examples:"
echo " echo 'Title\n\nDescription line 1\nDescription line 2' | $0"
echo " echo -e 'Title\\n\\nDescription line 1\\nDescription line 2' | $0"
echo " $0 -f commit_message.txt"
echo " $0 < commit_message.txt"
}

# Function to process commit text and execute git commit
commit_with_text() {
local input_text="$1"

# Split input into lines and process
local lines=()

# Use readarray to split input into lines
readarray -t lines <<< "$input_text"
local line_count=${#lines[@]}

# Check if we have at least one line
if [[ $line_count -eq 0 ]]; then
echo "Error: No commit message provided" >&2
exit 1
fi

# First line is the commit title
local commit_title="${lines[0]}"

# Check if title is empty
if [[ -z "$commit_title" ]]; then
echo "Error: Commit title cannot be empty" >&2
exit 1
fi

# Build git commit command
local git_cmd=("git" "commit" "-m" "$commit_title")

# Add description lines (skip first line and any immediately following empty lines)
local in_description=false
for ((i=1; i<line_count; i++)); do
local current_line="${lines[i]}"

# Skip empty lines immediately after title until we find content
if [[ -z "$current_line" ]] && [[ "$in_description" == false ]]; then
continue
fi

# Once we find content, we're in description mode
in_description=true
git_cmd+=("-m" "$current_line")
done

# Execute the git commit command
"${git_cmd[@]}"
}

# Main script logic
main() {
local input_text=""
Expand Down Expand Up @@ -123,8 +77,8 @@ main() {
exit 1
fi

# Process and commit
commit_with_text "$input_text"
# Use git commit -F - to read the message from stdin
echo "$input_text" | git commit -F -
}

# Run main function with all arguments
Expand Down
43 changes: 5 additions & 38 deletions conf/bash/scripts/jj-ai-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,15 @@ describe_with_text() {
local rev="$1"
local input_text="$2"

# Split input into lines and process
local lines=()

# Use readarray to split input into lines
readarray -t lines <<< "$input_text"
local line_count=${#lines[@]}

# Check if we have at least one line
if [[ $line_count -eq 0 ]]; then
# Check if input is empty
if [[ -z "$input_text" ]]; then
echo "Error: No commit message provided" >&2
exit 1
fi

# First line is the commit title
local commit_title="${lines[0]}"

# Check if title is empty
if [[ -z "$commit_title" ]]; then
echo "Error: Commit title cannot be empty" >&2
exit 1
fi

# Build jj describe command
local jj_cmd=("jj" "describe" "--no-edit" "-r" "$rev" "-m" "$commit_title")

# Add description lines (skip first line and any immediately following empty lines)
local in_description=false
for ((i=1; i<line_count; i++)); do
local current_line="${lines[i]}"

# Skip empty lines immediately after title until we find content
if [[ -z "$current_line" ]] && [[ "$in_description" == false ]]; then
continue
fi

# Once we find content, we're in description mode
in_description=true
jj_cmd+=("-m" "$current_line")
done

# Execute the jj describe command
"${jj_cmd[@]}"
# Use stdin to pass the commit message to avoid issues with messages starting with dashes
# This is more robust than using multiple -m arguments
echo "$input_text" | jj describe --stdin --no-edit -r "$rev"
}

# Main script logic
Expand Down
49 changes: 47 additions & 2 deletions conf/bash/scripts/jj-commit-context.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
# NOTE: This script limits the amount of context provided to avoid exceeding
# the AI model's token limits (max_input_tokens). Large diffs and too much
# commit history can cause "Exceed max_input_tokens limit" errors.
#
# IMPORTANT: jj diff returns exit code 3 (not the typical SIGPIPE exit code 141)
# when used in a pipeline with head. This is jj's way of handling SIGPIPE when
# the pipe is closed early. The script handles this by treating exit code 3 as
# acceptable in pipeline contexts.

## The context should include:

Expand Down Expand Up @@ -43,32 +48,67 @@ get_revision() {
# Function to get changes for the specified revision
get_revision_changes() {
local rev="$1"

echo "=== STAGED CHANGES ==="
echo
# Show compact summary with file stats
echo "Files changed:"
jj diff -r "$rev" --stat --color=never --no-pager
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Error: jj diff --stat failed with exit code: $exit_code" >&2
exit $exit_code
fi
echo

# Check if diff is too large
local diff_lines=$(jj diff -r "$rev" --color=never --no-pager | wc -l)
local max_diff_lines=50 # Conservative limit for token usage
local diff_lines=$(jj diff -r "$rev" --color=never --no-pager | wc -l | tr -d ' ')
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Error: jj diff | wc -l failed with exit code: $exit_code" >&2
exit $exit_code
fi
local max_diff_lines=100 # Conservative limit for token usage
local minimal_threshold=200 # If diff is huge, use minimal output

if [ "$diff_lines" -gt "$minimal_threshold" ]; then
echo "Very large diff detected ($diff_lines lines). Showing minimal summary:"
# For huge diffs, just show summary stats
jj diff -r "$rev" --stat --color=never --no-pager
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Error: jj diff --stat (minimal) failed with exit code: $exit_code" >&2
exit $exit_code
fi
echo
echo "... (full diff omitted due to size - $diff_lines lines total) ..."
elif [ "$diff_lines" -gt "$max_diff_lines" ]; then
echo "Large diff detected ($diff_lines lines). Showing truncated diff:"

# Temporarily disable pipefail for this command to handle SIGPIPE gracefully
# NOTE: jj diff returns exit code 3 (not 141) when used in a pipeline with head
# This is jj's way of handling SIGPIPE when the pipe is closed early by head
set +o pipefail
jj diff -r "$rev" --color=never --no-pager | head -n "$max_diff_lines"
local jj_exit_code=${PIPESTATUS[0]} # Get exit code of jj diff
local head_exit_code=${PIPESTATUS[1]:-0} # Get exit code of head, default to 0 if not set
set -o pipefail

# Ignore SIGPIPE (exit code 141) and jj's exit code 3 which happens when head closes the pipe early
if [ $jj_exit_code -ne 0 ] && [ $jj_exit_code -ne 141 ] && [ $jj_exit_code -ne 3 ]; then
echo "Error: jj diff (truncated) failed with exit code: $jj_exit_code" >&2
exit $jj_exit_code
fi
echo
echo "... (diff truncated - $((diff_lines - max_diff_lines)) lines omitted) ..."
else
echo "Full diff:"
jj diff -r "$rev" --color=never --no-pager
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Error: jj diff (full) failed with exit code: $exit_code" >&2
exit $exit_code
fi
fi
echo
}
Expand All @@ -82,6 +122,11 @@ get_recent_commits() {
# Use template to format output similar to git log --oneline
jj log -n 2 -r "..$rev" --no-pager --no-graph --color=never \
-T 'change_id.short() ++ " " ++ description.first_line() ++ "\n"'
local exit_code=$?
if [ $exit_code -ne 0 ]; then
echo "Error: jj log failed with exit code: $exit_code" >&2
exit $exit_code
fi
echo
}

Expand Down
Loading