git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Misusing git: trimming old commits

From
Martin Langhoff <martin.langhoff@gmail.com>
Date
May 11, 2013, 16:56 UTC
Message-ID
<CACPiFCK3b2Ohr1rNSkxa2LFwVOo+w+NQ6D6AXwwHifQo_Lcp_g@mail.gmail.com>
In-Reply-To
<CACPiFC+XKVzNk2fm4beer0xD6yBnVW6QXCrSFm2w8T2r68xGvg@mail.gmail.com>

On Thu, May 9, 2013 at 11:40 AM, Martin Langhoff <martin.langhoff@gmail.com> wrote:

> With the exaction of the final destination, I want to expire reports
> that are old and successfully transferred.

OK, that took some effort to make work. Make sure you are not using reflogs (or that reflogs are promptly expired).

# right after a successful push of all heads to the receiving server...
for head in $(git branch|sed 's/^..//'); do
    # FIXME period
    graft_sha1=$(git log --until=one.month.ago -n1 --pretty=format:%H ${head})
    if [[ -z "$graft_sha1" ]]; then
        # nothing to prune
        continue
    fi
    # is it already grafted?
    if grep -q "^${graft_sha1} " "${GIT_DIR}/info/grafts" &>/dev/null ; then
        # don't duplicate the graft
        continue
    fi
    some_grafted='true'
    # prep empty commit
    # skip git read-tree --empty, we get the same with
    export GIT_INDEX_FILE=/tmp/ppg-emptytree.$$
    empty_tree="$(git write-tree)"
    rm ${GIT_INDEX_FILE}
    unset GIT_INDEX_FILE
    empty_commit=$(git commit-tree -m empty ${empty_tree})
    echo "${graft_sha1} ${empty_commit}" >> ${GIT_DIR}/info/grafts
done
if [[ -z "$some_grafted" ]]; then
    # nothing to do
    exit 0
fi

# ppg-repack makes the unreachable objects "loose" # (it is git-repack hacked to remove --keep-true-parents), # git prune --expire actually deletes them $PPG_EXEC_PATH/ppg-repack -AFfd git prune --expire=now

### Cleanup stale grafts
# current grafts points are reachable,
# pruned graft points (made obsolete by a newer graft)
# cannot be retrieved and git cat-file exit code is 128
touch ${GIT_DIR}/info/grafts.tmp.$$
while read line; do
    graftpoint=$(echo "${line}" | cut -d' ' -f1)
    if git cat-file commit ${graftpoint} &>/dev/null ; then
        echo ${line} >> ${GIT_DIR}/info/grafts.tmp.$$
    fi
done < "${GIT_DIR}/info/grafts"
if [ -s ${GIT_DIR}/info/grafts.tmp.$$ ]; then
    mv ${GIT_DIR}/info/grafts.tmp.$$ ${GIT_DIR}/info/grafts
fi
Perhaps it helps someone else trying to run git as a spooler :-)
cheers,
m
--
 martin.langhoff@gmail.com
 -  ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 ~ http://docs.moodle.org/en/User:Martin_Langhoff
Previous: Martin Langhoff
Message 2 of 2 in “Misusing git: trimming old commits”
  1. Martin LanghoffMay 9, 2013
  2. Martin LanghoffMay 11, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.