Skip to content

Commit 165dc78

Browse files
committed
Merge branch 'cc/find-commit-subject'
* cc/find-commit-subject: blame: use find_commit_subject() instead of custom code merge-recursive: use find_commit_subject() instead of custom code bisect: use find_commit_subject() instead of custom code revert: rename variables related to subject in get_message() revert: refactor code to find commit subject in find_commit_subject() revert: fix off by one read when searching the end of a commit subject
2 parents 29e1353 + ad98a58 commit 165dc78

File tree

7 files changed

+62
-49
lines changed

7 files changed

+62
-49
lines changed

bisect.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ static void show_list(const char *debug, int counted, int nr,
141141
enum object_type type;
142142
unsigned long size;
143143
char *buf = read_sha1_file(commit->object.sha1, &type, &size);
144-
char *ep, *sp;
144+
const char *subject_start;
145+
int subject_len;
145146

146147
fprintf(stderr, "%c%c%c ",
147148
(flags & TREESAME) ? ' ' : 'T',
@@ -156,13 +157,9 @@ static void show_list(const char *debug, int counted, int nr,
156157
fprintf(stderr, " %.*s", 8,
157158
sha1_to_hex(pp->item->object.sha1));
158159

159-
sp = strstr(buf, "\n\n");
160-
if (sp) {
161-
sp += 2;
162-
for (ep = sp; *ep && *ep != '\n'; ep++)
163-
;
164-
fprintf(stderr, " %.*s", (int)(ep - sp), sp);
165-
}
160+
subject_len = find_commit_subject(buf, &subject_start);
161+
if (subject_len)
162+
fprintf(stderr, " %.*s", subject_len, subject_start);
166163
fprintf(stderr, "\n");
167164
}
168165
}

builtin/blame.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,8 @@ static void get_commit_info(struct commit *commit,
14071407
int detailed)
14081408
{
14091409
int len;
1410-
char *tmp, *endp, *reencoded, *message;
1410+
const char *subject;
1411+
char *reencoded, *message;
14111412
static char author_name[1024];
14121413
static char author_mail[1024];
14131414
static char committer_name[1024];
@@ -1449,22 +1450,13 @@ static void get_commit_info(struct commit *commit,
14491450
&ret->committer_time, &ret->committer_tz);
14501451

14511452
ret->summary = summary_buf;
1452-
tmp = strstr(message, "\n\n");
1453-
if (!tmp) {
1454-
error_out:
1453+
len = find_commit_subject(message, &subject);
1454+
if (len && len < sizeof(summary_buf)) {
1455+
memcpy(summary_buf, subject, len);
1456+
summary_buf[len] = 0;
1457+
} else {
14551458
sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
1456-
free(reencoded);
1457-
return;
14581459
}
1459-
tmp += 2;
1460-
endp = strchr(tmp, '\n');
1461-
if (!endp)
1462-
endp = tmp + strlen(tmp);
1463-
len = endp - tmp;
1464-
if (len >= sizeof(summary_buf) || len == 0)
1465-
goto error_out;
1466-
memcpy(summary_buf, tmp, len);
1467-
summary_buf[len] = 0;
14681460
free(reencoded);
14691461
}
14701462

builtin/revert.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ struct commit_message {
102102
static int get_message(const char *raw_message, struct commit_message *out)
103103
{
104104
const char *encoding;
105-
const char *p, *abbrev, *eol;
105+
const char *abbrev, *subject;
106+
int abbrev_len, subject_len;
106107
char *q;
107-
int abbrev_len, oneline_len;
108108

109109
if (!raw_message)
110110
return -1;
@@ -125,27 +125,17 @@ static int get_message(const char *raw_message, struct commit_message *out)
125125
abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
126126
abbrev_len = strlen(abbrev);
127127

128-
/* Find beginning and end of commit subject. */
129-
p = out->message;
130-
while (*p && (*p != '\n' || p[1] != '\n'))
131-
p++;
132-
if (*p) {
133-
p += 2;
134-
for (eol = p + 1; *eol && *eol != '\n'; eol++)
135-
; /* do nothing */
136-
} else
137-
eol = p;
138-
oneline_len = eol - p;
128+
subject_len = find_commit_subject(out->message, &subject);
139129

140130
out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
141-
strlen("... ") + oneline_len + 1);
131+
strlen("... ") + subject_len + 1);
142132
q = out->parent_label;
143133
q = mempcpy(q, "parent of ", strlen("parent of "));
144134
out->label = q;
145135
q = mempcpy(q, abbrev, abbrev_len);
146136
q = mempcpy(q, "... ", strlen("... "));
147137
out->subject = q;
148-
q = mempcpy(q, p, oneline_len);
138+
q = mempcpy(q, subject, subject_len);
149139
*q = '\0';
150140
return 0;
151141
}

commit.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,25 @@ int parse_commit(struct commit *item)
315315
return ret;
316316
}
317317

318+
int find_commit_subject(const char *commit_buffer, const char **subject)
319+
{
320+
const char *eol;
321+
const char *p = commit_buffer;
322+
323+
while (*p && (*p != '\n' || p[1] != '\n'))
324+
p++;
325+
if (*p) {
326+
p += 2;
327+
for (eol = p; *eol && *eol != '\n'; eol++)
328+
; /* do nothing */
329+
} else
330+
eol = p;
331+
332+
*subject = p;
333+
334+
return eol - p;
335+
}
336+
318337
struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
319338
{
320339
struct commit_list *new_list = xmalloc(sizeof(struct commit_list));

commit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
4141

4242
int parse_commit(struct commit *item);
4343

44+
/* Find beginning and length of commit subject. */
45+
int find_commit_subject(const char *commit_buffer, const char **subject);
46+
4447
struct commit_list * commit_list_insert(struct commit *item, struct commit_list **list_p);
4548
unsigned commit_list_count(const struct commit_list *l);
4649
struct commit_list * insert_by_date(struct commit *item, struct commit_list **list);

merge-recursive.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,10 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
136136
if (parse_commit(commit) != 0)
137137
printf("(bad commit)\n");
138138
else {
139-
const char *s;
140-
int len;
141-
for (s = commit->buffer; *s; s++)
142-
if (*s == '\n' && s[1] == '\n') {
143-
s += 2;
144-
break;
145-
}
146-
for (len = 0; s[len] && '\n' != s[len]; len++)
147-
; /* do nothing */
148-
printf("%.*s\n", len, s);
139+
const char *title;
140+
int len = find_commit_subject(commit->buffer, &title);
141+
if (len)
142+
printf("%.*s\n", len, title);
149143
}
150144
}
151145
}

t/t3505-cherry-pick-empty.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,29 @@ test_expect_success setup '
1313
1414
git checkout -b empty-branch &&
1515
test_tick &&
16-
git commit --allow-empty -m "empty"
16+
git commit --allow-empty -m "empty" &&
17+
18+
echo third >> file1 &&
19+
git add file1 &&
20+
test_tick &&
21+
git commit --allow-empty-message -m ""
1722
1823
'
1924

2025
test_expect_success 'cherry-pick an empty commit' '
26+
git checkout master && {
27+
git cherry-pick empty-branch^
28+
test "$?" = 1
29+
}
30+
'
31+
32+
test_expect_success 'index lockfile was removed' '
33+
34+
test ! -f .git/index.lock
35+
36+
'
37+
38+
test_expect_success 'cherry-pick a commit with an empty message' '
2139
git checkout master && {
2240
git cherry-pick empty-branch
2341
test "$?" = 1

0 commit comments

Comments
 (0)