Skip to content

Commit 3a5d13a

Browse files
committed
commit: --dry-run
This teaches --dry-run option to "git commit". It is the same as "git status", but in the longer term we would want to change the semantics of "git status" not to be the preview of commit, and this is the first step for doing so. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d4d572 commit 3a5d13a

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Documentation/git-commit.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-commit - Record changes to the repository
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git commit' [-a | --interactive] [-s] [-v] [-u<mode>] [--amend]
11+
'git commit' [-a | --interactive] [-s] [-v] [-u<mode>] [--amend] [--dry-run]
1212
[(-c | -C) <commit>] [-F <file> | -m <msg>]
1313
[--allow-empty] [--no-verify] [-e] [--author=<author>]
1414
[--cleanup=<mode>] [--] [[-i | -o ]<file>...]
@@ -198,6 +198,11 @@ specified.
198198
--quiet::
199199
Suppress commit summary message.
200200

201+
--dry-run::
202+
Do not create a commit, but show a list of paths that are
203+
to be committed, paths with local changes that will be left
204+
uncommitted and paths that are untracked.
205+
201206
\--::
202207
Do not interpret any more arguments as options.
203208

builtin-commit.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static const char *template_file;
5151
static char *edit_message, *use_message;
5252
static char *author_name, *author_email, *author_date;
5353
static int all, edit_flag, also, interactive, only, amend, signoff;
54-
static int quiet, verbose, no_verify, allow_empty;
54+
static int quiet, verbose, no_verify, allow_empty, dry_run;
5555
static char *untracked_files_arg;
5656
/*
5757
* The default commit message cleanup mode will remove the lines
@@ -103,6 +103,7 @@ static struct option builtin_commit_options[] = {
103103
OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
104104
OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
105105
OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
106+
OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
106107
OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
107108
{ 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" },
108109
OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
@@ -813,28 +814,28 @@ static int parse_and_validate_options(int argc, const char *argv[],
813814
return argc;
814815
}
815816

816-
int cmd_status(int argc, const char **argv, const char *prefix)
817+
static int dry_run_commit(int argc, const char **argv, const char *prefix)
817818
{
818-
const char *index_file;
819819
int commitable;
820+
const char *index_file;
820821

821-
git_config(git_status_config, NULL);
822+
index_file = prepare_index(argc, argv, prefix, 1);
823+
commitable = run_status(stdout, index_file, prefix, 0);
824+
rollback_index_files();
822825

826+
return commitable ? 0 : 1;
827+
}
828+
829+
int cmd_status(int argc, const char **argv, const char *prefix)
830+
{
831+
git_config(git_status_config, NULL);
823832
if (wt_status_use_color == -1)
824833
wt_status_use_color = git_use_color_default;
825-
826834
if (diff_use_color_default == -1)
827835
diff_use_color_default = git_use_color_default;
828836

829837
argc = parse_and_validate_options(argc, argv, builtin_status_usage, prefix);
830-
831-
index_file = prepare_index(argc, argv, prefix, 1);
832-
833-
commitable = run_status(stdout, index_file, prefix, 0);
834-
835-
rollback_index_files();
836-
837-
return commitable ? 0 : 1;
838+
return dry_run_commit(argc, argv, prefix);
838839
}
839840

840841
static void print_summary(const char *prefix, const unsigned char *sha1)
@@ -909,6 +910,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
909910
wt_status_use_color = git_use_color_default;
910911

911912
argc = parse_and_validate_options(argc, argv, builtin_commit_usage, prefix);
913+
if (dry_run)
914+
return dry_run_commit(argc, argv, prefix);
912915

913916
index_file = prepare_index(argc, argv, prefix, 0);
914917

0 commit comments

Comments
 (0)