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

Skip to content
Merged

next #105

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
1 change: 0 additions & 1 deletion conf/bash/scripts/git-commit-chunk-text.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ commit_with_text() {
done

# Execute the git commit command
echo "Executing: ${git_cmd[*]}"
"${git_cmd[@]}"
}

Expand Down
148 changes: 148 additions & 0 deletions conf/bash/scripts/jj-ai-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#!/usr/bin/env bash

# This script will run the jj describe command with input text for a given revision
# 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: jj describe -r <rev> -m "title" -m "description line 1" -m "description line 2" .........

set -euo pipefail

# Function to show usage
show_usage() {
echo "Usage: $0 <rev> [OPTIONS]"
echo "Updates commit message for the given revision using multi-line text input"
echo ""
echo "Arguments:"
echo " <rev> Revision to update (required)"
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 " $0 @ -f commit_message.txt"
echo " $0 @ < commit_message.txt"
echo " aichat 'generate commit message' | $0 @"
}

# Function to process commit text and execute jj describe
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
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[@]}"
}

# Main script logic
main() {
local rev=""
local input_text=""
local read_from_file=""

# Check if at least one argument is provided
if [[ $# -eq 0 ]]; then
echo "Error: Revision argument is required" >&2
show_usage
exit 1
fi

# First argument is the revision
rev="$1"
shift

# Parse remaining command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-f|--file)
if [[ -z "${2:-}" ]]; then
echo "Error: -f/--file requires a filename" >&2
exit 1
fi
read_from_file="$2"
shift 2
;;
*)
echo "Error: Unknown option $1" >&2
show_usage
exit 1
;;
esac
done

# Read input based on source
if [[ -n "$read_from_file" ]]; then
# Read from file
if [[ ! -f "$read_from_file" ]]; then
echo "Error: File '$read_from_file' not found" >&2
exit 1
fi
input_text=$(cat "$read_from_file")
elif [[ ! -t 0 ]]; then
# Read from stdin (pipe or redirect)
input_text=$(cat)
else
# No input source provided
echo "Error: No input provided. Use -f to read from file or pipe/redirect input" >&2
show_usage
exit 1
fi

# Check if input is empty
if [[ -z "$input_text" ]]; then
echo "Error: No input provided" >&2
exit 1
fi

# Process and describe
describe_with_text "$rev" "$input_text"
}

# Run main function with all arguments
main "$@"
100 changes: 100 additions & 0 deletions conf/bash/scripts/jj-commit-context.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash

# get context for llm jj commit message generation
# script should redirect output to stdout, so it can be used in a pipeline

## The context should include:

# 1. Changes in the specified revision
# 2. Last 10 commit messages

## It should output in well formatted text

## Steps

### 1. check if repo is a jj repository, exit 1 if not
### 2. Get the revision argument (default to @)
### 3. Get changes for the specified revision
### 4. Get last 10 commit messages
### 5. Format the output in well formatted text
### 6. Redirect the output to stdout
### 7. Exit 0

set -euo pipefail

# Function to check if we're in a jj repository
check_jj_repo() {
if ! jj status >/dev/null 2>&1; then
echo "Error: Not in a jj repository" >&2
exit 1
fi
}

# Function to get the revision argument
get_revision() {
local rev="${1:-@}"
echo "$rev"
}

# Function to get changes for the specified revision
get_revision_changes() {
local rev="$1"
echo "=== STAGED CHANGES ==="
echo
# Show the actual diff
echo "Changes:"
jj diff -r "$rev" --stat --color=never --no-pager
echo
jj diff -r "$rev" --color=never --no-pager
echo
}

# Function to get last 10 commit messages using template
get_recent_commits() {
local rev="$1"
echo "=== RECENT COMMIT HISTORY ==="
echo
echo "Last 10 commits:"
# Use template to format output similar to git log --oneline
jj log -n 10 -r "..$rev" --no-pager --no-graph --color=never \
-T 'change_id.short() ++ " " ++ description.first_line() ++ "\n"'
echo
}

# Function to show usage
usage() {
echo "Usage: $0 [revision]"
echo " revision: The revision to analyze (default: @)"
echo " Examples:"
echo " $0 # Analyze working copy (@)"
echo " $0 @- # Analyze parent of working copy"
echo " $0 abc123 # Analyze specific revision"
exit 1
}

# Main function
main() {
# Check for help flag
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
fi

check_jj_repo

local rev
rev=$(get_revision "${1:-}")

echo "Git Commit Context"
echo "=================="
echo

get_revision_changes "$rev"
get_recent_commits "$rev"

echo "=== END OF CONTEXT ==="
}

# Run main function
main "$@"

exit 0
Loading
Loading