Git Log Parser
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
-
Error: spawn git ENOENT
cause The `git` command is not found in the system's PATH.fixInstall Git on your system and ensure its executable path is added to your environment variables. For Windows, reinstall Git and select the option to add Git to PATH. For Linux/macOS, ensure Git is installed (e.g., `sudo apt-get install git` or `brew install git`). -
TypeError: Cannot read properties of undefined (reading 'parse') (or similar for 'fields')
cause Incorrect import statement or module not found.fixIf using ESM, ensure you are using `import log from 'git-log-parser';` (default import). If using CommonJS, use `const log = require('git-log-parser');`. Verify the package is correctly installed.
Warnings
- breaking The package has been abandoned since 2016. It may not be compatible with newer versions of Node.js, `git` CLI changes, or modern stream APIs without significant issues. Use with caution in new projects.
- gotcha This library relies on the `git` command-line tool being installed and accessible in the system's PATH. If `git` is not found, the parser will fail.
- gotcha The `parse` method returns a Node.js Readable Stream. To get an array of commit objects, you need to pipe it to a consumer like `stream-to-array` or manually process the stream chunks.
Install
-
npm install git-log-parser -
yarn add git-log-parser -
pnpm add git-log-parser
Imports
- log
import { parse } from 'git-log-parser';import log from 'git-log-parser';
- log
const log = require('git-log-parser'); - log.fields
import { fields } from 'git-log-parser';import log from 'git-log-parser'; console.log(log.fields);
Quickstart
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();