Skip to content

Instantly share code, notes, and snippets.

@youknowjack
Last active November 27, 2023 17:23
Show Gist options
  • Save youknowjack/8ebd70da63669f8fc46d010a5944ccd9 to your computer and use it in GitHub Desktop.
Save youknowjack/8ebd70da63669f8fc46d010a5944ccd9 to your computer and use it in GitHub Desktop.

Revisions

  1. youknowjack revised this gist Nov 27, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-blob-ids.sh
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    #
    # -r REF an optional reference to a commit (hash or symbolic like HEAD), default: HEAD
    # -p PATH an option path to a subtree in repo, default is blank for root tree
    # -s shortens blob hashs to 6 characters
    # -s shortens blob hash ids to 6 characters
    #

    function short { echo $@ | awk '{print substr($1,0,6);}'; }
  2. youknowjack created this gist Nov 27, 2023.
    72 changes: 72 additions & 0 deletions git-blob-ids.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/bin/bash
    #
    # git-blob-ids: List blobs (hash id and path) in a git repo
    #
    # Usage: git-blob-ids [-s] [-r REF] [-p PATH]
    #
    # -r REF an optional reference to a commit (hash or symbolic like HEAD), default: HEAD
    # -p PATH an option path to a subtree in repo, default is blank for root tree
    # -s shortens blob hashs to 6 characters
    #

    function short { echo $@ | awk '{print substr($1,0,6);}'; }
    function nochange { echo $@; }

    ref=HEAD
    path=
    idfunc=nochange
    while getopts "sr:p:" options; do
    case "${options}" in
    s)
    idfunc=short
    ;;
    r)
    ref=${OPTARG}
    ;;
    p)
    path=${OPTARG}
    ;;
    esac
    done

    if [[ ${#path} > 0 ]]; then
    tree=$ref:$path
    else
    # start from root tree
    tree=(`git cat-file -p $ref | head -1 | awk '{print $2}'`)
    tree="${tree}|"
    fi

    # tokenize by line in git command output
    IFS='
    '

    # breadth-first iteration of sub-trees
    while [[ ${#tree} > 0 ]]; do
    el=`echo $tree | awk '{print $1}'`
    t=`echo $el | awk -F"|" '{print $1}'`
    path=`echo $el | awk -F"|" '{print $2}'`
    if [[ ${#path} > 0 ]]; then
    pfx=$path/
    fi
    # pop
    tree=`echo $tree | sed 's/[^ ]* *//'`
    for l in `git cat-file -p $t`; do
    if [[ $l =~ "blob" ]]; then
    blob=`echo $l | sed 's/.* blob //;s/\t.*//'`
    blob=`$idfunc $blob`
    lpath=`echo $l | sed 's/.* blob //;s/.*\t//'`
    # output blob info
    echo $blob $pfx$lpath
    elif [[ $l =~ "tree" ]]; then
    id=`echo $l | sed 's/.* tree //;s/\t.*//'`
    lpath=`echo $l | sed 's/.* tree //;s/.*\t//'`
    # push
    if [[ ${#tree} > 0 ]]; then
    tree="${tree} $id|$pfx$lpath"
    else
    tree="$id|$pfx$lpath"
    fi
    fi
    done
    done