Security News
Nightmares on npm: How Two Malicious Packages Facilitate Data Theft and Destruction
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.
git-log-parser
Advanced tools
The git-log-parser npm package is a tool for parsing git logs in a structured way. It allows users to extract and manipulate information from git logs programmatically, making it easier to analyze commit history, authorship, and other repository metadata.
Parse Commit Logs
This feature allows you to parse the commit logs of a git repository. The code sample demonstrates how to use the git-log-parser to extract commit information and store it in an array.
const gitLogParser = require('git-log-parser');
const { exec } = require('child_process');
const commits = [];
exec('git log', { maxBuffer: Infinity }, (err, stdout) => {
if (err) throw err;
gitLogParser.parse({ _: ['log'] }, stdout).on('data', commit => {
commits.push(commit);
}).on('end', () => {
console.log(commits);
});
});
Filter Commits by Author
This feature allows you to filter commits by a specific author. The code sample shows how to use the git-log-parser to extract commits made by a particular author.
const gitLogParser = require('git-log-parser');
const { exec } = require('child_process');
const commits = [];
exec('git log --author="Author Name"', { maxBuffer: Infinity }, (err, stdout) => {
if (err) throw err;
gitLogParser.parse({ _: ['log'] }, stdout).on('data', commit => {
commits.push(commit);
}).on('end', () => {
console.log(commits);
});
});
Extract Commit Messages
This feature allows you to extract commit messages from the git log. The code sample demonstrates how to use the git-log-parser to get the commit messages and store them in an array.
const gitLogParser = require('git-log-parser');
const { exec } = require('child_process');
const commitMessages = [];
exec('git log --pretty=format:"%s"', { maxBuffer: Infinity }, (err, stdout) => {
if (err) throw err;
gitLogParser.parse({ _: ['log'] }, stdout).on('data', commit => {
commitMessages.push(commit.subject);
}).on('end', () => {
console.log(commitMessages);
});
});
simple-git is a lightweight interface for running git commands in any node.js application. It provides a more general-purpose approach to interacting with git repositories, including features for parsing logs, managing branches, and handling remote repositories. Compared to git-log-parser, simple-git offers a broader range of git functionalities.
nodegit is a native Node.js library for interacting with git repositories. It provides a comprehensive set of features for working with git, including parsing logs, managing branches, and handling commits. nodegit is more feature-rich and lower-level compared to git-log-parser, making it suitable for more complex git operations.
git-rev-sync is a simple utility for synchronously getting git repository information, such as the current branch, commit hash, and commit message. While it does not offer the same level of detailed log parsing as git-log-parser, it is useful for quickly retrieving basic git information in a synchronous manner.
Run git log
and return a stream of commit objects.
$ npm install git-log-parser
log.parse(config, options)
-> Stream(commits)
Accepts a config
object mapping to the options accepted by git log
. config
will be automatically converted to command line options and flags by argv-formatter. Returns a stream of commit objects.
options
is passed directly to child_process.spawn
.
A commit is structured as follows:
{
commit: {
'long': '4bba6092ecb2571301ca0daa2c55336ea2c74ea2',
'short': '4bba609'
},
tree: {
'long': 'b4ef3379e639f8c0034831deae8f6ce63dd41566',
'short': 'b4ef337'
},
author: {
'name': 'Ben Drucker',
'email': 'bvdrucker@gmail.com',
'date': new Date('2014-11-20T14:39:01.000Z')
},
committer: {
'name': 'Ben Drucker',
'email': 'bvdrucker@gmail.com',
'date': new Date('2014-11-20T14:39:01.000Z')
},
subject: 'Initial commit',
body: 'The commit body'
}
author.date
and commiter.date
are Date
objects while all other values are strings.
If you just want an array of commits, use stream-to-array to wrap the returned stream.
log.fields
-> Object
Commit objects contain the most frequently used commit information. However, the field mappings used to format and then parse log output can be amended before calling the parser. Consult the full range of formatting placeholders and add the placeholder to the object tree if you wish to add extra fields.
Get all commits from earlier than an hour ago and stream them to stdout
as pretty-printed JSON
var log = require('git-log-parser');
var through2 = require('through2');
log.parse({
before: new Date(Date.now() - 60 * 60 * 1000)
})
.pipe(through2.obj(function (chunk, enc, callback) {
callback(null, JSON.stringify(chunk, undefined, 2));
}))
.pipe(process.stdout);
Note that before
is stringified and passed directly as an argument to git log
. No special handling is required for any standard git log
option. You can filter by committer, time, or any other field supported by git log
.
FAQs
git-log-parser
The npm package git-log-parser receives a total of 956,815 weekly downloads. As such, git-log-parser popularity was classified as popular.
We found that git-log-parser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.
Security News
A senior white house official is urging insurers to stop covering ransomware payments, indicating possible stricter regulations to deter cybercrime.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.