From: Alexander Joss Date: Wed, 18 Nov 2026 23:09:35 +0000 (-0800) Subject: refactor to use a temporary index instead of switching branches X-Git-Tag: v1.0.5^0 X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=HEAD;p=git-extra refactor to use a temporary index instead of switching branches The previous approach had performance issues when used with large repositories. --- diff --git a/git-extra b/git-extra index 90c1c9f..9025962 100644 --- a/git-extra +++ b/git-extra @@ -12,9 +12,7 @@ TEMPLATE=${GIT_EXTRA_TEMPLATE:-} # helper variables LONG_OUTPUT=false POSITIONAL_ARGUMENTS= -PREV_COMMIT= UTILITY="ls" -WORKING_BRANCH= detect_git() { if [[ -z "$(git --version)" ]]; then @@ -158,45 +156,30 @@ branch_exists() { fi } -branch_hash() { - # changes to extra branch and stores prevous commit hash in PREV_COMMIT - branch_exists - - WORKING_BRANCH=$(git rev-parse --verify HEAD --abbrev-ref) - - git symbolic-ref HEAD "refs/heads/$BRANCH" - git reset > /dev/null - - PREV_COMMIT=$(git rev-parse --verify HEAD) -} - -branch_reset() { - log_info "reseting branch back to $WORKING_BRANCH" - - git symbolic-ref HEAD "refs/heads/$WORKING_BRANCH" - git reset > /dev/null -} - add_to_index() { local commit_hash local hash + local index + local prev_commit local tree_hash log_info "adding $2 to index" - # fills PREV_COMMIT with $BRANCH's previous commit hash - branch_hash + branch_exists hash=$(git hash-object "$1" -w) - rm "$1" + prev_commit=$(git rev-parse --verify "refs/heads/$BRANCH") - git update-index --add --cacheinfo 100644 "$hash" "$2" - tree_hash=$(git write-tree) - commit_hash=$(git commit-tree -m "$MESSAGE" -p "$PREV_COMMIT" "$tree_hash") + index=$(mktemp) + GIT_INDEX_FILE="$index" git read-tree "refs/heads/$BRANCH" + GIT_INDEX_FILE="$index" git update-index --add --cacheinfo "100644,$hash,$2" + tree_hash=$(GIT_INDEX_FILE="$index" git write-tree) - git update-ref "refs/heads/$BRANCH" "$commit_hash" - - branch_reset + commit_hash=$(git commit-tree -m "$MESSAGE" -p "$prev_commit" "$tree_hash") + + git update-ref "refs/heads/$BRANCH" "$commit_hash" "$prev_commit" + + rm -f "$index" "$index.lock" "$1" } add() { diff --git a/manifest.scm b/manifest.scm index ca1ff2e..cd58289 100644 --- a/manifest.scm +++ b/manifest.scm @@ -1,6 +1,9 @@ (specifications->manifest (list "bash" "coreutils" "debianutils" + "gawk" + "git" "grep" "make" + "sed" "shellcheck")) diff --git a/test/add.tests.sh b/test/add.tests.sh index ac7283a..07ee865 100755 --- a/test/add.tests.sh +++ b/test/add.tests.sh @@ -38,8 +38,7 @@ set -e add "todo" > result 2>&1 TEMP=`head -n 1 result | sed 's/opening temporary file //' | tr -d "'"` EXPECTED="opening temporary file '$TEMP'" -EXPECTED+=$'\nadding todo to index\nreseting branch back to ' -EXPECTED+="$WORKING_BRANCH" +EXPECTED+=$'\nadding todo to index' EXPECTED+=$'\nadded todo' RESULT=$( "$TEMP" add_to_index "$TEMP" "todo" > result 2>&1 EXPECTED=$'adding todo to index\ncreating branch ' EXPECTED+="$BRANCH" -EXPECTED+=$'\nreseting branch back to ' -EXPECTED+="$WORKING_BRANCH" RESULT=$( result 2>&1 -EXPECTED="creating branch $BRANCH" -RESULT=$( result 2>&1 -EXPECTED="creating branch $BRANCH" -RESULT=$( result 2>&1 -EXPECTED="reseting branch back to $WORKING_BRANCH" -RESULT=$(