Search your code using Sourcegraph

Search your code using Sourcegraph

Recently I have figured out how to handle link rot problem using Recoll. Of course you can use it for code searching, but we can do better.

Another method for searching code is using Github, if you don’t mind it’s online. Searching your own code is also easy with syntax user:<username>. However I want to be faster.

Years ago I tried Sourcegraph. I do not remember why I gave up. Maybe it’s because I was not familiar with Docker. Now I am using Docker everyday, actuaaly docker-compose, so I decided to give it another try. After finishing writing the previous sentence, I remembered that months ago sourcegraph cloud can host your own code, so I gave up self-hosting.

Now things is changing. Sourcegraph discontinued their cloud service. I have to host it myself, which is faster. Bad news is that you can only have 1 private repo now. https://news.ycombinator.com/item?id=34607118

We will be implementing changes to the Sourcegraph free tier on February 22, 2023 with the release of Sourcegraph 4.5. Details on the new free tier can be found here [1]. The changes are: The maximum number of private repositories that can be synced to each instance will be reduced from unlimited to 1. The free tier will no longer support SSO/SAML.

After more digging into hacker news, I found that you can still use sourcegraph to search your local code. https://news.ycombinator.com/item?id=36589033

In case someone is wondering how to search local repositories with sourcegraph, see https://docs.sourcegraph.com/admin/external_service/src_serv... and https://docs.sourcegraph.com/admin/deploy/docker-single-cont...
  docker run --add-host=host.docker.internal:host-gateway --publish 7080:7080 --publish 127.0.0.1:3370:3370 --rm --volume ~/.sourcegraph/config:/etc/sourcegraph --volume ~/.sourcegraph/data:/var/opt/sourcegraph sourcegraph/server:5.1.2


  docker run --rm=true --publish 3434:3434 --volume $PWD:/data/repos:ro sourcegraph/src-cli:latest serve-git /data/repos

So no more problems. Here is my docker-compose.yaml

version: "2.4"                                                                                                                                                                                                          [5/3244]

services:
  sourcegraph:
    image: sourcegraph/server:5.2.0
    restart: unless-stopped
    container_name: sourcegraph
    networks:
      - traefik
    ports:
      - "7080:7080/tcp"
      - "3370:3370/tcp"
    environment:
      - PUID=1000
      - PGID=1000
    volumes:
      - /data/docker/sourcegraph/config:/etc/sourcegraph
      - /data/docker/sourcegraph/data:/var/opt/sourcegraph
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.code.entrypoints=http"
      - "traefik.http.routers.code.rule=Host(`code.pm.lo`)"
      - "traefik.http.services.code.loadbalancer.server.port=7080"
  src-cli:
    image: sourcegraph/src-cli:latest
    restart: unless-stopped
    container_name: src-cli
    networks:
      - traefik
    ports:
      - "3434:3434/tcp"
    environment:
      - PUID=1000
      - PGID=1000
    volumes:
      - /data/Dropbox/Repos:/data/repos
    command: serve-git /data/repos
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.codesrc.entrypoints=http"
      - "traefik.http.routers.codesrc.rule=Host(`codesrc.pm.lo`)"
      - "traefik.http.services.codesrc.loadbalancer.server.port=3434"
networks:
  traefik:
    external: true