UNPKG

git-patch-additions

Version:
82 lines (74 loc) 2.8 kB
/* * The MIT License * * Copyright (c) 2016 Juan Cruz Viotti. https://github.com/jviotti * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /** * @module gitPatchAdditions */ var _ = require('lodash'); _.str = require('underscore.string'); var gitPatchParser = require('git-patch-parser'); /** * @summary Get patch additions * @function * @public * * @param {String} patch - git patch * @returns {Object[]} additions * * @example * var patch = fs.readFileSync('path/to/mypatch.patch', { * encoding: 'utf8' * }); * * var additions = gitPatchAdditions.get(patch); * * additions.forEach(function(addition) { * console.log('The addition text is:', addition.content); * console.log('The addition commit is:', addition.commit); * console.log('The addition was made in the following file:', addition.file); * console.log('The addition was made in the following line:', addition.line); * }); */ exports.get = function(patch) { var parsedPatch = gitPatchParser.parseMultiPatch(patch); return _.flattenDeep(_.map(parsedPatch, function(singlePatch) { return _.map(singlePatch.files, function(changes, fileName) { return _.map(changes, function(change) { var additions = _.filter(change.lines, { type: 'added' }); additions = _.reject(additions, function(addition) { // Bypass empty strings and strings containing // only spaces for efficiency. return _.str.isBlank(addition.content); }); return _.map(additions, function(addition) { return { content: addition.content, file: fileName, commit: singlePatch.sha, line: _.indexOf(change.lines, addition) + 1 }; }); }); }); })); };