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
This commit is contained in:
Junio C Hamano 2010-08-18 12:46:55 -07:00
Родитель 29e1353a7d ad98a58b3d
Коммит 165dc789d5
7 изменённых файлов: 62 добавлений и 49 удалений

Просмотреть файл

@ -141,7 +141,8 @@ static void show_list(const char *debug, int counted, int nr,
enum object_type type;
unsigned long size;
char *buf = read_sha1_file(commit->object.sha1, &type, &size);
char *ep, *sp;
const char *subject_start;
int subject_len;
fprintf(stderr, "%c%c%c ",
(flags & TREESAME) ? ' ' : 'T',
@ -156,13 +157,9 @@ static void show_list(const char *debug, int counted, int nr,
fprintf(stderr, " %.*s", 8,
sha1_to_hex(pp->item->object.sha1));
sp = strstr(buf, "\n\n");
if (sp) {
sp += 2;
for (ep = sp; *ep && *ep != '\n'; ep++)
;
fprintf(stderr, " %.*s", (int)(ep - sp), sp);
}
subject_len = find_commit_subject(buf, &subject_start);
if (subject_len)
fprintf(stderr, " %.*s", subject_len, subject_start);
fprintf(stderr, "\n");
}
}

Просмотреть файл

@ -1407,7 +1407,8 @@ static void get_commit_info(struct commit *commit,
int detailed)
{
int len;
char *tmp, *endp, *reencoded, *message;
const char *subject;
char *reencoded, *message;
static char author_name[1024];
static char author_mail[1024];
static char committer_name[1024];
@ -1449,22 +1450,13 @@ static void get_commit_info(struct commit *commit,
&ret->committer_time, &ret->committer_tz);
ret->summary = summary_buf;
tmp = strstr(message, "\n\n");
if (!tmp) {
error_out:
len = find_commit_subject(message, &subject);
if (len && len < sizeof(summary_buf)) {
memcpy(summary_buf, subject, len);
summary_buf[len] = 0;
} else {
sprintf(summary_buf, "(%s)", sha1_to_hex(commit->object.sha1));
free(reencoded);
return;
}
tmp += 2;
endp = strchr(tmp, '\n');
if (!endp)
endp = tmp + strlen(tmp);
len = endp - tmp;
if (len >= sizeof(summary_buf) || len == 0)
goto error_out;
memcpy(summary_buf, tmp, len);
summary_buf[len] = 0;
free(reencoded);
}

Просмотреть файл

@ -102,9 +102,9 @@ struct commit_message {
static int get_message(const char *raw_message, struct commit_message *out)
{
const char *encoding;
const char *p, *abbrev, *eol;
const char *abbrev, *subject;
int abbrev_len, subject_len;
char *q;
int abbrev_len, oneline_len;
if (!raw_message)
return -1;
@ -125,27 +125,17 @@ static int get_message(const char *raw_message, struct commit_message *out)
abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
abbrev_len = strlen(abbrev);
/* Find beginning and end of commit subject. */
p = out->message;
while (*p && (*p != '\n' || p[1] != '\n'))
p++;
if (*p) {
p += 2;
for (eol = p + 1; *eol && *eol != '\n'; eol++)
; /* do nothing */
} else
eol = p;
oneline_len = eol - p;
subject_len = find_commit_subject(out->message, &subject);
out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
strlen("... ") + oneline_len + 1);
strlen("... ") + subject_len + 1);
q = out->parent_label;
q = mempcpy(q, "parent of ", strlen("parent of "));
out->label = q;
q = mempcpy(q, abbrev, abbrev_len);
q = mempcpy(q, "... ", strlen("... "));
out->subject = q;
q = mempcpy(q, p, oneline_len);
q = mempcpy(q, subject, subject_len);
*q = '\0';
return 0;
}

Просмотреть файл

@ -315,6 +315,25 @@ int parse_commit(struct commit *item)
return ret;
}
int find_commit_subject(const char *commit_buffer, const char **subject)
{
const char *eol;
const char *p = commit_buffer;
while (*p && (*p != '\n' || p[1] != '\n'))
p++;
if (*p) {
p += 2;
for (eol = p; *eol && *eol != '\n'; eol++)
; /* do nothing */
} else
eol = p;
*subject = p;
return eol - p;
}
struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
{
struct commit_list *new_list = xmalloc(sizeof(struct commit_list));

Просмотреть файл

@ -41,6 +41,9 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);
int parse_commit(struct commit *item);
/* Find beginning and length of commit subject. */
int find_commit_subject(const char *commit_buffer, const char **subject);
struct commit_list * commit_list_insert(struct commit *item, struct commit_list **list_p);
unsigned commit_list_count(const struct commit_list *l);
struct commit_list * insert_by_date(struct commit *item, struct commit_list **list);

Просмотреть файл

@ -136,16 +136,10 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
if (parse_commit(commit) != 0)
printf("(bad commit)\n");
else {
const char *s;
int len;
for (s = commit->buffer; *s; s++)
if (*s == '\n' && s[1] == '\n') {
s += 2;
break;
}
for (len = 0; s[len] && '\n' != s[len]; len++)
; /* do nothing */
printf("%.*s\n", len, s);
const char *title;
int len = find_commit_subject(commit->buffer, &title);
if (len)
printf("%.*s\n", len, title);
}
}
}

Просмотреть файл

@ -13,11 +13,29 @@ test_expect_success setup '
git checkout -b empty-branch &&
test_tick &&
git commit --allow-empty -m "empty"
git commit --allow-empty -m "empty" &&
echo third >> file1 &&
git add file1 &&
test_tick &&
git commit --allow-empty-message -m ""
'
test_expect_success 'cherry-pick an empty commit' '
git checkout master && {
git cherry-pick empty-branch^
test "$?" = 1
}
'
test_expect_success 'index lockfile was removed' '
test ! -f .git/index.lock
'
test_expect_success 'cherry-pick a commit with an empty message' '
git checkout master && {
git cherry-pick empty-branch
test "$?" = 1