Skip to main content
View plugin on GitHub
See starters using this

gatsby-source-local-git

npm MIT build

A Gatsby source plugin for sourcing data into your Gatsby application from your local git repository.

Description

This source plugin enables gatsby sites to include meta data about their own git repository at build time. Possible features are:

  • Printing the hash of the latest git commit on an about page
  • Including the name of the current git branch (might be useful for canary builds or split deployments)
  • Creating a list of all git authors / contributors

Missing a feature? Please create an issue (or even a pull request).

How to install

npm install --save gatsby-source-local-git

Examples of usage

// In your gatsby-config.js
module.exports = {
  plugins: [`gatsby-source-local-git`],
}

How to query for data

Commits

You can query commit nodes like the following:

{
  allGitCommit(limit: 25, sort: { fields: date, order: DESC }) {
    edges {
      node {
        hash
        message
        body
        date(fromNow: true)
        refs
        author {
          name
          email
        }
      }
    }
    totalCount
  }
}

Query hash of latest commit:

{
  gitCommit(latest: { eq: true }) {
    hash
  }
}

Tags

You can query tags like this:

{
  allGitTag {
    edges {
      node {
        name
      }
    }
  }
}

Query the latest tag:

{
  gitTag(latest: { eq: true }) {
    name
  }
}

Tags will simply be sorted by semantic version number to find the latest tag (including every tag containing a dot like v1.2.3 or 1.2 but not newest-feature).

Branches

You can query branches like this:

{
  allGitBranch {
    edges {
      node {
        commit
        id
        name
        current
      }
    }
  }
}

Query the current branch:

{
  gitBranch(current: { eq: true }) {
    name
  }
}

Authors

You can query author nodes like the following:

{
  allGitAuthor(sort: { fields: name }) {
    nodes {
      name
      email
    }
    totalCount
  }
}

How to develop locally

Clone gatsby default starter and change directories into site folder

git clone https://github.com/gatsbyjs/gatsby-starter-default.git && cd gatsby-starter-default

Add this repository as git submodule

git submodule add https://github.com/PMudra/gatsby-source-local-git.git plugins/gatsby-source-local-git

Add the plugin to gatsby-config.js

See top of readme for an example.

Start developing the plugin

Just be sure to switch to the right directory depending on the task. Working on plugin:

cd plugins/gatsby-source-local-git
npm install
npm start

See if the plugin is working as expected (in root directory i.e. gatsby-starter-default):

npm install
npm start