From adc3ad94a1e3c9e1595b37c7f431f42d13937e80 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 15 Apr 2015 10:32:58 -0700 Subject: [PATCH 1/3] GitCommit.raw_log only drop last msg line if blank --- lib/git/internal/git_commit.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/git/internal/git_commit.js b/lib/git/internal/git_commit.js index 6c6cb77..d96697c 100644 --- a/lib/git/internal/git_commit.js +++ b/lib/git/internal/git_commit.js @@ -65,9 +65,15 @@ GitCommit.from_raw = function(raw_object, repository) { GitCommit.prototype.raw_log = function(sha1) { var output = "commit " + sha1 + "\n"; output = output + this.headers + "\n\n"; - var lines = this.message.split("\n"); - // Remove the last line which will be empty - for(var i = 0; i < (lines.length > 1 ? lines.length - 1 : lines.length); i++) { + var lines = this.message.split("\n"); + var length = lines.length; + + // drop last line if it's blank + if (length > 0 && lines[length - 1].length === 0) { + length -= 1; + } + + for(var i = 0; i < length; i++) { output = output + ' ' + lines[i] + '\n'; } // Return the output From add82b54610c7d33005fb83f247842982555b1b9 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Thu, 16 Apr 2015 09:33:52 -0700 Subject: [PATCH 2/3] nits: length-- and output += --- lib/git/internal/git_commit.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/git/internal/git_commit.js b/lib/git/internal/git_commit.js index d96697c..023ce41 100644 --- a/lib/git/internal/git_commit.js +++ b/lib/git/internal/git_commit.js @@ -70,11 +70,11 @@ GitCommit.prototype.raw_log = function(sha1) { // drop last line if it's blank if (length > 0 && lines[length - 1].length === 0) { - length -= 1; + length--; } for(var i = 0; i < length; i++) { - output = output + ' ' + lines[i] + '\n'; + output += ' ' + lines[i] + '\n'; } // Return the output return output + '\n'; From c88e44a22a6a81aabf1984c5881d53ce3df1f713 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Thu, 16 Apr 2015 09:35:45 -0700 Subject: [PATCH 3/3] Add unit tests for GitCommit.raw_log --- test/test_git_basic.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/test/test_git_basic.js b/test/test_git_basic.js index 7ac1ef6..147c664 100644 --- a/test/test_git_basic.js +++ b/test/test_git_basic.js @@ -2,7 +2,7 @@ var testCase = require('nodeunit').testCase, Repo = require('../lib/git').Repo, Git = require('../lib/git').Git, fs = require('fs'), - Commit = require('../lib/git').Commit, + Commit = require('../lib/git/internal/git_commit').GitCommit, Blob = require('../lib/git').Blob, GitFileOperations = require('../lib/git').GitFileOperations; @@ -24,6 +24,39 @@ module.exports = testCase({ callback(); }, + "GitCommit.raw_log eliminates final empty line":function(assert) { + var message = 'first line\nsecond line\n'; + var headers = 'headers'; + var sha = 'SHA'; + + var expected = 'commit SHA\n' + + 'headers\n\n' + + ' first line\n' + + ' second line\n\n'; + + var commit = new Commit(null, null, null, null, message, headers); + + assert.equal(expected, commit.raw_log(sha)); + assert.done(); + }, + + "GitCommit.raw_log does not drop final line if non-empty":function(assert) { + var message = 'first line\nsecond line\nthird line'; + var headers = 'headers'; + var sha = 'SHA'; + + var expected = 'commit SHA\n' + + 'headers\n\n' + + ' first line\n' + + ' second line\n' + + ' third line\n\n'; + + var commit = new Commit(null, null, null, null, message, headers); + + assert.equal(expected, commit.raw_log(sha)); + assert.done(); + }, + "Should correctly init gitdir":function(assert) { var tmp_path = './test/tmp'; GitFileOperations.fs_rmdir_r(tmp_path, function(err, result) {