Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

git-parse

wayfair644.3kBSD-2-Clause3.0.1TypeScript support: included

Turn a git repository into a javascript object.

git

readme

Git-Parse

NPM version NPM Downloads Bundlephobia

git-parse is a utility which generates an array of javascript objects representing the current branch of a local git repository's commit history.

Details

  • Support NodeJS >= 14

Installation

npm install git-parse

Usage

const { gitToJs } = require('git-parse');

const commitsPromise = gitToJs('path/to/repo/');

commitsPromise.then(commits => console.log(JSON.stringify(commits, null, 2)));
<summary>Console output:</summary>
  [
    {
      "hash": "7cedc121ee163d859dfdb9911e627d4b5933cc6d",
      "authorName": "mpackard@wayfair.com",
      "authorEmail": "mpackard@wayfair.com",
      "date": "Wed, 10 Jan 2018 16:44:52 -0500",
      "message": "initial setup",
      "filesAdded":[
          { "path": "packages/raspberry-popsicle/index.js" },
          { "path": "packages/raspberry-popsicle/package-lock.json" },
          { "path": "packages/raspberry-popsicle/package.json" }
      ],
      "filesDeleted": [],
      "filesModified": [],
      "filesRenamed": []
    },
    {
      "hash": "226f032eb87ac1eb18b7212eeaf1356980a9ae03",
      "authorName": "mpackard@wayfair.com",
      "authorEmail": "mpackard@wayfair.com",
      "date": "Wed, 10 Jan 2018 15:25:16 -0500",
      "message": "add README",
      "filesAdded": [
        { "path": "README.md" }
      ],
      "filesDeleted": [],
      "filesModified": [],
      "filesRenamed": []
    }
  ]

API

.gitToJs(pathToRepo, [options])

Returns a promise which resolves with a list of objects describing git commits on the current branch. pathToRepo is a string. options is an optional object with one property, sinceCommit, which is a commit hash. If sinceCommit is present, gitToJs will return logs for commits after the commit specified.

const { gitToJs } = require('git-parse');

const commitsPromise = gitToJs('path/to/repo/');

commitsPromise.then(commits => console.log(JSON.stringify(commits, null, 2)));

.checkOutCommit(pathToRepo, commitHash, [options])

Checks a repository out to a given commit. hash is the commit hash. Options is an optional object with one property, force. force adds --force to the underlying git checkout. Returns a promise.

.gitPull(pathToRepo)

Runs 'git pull' on the repository at the given path. Returns a promise.

.gitDiff(pathToRepo, commitHash1, [commitHash2], [file])

Returns a git diff given a path to the repo, a commit, an optional second commit, and an optional file path.

Returns a promise resolving with the diff as a string.

Development

Build

To generate a local distribution for the git-parse library run the following command:

yarn build

To build the library in the background on file changes, use:

yarn build:watch

Code Formatting

To automatically format the code using Prettier run:

yarn format

or

yarn format:check

Linting

To lint the project using ESLint run:

yarn lint

Tests

To run the project tests using the Jest framework run:

yarn test

Type Checking

To check the project for type errors using Typescript run:

yarn tsc

Use --watch to run the type checker in the background:

yarn tsc --watch

License

This project is licensed under the BSD-2-Clause license.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

3.0.1 (Nov 9, 2022)

  • Minor and patch bumps:
    • @babel/cli to ^7.19.3
    • @bable/core to ^7.19.3
    • @babel/eslint-parser to ^7.19.1
    • @babel/plugin-proposal-object-rest-spread to ^7.19.4
    • @babel/preset-env to ^7.19.4
    • @commitlint/cli to ^17.1.2
    • @commitlint/config-conventional to ^17.1.0
    • @typescript-eslint/eslint-plugin to ^5.40.0
    • @typescript-eslint/parser to ^5.40.0
    • eslint to 8.25.0
    • lerna to 5.6.2
    • typescript to ^4.8.4
  • update checkoutCommit to use execFile instead of exec. See CVE-2021-26543 and patch for same.

3.0.0 (August 8, 2022)

  • Bump minimum Node version to 14 and bump Lerna to 5.1.8
  • Upgrade yarn to 3.2.2 and add outdated plugin
  • Major bumps:
    • @commitlint/cli to ^17.0.3
    • @commitlint/config-conventional to ^17.0.3
    • @types/jest to ^28.1.6
    • eslint to 8.20.0
    • eslint-plugin-flowtype to 8.0.3
    • husky to ^8.0.1
    • jest to ^28
    • pinst to ^3.0.0
  • Minor and patch bumps:
    • @babel/cli to ^7.18.9
    • @babel/core to ^7.18.9
    • @babel/eslint-parser to ^7.18.9
    • @babel/plugin-proposal-class-properties to ^7.18.6
    • @babel/plugin-proposal-object-rest-spread to ^7.18.9
    • @babel/preset-env to ^7.18.9
    • @babel/preset-typescript to ^7.18.6
    • @typescript-eslint/eslint-plugin to ^5.30.7
    • @typescript-eslint/parser to ^5.30.7
    • eslint-plugin-import to 2.26.0
    • prettier to ^2.7.1
    • typescript to ^4.7.4

2.1.2 (June 28, 2022)

  • Upgrade minimist dep ^1.2.6
  • Allow whitespace in file names for files modified, deleted, or added in parsed commit output.
  • Add yarn resolutions for node-fetch ^2.6.7 and json-schema ^0.4.0 to avoid vulnerabilities.

2.1.1 (March 29, 2022)

Chores

  • Migrates package manager from npm to yarn2
  • Uses node 12 instead of 14 to better support apollo-graphql downstream
  • Replace Flow with TypeScript for type checking
  • Bundle TS types with library
  • Add workflow to auto close PRs with no response from the author after 30 days
  • Add workflow to close stale PRs and Issues
  • Add checks via https://www.npmjs.com/package/husky and commit-lint to validate commit messages match the Conventional Commit specification specification.

2.0.0 (November 8, 2021)

Features

  • Changelog will now track changes when they are made!
  • Increased verbosity in npm ci for GitHub Action checks

Bug Fixes

  • Removed unused inquirer and lodash dependencies
  • Removed unused nock dependency

Breaking Changes

  • Updated project to indicate support for Nodejs v14.x and above