ctucx.git: nimstagit

[nimlang] incomplete reimplementation of stagit

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 
import os, posix, times, json, tables, algorithm, uri
import nimgit

import types, utils

proc reposOverview* (config: Config): seq[Category] =
    template getRepoMetadata = 
        try:
            let
              repoName      = nameFromPath(repoPath)
              repository    = openGitRepository(repoPath)
              config        = repository.config
              objId         = repository.lookupObjectIdByName("HEAD")
              commit        = repository.lookupCommit(objId)
              category      = config.get("gitweb.category")

            if not categoriesTable.hasKey(category):
                categoriesTable[category] = newSeq[Repository]()

            categoriesTable[category].add(Repository(
                    name:         repoName,
                    url:          encodeUrl(repoName),
                    description:  config.get("gitweb.description"),
                    lastActivity: commit.time.time.format("yyyy'.'MM'.'dd HH:mm"),
                    objId:        $objId
                ))

            free(commit)
            free(objId)
            free(config)
            free(repository)

        except:
            echo "This seems to not be a git-repo: " & repoPath
            echo "Error:\n" & getCurrentExceptionMsg()

    var categoriesTable = initTable[string, seq[Repository]]()

    if config.projectsList.len != 0:
        for element in config.projectsList:
            let repoPath = joinPath(config.scanPath, element)
            getRepoMetadata()

    for name, repos in categoriesTable:
        var repos = repos

        repos.sort(proc (x, y: Repository): int = cmp(x.name, y.name))
        
        result.add(Category(
                name: name,
                repos: repos
            ))

    result.sort(proc (x, y: Category): int = cmp(x.name, y.name))