1 module doveralls.git;
2 import std.json;
3 
4 JSONValue getGitEntry(string repoPath)
5 {
6     version(Have_dlibgit)
7     {
8         import git;
9         import std.range;
10 
11         auto repo = openRepository(repoPath);
12         auto head = repo.lookupCommit(repo.head.target);
13 
14         auto info = [
15             "head": JSONValue(
16             [
17                 "id": head.id.toHex(),
18                 "author_name": head.author.name,
19                 "author_email": head.author.email,
20                 "committer_name": head.committer.name,
21                 "committer_email": head.committer.email,
22                 "message": head.message()
23             ])
24         ];
25 
26         repo.iterateBranches(GitBranchType.local, (branch, type)
27         {
28             if (!branch.isHead) return ContinueWalk.yes;
29             info["branch"] = branch.name;
30             return ContinueWalk.no;
31         });
32 
33         info["remotes"] = repo.listRemotes.map!(n => loadRemote(repo, n))
34             .map!(r => JSONValue(["name": r.name, "url": r.url])).array();
35 
36         return JSONValue(info);
37     }
38     else
39     {
40         import std.algorithm, std.process, std.range, std.string;
41 
42         auto res = execute(["git", "-C", repoPath, "log", "-1", "--pretty=format:%H\n%aN\n%ae\n%cN\n%ce\n%s"]);
43         if (res.status)
44             return JSONValue(null);
45         string[6] parts;
46         res.output.splitter('\n').takeExactly(parts.length).copy(parts[]);
47 
48         auto info = [
49             "head": JSONValue(
50             [
51                 "id": parts[0],
52                 "author_name": parts[1],
53                 "author_email": parts[2],
54                 "comitter_name": parts[3],
55                 "comitter_email": parts[4],
56                 "message": res.output[&parts[5][0] - &res.output[0] .. $],
57             ])
58         ];
59 
60         res = execute(["git", "-C", repoPath, "rev-parse", "--abbrev-ref", "HEAD"]);
61         if (res.status)
62             return JSONValue(info);
63         info["branch"] = res.output.stripRight();
64 
65         res = execute(["git", "-C", repoPath, "remote", "-v"]);
66         if (res.status)
67             return JSONValue(info);
68 
69         JSONValue[] remotes;
70         foreach (line; res.output.splitter('\n'))
71         {
72             if (!line.endsWith("(fetch)")) continue;
73             line.splitter().takeExactly(2).copy(parts[0 .. 2]);
74             remotes ~= JSONValue(["name": parts[0], "url": parts[1]]);
75         }
76         info["remotes"] = remotes;
77         return JSONValue(info);
78     }
79 }