> ## Documentation Index
> Fetch the complete documentation index at: https://sourcebot-jminnetian-askskills.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Local Git repositories

Sourcebot can sync code from generic git repositories stored in a local directory. This can be helpful in scenarios where you already have a large number of repos already checked out. Local repositories are treated as **read-only**, meaning Sourcebot will **not** `git fetch` new revisions.

If you're not familiar with Sourcebot [connections](/docs/connections/indexing-your-code), please read that overview first.

## Getting Started

<Warning>
  Only folders containing git repositories at their root **and** have a `remote.origin.url` set in their git config are supported at this time. All other folders will be skipped.
</Warning>

Let's assume we have a `repos` directory located at `$(PWD)` with a collection of git repositories:

```sh theme={null}
repos/
├─ repo_1/
├─ repo_2/
├─ repo_3/
├─ ...
```

To get Sourcebot to index these repositories:

<Steps>
  <Step title="Mount a volume">
    We need to mount a docker volume to the `repos` directory so Sourcebot can read it's contents. Sourcebot will **not** write to local repositories, so we can mount a separate **read-only** volume:

    ```bash theme={null}
    docker run \
        -v $(pwd)/repos:/repos:ro \
        /* additional args */ \
        ghcr.io/sourcebot-dev/sourcebot:latest
    ```
  </Step>

  <Step title="Create a connection">
    We can now create a new git [connection](/docs/connections/indexing-your-code), specifying local paths with the `file://` prefix. Glob patterns are supported. For example:

    ```json theme={null}
    {
        "type": "git",
        "url": "file:///repos/*"
    }
    ```

    Sourcebot will expand this glob pattern into paths `/repos/repo_1`, `/repos/repo_2`, etc. and index all valid git repositories.
  </Step>
</Steps>

## Examples

<AccordionGroup>
  <Accordion title="Sync individual repo">
    ```json theme={null}
    {
        "type": "git",
        "url": "file:///path/to/git_repo"
    }
    ```
  </Accordion>

  <Accordion title="Sync multiple repos using glob patterns">
    ```json theme={null}
    // Attempt to sync directories contained in `repos/` (non-recursive)
    {
        "type": "git",
        "url": "file:///repos/*"
    }
    ```
  </Accordion>
</AccordionGroup>

## Optimizing git operations

Sourcebot performs a number of operations that require traversing a repository's entire commit history (e.g., `git rev-list --count HEAD`). These operations can be slow in repositories with a large number of commits.

Typically, a [commit graph](https://git-scm.com/docs/commit-graph) is generated to speed up these operations (see [#791](https://github.com/sourcebot-dev/sourcebot/pull/791)). However, since local repositories are treated as read-only, Sourcebot **will not** generate a commit graph for them.

A commit graph can be manually generated by running the following command in the repository's root directory:

```sh theme={null}
git commit-graph write --reachable
```

The commit graph can be updated when fetching with `--write-commit-graph`:

```sh theme={null}
git fetch --write-commit-graph
```

## Schema reference

<Accordion title="Reference">
  [schemas/v3/genericGitHost.json](https://github.com/sourcebot-dev/sourcebot/blob/main/schemas/v3/genericGitHost.json)

  ```json theme={null}
  {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "title": "GenericGitHostConnectionConfig",
    "properties": {
      "type": {
        "const": "git",
        "description": "Generic Git host configuration"
      },
      "url": {
        "type": "string",
        "format": "url",
        "description": "The URL to the git repository. This can either be a remote URL (prefixed with `http://` or `https://`) or a absolute path to a directory on the local machine (prefixed with `file://`). If a local directory is specified, it must point to the root of a git repository. Local directories are treated as read-only modified. Local directories support glob patterns.",
        "pattern": "^(https?:\\/\\/[^\\s/$.?#].[^\\s]*|file:\\/\\/\\/[^\\s]+)$",
        "examples": [
          "https://github.com/sourcebot-dev/sourcebot",
          "file:///path/to/repo",
          "file:///repos/*"
        ]
      },
      "revisions": {
        "type": "object",
        "description": "The revisions (branches, tags) that should be included when indexing. The default branch (HEAD) is always indexed. A maximum of 64 revisions can be indexed, with any additional revisions being ignored.",
        "properties": {
          "branches": {
            "type": "array",
            "description": "List of branches to include when indexing. For a given repo, only the branches that exist on the repo's remote *and* match at least one of the provided `branches` will be indexed. The default branch (HEAD) is always indexed. Glob patterns are supported. A maximum of 64 branches can be indexed, with any additional branches being ignored.",
            "items": {
              "type": "string"
            },
            "examples": [
              [
                "main",
                "release/*"
              ],
              [
                "**"
              ]
            ],
            "default": []
          },
          "tags": {
            "type": "array",
            "description": "List of tags to include when indexing. For a given repo, only the tags that exist on the repo's remote *and* match at least one of the provided `tags` will be indexed. Glob patterns are supported. A maximum of 64 tags can be indexed, with any additional tags being ignored.",
            "items": {
              "type": "string"
            },
            "examples": [
              [
                "latest",
                "v2.*.*"
              ],
              [
                "**"
              ]
            ],
            "default": []
          }
        },
        "additionalProperties": false
      },
      "enforcePermissions": {
        "type": "boolean",
        "description": "Controls whether repository permissions are enforced for this connection. When `PERMISSION_SYNC_ENABLED` is false, this setting has no effect. Defaults to the value of `PERMISSION_SYNC_ENABLED`. See https://docs.sourcebot.dev/docs/features/permission-syncing"
      },
      "enforcePermissionsForPublicRepos": {
        "type": "boolean",
        "default": false,
        "description": "Controls whether repository permissions are enforced for public repositories in this connection. When true, public repositories are only visible to users with a linked account for this connection's code host. When false, public repositories are visible to all users. Has no effect when enforcePermissions is false. Defaults to false. See https://docs.sourcebot.dev/docs/features/permission-syncing"
      }
    },
    "required": [
      "type",
      "url"
    ],
    "additionalProperties": false
  }
  ```
</Accordion>
