Skip to content

Commit 60976b1

Browse files
AtkinsSJKernelDeimos
authored andcommitted
feat(git): Format output closer to canonical git
Make some parts colored or bold, and indent commit messages.
1 parent 9551544 commit 60976b1

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

packages/git/src/format.js

+22-21
Original file line numberDiff line numberDiff line change
@@ -117,58 +117,59 @@ export const format_timestamp_and_offset = (owner) => {
117117
*/
118118
export const format_commit = (commit, oid, options = {}) => {
119119
const title_line = () => commit.message.split('\n')[0];
120+
const indent = (text) => text.split('\n').map(it => ` ${it}`).join('\n');
120121

121122
switch (options.format || 'medium') {
122123
// TODO: Other formats
123124
case 'oneline':
124-
return `${format_oid(oid, options)} ${title_line()}`;
125+
return `${chalk.yellow(format_oid(oid, options))} ${title_line()}`;
125126
case 'short': {
126127
let s = '';
127-
s += `commit ${format_oid(oid, options)}\n`;
128+
s += chalk.yellow(`commit ${format_oid(oid, options)}\n`);
128129
s += `Author: ${format_person(commit.author)}\n`;
129130
s += '\n';
130-
s += title_line();
131+
s += indent(title_line());
131132
return s;
132133
}
133134
case 'medium': {
134135
let s = '';
135-
s += `commit ${format_oid(oid, options)}\n`;
136+
s += chalk.yellow(`commit ${format_oid(oid, options)}\n`);
136137
s += `Author: ${format_person(commit.author)}\n`;
137138
s += `Date: ${format_date(commit.author)}\n`;
138139
s += '\n';
139-
s += commit.message;
140+
s += indent(commit.message);
140141
return s;
141142
}
142143
case 'full': {
143144
let s = '';
144-
s += `commit ${format_oid(oid, options)}\n`;
145+
s += chalk.yellow(`commit ${format_oid(oid, options)}\n`);
145146
s += `Author: ${format_person(commit.author)}\n`;
146147
s += `Commit: ${format_person(commit.committer)}\n`;
147148
s += '\n';
148-
s += commit.message;
149+
s += indent(commit.message);
149150
return s;
150151
}
151152
case 'fuller': {
152153
let s = '';
153-
s += `commit ${format_oid(oid, options)}\n`;
154+
s += chalk.yellow(`commit ${format_oid(oid, options)}\n`);
154155
s += `Author: ${format_person(commit.author)}\n`;
155156
s += `AuthorDate: ${format_date(commit.author)}\n`;
156157
s += `Commit: ${format_person(commit.committer)}\n`;
157158
s += `CommitDate: ${format_date(commit.committer)}\n`;
158159
s += '\n';
159-
s += commit.message;
160+
s += indent(commit.message);
160161
return s;
161162
}
162163
case 'raw': {
163164
let s = '';
164-
s += `commit ${oid}\n`;
165+
s += chalk.yellow(`commit ${oid}\n`);
165166
s += `tree ${commit.tree}\n`;
166167
if (commit.parent[0])
167168
s += `parent ${commit.parent[0]}\n`;
168169
s += `author ${format_person(commit.author)} ${format_timestamp_and_offset(commit.author)}\n`;
169170
s += `committer ${format_person(commit.committer)} ${format_timestamp_and_offset(commit.committer)}\n`;
170171
s += '\n';
171-
s += commit.message;
172+
s += indent(commit.message);
172173
return s;
173174
}
174175
default: {
@@ -186,7 +187,7 @@ export const format_commit = (commit, oid, options = {}) => {
186187
*/
187188
export const format_tree = (oid, tree, options = {}) => {
188189
let s = '';
189-
s += `tree ${oid}\n`;
190+
s += chalk.yellow(`tree ${oid}\n`);
190191
s += '\n';
191192
for (const tree_entry of tree) {
192193
s += `${tree_entry.path}\n`;
@@ -205,7 +206,7 @@ export const format_tree = (oid, tree, options = {}) => {
205206
*/
206207
export const format_tag = (tag, options = {}) => {
207208
let s = '';
208-
s += `tag ${tag.tag}\n`;
209+
s += chalk.yellow(`tag ${tag.tag}\n`);
209210
s += `Tagger: ${format_person(tag.tagger)}\n`;
210211
s += `Date: ${format_date(tag.tagger, options)}\n`;
211212
if (tag.message) {
@@ -410,23 +411,23 @@ export const format_diffs = (diffs, options) => {
410411

411412
// NOTE: This first line shows `a/$newFileName` for files that are new, not `/dev/null`.
412413
const first_line_a_path = a_path !== '/dev/null' ? a_path : `${options.source_prefix}${diff.newFileName}`;
413-
s += `diff --git ${first_line_a_path} ${b_path}\n`;
414+
s += chalk.bold(`diff --git ${first_line_a_path} ${b_path}\n`);
414415
if (a.mode === b.mode) {
415-
s += `index ${shorten_hash(a.oid)}..${shorten_hash(b.oid)} ${a.mode}`;
416+
s += chalk.bold(`index ${shorten_hash(a.oid)}..${shorten_hash(b.oid)} ${a.mode}\n`);
416417
} else {
417418
if (a.mode === '000000') {
418-
s += `new file mode ${b.mode}\n`;
419+
s += chalk.bold(`new file mode ${b.mode}\n`);
419420
} else {
420-
s += `old mode ${a.mode}\n`;
421-
s += `new mode ${b.mode}\n`;
421+
s += chalk.bold(`old mode ${a.mode}\n`);
422+
s += chalk.bold(`new mode ${b.mode}\n`);
422423
}
423-
s += `index ${shorten_hash(a.oid)}..${shorten_hash(b.oid)}\n`;
424+
s += chalk.bold(`index ${shorten_hash(a.oid)}..${shorten_hash(b.oid)}\n`);
424425
}
425426
if (!diff.hunks.length)
426427
continue;
427428

428-
s += `--- ${a_path}\n`;
429-
s += `+++ ${b_path}\n`;
429+
s += chalk.bold(`--- ${a_path}\n`);
430+
s += chalk.bold(`+++ ${b_path}\n`);
430431

431432
for (const hunk of diff.hunks) {
432433
s += chalk.blueBright(`@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@\n`);

packages/git/src/subcommands/show.js

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export default {
8080
read_a: read_tree,
8181
read_b: read_tree,
8282
});
83+
s += '\n';
8384
s += format_diffs(diffs, diff_options);
8485
}
8586
return s;

0 commit comments

Comments
 (0)