18
18
*/
19
19
import { shorten_hash } from './git-helpers.js' ;
20
20
import chalk from 'chalk' ;
21
+ import { get_matching_refs } from './refs.js' ;
21
22
22
23
export const commit_formatting_options = {
23
24
'abbrev-commit' : {
@@ -68,9 +69,35 @@ export const process_commit_formatting_options = (options) => {
68
69
* @param short_hashes Whwther to shorten the hash
69
70
* @returns {String }
70
71
*/
71
- export const format_oid = async ( git_context , oid , { short_hashes = false } = { } ) => {
72
- // TODO: List refs at this commit, after the hash
73
- return short_hashes ? shorten_hash ( git_context , oid ) : oid ;
72
+ export const format_commit_oid = async ( git_context , oid , { short_hashes = false } = { } ) => {
73
+ const hash = short_hashes ? await shorten_hash ( git_context , oid ) : oid ;
74
+
75
+ const refs = await get_matching_refs ( git_context , oid ) ;
76
+ if ( refs . length === 0 )
77
+ return hash ;
78
+
79
+ let s = `${ hash } (` ;
80
+ s += refs . map ( ref => {
81
+ // Different kinds of ref are styled differently, but all are in bold:
82
+ // HEAD and local branches are cyan
83
+ if ( ref === 'HEAD' ) {
84
+ // TODO: If HEAD points to another ref, that should be shown here as `HEAD -> other`
85
+ return chalk . bold . cyan ( ref ) ;
86
+ }
87
+ if ( ref . startsWith ( 'refs/heads/' ) )
88
+ return chalk . bold . cyanBright ( ref . slice ( 'refs/heads/' . length ) ) ;
89
+ // Tags are `tag: foo` in yellow
90
+ if ( ref . startsWith ( 'refs/tags/' ) )
91
+ return chalk . bold . yellowBright ( `tag: ${ ref . slice ( 'refs/tags/' . length ) } ` ) ;
92
+ // Remote branches are red
93
+ if ( ref . startsWith ( 'refs/remotes/' ) )
94
+ return chalk . bold . red ( ref . slice ( 'refs/remotes/' . length ) ) ;
95
+ // Assuming there's anything else, we'll just bold it.
96
+ return chalk . bold ( ref ) ;
97
+ } ) . join ( ', ' ) ;
98
+ s += ')' ;
99
+
100
+ return s ;
74
101
}
75
102
76
103
/**
@@ -124,18 +151,18 @@ export const format_commit = async (git_context, commit, oid, options = {}) => {
124
151
switch ( options . format || 'medium' ) {
125
152
// TODO: Other formats
126
153
case 'oneline' :
127
- return `${ chalk . yellow ( await format_oid ( git_context , oid , options ) ) } ${ title_line ( ) } ` ;
154
+ return `${ chalk . yellow ( await format_commit_oid ( git_context , oid , options ) ) } ${ title_line ( ) } ` ;
128
155
case 'short' : {
129
156
let s = '' ;
130
- s += chalk . yellow ( `commit ${ await format_oid ( git_context , oid , options ) } \n` ) ;
157
+ s += chalk . yellow ( `commit ${ await format_commit_oid ( git_context , oid , options ) } \n` ) ;
131
158
s += `Author: ${ format_person ( commit . author ) } \n` ;
132
159
s += '\n' ;
133
160
s += indent ( title_line ( ) ) ;
134
161
return s ;
135
162
}
136
163
case 'medium' : {
137
164
let s = '' ;
138
- s += chalk . yellow ( `commit ${ await format_oid ( git_context , oid , options ) } \n` ) ;
165
+ s += chalk . yellow ( `commit ${ await format_commit_oid ( git_context , oid , options ) } \n` ) ;
139
166
s += `Author: ${ format_person ( commit . author ) } \n` ;
140
167
s += `Date: ${ format_date ( commit . author ) } \n` ;
141
168
s += '\n' ;
@@ -144,7 +171,7 @@ export const format_commit = async (git_context, commit, oid, options = {}) => {
144
171
}
145
172
case 'full' : {
146
173
let s = '' ;
147
- s += chalk . yellow ( `commit ${ await format_oid ( git_context , oid , options ) } \n` ) ;
174
+ s += chalk . yellow ( `commit ${ await format_commit_oid ( git_context , oid , options ) } \n` ) ;
148
175
s += `Author: ${ format_person ( commit . author ) } \n` ;
149
176
s += `Commit: ${ format_person ( commit . committer ) } \n` ;
150
177
s += '\n' ;
@@ -153,7 +180,7 @@ export const format_commit = async (git_context, commit, oid, options = {}) => {
153
180
}
154
181
case 'fuller' : {
155
182
let s = '' ;
156
- s += chalk . yellow ( `commit ${ await format_oid ( git_context , oid , options ) } \n` ) ;
183
+ s += chalk . yellow ( `commit ${ await format_commit_oid ( git_context , oid , options ) } \n` ) ;
157
184
s += `Author: ${ format_person ( commit . author ) } \n` ;
158
185
s += `AuthorDate: ${ format_date ( commit . author ) } \n` ;
159
186
s += `Commit: ${ format_person ( commit . committer ) } \n` ;
@@ -164,7 +191,7 @@ export const format_commit = async (git_context, commit, oid, options = {}) => {
164
191
}
165
192
case 'raw' : {
166
193
let s = '' ;
167
- s += chalk . yellow ( `commit ${ oid } \n` ) ;
194
+ s += chalk . yellow ( `commit ${ await format_commit_oid ( git_context , oid , options ) } \n` ) ;
168
195
s += `tree ${ commit . tree } \n` ;
169
196
if ( commit . parent [ 0 ] )
170
197
s += `parent ${ commit . parent [ 0 ] } \n` ;
0 commit comments