about summary refs log tree commit diff
path: root/pkgs/profpatsch/git-commit-index/lib.sh
blob: 229eec1e56c33660e1b961e4ad28ceeac27ef677 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
set -euo pipefail

# nix-shell -p cdb --run 'bash -c \'source ~/tmp/gitcdb.sh; for r in $(findRepos ~/kot); do genIndex . "$r"; done\''

findRepos () {
    find "$1" -type d -name ".git"
    # TODO check each repo is actually git repo
}

genIndex () {
    local indexDir=$(realpath -- "$1")
    local path=$(realpath -- "$2")
    if [ ! -d "$indexDir" ]; then
        echo "index directory does not exist: $indexDir"
        exit 111
    fi
    # TODO: multimap failure
    #
    local filename="$indexDir/$(echo $path | sed -e 's|_|__|g' -e 's|/|_|g')"
    local pathLength=$(echo "$path" | wc --bytes | tr -d '\n')
    (pushd "$path" > /dev/null \
            && ( git log --all --format="format:%H" \
                     | sed -e "s/^\(.*\)$/+40,0:\1->/" \
               ; echo \
               ; echo "+8,${pathLength}:git-path->${path}" \
               ; echo; echo \
               ) \
            | cdbmake "$filename" "$filename.tmp" \
    )
}

query () {
    local indexDir=$(realpath -- "$1")
    local key="$2"

    local found=0
    local result=

    # TODO make this parallel (and switch away from bash)
    for f in "$indexDir"/*; do

        set +e
        # don't need result because empty string
        <"$f" cdbget "$key" >/dev/null
        local ret=$?
        set -e

        case $ret in
            0)
                # TODO: back
                found=1

                set +e
                # now find original path
                local origDotGit=$(<"$f" cdbget "git-path")
                local retGitPath=$?
                set -e

                case $retGitPath in
                    0)
                        :
                        ;;
                    100)
                        echo "shouldn’t happen; git-path was not in $f"
                        exit 127
                        ;;
                    111)
                        echo "db error in $f"
                        exit 111
                        ;;
                    *)
                        echo "shouldn’t happen; exitcode was $ret"
                        exit 127
                        ;;
                esac

                # return workspace file
                result=$(dirname "$origDotGit")
                break
                ;;
            100)
                # not found
                :
                ;;
            111)
                echo "db error in $f"
                exit 111
                ;;
            *)
                echo "shouldn’t happen; exitcode was $ret"
                exit 127
                ;;
        esac
    done

    if [ $found -eq 0 ]; then
        exit 100
    else
        echo "$result"
        exit 0
    fi
}