#include "cache.h"
#include "sequencer.h"
#include "dir.h"
#include "object.h"
#include "commit.h"
#include "tag.h"
#include "run-command.h"
#include "exec_cmd.h"
#include "utf8.h"
#include "cache-tree.h"
#include "diff.h"
#include "revision.h"
#include "rerere.h"
#include "merge-recursive.h"
#include "refs.h"
#include "argv-array.h"
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
const char sign_off_header[] = "Signed-off-by: ";
static const char cherry_picked_prefix[] = "(cherry picked from commit ";
static int is_rfc2822_line(const char *buf, int len)
{
int i;
for (i = 0; i < len; i++) {
int ch = buf[i];
if (ch == ':')
return 1;
if (!isalnum(ch) && ch != '-')
break;
}
return 0;
}
static int is_cherry_picked_from_line(const char *buf, int len)
{
/*
* We only care that it looks roughly like (cherry picked from ...)
*/
return len > strlen(cherry_picked_prefix) + 1 &&
starts_with(buf, cherry_picked_prefix) && buf[len - 1] == ')';
}
/*
* Returns 0 for non-conforming footer
* Returns 1 for conforming footer
* Returns 2 when sob exists within conforming footer
* Returns 3 when sob exists within conforming footer as last entry
*/
static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
int ignore_footer)
{
char prev;
int i, k;
int len = sb->len - ignore_footer;
const char *buf = sb->buf;
int found_sob = 0;
/* footer must end with newline */
if (!len || buf[len - 1] != '\n')
return 0;
prev = '\0';
for (i = len - 1; i > 0; i--) {
char ch = buf[i];
if (prev == '\n' && ch == '\n') /* paragraph break */
break;
prev = ch;
}
/* require at least one blank line */
if (prev != '\n' || buf[i] != '\n')
return 0;
/* advance to start of last paragraph */
while (i < len - 1 && buf[i] == '\n')
i++;
for (; i < len; i = k) {
int found_rfc2822;
for (k = i; k < len && buf[k] != '\n'; k++)
; /* do nothing */
k++;
found_rfc2822 = is_rfc2822_line(buf + i, k - i - 1);
if (found_rfc2822 && sob &&
!strncmp(buf + i, sob->buf, sob->len))
found_sob = k;
if (!(found_rfc2822 ||
is_cherry_picked_from_line(buf + i, k - i - 1)))
return 0;
}
if (found_sob == i)
return 3;
if (found_sob)
return 2;
return 1;
}
static void remove_sequencer_state(void)
{
struct strbuf seq_dir = STRBUF_INIT;
strbuf_addf(&seq_dir, "%s", git_path(SEQ_DIR));
remove_dir_recursively(&seq_dir, 0);
strbuf_release(&seq_dir);
}
static const char *action_name(const struct replay_opts *opts)
{
return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
}
struct commit_message {
char *parent_label;
const char *label;
const char *subject;
const char *message;
};
static int get_message(struct commit *commit, struct commit_message *out)
{
const char *abbrev, *subject;
int abbrev_len, subject_len;
char *q;
if (!git_commit_encoding)
git_commit_encoding = "UTF-8";
out->message = logmsg_reencode(commit, NULL, git_commit_encoding);
abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
abbrev_len = strlen(abbrev);
subject_len = find_commit_subject(out->message, &subject);
out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
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, subject, subject_len);
*q = '\0';
return 0;
}
|