Skip to content

Commit 3f3e760

Browse files
chooglengitster
authored andcommitted
branch: add a dry_run parameter to create_branch()
Add a dry_run parameter to create_branch() such that dry_run = 1 will validate a new branch without trying to create it. This will be used in `git branch --recurse-submodules` to ensure that the new branch can be created in all submodules. Signed-off-by: Glen Choo <[email protected]> Reviewed-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc0893c commit 3f3e760

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

branch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ static void dwim_branch_start(struct repository *r, const char *start_name,
423423
void create_branch(struct repository *r,
424424
const char *name, const char *start_name,
425425
int force, int clobber_head_ok, int reflog,
426-
int quiet, enum branch_track track)
426+
int quiet, enum branch_track track, int dry_run)
427427
{
428428
struct object_id oid;
429429
char *real_ref;
@@ -445,6 +445,8 @@ void create_branch(struct repository *r,
445445
}
446446

447447
dwim_branch_start(r, start_name, track, &real_ref, &oid);
448+
if (dry_run)
449+
goto cleanup;
448450

449451
if (reflog)
450452
log_all_ref_updates = LOG_REFS_NORMAL;
@@ -467,6 +469,7 @@ void create_branch(struct repository *r,
467469
if (real_ref && track)
468470
setup_tracking(ref.buf + 11, real_ref, track, quiet);
469471

472+
cleanup:
470473
strbuf_release(&ref);
471474
free(real_ref);
472475
}

branch.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,14 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
6262
* - track causes the new branch to be configured to merge the remote branch
6363
* that start_name is a tracking branch for (if any).
6464
*
65+
* - dry_run causes the branch to be validated but not created.
66+
*
6567
*/
6668
void create_branch(struct repository *r,
6769
const char *name, const char *start_name,
6870
int force, int clobber_head_ok,
69-
int reflog, int quiet, enum branch_track track);
71+
int reflog, int quiet, enum branch_track track,
72+
int dry_run);
7073

7174
/*
7275
* Check if 'name' can be a valid name for a branch; die otherwise.

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
859859

860860
create_branch(the_repository,
861861
argv[0], (argc == 2) ? argv[1] : head,
862-
force, 0, reflog, quiet, track);
862+
force, 0, reflog, quiet, track, 0);
863863

864864
} else
865865
usage_with_options(builtin_branch_usage, options);

builtin/checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
893893
opts->new_branch_force ? 1 : 0,
894894
opts->new_branch_log,
895895
opts->quiet,
896-
opts->track);
896+
opts->track,
897+
0);
897898
new_branch_info->name = opts->new_branch;
898899
setup_branch_path(new_branch_info);
899900
}

0 commit comments

Comments
 (0)