Other Git repository hosts

Site admins can sync Git repositories on any Git repository host (by Git clone URL) with Sourcegraph so that users can search and navigate the repositories. Use this method only when your repository host is not named as a supported external service.

To connect generic Git host to Sourcegraph:

  1. Go to Site admin > Manage repositories > Add repositories
  2. Select Generic Git host.
  3. Configure the connection to generic Git host the action buttons above the text field, and additional fields can be added using Cmd/Ctrl+Space for auto-completion. See the configuration documentation below.
  4. Press Add repositories.

Constructing the url for SSH access

If your code host serves git repositories over SSH (e.g. Gerrit), make sure your Sourcegraph instance can connect to your code host over SSH:

docker exec $CONTAINER ssh -p $PORT $USER@$HOSTNAME
  • $CONTAINER is the name or ID of your sourcegraph/server container
  • $PORT is the port on which your code host's git server is listening for connections (Gerrit defaults to 29418)
  • $USER is your user on your code host (Gerrit defaults to admin)
  • $HOSTNAME is the hostname of your code host from within the sourcegraph/server container (e.g. gerrit.example.com)

Here's an example for Gerrit:

docker exec sourcegraph ssh -p 29418 [email protected]

The url field is then

  "url": "ssh://$USER@$HOSTNAME:$PORT"`

Here's an example for Gerrit:

  "url": "ssh://[email protected]:29418",

Adding repositories

For Gerrit, elements of the repos field are the same as the repository names. For example, a repository at https://gerrit.example.com/admin/repos/gorilla/mux will be "gorilla/mux" in the repos field.

Repositories must be listed individually:

  "repos": [
    "gorilla/mux",
    "sourcegraph/sourcegraph"
  ]

Experimental: src-expose

src-expose is a tool to periodically snapshot local directories and serve them as Git repositories over HTTP. This is a useful way to get code from other version control systems or textual artifacts from non version controlled systems (eg configuration) into Sourcegraph.

Quick start

Start up a Sourcegraph instance

docker run --publish 7080:7080 --publish 127.0.0.1:3370:3370 --rm --volume ~/.sourcegraph/config:/etc/sourcegraph --volume ~/.sourcegraph/data:/var/opt/sourcegraph sourcegraph/server:3.12.7

Pick a directory you want to export from, then run:

wget https://storage.googleapis.com/sourcegraph-artifacts/src-expose/latest/darwin-amd64/src-expose
# For linux comment the above and uncomment the below
# wget https://storage.googleapis.com/sourcegraph-artifacts/src-expose/latest/linux-amd64/src-expose

chmod +x src-expose
./src-expose dir1 dir2 dir3

src-expose will output a configuration to use. It may scroll by quickly due to snapshot logging, so scroll up. However, this configuration should work:

 {
    // url is the http url to src-expose (listening on 127.0.0.1:3434)
    // url should be reachable by Sourcegraph.
    // "http://host.docker.internal:3434" works from Sourcegraph when using Docker for Desktop.
    "url": "http://host.docker.internal:3434",
    "repos": ["src-expose"]
}

Go to Admin > External services > Add external service > Single Git repositories. Input the above configuration. Your directories should now be syncing in Sourcegraph.

Advanced configuration

The command line argument used by the quick start is for quickly validating the approach. However, you may have more complicated scenarios for snapshotting. In that case you can pass a YAML configuration file:

src-expose -snapshot-config config.yaml

To see the configuration please consult src-expose -help. The example.yaml also documents the possibilities.

Serving git repositories

Alternatively you can serve git repositories. See src-expose serve -help.

Configuration

admin/external_service/other_external_service.schema.json

{
	"repos": null,

	// The pattern used to generate the corresponding Sourcegraph repository name for the repositories. In the pattern, the variable "{base}" is replaced with the Git clone base URL host and path, and "{repo}" is replaced with the repository path taken from the `repos` field.
	//
	// For example, if your Git clone base URL is https://git.example.com/repos and `repos` contains the value "my/repo", then a repositoryPathPattern of "{base}/{repo}" would mean that a repository at https://git.example.com/repos/my/repo is available on Sourcegraph at https://sourcegraph.example.com/git.example.com/repos/my/repo.
	//
	// It is important that the Sourcegraph repository name generated with this pattern be unique to this code host. If different code hosts generate repository names that collide, Sourcegraph's behavior is undefined.
	"repositoryPathPattern": "{base}/{repo}",
	// Other example values:
	// - "pretty-host-name/{repo}"

	"url": null
	// Other example values:
	// - "https://github.com/?access_token=secret"
	// - "ssh://[email protected]:2333/"
	// - "git://host.xz:2333/"
}