Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitCommit.raw_log only drop last msg line if blank #42

Merged
merged 3 commits into from
Apr 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/git/internal/git_commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ 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++) {
output = output + ' ' + lines[i] + '\n';
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--;
}

for(var i = 0; i < length; i++) {
output += ' ' + lines[i] + '\n';
}
// Return the output
return output + '\n';
Expand Down
35 changes: 34 additions & 1 deletion test/test_git_basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down