Git Log Parser

1.2.1 · abandoned · verified Tue Apr 21

git-log-parser is a Node.js library designed to execute the `git log` command and stream its output as structured JavaScript objects. Released in 2015 with its latest stable version 1.2.1, it provides a programmatic interface to extract commit metadata such as commit hashes, author and committer details (name, email, date), subject, and body. It handles the conversion of configuration objects into command-line arguments for `git log` using `argv-formatter` and spawns a `child_process` to execute the `git` command. While functional, the package has seen no updates since its last release, indicating an abandoned status. Developers should be aware of its reliance on an external `git` installation and the need for stream processing libraries (like `through2` or `stream-to-array`) to consume its output as an array. Its primary differentiator is simplifying the interaction with `git log` by abstracting command-line argument formatting and providing parsed object streams.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use git-log-parser to retrieve commits from the last 24 hours, collect them into an array, and print details of the latest commit. It also includes basic error handling for common issues like 'git' not found.

import log from 'git-log-parser';
import toArray from 'stream-to-array';

async function getRecentCommits() {
  try {
    // Parse commits from the last 24 hours
    const commitsStream = log.parse({
      before: new Date(Date.now() - 24 * 60 * 60 * 1000) 
    });

    // Collect all commits into an array
    const commits = await toArray(commitsStream);

    console.log(`Found ${commits.length} commits from the last 24 hours.`);
    if (commits.length > 0) {
      console.log('--- Latest Commit ---');
      const latest = commits[0];
      console.log(`Subject: ${latest.subject}`);
      console.log(`Author: ${latest.author.name} <${latest.author.email}>`);
      console.log(`Date: ${latest.author.date.toISOString()}`);
      console.log(`Short SHA: ${latest.commit.short}`);
    }
  } catch (error) {
    console.error("Failed to parse git log:", error.message);
    if (error.code === 'ENOENT' && error.syscall === 'spawn git') {
      console.error('Make sure Git is installed and available in your system PATH.');
    }
  }
}

getRecentCommits();

view raw JSON →