Skip to content

Commit 1e6eb24

Browse files
authored
Merge pull request #1 from freva/freva/devel
Initial release
2 parents d481e2c + ad5a38d commit 1e6eb24

64 files changed

Lines changed: 9189 additions & 134 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/main.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: Main pipeline
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
release:
8+
types: [ published ]
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
frontend:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v5
18+
19+
- name: Install pnpm
20+
uses: pnpm/action-setup@v3
21+
with:
22+
version: 10
23+
24+
- name: Setup Node.js
25+
uses: actions/setup-node@v6
26+
with:
27+
node-version: 22
28+
cache: 'pnpm'
29+
cache-dependency-path: frontend/pnpm-lock.yaml
30+
31+
- name: Install Dependencies
32+
run: pnpm install -C frontend --frozen-lockfile
33+
34+
- name: Typecheck
35+
working-directory: frontend
36+
run: pnpm typecheck
37+
38+
- name: Lint
39+
working-directory: frontend
40+
run: pnpm lint
41+
42+
- name: Test
43+
working-directory: frontend
44+
run: pnpm test
45+
46+
- name: Build Frontend
47+
run: make ui
48+
49+
- name: Setup Go
50+
uses: actions/setup-go@v5
51+
with:
52+
go-version-file: 'go.mod'
53+
54+
- name: Test Backend
55+
run: make test
56+
57+
- name: Build Go Binaries
58+
run: |
59+
# os/arch/extension
60+
TARGETS=("linux/amd64/" "linux/arm64/" "darwin/amd64/" "darwin/arm64/" "windows/amd64/.exe")
61+
62+
for target in "${TARGETS[@]}"; do
63+
IFS="/" read -r OS ARCH EXT <<< "$target"
64+
65+
echo "Building for $OS-$ARCH..."
66+
PLATFORM_DIR="dist/${OS}_${ARCH}"
67+
mkdir -p "$PLATFORM_DIR"
68+
69+
for d in cmd/*/; do
70+
binary_name=$(basename "$d")
71+
GOOS=$OS GOARCH=$ARCH go build -o "${PLATFORM_DIR}/${binary_name}${EXT}" "./$d"
72+
done
73+
74+
VERSION=${{ github.event.release.tag_name || 'dev' }}
75+
if [ "$OS" == "windows" ]; then
76+
cd dist && zip -r "../codesearch-${VERSION}-${OS}-${ARCH}.zip" "${OS}_${ARCH}" && cd ..
77+
else
78+
tar -cvzf "codesearch-${VERSION}-${OS}-${ARCH}.tar.gz" -C dist "${OS}_${ARCH}"
79+
fi
80+
done
81+
82+
- name: Upload to Release
83+
if: github.event_name == 'release'
84+
uses: softprops/action-gh-release@v2
85+
with:
86+
files: |
87+
*.tar.gz
88+
*.zip
89+
env:
90+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea/
2+
3+
/cmd/cserver/static/
4+
/db/
5+
/config
6+
/Makefile

AUTHORS

Lines changed: 0 additions & 4 deletions
This file was deleted.

CONTRIBUTORS

Lines changed: 0 additions & 5 deletions
This file was deleted.

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
all: ui go
2+
all-restart: all
3+
systemctl --user restart codesearch-server.service
4+
5+
go:
6+
go install ./...
7+
8+
test:
9+
go test ./...
10+
11+
ui:
12+
cd frontend && pnpm install && pnpm build --emptyOutDir --outDir ../cmd/cserver/static
13+
14+
update:
15+
~/.go/bin/csupdater --config ./config

README

Lines changed: 0 additions & 16 deletions
This file was deleted.

README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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`).

cmd/cgrep/cgrep.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"os"
1212
"runtime/pprof"
1313

14-
"github.com/google/codesearch/regexp"
14+
"github.com/freva/codesearch/regexp"
1515
)
1616

1717
var usageMessage = `usage: cgrep [-c] [-h] [-i] [-l] [-n] [-v] regexp [file...]
@@ -24,7 +24,7 @@ cannot be abbreviated to -in.
2424
`
2525

2626
func usage() {
27-
fmt.Fprintf(os.Stderr, usageMessage)
27+
fmt.Fprint(os.Stderr, usageMessage)
2828
os.Exit(2)
2929
}
3030

cmd/cindex/cindex.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
"runtime/pprof"
1414
"slices"
1515

16-
"github.com/google/codesearch/index"
16+
"github.com/freva/codesearch/index"
1717
)
1818

19-
var usageMessage = `usage: cindex [-list] [-reset] [-zip] [path...]
19+
var usageMessage = `usage: cindex [-index file] [-list] [-reset] [-zip] [path...]
2020
2121
Cindex prepares the trigram index for use by csearch. The index is the
22-
file named by $CSEARCHINDEX, or else $HOME/.csearchindex.
22+
file named by -index, or else $CSEARCHINDEX, or else $HOME/.csearchindex.
2323
2424
The simplest invocation is
2525
@@ -53,11 +53,12 @@ With no path arguments, cindex -reset removes the index.
5353
`
5454

5555
func usage() {
56-
fmt.Fprintf(os.Stderr, usageMessage)
56+
fmt.Fprint(os.Stderr, usageMessage)
5757
os.Exit(2)
5858
}
5959

6060
var (
61+
indexFlag = flag.String("index", "", "path to index file")
6162
listFlag = flag.Bool("list", false, "list indexed paths and exit")
6263
resetFlag = flag.Bool("reset", false, "discard existing index")
6364
verboseFlag = flag.Bool("verbose", false, "print extra information")
@@ -72,8 +73,10 @@ func main() {
7273
flag.Usage = usage
7374
flag.Parse()
7475

76+
indexpath := index.File(*indexFlag)
77+
7578
if *listFlag {
76-
ix := index.Open(index.File())
79+
ix := index.Open(index.File(indexpath))
7780
if *checkFlag {
7881
if err := ix.Check(); err != nil {
7982
log.Fatal(err)
@@ -96,12 +99,12 @@ func main() {
9699
}
97100

98101
if *resetFlag && flag.NArg() == 0 {
99-
os.Remove(index.File())
102+
os.Remove(index.File(indexpath))
100103
return
101104
}
102105
var roots []index.Path
103106
if flag.NArg() == 0 {
104-
ix := index.Open(index.File())
107+
ix := index.Open(index.File(indexpath))
105108
roots = slices.Collect(ix.Roots().All())
106109
} else {
107110
// Translate arguments to absolute paths so that
@@ -117,7 +120,7 @@ func main() {
117120
slices.SortFunc(roots, index.Path.Compare)
118121
}
119122

120-
master := index.File()
123+
master := index.File(indexpath)
121124
if _, err := os.Stat(master); err != nil {
122125
// Does not exist.
123126
*resetFlag = true

cmd/csearch/csearch.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"runtime/pprof"
1515
"strings"
1616

17-
"github.com/google/codesearch/index"
18-
"github.com/google/codesearch/regexp"
17+
"github.com/freva/codesearch/index"
18+
"github.com/freva/codesearch/regexp"
1919
)
2020

2121
var usageMessage = `usage: csearch [-c] [-f fileregexp] [-h] [-i] [-l] [-n] regexp
@@ -44,14 +44,15 @@ empty, $HOME/.csearchindex.
4444
`
4545

4646
func usage() {
47-
fmt.Fprintf(os.Stderr, usageMessage)
47+
fmt.Fprint(os.Stderr, usageMessage)
4848
os.Exit(2)
4949
}
5050

5151
var (
5252
fFlag = flag.String("f", "", "search only files with names matching this regexp")
5353
iFlag = flag.Bool("i", false, "case-insensitive search")
5454
htmlFlag = flag.Bool("html", false, "print HTML output")
55+
indexFlag = flag.String("index", "", "path to index file")
5556
verboseFlag = flag.Bool("verbose", false, "print extra information")
5657
bruteFlag = flag.Bool("brute", false, "brute force - search all files in index")
5758
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to this file")
@@ -109,7 +110,7 @@ func Main() {
109110
log.Printf("query: %s\n", q)
110111
}
111112

112-
ix := index.Open(index.File())
113+
ix := index.Open(index.File(*indexFlag))
113114
ix.Verbose = *verboseFlag
114115
var post []int
115116
if *bruteFlag {

0 commit comments

Comments
 (0)