Skip to content

Commit

Permalink
Merge pull request #1271 from campersau/gitparser
Browse files Browse the repository at this point in the history
fix gitparser for merge / empty commits
  • Loading branch information
campersau authored Feb 3, 2020
2 parents 621a04f + 06d7a45 commit 41fc612
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 16 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
We are following the [Keep a Changelog](https://keepachangelog.com/) format.

## [Unreleased](https://github.com/FredrikNoren/ungit/compare/v1.5.2...master)
## [Unreleased](https://github.com/FredrikNoren/ungit/compare/v1.5.3...master)

## [1.5.3](https://github.com/FredrikNoren/ungit/compare/v1.5.2...v1.5.3)

### Fixed
- Git log for merge / empty commits does not work correctly [#1270](https://github.com/FredrikNoren/ungit/issues/1270)

## [1.5.2](https://github.com/FredrikNoren/ungit/compare/v1.5.1...v1.5.2)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ungit",
"author": "Fredrik Norén <[email protected]>",
"description": "Git made easy",
"version": "1.5.2",
"version": "1.5.3",
"ungitPluginApiVersion": "0.2.0",
"scripts": {
"start": "node ./bin/ungit",
Expand Down
19 changes: 7 additions & 12 deletions source/git-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ exports.parseGitLog = (data) => {
let currentCommmit;
const parseCommitLine = (row) => {
if (!row.trim()) return;
currentCommmit = { refs: [], fileLineDiffs: [] };
currentCommmit = { refs: [], fileLineDiffs: [], total: { additions: 0, deletions: 0 } };
const refStartIndex = row.indexOf('(');
const sha1s = row.substring(0, refStartIndex < 0 ? row.length : refStartIndex).split(' ').slice(1).filter((sha1) => { return sha1 && sha1.length; });
currentCommmit.sha1 = sha1s[0];
Expand All @@ -145,17 +145,17 @@ exports.parseGitLog = (data) => {
}
}
const parseCommitMessage = (row, index) => {
if (currentCommmit.message) currentCommmit.message += '\n';
else currentCommmit.message = '';
currentCommmit.message += row.trim();
if (/[\d-]+\t[\d-]+\t.+/g.test(rows[index + 1])) {
parser = parseFileChanges;
return;
}
if (rows[index + 1] && rows[index + 1].indexOf('commit ') == 0) {
if (rows[index + 1] && rows[index + 1].indexOf('\x00commit ') == 0) {
parser = parseCommitLine;
return;
}
if (currentCommmit.message) currentCommmit.message += '\n';
else currentCommmit.message = '';
currentCommmit.message += row.trim();
}
const parseFileChanges = (row, index) => {
// git log is using -z so all the file changes are on one line
Expand Down Expand Up @@ -184,19 +184,14 @@ exports.parseGitLog = (data) => {
});
}
const nextRow = row.slice(fileChangeRegex.lastIndex + 1);
const total = {
additions: 0,
deletions: 0
};
for (let fileLineDiff of currentCommmit.fileLineDiffs) {
if (!isNaN(parseInt(fileLineDiff.additions, 10))) {
total.additions += fileLineDiff.additions = parseInt(fileLineDiff.additions, 10);
currentCommmit.total.additions += fileLineDiff.additions = parseInt(fileLineDiff.additions, 10);
}
if (!isNaN(parseInt(fileLineDiff.deletions, 10))) {
total.deletions += fileLineDiff.deletions = parseInt(fileLineDiff.deletions, 10);
currentCommmit.total.deletions += fileLineDiff.deletions = parseInt(fileLineDiff.deletions, 10);
}
}
currentCommmit.total = total;
parser = parseCommitLine;
if (nextRow) {
parser(nextRow, index);
Expand Down
107 changes: 106 additions & 1 deletion test/spec.git-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ describe('git-parser parseGitLog', () => {
expect(gitParser.parseGitLog(gitLog)[0]).to.eql({
authorName: "Test ungit",
committerName: "Test ungit",
total: {
"additions": 0,
"deletions": 0
},
fileLineDiffs: [],
isHead: true,
message: "",
Expand Down Expand Up @@ -201,7 +205,24 @@ describe('git-parser parseGitLog', () => {
submodules parser
32 0 test/spec.git-parser.js\x00\x00`)
32 0 test/spec.git-parser.js\x00\x00commit 02efa0da7b1eccb1e0f1c2ff0433ce7387738f60 985617e19e30e9abe0a5711bf455f0dc10f97dff
Author: Test ungit <[email protected]>
AuthorDate: Fri Jan 4 14:02:56 2019 +0100
Commit: Test ungit <[email protected]>
CommitDate: Fri Jan 4 14:02:56 2019 +0100
empty commit
\x00commit 621a04f931ea9007ac826c04a1a02832e20aa470 4e5d611fdad85bcad44abf65936c95f748abef4e e2dc3ef6e2cbf6ab0acb456c0837257dc01baafd
Merge: 4e5d611f e2dc3ef6
Author: Test ungit <[email protected]>
AuthorDate: Fri Jan 4 14:01:56 2019 +0100
Commit: Test ungit <[email protected]>
CommitDate: Fri Jan 4 14:01:56 2019 +0100
Merge pull request #1268 from campersau/prepare_152
Prepare version 1.5.2
\x004 1 CHANGELOG.md\x001 1 package-lock.json\x001 1 package.json\x008 6 source/git-parser.js\x00\x00`)

const res = gitParser.parseGitLog(gitLog)
expect(res[0]).to.eql({
Expand Down Expand Up @@ -273,6 +294,82 @@ describe('git-parser parseGitLog', () => {
refs: [],
sha1: "37d1154434b70854ed243967e0d7e37aa3564551"
})
// empty commit
expect(res[2]).to.eql({
authorDate: "Fri Jan 4 14:02:56 2019 +0100",
authorEmail: "[email protected]",
authorName: "Test ungit",
commitDate: "Fri Jan 4 14:02:56 2019 +0100",
committerEmail: "[email protected]",
committerName: "Test ungit",
total: {
"additions": 0,
"deletions": 0
},
fileLineDiffs: [],
isHead: false,
message: "empty commit",
parents: [
"985617e19e30e9abe0a5711bf455f0dc10f97dff",
],
refs: [],
sha1: "02efa0da7b1eccb1e0f1c2ff0433ce7387738f60"
})
// merge commit
expect(res[3]).to.eql({
authorDate: "Fri Jan 4 14:01:56 2019 +0100",
authorEmail: "[email protected]",
authorName: "Test ungit",
commitDate: "Fri Jan 4 14:01:56 2019 +0100",
committerEmail: "[email protected]",
committerName: "Test ungit",
total: {
"additions": 14,
"deletions": 9
},
fileLineDiffs: [
{
"additions": 4,
"deletions": 1,
"displayName": "CHANGELOG.md",
"fileName": "CHANGELOG.md",
"oldFileName": "CHANGELOG.md",
"type": "text"
},
{
"additions": 1,
"deletions": 1,
"displayName": "package-lock.json",
"fileName": "package-lock.json",
"oldFileName": "package-lock.json",
"type": "text"
},
{
"additions": 1,
"deletions": 1,
"displayName": "package.json",
"fileName": "package.json",
"oldFileName": "package.json",
"type": "text"
},
{
"additions": 8,
"deletions": 6,
"displayName": "source/git-parser.js",
"fileName": "source/git-parser.js",
"oldFileName": "source/git-parser.js",
"type": "text"
}
],
isHead: false,
message: "Merge pull request #1268 from campersau/prepare_152\n\nPrepare version 1.5.2",
parents: [
"4e5d611fdad85bcad44abf65936c95f748abef4e",
"e2dc3ef6e2cbf6ab0acb456c0837257dc01baafd",
],
refs: [],
sha1: "621a04f931ea9007ac826c04a1a02832e20aa470"
})
});
it('parses reflog commits without email', () => {
const gitLog = dedent(`
Expand Down Expand Up @@ -388,6 +485,10 @@ describe('git-parser parseGitLog', () => {
expect(gitParser.parseGitLog(gitLog)[0]).to.eql({
authorEmail: "[email protected]",
authorName: "Test Ungit",
total: {
"additions": 0,
"deletions": 0
},
fileLineDiffs: [],
isHead: true,
message: "",
Expand All @@ -414,6 +515,10 @@ describe('git-parser parseGitLog', () => {
expect(gitParser.parseGitLog(gitLog)[0]).to.eql({
authorEmail: "[email protected]",
authorName: "Test Ungit",
total: {
"additions": 0,
"deletions": 0
},
fileLineDiffs: [],
isHead: true,
message: "",
Expand Down

0 comments on commit 41fc612

Please sign in to comment.