|
| 1 | +# Code search |
| 2 | + |
| 3 | +Extends [github.com/google/codesearch](https://github.com/google/codesearch) with a tool to sync git repositories |
| 4 | +to be indexed and a web interface. The sync tool and web interface inspired by [github.com/hakonhall/codesearch](https://github.com/hakonhall/codesearch). |
| 5 | + |
| 6 | +## Configuration |
| 7 | + |
| 8 | +Configuration is done via a config file. The format is as follows: |
| 9 | + |
| 10 | +- The file consists of global settings and one or more `[server NAME]` sections. |
| 11 | +- Relative paths are resolved relative to the config file location. |
| 12 | + |
| 13 | +**Global settings:** |
| 14 | +- `code`: Directory for checked-out and indexed source code. Default: `[workdir]/code` |
| 15 | +- `fileindex`: Path to the file index file. Default: `[workdir]/csearch.fileindex` |
| 16 | +- `index`: Path to the code index file. Default: `[workdir]/csearch.index` |
| 17 | +- `port`: Port for the server. Default: `80` |
| 18 | +- `manifest`: Path to the manifest file. Default: `[workdir]/manifest.json` |
| 19 | +- `workdir`: Working directory managed by the program. Required. |
| 20 | + |
| 21 | +**Server section (`[server NAME]`):** |
| 22 | +- `api`: GitHub REST API URL. Default: `https://api.github.com` |
| 23 | +- `exclude`: Regex to exclude repositories (at most one). |
| 24 | +- `include`: Repository or owner to include. Formats: |
| 25 | + - `OWNER` (all repos for user/org) |
| 26 | + - `OWNER/REPO` (specific repo) |
| 27 | + - `OWNER/REPO#BRANCH` (specific branch) |
| 28 | + - `OWNER/REPO#REF` (specific commit) |
| 29 | +- `token`: OAuth2 token (e.g., personal access token). |
| 30 | +- `weburl`: Git web interface URL. Default: `https://github.com` |
| 31 | +- `url`: Base URL for cloning (e.g., `git@github.com`, `https://github.com`). Required. |
| 32 | + |
| 33 | +**Example config:** |
| 34 | +```ini |
| 35 | +[global] |
| 36 | +code = db |
| 37 | +index = /home/user/.csearchindex |
| 38 | +port = 8080 |
| 39 | + |
| 40 | +[server github] |
| 41 | +exclude = ^DEPRECATED |
| 42 | +include = freva |
| 43 | +include = torvalds/linux |
| 44 | + |
| 45 | +[server internal] |
| 46 | +api = https://git.example.com/api |
| 47 | +include = internalorg |
| 48 | +token = ghp_YYYYYYYYYYYYYYYYYYYY |
| 49 | +weburl = https://git.example.com |
| 50 | +url = https://ghp_YYYYYYYYYYYYYYYYYYYY@git.example.com |
| 51 | +``` |
| 52 | + |
| 53 | +## Binaries |
| 54 | + |
| 55 | +The following binaries are produced: |
| 56 | +- `cgrep`: Command-line code search using the index. Usage: |
| 57 | + ```sh |
| 58 | + cgrep [flags] regexp [file...] |
| 59 | + ``` |
| 60 | + Flags: `-c` (count), `-h` (no filename), `-i` (case-insensitive), `-l` (list files), `-n` (line numbers), `-v` (invert match) |
| 61 | + |
| 62 | +- `cindex`: Builds the code search index. Usage: |
| 63 | + ```sh |
| 64 | + cindex [flags] [path...] |
| 65 | + ``` |
| 66 | + Flags: `-index` (index file), `-list` (list indexed paths), `-reset` (discard existing index), `-zip` (index zip files) |
| 67 | + |
| 68 | +- `csearch`: Behaves like `cgrep`, but over all indexed files with option to limit search to files matching a regex. Usage: |
| 69 | + ```sh |
| 70 | + csearch [flags] regexp |
| 71 | + ``` |
| 72 | + Flags: `-c` (count), `-f` (file regexp), `-h` (no filename), `-i` (case-insensitive), `-l` (list files), `-n` (line numbers), `-index` (index file) |
| 73 | + |
| 74 | +- `cserver`: HTTP server providing the web UI. Usage: |
| 75 | + ```sh |
| 76 | + cserver --config path/to/config |
| 77 | + ``` |
| 78 | +- `csupdater`: Updates repositories and indices from remote sources. Usage: |
| 79 | + ```sh |
| 80 | + csupdater --config path/to/config [--manifest] [--sync] [--index] [--verbose] [--exit-early] [--help-config] |
| 81 | + ``` |
| 82 | + Flags: `--config` (config file), `--exit-early` (exit without updating index if no change in sync), `--manifest` (whether to update manifest), `--sync` (whether to sync repositories), `--index` (whether to update index) |
| 83 | + By default, runs all update steps. |
| 84 | + |
| 85 | +## Building |
| 86 | + |
| 87 | +Requirements: [Go](https://golang.org/doc/install) (>=1.23), [pnpm](https://pnpm.io/) (>=10). |
| 88 | + |
| 89 | +Run: |
| 90 | +```sh |
| 91 | +make |
| 92 | +``` |
| 93 | +This installs Go binaries and builds the frontend UI. |
| 94 | + |
| 95 | +## Automatic Updates and Server |
| 96 | + |
| 97 | +To run the server and keep the index up to date automatically, install the provided systemd user services: |
| 98 | + |
| 99 | +1. Copy the service files from `systemd/` to your user systemd directory: |
| 100 | + ```sh |
| 101 | + cp systemd/codesearch-server.service systemd/codesearch-updater.service ~/.config/systemd/user/ |
| 102 | + ``` |
| 103 | + then edit them to set the correct paths for the binaries and config file. |
| 104 | +2. Reload systemd and enable the services: |
| 105 | + ```sh |
| 106 | + systemctl --user daemon-reload |
| 107 | + systemctl --user enable --now codesearch-server.service codesearch-updater.service |
| 108 | + ``` |
| 109 | + |
| 110 | +- `codesearch-server.service` runs the HTTP server (`cserver`). |
| 111 | +- `codesearch-updater.service` keeps the repositories and index up to date (`csupdater`). |
0 commit comments