Skip to content

Commit

Permalink
Merge branch 'pn/commit-autosquash'
Browse files Browse the repository at this point in the history
* pn/commit-autosquash:
  add tests of commit --squash
  commit: --squash option for use with rebase --autosquash
  add tests of commit --fixup
  commit: --fixup option for use with rebase --autosquash
  pretty.c: teach format_commit_message() to reencode the output
  commit: helper methods to reduce redundant blocks of code

Conflicts:
	Documentation/git-commit.txt
	t/t3415-rebase-autosquash.sh
  • Loading branch information
Junio C Hamano committed Dec 4, 2010
2 parents 45a7b55 + 7951bd3 commit 9bec60d
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 49 deletions.
21 changes: 17 additions & 4 deletions Documentation/git-commit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ SYNOPSIS
--------
[verse]
'git commit' [-a | --interactive] [-s] [-v] [-u<mode>] [--amend] [--dry-run]
[(-c | -C) <commit>] [-F <file> | -m <msg>] [--reset-author]
[--allow-empty] [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
[--date=<date>] [--cleanup=<mode>] [--status | --no-status]
[-i | -o] [--] [<file>...]
[(-c | -C | --fixup | --squash) <commit>] [-F <file> | -m <msg>]
[--reset-author] [--allow-empty] [--allow-empty-message] [--no-verify]
[-e] [--author=<author>] [--date=<date>] [--cleanup=<mode>]
[--status | --no-status] [-i | -o] [--] [<file>...]

DESCRIPTION
-----------
Expand Down Expand Up @@ -70,6 +70,19 @@ OPTIONS
Like '-C', but with '-c' the editor is invoked, so that
the user can further edit the commit message.

--fixup=<commit>::
Construct a commit message for use with `rebase --autosquash`.
The commit message will be the subject line from the specified
commit with a prefix of "fixup! ". See linkgit:git-rebase[1]
for details.

--squash=<commit>::
Construct a commit message for use with `rebase --autosquash`.
The commit message subject line is taken from the specified
commit with a prefix of "squash! ". Can be used with additional
commit message options (`-m`/`-c`/`-C`/`-F`). See
linkgit:git-rebase[1] for details.

--reset-author::
When used with -C/-c/--amend options, declare that the
authorship of the resulting commit now belongs of the committer.
Expand Down
81 changes: 54 additions & 27 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static enum {
static const char *logfile, *force_author;
static const char *template_file;
static char *edit_message, *use_message;
static char *fixup_message, *squash_message;
static char *author_name, *author_email, *author_date;
static int all, edit_flag, also, interactive, only, amend, signoff;
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
Expand Down Expand Up @@ -124,6 +125,8 @@ static struct option builtin_commit_options[] = {
OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"),
OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
OPT_STRING(0, "fixup", &fixup_message, "COMMIT", "use autosquash formatted message to fixup specified commit"),
OPT_STRING(0, "squash", &squash_message, "COMMIT", "use autosquash formatted message to squash specified commit"),
OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"),
OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
OPT_FILENAME('t', "template", &template_file, "use specified template file"),
Expand Down Expand Up @@ -565,6 +568,25 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
if (!no_verify && run_hook(index_file, "pre-commit", NULL))
return 0;

if (squash_message) {
/*
* Insert the proper subject line before other commit
* message options add their content.
*/
if (use_message && !strcmp(use_message, squash_message))
strbuf_addstr(&sb, "squash! ");
else {
struct pretty_print_context ctx = {0};
struct commit *c;
c = lookup_commit_reference_by_name(squash_message);
if (!c)
die("could not lookup commit %s", squash_message);
ctx.output_encoding = get_commit_output_encoding();
format_commit_message(c, "squash! %s\n\n", &sb,
&ctx);
}
}

if (message.len) {
strbuf_addbuf(&sb, &message);
hook_arg1 = "message";
Expand All @@ -586,6 +608,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
hook_arg1 = "commit";
hook_arg2 = use_message;
} else if (fixup_message) {
struct pretty_print_context ctx = {0};
struct commit *commit;
commit = lookup_commit_reference_by_name(fixup_message);
if (!commit)
die("could not lookup commit %s", fixup_message);
ctx.output_encoding = get_commit_output_encoding();
format_commit_message(commit, "fixup! %s\n\n",
&sb, &ctx);
hook_arg1 = "message";
} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
die_errno("could not read MERGE_MSG");
Expand All @@ -607,6 +639,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
else if (in_merge)
hook_arg1 = "merge";

if (squash_message) {
/*
* If squash_commit was used for the commit subject,
* then we're possibly hijacking other commit log options.
* Reset the hook args to tell the real story.
*/
hook_arg1 = "message";
hook_arg2 = "";
}

fp = fopen(git_path(commit_editmsg), "w");
if (fp == NULL)
die_errno("could not open '%s'", git_path(commit_editmsg));
Expand Down Expand Up @@ -863,7 +905,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
if (force_author && renew_authorship)
die("Using both --reset-author and --author does not make sense");

if (logfile || message.len || use_message)
if (logfile || message.len || use_message || fixup_message)
use_editor = 0;
if (edit_flag)
use_editor = 1;
Expand All @@ -878,48 +920,35 @@ static int parse_and_validate_options(int argc, const char *argv[],
die("You have nothing to amend.");
if (amend && in_merge)
die("You are in the middle of a merge -- cannot amend.");

if (fixup_message && squash_message)
die("Options --squash and --fixup cannot be used together");
if (use_message)
f++;
if (edit_message)
f++;
if (fixup_message)
f++;
if (logfile)
f++;
if (f > 1)
die("Only one of -c/-C/-F can be used.");
die("Only one of -c/-C/-F/--fixup can be used.");
if (message.len && f > 0)
die("Option -m cannot be combined with -c/-C/-F.");
die("Option -m cannot be combined with -c/-C/-F/--fixup.");
if (edit_message)
use_message = edit_message;
if (amend && !use_message)
if (amend && !use_message && !fixup_message)
use_message = "HEAD";
if (!use_message && renew_authorship)
die("--reset-author can be used only with -C, -c or --amend.");
if (use_message) {
unsigned char sha1[20];
static char utf8[] = "UTF-8";
const char *out_enc;
char *enc, *end;
struct commit *commit;

if (get_sha1(use_message, sha1))
commit = lookup_commit_reference_by_name(use_message);
if (!commit)
die("could not lookup commit %s", use_message);
commit = lookup_commit_reference(sha1);
if (!commit || parse_commit(commit))
die("could not parse commit %s", use_message);

enc = strstr(commit->buffer, "\nencoding");
if (enc) {
end = strchr(enc + 10, '\n');
enc = xstrndup(enc + 10, end - (enc + 10));
} else {
enc = utf8;
}
out_enc = git_commit_encoding ? git_commit_encoding : utf8;

if (strcmp(out_enc, enc))
use_message_buffer =
reencode_string(commit->buffer, out_enc, enc);
out_enc = get_commit_output_encoding();
use_message_buffer = logmsg_reencode(commit, out_enc);

/*
* If we failed to reencode the buffer, just copy it
Expand All @@ -929,8 +958,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
*/
if (use_message_buffer == NULL)
use_message_buffer = xstrdup(commit->buffer);
if (enc != utf8)
free(enc);
}

if (!!also + !!only + !!all + !!interactive > 1)
Expand Down
3 changes: 1 addition & 2 deletions builtin/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ static void show_tagger(char *buf, int len, struct rev_info *rev)
struct strbuf out = STRBUF_INIT;

pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
git_log_output_encoding ?
git_log_output_encoding: git_commit_encoding);
get_log_output_encoding());
printf("%s", out.buf);
strbuf_release(&out);
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/mailinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
*/
git_config(git_mailinfo_config, NULL);

def_charset = (git_commit_encoding ? git_commit_encoding : "UTF-8");
def_charset = get_commit_output_encoding();
metainfo_charset = def_charset;

while (1 < argc && argv[1][0] == '-') {
Expand Down
3 changes: 3 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,9 @@ extern int git_env_bool(const char *, int);
extern int git_config_system(void);
extern int git_config_global(void);
extern int config_error_nonbool(const char *);
extern const char *get_log_output_encoding(void);
extern const char *get_commit_output_encoding(void);

extern const char *config_exclusive_filename;

#define MAX_GITNAME (1000)
Expand Down
13 changes: 13 additions & 0 deletions commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ struct commit *lookup_commit(const unsigned char *sha1)
return check_commit(obj, sha1, 0);
}

struct commit *lookup_commit_reference_by_name(const char *name)
{
unsigned char sha1[20];
struct commit *commit;

if (get_sha1(name, sha1))
return NULL;
commit = lookup_commit_reference(sha1);
if (!commit || parse_commit(commit))
return NULL;
return commit;
}

static unsigned long parse_commit_date(const char *buf, const char *tail)
{
const char *dateptr;
Expand Down
4 changes: 4 additions & 0 deletions commit.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct commit *lookup_commit(const unsigned char *sha1);
struct commit *lookup_commit_reference(const unsigned char *sha1);
struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
int quiet);
struct commit *lookup_commit_reference_by_name(const char *name);

int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size);

Expand Down Expand Up @@ -76,6 +77,7 @@ struct pretty_print_context
int need_8bit_cte;
int show_notes;
struct reflog_walk_info *reflog_info;
const char *output_encoding;
};

struct userformat_want {
Expand All @@ -84,6 +86,8 @@ struct userformat_want {

extern int has_non_ascii(const char *text);
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
extern char *logmsg_reencode(const struct commit *commit,
const char *output_encoding);
extern char *reencode_commit_message(const struct commit *commit,
const char **encoding_p);
extern void get_commit_format(const char *arg, struct rev_info *);
Expand Down
11 changes: 11 additions & 0 deletions environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,14 @@ int set_git_dir(const char *path)
setup_git_env();
return 0;
}

const char *get_log_output_encoding(void)
{
return git_log_output_encoding ? git_log_output_encoding
: get_commit_output_encoding();
}

const char *get_commit_output_encoding(void)
{
return git_commit_encoding ? git_commit_encoding : "UTF-8";
}
36 changes: 23 additions & 13 deletions pretty.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ static char *replace_encoding_header(char *buf, const char *encoding)
return strbuf_detach(&tmp, NULL);
}

static char *logmsg_reencode(const struct commit *commit,
const char *output_encoding)
char *logmsg_reencode(const struct commit *commit,
const char *output_encoding)
{
static const char *utf8 = "UTF-8";
const char *use_encoding;
Expand Down Expand Up @@ -555,6 +555,7 @@ struct format_commit_context {
const struct pretty_print_context *pretty_ctx;
unsigned commit_header_parsed:1;
unsigned commit_message_parsed:1;
char *message;
size_t width, indent1, indent2;

/* These offsets are relative to the start of the commit message. */
Expand Down Expand Up @@ -591,7 +592,7 @@ static int add_again(struct strbuf *sb, struct chunk *chunk)

static void parse_commit_header(struct format_commit_context *context)
{
const char *msg = context->commit->buffer;
const char *msg = context->message;
int i;

for (i = 0; msg[i]; i++) {
Expand Down Expand Up @@ -677,8 +678,8 @@ const char *format_subject(struct strbuf *sb, const char *msg,

static void parse_commit_message(struct format_commit_context *c)
{
const char *msg = c->commit->buffer + c->message_off;
const char *start = c->commit->buffer;
const char *msg = c->message + c->message_off;
const char *start = c->message;

msg = skip_empty_lines(msg);
c->subject_off = msg - start;
Expand Down Expand Up @@ -741,7 +742,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
{
struct format_commit_context *c = context;
const struct commit *commit = c->commit;
const char *msg = commit->buffer;
const char *msg = c->message;
struct commit_list *p;
int h1, h2;

Expand Down Expand Up @@ -886,8 +887,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
case 'N':
if (c->pretty_ctx->show_notes) {
format_display_notes(commit->object.sha1, sb,
git_log_output_encoding ? git_log_output_encoding
: git_commit_encoding, 0);
get_log_output_encoding(), 0);
return 1;
}
return 0;
Expand Down Expand Up @@ -1012,13 +1012,27 @@ void format_commit_message(const struct commit *commit,
const struct pretty_print_context *pretty_ctx)
{
struct format_commit_context context;
static const char utf8[] = "UTF-8";
const char *enc;
const char *output_enc = pretty_ctx->output_encoding;

memset(&context, 0, sizeof(context));
context.commit = commit;
context.pretty_ctx = pretty_ctx;
context.wrap_start = sb->len;
context.message = commit->buffer;
if (output_enc) {
enc = get_header(commit, "encoding");
enc = enc ? enc : utf8;
if (strcmp(enc, output_enc))
context.message = logmsg_reencode(commit, output_enc);
}

strbuf_expand(sb, format, format_commit_item, &context);
rewrap_message_tail(sb, &context, 0, 0, 0);

if (context.message != commit->buffer)
free(context.message);
}

static void pp_header(enum cmit_fmt fmt,
Expand Down Expand Up @@ -1159,11 +1173,7 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
{
const char *encoding;

encoding = (git_log_output_encoding
? git_log_output_encoding
: git_commit_encoding);
if (!encoding)
encoding = "UTF-8";
encoding = get_log_output_encoding();
if (encoding_p)
*encoding_p = encoding;
return logmsg_reencode(commit, encoding);
Expand Down
Loading

0 comments on commit 9bec60d

Please sign in to comment.