Skip to content

Commit 28a1540

Browse files
jaysoffiangitster
authored andcommitted
builtin-fetch: add --dry-run option
Teach fetch --dry-run as users of "git remote prune" switching to "git fetch --prune" may expect it. Unfortunately OPT__DRY_RUN() cannot be used as fetch already uses "-n" for something else. Signed-off-by: Jay Soffian <[email protected]> Signed-off-by: Björn Gustavsson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f360d84 commit 28a1540

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Documentation/fetch-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
`git clone` with `--depth=<depth>` option (see linkgit:git-clone[1])
1313
by the specified number of commits.
1414

15+
ifndef::git-pull[]
16+
--dry-run::
17+
Show what would be done, without making any changes.
18+
endif::git-pull[]
19+
1520
-f::
1621
--force::
1722
When 'git-fetch' is used with `<rbranch>:<lbranch>`

builtin-fetch.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum {
2626
TAGS_SET = 2
2727
};
2828

29-
static int all, append, force, keep, multiple, prune, update_head_ok, verbosity;
29+
static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
3030
static int tags = TAGS_DEFAULT;
3131
static const char *depth;
3232
static const char *upload_pack;
@@ -51,6 +51,8 @@ static struct option builtin_fetch_options[] = {
5151
"do not fetch all tags (--no-tags)", TAGS_UNSET),
5252
OPT_BOOLEAN('p', "prune", &prune,
5353
"prune tracking branches no longer on remote"),
54+
OPT_BOOLEAN(0, "dry-run", &dry_run,
55+
"dry run"),
5456
OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
5557
OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
5658
"allow updating of HEAD ref"),
@@ -187,6 +189,8 @@ static int s_update_ref(const char *action,
187189
char *rla = getenv("GIT_REFLOG_ACTION");
188190
static struct ref_lock *lock;
189191

192+
if (dry_run)
193+
return 0;
190194
if (!rla)
191195
rla = default_rla.buf;
192196
snprintf(msg, sizeof(msg), "%s: %s", rla, action);
@@ -312,7 +316,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
312316
char note[1024];
313317
const char *what, *kind;
314318
struct ref *rm;
315-
char *url, *filename = git_path("FETCH_HEAD");
319+
char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
316320

317321
fp = fopen(filename, "a");
318322
if (!fp)
@@ -617,7 +621,7 @@ static int do_fetch(struct transport *transport,
617621
die("Don't know how to fetch from %s", transport->url);
618622

619623
/* if not appending, truncate FETCH_HEAD */
620-
if (!append) {
624+
if (!append && !dry_run) {
621625
char *filename = git_path("FETCH_HEAD");
622626
FILE *fp = fopen(filename, "w");
623627
if (!fp)
@@ -725,9 +729,11 @@ static int add_remote_or_group(const char *name, struct string_list *list)
725729
static int fetch_multiple(struct string_list *list)
726730
{
727731
int i, result = 0;
728-
const char *argv[] = { "fetch", NULL, NULL, NULL, NULL, NULL };
732+
const char *argv[] = { "fetch", NULL, NULL, NULL, NULL, NULL, NULL };
729733
int argc = 1;
730734

735+
if (dry_run)
736+
argv[argc++] = "--dry-run";
731737
if (prune)
732738
argv[argc++] = "--prune";
733739
if (verbosity >= 2)

0 commit comments

Comments
 (0)