55#include "builtin.h"
66#include "exec_cmd.h"
77#include "parse-options.h"
8+ #include "diff.h"
89
910#define SEEN (1u<<0)
1011#define MAX_TAGS (FLAG_BITS - 1)
1112
1213static const char * const describe_usage [] = {
1314 "git describe [options] <committish>*" ,
15+ "git describe [options] --dirty" ,
1416 NULL
1517};
1618
@@ -23,6 +25,13 @@ static int max_candidates = 10;
2325static int found_names ;
2426static const char * pattern ;
2527static int always ;
28+ static const char * dirty ;
29+
30+ /* diff-index command arguments to check if working tree is dirty. */
31+ static const char * diff_index_args [] = {
32+ "diff-index" , "--quiet" , "HEAD" , "--" , NULL
33+ };
34+
2635
2736struct commit_name {
2837 struct tag * tag ;
@@ -199,6 +208,8 @@ static void describe(const char *arg, int last_one)
199208 display_name (n );
200209 if (longformat )
201210 show_suffix (0 , n -> tag ? n -> tag -> tagged -> sha1 : sha1 );
211+ if (dirty )
212+ printf ("%s" , dirty );
202213 printf ("\n" );
203214 return ;
204215 }
@@ -256,7 +267,10 @@ static void describe(const char *arg, int last_one)
256267 if (!match_cnt ) {
257268 const unsigned char * sha1 = cmit -> object .sha1 ;
258269 if (always ) {
259- printf ("%s\n" , find_unique_abbrev (sha1 , abbrev ));
270+ printf ("%s" , find_unique_abbrev (sha1 , abbrev ));
271+ if (dirty )
272+ printf ("%s" , dirty );
273+ printf ("\n" );
260274 return ;
261275 }
262276 die ("cannot describe '%s'" , sha1_to_hex (sha1 ));
@@ -291,6 +305,8 @@ static void describe(const char *arg, int last_one)
291305 display_name (all_matches [0 ].name );
292306 if (abbrev )
293307 show_suffix (all_matches [0 ].depth , cmit -> object .sha1 );
308+ if (dirty )
309+ printf ("%s" , dirty );
294310 printf ("\n" );
295311
296312 if (!last_one )
@@ -315,6 +331,9 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
315331 "only consider tags matching <pattern>" ),
316332 OPT_BOOLEAN (0 , "always" , & always ,
317333 "show abbreviated commit object as fallback" ),
334+ {OPTION_STRING , 0 , "dirty" , & dirty , "mark" ,
335+ "append <mark> on dirty working tree (default: \"-dirty\")" ,
336+ PARSE_OPT_OPTARG , NULL , (intptr_t ) "-dirty" },
318337 OPT_END (),
319338 };
320339
@@ -355,7 +374,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
355374 die ("No names found, cannot describe anything." );
356375
357376 if (argc == 0 ) {
377+ if (dirty && !cmd_diff_index (ARRAY_SIZE (diff_index_args ) - 1 , diff_index_args , prefix ))
378+ dirty = NULL ;
358379 describe ("HEAD" , 1 );
380+ } else if (dirty ) {
381+ die ("--dirty is incompatible with committishes" );
359382 } else {
360383 while (argc -- > 0 ) {
361384 describe (* argv ++ , argc == 0 );
0 commit comments