Skip to content

armgit / janas

Public Imported from GitHub
0

37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.

Release 2026-10-02: janas-github and janas-git - GitHub's projects and the computer's repositories as MCP services; tools that change something asked every time

camauri9cecbcc6736a
diff --git a/ChangeLog.md b/ChangeLog.md
index 4fc6271..9ae883e 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -2,6 +2,14 @@
> Curated, user-facing summary of completed work, newest first.
+## [2026-10-02] - janas-github and janas-git
+
+- **janas-github**, GitHub as an MCP service that only reads: a repository's issues and pull requests (`new`: those opened since the last time asked, kept in `~/.config/janas/github-seen.txt`), its latest releases (its tags when it has none), a branch's recent commits, what it is. A name alone is looked for among the user's repositories, else the one with the most stars, and the answer says which. The token of `GH_TOKEN`, `GITHUB_TOKEN` or `gh auth token`, kept in memory only; without one, 60 requests an hour. Layouts for every answer; `tests/test_github_parse.c` on answers of 2 October.
+- **janas-git**, the computer's git repositories: `git_status`, `git_log`, `git_diff`, `git_branches` read; `git_commit`, `git_pull` (fast-forward only), `git_push` (never by force), `git_switch` change something and say so (`readOnlyHint` false). Nothing that throws work away; git without a shell, without a terminal, with a time limit; no argument of the model's taken as an option. `tests/test_git_parse.c`.
+- **janas-chat asks every time for a tool that changes something**, with `--mcp-auto` too and never "always", showing the call whole; on a pipe it is not run (`mcpc_writes`).
+- **Translations of layouts**: a text given back untranslated is refused and asked again the next time; git's and GitHub's layouts get the terms of the craft from the glossary (commit, branch, stage...), the others keep theirs; the states of an issue ("aperta", not the "aperte" the model wrote for one) from the glossary in Italian, French, Spanish and German.
+- **A redirect to another host loses the `Authorization` header** (`janas_https_get`), as curl does.
+
## [2026-10-02] - janas-maps: roads and places
- **janas-maps**, roads and places as an MCP service, from OpenStreetMap's data through free services with no key: a route by car, bike or on foot between two places or through a third, avoiding tolls, motorways or ferries when asked, with its length, time without traffic, main roads, up to two other ways, the way step by step in the computer's language and a link to the map (`maps_route`: Valhalla on FOSSGIS' servers, OSRM's route without steps when Valhalla does not answer); journeys by public transport leaving or arriving at a time, leg by leg (`maps_transit`: Transitous, over the operators' open timetables); the places of a kind around a place, nearest first, from a list of twenty kinds (`maps_nearby`: Overpass); where a place or an address is (`maps_find`: Nominatim, Photon when it does not answer). Every answer is data with a layout: the user sees the steps and the journeys, the model reads a brief. `janas-chat` starts it by itself; [docs/services/maps.md](docs/services/maps.md).
diff --git a/README.md b/README.md
index 771ce90..6d06720 100644
--- a/README.md
+++ b/README.md
@@ -55,6 +55,8 @@ Everything the engine does is chosen from what it measures, so another machine w
- **`janas-flights`:** where a flight is and what flies over a sea or a place, as MCP tools, from the ADS-B receivers of the community, and today's schedules with a free AviationStack key: the first of Janas's services ([docs/services](docs/services/README.md)).
- **`janas-weather`:** the weather now, with what the nearest station measured, the forecast, the sea and its waves, and the warnings in force (in Italy the Civil Protection's levels), from free sources with no key ([docs/services/weather.md](docs/services/weather.md)).
- **`janas-maps`:** roads and places: a route by car, bike or on foot, step by step in your language, journeys by public transport, what is near (fuel, a pharmacy, a cash machine...), where an address is; OpenStreetMap's data through free services with no key ([docs/services/maps.md](docs/services/maps.md)).
+- **`janas-github`:** a project on GitHub: its issues and pull requests (the new ones since you last asked), its latest releases, its recent commits, what it is; it only reads, with the gh command's token when you have one ([docs/services/github.md](docs/services/github.md)).
+- **`janas-git`:** the git repositories of your computer: what has changed, the commits, the branches, a diff; and a commit, pull, push or switch of branch, each shown whole and asked of you first; nothing that throws work away ([docs/services/git.md](docs/services/git.md)).
- **`janas-wiki`:** Wikipedia when you ask for it: its search, a page shown to you as it is (the introduction, then the section asked for) while the model reads only a note of it, and the pages about what lies around a place ([docs/services/wiki.md](docs/services/wiki.md)).
- **`janas-server`:** the model behind an HTTP API that follows OpenAI's, so that clients written for it can use a model running on this machine: chat and completions with tools, JSON output held to a schema and log-probabilities, the Responses API with its conversations, embeddings and moderations, a chat page of its own to try it from a browser ([below](#using-it-over-http)), and code completion for VSCodium and VS Code ([docs/code-completion.md](docs/code-completion.md)).
- **`janas-bench`:** measures the machine, fills its profile and writes a report.
@@ -295,7 +297,7 @@ printf 'Explain mixture of experts in one paragraph.\n/quit\n' \
### Janas's services
-The chat starts by itself the services of Janas it finds beside it - `janas-flights`, `janas-maps`, `janas-weather` and `janas-wiki` - and the model uses their tools when a question needs them, without being told: ask "which flights are over the Tyrrhenian Sea now?", "what's the weather like?", "how do I get to Palermo by train?" or "show me Wikipedia's page on Zeffirelli" (with no place, where you are: `JANAS_LOCATION`, or a guess from the internet connection). `--no-services` starts none. How they work, their keys, and how to use them in Claude Code and other clients: [docs/services](docs/services/README.md).
+The chat starts by itself the services of Janas it finds beside it - `janas-flights`, `janas-git`, `janas-github`, `janas-maps`, `janas-weather` and `janas-wiki` - and the model uses their tools when a question needs them, without being told: ask "which flights are over the Tyrrhenian Sea now?", "what's the weather like?", "how do I get to Palermo by train?", "are there new issues on Janas?", "what has changed in this repository?" or "show me Wikipedia's page on Zeffirelli" (with no place, where you are: `JANAS_LOCATION`, or a guess from the internet connection). `--no-services` starts none. How they work, their keys, and how to use them in Claude Code and other clients: [docs/services](docs/services/README.md).
**The services' tools are given when they are needed.** Written into the system message, the tools of three services made it four thousand tokens, over a minute of a large model before the first message, and every service more made it longer. The system message now holds a catalog, a line a service, and the model opens a service when a question needs it: that service's instructions and tools come back as the answer of the call, its tools can be called from then on, and the next reply is held to be a call (a small model told to call one made the answer up instead). With Qwen3.6-35B-A3B the system message went from 4,057 tokens to 616, and opening the weather costs about a thousand tokens, once in a conversation, when the first question about the weather comes. `--all-tools` writes them all into the system message as before.
@@ -597,6 +599,7 @@ It stands on the work of others, and says so:
- **`janas-flights`** reads the positions of aircraft from [adsb.lol](https://adsb.lol) (data under the Open Database License) and [adsb.fi](https://adsb.fi), routes from [adsbdb.com](https://www.adsbdb.com) and, with a key of the user's, schedules from [AviationStack](https://aviationstack.com); its tables of places are built from [OurAirports](https://ourairports.com) (public domain), [GeoNames](https://www.geonames.org) (CC BY 4.0) and [Natural Earth](https://www.naturalearthdata.com) (public domain).
- **`janas-weather`** reads forecasts and the sea from [Open-Meteo](https://open-meteo.com) (CC BY 4.0) and [MET Norway](https://api.met.no) (CC BY 4.0), the stations' reports from [aviationweather.gov](https://aviationweather.gov) (NOAA), warnings from [MeteoAlarm](https://meteoalarm.org) (EUMETNET) and the Italian alert levels from the [Dipartimento della Protezione Civile](https://github.com/pcm-dpc/DPC-Bollettini-Criticita-Idrogeologica-Idraulica) (CC BY 4.0). With no place named, the services ask [GeoJS](https://www.geojs.io) or [ipwho.is](https://ipwho.is) where the internet connection is.
- **`janas-maps`** reads [OpenStreetMap](https://www.openstreetmap.org)'s data (© OpenStreetMap contributors, ODbL) through [Nominatim](https://nominatim.org), [Photon](https://photon.komoot.io), [Valhalla](https://github.com/valhalla/valhalla) and [OSRM](https://project-osrm.org) on the servers of [FOSSGIS](https://fossgis.de), and the [Overpass API](https://overpass-api.de) (with [Private.coffee's instance](https://overpass.private.coffee) when it is busy), and journeys by public transport from [Transitous](https://transitous.org), over the operators' open timetables.
+- **`janas-github`** reads [GitHub](https://github.com)'s REST API; **`janas-git`** runs [git](https://git-scm.com).
- **`janas-wiki`** reads [Wikipedia](https://www.wikipedia.org) through its API; the text is Wikipedia's, under CC BY-SA 4.0, and every answer gives the page's address.
- **The people who tried it on their own machines** and reported what they saw, in the issues of this repository: their reports changed the engine more than once (the threads it picks, the prompt's progress, the GPU's diagnosis).
diff --git a/docs/services/README.md b/docs/services/README.md
index 5ac8035..c10a903 100644
--- a/docs/services/README.md
+++ b/docs/services/README.md
@@ -1,16 +1,19 @@
# Janas's services
-A service of Janas is a program that brings data of the world - flights, the weather, roads and places, an encyclopedia - to a model, as tools of the [Model Context Protocol](https://modelcontextprotocol.io) (MCP). Each one is an MCP server of its own, over its standard input and output, so that any MCP client can use it: `janas-chat`, and the assistants of others.
+A service of Janas is a program that brings data of the world - flights, the weather, roads and places, an encyclopedia, GitHub's projects, the git repositories of your computer - to a model, as tools of the [Model Context Protocol](https://modelcontextprotocol.io) (MCP). Each one is an MCP server of its own, over its standard input and output, so that any MCP client can use it: `janas-chat`, and the assistants of others.
| Service | What it answers | Key |
|---|---|---|
| [`janas-flights`](flights.md) | where a flight is and what it is doing, its times, the flights of a route, what flies over a sea or near a place | none for live data; a free one of your own for schedules |
+| [`janas-git`](git.md) | the git repositories of this computer: what has changed, the commits, the branches, a diff; a commit, pull, push or switch of branch, each asked of you first | none |
+| [`janas-github`](github.md) | a project's issues and pull requests (the new ones), its latest releases, its recent commits, what it is | none; the gh command's token when you have one |
| [`janas-maps`](maps.md) | a route by car, bike or on foot, step by step; journeys by public transport; what is near a place; where an address is | none |
| [`janas-weather`](weather.md) | the weather now (a model's and a station's), the forecast, the sea and its waves, the warnings in force | none |
| [`janas-wiki`](wiki.md) | Wikipedia's search, its pages as text, the pages about what lies around a place | none |
What every service does the same way:
+- **Reading by default.** Every tool only reads, but those of `janas-git` that commit, pull, push or switch branch; they say so, and nothing that throws work away is offered.
- **Facts already worked out.** The answers are text for the model, with places, distances and times already computed and put into words, so that the model telling them has nothing to compute; what is not known is said to be not known.
- **Open data first.** Questions about an area or a list are answered from open sources only; a source with a small quota (a key of yours) is kept for questions about one thing.
- **It says when to use it.** A service tells the client's model when its tools apply: a question on the subject is enough, the tools need not be named, and the model is told not to name them in its answers.
@@ -22,7 +25,7 @@ What every service does the same way:
## In janas-chat
-`janas-chat` starts by itself the services it finds beside it (in the same directory as the program) and runs their calls without asking: they read public data and change nothing. Their tools are not written into the system message: it holds a catalog, a line a service, and the model opens a service when a question needs it, getting its instructions and tools then (`--all-tools` writes them all at the start instead). A call shows as a dimmed line. Nothing to configure, nothing to name: ask your question.
+`janas-chat` starts by itself the services it finds beside it (in the same directory as the program) and runs without asking the calls of the tools that only read. A tool that changes something - `janas-git`'s commit, pull, push, switch - says so (MCP's `readOnlyHint` false), and its call is shown whole and asked of you every time, with `--mcp-auto` too; on a pipe it is not run. Their tools are not written into the system message: it holds a catalog, a line a service, and the model opens a service when a question needs it, getting its instructions and tools then (`--all-tools` writes them all at the start instead). A call shows as a dimmed line. Nothing to configure, nothing to name: ask your question.
- `/mcp` lists the servers running, the services marked as Janas's.
- `--no-services` starts none of them; `--no-mcp` no server at all.
diff --git a/docs/services/git.md b/docs/services/git.md
new file mode 100644
index 0000000..0cbaa10
--- /dev/null
+++ b/docs/services/git.md
@@ -0,0 +1,53 @@
+# janas-git
+
+The git repositories of your computer for `janas-chat` or any other MCP client (see [the services](README.md)): what has changed, the latest commits, the branches, a diff; and, asked of you every time, a commit, a pull, a push, a switch of branch. Nothing that throws work away is offered.
+
+## The tools
+
+They only read, and run without asking:
+
+- **`git_status`** - the branch, the remote branch it follows and how far ahead or behind it is, the files staged, changed, new and in conflict.
+- **`git_log`** - a branch's latest commits (`count`, 10 unless asked, 20 at most), only those changing a path (`path`), of an author (`author`), of the last days (`days`).
+- **`git_diff`** - the changes not staged, or those staged for the next commit (`staged`), in a path or all: the files with their lines added and removed, and the diff, shown to you (400 lines at most); the model reads its start, enough to write a commit's message.
+- **`git_branches`** - the local branches, the latest worked on first, each with the remote branch it follows and how far ahead or behind.
+
+They change something, and say so (MCP's `readOnlyHint` false), so that the client asks you first:
+
+- **`git_commit`** - commits what is staged, after staging the files named (`files`) or every tracked file changed (`all`); a `signoff` when asked. The answer lists the files the commit holds, those staged before included.
+- **`git_pull`** - brings the branch up to its remote one, only when that needs no merge (`git pull --ff-only`).
+- **`git_push`** - sends the branch's commits to the remote branch it follows; a branch that follows none is pushed to `origin` and followed only when asked (`set_upstream`). Never by force.
+- **`git_switch`** - goes to another branch, or makes a new one and goes to it (`create`); git refuses when changes would be lost, and the refusal is the answer.
+
+Never offered: `reset`, `restore`, `checkout` of files, `clean`, `stash drop`, `rebase`, `commit --amend`, `--no-verify`, `push --force`, deleting a branch. git runs with its arguments as they are, no shell between, and nothing the model writes is taken as an option: a branch's name or a path beginning with `-` is refused, paths come after `--`. git runs without a terminal, so that a password it asks for (ssh, https) fails at once instead of waiting; your ssh agent and git's credential helpers work as usual.
+
+A repository is a path (`~/src/x`), a name listed in `~/.config/janas/git-repos.txt` (one path a line; the name is its last part), or, when none is named, the one `janas-chat` was started in.
+
+## In janas-chat
+
+A call that changes something shows whole, highlighted, and waits for your `y` - every time, with `--mcp-auto` too, and never "always". On a pipe, where nobody can answer, it is not run.
+
+## The answers
+
+Every answer is data with a layout (see [the services](README.md)): the list of files, the commits, the diff are shown to you in your language, and the model reads a brief. After a command that changed something, the answer is git's own words.
+
+## Running it
+
+```sh
+janas-git
+```
+
+It speaks MCP over its standard input and output and writes what it does on its standard error. In other clients (which decide by themselves whether to ask before a call that changes something):
+
+```sh
+claude mcp add git -- /path/to/janas-git
+```
+
+```json
+{"mcpServers": {"git": {"command": "/path/to/janas-git"}}}
+```
+
+## What has been run
+
+2 October 2026, over the protocol, in the release and AddressSanitizer builds, on a repository made for the purpose with a remote of its own: the status with a file changed, one renamed, one new; the diffs staged and not; a commit with nothing staged (refused), of the files named and of all the tracked ones; a push; a branch named `--force` (refused); a new branch, its push without a remote branch (refused) and with `set_upstream`; the branches and the log; a pull with nothing to bring; a switch with changes that would be lost (refused by git); a path that is not there and a directory outside any repository.
+
+In `janas-chat` with Qwen3.6-35B-A3B, in a terminal, in that repository: "Cosa è cambiato in questo repository?" (the status and the diff), then "Fai il commit di tutto con un messaggio adatto.": the call shown whole, asked, confirmed with `y`, one commit made with a message the model wrote in Italian. Then, with `--mcp-auto`: on a pipe, "Fai il commit di a.txt con il messaggio 'otto'." was called by the model and not run ("not run: on a pipe nobody can say yes"); in a terminal it was asked all the same, answered `n`, and not run. The status, diff, branches and log were shown in Italian, git's terms as the glossary gives them.
diff --git a/docs/services/github.md b/docs/services/github.md
new file mode 100644
index 0000000..22b59bb
--- /dev/null
+++ b/docs/services/github.md
@@ -0,0 +1,44 @@
+# janas-github
+
+GitHub for `janas-chat` or any other MCP client (see [the services](README.md)): a project's issues and pull requests (the new ones since you last asked), its latest releases, its recent commits, what it is. It only reads: nothing on GitHub is changed.
+
+## The tools
+
+- **`github_issues`** - a repository's issues, or its pull requests (`kind`), open, closed or all (`state`), the newest first, with how many there are; `new` gives only those opened since the last time you asked ("are there new issues on Janas?"), and `days` those opened in the last days. Each with its number, title, author, date, comments, labels and link; a pull request says whether it was merged or is a draft.
+- **`github_releases`** - the latest releases (`count`, 1 for the latest), each with its tag, name, date, author, whether it is a pre-release, its files, the start of its notes and its link; a repository with no releases gives its latest tags.
+- **`github_commits`** - a branch's commits of the last days (`days`, 7 unless asked; `branch`, the main one unless asked; `author`), the newest first, ten at most.
+- **`github_repo`** - what a repository is: its description, stars, forks, watchers, open issues and pull requests, language, licence, main branch, last push, topics, site.
+
+A repository is `owner/name`, a github.com link, or a name alone ("Janas", "llama.cpp"): among your own repositories first (with a token), else the one of that name with the most stars on GitHub; the answer says which it took.
+
+## The answers
+
+Every answer is data with a layout (see [the services](README.md)): `janas-chat` shows the user the list in the user's language - titles, messages and notes as their authors wrote them - and gives the model only a brief, from which it answers in a line or two. The terms of the craft (commit, branch, pull request, release...) come from the glossary of `janas-chat`, so that the translation does not turn them into words of everyday speech.
+
+## The token
+
+GitHub answers 60 requests an hour without a token, and 10 searches a minute; with one 5,000 and 30, and your private repositories too. `janas-github` takes the token of `GH_TOKEN` or `GITHUB_TOKEN` when set, else that of the [gh](https://cli.github.com) command (`gh auth token`) when you have logged in with it (`gh auth login`); without one it asks GitHub anonymously. The token stays in the program's memory: it is never written, printed or sent anywhere but to GitHub's API, and a redirect to another host loses it.
+
+When you last looked at a repository's issues or pull requests is kept in `~/.config/janas/github-seen.txt`, a line a repository; the first time, "new" means the last seven days.
+
+## Running it
+
+```sh
+janas-github
+```
+
+It speaks MCP over its standard input and output and writes what it does on its standard error. In other clients:
+
+```sh
+claude mcp add github -- /path/to/janas-github
+```
+
+```json
+{"mcpServers": {"github": {"command": "/path/to/janas-github"}}}
+```
+
+## What has been run
+
+2 October 2026, over the protocol, in the release and AddressSanitizer builds, with the gh command's token: the new issues of Janas (eight in the last seven days the first time, none the second), its closed issues, the latest release of llama.cpp (found by its name), Janas's commits of three days and llama.cpp's of one, the open pull requests of llama.cpp (1,656), the repository of Janas from its link, and a repository that does not exist (a clear error).
+
+In `janas-chat` with Qwen3.6-35B-A3B, the same day, in Italian: "Ci sono nuove issue su Janas?" (the issues of the last seven days the first time, none the second), the latest release of llama.cpp and its commits of the week. The first run showed the issues' layout given back untranslated (now refused and asked again) and the model calling eight issues open when four were closed (the brief now counts them). Run again with the layouts translated anew: the open issues ("4 issue aperte", each "aperta": the states of an issue come from the glossary, as the model wrote "aperte" for one), the latest release, the commits and the repository of llama.cpp, all in Italian.
diff --git a/src/chat/glossary.c b/src/chat/glossary.c
index 6f608cb..e3212ff 100644
--- a/src/chat/glossary.c
+++ b/src/chat/glossary.c
@@ -15,7 +15,8 @@ static const struct {
const char *lang;
const char *bft[13]; /* Beaufort 0-12 */
const char *sea[10]; /* Douglas 0-9 */
- const char *terms;
+ const char *terms; /* of the running text */
+ const char *dev; /* the same, of git's and GitHub's layouts */
} G[] = {
{"it",
{"calma", "bava di vento", "brezza leggera", "brezza tesa",
@@ -24,7 +25,11 @@ static const struct {
{"calmo", "quasi calmo", "poco mosso", "mosso", "molto mosso", "agitato",
"molto agitato", "grosso", "molto grosso", "tempestoso"},
"swell = mare lungo; wind waves = mare di vento; significant height = "
- "altezza significativa; gusts = raffiche"},
+ "altezza significativa; gusts = raffiche",
+ "commit = commit; push = push; pull = pull; staged = nell'area di "
+ "stage; working tree = cartella di lavoro; branch = ramo; repository = "
+ "repository; issue = issue; pull request = pull request; release = "
+ "release; tag = tag"},
{"fr",
{"calme", "très légère brise", "légère brise", "petite brise",
"jolie brise", "bonne brise", "vent frais", "grand frais", "coup de vent",
@@ -32,7 +37,10 @@ static const struct {
{"calme", "calme, ridée", "belle", "peu agitée", "agitée", "forte",
"très forte", "grosse", "très grosse", "énorme"},
"swell = houle; wind waves = mer du vent; significant height = "
- "hauteur significative; gusts = rafales"},
+ "hauteur significative; gusts = rafales",
+ "commit = commit; push = push; pull = pull; staged = indexé; working "
+ "tree = répertoire de travail; branch = branche; repository = dépôt; "
+ "pull request = pull request; release = version; tag = étiquette"},
{"es",
{"calma", "ventolina", "flojito", "flojo", "bonancible", "fresquito",
"fresco", "frescachón", "temporal", "temporal fuerte", "temporal duro",
@@ -40,7 +48,10 @@ static const struct {
{"calma", "rizada", "marejadilla", "marejada", "fuerte marejada", "gruesa",
"muy gruesa", "arbolada", "montañosa", "enorme"},
"swell = mar de fondo; wind waves = mar de viento; significant height "
- "= altura significativa; gusts = rachas"},
+ "= altura significativa; gusts = rachas",
+ "commit = commit; push = push; pull = pull; staged = preparado; "
+ "working tree = directorio de trabajo; branch = rama; repository = "
+ "repositorio; pull request = pull request; release = versión"},
{"de",
{"Windstille", "leiser Zug", "leichte Brise", "schwache Brise",
"mäßige Brise", "frische Brise", "starker Wind", "steifer Wind",
@@ -50,9 +61,41 @@ static const struct {
"leicht bewegte See", "mäßig bewegte See", "grobe See", "sehr grobe See",
"hohe See", "sehr hohe See", "außergewöhnlich schwere See"},
"swell = Dünung; wind waves = Windsee; significant height = "
- "signifikante Wellenhöhe; gusts = Böen"},
+ "signifikante Wellenhöhe; gusts = Böen",
+ "commit = Commit; push = Push; pull = Pull; staged = in der "
+ "Staging-Area; working tree = Arbeitsverzeichnis; branch = Branch; "
+ "repository = Repository; pull request = Pull-Request; release = "
+ "Release"},
};
+/* The state of one issue or pull request (github/layouts.c): a model
+ asked wrote the plural ("aperte") for one. Feminine, as "issue" and
+ "pull request" are in these languages. */
+static const struct {
+ const char *lang, *open, *closed, *merged, *draft;
+} STATUS[] = {
+ {"it", "aperta", "chiusa", "unita", "bozza"},
+ {"fr", "ouverte", "fermée", "fusionnée", "brouillon"},
+ {"es", "abierta", "cerrada", "fusionada", "borrador"},
+ {"de", "offen", "geschlossen", "zusammengeführt", "Entwurf"},
+};
+
+static const char *status_word(const char *lang, const char *key, size_t n)
+{
+ for (size_t i = 0; lang && i < sizeof STATUS / sizeof *STATUS; i++) {
+ if (strcmp(STATUS[i].lang, lang) != 0)
+ continue;
+ const char *k[4] = {"status.open", "status.closed", "status.merged",
+ "status.draft"};
+ const char *v[4] = {STATUS[i].open, STATUS[i].closed, STATUS[i].merged,
+ STATUS[i].draft};
+ for (int j = 0; j < 4; j++)
+ if (strlen(k[j]) == n && strncmp(key, k[j], n) == 0)
+ return v[j];
+ }
+ return NULL;
+}
+
static int find(const char *lang)
{
for (size_t i = 0; lang && i < sizeof G / sizeof *G; i++)
@@ -85,13 +128,19 @@ const char *glossary_word(const char *lang, const char *key, size_t n)
return G[g].bft[d];
if ((d = degree(key, n, "sea.", 9)) >= 0)
return G[g].sea[d];
- return NULL;
+ return status_word(lang, key, n);
+}
+
+/* git_status, github_issues: the layouts of git and GitHub */
+static int dev_layout(const char *name)
+{
+ return name && strncmp(name, "git", 3) == 0;
}
-const char *glossary_terms(const char *lang)
+const char *glossary_terms(const char *lang, const char *name)
{
int g = find(lang);
- return g < 0 ? NULL : G[g].terms;
+ return g < 0 ? NULL : dev_layout(name) ? G[g].dev : G[g].terms;
}
static uint64_t fnv(uint64_t h, const char *s)
@@ -101,7 +150,7 @@ static uint64_t fnv(uint64_t h, const char *s)
return (h ^ '\n') * 1099511628211ull;
}
-uint64_t glossary_mark(const char *lang, uint64_t h)
+uint64_t glossary_mark(const char *lang, const char *name, uint64_t h)
{
int g = find(lang);
if (g < 0)
@@ -110,5 +159,13 @@ uint64_t glossary_mark(const char *lang, uint64_t h)
h = fnv(h, G[g].bft[i]);
for (int i = 0; i < 10; i++)
h = fnv(h, G[g].sea[i]);
+ if (dev_layout(name)) {
+ for (size_t i = 0; i < sizeof STATUS / sizeof *STATUS; i++)
+ if (strcmp(STATUS[i].lang, lang) == 0)
+ h = fnv(fnv(fnv(fnv(h, STATUS[i].open), STATUS[i].closed),
+ STATUS[i].merged),
+ STATUS[i].draft);
+ return fnv(h, G[g].dev);
+ }
return fnv(h, G[g].terms);
}
diff --git a/src/chat/glossary.h b/src/chat/glossary.h
index 15aace5..c450e1c 100644
--- a/src/chat/glossary.h
+++ b/src/chat/glossary.h
@@ -13,16 +13,17 @@
#include <stddef.h>
#include <stdint.h>
-/* The term of the word key (n bytes: "bft.4", "sea.3") in lang; NULL when
- the glossary has none. */
+/* The term of the word key (n bytes: "bft.4", "sea.3", "status.open")
+ in lang; NULL when the glossary has none. */
const char *glossary_word(const char *lang, const char *key, size_t n);
-/* The terms of the running text in lang, for the request to the model
- ("swell = mare lungo; ..."); NULL when there are none. */
-const char *glossary_terms(const char *lang);
+/* The terms of the running text in lang for the layout name, for the
+ request to the model ("swell = mare lungo; ..."; those of git's craft
+ for git_* and github_*); NULL when there are none. */
+const char *glossary_terms(const char *lang, const char *name);
-/* h carried on (FNV-1a) over the glossary of lang: a layout kept on disk
- is translated again when the glossary changes. */
-uint64_t glossary_mark(const char *lang, uint64_t h);
+/* h carried on (FNV-1a) over the glossary of lang for the layout name: a
+ layout kept on disk is translated again when the glossary changes. */
+uint64_t glossary_mark(const char *lang, const char *name, uint64_t h);
#endif
diff --git a/src/chat/layout.c b/src/chat/layout.c
index 9cfb67b..e216298 100644
--- a/src/chat/layout.c
+++ b/src/chat/layout.c
@@ -144,7 +144,9 @@ static int comma_lang(const char *l)
static const char SYSTEM[] =
"You translate the texts of a program. The text holds markers such as "
"{7}: keep every marker exactly as it is, once each, where the sense "
- "puts it; never translate, change, add or drop one. After the line "
+ "puts it; never translate, change, add or drop one. Translate all the "
+ "text between them, however short (\", by \", \", comments: \"). "
+ "After the line "
"\"=== words\" come words and short phrases, one a line, each after "
"its number and a colon (\"12: north-east\"): translate what follows "
"the colon, keeping the numbers and the lines; what is in square "
@@ -213,6 +215,10 @@ static const char *hint(const char *key, size_t n)
{"dir.", "compass point, abbreviated"},
{"from.", "wind direction"},
{"wd.", "day of the week"},
+ {"status.", "the state of one issue or pull request"},
+ {"kind.", "a plural, after a number"},
+ {"change.", "what happened to one file"},
+ {"done.", "what a git command did"},
{"mon.", "month"}};
for (size_t i = 0; i < sizeof k / sizeof *k; i++) {
size_t pn = strlen(k[i].prefix);
@@ -401,7 +407,7 @@ static char *translated(const char *name, const char *en)
if (dir_of(dir, sizeof dir) != 0)
return NULL;
snprintf(path, sizeof path, "%s/%s.%s.%016llx.txt", dir, name, lang,
- (unsigned long long)glossary_mark(lang, mark(en)));
+ (unsigned long long)glossary_mark(lang, name, mark(en)));
char *have = read_file(path);
char why[200];
if (have && janas_tpl_same_shape(en, strlen(en), have, strlen(have), why,
@@ -412,7 +418,7 @@ static char *translated(const char *name, const char *en)
if (!translator || encode(en, &c) != 0)
return NULL;
struct janas_buf ask = {0};
- const char *terms = glossary_terms(lang);
+ const char *terms = glossary_terms(lang, name);
janas_buf_printf(&ask, "Translate into %s%s%s:\n\n%.*s", lang_name(lang),
terms ? ", with these terms: " : "", terms ? terms : "",
(int)c.text.n, c.text.p);
@@ -422,7 +428,21 @@ static char *translated(const char *name, const char *en)
char *out = NULL;
if (r == 0 && ans) {
unwrap(ans);
- out = decode(&c, ans, why, sizeof why);
+ /* the text given back as it came: the 35B did so with GitHub's
+ issues, translating only the words. Not kept, so that it is
+ asked again the next time. */
+ const char *w = strstr(ans, "=== words");
+ const char *cw = strstr(c.text.p, "\n=== words");
+ size_t an = w ? (size_t)(w - ans) : strlen(ans);
+ size_t cn = cw ? (size_t)(cw - c.text.p) : c.text.n;
+ while (an && (ans[an - 1] == '\n' || ans[an - 1] == ' '))
+ an--;
+ while (cn && (c.text.p[cn - 1] == '\n' || c.text.p[cn - 1] == ' '))
+ cn--;
+ if (an == cn && memcmp(ans, c.text.p, cn) == 0)
+ snprintf(why, sizeof why, "the text came back untranslated");
+ else
+ out = decode(&c, ans, why, sizeof why);
} else {
snprintf(why, sizeof why, "no answer");
}
diff --git a/src/chat/main.c b/src/chat/main.c
index 43e5cd5..3faa9bd 100644
--- a/src/chat/main.c
+++ b/src/chat/main.c
@@ -22,6 +22,7 @@
#include <sys/stat.h>
#include <signal.h>
#include <stdarg.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -1193,11 +1194,16 @@ static int run_calls(janas_llm_chat *c, int autorun)
}
term_gap(&term);
/* a service of Janas's runs by itself: its call is only noted */
+ /* one that changes something (janas-git's commit, push) is asked
+ for every time, even with --mcp-auto: never "always"; shown
+ whole, since the user says yes to what it holds */
+ int writes = mcpc_writes(name);
+ int cut = writes ? INT_MAX : 400;
term_printf(&term, "%s→ %s%s %s%.*s%s%s\n",
- mcpc_own(name) ? T_DIM(&term) : T_CMD(&term), name,
- T_DIM(&term), "", 400, args,
- strlen(args) > 400 ? " ..." : "", T_RESET(&term));
- int go = autorun || mcpc_always(name) || mcpc_own(name);
+ mcpc_own(name) && !writes ? T_DIM(&term) : T_CMD(&term),
+ name, T_DIM(&term), "", cut, args,
+ strlen(args) > (size_t)cut ? " ..." : "", T_RESET(&term));
+ int go = !writes && (autorun || mcpc_always(name) || mcpc_own(name));
char *key = call_key(name, args);
if (failed_before(key)) {
go = 0;
@@ -1212,8 +1218,12 @@ static int run_calls(janas_llm_chat *c, int autorun)
} else if (!go && interrupted) {
go = 0;
} else if (!go && !term.tty) {
- term_printf(&term, "not run: on a pipe nobody can say yes "
- "(--mcp-auto runs them)\n");
+ term_printf(&term, "not run: on a pipe nobody can say yes%s\n",
+ writes ? " (it changes something: always asked)"
+ : " (--mcp-auto runs them)");
+ } else if (!go && writes) {
+ int k = ask_key("it changes something: run it? [y]es, [n]o");
+ go = k == 'y';
} else if (!go) {
int k = ask_key("run it? [y]es, [n]o, [a]lways this tool");
go = k == 'y' || k == 'a';
diff --git a/src/chat/mcp_chat.c b/src/chat/mcp_chat.c
index de19fbf..f9bf456 100644
--- a/src/chat/mcp_chat.c
+++ b/src/chat/mcp_chat.c
@@ -29,15 +29,21 @@ struct server {
};
/* The services of Janas that janas-chat starts by itself when they are
- beside it: janas-<name>. They read public data and change nothing, so
- their tools run without asking, and what they say of their tools goes
- to the model. */
+ beside it: janas-<name>. Their tools that only read run without asking;
+ those that change something (readOnlyHint false: janas-git's commit,
+ push...) are asked for every time (mcpc_writes). What they say of their
+ tools goes to the model. */
static const struct {
const char *name, *about; /* for the catalog */
} SERVICES[] = {
{"flights", "flights and aircraft: where a flight is and what it is "
"doing, today's flights of a route, what flies over a sea "
"or near a place"},
+ {"git", "the git repositories of this computer: what has changed, "
+ "the commits, the branches, a diff; a commit, pull, push or "
+ "switch of branch, each confirmed by the user"},
+ {"github", "GitHub: a project's issues and pull requests (the new "
+ "ones), its latest release, its recent commits, what it is"},
{"maps", "roads and places: a route by car, bike or on foot, public "
"transport between places, what is near (fuel, a pharmacy...), "
"where an address is"},
@@ -416,6 +422,16 @@ int mcpc_own(const char *tool)
return 0;
}
+int mcpc_writes(const char *tool)
+{
+ for (size_t i = 0; i < n_servers; i++) {
+ size_t n = strlen(servers[i].prefix);
+ if (servers[i].own && strncmp(tool, servers[i].prefix, n) == 0)
+ return janas_mcp_tool_read_only(servers[i].m, tool + n, -1) == 0;
+ }
+ return 0;
+}
+
int mcpc_is_own(size_t i)
{
return i < n_servers && servers[i].own;
diff --git a/src/chat/mcp_chat.h b/src/chat/mcp_chat.h
index de07f9c..191b7af 100644
--- a/src/chat/mcp_chat.h
+++ b/src/chat/mcp_chat.h
@@ -24,6 +24,9 @@ int mcpc_start_services(void);
services: they run without asking. */
int mcpc_own(const char *tool);
int mcpc_is_own(size_t i);
+/* Whether a tool of Janas's own services changes something (it says it
+ does not only read): it is asked for every time, --mcp-auto or not. */
+int mcpc_writes(const char *tool);
void mcpc_stop(void);
/* Janas's services as a catalog in the system message, opened by the model
diff --git a/src/common/https_get.c b/src/common/https_get.c
index 8cf66c9..7d78b8a 100644
--- a/src/common/https_get.c
+++ b/src/common/https_get.c
@@ -6,6 +6,7 @@
*/
#include <stdio.h>
#include <string.h>
+#include <strings.h>
#include "https_get.h"
@@ -27,12 +28,25 @@ int janas_https_get_ms(const char *url, const char *const *headers,
size_t n_headers, int timeout_ms, struct janas_buf *out,
char *err, size_t err_len)
{
- char where[4096];
+ char where[4096], first[256] = "";
snprintf(where, sizeof where, "%s", url);
+ const char *kept[16];
for (int hop = 0; hop <= MAX_REDIRECTS; hop++) {
struct janas_url u;
if (janas_url_parse(where, &u, err, err_len) != 0)
return -1;
+ if (!hop)
+ snprintf(first, sizeof first, "%s", u.host);
+ /* sent to another host, a redirect loses the credentials (as curl
+ does): a token for one server is not for the next */
+ if (hop && strcasecmp(first, u.host) != 0 && headers) {
+ size_t k = 0;
+ for (size_t i = 0; i < n_headers && k < 16; i++)
+ if (strncasecmp(headers[i], "Authorization:", 14) != 0)
+ kept[k++] = headers[i];
+ headers = kept;
+ n_headers = k;
+ }
struct janas_http h;
if (janas_http_request(&h, &u, "GET", headers, n_headers, NULL, 0,
timeout_ms, NULL, err, err_len) != 0)
diff --git a/src/git/data.c b/src/git/data.c
new file mode 100644
index 0000000..346fae7
--- /dev/null
+++ b/src/git/data.c
@@ -0,0 +1,163 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * data.c - what janas-git read, as the data of its layouts (see git.h,
+ * layouts.c and common/template.h). Every field is written, even empty: a
+ * layout looks a name up in the item, then in what holds it.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include "git.h"
+
+void gt_jstr(struct janas_buf *b, const char *key, const char *v)
+{
+ if (!v)
+ v = "";
+ janas_buf_printf(b, ", \"%s\": ", key);
+ janas_json_write_str(b, v, strlen(v));
+}
+
+void gt_jwhen(struct janas_buf *b, const char *key, time_t t)
+{
+ if (t < 0) {
+ janas_buf_printf(b,
+ ", \"%s\": {\"day\": 0, \"mon\": 0, \"year\": 0, "
+ "\"at\": \"\", \"ago_d\": 0}",
+ key);
+ return;
+ }
+ struct tm lt, nt;
+ time_t now = time(NULL);
+ localtime_r(&t, &lt);
+ localtime_r(&now, &nt);
+ long ago = (long)((now - t) / 86400);
+ janas_buf_printf(b,
+ ", \"%s\": {\"day\": %d, \"mon\": %d, \"year\": %d, "
+ "\"at\": \"%02d:%02d\", \"ago_d\": %ld}",
+ key, lt.tm_mday, lt.tm_mon + 1,
+ lt.tm_year == nt.tm_year ? 0 : lt.tm_year + 1900,
+ lt.tm_hour, lt.tm_min, ago < 0 ? 0 : ago);
+}
+
+void gt_jhead(struct janas_buf *b, const char *top)
+{
+ char shown[1024];
+ gt_short_path(top, shown, sizeof shown);
+ const char *last = strrchr(top, '/');
+ last = last && last[1] ? last + 1 : top;
+ janas_buf_puts(b, "{\"repo\": ");
+ janas_json_write_str(b, last, strlen(last));
+ gt_jstr(b, "path", shown);
+}
+
+void gt_status_data(struct janas_buf *b, const char *top,
+ const struct gt_status *s)
+{
+ gt_jhead(b, top);
+ gt_jstr(b, "branch", s->branch);
+ gt_jstr(b, "upstream", s->upstream);
+ janas_buf_printf(b,
+ ", \"detached\": %s, \"ahead\": %d, \"behind\": %d, "
+ "\"staged\": %d, \"changed\": %d, \"untracked\": %d, "
+ "\"conflicts\": %d, \"clean\": %s, \"more\": %d",
+ s->detached ? "true" : "false", s->ahead, s->behind,
+ s->n_staged, s->n_changed, s->n_untracked, s->n_conflicts,
+ s->n || s->more ? "false" : "true", s->more);
+ janas_buf_puts(b, ", \"files\": [");
+ for (int i = 0; i < s->n; i++) {
+ const struct gt_file *f = &s->f[i];
+ char st[2] = {f->staged, 0}, ch[2] = {f->changed, 0};
+ janas_buf_printf(b, "%s{\"path\": ", i ? ", " : "");
+ janas_json_write_str(b, f->path, strlen(f->path));
+ gt_jstr(b, "in_index", st);
+ gt_jstr(b, "in_tree", ch);
+ janas_buf_printf(b, ", \"new\": %s, \"conflict\": %s}",
+ f->untracked ? "true" : "false",
+ f->conflict ? "true" : "false");
+ }
+ janas_buf_puts(b, "]}");
+}
+
+void gt_log_data(struct janas_buf *b, const char *top, const char *branch,
+ const struct gt_log *l)
+{
+ gt_jhead(b, top);
+ gt_jstr(b, "branch", branch);
+ janas_buf_printf(b, ", \"n\": %d, \"more\": %s", l->n,
+ l->more ? "true" : "false");
+ janas_buf_puts(b, ", \"items\": [");
+ for (int i = 0; i < l->n; i++) {
+ const struct gt_commit *c = &l->c[i];
+ janas_buf_printf(b, "%s{\"sha\": ", i ? ", " : "");
+ janas_json_write_str(b, c->sha, strlen(c->sha));
+ gt_jstr(b, "message", c->message);
+ gt_jstr(b, "author", c->author);
+ gt_jstr(b, "refs", c->refs);
+ gt_jwhen(b, "when", c->at);
+ janas_buf_puts(b, "}");
+ }
+ janas_buf_puts(b, "]}");
+}
+
+void gt_branches_data(struct janas_buf *b, const char *top,
+ const struct gt_branches *v)
+{
+ gt_jhead(b, top);
+ janas_buf_printf(b, ", \"n\": %d, \"more\": %d", v->n, v->more);
+ janas_buf_puts(b, ", \"items\": [");
+ for (int i = 0; i < v->n; i++) {
+ const struct gt_branch *x = &v->b[i];
+ janas_buf_printf(b, "%s{\"name\": ", i ? ", " : "");
+ janas_json_write_str(b, x->name, strlen(x->name));
+ gt_jstr(b, "upstream", x->upstream);
+ gt_jstr(b, "track", x->track);
+ gt_jstr(b, "message", x->message);
+ gt_jwhen(b, "when", x->at);
+ janas_buf_printf(b, ", \"current\": %s}",
+ x->current ? "true" : "false");
+ }
+ janas_buf_puts(b, "]}");
+}
+
+void gt_diff_data(struct janas_buf *b, const char *top, int staged,
+ const char *path, const struct gt_diff *d, const char *patch,
+ size_t patch_n, size_t brief_n)
+{
+ gt_jhead(b, top);
+ gt_jstr(b, "only", path);
+ janas_buf_printf(b,
+ ", \"staged\": %s, \"n\": %d, \"more\": %d, \"add\": "
+ "%ld, \"del\": %ld",
+ staged ? "true" : "false", d->n, d->more, d->add, d->del);
+ janas_buf_puts(b, ", \"files\": [");
+ for (int i = 0; i < d->n; i++) {
+ const struct gt_numstat *f = &d->f[i];
+ janas_buf_printf(b, "%s{\"path\": ", i ? ", " : "");
+ janas_json_write_str(b, f->path, strlen(f->path));
+ janas_buf_printf(b, ", \"binary\": %s, \"plus\": %ld, \"minus\": %ld}",
+ f->add < 0 ? "true" : "false", f->add < 0 ? 0 : f->add,
+ f->del < 0 ? 0 : f->del);
+ }
+ janas_buf_puts(b, "], \"patch\": ");
+ janas_json_write_str(b, patch ? patch : "", patch ? patch_n : 0);
+ janas_buf_puts(b, ", \"start\": ");
+ janas_json_write_str(b, patch ? patch : "",
+ patch ? (brief_n < patch_n ? brief_n : patch_n) : 0);
+ janas_buf_printf(b, ", \"cut\": %s}",
+ patch && brief_n < patch_n ? "true" : "false");
+}
+
+void gt_done_data(struct janas_buf *b, const char *top, const char *what,
+ const char *branch, const char *sha, const char *message,
+ const char *output)
+{
+ gt_jhead(b, top);
+ gt_jstr(b, "what", what);
+ gt_jstr(b, "branch", branch);
+ gt_jstr(b, "sha", sha);
+ gt_jstr(b, "message", message);
+ gt_jstr(b, "output", output);
+ janas_buf_puts(b, "}");
+}
diff --git a/src/git/git.h b/src/git/git.h
new file mode 100644
index 0000000..49ba89b
--- /dev/null
+++ b/src/git/git.h
@@ -0,0 +1,156 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * git.h - janas-git, the git repositories of this computer as an MCP
+ * service: what its parts share. run.c runs git (no shell, no terminal),
+ * repo.c finds the repository meant, read.c reads git's output, data.c and
+ * layouts.c write the answers as data with a layout, tools.c offers the
+ * tools: those that read, and those that change something (commit, pull,
+ * push, switch), which say so (readOnlyHint false) so that a client asks
+ * the user every time. Nothing that throws work away is offered: no
+ * reset, restore, clean, force push, amend.
+ */
+#ifndef JANAS_GIT_H
+#define JANAS_GIT_H
+
+#include <stddef.h>
+#include <time.h>
+
+#include "llm/json.h"
+
+/* ---- run.c ---- */
+
+/* git with the arguments argv (NULL-ended, without "git") in the directory
+ dir, at most timeout_ms; its output into out, what it said on its error
+ stream into err (both cut at 1 MiB). git's exit status, or -1 when it
+ could not be run or took too long (the reason in why). */
+int gt_run(const char *dir, const char *const *argv, int timeout_ms,
+ struct janas_buf *out, struct janas_buf *err, char *why,
+ size_t why_len);
+
+/* ---- repo.c ---- */
+
+/* The repository meant: a path ("~/src/x", absolute, or relative to where
+ janas-git runs), a name listed in ~/.config/janas/git-repos.txt (a path
+ a line; its last part is its name), or, with q NULL or empty, the one
+ janas-git runs in. Its top directory into top. 0, or -1 with the reason
+ in why. */
+int gt_repo_find(const char *q, char *top, size_t cap, char *why,
+ size_t why_len);
+/* "~/Progetti/x" for the home's paths */
+void gt_short_path(const char *path, char *out, size_t cap);
+
+/* ---- read.c: git's output (no git here: the tests read kept output) ---- */
+
+#define GT_FILES 40
+#define GT_ITEMS 20
+
+struct gt_file {
+ char path[512];
+ char staged, changed; /* git's letter (M, A, D, R...), 0 when not */
+ int untracked, conflict;
+};
+
+struct gt_status {
+ char branch[128], upstream[160], oid[16];
+ int ahead, behind, detached;
+ int n_staged, n_changed, n_untracked, n_conflicts;
+ struct gt_file f[GT_FILES];
+ int n, more;
+};
+
+struct gt_commit {
+ char sha[16], author[96], message[200], refs[160];
+ time_t at;
+};
+
+struct gt_log {
+ struct gt_commit c[GT_ITEMS];
+ int n, more;
+};
+
+struct gt_branch {
+ char name[128], upstream[160], track[64], message[200];
+ time_t at;
+ int current;
+};
+
+struct gt_branches {
+ struct gt_branch b[GT_ITEMS];
+ int n, more;
+};
+
+struct gt_numstat {
+ char path[512];
+ long add, del; /* -1: a binary file */
+};
+
+struct gt_diff {
+ struct gt_numstat f[GT_FILES];
+ int n, more;
+ long add, del;
+};
+
+/* git status --porcelain=v2 --branch -z */
+int gt_read_status(const char *text, size_t n, struct gt_status *s);
+/* git log --format=%h%x1f%an%x1f%at%x1f%s%x1f%D%x1e; max shown, more when
+ there were more */
+int gt_read_log(const char *text, size_t n, int max, struct gt_log *l);
+/* git for-each-ref refs/heads, fields as gt_branch, %x1f between, %x1e
+ after each: %(HEAD) %(refname:short) %(upstream:short)
+ %(upstream:track) %(committerdate:unix) %(subject) */
+int gt_read_branches(const char *text, size_t n, int max,
+ struct gt_branches *b);
+/* git diff --numstat -z */
+int gt_read_numstat(const char *text, size_t n, struct gt_diff *d);
+
+/* ---- data.c ---- */
+
+void gt_jstr(struct janas_buf *b, const char *key, const char *v);
+void gt_jwhen(struct janas_buf *b, const char *key, time_t t);
+/* {"repo": its name, "path": where it is (no "}") */
+void gt_jhead(struct janas_buf *b, const char *top);
+void gt_status_data(struct janas_buf *b, const char *top,
+ const struct gt_status *s);
+void gt_log_data(struct janas_buf *b, const char *top, const char *branch,
+ const struct gt_log *l);
+void gt_branches_data(struct janas_buf *b, const char *top,
+ const struct gt_branches *v);
+/* patch: the diff for the user (cut), brief: its start for the model */
+void gt_diff_data(struct janas_buf *b, const char *top, int staged,
+ const char *path, const struct gt_diff *d, const char *patch,
+ size_t patch_n, size_t brief_n);
+/* what a command that changes something did: git's own words */
+void gt_done_data(struct janas_buf *b, const char *top, const char *what,
+ const char *branch, const char *sha, const char *message,
+ const char *output);
+
+/* ---- layouts.c ---- */
+
+extern const char GT_STATUS_LAYOUT[], GT_STATUS_BRIEF[];
+extern const char GT_LOG_LAYOUT[], GT_LOG_BRIEF[];
+extern const char GT_BRANCHES_LAYOUT[], GT_BRANCHES_BRIEF[];
+extern const char GT_DIFF_LAYOUT[], GT_DIFF_BRIEF[];
+extern const char GT_DONE_LAYOUT[], GT_DONE_BRIEF[];
+
+/* ---- tools.c, write.c ---- */
+
+void gt_tools_list(void *ctx, struct janas_buf *b);
+int gt_tools_call(void *ctx, const struct janas_json *params,
+ struct janas_buf *b);
+const char *gt_arg_str(const struct janas_json *args, const char *name);
+int gt_arg_bool(const struct janas_json *args, const char *name);
+/* The text as the tool's result, an error when is_error */
+int gt_fail(struct janas_buf *b, const char *fmt, ...)
+ __attribute__((format(printf, 2, 3)));
+int gt_answer(struct janas_buf *b, const char *name, struct janas_buf *d,
+ const char *layout, const char *brief);
+/* A text git takes as an argument and never as an option: not empty, not
+ beginning with "-", no line end. */
+int gt_plain_arg(const char *s);
+int gt_tool_commit(const struct janas_json *args, struct janas_buf *b);
+int gt_tool_pull(const struct janas_json *args, struct janas_buf *b);
+int gt_tool_push(const struct janas_json *args, struct janas_buf *b);
+int gt_tool_switch(const struct janas_json *args, struct janas_buf *b);
+
+#endif
diff --git a/src/git/layouts.c b/src/git/layouts.c
new file mode 100644
index 0000000..2dec7c6
--- /dev/null
+++ b/src/git/layouts.c
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * layouts.c - the layouts of janas-git's answers (see git.h and
+ * common/template.h): the text the user reads, filled with the data in
+ * English here and in the user's language by a client that translates the
+ * layout once (janas-chat), and the brief, all the model reads of it.
+ * Paths, messages and diffs stay as they are; git's own output, after a
+ * command that changed something, is git's (in English).
+ */
+#include "git.h"
+
+#define MON_WORDS \
+ "mon.1 = January\n" \
+ "mon.2 = February\n" \
+ "mon.3 = March\n" \
+ "mon.4 = April\n" \
+ "mon.5 = May\n" \
+ "mon.6 = June\n" \
+ "mon.7 = July\n" \
+ "mon.8 = August\n" \
+ "mon.9 = September\n" \
+ "mon.10 = October\n" \
+ "mon.11 = November\n" \
+ "mon.12 = December\n"
+
+/* A day, with its year when not this one, and the time; the same for the
+ model, "2/10, 3 days ago". */
+#define W_WHEN \
+ "{{when.day}} {{when.mon|mon}}{{#when.year}} {{when.year}}{{/when.year}}, " \
+ "{{when.at}}"
+#define A_WHEN \
+ "{{when.day}}/{{when.mon}}{{#when.year}}/{{when.year}}{{/when.year}}, " \
+ "{{when.ago_d}} days ago"
+
+#define REPO "{{repo}} ({{path}})"
+
+const char GT_STATUS_LAYOUT[] =
+ REPO ", {{#detached}}on no branch "
+ "(detached){{/detached}}{{^detached}}branch "
+ "{{branch}}{{/detached}}{{#upstream}}, following "
+ "{{upstream}}{{#ahead}}; commits to push: "
+ "{{ahead}}{{/ahead}}{{#behind}}; commits to pull: "
+ "{{behind}}{{/behind}}{{/upstream}}{{^upstream}}, following no "
+ "remote branch{{/upstream}}.\n"
+ "{{#clean}}Nothing to commit: the working tree is "
+ "clean.{{/clean}}{{^clean}}staged: {{staged}}, changed: "
+ "{{changed}}, new: {{untracked}}{{#conflicts}}, in conflict: "
+ "{{conflicts}}{{/conflicts}}{{/clean}}\n"
+ "{{#files}}\n"
+ " {{path}}: {{#conflict}}in conflict{{/conflict}}{{#new}}new, not "
+ "tracked{{/new}}{{#in_index}}{{in_index|change}}, "
+ "staged{{/in_index}}{{#in_tree}}{{#in_index}}; "
+ "{{/in_index}}{{in_tree|change}}{{/in_tree}}\n"
+ "{{/files}}\n"
+ "{{#more}} ... and {{more}} more files.\n{{/more}}\n"
+ "---\n"
+ "change.M = modified\n"
+ "change.A = added\n"
+ "change.D = deleted\n"
+ "change.R = renamed\n"
+ "change.C = copied\n"
+ "change.T = type changed\n";
+
+const char GT_STATUS_BRIEF[] =
+ REPO ", {{#detached}}detached{{/detached}}{{^detached}}branch "
+ "{{branch}}{{/detached}}{{#upstream}} following {{upstream}}, "
+ "{{ahead}} to push, {{behind}} to pull{{/upstream}}; "
+ "{{#clean}}clean{{/clean}}{{^clean}}staged {{staged}}, changed "
+ "{{changed}}, new {{untracked}}, in conflict {{conflicts}}: "
+ "{{#files}}{{path}} {{in_index}}{{in_tree}}{{#new}}?{{/new}}{{#conf"
+ "lict}}U{{/conflict}}; {{/files}}{{/clean}}. Shown to the user: do "
+ "not repeat the list; answer in a line or two, in the user's "
+ "language.";
+
+const char GT_LOG_LAYOUT[] =
+ REPO ", {{branch}}: {{#n}}{{#more}}the latest commits, "
+ "{{n}}{{/more}}{{^more}}commits: {{n}}{{/more}}{{/n}}{{^n}}no "
+ "commits{{/n}}\n"
+ "{{#items}}\n"
+ " {{sha}} {{message}}{{#refs}} ({{refs}}){{/refs}}\n"
+ " {{author}}, " W_WHEN "\n"
+ "{{/items}}\n"
+ "---\n" MON_WORDS;
+
+const char GT_LOG_BRIEF[] =
+ REPO ", {{branch}}: {{#more}}the latest {{/more}}{{n}} commits. "
+ "{{#items}}{{sha}} {{message}} ({{author}}, " A_WHEN
+ "); {{/items}}Shown to the user: do not repeat the list; answer in "
+ "a line or two, in the user's language.";
+
+const char GT_BRANCHES_LAYOUT[] =
+ REPO ": the branches, the latest worked on first{{#more}} ({{n}} "
+ "listed, {{more}} more){{/more}} (* the one you are on):\n"
+ "{{#items}}\n"
+ "{{#current}}* {{/current}}{{^current}} "
+ "{{/current}}{{name}}{{#upstream}} -> {{upstream}}{{#track}} "
+ "{{track}}{{/track}}{{/upstream}}\n"
+ " " W_WHEN ": {{message}}\n"
+ "{{/items}}\n"
+ "---\n" MON_WORDS;
+
+const char GT_BRANCHES_BRIEF[] =
+ REPO ": {{n}} branches. {{#items}}{{#current}}(current) "
+ "{{/current}}{{name}}{{#upstream}} -> {{upstream}} "
+ "{{track}}{{/upstream}}, " A_WHEN
+ "; {{/items}}Shown to the user: answer in a line or two, in the "
+ "user's language.";
+
+const char GT_DIFF_LAYOUT[] =
+ REPO ": {{#n}}{{#staged}}what is staged for the next "
+ "commit{{/staged}}{{^staged}}the changes not "
+ "staged{{/staged}}{{#only}}, in {{only}}{{/only}}; files: {{n}}, "
+ "lines +{{add}} -{{del}}{{/n}}{{^n}}{{#staged}}nothing "
+ "staged{{/staged}}{{^staged}}no changes but those "
+ "staged{{/staged}}{{#only}} in {{only}}{{/only}}{{/n}}\n"
+ "{{#files}}\n"
+ " {{path}}{{#binary}} (binary){{/binary}}{{^binary}} +{{plus}} "
+ "-{{minus}}{{/binary}}\n"
+ "{{/files}}\n"
+ "{{#more}} ... and {{more}} more "
+ "files.\n{{/more}}{{#patch}}\n{{patch}}{{/patch}}";
+
+const char GT_DIFF_BRIEF[] =
+ REPO ": {{#staged}}staged{{/staged}}{{^staged}}not "
+ "staged{{/staged}}{{#only}}, in {{only}}{{/only}}: {{n}} files, "
+ "+{{add}} -{{del}}. {{#files}}{{path}} +{{plus}} -{{minus}}; "
+ "{{/files}}The diff is shown to the user{{#cut}}; it "
+ "begins{{/cut}}:\n{{start}}";
+
+const char GT_DONE_LAYOUT[] =
+ REPO ": {{what|done}}{{#branch}}, branch {{branch}}{{/branch}}{{#sha}}, "
+ "{{sha}}{{/sha}}{{#message}}: {{message}}{{/message}}\n"
+ "{{#output}}\n{{output}}\n{{/output}}\n"
+ "---\n"
+ "done.commit = committed\n"
+ "done.pull = pulled\n"
+ "done.push = pushed\n"
+ "done.switch = switched\n"
+ "done.create = a new branch, switched to\n";
+
+const char GT_DONE_BRIEF[] =
+ REPO ": {{what}} done{{#branch}}, branch {{branch}}{{/branch}}{{#sha}}, "
+ "{{sha}}{{/sha}}. Shown to the user with git's words; answer in a "
+ "line, in the user's language. git said:\n{{output}}";
diff --git a/src/git/main.c b/src/git/main.c
new file mode 100644
index 0000000..d2f3e03
--- /dev/null
+++ b/src/git/main.c
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * main.c - janas-git: the git repositories of this computer as an MCP
+ * server over stdio, for janas-chat (which starts it by itself) and any
+ * other MCP client. It reads (status, log, diff, branches) and, asked of
+ * the user each time, changes (commit, pull, push, switch); it never
+ * throws work away.
+ */
+#include <stdio.h>
+#include <string.h>
+
+#include "common/mcp_server.h"
+#include "git.h"
+
+#ifndef JANAS_VERSION
+#define JANAS_VERSION "unknown"
+#endif
+
+static const char INSTRUCTIONS[] =
+ "Use these tools by yourself when the user asks about a git repository "
+ "on this computer: what has changed, the latest commits, the branches, "
+ "a diff; never name them. With no repository named, leave it out: the "
+ "tool takes the one janas-chat runs in. To commit, pull, push or "
+ "switch branch, only when the user asked for it: then call the tool at "
+ "once, without asking first, since the user is asked to confirm each "
+ "of these calls. Write a commit's message from the diff, in the "
+ "language of the repository's earlier commits.";
+
+static void usage(void)
+{
+ fprintf(stderr,
+ "usage: janas-git\n"
+ "An MCP server over stdio: a client starts it and calls its\n"
+ "tools - git_status, git_log, git_diff and git_branches, which\n"
+ "only read, and git_commit, git_pull (fast-forward only),\n"
+ "git_push (never by force) and git_switch, which say they\n"
+ "change something, so that the client asks the user each\n"
+ "time. Nothing that throws work away: no reset, restore,\n"
+ "clean, amend.\n"
+ "A repository is a path, the directory it runs in, or a name\n"
+ "listed in ~/.config/janas/git-repos.txt (a path a line).\n"
+ "git runs without a terminal: a password asked fails at once.\n"
+ "janas-chat starts it by itself when it is beside it.\n"
+ "Configured in a client as, for instance:\n"
+ " {\"mcpServers\": {\"git\": {\"command\": \"janas-git\"}}}\n");
+}
+
+int main(int argc, char **argv)
+{
+ for (int i = 1; i < argc; i++) {
+ usage();
+ return strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 ? 0
+ : 2;
+ }
+ struct janas_mcps srv = {.name = "janas-git",
+ .version = JANAS_VERSION,
+ .instructions = INSTRUCTIONS,
+ .tools_list = gt_tools_list,
+ .tools_call = gt_tools_call};
+ if (janas_mcps_open(&srv) != 0) {
+ fprintf(stderr, "janas-git: cannot set its output aside\n");
+ return 1;
+ }
+ fprintf(stderr,
+ "janas-git %s: ready, 8 tools (git_status, git_log, git_diff, "
+ "git_branches; asked first: git_commit, git_pull, git_push, "
+ "git_switch)\n",
+ JANAS_VERSION);
+ janas_mcps_run(&srv);
+ return 0;
+}
diff --git a/src/git/read.c b/src/git/read.c
new file mode 100644
index 0000000..7835b45
--- /dev/null
+++ b/src/git/read.c
@@ -0,0 +1,230 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * read.c - janas-git reads git's output (see git.h), in the formats meant
+ * for programs: status --porcelain=v2 -z, log and for-each-ref with
+ * fields between 0x1f and records ended by 0x1e, diff --numstat -z. No git
+ * here, so that the tests can read output kept as it came.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "git.h"
+
+static void copy(char *to, size_t cap, const char *s, size_t n)
+{
+ snprintf(to, cap, "%.*s", (int)(n < cap ? n : cap - 1), s);
+}
+
+/* The field after skip spaces of a status line: where the path begins */
+static const char *after_fields(const char *s, const char *end, int skip)
+{
+ while (skip-- > 0) {
+ const char *sp = memchr(s, ' ', (size_t)(end - s));
+ if (!sp)
+ return NULL;
+ s = sp + 1;
+ }
+ return s;
+}
+
+int gt_read_status(const char *text, size_t n, struct gt_status *s)
+{
+ memset(s, 0, sizeof *s);
+ const char *p = text, *end = text + n;
+ while (p < end) {
+ const char *z = memchr(p, 0, (size_t)(end - p));
+ if (!z)
+ z = end;
+ size_t len = (size_t)(z - p);
+ const char *path = NULL;
+ struct gt_file f = {.staged = 0};
+ if (len > 16 && strncmp(p, "# branch.head ", 14) == 0) {
+ copy(s->branch, sizeof s->branch, p + 14, len - 14);
+ s->detached = strcmp(s->branch, "(detached)") == 0;
+ } else if (len > 15 && strncmp(p, "# branch.oid ", 13) == 0) {
+ copy(s->oid, 8, p + 13, len - 13);
+ } else if (len > 18 && strncmp(p, "# branch.upstream ", 18) == 0) {
+ copy(s->upstream, sizeof s->upstream, p + 18, len - 18);
+ } else if (len > 12 && strncmp(p, "# branch.ab ", 12) == 0) {
+ sscanf(p + 12, "+%d -%d", &s->ahead, &s->behind);
+ } else if (len > 4 && (p[0] == '1' || p[0] == '2') && p[1] == ' ') {
+ /* 1 XY sub mH mI mW hH hI path; 2 adds a score, then the
+ path it came from after the next 0 */
+ f.staged = p[2] != '.' ? p[2] : 0;
+ f.changed = p[3] != '.' ? p[3] : 0;
+ path = after_fields(p, z, p[0] == '1' ? 8 : 9);
+ if (f.staged)
+ s->n_staged++;
+ if (f.changed)
+ s->n_changed++;
+ } else if (len > 4 && p[0] == 'u' && p[1] == ' ') {
+ f.conflict = 1;
+ path = after_fields(p, z, 10);
+ s->n_conflicts++;
+ } else if (len > 2 && p[0] == '?' && p[1] == ' ') {
+ f.untracked = 1;
+ path = p + 2;
+ s->n_untracked++;
+ }
+ if (path) {
+ if (s->n < GT_FILES) {
+ copy(f.path, sizeof f.path, path, (size_t)(z - path));
+ s->f[s->n++] = f;
+ } else {
+ s->more++;
+ }
+ }
+ if (p[0] == '2') { /* the path it came from */
+ const char *z2 =
+ z < end ? memchr(z + 1, 0, (size_t)(end - z - 1)) : NULL;
+ z = z2 ? z2 : end;
+ }
+ p = z + 1;
+ }
+ return s->branch[0] ? 0 : -1;
+}
+
+/* The n-th field (0x1f between) of a record, as a pointer and a length */
+static const char *field(const char *r, size_t rn, int k, size_t *len)
+{
+ const char *e = r + rn;
+ while (k-- > 0) {
+ const char *u = memchr(r, 0x1f, (size_t)(e - r));
+ if (!u)
+ return NULL;
+ r = u + 1;
+ }
+ const char *u = memchr(r, 0x1f, (size_t)(e - r));
+ *len = (size_t)((u ? u : e) - r);
+ return r;
+}
+
+/* The next record (0x1e after it, line ends before it left out): its
+ start and length, NULL at the end */
+static const char *next_record(const char **p, const char *end, size_t *rn)
+{
+ while (*p < end && (**p == '\n' || **p == '\r'))
+ (*p)++;
+ const char *z = *p < end ? memchr(*p, 0x1e, (size_t)(end - *p)) : NULL;
+ if (!z)
+ return NULL;
+ const char *r = *p;
+ *rn = (size_t)(z - r);
+ *p = z + 1;
+ return r;
+}
+
+int gt_read_log(const char *text, size_t n, int max, struct gt_log *l)
+{
+ memset(l, 0, sizeof *l);
+ if (max > GT_ITEMS)
+ max = GT_ITEMS;
+ const char *p = text, *r;
+ size_t rn;
+ while ((r = next_record(&p, text + n, &rn))) {
+ if (l->n >= max) {
+ l->more = 1;
+ continue;
+ }
+ struct gt_commit *c = &l->c[l->n++];
+ size_t fl;
+ const char *f;
+ if ((f = field(r, rn, 0, &fl)))
+ copy(c->sha, sizeof c->sha, f, fl);
+ if ((f = field(r, rn, 1, &fl)))
+ copy(c->author, sizeof c->author, f, fl);
+ c->at = (f = field(r, rn, 2, &fl)) ? (time_t)strtoll(f, NULL, 10) : -1;
+ if ((f = field(r, rn, 3, &fl)))
+ copy(c->message, sizeof c->message, f, fl);
+ if ((f = field(r, rn, 4, &fl)))
+ copy(c->refs, sizeof c->refs, f, fl);
+ }
+ return 0;
+}
+
+int gt_read_branches(const char *text, size_t n, int max, struct gt_branches *b)
+{
+ memset(b, 0, sizeof *b);
+ if (max > GT_ITEMS)
+ max = GT_ITEMS;
+ const char *p = text, *r;
+ size_t rn;
+ while ((r = next_record(&p, text + n, &rn))) {
+ size_t fl;
+ const char *f = field(r, rn, 0, &fl);
+ int current = f && fl && *f == '*';
+ /* the branch one is on is listed whatever its place */
+ if (b->n >= max && !current) {
+ b->more++;
+ continue;
+ }
+ if (b->n >= max)
+ b->more++; /* the last listed gives way to it */
+ struct gt_branch *x = &b->b[b->n < max ? b->n++ : max - 1];
+ memset(x, 0, sizeof *x);
+ x->current = current;
+ if ((f = field(r, rn, 1, &fl)))
+ copy(x->name, sizeof x->name, f, fl);
+ if ((f = field(r, rn, 2, &fl)))
+ copy(x->upstream, sizeof x->upstream, f, fl);
+ if ((f = field(r, rn, 3, &fl)))
+ copy(x->track, sizeof x->track, f, fl);
+ x->at = (f = field(r, rn, 4, &fl)) ? (time_t)strtoll(f, NULL, 10) : -1;
+ if ((f = field(r, rn, 5, &fl)))
+ copy(x->message, sizeof x->message, f, fl);
+ }
+ return 0;
+}
+
+int gt_read_numstat(const char *text, size_t n, struct gt_diff *d)
+{
+ memset(d, 0, sizeof *d);
+ const char *p = text, *end = text + n;
+ while (p < end) {
+ const char *z = memchr(p, 0, (size_t)(end - p));
+ if (!z)
+ z = end;
+ /* "add\tdel\tpath", or "add\tdel\t" and the two paths of a
+ rename, each after a 0 */
+ char *t1, *t2;
+ long add = strtol(p, &t1, 10), del = 0;
+ int binary = *p == '-';
+ if (binary)
+ t1 = (char *)p + 1;
+ if (*t1 != '\t')
+ break;
+ del = binary ? 0 : strtol(t1 + 1, &t2, 10);
+ if (binary)
+ t2 = t1 + 2;
+ if (*t2 != '\t')
+ break;
+ const char *path = t2 + 1;
+ if (path == z) { /* a rename: from, then to */
+ const char *z1 =
+ z + 1 < end ? memchr(z + 1, 0, (size_t)(end - z - 1)) : NULL;
+ const char *z2 = z1 && z1 + 1 < end
+ ? memchr(z1 + 1, 0, (size_t)(end - z1 - 1))
+ : NULL;
+ if (!z1 || !z2)
+ break;
+ path = z1 + 1;
+ z = z2;
+ }
+ if (!binary) {
+ d->add += add;
+ d->del += del;
+ }
+ if (d->n < GT_FILES) {
+ struct gt_numstat *f = &d->f[d->n++];
+ copy(f->path, sizeof f->path, path, (size_t)(z - path));
+ f->add = binary ? -1 : add;
+ f->del = binary ? -1 : del;
+ } else {
+ d->more++;
+ }
+ p = z + 1;
+ }
+ return 0;
+}
diff --git a/src/git/repo.c b/src/git/repo.c
new file mode 100644
index 0000000..ba938d5
--- /dev/null
+++ b/src/git/repo.c
@@ -0,0 +1,113 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * repo.c - the repository the user means (see git.h): a path, a name
+ * listed in ~/.config/janas/git-repos.txt, or the directory janas-git runs
+ * in (janas-chat's). git itself says where its top is.
+ */
+#define _GNU_SOURCE /* strcasecmp */
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+#include "git.h"
+
+void gt_short_path(const char *path, char *out, size_t cap)
+{
+ const char *home = getenv("HOME");
+ size_t h = home ? strlen(home) : 0;
+ if (h > 1 && strncmp(path, home, h) == 0 && (path[h] == '/' || !path[h]))
+ snprintf(out, cap, "~%s", path + h);
+ else
+ snprintf(out, cap, "%s", path);
+}
+
+/* "~/x" with the home's path */
+static void expand(const char *q, char *out, size_t cap)
+{
+ const char *home = getenv("HOME");
+ if (q[0] == '~' && (q[1] == '/' || !q[1]) && home)
+ snprintf(out, cap, "%s%s", home, q + 1);
+ else
+ snprintf(out, cap, "%s", q);
+}
+
+/* A name of the list: its path into out. 1, 0 when not there. */
+static int listed(const char *name, char *out, size_t cap)
+{
+ char file[1024], line[1200], path[1100];
+ const char *xdg = getenv("XDG_CONFIG_HOME"), *home = getenv("HOME");
+ if (xdg && *xdg)
+ snprintf(file, sizeof file, "%s/janas/git-repos.txt", xdg);
+ else if (home)
+ snprintf(file, sizeof file, "%s/.config/janas/git-repos.txt", home);
+ else
+ return 0;
+ FILE *f = fopen(file, "r");
+ if (!f)
+ return 0;
+ int found = 0;
+ while (!found && fgets(line, sizeof line, f)) {
+ line[strcspn(line, "\r\n")] = 0;
+ if (!line[0] || line[0] == '#')
+ continue;
+ expand(line, path, sizeof path);
+ size_t n = strlen(path);
+ while (n > 1 && path[n - 1] == '/')
+ path[--n] = 0;
+ const char *last = strrchr(path, '/');
+ if (strcasecmp(last ? last + 1 : path, name) == 0) {
+ snprintf(out, cap, "%s", path);
+ found = 1;
+ }
+ }
+ fclose(f);
+ return found;
+}
+
+int gt_repo_find(const char *q, char *top, size_t cap, char *why,
+ size_t why_len)
+{
+ char dir[PATH_MAX + 64];
+ top[0] = 0;
+ if (!q || !*q) {
+ if (!getcwd(dir, sizeof dir)) {
+ snprintf(why, why_len, "no repository given");
+ return -1;
+ }
+ } else if (q[0] == '/' || q[0] == '~' || q[0] == '.') {
+ expand(q, dir, sizeof dir);
+ } else if (!strchr(q, '/') && listed(q, dir, sizeof dir)) {
+ ;
+ } else if (strchr(q, '/') || access(q, F_OK) == 0) {
+ expand(q, dir, sizeof dir); /* relative to where janas-git runs */
+ } else {
+ snprintf(why, why_len,
+ "no repository named %s: give its path, or list it in "
+ "~/.config/janas/git-repos.txt",
+ q);
+ return -1;
+ }
+ char real[PATH_MAX];
+ if (!realpath(dir, real)) {
+ snprintf(why, why_len, "%s is not there", q && *q ? q : dir);
+ return -1;
+ }
+ const char *argv[] = {"rev-parse", "--show-toplevel", NULL};
+ struct janas_buf out = {0}, err = {0};
+ int st = gt_run(real, argv, 10000, &out, &err, why, why_len);
+ if (st == 0 && out.n) {
+ out.p[strcspn(out.p, "\n")] = 0;
+ snprintf(top, cap, "%s", out.p);
+ } else if (st > 0) {
+ char s[PATH_MAX];
+ gt_short_path(real, s, sizeof s);
+ snprintf(why, why_len, "%s is not in a git repository", s);
+ }
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ return st == 0 && top[0] ? 0 : -1;
+}
diff --git a/src/git/run.c b/src/git/run.c
new file mode 100644
index 0000000..be3b7d7
--- /dev/null
+++ b/src/git/run.c
@@ -0,0 +1,138 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * run.c - janas-git runs git (see git.h): with its arguments as they are,
+ * no shell between; in a session of its own, without a terminal, so that
+ * a password asked (ssh, https) fails at once instead of waiting for an
+ * answer nobody can give; with a time limit, after which git and what it
+ * started are stopped.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "git.h"
+
+#define OUT_MAX (1u << 20)
+
+static double now_ms(void)
+{
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return (double)t.tv_sec * 1e3 + (double)t.tv_nsec / 1e6;
+}
+
+int gt_run(const char *dir, const char *const *argv, int timeout_ms,
+ struct janas_buf *out, struct janas_buf *err, char *why,
+ size_t why_len)
+{
+ const char *av[64];
+ size_t n = 0;
+ av[n++] = "git";
+ for (size_t i = 0; argv[i] && n < 63; i++)
+ av[n++] = argv[i];
+ av[n] = NULL;
+ int po[2], pe[2];
+ if (pipe2(po, O_CLOEXEC) != 0) {
+ snprintf(why, why_len, "no pipe: %s", strerror(errno));
+ return -1;
+ }
+ if (pipe2(pe, O_CLOEXEC) != 0) {
+ close(po[0]);
+ close(po[1]);
+ snprintf(why, why_len, "no pipe: %s", strerror(errno));
+ return -1;
+ }
+ pid_t pid = fork();
+ if (pid < 0) {
+ snprintf(why, why_len, "cannot start git: %s", strerror(errno));
+ close(po[0]), close(po[1]), close(pe[0]), close(pe[1]);
+ return -1;
+ }
+ if (pid == 0) {
+ setsid(); /* no terminal: nothing can be asked of the user */
+ int nul = open("/dev/null", O_RDONLY);
+ if (nul >= 0)
+ dup2(nul, 0);
+ dup2(po[1], 1);
+ dup2(pe[1], 2);
+ if (chdir(dir) != 0)
+ _exit(126);
+ setenv("GIT_TERMINAL_PROMPT", "0", 1);
+ setenv("GIT_EDITOR", "true", 1);
+ setenv("GIT_OPTIONAL_LOCKS", "0", 1); /* status takes no lock */
+ setenv("LC_ALL", "C", 1); /* git's words, in English */
+ execvp("git", (char *const *)av);
+ _exit(127);
+ }
+ close(po[1]);
+ close(pe[1]);
+ struct pollfd fd[2] = {{.fd = po[0], .events = POLLIN},
+ {.fd = pe[0], .events = POLLIN}};
+ struct janas_buf *to[2] = {out, err};
+ double end = now_ms() + timeout_ms;
+ int open_n = 2, late = 0;
+ while (open_n) {
+ int left = (int)(end - now_ms());
+ if (left <= 0) {
+ late = 1;
+ break;
+ }
+ if (poll(fd, 2, left) < 0) {
+ if (errno == EINTR)
+ continue;
+ break;
+ }
+ for (int i = 0; i < 2; i++) {
+ if (fd[i].fd < 0 || !(fd[i].revents & (POLLIN | POLLHUP | POLLERR)))
+ continue;
+ char chunk[8192];
+ ssize_t r = read(fd[i].fd, chunk, sizeof chunk);
+ if (r > 0 && to[i]->n < OUT_MAX)
+ janas_buf_put(to[i], chunk, (size_t)r);
+ else if (r == 0 || (r < 0 && errno != EINTR)) {
+ close(fd[i].fd);
+ fd[i].fd = -1;
+ open_n--;
+ }
+ }
+ }
+ for (int i = 0; i < 2; i++)
+ if (fd[i].fd >= 0)
+ close(fd[i].fd);
+ if (late)
+ kill(-pid, SIGKILL); /* git and what it started (ssh) */
+ int st = 0;
+ while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
+ ;
+ janas_buf_put(out, "", 1);
+ out->n--;
+ janas_buf_put(err, "", 1);
+ err->n--;
+ if (late) {
+ snprintf(why, why_len, "git took over %d s and was stopped",
+ timeout_ms / 1000);
+ return -1;
+ }
+ if (!WIFEXITED(st)) {
+ snprintf(why, why_len, "git stopped by a signal");
+ return -1;
+ }
+ if (WEXITSTATUS(st) == 127) {
+ snprintf(why, why_len, "git is not installed");
+ return -1;
+ }
+ if (WEXITSTATUS(st) == 126) {
+ snprintf(why, why_len, "cannot enter %s", dir);
+ return -1;
+ }
+ return WEXITSTATUS(st);
+}
diff --git a/src/git/tools.c b/src/git/tools.c
new file mode 100644
index 0000000..cbf7a91
--- /dev/null
+++ b/src/git/tools.c
@@ -0,0 +1,364 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * tools.c - janas-git's tools (see git.h) and what they share: those that
+ * read (git_status, git_log, git_diff, git_branches) say so; those that
+ * change something (git_commit, git_pull, git_push, git_switch, in
+ * write.c) say they do not only read, so that a client asks the user
+ * every time (janas-chat does, --mcp-auto or not).
+ */
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "common/mcp_server.h"
+#include "common/template.h"
+#include "git.h"
+
+#define LOCAL_MS 15000
+#define PATCH_LINES 400 /* of a diff shown to the user */
+#define PATCH_MAX 32000 /* bytes of it */
+#define BRIEF_PATCH 3000 /* of it the model reads */
+
+#define REPO_ARG \
+ "\"repo\": {\"type\": \"string\", \"description\": \"a path, or a name " \
+ "the user listed; leave it out for the one janas-chat runs in\"}"
+
+#define READS "\"annotations\": {\"readOnlyHint\": true}"
+#define WRITES \
+ "\"annotations\": {\"readOnlyHint\": false, \"destructiveHint\": false}"
+
+void gt_tools_list(void *ctx, struct janas_buf *b)
+{
+ (void)ctx;
+ janas_buf_puts(
+ b,
+ "\"tools\": [{\"name\": \"git_status\", \"title\": \"What has "
+ "changed\", \"description\": \"A repository's branch, what it is "
+ "ahead or behind, the files staged, changed, new.\", "
+ "\"inputSchema\": {\"type\": \"object\", \"properties\": "
+ "{" REPO_ARG "}, \"additionalProperties\": false}, " READS "}, "
+ "{\"name\": \"git_log\", \"title\": \"Commits\", \"description\": "
+ "\"A branch's latest commits.\", \"inputSchema\": {\"type\": "
+ "\"object\", \"properties\": {" REPO_ARG ", \"count\": {\"type\": "
+ "\"number\", \"description\": \"default 10, at most 20\"}, "
+ "\"branch\": {\"type\": \"string\"}, \"path\": {\"type\": "
+ "\"string\", \"description\": \"only those changing it\"}, "
+ "\"author\": {\"type\": \"string\"}, \"days\": {\"type\": "
+ "\"number\"}}, \"additionalProperties\": false}, " READS "}, "
+ "{\"name\": \"git_diff\", \"title\": \"The changes\", "
+ "\"description\": \"The changes not staged, or those staged for "
+ "the next commit: the files, and the diff shown to the user.\", "
+ "\"inputSchema\": {\"type\": \"object\", \"properties\": "
+ "{" REPO_ARG ", \"staged\": {\"type\": \"boolean\"}, \"path\": "
+ "{\"type\": \"string\"}}, \"additionalProperties\": false}, " READS
+ "}, "
+ "{\"name\": \"git_branches\", \"title\": \"Branches\", "
+ "\"description\": \"The local branches, the latest worked on "
+ "first.\", \"inputSchema\": {\"type\": \"object\", \"properties\": "
+ "{" REPO_ARG "}, \"additionalProperties\": false}, " READS "}, "
+ "{\"name\": \"git_commit\", \"title\": \"Commit\", \"description\": "
+ "\"Commits what is staged, after staging the files named, or all "
+ "the tracked files changed. The user is asked first.\", "
+ "\"inputSchema\": {\"type\": \"object\", \"properties\": "
+ "{" REPO_ARG ", \"message\": {\"type\": \"string\"}, \"files\": "
+ "{\"type\": \"array\", \"items\": {\"type\": \"string\"}}, \"all\": "
+ "{\"type\": \"boolean\", \"description\": \"stage every tracked "
+ "file changed\"}, \"signoff\": {\"type\": \"boolean\"}}, "
+ "\"required\": [\"message\"], \"additionalProperties\": false}, " WRITES
+ "}, "
+ "{\"name\": \"git_pull\", \"title\": \"Pull\", \"description\": "
+ "\"Brings the branch up to its remote one, only when that needs "
+ "no merge (fast-forward). The user is asked first.\", "
+ "\"inputSchema\": {\"type\": \"object\", \"properties\": "
+ "{" REPO_ARG "}, \"additionalProperties\": false}, " WRITES "}, "
+ "{\"name\": \"git_push\", \"title\": \"Push\", \"description\": "
+ "\"Sends the branch's commits to its remote branch (never by "
+ "force). The user is asked first.\", \"inputSchema\": {\"type\": "
+ "\"object\", \"properties\": {" REPO_ARG ", \"set_upstream\": "
+ "{\"type\": \"boolean\", \"description\": \"a branch not on the "
+ "remote yet: push it to origin and follow it\"}}, "
+ "\"additionalProperties\": false}, " WRITES "}, "
+ "{\"name\": \"git_switch\", \"title\": \"Switch branch\", "
+ "\"description\": \"Goes to another branch, or makes a new one and "
+ "goes to it; git refuses when changes would be lost. The user is "
+ "asked first.\", \"inputSchema\": {\"type\": \"object\", "
+ "\"properties\": {" REPO_ARG ", \"branch\": {\"type\": \"string\"}, "
+ "\"create\": {\"type\": \"boolean\"}}, \"required\": [\"branch\"], "
+ "\"additionalProperties\": false}, " WRITES "}]");
+}
+
+const char *gt_arg_str(const struct janas_json *args, const char *name)
+{
+ const char *s = janas_json_str(janas_json_get(args, name));
+ return s && *s ? s : NULL;
+}
+
+static double arg_num(const struct janas_json *args, const char *name,
+ double def)
+{
+ const struct janas_json *v = janas_json_get(args, name);
+ return v && v->type == JANAS_JSON_NUMBER ? janas_json_num(v, def) : def;
+}
+
+int gt_arg_bool(const struct janas_json *args, const char *name)
+{
+ const struct janas_json *v = janas_json_get(args, name);
+ return v && v->type == JANAS_JSON_TRUE;
+}
+
+int gt_plain_arg(const char *s)
+{
+ return s && *s && *s != '-' && !strpbrk(s, "\r\n");
+}
+
+int gt_fail(struct janas_buf *b, const char *fmt, ...)
+{
+ struct janas_buf out = {0};
+ char line[4096];
+ va_list ap;
+ va_start(ap, fmt);
+ vsnprintf(line, sizeof line, fmt, ap);
+ va_end(ap);
+ janas_buf_puts(&out, line);
+ janas_mcps_text_result(b, out.p ? out.p : "", out.n, 1);
+ janas_buf_free(&out);
+ return 0;
+}
+
+int gt_answer(struct janas_buf *b, const char *name, struct janas_buf *d,
+ const char *layout, const char *brief)
+{
+ char err[300];
+ int oom = d->oom;
+ int r = oom ? -1
+ : janas_tpl_result(b, name, d->p, d->n, layout, brief, err,
+ sizeof err);
+ janas_buf_free(d);
+ if (r != 0)
+ return gt_fail(b, "The answer could not be written: %s.",
+ oom ? "out of memory" : err);
+ return 0;
+}
+
+/* The repository of the arguments: 0, or -1 with the error written. */
+static int repo_of(const struct janas_json *args, char *top, size_t cap,
+ struct janas_buf *b)
+{
+ char why[600];
+ if (gt_repo_find(gt_arg_str(args, "repo"), top, cap, why, sizeof why) == 0)
+ return 0;
+ gt_fail(b, "%s.", why);
+ return -1;
+}
+
+/* git's complaint, its first lines */
+static const char *said(const struct janas_buf *err, const struct janas_buf *o)
+{
+ const char *s = err->n ? err->p : o->n ? o->p : "no reason given";
+ return s;
+}
+
+static int tool_status(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ const char *argv[] = {"status", "--porcelain=v2", "--branch", "-z", NULL};
+ struct janas_buf out = {0}, err = {0};
+ int st = gt_run(top, argv, LOCAL_MS, &out, &err, why, sizeof why);
+ struct gt_status s;
+ int ok = st == 0 && gt_read_status(out.p, out.n, &s) == 0;
+ if (!ok)
+ gt_fail(b, "git status failed: %.1500s",
+ st < 0 ? why : said(&err, &out));
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ if (!ok)
+ return 0;
+ struct janas_buf d = {0};
+ gt_status_data(&d, top, &s);
+ return gt_answer(b, "git_status", &d, GT_STATUS_LAYOUT, GT_STATUS_BRIEF);
+}
+
+/* The branch one is on ("HEAD" when none) */
+static void current_branch(const char *top, char *out, size_t cap)
+{
+ const char *argv[] = {"rev-parse", "--abbrev-ref", "HEAD", NULL};
+ struct janas_buf o = {0}, e = {0};
+ char why[200];
+ if (gt_run(top, argv, LOCAL_MS, &o, &e, why, sizeof why) == 0 && o.n)
+ snprintf(out, cap, "%.*s", (int)strcspn(o.p, "\n"), o.p);
+ else
+ snprintf(out, cap, "HEAD");
+ janas_buf_free(&o);
+ janas_buf_free(&e);
+}
+
+static int tool_log(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300], n[16], author[300], since[48], branch[256];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ int count = (int)arg_num(args, "count", 10);
+ count = count < 1 ? 1 : count > GT_ITEMS ? GT_ITEMS : count;
+ snprintf(n, sizeof n, "-n%d", count + 1); /* one more: are there more? */
+ const char *argv[16];
+ int k = 0;
+ argv[k++] = "log";
+ argv[k++] = n;
+ argv[k++] = "--format=%h%x1f%an%x1f%at%x1f%s%x1f%D%x1e";
+ const char *a = gt_arg_str(args, "author");
+ if (a) {
+ snprintf(author, sizeof author, "--author=%s", a);
+ argv[k++] = author;
+ }
+ double days = arg_num(args, "days", 0);
+ if (days > 0) {
+ snprintf(since, sizeof since, "--since=%.0f.days.ago", days);
+ argv[k++] = since;
+ }
+ const char *br = gt_arg_str(args, "branch");
+ if (br && !gt_plain_arg(br))
+ return gt_fail(b, "\"%s\" is not a branch's name.", br);
+ if (br) {
+ argv[k++] = br;
+ snprintf(branch, sizeof branch, "%s", br);
+ } else {
+ current_branch(top, branch, sizeof branch);
+ }
+ argv[k++] = "--";
+ const char *path = gt_arg_str(args, "path");
+ if (path)
+ argv[k++] = path;
+ argv[k] = NULL;
+ struct janas_buf out = {0}, err = {0};
+ int st = gt_run(top, argv, LOCAL_MS, &out, &err, why, sizeof why);
+ struct gt_log l;
+ if (st == 0)
+ gt_read_log(out.p, out.n, count, &l);
+ else
+ gt_fail(b, "git log failed: %.1500s", st < 0 ? why : said(&err, &out));
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ if (st != 0)
+ return 0;
+ struct janas_buf d = {0};
+ gt_log_data(&d, top, branch, &l);
+ return gt_answer(b, "git_log", &d, GT_LOG_LAYOUT, GT_LOG_BRIEF);
+}
+
+static int tool_branches(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ const char *argv[] = {
+ "for-each-ref", "refs/heads", "--sort=-committerdate",
+ "--format=%(HEAD)%1f%(refname:short)%1f%(upstream:short)%1f"
+ "%(upstream:track)%1f%(committerdate:unix)%1f%(subject)%1e",
+ NULL};
+ struct janas_buf out = {0}, err = {0};
+ int st = gt_run(top, argv, LOCAL_MS, &out, &err, why, sizeof why);
+ struct gt_branches v;
+ if (st == 0)
+ gt_read_branches(out.p, out.n, GT_ITEMS, &v);
+ else
+ gt_fail(b, "git for-each-ref failed: %.1500s",
+ st < 0 ? why : said(&err, &out));
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ if (st != 0)
+ return 0;
+ struct janas_buf d = {0};
+ gt_branches_data(&d, top, &v);
+ return gt_answer(b, "git_branches", &d, GT_BRANCHES_LAYOUT,
+ GT_BRANCHES_BRIEF);
+}
+
+/* The first PATCH_LINES lines, PATCH_MAX bytes at most, ending at a line */
+static size_t patch_cut(const char *p, size_t n, size_t max_bytes)
+{
+ size_t at = 0;
+ int lines = 0;
+ while (at < n && lines < PATCH_LINES) {
+ const char *nl = memchr(p + at, '\n', n - at);
+ size_t next = nl ? (size_t)(nl - p) + 1 : n;
+ if (next > max_bytes)
+ break;
+ at = next;
+ lines++;
+ }
+ return at;
+}
+
+static int tool_diff(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ int staged = gt_arg_bool(args, "staged");
+ const char *path = gt_arg_str(args, "path");
+ const char *num[8], *pat[8];
+ int k = 0, j = 0;
+ num[k++] = "diff", pat[j++] = "diff";
+ if (staged)
+ num[k++] = "--cached", pat[j++] = "--cached";
+ num[k++] = "--numstat";
+ num[k++] = "-z";
+ pat[j++] = "--no-color";
+ pat[j++] = "--no-ext-diff";
+ num[k++] = "--", pat[j++] = "--";
+ if (path)
+ num[k++] = path, pat[j++] = path;
+ num[k] = pat[j] = NULL;
+ struct janas_buf out = {0}, err = {0}, patch = {0};
+ int st = gt_run(top, num, LOCAL_MS, &out, &err, why, sizeof why);
+ struct gt_diff d;
+ if (st == 0) {
+ gt_read_numstat(out.p, out.n, &d);
+ janas_buf_free(&err);
+ st = gt_run(top, pat, LOCAL_MS, &patch, &err, why, sizeof why);
+ }
+ if (st != 0)
+ gt_fail(b, "git diff failed: %.1500s", st < 0 ? why : said(&err, &out));
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ if (st != 0) {
+ janas_buf_free(&patch);
+ return 0;
+ }
+ size_t shown = patch_cut(patch.p, patch.n, PATCH_MAX);
+ size_t start = patch_cut(patch.p, shown, BRIEF_PATCH);
+ struct janas_buf data = {0};
+ gt_diff_data(&data, top, staged, path, &d, patch.p, shown, start);
+ janas_buf_free(&patch);
+ return gt_answer(b, "git_diff", &data, GT_DIFF_LAYOUT, GT_DIFF_BRIEF);
+}
+
+int gt_tools_call(void *ctx, const struct janas_json *params,
+ struct janas_buf *b)
+{
+ (void)ctx;
+ const struct janas_json *name = janas_json_get(params, "name");
+ const struct janas_json *args = janas_json_get(params, "arguments");
+ if (args && args->type != JANAS_JSON_OBJECT)
+ args = NULL;
+ if (janas_json_is(name, "git_status"))
+ return tool_status(args, b);
+ if (janas_json_is(name, "git_log"))
+ return tool_log(args, b);
+ if (janas_json_is(name, "git_diff"))
+ return tool_diff(args, b);
+ if (janas_json_is(name, "git_branches"))
+ return tool_branches(args, b);
+ if (janas_json_is(name, "git_commit"))
+ return gt_tool_commit(args, b);
+ if (janas_json_is(name, "git_pull"))
+ return gt_tool_pull(args, b);
+ if (janas_json_is(name, "git_push"))
+ return gt_tool_push(args, b);
+ if (janas_json_is(name, "git_switch"))
+ return gt_tool_switch(args, b);
+ return -1;
+}
diff --git a/src/git/write.c b/src/git/write.c
new file mode 100644
index 0000000..8f62d8b
--- /dev/null
+++ b/src/git/write.c
@@ -0,0 +1,235 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * write.c - janas-git's tools that change something (see git.h): commit,
+ * pull, push, switch. Each is asked of the user first by the client
+ * (readOnlyHint false). What they never do: amend, skip the hooks, merge
+ * or rebase on a pull (fast-forward only), push by force, throw away a
+ * change (git switch refuses when it would), take an argument of the
+ * model's as an option.
+ */
+#include <stdio.h>
+#include <string.h>
+
+#include "git.h"
+
+#define LOCAL_MS 15000
+#define HOOKS_MS 45000 /* a commit's hooks may run tests */
+#define REMOTE_MS 50000 /* janas-chat waits a minute for a tool */
+#define FILES_MAX 32
+#define OUTPUT_MAX 1500
+
+static int run(const char *top, const char *const *argv, int ms,
+ struct janas_buf *out, struct janas_buf *err, char *why,
+ size_t why_len)
+{
+ out->n = err->n = 0;
+ return gt_run(top, argv, ms, out, err, why, why_len);
+}
+
+/* git's words, cut: what it printed, its error stream first */
+static void words(char *to, size_t cap, const struct janas_buf *out,
+ const struct janas_buf *err)
+{
+ size_t k = 0;
+ to[0] = 0;
+ const struct janas_buf *v[2] = {err, out};
+ for (int i = 0; i < 2; i++)
+ if (v[i]->n && k + 1 < cap)
+ k += (size_t)snprintf(to + k, cap - k, "%s%.*s", k ? "\n" : "",
+ (int)v[i]->n, v[i]->p);
+ while (k && (to[k - 1] == '\n' || to[k - 1] == ' '))
+ to[--k] = 0;
+}
+
+static int repo_of(const struct janas_json *args, char *top, size_t cap,
+ struct janas_buf *b)
+{
+ char why[600];
+ if (gt_repo_find(gt_arg_str(args, "repo"), top, cap, why, sizeof why) == 0)
+ return 0;
+ gt_fail(b, "%s.", why);
+ return -1;
+}
+
+static void branch_of(const char *top, char *out, size_t cap)
+{
+ const char *argv[] = {"rev-parse", "--abbrev-ref", "HEAD", NULL};
+ struct janas_buf o = {0}, e = {0};
+ char why[200];
+ out[0] = 0;
+ if (gt_run(top, argv, LOCAL_MS, &o, &e, why, sizeof why) == 0 && o.n)
+ snprintf(out, cap, "%.*s", (int)strcspn(o.p, "\n"), o.p);
+ janas_buf_free(&o);
+ janas_buf_free(&e);
+}
+
+static int done(struct janas_buf *b, const char *top, const char *what,
+ const char *branch, const char *sha, const char *message,
+ const char *output)
+{
+ struct janas_buf d = {0};
+ gt_done_data(&d, top, what, branch, sha, message, output);
+ char name[32];
+ snprintf(name, sizeof name, "git_%s",
+ strcmp(what, "create") == 0 ? "switch" : what);
+ return gt_answer(b, name, &d, GT_DONE_LAYOUT, GT_DONE_BRIEF);
+}
+
+int gt_tool_commit(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300], said[OUTPUT_MAX];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ const char *message = gt_arg_str(args, "message");
+ if (!message)
+ return gt_fail(b, "No message given for the commit.");
+ struct janas_buf out = {0}, err = {0};
+ int st = 0;
+ /* the files named, after "--": a path is never an option */
+ const struct janas_json *files = janas_json_get(args, "files");
+ if (files && files->type == JANAS_JSON_ARRAY && files->child) {
+ const char *argv[FILES_MAX + 4];
+ int k = 0;
+ argv[k++] = "add";
+ argv[k++] = "--";
+ for (const struct janas_json *f = files->child; f; f = f->next) {
+ const char *p = janas_json_str(f);
+ if (!p || !*p || strpbrk(p, "\r\n") || k >= FILES_MAX + 2) {
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ return gt_fail(b,
+ "The files to stage: at most %d paths, "
+ "each a line.",
+ FILES_MAX);
+ }
+ argv[k++] = p;
+ }
+ argv[k] = NULL;
+ st = run(top, argv, LOCAL_MS, &out, &err, why, sizeof why);
+ } else if (gt_arg_bool(args, "all")) {
+ const char *argv[] = {"add", "-u", NULL}; /* the tracked ones */
+ st = run(top, argv, LOCAL_MS, &out, &err, why, sizeof why);
+ }
+ if (st != 0) {
+ words(said, sizeof said, &out, &err);
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ return gt_fail(b, "git add failed: %s", st < 0 ? why : said);
+ }
+ const char *check[] = {"diff", "--cached", "--quiet", NULL};
+ st = run(top, check, LOCAL_MS, &out, &err, why, sizeof why);
+ if (st == 0) {
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ return gt_fail(b, "Nothing is staged: name the files to commit, or "
+ "ask for all the tracked files changed.");
+ }
+ const char *commit[] = {"commit", "-m", message, NULL, NULL};
+ if (gt_arg_bool(args, "signoff"))
+ commit[3] = "--signoff";
+ st = run(top, commit, HOOKS_MS, &out, &err, why, sizeof why);
+ if (st != 0) {
+ words(said, sizeof said, &out, &err);
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ return gt_fail(b, "git commit failed: %s", st < 0 ? why : said);
+ }
+ /* the files the commit holds: those staged before count too */
+ const char *show[] = {"show", "--stat=72", "--format=%h", "HEAD", NULL};
+ char sha[16] = "", stat[OUTPUT_MAX] = "", branch[256];
+ if (run(top, show, LOCAL_MS, &out, &err, why, sizeof why) == 0 && out.n) {
+ snprintf(sha, sizeof sha, "%.*s", (int)strcspn(out.p, "\n"), out.p);
+ const char *s = strchr(out.p, '\n');
+ while (s && *s == '\n')
+ s++;
+ if (s)
+ snprintf(stat, sizeof stat, "%s", s);
+ size_t k = strlen(stat);
+ while (k && stat[k - 1] == '\n')
+ stat[--k] = 0;
+ }
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ branch_of(top, branch, sizeof branch);
+ char first[200];
+ snprintf(first, sizeof first, "%.*s", (int)strcspn(message, "\n"), message);
+ return done(b, top, "commit", branch, sha, first, stat);
+}
+
+int gt_tool_pull(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300], said[OUTPUT_MAX], branch[256];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ const char *argv[] = {"pull", "--ff-only", NULL};
+ struct janas_buf out = {0}, err = {0};
+ int st = run(top, argv, REMOTE_MS, &out, &err, why, sizeof why);
+ words(said, sizeof said, &out, &err);
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ if (st != 0)
+ return gt_fail(b, "git pull (fast-forward only) failed: %s",
+ st < 0 ? why : said);
+ branch_of(top, branch, sizeof branch);
+ return done(b, top, "pull", branch, "", "", said);
+}
+
+int gt_tool_push(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300], said[OUTPUT_MAX], branch[256];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ branch_of(top, branch, sizeof branch);
+ struct janas_buf out = {0}, err = {0};
+ const char *up[] = {"rev-parse", "--abbrev-ref", "--symbolic-full-name",
+ "@{upstream}", NULL};
+ int has_up = run(top, up, LOCAL_MS, &out, &err, why, sizeof why) == 0;
+ int set = gt_arg_bool(args, "set_upstream");
+ if (!has_up && !set) {
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ return gt_fail(b,
+ "The branch %s follows no remote branch: ask the user "
+ "whether to push it to origin and follow it "
+ "(set_upstream).",
+ branch);
+ }
+ const char *plain[] = {"push", NULL};
+ const char *first[] = {"push", "-u", "origin", "HEAD", NULL};
+ int st = run(top, has_up ? plain : first, REMOTE_MS, &out, &err, why,
+ sizeof why);
+ words(said, sizeof said, &out, &err);
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ if (st != 0)
+ return gt_fail(b, "git push failed: %s", st < 0 ? why : said);
+ return done(b, top, "push", branch, "", "", said);
+}
+
+int gt_tool_switch(const struct janas_json *args, struct janas_buf *b)
+{
+ char top[4096], why[300], said[OUTPUT_MAX];
+ if (repo_of(args, top, sizeof top, b) != 0)
+ return 0;
+ const char *name = gt_arg_str(args, "branch");
+ struct janas_buf out = {0}, err = {0};
+ const char *check[] = {"check-ref-format", "--branch", name, NULL};
+ if (!gt_plain_arg(name) ||
+ run(top, check, LOCAL_MS, &out, &err, why, sizeof why) != 0) {
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ return gt_fail(b, "\"%s\" is not a branch's name.", name ? name : "");
+ }
+ int create = gt_arg_bool(args, "create");
+ const char *go[] = {"switch", name, NULL};
+ const char *make[] = {"switch", "-c", name, NULL};
+ int st =
+ run(top, create ? make : go, LOCAL_MS, &out, &err, why, sizeof why);
+ words(said, sizeof said, &out, &err);
+ janas_buf_free(&out);
+ janas_buf_free(&err);
+ if (st != 0)
+ return gt_fail(b, "git switch failed: %s", st < 0 ? why : said);
+ return done(b, top, create ? "create" : "switch", name, "", "", said);
+}
diff --git a/src/github/data.c b/src/github/data.c
new file mode 100644
index 0000000..b4bda77
--- /dev/null
+++ b/src/github/data.c
@@ -0,0 +1,155 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * data.c - what janas-github read, as the data of its layouts (see
+ * github.h, layouts.c and common/template.h). Every field is written, even
+ * empty: a layout looks a name up in the item, then in what holds it, and
+ * an issue without labels would show another's.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include "github.h"
+
+void gh_jstr(struct janas_buf *b, const char *key, const char *v)
+{
+ if (!v)
+ v = "";
+ janas_buf_printf(b, ", \"%s\": ", key);
+ janas_json_write_str(b, v, strlen(v));
+}
+
+void gh_jwhen(struct janas_buf *b, const char *key, time_t t)
+{
+ if (t < 0) {
+ janas_buf_printf(b,
+ ", \"%s\": {\"day\": 0, \"mon\": 0, \"year\": 0, "
+ "\"at\": \"\", \"ago_d\": 0}",
+ key);
+ return;
+ }
+ struct tm lt, nt;
+ time_t now = time(NULL);
+ localtime_r(&t, &lt);
+ localtime_r(&now, &nt);
+ long ago = (long)((now - t) / 86400);
+ janas_buf_printf(b,
+ ", \"%s\": {\"day\": %d, \"mon\": %d, \"year\": %d, "
+ "\"at\": \"%02d:%02d\", \"ago_d\": %ld}",
+ key, lt.tm_mday, lt.tm_mon + 1,
+ lt.tm_year == nt.tm_year ? 0 : lt.tm_year + 1900,
+ lt.tm_hour, lt.tm_min, ago < 0 ? 0 : ago);
+}
+
+static void head(struct janas_buf *b, const char *repo, const char *how)
+{
+ janas_buf_puts(b, "{\"repo\": ");
+ janas_json_write_str(b, repo, strlen(repo));
+ gh_jstr(b, "how", how);
+}
+
+void gh_issues_data(struct janas_buf *b, const char *repo, const char *how,
+ int pr, const char *state, time_t since, int first,
+ const struct gh_issues *v)
+{
+ head(b, repo, how);
+ char kind[32]; /* "open_issues", "new_pulls": a word of the layout */
+ snprintf(kind, sizeof kind, "%s_%s", since > 0 ? "new" : state,
+ pr ? "pulls" : "issues");
+ gh_jstr(b, "kind", kind);
+ janas_buf_printf(b, ", \"total\": %ld, \"shown\": %d, \"more\": %s",
+ v->total, v->n, v->total > v->n ? "true" : "false");
+ janas_buf_printf(b, ", \"new\": %s, \"first\": %s",
+ since > 0 ? "true" : "false", first ? "true" : "false");
+ gh_jwhen(b, "since", since > 0 ? since : -1);
+ int open_n = 0;
+ for (int i = 0; i < v->n; i++)
+ open_n += v->i[i].open;
+ janas_buf_printf(b, ", \"open_n\": %d, \"closed_n\": %d", open_n,
+ v->n - open_n);
+ janas_buf_puts(b, ", \"items\": [");
+ for (int i = 0; i < v->n; i++) {
+ const struct gh_issue *x = &v->i[i];
+ janas_buf_printf(b, "%s{\"n\": %d", i ? ", " : "", x->number);
+ gh_jstr(b, "title", x->title);
+ gh_jstr(b, "user", x->user);
+ gh_jwhen(b, "created", x->created);
+ janas_buf_printf(b, ", \"comments\": %d", x->comments);
+ gh_jstr(b, "labels", x->labels);
+ gh_jstr(b, "status",
+ x->merged ? "merged"
+ : x->draft && x->open ? "draft"
+ : x->open ? "open"
+ : "closed");
+ gh_jstr(b, "url", x->url);
+ janas_buf_puts(b, "}");
+ }
+ janas_buf_puts(b, "]}");
+}
+
+void gh_releases_data(struct janas_buf *b, const char *repo, const char *how,
+ const struct gh_releases *v)
+{
+ head(b, repo, how);
+ janas_buf_printf(b, ", \"n\": %d, \"one\": %s, \"tags\": %s", v->n,
+ v->n == 1 ? "true" : "false", v->tags ? "true" : "false");
+ janas_buf_puts(b, ", \"items\": [");
+ for (int i = 0; i < v->n; i++) {
+ const struct gh_release *x = &v->r[i];
+ janas_buf_printf(b, "%s{\"tag\": ", i ? ", " : "");
+ janas_json_write_str(b, x->tag, strlen(x->tag));
+ gh_jstr(b, "name", x->name);
+ gh_jwhen(b, "when", x->at);
+ gh_jstr(b, "author", x->author);
+ janas_buf_printf(b, ", \"pre\": %s, \"assets\": %d",
+ x->pre ? "true" : "false", x->assets);
+ gh_jstr(b, "notes", x->notes);
+ gh_jstr(b, "url", x->url);
+ janas_buf_puts(b, "}");
+ }
+ janas_buf_puts(b, "]}");
+}
+
+void gh_commits_data(struct janas_buf *b, const char *repo, const char *how,
+ const char *branch, int days, const struct gh_commits *v)
+{
+ head(b, repo, how);
+ gh_jstr(b, "branch", branch);
+ janas_buf_printf(b, ", \"days\": %d, \"n\": %d, \"more\": %s", days, v->n,
+ v->more ? "true" : "false");
+ janas_buf_puts(b, ", \"items\": [");
+ for (int i = 0; i < v->n; i++) {
+ const struct gh_commit *x = &v->c[i];
+ janas_buf_printf(b, "%s{\"sha\": ", i ? ", " : "");
+ janas_json_write_str(b, x->sha, strlen(x->sha));
+ gh_jstr(b, "message", x->message);
+ gh_jstr(b, "author", x->author);
+ gh_jwhen(b, "when", x->at);
+ gh_jstr(b, "url", x->url);
+ janas_buf_puts(b, "}");
+ }
+ janas_buf_puts(b, "]}");
+}
+
+void gh_repo_data(struct janas_buf *b, const char *how, const struct gh_repo *r)
+{
+ head(b, r->full, how);
+ gh_jstr(b, "description", r->description);
+ gh_jstr(b, "language", r->language);
+ gh_jstr(b, "license", r->license);
+ gh_jstr(b, "branch", r->branch);
+ gh_jstr(b, "homepage", r->homepage);
+ gh_jstr(b, "topics", r->topics);
+ gh_jstr(b, "url", r->url);
+ janas_buf_printf(b,
+ ", \"stars\": %ld, \"forks\": %ld, \"watchers\": %ld, "
+ "\"open_issues\": %ld",
+ r->stars, r->forks, r->watchers, r->open_issues);
+ gh_jwhen(b, "pushed", r->pushed);
+ gh_jwhen(b, "created", r->created);
+ janas_buf_printf(b, ", \"archived\": %s, \"private\": %s, \"fork\": %s}",
+ r->archived ? "true" : "false",
+ r->private_ ? "true" : "false",
+ r->fork ? "true" : "false");
+}
diff --git a/src/github/fetch.c b/src/github/fetch.c
new file mode 100644
index 0000000..858cceb
--- /dev/null
+++ b/src/github/fetch.c
@@ -0,0 +1,163 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * fetch.c - janas-github asks GitHub's REST API (see github.h). Without a
+ * token GitHub answers 60 requests an hour and 10 searches a minute; with
+ * one 5,000 and 30, and the user's private repositories too. The token is
+ * GH_TOKEN's or GITHUB_TOKEN's, else the gh command's (gh auth token): it
+ * is kept in memory and never written anywhere. Answers are kept a
+ * minute, so that a question asked again costs nothing.
+ */
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "common/https_get.h"
+#include "github.h"
+
+#ifndef JANAS_VERSION
+#define JANAS_VERSION "unknown"
+#endif
+
+#define API "https://api.github.com"
+#define KEEP_S 60
+#define N_KEPT 32
+
+static struct {
+ char *path;
+ time_t at;
+ struct janas_buf body;
+} kept[N_KEPT];
+
+static char token[256];
+static int kind = -1; /* not looked for yet */
+
+/* A token is letters, digits and underscores: anything else is not one
+ (a message of gh's, a line end). */
+static int take(const char *s, int k)
+{
+ size_t n = s ? strlen(s) : 0;
+ while (n && isspace((unsigned char)s[n - 1]))
+ n--;
+ if (n < 20 || n >= sizeof token)
+ return 0;
+ for (size_t i = 0; i < n; i++)
+ if (!isalnum((unsigned char)s[i]) && s[i] != '_')
+ return 0;
+ memcpy(token, s, n);
+ token[n] = 0;
+ kind = k;
+ return 1;
+}
+
+int gh_token_kind(void)
+{
+ if (kind >= 0)
+ return kind;
+ kind = 0;
+ if (take(getenv("GH_TOKEN"), 1) || take(getenv("GITHUB_TOKEN"), 1))
+ return kind;
+ FILE *p = popen("gh auth token 2>/dev/null", "r");
+ if (p) {
+ char line[512] = "";
+ if (!fgets(line, sizeof line, p))
+ line[0] = 0;
+ pclose(p);
+ take(line, 2);
+ memset(line, 0, sizeof line);
+ }
+ return kind;
+}
+
+void gh_url_put(struct janas_buf *b, const char *s)
+{
+ static const char hex[] = "0123456789ABCDEF";
+ for (; *s; s++) {
+ unsigned char c = (unsigned char)*s;
+ if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
+ janas_buf_put(b, (const char *)&c, 1);
+ else {
+ char e[3] = {'%', hex[c >> 4], hex[c & 15]};
+ janas_buf_put(b, e, 3);
+ }
+ }
+}
+
+static int ask(const char *url, int with_token, struct janas_buf *out,
+ char *err, size_t err_len)
+{
+ char auth[300];
+ const char *h[4] = {"User-Agent: janas-github/" JANAS_VERSION
+ " (+https://github.com/prabanta-dev/janas)",
+ "Accept: application/vnd.github+json",
+ "X-GitHub-Api-Version: 2022-11-28", auth};
+ snprintf(auth, sizeof auth, "Authorization: Bearer %s", token);
+ int status = janas_https_get(url, h, with_token ? 4 : 3, out, err, err_len);
+ memset(auth, 0, sizeof auth);
+ return status;
+}
+
+int gh_get(const char *path, struct janas_buf *out, char *err, size_t err_len)
+{
+ time_t now = time(NULL);
+ int slot = 0;
+ for (int i = 0; i < N_KEPT; i++) {
+ if (kept[i].path && strcmp(kept[i].path, path) == 0 &&
+ now - kept[i].at < KEEP_S) {
+ out->n = 0;
+ janas_buf_put(out, kept[i].body.p, kept[i].body.n);
+ janas_buf_put(out, "", 1);
+ out->n--;
+ return out->oom ? -1 : 200;
+ }
+ if (kept[i].at < kept[slot].at)
+ slot = i;
+ }
+ char url[2048];
+ snprintf(url, sizeof url, "%s%s", API, path);
+ int tok = gh_token_kind() > 0;
+ int status = ask(url, tok, out, err, err_len);
+ if (status == 401 && tok) { /* a token GitHub no longer takes */
+ kind = 0;
+ memset(token, 0, sizeof token);
+ status = ask(url, 0, out, err, err_len);
+ }
+ if (status == 200 && !out->oom) {
+ free(kept[slot].path);
+ janas_buf_free(&kept[slot].body);
+ kept[slot].path = strdup(path);
+ kept[slot].at = now;
+ janas_buf_put(&kept[slot].body, out->p, out->n);
+ return 200;
+ }
+ if (status == 403 || status == 429)
+ snprintf(err, err_len,
+ "GitHub refused (HTTP %d): its limit of requests is "
+ "reached (%s), or this is not allowed",
+ status,
+ gh_token_kind() ? "5,000 an hour, 30 searches a minute"
+ : "60 an hour and 10 searches a minute "
+ "without a token: gh auth login gives one");
+ else if (status == 404)
+ snprintf(err, err_len,
+ "GitHub has nothing there (HTTP 404): no such repository, "
+ "or a private one%s",
+ gh_token_kind() ? " of someone else" : " (no token is used)");
+ else if (status == 422)
+ snprintf(err, err_len, "GitHub could not do this search (HTTP 422)");
+ else if (status > 0 && status != 200)
+ snprintf(err, err_len, "GitHub: HTTP %d", status);
+ return status;
+}
+
+void gh_fetch_free(void)
+{
+ for (int i = 0; i < N_KEPT; i++) {
+ free(kept[i].path);
+ janas_buf_free(&kept[i].body);
+ }
+ memset(kept, 0, sizeof kept);
+ memset(token, 0, sizeof token);
+}
diff --git a/src/github/github.h b/src/github/github.h
new file mode 100644
index 0000000..214db28
--- /dev/null
+++ b/src/github/github.h
@@ -0,0 +1,147 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * github.h - janas-github, GitHub as an MCP service that only reads: what
+ * its parts share. fetch.c asks GitHub's REST API (with the token of the
+ * gh command when there is one), repo.c finds a repository by its name,
+ * read.c reads the answers, seen.c remembers when the user last looked,
+ * data.c and layouts.c write the answers as data with a layout, tools.c
+ * offers the tools.
+ */
+#ifndef JANAS_GITHUB_H
+#define JANAS_GITHUB_H
+
+#include <stddef.h>
+#include <time.h>
+
+#include "llm/json.h"
+
+/* ---- fetch.c ---- */
+
+/* GET of a path of the API ("/repos/o/r"), the body into out; answers
+ kept a minute. The HTTP status, or -1 with the reason in err; a status
+ that is not 200 with what it means in err. */
+int gh_get(const char *path, struct janas_buf *out, char *err, size_t err_len);
+void gh_fetch_free(void);
+/* Whether a token is used: 1 from GH_TOKEN or GITHUB_TOKEN, 2 from the gh
+ command, 0 none (60 requests an hour). */
+int gh_token_kind(void);
+/* s into b as a part of a URL's query, escaped. */
+void gh_url_put(struct janas_buf *b, const char *s);
+
+/* ---- read.c: the answers (no network: the tests read kept ones) ---- */
+
+/* A text of the API ("2026-10-02T11:18:31Z") as a time; -1 when none. */
+time_t gh_utc(const char *s);
+
+#define GH_ITEMS 10
+
+struct gh_issue {
+ int number;
+ char title[200], user[64], labels[160], url[160];
+ time_t created, closed;
+ int comments, pr, draft, merged, open;
+};
+
+struct gh_issues {
+ long total; /* all those the search found */
+ struct gh_issue i[GH_ITEMS];
+ int n;
+};
+
+struct gh_release {
+ char tag[64], name[128], author[64], url[200];
+ char notes[640]; /* the first lines, without HTML's tags */
+ time_t at;
+ int pre, assets;
+};
+
+struct gh_releases {
+ struct gh_release r[GH_ITEMS];
+ int n;
+ int tags; /* none: the repository's tags instead */
+};
+
+struct gh_commit {
+ char sha[8], message[200], author[64], url[160];
+ time_t at;
+};
+
+struct gh_commits {
+ struct gh_commit c[GH_ITEMS];
+ int n, more; /* more: the page was full */
+};
+
+struct gh_repo {
+ char full[128], description[400], language[48], license[64];
+ char branch[64], homepage[160], topics[200], url[160];
+ long stars, forks, watchers, open_issues; /* issues: with the PRs */
+ time_t pushed, created;
+ int archived, private_, fork;
+};
+
+/* One read each, 0 or -1 with the reason in err. */
+int gh_read_issues(const char *text, size_t n, int max, struct gh_issues *v,
+ char *err, size_t err_len);
+int gh_read_releases(const char *text, size_t n, int max, struct gh_releases *v,
+ char *err, size_t err_len);
+int gh_read_tags(const char *text, size_t n, int max, struct gh_releases *v,
+ char *err, size_t err_len);
+int gh_read_commits(const char *text, size_t n, int max, struct gh_commits *v,
+ char *err, size_t err_len);
+int gh_read_repo(const char *text, size_t n, struct gh_repo *r, char *err,
+ size_t err_len);
+/* A search of repositories: the one named name (case aside) with the most
+ stars, else the first; its full name into full. 0, or -1. */
+int gh_read_search_repo(const char *text, size_t n, const char *name,
+ char *full, size_t cap, char *err, size_t err_len);
+/* The user's own repositories (/user/repos): the full name of the one
+ named name into full. 1 found, 0 not, -1 on error. */
+int gh_read_mine(const char *text, size_t n, const char *name, char *full,
+ size_t cap, char *err, size_t err_len);
+
+/* ---- repo.c ---- */
+
+/* The repository the user means: "owner/name", a github.com link, or a
+ name alone - among the user's repositories (with a token), else the
+ one of that name with the most stars. full: "owner/name"; how: "" when
+ given whole, else how it was chosen ("yours", "found by its name").
+ 0, or -1 with the reason in err. */
+int gh_repo_find(const char *q, char *full, size_t cap, char *how,
+ size_t how_cap, char *err, size_t err_len);
+
+/* ---- seen.c ---- */
+
+/* When the user last looked at what of repo (0: never); and now. */
+time_t gh_seen(const char *repo, const char *what);
+void gh_seen_set(const char *repo, const char *what, time_t t);
+
+/* ---- data.c ---- */
+
+void gh_jstr(struct janas_buf *b, const char *key, const char *v);
+/* , "key": {"day", "mon", "year" (0 this year), "at", "ago_d"} */
+void gh_jwhen(struct janas_buf *b, const char *key, time_t t);
+void gh_issues_data(struct janas_buf *b, const char *repo, const char *how,
+ int pr, const char *state, time_t since, int first,
+ const struct gh_issues *v);
+void gh_releases_data(struct janas_buf *b, const char *repo, const char *how,
+ const struct gh_releases *v);
+void gh_commits_data(struct janas_buf *b, const char *repo, const char *how,
+ const char *branch, int days, const struct gh_commits *v);
+void gh_repo_data(struct janas_buf *b, const char *how,
+ const struct gh_repo *r);
+
+/* ---- layouts.c ---- */
+
+extern const char GH_ISSUES_LAYOUT[], GH_ISSUES_BRIEF[];
+extern const char GH_RELEASES_LAYOUT[], GH_RELEASES_BRIEF[];
+extern const char GH_COMMITS_LAYOUT[], GH_COMMITS_BRIEF[];
+extern const char GH_REPO_LAYOUT[], GH_REPO_BRIEF[];
+
+/* ---- tools.c ---- */
+
+void gh_tools_list(void *ctx, struct janas_buf *b);
+int gh_tools_call(void *ctx, const struct janas_json *params,
+ struct janas_buf *b);
+
+#endif
diff --git a/src/github/layouts.c b/src/github/layouts.c
new file mode 100644
index 0000000..15b29fe
--- /dev/null
+++ b/src/github/layouts.c
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * layouts.c - the layouts of janas-github's answers (see github.h and
+ * common/template.h): the text the user reads, filled with the data in
+ * English here and in the user's language by a client that translates the
+ * layout once (janas-chat), and the brief, all the model reads of it.
+ * Titles, messages and notes stay as their authors wrote them.
+ */
+#include "github.h"
+
+#define MON_WORDS \
+ "mon.1 = January\n" \
+ "mon.2 = February\n" \
+ "mon.3 = March\n" \
+ "mon.4 = April\n" \
+ "mon.5 = May\n" \
+ "mon.6 = June\n" \
+ "mon.7 = July\n" \
+ "mon.8 = August\n" \
+ "mon.9 = September\n" \
+ "mon.10 = October\n" \
+ "mon.11 = November\n" \
+ "mon.12 = December\n"
+
+/* How the repository was chosen, when the user named it alone. */
+#define HOW_WORDS \
+ "how.yours = one of yours\n" \
+ "how.found by its name = the one of that name with the most stars\n"
+
+/* A day, with its year when not this one, and the time. */
+#define WHEN(t) \
+ "{{" t ".day}} {{" t ".mon|mon}}{{#" t ".year}} {{" t ".year}}{{/" t \
+ ".year}}, {{" t ".at}}"
+
+/* The same for the model: "2/10, 3 days ago" */
+#define AGO(t) \
+ "{{" t ".day}}/{{" t ".mon}}{{#" t ".year}}/{{" t ".year}}{{/" t \
+ ".year}}, {{" t ".ago_d}} days ago"
+
+#define W_SINCE WHEN("since")
+#define W_CREATED WHEN("created")
+#define W_WHEN WHEN("when")
+#define W_PUSHED WHEN("pushed")
+#define A_SINCE AGO("since")
+#define A_CREATED AGO("created")
+#define A_WHEN AGO("when")
+#define A_PUSHED AGO("pushed")
+
+#define REPO "{{repo}}{{#how}} ({{how|how}}){{/how}}"
+#define REPO_BRIEF \
+ "{{repo}}{{#how}} ({{how}}: say which repository was taken){{/how}}"
+
+const char GH_ISSUES_LAYOUT[] =
+ REPO ": {{#total}}{{total}}{{/total}}{{^total}}no{{/total}} "
+ "{{kind|kind}}{{#new}} since " W_SINCE
+ "{{/new}}{{#first}} (the first time you ask: the last 7 "
+ "days){{/first}}{{#more}}; the latest "
+ "{{shown}}{{/more}}{{#total}}:{{/total}}{{^total}}.{{/total}}\n"
+ "{{#items}}\n"
+ " #{{n}} {{title}}\n"
+ " {{status|status}}, by {{user}}, " W_CREATED
+ "{{#comments}}, comments: {{comments}}{{/comments}}{{#labels}}, "
+ "labels: {{labels}}{{/labels}}\n"
+ " {{url}}\n"
+ "{{/items}}\n"
+ "---\n" MON_WORDS HOW_WORDS "kind.open_issues = open issues\n"
+ "kind.closed_issues = closed issues\n"
+ "kind.all_issues = issues\n"
+ "kind.new_issues = new issues\n"
+ "kind.open_pulls = open pull requests\n"
+ "kind.closed_pulls = closed pull requests\n"
+ "kind.all_pulls = pull requests\n"
+ "kind.new_pulls = new pull requests\n"
+ "status.open = open\n"
+ "status.closed = closed\n"
+ "status.merged = merged\n"
+ "status.draft = draft\n";
+
+const char GH_ISSUES_BRIEF[] = REPO_BRIEF
+ ": {{total}} {{kind}} (of those listed: {{open_n}} open, "
+ "{{closed_n}} closed){{#new}} since " A_SINCE
+ "{{/new}}{{#first}} (asked the first time: the last 7 "
+ "days){{/first}}. {{#items}}#{{n}} {{title}} ({{status}}, " A_CREATED
+ "); {{/items}}The list is shown to the user as it is: do not "
+ "repeat it; answer in a line or two, in the user's language.";
+
+const char GH_RELEASES_LAYOUT[] = REPO
+ ": {{#tags}}no releases; its latest "
+ "tags:{{/tags}}{{^tags}}{{#n}}{{#one}}the latest release:{{/one}}{{^one}}the latest releases:{{/one}}{{/n}}{{^n}}no "
+ "releases and no tags.{{/n}}{{/tags}}\n"
+ "{{#items}}\n"
+ "\n{{tag}}{{#name}} - {{name}}{{/name}}{{#pre}} "
+ "(pre-release){{/pre}}{{#when.day}}, " W_WHEN
+ "{{/when.day}}{{#author}}, by {{author}}{{/author}}{{#assets}}, "
+ "files: {{assets}}{{/assets}}\n"
+ "{{#notes}}{{notes}}\n{{/notes}}"
+ "{{#url}}{{url}}\n{{/url}}"
+ "{{/items}}\n"
+ "---\n" MON_WORDS HOW_WORDS;
+
+const char GH_RELEASES_BRIEF[] = REPO_BRIEF
+ ": {{#tags}}no releases; tags: {{/tags}}{{^n}}no releases and no "
+ "tags. {{/n}}{{#items}}{{tag}}{{#pre}} "
+ "(pre-release){{/pre}}{{#when.day}}, " A_WHEN
+ "{{/when.day}}; {{/items}}The notes are shown to the user: do not "
+ "repeat them; answer in a line or two, in the user's language.";
+
+const char GH_COMMITS_LAYOUT[] =
+ REPO ", branch {{branch}}: {{#n}}{{#more}}the latest {{n}} "
+ "commits{{/more}}{{^more}}{{n}} commits{{/more}} in the last "
+ "{{days}} days:{{/n}}{{^n}}no commits in the last {{days}} "
+ "days.{{/n}}\n"
+ "{{#items}}\n"
+ " {{sha}} {{message}}\n"
+ " {{author}}, " W_WHEN "\n"
+ "{{/items}}\n"
+ "---\n" MON_WORDS HOW_WORDS;
+
+const char GH_COMMITS_BRIEF[] = REPO_BRIEF
+ ", branch {{branch}}: {{#more}}more than {{/more}}{{n}} commits in "
+ "the last {{days}} days. {{#items}}{{message}} ({{author}}, " A_WHEN
+ "); {{/items}}The list is shown to the user as it is: do not "
+ "repeat it; answer in a line or two, in the user's language.";
+
+const char GH_REPO_LAYOUT[] = REPO
+ "{{#archived}} (archived){{/archived}}{{#fork}} (a "
+ "fork){{/fork}}{{#private}} (private){{/private}}\n"
+ "{{#description}}{{description}}\n{{/description}}"
+ "\nstars: {{stars}}, forks: {{forks}}, watching: {{watchers}}; "
+ "open issues and pull requests: {{open_issues}}\n"
+ "{{#language}}language: {{language}}\n{{/language}}"
+ "{{#license}}licence: {{license}}\n{{/license}}"
+ "main branch: {{branch}}; last push: " W_PUSHED "; created: " W_CREATED "\n"
+ "{{#topics}}topics: {{topics}}\n{{/topics}}"
+ "{{#homepage}}site: {{homepage}}\n{{/homepage}}"
+ "{{url}}\n"
+ "---\n" MON_WORDS HOW_WORDS;
+
+const char GH_REPO_BRIEF[] = REPO_BRIEF
+ "{{#archived}} (archived){{/archived}}: {{description}}; {{stars}} "
+ "stars, {{forks}} forks, {{open_issues}} open issues and pull "
+ "requests, {{language}}, {{license}}, last push " A_PUSHED
+ ". Shown to the user: answer in a line or two, in the user's "
+ "language.";
diff --git a/src/github/main.c b/src/github/main.c
new file mode 100644
index 0000000..4194b20
--- /dev/null
+++ b/src/github/main.c
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * main.c - janas-github: GitHub as an MCP server over stdio, for janas-chat
+ * (which starts it by itself) and any other MCP client. It only reads: a
+ * repository's issues and pull requests, its releases, its recent commits,
+ * what it is. GitHub's REST API, with the gh command's token when there is
+ * one.
+ */
+#include <stdio.h>
+#include <string.h>
+
+#include "common/mcp_server.h"
+#include "github.h"
+
+#ifndef JANAS_VERSION
+#define JANAS_VERSION "unknown"
+#endif
+
+static const char INSTRUCTIONS[] =
+ "Use these tools by yourself when the user asks about a project on "
+ "GitHub: its issues or pull requests (new ones, open ones), its latest "
+ "release, its recent commits, what it is; never name them. A project "
+ "named alone (\"Janas\") is looked up by its name: tell the user which "
+ "repository it was. They only read: nothing on GitHub is changed.";
+
+static void usage(void)
+{
+ fprintf(stderr,
+ "usage: janas-github\n"
+ "An MCP server over stdio: a client starts it and calls its\n"
+ "tools - github_issues (issues and pull requests, the new ones\n"
+ "since the last time asked), github_releases, github_commits\n"
+ "and github_repo. It only reads.\n"
+ "GitHub's REST API, with a token from GH_TOKEN, GITHUB_TOKEN\n"
+ "or the gh command (gh auth token) when there is one: 5,000\n"
+ "requests an hour and the user's private repositories; without\n"
+ "one, 60 an hour. The token stays in memory, never written.\n"
+ "When the issues were last seen: ~/.config/janas/"
+ "github-seen.txt.\n"
+ "janas-chat starts it by itself when it is beside it.\n"
+ "Configured in a client as, for instance:\n"
+ " {\"mcpServers\": {\"github\": {\"command\": "
+ "\"janas-github\"}}}\n");
+}
+
+int main(int argc, char **argv)
+{
+ for (int i = 1; i < argc; i++) {
+ usage();
+ return strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0 ? 0
+ : 2;
+ }
+ struct janas_mcps srv = {.name = "janas-github",
+ .version = JANAS_VERSION,
+ .instructions = INSTRUCTIONS,
+ .tools_list = gh_tools_list,
+ .tools_call = gh_tools_call};
+ if (janas_mcps_open(&srv) != 0) {
+ fprintf(stderr, "janas-github: cannot set its output aside\n");
+ return 1;
+ }
+ int k = gh_token_kind();
+ fprintf(stderr,
+ "janas-github %s: ready, 4 tools (github_issues, "
+ "github_releases, github_commits, github_repo), %s\n",
+ JANAS_VERSION,
+ k == 2 ? "with the gh command's token"
+ : k == 1 ? "with the token of GH_TOKEN or GITHUB_TOKEN"
+ : "without a token (60 requests an hour)");
+ janas_mcps_run(&srv);
+ gh_fetch_free();
+ return 0;
+}
diff --git a/src/github/read.c b/src/github/read.c
new file mode 100644
index 0000000..d99fb6f
--- /dev/null
+++ b/src/github/read.c
@@ -0,0 +1,351 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * read.c - janas-github reads GitHub's answers (see github.h): issues and
+ * pull requests as the search gives them, releases, tags, commits, a
+ * repository, a search of repositories. No network here, so that the
+ * tests can read answers kept as they came.
+ */
+#define _GNU_SOURCE /* timegm, strcasecmp */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <time.h>
+
+#include "github.h"
+
+static void copy(char *to, size_t cap, const char *s)
+{
+ snprintf(to, cap, "%s", s ? s : "");
+}
+
+static const char *str(const struct janas_json *o, const char *key)
+{
+ const char *s = janas_json_str(janas_json_get(o, key));
+ return s && *s ? s : NULL;
+}
+
+static long num(const struct janas_json *o, const char *key)
+{
+ return (long)janas_json_num(janas_json_get(o, key), 0);
+}
+
+static int truth(const struct janas_json *o, const char *key)
+{
+ const struct janas_json *v = janas_json_get(o, key);
+ return v && v->type == JANAS_JSON_TRUE;
+}
+
+/* The first line of a text, cut to cap */
+static void first_line(char *to, size_t cap, const char *s)
+{
+ s = s ? s : "";
+ size_t n = strcspn(s, "\r\n");
+ snprintf(to, cap, "%.*s", (int)(n < cap ? n : cap - 1), s);
+}
+
+static struct janas_json_doc *parse(const char *text, size_t n, char *err,
+ size_t err_len)
+{
+ char why[128];
+ struct janas_json_doc *d = janas_json_parse(text, n, why, sizeof why);
+ if (!d)
+ snprintf(err, err_len, "GitHub: an answer it could not read (%s)", why);
+ return d;
+}
+
+static const struct janas_json *array(struct janas_json_doc *d, char *err,
+ size_t err_len)
+{
+ const struct janas_json *a = janas_json_root(d);
+ if (a && a->type == JANAS_JSON_ARRAY)
+ return a;
+ const char *m = str(a, "message");
+ snprintf(err, err_len, "GitHub: %s", m ? m : "not a list");
+ return NULL;
+}
+
+time_t gh_utc(const char *s)
+{
+ struct tm tm = {0};
+ if (!s || sscanf(s, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
+ &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) < 5)
+ return -1;
+ tm.tm_year -= 1900;
+ tm.tm_mon -= 1;
+ return timegm(&tm);
+}
+
+int gh_read_issues(const char *text, size_t n, int max, struct gh_issues *v,
+ char *err, size_t err_len)
+{
+ memset(v, 0, sizeof *v);
+ struct janas_json_doc *d = parse(text, n, err, err_len);
+ if (!d)
+ return -1;
+ const struct janas_json *r = janas_json_root(d);
+ const struct janas_json *items = janas_json_get(r, "items");
+ if (!items || items->type != JANAS_JSON_ARRAY) {
+ const char *m = str(r, "message");
+ snprintf(err, err_len, "GitHub: %s", m ? m : "no items");
+ janas_json_free(d);
+ return -1;
+ }
+ v->total = num(r, "total_count");
+ if (max > GH_ITEMS)
+ max = GH_ITEMS;
+ for (const struct janas_json *it = items->child; it && v->n < max;
+ it = it->next) {
+ struct gh_issue *x = &v->i[v->n++];
+ x->number = (int)num(it, "number");
+ copy(x->title, sizeof x->title, str(it, "title"));
+ copy(x->user, sizeof x->user, str(janas_json_get(it, "user"), "login"));
+ copy(x->url, sizeof x->url, str(it, "html_url"));
+ x->created = gh_utc(str(it, "created_at"));
+ x->closed = gh_utc(str(it, "closed_at"));
+ x->comments = (int)num(it, "comments");
+ x->open = janas_json_is(janas_json_get(it, "state"), "open");
+ const struct janas_json *pr = janas_json_get(it, "pull_request");
+ x->pr = pr != NULL;
+ x->merged = pr && str(pr, "merged_at") != NULL;
+ x->draft = truth(it, "draft");
+ size_t k = 0;
+ const struct janas_json *ls = janas_json_get(it, "labels");
+ for (const struct janas_json *l = ls ? ls->child : NULL; l;
+ l = l->next) {
+ const char *name = str(l, "name");
+ if (name && k < sizeof x->labels)
+ k += (size_t)snprintf(x->labels + k, sizeof x->labels - k,
+ "%s%s", k ? ", " : "", name);
+ }
+ }
+ janas_json_free(d);
+ return 0;
+}
+
+/* Release notes as the user reads them in a terminal: the lines without
+ HTML's tags (llama.cpp's open with <details>) and without the empty
+ ones, up to cap at a line's end. */
+static void notes(char *to, size_t cap, const char *s)
+{
+ size_t k = 0;
+ to[0] = 0;
+ while (s && *s && k + 1 < cap) {
+ size_t n = strcspn(s, "\r\n");
+ char line[512];
+ size_t m = 0;
+ int in_tag = 0, in_link = 0;
+ for (size_t i = 0; i < n && m + 1 < sizeof line; i++) {
+ if (s[i] == '<' && i + 5 < n && strncmp(s + i + 1, "http", 4) == 0)
+ in_link = 1; /* <https://...>: a link, kept */
+ else if (in_link && s[i] == '>')
+ in_link = 0;
+ else if (s[i] == '<' && i + 1 < n &&
+ (s[i + 1] == '/' || (s[i + 1] >= 'a' && s[i + 1] <= 'z')))
+ in_tag = 1;
+ else if (in_tag && s[i] == '>')
+ in_tag = 0;
+ else if (!in_tag)
+ line[m++] = s[i];
+ }
+ while (m && line[m - 1] == ' ')
+ m--;
+ line[m] = 0;
+ if (m) {
+ if (k + m + 2 > cap) {
+ if (k + 5 < cap)
+ k += (size_t)snprintf(to + k, cap - k, "...\n");
+ break;
+ }
+ k += (size_t)snprintf(to + k, cap - k, "%s\n", line);
+ }
+ s += n;
+ while (*s == '\r' || *s == '\n')
+ s++;
+ }
+ while (k && to[k - 1] == '\n')
+ to[--k] = 0;
+}
+
+int gh_read_releases(const char *text, size_t n, int max, struct gh_releases *v,
+ char *err, size_t err_len)
+{
+ memset(v, 0, sizeof *v);
+ struct janas_json_doc *d = parse(text, n, err, err_len);
+ if (!d)
+ return -1;
+ const struct janas_json *a = array(d, err, err_len);
+ if (max > GH_ITEMS)
+ max = GH_ITEMS;
+ for (const struct janas_json *it = a ? a->child : NULL; it && v->n < max;
+ it = it->next) {
+ if (truth(it, "draft"))
+ continue;
+ struct gh_release *x = &v->r[v->n++];
+ copy(x->tag, sizeof x->tag, str(it, "tag_name"));
+ const char *name = str(it, "name");
+ if (name && strcmp(name, x->tag) != 0)
+ copy(x->name, sizeof x->name, name);
+ copy(x->author, sizeof x->author,
+ str(janas_json_get(it, "author"), "login"));
+ copy(x->url, sizeof x->url, str(it, "html_url"));
+ notes(x->notes, sizeof x->notes, str(it, "body"));
+ x->at = gh_utc(str(it, "published_at"));
+ x->pre = truth(it, "prerelease");
+ const struct janas_json *as = janas_json_get(it, "assets");
+ x->assets = as && as->type == JANAS_JSON_ARRAY ? (int)as->n : 0;
+ }
+ janas_json_free(d);
+ return a ? 0 : -1;
+}
+
+int gh_read_tags(const char *text, size_t n, int max, struct gh_releases *v,
+ char *err, size_t err_len)
+{
+ memset(v, 0, sizeof *v);
+ v->tags = 1;
+ struct janas_json_doc *d = parse(text, n, err, err_len);
+ if (!d)
+ return -1;
+ const struct janas_json *a = array(d, err, err_len);
+ if (max > GH_ITEMS)
+ max = GH_ITEMS;
+ for (const struct janas_json *it = a ? a->child : NULL; it && v->n < max;
+ it = it->next) {
+ struct gh_release *x = &v->r[v->n++];
+ copy(x->tag, sizeof x->tag, str(it, "name"));
+ x->at = -1; /* a tag's answer has no date */
+ }
+ janas_json_free(d);
+ return a ? 0 : -1;
+}
+
+int gh_read_commits(const char *text, size_t n, int max, struct gh_commits *v,
+ char *err, size_t err_len)
+{
+ memset(v, 0, sizeof *v);
+ struct janas_json_doc *d = parse(text, n, err, err_len);
+ if (!d)
+ return -1;
+ const struct janas_json *a = array(d, err, err_len);
+ if (max > GH_ITEMS)
+ max = GH_ITEMS;
+ int all = 0;
+ for (const struct janas_json *it = a ? a->child : NULL; it; it = it->next) {
+ all++;
+ if (v->n >= max)
+ continue;
+ struct gh_commit *x = &v->c[v->n++];
+ copy(x->sha, sizeof x->sha, str(it, "sha"));
+ const struct janas_json *c = janas_json_get(it, "commit");
+ first_line(x->message, sizeof x->message, str(c, "message"));
+ const struct janas_json *au = janas_json_get(c, "author");
+ copy(x->author, sizeof x->author, str(au, "name"));
+ x->at = gh_utc(str(au, "date"));
+ copy(x->url, sizeof x->url, str(it, "html_url"));
+ }
+ v->more = all > v->n;
+ janas_json_free(d);
+ return a ? 0 : -1;
+}
+
+int gh_read_repo(const char *text, size_t n, struct gh_repo *r, char *err,
+ size_t err_len)
+{
+ memset(r, 0, sizeof *r);
+ struct janas_json_doc *d = parse(text, n, err, err_len);
+ if (!d)
+ return -1;
+ const struct janas_json *o = janas_json_root(d);
+ if (!str(o, "full_name")) {
+ const char *m = str(o, "message");
+ snprintf(err, err_len, "GitHub: %s", m ? m : "not a repository");
+ janas_json_free(d);
+ return -1;
+ }
+ copy(r->full, sizeof r->full, str(o, "full_name"));
+ copy(r->description, sizeof r->description, str(o, "description"));
+ copy(r->language, sizeof r->language, str(o, "language"));
+ const struct janas_json *l = janas_json_get(o, "license");
+ const char *spdx = str(l, "spdx_id");
+ copy(r->license, sizeof r->license,
+ spdx && strcmp(spdx, "NOASSERTION") != 0 ? spdx : str(l, "name"));
+ copy(r->branch, sizeof r->branch, str(o, "default_branch"));
+ copy(r->homepage, sizeof r->homepage, str(o, "homepage"));
+ copy(r->url, sizeof r->url, str(o, "html_url"));
+ size_t k = 0;
+ const struct janas_json *t = janas_json_get(o, "topics");
+ for (const struct janas_json *x = t ? t->child : NULL; x; x = x->next) {
+ const char *s = janas_json_str(x);
+ if (s && k < sizeof r->topics)
+ k += (size_t)snprintf(r->topics + k, sizeof r->topics - k, "%s%s",
+ k ? ", " : "", s);
+ }
+ r->stars = num(o, "stargazers_count");
+ r->forks = num(o, "forks_count");
+ r->watchers = num(o, "subscribers_count");
+ r->open_issues = num(o, "open_issues_count");
+ r->pushed = gh_utc(str(o, "pushed_at"));
+ r->created = gh_utc(str(o, "created_at"));
+ r->archived = truth(o, "archived");
+ r->private_ = truth(o, "private");
+ r->fork = truth(o, "fork");
+ janas_json_free(d);
+ return 0;
+}
+
+/* "name", or "owner/name" whose name part is name */
+static int named(const char *full, const char *name)
+{
+ const char *slash = full ? strrchr(full, '/') : NULL;
+ return slash && strcasecmp(slash + 1, name) == 0;
+}
+
+int gh_read_search_repo(const char *text, size_t n, const char *name,
+ char *full, size_t cap, char *err, size_t err_len)
+{
+ struct janas_json_doc *d = parse(text, n, err, err_len);
+ if (!d)
+ return -1;
+ const struct janas_json *items =
+ janas_json_get(janas_json_root(d), "items");
+ const char *best = NULL;
+ long stars = -1;
+ for (const struct janas_json *it = items ? items->child : NULL; it;
+ it = it->next) {
+ const char *f = str(it, "full_name");
+ if (f && named(f, name) && num(it, "stargazers_count") > stars) {
+ best = f;
+ stars = num(it, "stargazers_count");
+ }
+ }
+ if (!best && items && items->child)
+ best = str(items->child, "full_name");
+ if (best)
+ copy(full, cap, best);
+ else
+ snprintf(err, err_len, "GitHub has no repository named %s", name);
+ janas_json_free(d);
+ return best ? 0 : -1;
+}
+
+int gh_read_mine(const char *text, size_t n, const char *name, char *full,
+ size_t cap, char *err, size_t err_len)
+{
+ struct janas_json_doc *d = parse(text, n, err, err_len);
+ if (!d)
+ return -1;
+ const struct janas_json *a = array(d, err, err_len);
+ int found = 0;
+ for (const struct janas_json *it = a ? a->child : NULL; it && !found;
+ it = it->next) {
+ const char *f = str(it, "full_name");
+ if (f && named(f, name)) {
+ copy(full, cap, f);
+ found = 1;
+ }
+ }
+ janas_json_free(d);
+ return a ? found : -1;
+}
diff --git a/src/github/repo.c b/src/github/repo.c
new file mode 100644
index 0000000..8249853
--- /dev/null
+++ b/src/github/repo.c
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * repo.c - the repository the user means (see github.h): given whole
+ * ("owner/name", a link), or by its name alone, first among the user's own
+ * (with a token), then the one of that name with the most stars - the
+ * answer says which it took.
+ */
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "github.h"
+
+/* A name GitHub allows: letters, digits, "-", "_", "." */
+static int plain(const char *s, size_t n)
+{
+ if (!n || n > 100)
+ return 0;
+ for (size_t i = 0; i < n; i++)
+ if (!isalnum((unsigned char)s[i]) && !strchr("-_.", s[i]))
+ return 0;
+ return 1;
+}
+
+int gh_repo_find(const char *q, char *full, size_t cap, char *how,
+ size_t how_cap, char *err, size_t err_len)
+{
+ how[0] = 0;
+ while (q && isspace((unsigned char)*q))
+ q++;
+ if (!q || !*q) {
+ snprintf(err, err_len, "No repository given");
+ return -1;
+ }
+ /* a link: github.com/owner/name, whatever follows */
+ const char *g = strstr(q, "github.com/");
+ if (g)
+ q = g + 11;
+ const char *slash = strchr(q, '/');
+ if (slash) {
+ size_t on = (size_t)(slash - q);
+ const char *name = slash + 1;
+ size_t nn = strcspn(name, "/?# \t");
+ if (nn > 4 && strncmp(name + nn - 4, ".git", 4) == 0)
+ nn -= 4;
+ if (plain(q, on) && plain(name, nn)) {
+ snprintf(full, cap, "%.*s/%.*s", (int)on, q, (int)nn, name);
+ return 0;
+ }
+ snprintf(err, err_len, "\"%s\" is not a repository's name", q);
+ return -1;
+ }
+ size_t n = strlen(q);
+ while (n && isspace((unsigned char)q[n - 1]))
+ n--;
+ char name[128];
+ snprintf(name, sizeof name, "%.*s", (int)(n < 127 ? n : 127), q);
+ struct janas_buf b = {0};
+ if (gh_token_kind() && plain(name, strlen(name)) &&
+ gh_get("/user/repos?per_page=100&sort=pushed", &b, err, err_len) ==
+ 200 &&
+ gh_read_mine(b.p, b.n, name, full, cap, err, err_len) == 1) {
+ janas_buf_free(&b);
+ snprintf(how, how_cap, "yours");
+ return 0;
+ }
+ janas_buf_free(&b);
+ struct janas_buf path = {0};
+ janas_buf_puts(&path, "/search/repositories?q=");
+ gh_url_put(&path, name);
+ janas_buf_puts(&path, "+in:name&sort=stars&per_page=10");
+ int st = path.oom ? -1 : gh_get(path.p, &b, err, err_len);
+ janas_buf_free(&path);
+ int r = st == 200
+ ? gh_read_search_repo(b.p, b.n, name, full, cap, err, err_len)
+ : -1;
+ janas_buf_free(&b);
+ if (r == 0)
+ snprintf(how, how_cap, "found by its name");
+ return r;
+}
diff --git a/src/github/seen.c b/src/github/seen.c
new file mode 100644
index 0000000..a87f895
--- /dev/null
+++ b/src/github/seen.c
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * seen.c - when the user last looked at a repository's issues or pull
+ * requests (see github.h), so that "are there new issues?" has an answer:
+ * a line a repository in ~/.config/janas/github-seen.txt, "owner/name what
+ * time". Nothing but these lines is kept.
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include "github.h"
+
+#define LINES_MAX 256
+
+static int path_of(char *buf, size_t cap)
+{
+ const char *xdg = getenv("XDG_CONFIG_HOME"), *home = getenv("HOME");
+ int n = xdg && *xdg ? snprintf(buf, cap, "%s/janas", xdg)
+ : home ? snprintf(buf, cap, "%s/.config/janas", home)
+ : -1;
+ if (n < 0 || (size_t)n + 20 >= cap)
+ return -1;
+ if (mkdir(buf, 0700) != 0 && errno != EEXIST)
+ return -1;
+ strcat(buf, "/github-seen.txt");
+ return 0;
+}
+
+time_t gh_seen(const char *repo, const char *what)
+{
+ char path[1024], line[512], r[256], w[32];
+ long long t;
+ time_t at = 0;
+ if (path_of(path, sizeof path) != 0)
+ return 0;
+ FILE *f = fopen(path, "r");
+ if (!f)
+ return 0;
+ while (fgets(line, sizeof line, f))
+ if (sscanf(line, "%255s %31s %lld", r, w, &t) == 3 &&
+ strcmp(r, repo) == 0 && strcmp(w, what) == 0)
+ at = (time_t)t;
+ fclose(f);
+ return at;
+}
+
+void gh_seen_set(const char *repo, const char *what, time_t t)
+{
+ char path[1024], tmp[1100], line[512], r[256], w[32];
+ long long old;
+ if (path_of(path, sizeof path) != 0)
+ return;
+ snprintf(tmp, sizeof tmp, "%s.new", path);
+ FILE *in = fopen(path, "r"), *out = fopen(tmp, "w");
+ if (!out) {
+ if (in)
+ fclose(in);
+ return;
+ }
+ /* the others' lines as they were, the newest last (LINES_MAX of
+ them: the oldest go) */
+ char *keep[LINES_MAX];
+ int n = 0;
+ while (in && fgets(line, sizeof line, in)) {
+ if (sscanf(line, "%255s %31s %lld", r, w, &old) != 3 ||
+ (strcmp(r, repo) == 0 && strcmp(w, what) == 0))
+ continue;
+ if (n == LINES_MAX - 1) {
+ free(keep[0]);
+ memmove(keep, keep + 1, (size_t)(n - 1) * sizeof *keep);
+ n--;
+ }
+ if ((keep[n] = strdup(line)))
+ n++;
+ }
+ if (in)
+ fclose(in);
+ for (int i = 0; i < n; i++) {
+ fputs(keep[i], out);
+ free(keep[i]);
+ }
+ fprintf(out, "%s %s %lld\n", repo, what, (long long)t);
+ if (fclose(out) == 0)
+ rename(tmp, path);
+ else
+ remove(tmp);
+}
diff --git a/src/github/tools.c b/src/github/tools.c
new file mode 100644
index 0000000..af0bf84
--- /dev/null
+++ b/src/github/tools.c
@@ -0,0 +1,292 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * tools.c - janas-github's tools (see github.h): github_issues,
+ * github_releases, github_commits and github_repo. They only read, and say
+ * so (readOnlyHint): a client may run them without asking.
+ */
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include "common/mcp_server.h"
+#include "common/template.h"
+#include "github.h"
+
+#define DAY 86400
+#define FIRST_DAYS 7 /* "new", asked the first time: the last week */
+
+#define REPO_ARG \
+ "\"repo\": {\"type\": \"string\", \"description\": \"owner/name, a " \
+ "github.com link, or a name alone (\\\"Janas\\\", \\\"llama.cpp\\\"): " \
+ "the user's own first, else the one with the most stars\"}"
+
+#define READ_ONLY \
+ "\"annotations\": {\"readOnlyHint\": true, \"openWorldHint\": true}"
+
+void gh_tools_list(void *ctx, struct janas_buf *b)
+{
+ (void)ctx;
+ janas_buf_puts(
+ b,
+ "\"tools\": [{\"name\": \"github_issues\", \"title\": \"Issues and pull "
+ "requests\", \"description\": \"A repository's issues or pull "
+ "requests, the newest first, with how many there are.\", "
+ "\"inputSchema\": {\"type\": \"object\", \"properties\": "
+ "{" REPO_ARG ", \"kind\": {\"type\": \"string\", \"enum\": "
+ "[\"issues\", \"pulls\"]}, \"state\": {\"type\": \"string\", "
+ "\"enum\": [\"open\", \"closed\", \"all\"]}, \"new\": {\"type\": "
+ "\"boolean\", \"description\": \"only those opened since the user "
+ "last asked (\\\"are there new issues?\\\")\"}, \"days\": "
+ "{\"type\": \"number\", \"description\": \"only those opened in "
+ "the last days\"}}, \"required\": [\"repo\"], "
+ "\"additionalProperties\": false}, " READ_ONLY "}, "
+ "{\"name\": \"github_releases\", \"title\": \"Releases\", "
+ "\"description\": \"A repository's latest releases (its tags when "
+ "it has none), with their notes.\", \"inputSchema\": {\"type\": "
+ "\"object\", \"properties\": {" REPO_ARG ", \"count\": {\"type\": "
+ "\"number\", \"description\": \"1 for the latest; at most 10\"}}, "
+ "\"required\": [\"repo\"], \"additionalProperties\": false}, "
+ "" READ_ONLY "}, "
+ "{\"name\": \"github_commits\", \"title\": \"Recent commits\", "
+ "\"description\": \"A branch's commits of the last days, the "
+ "newest first.\", \"inputSchema\": {\"type\": \"object\", "
+ "\"properties\": {" REPO_ARG ", \"branch\": {\"type\": "
+ "\"string\", \"description\": \"leave it out for the main "
+ "one\"}, \"days\": {\"type\": \"number\", \"description\": "
+ "\"default 7\"}, \"author\": {\"type\": \"string\", "
+ "\"description\": \"a GitHub login or an e-mail\"}}, "
+ "\"required\": [\"repo\"], \"additionalProperties\": false}, "
+ "" READ_ONLY "}, "
+ "{\"name\": \"github_repo\", \"title\": \"A repository\", "
+ "\"description\": \"What a repository is: description, stars, "
+ "language, licence, last push.\", \"inputSchema\": {\"type\": "
+ "\"object\", \"properties\": {" REPO_ARG "}, \"required\": "
+ "[\"repo\"], \"additionalProperties\": false}, " READ_ONLY "}]");
+}
+
+static const char *arg_str(const struct janas_json *args, const char *name)
+{
+ const char *s = janas_json_str(janas_json_get(args, name));
+ return s && *s ? s : NULL;
+}
+
+static double arg_num(const struct janas_json *args, const char *name,
+ double def)
+{
+ const struct janas_json *v = janas_json_get(args, name);
+ return v && v->type == JANAS_JSON_NUMBER ? janas_json_num(v, def) : def;
+}
+
+static int arg_bool(const struct janas_json *args, const char *name)
+{
+ const struct janas_json *v = janas_json_get(args, name);
+ return v && v->type == JANAS_JSON_TRUE;
+}
+
+static int fail(struct janas_buf *b, const char *fmt, const char *a,
+ const char *c)
+{
+ struct janas_buf out = {0};
+ janas_buf_printf(&out, fmt, a, c);
+ janas_mcps_text_result(b, out.p ? out.p : "", out.n, 1);
+ janas_buf_free(&out);
+ return 0;
+}
+
+static int answer(struct janas_buf *b, const char *name, struct janas_buf *d,
+ const char *layout, const char *brief)
+{
+ char err[300];
+ int oom = d->oom;
+ int r = oom ? -1
+ : janas_tpl_result(b, name, d->p, d->n, layout, brief, err,
+ sizeof err);
+ janas_buf_free(d);
+ if (r != 0)
+ return fail(b, "The answer could not be written: %s%s.",
+ oom ? "out of memory" : err, "");
+ return 0;
+}
+
+/* The repository of the arguments: 0, or -1 with the error written. */
+static int repo_of(const struct janas_json *args, char *full, size_t cap,
+ char *how, size_t how_cap, struct janas_buf *b)
+{
+ char err[400];
+ if (gh_repo_find(arg_str(args, "repo"), full, cap, how, how_cap, err,
+ sizeof err) == 0)
+ return 0;
+ fail(b, "%s. Ask the user which repository (owner/name)%s.", err, "");
+ return -1;
+}
+
+/* "2026-09-25T10:00:00Z" */
+static void iso(char *out, size_t cap, time_t t)
+{
+ struct tm g;
+ gmtime_r(&t, &g);
+ strftime(out, cap, "%Y-%m-%dT%H:%M:%SZ", &g);
+}
+
+static int tool_issues(const struct janas_json *args, struct janas_buf *b)
+{
+ char repo[160], how[48], err[400];
+ if (repo_of(args, repo, sizeof repo, how, sizeof how, b) != 0)
+ return 0;
+ int pr = janas_json_is(janas_json_get(args, "kind"), "pulls");
+ const char *state = arg_str(args, "state");
+ if (!state || (strcmp(state, "closed") && strcmp(state, "all")))
+ state = "open";
+ int fresh = arg_bool(args, "new"), first = 0;
+ double days = arg_num(args, "days", 0);
+ time_t now = time(NULL), since = 0;
+ const char *what = pr ? "pulls" : "issues";
+ if (fresh) {
+ since = gh_seen(repo, what);
+ if (!since) {
+ first = 1;
+ since = now - FIRST_DAYS * DAY;
+ }
+ state = "all"; /* opened since then, closed or not */
+ } else if (days > 0) {
+ since = now - (time_t)(days * DAY);
+ }
+ struct janas_buf q = {0}, path = {0}, body = {0};
+ janas_buf_printf(&q, "repo:%s is:%s", repo, pr ? "pr" : "issue");
+ if (strcmp(state, "all") != 0)
+ janas_buf_printf(&q, " is:%s", state);
+ if (since) {
+ char t[32];
+ iso(t, sizeof t, since);
+ janas_buf_printf(&q, " created:>=%s", t);
+ }
+ janas_buf_puts(&path, "/search/issues?q=");
+ if (!q.oom)
+ gh_url_put(&path, q.p);
+ janas_buf_printf(&path, "&sort=created&order=desc&per_page=%d", GH_ITEMS);
+ janas_buf_free(&q);
+ int st = path.oom ? -1 : gh_get(path.p, &body, err, sizeof err);
+ janas_buf_free(&path);
+ struct gh_issues v;
+ int ok = st == 200 &&
+ gh_read_issues(body.p, body.n, GH_ITEMS, &v, err, sizeof err) == 0;
+ janas_buf_free(&body);
+ if (!ok)
+ return fail(b, "The issues of %s could not be read: %s.", repo, err);
+ if (fresh)
+ gh_seen_set(repo, what, now);
+ struct janas_buf d = {0};
+ gh_issues_data(&d, repo, how, pr, state, fresh ? since : 0, first, &v);
+ return answer(b, "github_issues", &d, GH_ISSUES_LAYOUT, GH_ISSUES_BRIEF);
+}
+
+static int tool_releases(const struct janas_json *args, struct janas_buf *b)
+{
+ char repo[160], how[48], err[400], path[256];
+ if (repo_of(args, repo, sizeof repo, how, sizeof how, b) != 0)
+ return 0;
+ int count = (int)arg_num(args, "count", 3);
+ count = count < 1 ? 1 : count > GH_ITEMS ? GH_ITEMS : count;
+ struct janas_buf body = {0};
+ struct gh_releases v;
+ /* a few more than asked: drafts are left out */
+ snprintf(path, sizeof path, "/repos/%s/releases?per_page=%d", repo,
+ count + 2);
+ int ok = gh_get(path, &body, err, sizeof err) == 200 &&
+ gh_read_releases(body.p, body.n, count, &v, err, sizeof err) == 0;
+ if (ok && v.n == 0) { /* no releases: the tags */
+ snprintf(path, sizeof path, "/repos/%s/tags?per_page=%d", repo, count);
+ ok = gh_get(path, &body, err, sizeof err) == 200 &&
+ gh_read_tags(body.p, body.n, count, &v, err, sizeof err) == 0;
+ if (ok && v.n == 0)
+ v.tags = 0;
+ }
+ janas_buf_free(&body);
+ if (!ok)
+ return fail(b, "The releases of %s could not be read: %s.", repo, err);
+ struct janas_buf d = {0};
+ gh_releases_data(&d, repo, how, &v);
+ return answer(b, "github_releases", &d, GH_RELEASES_LAYOUT,
+ GH_RELEASES_BRIEF);
+}
+
+static int tool_commits(const struct janas_json *args, struct janas_buf *b)
+{
+ char repo[160], how[48], err[400], path[256];
+ if (repo_of(args, repo, sizeof repo, how, sizeof how, b) != 0)
+ return 0;
+ struct janas_buf body = {0}, p = {0};
+ const char *branch = arg_str(args, "branch");
+ struct gh_repo r;
+ if (!branch) { /* the main one, by its name */
+ snprintf(path, sizeof path, "/repos/%s", repo);
+ if (gh_get(path, &body, err, sizeof err) != 200 ||
+ gh_read_repo(body.p, body.n, &r, err, sizeof err) != 0) {
+ janas_buf_free(&body);
+ return fail(b, "%s could not be read: %s.", repo, err);
+ }
+ branch = r.branch;
+ }
+ int days = (int)arg_num(args, "days", 7);
+ days = days < 1 ? 1 : days > 365 ? 365 : days;
+ char since[32];
+ iso(since, sizeof since, time(NULL) - (time_t)days * DAY);
+ /* one more than shown: whether there are more */
+ janas_buf_printf(&p, "/repos/%s/commits?per_page=%d&since=%s&sha=", repo,
+ GH_ITEMS + 1, since);
+ gh_url_put(&p, branch);
+ const char *author = arg_str(args, "author");
+ if (author) {
+ janas_buf_puts(&p, "&author=");
+ gh_url_put(&p, author);
+ }
+ struct gh_commits v;
+ int ok =
+ !p.oom && gh_get(p.p, &body, err, sizeof err) == 200 &&
+ gh_read_commits(body.p, body.n, GH_ITEMS, &v, err, sizeof err) == 0;
+ janas_buf_free(&p);
+ janas_buf_free(&body);
+ if (!ok)
+ return fail(b, "The commits of %s could not be read: %s.", repo, err);
+ struct janas_buf d = {0};
+ gh_commits_data(&d, repo, how, branch, days, &v);
+ return answer(b, "github_commits", &d, GH_COMMITS_LAYOUT, GH_COMMITS_BRIEF);
+}
+
+static int tool_repo(const struct janas_json *args, struct janas_buf *b)
+{
+ char repo[160], how[48], err[400], path[256];
+ if (repo_of(args, repo, sizeof repo, how, sizeof how, b) != 0)
+ return 0;
+ struct janas_buf body = {0};
+ struct gh_repo r;
+ snprintf(path, sizeof path, "/repos/%s", repo);
+ int ok = gh_get(path, &body, err, sizeof err) == 200 &&
+ gh_read_repo(body.p, body.n, &r, err, sizeof err) == 0;
+ janas_buf_free(&body);
+ if (!ok)
+ return fail(b, "%s could not be read: %s.", repo, err);
+ struct janas_buf d = {0};
+ gh_repo_data(&d, how, &r);
+ return answer(b, "github_repo", &d, GH_REPO_LAYOUT, GH_REPO_BRIEF);
+}
+
+int gh_tools_call(void *ctx, const struct janas_json *params,
+ struct janas_buf *b)
+{
+ (void)ctx;
+ const struct janas_json *name = janas_json_get(params, "name");
+ const struct janas_json *args = janas_json_get(params, "arguments");
+ if (args && args->type != JANAS_JSON_OBJECT)
+ args = NULL;
+ if (janas_json_is(name, "github_issues"))
+ return tool_issues(args, b);
+ if (janas_json_is(name, "github_releases"))
+ return tool_releases(args, b);
+ if (janas_json_is(name, "github_commits"))
+ return tool_commits(args, b);
+ if (janas_json_is(name, "github_repo"))
+ return tool_repo(args, b);
+ return -1;
+}
diff --git a/tests/test_git_parse.c b/tests/test_git_parse.c
new file mode 100644
index 0000000..f4c378a
--- /dev/null
+++ b/tests/test_git_parse.c
@@ -0,0 +1,216 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * test_git_parse.c - janas-git's reading of git's output, without running
+ * git, and the layouts filled with what was read: a status with a file
+ * changed, one renamed and staged, one new and one in conflict (that line
+ * written from git's documentation), a log, the branches and a numstat
+ * with a rename, as git 2.47 gave them on 2 October 2026 in a repository
+ * made for the purpose.
+ * The sources belong to the program, so they are compiled in here.
+ */
+#define _GNU_SOURCE /* run.c's pipe2 */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "common/template.h"
+#include "git/data.c"
+#include "git/layouts.c"
+#include "git/read.c"
+#include "git/repo.c"
+#include "git/run.c"
+
+static int failures;
+
+#define CHECK(c, ...) \
+ do { \
+ if (!(c)) { \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ failures++; \
+ } \
+ } while (0)
+
+static const char S_STATUS[] =
+ "# branch.oid dc786f796ff004c8599141031781b0e8e40d5ed2\0# branch.head m"
+ "ain\0# branch.upstream origin/main\0# branch.ab +1 -0\0"
+ "1 .M N... 100644 100644 100644 fb85c0a2eacf48a66ef1b6802bbaacfd179e118"
+ "1 fb85c0a2eacf48a66ef1b6802bbaacfd179e1181 a.txt\0"
+ "2 R. N... 100644 100644 100644 b31b0b130d568d7e685646a4c191c5b7d11d49e"
+ "a b31b0b130d568d7e685646a4c191c5b7d11d49ea R100 e.txt\0"
+ "c.txt\0? d.txt\0u UU N... 100644 100644 100644 100644 1111111111111111"
+ "111111111111111111111111 2222222222222222222222222222222222222222 3333"
+ "333333333333333333333333333333333333 x y.txt\0";
+#define S_STATUS_N (sizeof S_STATUS - 1)
+static const char S_LOG[] =
+ "dc786f7\x1fTester\x1f"
+ "1790943106\x1fterzo\x1fHEAD -> main\x1e\n33f8fc2\x1fTester\x1f"
+ "1790943024\x1fsecondo: a e b\x1forigin/prova, origin/main, prova\x1e\n"
+ "ed69737\x1fTester\x1f"
+ "1790943024\x1fprimo\x1f\x1e\n";
+#define S_LOG_N (sizeof S_LOG - 1)
+static const char S_BRANCHES[] =
+ "*\x1fmain\x1forigin/main\x1f[ahead 1]\x1f"
+ "1790943106\x1fterzo\x1e\n \x1fprova\x1forigin/prova\x1f\x1f"
+ "1790943024\x1fsecondo: a e b\x1e\n";
+#define S_BRANCHES_N (sizeof S_BRANCHES - 1)
+static const char S_NUMSTAT[] = "0\t0\t\0"
+ "c.txt\0"
+ "e.txt\0"
+ "1\t0\ta.txt\0";
+#define S_NUMSTAT_N (sizeof S_NUMSTAT - 1)
+
+/* The layout filled with the data: the text, or "" when it failed. */
+static char text[16384];
+
+static const char *fill(const char *layout, const struct janas_buf *d)
+{
+ text[0] = 0;
+ struct janas_json_doc *doc =
+ janas_json_parse(d->p ? d->p : "", d->n, NULL, 0);
+ CHECK(doc != NULL, "the data is not JSON:\n%.*s", (int)d->n, d->p);
+ if (!doc)
+ return text;
+ struct janas_buf out = {0};
+ char err[200];
+ if (janas_tpl_render(layout, strlen(layout), janas_json_root(doc), &out,
+ err, sizeof err) == 0)
+ snprintf(text, sizeof text, "%.*s", (int)out.n, out.p);
+ else
+ CHECK(0, "a layout not filled: %s", err);
+ janas_buf_free(&out);
+ janas_json_free(doc);
+ return text;
+}
+
+#define TOP "/home/someone/src/work"
+
+static void status(void)
+{
+ struct gt_status s;
+ CHECK(gt_read_status(S_STATUS, S_STATUS_N, &s) == 0, "the status");
+ CHECK(strcmp(s.branch, "main") == 0 &&
+ strcmp(s.upstream, "origin/main") == 0 && s.ahead == 1 &&
+ s.behind == 0 && !s.detached && strcmp(s.oid, "dc786f7") == 0,
+ "the branch: %s %s +%d -%d %s", s.branch, s.upstream, s.ahead,
+ s.behind, s.oid);
+ CHECK(s.n == 4 && s.n_staged == 1 && s.n_changed == 1 &&
+ s.n_untracked == 1 && s.n_conflicts == 1,
+ "the counts: %d files", s.n);
+ CHECK(strcmp(s.f[0].path, "a.txt") == 0 && s.f[0].changed == 'M' &&
+ !s.f[0].staged,
+ "a file changed");
+ CHECK(strcmp(s.f[1].path, "e.txt") == 0 && s.f[1].staged == 'R' &&
+ !s.f[1].changed,
+ "a file renamed: %s", s.f[1].path);
+ CHECK(strcmp(s.f[2].path, "d.txt") == 0 && s.f[2].untracked &&
+ strcmp(s.f[3].path, "x y.txt") == 0 && s.f[3].conflict,
+ "a new file, one in conflict: %s, %s", s.f[2].path, s.f[3].path);
+ struct janas_buf d = {0};
+ gt_status_data(&d, TOP, &s);
+ const char *t = fill(GT_STATUS_LAYOUT, &d);
+ CHECK(strcmp(t, "work (~/src/work), branch main, following origin/main; "
+ "commits to push: 1.\n"
+ "staged: 1, changed: 1, new: 1, in conflict: 1\n"
+ " a.txt: modified\n"
+ " e.txt: renamed, staged\n"
+ " d.txt: new, not tracked\n"
+ " x y.txt: in conflict\n") == 0,
+ "the status' layout:\n%s", t);
+ t = fill(GT_STATUS_BRIEF, &d);
+ CHECK(strstr(t, "a.txt M; e.txt R; d.txt ?; x y.txt U;"),
+ "the status' brief:\n%s", t);
+ janas_buf_free(&d);
+ struct gt_status clean = {.branch = "main"};
+ gt_status_data(&d, TOP, &clean);
+ t = fill(GT_STATUS_LAYOUT, &d);
+ CHECK(strcmp(t, "work (~/src/work), branch main, following no remote "
+ "branch.\nNothing to commit: the working tree is "
+ "clean.\n") == 0,
+ "clean:\n%s", t);
+ janas_buf_free(&d);
+}
+
+static void log_branches(void)
+{
+ struct gt_log l;
+ gt_read_log(S_LOG, S_LOG_N, 2, &l);
+ CHECK(l.n == 2 && l.more && strcmp(l.c[0].sha, "dc786f7") == 0 &&
+ strcmp(l.c[0].message, "terzo") == 0 &&
+ strcmp(l.c[0].refs, "HEAD -> main") == 0 &&
+ l.c[0].at == 1790943106 &&
+ strcmp(l.c[1].message, "secondo: a e b") == 0,
+ "the log: %d", l.n);
+ struct janas_buf d = {0};
+ gt_log_data(&d, TOP, "main", &l);
+ const char *t = fill(GT_LOG_LAYOUT, &d);
+ CHECK(strstr(t, "work (~/src/work), main: the latest commits, 2\n"
+ " dc786f7 terzo (HEAD -> main)\n Tester, 2 "
+ "October") &&
+ strstr(t, " 33f8fc2 secondo: a e b (origin/prova"),
+ "the log's layout:\n%s", t);
+ janas_buf_free(&d);
+ struct gt_branches b;
+ gt_read_branches(S_BRANCHES, S_BRANCHES_N, 20, &b);
+ CHECK(b.n == 2 && b.b[0].current && strcmp(b.b[0].name, "main") == 0 &&
+ strcmp(b.b[0].track, "[ahead 1]") == 0 && !b.b[1].current &&
+ strcmp(b.b[1].upstream, "origin/prova") == 0 && !b.b[1].track[0],
+ "the branches");
+ gt_read_branches(S_BRANCHES, S_BRANCHES_N, 1, &b);
+ CHECK(b.n == 1 && b.more == 1 && b.b[0].current, "one branch listed");
+ gt_read_branches(S_BRANCHES + 0, S_BRANCHES_N, 20, &b);
+ gt_branches_data(&d, TOP, &b);
+ t = fill(GT_BRANCHES_LAYOUT, &d);
+ CHECK(strstr(t, "* main -> origin/main [ahead 1]\n 2 October") &&
+ strstr(t, "\n prova -> origin/prova\n"),
+ "the branches' layout:\n%s", t);
+ janas_buf_free(&d);
+}
+
+static void diff_done(void)
+{
+ struct gt_diff df;
+ gt_read_numstat(S_NUMSTAT, S_NUMSTAT_N, &df);
+ CHECK(df.n == 2 && strcmp(df.f[0].path, "e.txt") == 0 && df.f[0].add == 0 &&
+ strcmp(df.f[1].path, "a.txt") == 0 && df.f[1].add == 1 &&
+ df.add == 1 && df.del == 0,
+ "the numstat: %d %s", df.n, df.f[0].path);
+ gt_read_numstat("-\t-\tlogo.png\0", 13, &df);
+ CHECK(df.n == 1 && df.f[0].add == -1 &&
+ strcmp(df.f[0].path, "logo.png") == 0,
+ "a binary file");
+ static const char patch[] = "diff --git a/x b/x\n+uno\n+due\n";
+ struct janas_buf d = {0};
+ gt_diff_data(&d, TOP, 0, NULL, &df, patch, sizeof patch - 1, 19);
+ const char *t = fill(GT_DIFF_LAYOUT, &d);
+ CHECK(strstr(t, "the changes not staged; files: 1, lines +0 -0\n logo.png "
+ "(binary)\n\ndiff --git a/x b/x\n+uno\n+due\n"),
+ "the diff's layout:\n%s", t);
+ t = fill(GT_DIFF_BRIEF, &d);
+ CHECK(strstr(t, "; it begins:\ndiff --git a/x b/x\n") && !strstr(t, "+uno"),
+ "the diff's brief:\n%s", t);
+ janas_buf_free(&d);
+ gt_done_data(&d, TOP, "commit", "main", "abc1234", "terzo",
+ "2 files changed, 1 insertion(+)");
+ t = fill(GT_DONE_LAYOUT, &d);
+ CHECK(strcmp(t, "work (~/src/work): committed, branch main, abc1234: "
+ "terzo\n2 files changed, 1 insertion(+)\n") == 0,
+ "done:\n%s", t);
+ janas_buf_free(&d);
+}
+
+int main(void)
+{
+ setenv("TZ", "UTC", 1);
+ setenv("HOME", "/home/someone", 1);
+ status();
+ log_branches();
+ diff_done();
+ if (failures) {
+ printf("test_git_parse: %d failures\n", failures);
+ return 1;
+ }
+ printf("test_git_parse: ok\n");
+ return 0;
+}
diff --git a/tests/test_github_parse.c b/tests/test_github_parse.c
new file mode 100644
index 0000000..7504a57
--- /dev/null
+++ b/tests/test_github_parse.c
@@ -0,0 +1,338 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * test_github_parse.c - janas-github's reading of GitHub's answers, without
+ * the network, and the layouts filled with what was read: Janas's issues
+ * and a pull request of llama.cpp as the search gives them, a release of
+ * llama.cpp (its notes opening with HTML), Janas's commits and repository,
+ * a search of repositories and the user's own, as they came on 2 October
+ * 2026 (cut to a few items); and when the issues were last seen, in a
+ * directory of its own under ~/tmp.
+ * The sources belong to the program, so they are compiled in here.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common/template.h"
+#include "github/data.c"
+#include "github/layouts.c"
+#include "github/read.c"
+#include "github/seen.c"
+
+static int failures;
+
+#define CHECK(c, ...) \
+ do { \
+ if (!(c)) { \
+ printf(__VA_ARGS__); \
+ printf("\n"); \
+ failures++; \
+ } \
+ } while (0)
+
+static const char S_ISSUES[] =
+ "{\"total_count\": 14, \"incomplete_results\": false, \"items\": [{\"numb"
+ "er\": 15, \"title\": \"Test report: AMD Ryzen 9 9950X3D 16-Core Processo"
+ "r, medium\", \"user\": {\"login\": \"bfg9000it\"}, \"html_url\": \"https"
+ "://github.com/prabanta-dev/janas/issues/15\", \"created_at\": \"2026-09-"
+ "27T10:50:23Z\", \"closed_at\": null, \"comments\": 7, \"state\": \"open\""
+ ", \"labels\": []}, {\"number\": 14, \"title\": \"Test report: AMD Ryzen "
+ "9 9950X3D 16-Core Processor, medium\", \"user\": {\"login\": \"bfg9000it"
+ "\"}, \"html_url\": \"https://github.com/prabanta-dev/janas/issues/14\", "
+ "\"created_at\": \"2026-09-27T10:17:33Z\", \"closed_at\": null, \"comment"
+ "s\": 0, \"state\": \"open\", \"labels\": []}, {\"number\": 13, \"title\""
+ ": \"Test report: Intel(R) Core(TM) i9-14900HX, full\", \"user\": {\"logi"
+ "n\": \"smallrobots\"}, \"html_url\": \"https://github.com/prabanta-dev/j"
+ "anas/issues/13\", \"created_at\": \"2026-09-27T09:51:34Z\", \"closed_at\""
+ ": \"2026-09-27T18:33:41Z\", \"comments\": 5, \"state\": \"closed\", \"la"
+ "bels\": [{\"name\": \"test-report\"}]}, {\"number\": 29846, \"title\": \""
+ "CUDA: fuse KV cache dequantization into fattn-mma-f16\", \"user\": {\"lo"
+ "gin\": \"Abai\"}, \"html_url\": \"https://github.com/ggml-org/llama.cpp/"
+ "pull/29846\", \"created_at\": \"2026-10-02T11:03:32Z\", \"closed_at\": n"
+ "ull, \"comments\": 3, \"state\": \"open\", \"labels\": [], \"pull_reques"
+ "t\": {\"merged_at\": null}, \"draft\": false}]}";
+static const char S_RELEASES[] =
+ "[{\"tag_name\": \"b11344\", \"name\": \"b11344\", \"html_url\": \"https:"
+ "//github.com/ggml-org/llama.cpp/releases/tag/b11344\", \"published_at\":"
+ " \"2026-10-02T08:54:49Z\", \"prerelease\": true, \"draft\": false, \"bod"
+ "y\": \"<details open>\\n\\nCUDA: fix 2 broken Volta FA cases (#29803)\\n"
+ "\\n</details>\\n\\n**Website:**\\n- <https://llama.app>\\n\\n**Attestati"
+ "ons:**\\n- <https://github.com/ggml-org/llama.cpp/attestations/52093363>"
+ "\\n\\n**macOS/iOS:**\\n- [macOS Apple Silicon (arm64)](https://github.co"
+ "m/ggml-org/llama.cpp/releases/download/b11344/llama-b11344-bin-macos-arm"
+ "64.tar.gz)\\n- macOS Apple Silicon (arm64, KleidiAI enabled) [DISABLED]("
+ "https://github.com/ggml-org/llama.cpp/pull/23780)\\n- [macOS Intel (x64)"
+ "](https://github.com/ggml-org/llama.cpp/releases/download/b11344/llama-b"
+ "11344-bin-macos-x64.tar.gz)\\n- [iOS XCFramework](https://github.com/ggm"
+ "l-org/llama.cpp/releases/download/b11344/llama-b11344-xcframework.zip)\\n"
+ "\\n**Linux:**\\n- [Ubunt\", \"author\": {\"login\": \"github-actions[bot"
+ "]\"}, \"assets\": [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {"
+ "}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {"
+ "}, {}, {}, {}]}]";
+static const char S_COMMITS[] =
+ "[{\"sha\": \"d6b2cf29252ba3244b8c27d5cdd0721ac70fd9be\", \"html_url\": \""
+ "https://github.com/prabanta-dev/janas/commit/d6b2cf29252ba3244b8c27d5cdd"
+ "0721ac70fd9be\", \"commit\": {\"message\": \"Release 2026-10-02: janas-m"
+ "aps - roads and places as an MCP service (routes, public transport, what"
+ " is near, addresses), tried in janas-chat\\n\\nSigned-off-by: camauri <m"
+ "[email protected]>\", \"author\": {\"name\": \"camauri\", \"d"
+ "ate\": \"2026-10-02T11:18:31Z\"}}}, {\"sha\": \"8eb26dcb83bfef4e6d188a16"
+ "a1ddc0c843709589\", \"html_url\": \"https://github.com/prabanta-dev/jana"
+ "s/commit/8eb26dcb83bfef4e6d188a16a1ddc0c843709589\", \"commit\": {\"mess"
+ "age\": \"Release 2026-10-02: layouts - the services' data in the user's "
+ "language, a brief to the model; a glossary of the scales' terms\\n\\nSig"
+ "ned-off-by: camauri <[email protected]>\", \"author\": {\"na"
+ "me\": \"camauri\", \"date\": \"2026-10-01T22:11:17Z\"}}}, {\"sha\": \"45"
+ "0b548e585d20144fc15f68aa6db09a722d14b1\", \"html_url\": \"https://github"
+ ".com/prabanta-dev/janas/commit/450b548e585d20144fc15f68aa6db09a722d14b1\""
+ ", \"commit\": {\"message\": \"Release 2026-10-01: janas-wiki, the servic"
+ "es' tools given when needed, the system message read ahead\", \"author\""
+ ": {\"name\": \"camauri\", \"date\": \"2026-10-01T19:32:44Z\"}}}]";
+static const char S_REPO[] =
+ "{\"full_name\": \"prabanta-dev/janas\", \"description\": \"Janas-LLM: la"
+ "rge mixture-of-experts language models on ordinary computers, in C. Expe"
+ "rts streamed from NVMe, with or without a GPU.\", \"language\": \"C\", \""
+ "license\": {\"spdx_id\": \"GPL-3.0\", \"name\": \"GNU General Public Lic"
+ "ense v3.0\"}, \"default_branch\": \"main\", \"homepage\": null, \"html_u"
+ "rl\": \"https://github.com/prabanta-dev/janas\", \"topics\": [\"avx2\", "
+ "\"c\", \"cpu-inference\", \"gguf\", \"inference-engine\", \"linux\", \"l"
+ "lm\", \"local-llm\", \"mixture-of-experts\", \"moe\", \"nvme\", \"quanti"
+ "zation\", \"qwen3\", \"simd\", \"speculative-decoding\", \"vulkan\"], \""
+ "stargazers_count\": 29, \"forks_count\": 5, \"subscribers_count\": 1, \""
+ "open_issues_count\": 4, \"pushed_at\": \"2026-10-02T11:18:34Z\", \"creat"
+ "ed_at\": \"2026-09-20T09:55:09Z\", \"archived\": false, \"private\": fal"
+ "se, \"fork\": false}";
+static const char S_SEARCH[] =
+ "{\"total_count\": 2721, \"items\": [{\"full_name\": \"ggml-org/llama.cpp"
+ "\", \"stargazers_count\": 130126}, {\"full_name\": \"abetlen/llama-cpp-p"
+ "ython\", \"stargazers_count\": 10636}, {\"full_name\": \"ikawrakow/ik_ll"
+ "ama.cpp\", \"stargazers_count\": 3273}, {\"full_name\": \"withcatai/node"
+ "-llama-cpp\", \"stargazers_count\": 2187}, {\"full_name\": \"go-skynet/g"
+ "o-llama.cpp\", \"stargazers_count\": 940}]}";
+static const char S_MINE[] =
+ "[{\"full_name\": \"prabanta-dev/janas\"}, {\"full_name\": \"prabanta-dev"
+ "/limba\"}, {\"full_name\": \"prabanta-dev/meri\"}, {\"full_name\": \"cam"
+ "auri/SedaiBasic2\"}, {\"full_name\": \"prabanta-dev/.github\"}, {\"full_"
+ "name\": \"camauri/SedaiBasic2-Deps\"}, {\"full_name\": \"camauri/SedaiAu"
+ "dio\"}, {\"full_name\": \"camauri/PxLab\"}, {\"full_name\": \"camauri/Ka"
+ "irosProject\"}]";
+
+/* The layout filled with the data: the text, or "" when it failed. */
+static char text[16384];
+
+static const char *fill(const char *layout, const struct janas_buf *d)
+{
+ text[0] = 0;
+ struct janas_json_doc *doc =
+ janas_json_parse(d->p ? d->p : "", d->n, NULL, 0);
+ CHECK(doc != NULL, "the data is not JSON:\n%.*s", (int)d->n, d->p);
+ if (!doc)
+ return text;
+ struct janas_buf out = {0};
+ char err[200];
+ if (janas_tpl_render(layout, strlen(layout), janas_json_root(doc), &out,
+ err, sizeof err) == 0)
+ snprintf(text, sizeof text, "%.*s", (int)out.n, out.p);
+ else
+ CHECK(0, "a layout not filled: %s", err);
+ janas_buf_free(&out);
+ janas_json_free(doc);
+ return text;
+}
+
+static void issues(void)
+{
+ struct gh_issues v;
+ char err[200] = "";
+ CHECK(gh_read_issues(S_ISSUES, strlen(S_ISSUES), 10, &v, err, sizeof err) ==
+ 0 &&
+ v.total == 14 && v.n == 4,
+ "the issues: %s (%ld, %d)", err, v.total, v.n);
+ CHECK(v.i[0].number == 15 && strcmp(v.i[0].user, "bfg9000it") == 0 &&
+ v.i[0].open && !v.i[0].pr && v.i[0].comments == 7 &&
+ v.i[0].created == gh_utc("2026-09-27T10:50:23Z"),
+ "the first issue");
+ CHECK(v.i[2].number == 13 && !v.i[2].open &&
+ strcmp(v.i[2].labels, "test-report") == 0,
+ "a closed issue with its label: %s", v.i[2].labels);
+ CHECK(v.i[3].pr && !v.i[3].merged && v.i[3].number == 29846,
+ "a pull request");
+ struct janas_buf d = {0};
+ gh_issues_data(&d, "prabanta-dev/janas", "yours", 0, "all",
+ gh_utc("2026-09-25T12:00:00Z"), 1, &v);
+ const char *t = fill(GH_ISSUES_LAYOUT, &d);
+ CHECK(strstr(t, "prabanta-dev/janas (one of yours): 14 new issues since "
+ "25 September") &&
+ strstr(t, "(the first time you ask: the last 7 days); the "
+ "latest 4:\n") &&
+ strstr(t, " #15 Test report: AMD Ryzen 9 9950X3D 16-Core "
+ "Processor, medium\n open, by bfg9000it, 27 "
+ "September") &&
+ strstr(t, "closed, by smallrobots, 27 September") &&
+ strstr(t, ", comments: 5, labels: test-report\n") &&
+ strstr(t, " https://github.com/prabanta-dev/janas/issues/"
+ "15\n"),
+ "the issues' layout:\n%s", t);
+ /* a label is the issue's own: #15 has none */
+ const char *i15 = strstr(t, "#15");
+ const char *i14 = strstr(t, "#14");
+ CHECK(i15 && i14 && !memmem(i15, (size_t)(i14 - i15), "labels", 6),
+ "#15 without labels");
+ t = fill(GH_ISSUES_BRIEF, &d);
+ CHECK(strstr(t, "say which repository was taken") &&
+ strstr(t, "14 new_issues (of those listed: 3 open, 1 "
+ "closed) since 25/9") &&
+ strstr(t, "#15 "),
+ "the issues' brief:\n%s", t);
+ janas_buf_free(&d);
+ struct gh_issues none = {0};
+ gh_issues_data(&d, "a/b", "", 1, "open", 0, 0, &none);
+ t = fill(GH_ISSUES_LAYOUT, &d);
+ CHECK(strcmp(t, "a/b: no open pull requests.\n") == 0, "none:\n%s", t);
+ janas_buf_free(&d);
+}
+
+static void releases(void)
+{
+ struct gh_releases v;
+ char err[200] = "";
+ CHECK(gh_read_releases(S_RELEASES, strlen(S_RELEASES), 3, &v, err,
+ sizeof err) == 0 &&
+ v.n == 1,
+ "the releases: %s", err);
+ CHECK(strcmp(v.r[0].tag, "b11344") == 0 && !v.r[0].name[0] && v.r[0].pre &&
+ v.r[0].assets == 35 &&
+ strcmp(v.r[0].author, "github-actions[bot]") == 0,
+ "the release");
+ CHECK(strncmp(v.r[0].notes, "CUDA: fix 2 broken Volta FA cases (#29803)\n",
+ 43) == 0 &&
+ strstr(v.r[0].notes, "- https://llama.app\n") &&
+ !strchr(v.r[0].notes, '<'),
+ "the notes without HTML, links kept:\n%s", v.r[0].notes);
+ struct janas_buf d = {0};
+ gh_releases_data(&d, "ggml-org/llama.cpp", "found by its name", &v);
+ const char *t = fill(GH_RELEASES_LAYOUT, &d);
+ CHECK(strstr(t, "ggml-org/llama.cpp (the one of that name with the most "
+ "stars): the latest release:\n\nb11344 (pre-release), 2 "
+ "October") &&
+ strstr(t, ", by github-actions[bot], files: 35\nCUDA: fix") &&
+ strstr(t, "https://github.com/ggml-org/llama.cpp/releases/tag/"
+ "b11344\n"),
+ "the releases' layout:\n%s", t);
+ janas_buf_free(&d);
+ CHECK(gh_read_tags("[{\"name\": \"v1.2\"}]", 18, 3, &v, err, sizeof err) ==
+ 0 &&
+ v.tags && v.n == 1,
+ "the tags");
+ gh_releases_data(&d, "a/b", "", &v);
+ t = fill(GH_RELEASES_LAYOUT, &d);
+ CHECK(strcmp(t, "a/b: no releases; its latest tags:\n\nv1.2\n") == 0,
+ "tags:\n%s", t);
+ janas_buf_free(&d);
+}
+
+static void commits(void)
+{
+ struct gh_commits v;
+ char err[200] = "";
+ CHECK(gh_read_commits(S_COMMITS, strlen(S_COMMITS), 2, &v, err,
+ sizeof err) == 0 &&
+ v.n == 2 && v.more,
+ "the commits: %s", err);
+ CHECK(strcmp(v.c[0].sha, "d6b2cf2") == 0 &&
+ strncmp(v.c[0].message, "Release 2026-10-02: janas-maps", 30) ==
+ 0 &&
+ !strchr(v.c[0].message, '\n') &&
+ strcmp(v.c[0].author, "camauri") == 0,
+ "the first commit");
+ struct janas_buf d = {0};
+ gh_commits_data(&d, "prabanta-dev/janas", "", "main", 3, &v);
+ const char *t = fill(GH_COMMITS_LAYOUT, &d);
+ CHECK(strstr(t, "prabanta-dev/janas, branch main: the latest 2 commits in "
+ "the last 3 days:\n d6b2cf2 Release") &&
+ strstr(t, "\n camauri, 2 October"),
+ "the commits' layout:\n%s", t);
+ janas_buf_free(&d);
+}
+
+static void repo(void)
+{
+ struct gh_repo r;
+ char err[200] = "", full[128] = "";
+ CHECK(gh_read_repo(S_REPO, strlen(S_REPO), &r, err, sizeof err) == 0 &&
+ strcmp(r.full, "prabanta-dev/janas") == 0 && r.stars == 29 &&
+ strcmp(r.license, "GPL-3.0") == 0 &&
+ strncmp(r.topics, "avx2, c, ", 9) == 0 && !r.archived,
+ "the repository: %s", err);
+ struct janas_buf d = {0};
+ gh_repo_data(&d, "", &r);
+ const char *t = fill(GH_REPO_LAYOUT, &d);
+ CHECK(strstr(t, "prabanta-dev/janas\nJanas-LLM:") &&
+ strstr(t, "\nstars: 29, forks: 5, watching: 1; open issues and "
+ "pull requests: 4\nlanguage: C\nlicence: GPL-3.0\n"
+ "main branch: main; last push: 2 October") &&
+ !strstr(t, "site:"),
+ "the repository's layout:\n%s", t);
+ janas_buf_free(&d);
+ CHECK(gh_read_search_repo(S_SEARCH, strlen(S_SEARCH), "LLAMA.cpp", full,
+ sizeof full, err, sizeof err) == 0 &&
+ strcmp(full, "ggml-org/llama.cpp") == 0,
+ "the search: %s", full);
+ CHECK(gh_read_search_repo(S_SEARCH, strlen(S_SEARCH), "llama", full,
+ sizeof full, err, sizeof err) == 0 &&
+ strcmp(full, "ggml-org/llama.cpp") == 0,
+ "the search, no name alike: the first, %s", full);
+ CHECK(gh_read_mine(S_MINE, strlen(S_MINE), "Janas", full, sizeof full, err,
+ sizeof err) == 1 &&
+ strcmp(full, "prabanta-dev/janas") == 0 &&
+ gh_read_mine(S_MINE, strlen(S_MINE), "llama.cpp", full,
+ sizeof full, err, sizeof err) == 0,
+ "the user's own");
+}
+
+static void seen(void)
+{
+ char dir[512];
+ const char *home = getenv("HOME");
+ snprintf(dir, sizeof dir, "%s/tmp/janas-github-test-%d", home ? home : ".",
+ (int)getpid());
+ if (mkdir(dir, 0700) != 0) {
+ CHECK(0, "cannot make %s", dir);
+ return;
+ }
+ setenv("XDG_CONFIG_HOME", dir, 1);
+ CHECK(gh_seen("a/b", "issues") == 0, "never seen");
+ gh_seen_set("a/b", "issues", 100);
+ gh_seen_set("a/b", "pulls", 200);
+ gh_seen_set("c/d", "issues", 300);
+ gh_seen_set("a/b", "issues", 400);
+ CHECK(gh_seen("a/b", "issues") == 400 && gh_seen("a/b", "pulls") == 200 &&
+ gh_seen("c/d", "issues") == 300 && gh_seen("c/d", "pulls") == 0,
+ "seen");
+ char path[1200];
+ snprintf(path, sizeof path, "%s/janas/github-seen.txt", dir);
+ CHECK(unlink(path) == 0, "no file at %s", path);
+ snprintf(path, sizeof path, "%s/janas", dir);
+ rmdir(path);
+ rmdir(dir);
+}
+
+int main(void)
+{
+ setenv("TZ", "UTC", 1);
+ CHECK(gh_utc("2026-10-02T11:18:31Z") == 1790939911 && gh_utc(NULL) == -1,
+ "gh_utc");
+ issues();
+ releases();
+ commits();
+ repo();
+ seen();
+ if (failures) {
+ printf("test_github_parse: %d failures\n", failures);
+ return 1;
+ }
+ printf("test_github_parse: ok\n");
+ return 0;
+}
diff --git a/tests/test_layout.c b/tests/test_layout.c
index c5a04fd..9674b6e 100644
--- a/tests/test_layout.c
+++ b/tests/test_layout.c
@@ -9,6 +9,7 @@
* The sources belong to the program, so they are compiled in here.
*/
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -37,7 +38,10 @@ static const char EN[] = "Wind {{wind}} km/h ({{bft|bft}}), sea "
static char asked[4096];
static int calls;
-/* The text back as it came: "Translate into ...:\n\n" cut off. */
+/* The text back as it came ("Translate into ...:\n\n" cut off), with
+ "Wind" put into Italian unless asked not to. */
+static int translate_wind = 1;
+
static int echo(const char *name, const char *system, const char *text,
char **out)
{
@@ -46,8 +50,17 @@ static int echo(const char *name, const char *system, const char *text,
calls++;
snprintf(asked, sizeof asked, "%s", text);
const char *t = strstr(text, "\n\n");
- *out = strdup(t ? t + 2 : text);
- return *out ? 0 : -1;
+ t = t ? t + 2 : text;
+ const char *w = translate_wind ? strstr(t, "Wind") : NULL;
+ size_t n = strlen(t);
+ *out = malloc(n + 2);
+ if (!*out)
+ return -1;
+ if (w)
+ snprintf(*out, n + 2, "%.*sVento%s", (int)(w - t), t, w + 4);
+ else
+ memcpy(*out, t, n + 1);
+ return 0;
}
int main(void)
@@ -62,10 +75,16 @@ int main(void)
!glossary_word("it", "sea.1x", 6) &&
!glossary_word("it", "wd.Mon", 6) &&
!glossary_word("en", "bft.1", 5) &&
- !glossary_word("xx", "bft.1", 5),
+ !glossary_word("xx", "bft.1", 5) &&
+ strcmp(glossary_word("it", "status.open", 11), "aperta") == 0 &&
+ !glossary_word("it", "status.ope", 10),
"words the glossary has not");
- CHECK(glossary_mark("it", 1) != glossary_mark("fr", 1) &&
- glossary_mark("en", 1) == 1,
+ CHECK(glossary_mark("it", "x", 1) != glossary_mark("fr", "x", 1) &&
+ glossary_mark("en", "x", 1) == 1 &&
+ glossary_mark("it", "git_status", 1) !=
+ glossary_mark("it", "x", 1) &&
+ strstr(glossary_terms("it", "github_issues"), "branch = ramo") &&
+ !strstr(glossary_terms("it", "weather_now"), "branch"),
"the glossary's mark");
char dir[512];
@@ -83,7 +102,7 @@ int main(void)
char *it = translated("test", EN);
CHECK(it != NULL, "no layout");
if (it) {
- CHECK(strstr(it, "Wind {{wind}} km/h ({{bft|bft}}), sea {{wave}} m "
+ CHECK(strstr(it, "Vento {{wind}} km/h ({{bft|bft}}), sea {{wave}} m "
"({{code|sea}}).") == it,
"the tags back in place:\n%s", it);
CHECK(strstr(it, "bft.1 = bava di vento\n") &&
@@ -104,6 +123,19 @@ int main(void)
free(again);
free(it);
+ /* the text given back untranslated: not used, not kept */
+ translate_wind = 0;
+ static const char EN2[] = "Rain {{mm}} mm.\n";
+ int before = calls;
+ char *same = translated("test2", EN2);
+ char p2[1200];
+ snprintf(p2, sizeof p2, "%s/janas/layouts/test2.it.%016llx.txt", dir,
+ (unsigned long long)glossary_mark("it", "test2", mark(EN2)));
+ CHECK(!same && calls == before + 1 && access(p2, F_OK) != 0,
+ "an untranslated text refused");
+ free(same);
+ translate_wind = 1;
+
/* lines added by the model between two tags are not kept: those
between tags alone come from English */
{
@@ -127,7 +159,7 @@ int main(void)
/* tidy up: the one file and the two directories made here */
char path[1200];
snprintf(path, sizeof path, "%s/janas/layouts/test.it.%016llx.txt", dir,
- (unsigned long long)glossary_mark("it", mark(EN)));
+ (unsigned long long)glossary_mark("it", "test", mark(EN)));
CHECK(unlink(path) == 0, "no file at %s", path);
snprintf(path, sizeof path, "%s/janas/layouts", dir);
rmdir(path);