Skip to content

Commit

Permalink
Merge branch 'jk/status-porcelain-z-b'
Browse files Browse the repository at this point in the history
"git status --porcelain" ignored "--branch" option by mistake.  The output
for "git status --branch -z" was also incorrect and did not terminate the
record for the current branch name with NUL as asked.

By Jeff King
via Jeff King
* jk/status-porcelain-z-b:
  status: refactor colopts handling
  status: respect "-b" for porcelain format
  status: fix null termination with "-b"
  status: refactor null_termination option
  commit: refactor option parsing
  • Loading branch information
Junio C Hamano committed May 10, 2012
2 parents 1652867 + 4d2292e commit f7858ad
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Documentation/git-status.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ order is reversed (e.g 'from \-> to' becomes 'to from'). Second, a NUL
and the terminating newline (but a space still separates the status
field from the first filename). Third, filenames containing special
characters are not specially formatted; no quoting or
backslash-escaping is performed. Fourth, there is no branch line.
backslash-escaping is performed.

CONFIGURATION
-------------
Expand Down
144 changes: 71 additions & 73 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
static int no_post_rewrite, allow_empty_message;
static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
static char *sign_commit;
static unsigned int colopts;

/*
* The default commit message cleanup mode will remove the lines
Expand All @@ -111,13 +110,11 @@ static int show_ignored_in_status;
static const char *only_include_assumed;
static struct strbuf message = STRBUF_INIT;

static int null_termination;
static enum {
STATUS_FORMAT_LONG,
STATUS_FORMAT_SHORT,
STATUS_FORMAT_PORCELAIN
} status_format = STATUS_FORMAT_LONG;
static int status_show_branch;

static int opt_parse_m(const struct option *opt, const char *arg, int unset)
{
Expand All @@ -131,59 +128,6 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
return 0;
}

static struct option builtin_commit_options[] = {
OPT__QUIET(&quiet, "suppress summary after successful commit"),
OPT__VERBOSE(&verbose, "show diff in commit message template"),

OPT_GROUP("Commit message options"),
OPT_FILENAME('F', "file", &logfile, "read message from file"),
OPT_STRING(0, "author", &force_author, "author", "override author for commit"),
OPT_STRING(0, "date", &force_date, "date", "override date for commit"),
OPT_CALLBACK('m', "message", &message, "message", "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"),
OPT_BOOL('e', "edit", &edit_flag, "force edit of commit"),
OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, "key id",
"GPG sign commit", PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
/* end commit message options */

OPT_GROUP("Commit contents options"),
OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
OPT_BOOLEAN('p', "patch", &patch_interactive, "interactively add changes"),
OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
OPT_SET_INT(0, "short", &status_format, "show status concisely",
STATUS_FORMAT_SHORT),
OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
OPT_SET_INT(0, "porcelain", &status_format,
"machine-readable output", STATUS_FORMAT_PORCELAIN),
OPT_BOOLEAN('z', "null", &null_termination,
"terminate entries with NUL"),
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
/* end commit contents options */

{ OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
"ok to record an empty change",
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
{ OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
"ok to record a change with an empty message",
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },

OPT_END()
};

static void determine_whence(struct wt_status *s)
{
if (file_exists(git_path("MERGE_HEAD")))
Expand Down Expand Up @@ -501,10 +445,10 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int

switch (status_format) {
case STATUS_FORMAT_SHORT:
wt_shortstatus_print(s, null_termination, status_show_branch);
wt_shortstatus_print(s);
break;
case STATUS_FORMAT_PORCELAIN:
wt_porcelain_print(s, null_termination);
wt_porcelain_print(s);
break;
case STATUS_FORMAT_LONG:
wt_status_print(s);
Expand Down Expand Up @@ -1037,15 +981,15 @@ static const char *read_commit_message(const char *name)
}

static int parse_and_validate_options(int argc, const char *argv[],
const struct option *options,
const char * const usage[],
const char *prefix,
struct commit *current_head,
struct wt_status *s)
{
int f = 0;

argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
0);
argc = parse_options(argc, argv, prefix, options, usage, 0);

if (force_author && !strchr(force_author, '>'))
force_author = find_author_by_nickname(force_author);
Expand Down Expand Up @@ -1130,7 +1074,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
if (all && argc > 0)
die(_("Paths with -a does not make sense."));

if (null_termination && status_format == STATUS_FORMAT_LONG)
if (s->null_termination && status_format == STATUS_FORMAT_LONG)
status_format = STATUS_FORMAT_PORCELAIN;
if (status_format != STATUS_FORMAT_LONG)
dry_run = 1;
Expand Down Expand Up @@ -1176,7 +1120,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
struct wt_status *s = cb;

if (!prefixcmp(k, "column."))
return git_column_config(k, v, "status", &colopts);
return git_column_config(k, v, "status", &s->colopts);
if (!strcmp(k, "status.submodulesummary")) {
int is_bool;
s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
Expand Down Expand Up @@ -1219,19 +1163,19 @@ static int git_status_config(const char *k, const char *v, void *cb)

int cmd_status(int argc, const char **argv, const char *prefix)
{
struct wt_status s;
static struct wt_status s;
int fd;
unsigned char sha1[20];
static struct option builtin_status_options[] = {
OPT__VERBOSE(&verbose, "be verbose"),
OPT_SET_INT('s', "short", &status_format,
"show status concisely", STATUS_FORMAT_SHORT),
OPT_BOOLEAN('b', "branch", &status_show_branch,
OPT_BOOLEAN('b', "branch", &s.show_branch,
"show branch information"),
OPT_SET_INT(0, "porcelain", &status_format,
"machine-readable output",
STATUS_FORMAT_PORCELAIN),
OPT_BOOLEAN('z', "null", &null_termination,
OPT_BOOLEAN('z', "null", &s.null_termination,
"terminate entries with NUL"),
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
"mode",
Expand All @@ -1242,7 +1186,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
{ OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when",
"ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
OPT_COLUMN(0, "column", &colopts, "list untracked files in columns"),
OPT_COLUMN(0, "column", &s.colopts, "list untracked files in columns"),
OPT_END(),
};

Expand All @@ -1256,10 +1200,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix,
builtin_status_options,
builtin_status_usage, 0);
finalize_colopts(&colopts, -1);
s.colopts = colopts;
finalize_colopts(&s.colopts, -1);

if (null_termination && status_format == STATUS_FORMAT_LONG)
if (s.null_termination && status_format == STATUS_FORMAT_LONG)
status_format = STATUS_FORMAT_PORCELAIN;

handle_untracked_files_arg(&s);
Expand All @@ -1284,10 +1227,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)

switch (status_format) {
case STATUS_FORMAT_SHORT:
wt_shortstatus_print(&s, null_termination, status_show_branch);
wt_shortstatus_print(&s);
break;
case STATUS_FORMAT_PORCELAIN:
wt_porcelain_print(&s, null_termination);
wt_porcelain_print(&s);
break;
case STATUS_FORMAT_LONG:
s.verbose = verbose;
Expand Down Expand Up @@ -1422,6 +1365,60 @@ static int run_rewrite_hook(const unsigned char *oldsha1,

int cmd_commit(int argc, const char **argv, const char *prefix)
{
static struct wt_status s;
static struct option builtin_commit_options[] = {
OPT__QUIET(&quiet, "suppress summary after successful commit"),
OPT__VERBOSE(&verbose, "show diff in commit message template"),

OPT_GROUP("Commit message options"),
OPT_FILENAME('F', "file", &logfile, "read message from file"),
OPT_STRING(0, "author", &force_author, "author", "override author for commit"),
OPT_STRING(0, "date", &force_date, "date", "override date for commit"),
OPT_CALLBACK('m', "message", &message, "message", "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"),
OPT_BOOL('e', "edit", &edit_flag, "force edit of commit"),
OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, "key id",
"GPG sign commit", PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
/* end commit message options */

OPT_GROUP("Commit contents options"),
OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
OPT_BOOLEAN('p', "patch", &patch_interactive, "interactively add changes"),
OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
OPT_SET_INT(0, "short", &status_format, "show status concisely",
STATUS_FORMAT_SHORT),
OPT_BOOLEAN(0, "branch", &s.show_branch, "show branch information"),
OPT_SET_INT(0, "porcelain", &status_format,
"machine-readable output", STATUS_FORMAT_PORCELAIN),
OPT_BOOLEAN('z', "null", &s.null_termination,
"terminate entries with NUL"),
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
/* end commit contents options */

{ OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
"ok to record an empty change",
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
{ OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
"ok to record a change with an empty message",
PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },

OPT_END()
};

struct strbuf sb = STRBUF_INIT;
struct strbuf author_ident = STRBUF_INIT;
const char *index_file, *reflog_msg;
Expand All @@ -1431,7 +1428,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
struct commit_list *parents = NULL, **pptr = &parents;
struct stat statbuf;
int allow_fast_forward = 1;
struct wt_status s;
struct commit *current_head = NULL;
struct commit_extra_header *extra = NULL;

Expand All @@ -1441,6 +1437,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
wt_status_prepare(&s);
git_config(git_commit_config, &s);
determine_whence(&s);
s.colopts = 0;

if (get_sha1("HEAD", sha1))
current_head = NULL;
Expand All @@ -1449,7 +1446,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
if (!current_head || parse_commit(current_head))
die(_("could not parse HEAD commit"));
}
argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
argc = parse_and_validate_options(argc, argv, builtin_commit_options,
builtin_commit_usage,
prefix, current_head, &s);
if (dry_run)
return dry_run_commit(argc, argv, prefix, current_head, &s);
Expand Down
16 changes: 15 additions & 1 deletion t/t7508-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ test_expect_success 'status -s -b' '
'

test_expect_success 'status -s -z -b' '
tr "\\n" Q <expect >expect.q &&
mv expect.q expect &&
git status -s -z -b >output &&
nul_to_q <output >output.q &&
mv output.q output &&
test_cmp expect output
'

test_expect_success 'setup dir3' '
mkdir dir3 &&
: >dir3/untracked1 &&
Expand Down Expand Up @@ -671,9 +680,14 @@ test_expect_success 'status --porcelain ignores color.status' '
git config --unset color.status
git config --unset color.ui

test_expect_success 'status --porcelain ignores -b' '
test_expect_success 'status --porcelain respects -b' '
git status --porcelain -b >output &&
{
echo "## master" &&
cat expect
} >tmp &&
mv tmp expect &&
test_cmp expect output
'
Expand Down
Loading

0 comments on commit f7858ad

Please sign in to comment.