Skip to content

Commit 48cbf91

Browse files
committed
Merge branch 'jp/dirty-describe'
* jp/dirty-describe: Teach "git describe" --dirty option
2 parents 25dfd1b + 9f67d2e commit 48cbf91

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Documentation/git-describe.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ git-describe - Show the most recent tag that is reachable from a commit
99
SYNOPSIS
1010
--------
1111
'git describe' [--all] [--tags] [--contains] [--abbrev=<n>] <committish>...
12+
'git describe' [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]
1213

1314
DESCRIPTION
1415
-----------
@@ -27,6 +28,11 @@ OPTIONS
2728
<committish>...::
2829
Committish object names to describe.
2930

31+
--dirty[=<mark>]::
32+
Describe the working tree.
33+
It means describe HEAD and appends <mark> (`-dirty` by
34+
default) if the working tree is dirty.
35+
3036
--all::
3137
Instead of using only the annotated tags, use any ref
3238
found in `.git/refs/`. This option enables matching

builtin-describe.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
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

1213
static 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;
2325
static int found_names;
2426
static const char *pattern;
2527
static 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

2736
struct 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);

t/t6120-describe.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@ test_expect_success 'rename tag Q back to A' '
125125
test_expect_success 'pack tag refs' 'git pack-refs'
126126
check_describe A-* HEAD
127127

128+
check_describe "A-*[0-9a-f]" --dirty
129+
130+
test_expect_success 'set-up dirty work tree' '
131+
echo >>file
132+
'
133+
134+
check_describe "A-*[0-9a-f]-dirty" --dirty
135+
136+
check_describe "A-*[0-9a-f].mod" --dirty=.mod
137+
138+
test_expect_success 'describe --dirty HEAD' '
139+
test_must_fail git describe --dirty HEAD
140+
'
141+
128142
test_expect_success 'set-up matching pattern tests' '
129143
git tag -a -m test-annotated test-annotated &&
130144
echo >>file &&

0 commit comments

Comments
 (0)