]> git.ipfire.org Git - thirdparty/git.git/commitdiff
branch: add a dry_run parameter to create_branch()
authorGlen Choo <chooglen@google.com>
Sat, 29 Jan 2022 00:04:43 +0000 (16:04 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Feb 2022 22:18:56 +0000 (14:18 -0800)
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 <chooglen@google.com>
Reviewed-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
branch.c
branch.h
builtin/branch.c
builtin/checkout.c

index df24021f27847de3b739e76b08502de17bcb9fd6..02d46a69b8c379515ba81669b27bd82e774cf28f 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -423,7 +423,7 @@ static void dwim_branch_start(struct repository *r, const char *start_name,
 void create_branch(struct repository *r,
                   const char *name, const char *start_name,
                   int force, int clobber_head_ok, int reflog,
-                  int quiet, enum branch_track track)
+                  int quiet, enum branch_track track, int dry_run)
 {
        struct object_id oid;
        char *real_ref;
@@ -445,6 +445,8 @@ void create_branch(struct repository *r,
        }
 
        dwim_branch_start(r, start_name, track, &real_ref, &oid);
+       if (dry_run)
+               goto cleanup;
 
        if (reflog)
                log_all_ref_updates = LOG_REFS_NORMAL;
@@ -467,6 +469,7 @@ void create_branch(struct repository *r,
        if (real_ref && track)
                setup_tracking(ref.buf + 11, real_ref, track, quiet);
 
+cleanup:
        strbuf_release(&ref);
        free(real_ref);
 }
index cf3a4d3ff3694ab662c0b1fdf12c53017cfe13c0..509cfcc34e989e5987886cc0d31c14f7ae1ae2fe 100644 (file)
--- a/branch.h
+++ b/branch.h
@@ -62,11 +62,14 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
  *   - track causes the new branch to be configured to merge the remote branch
  *     that start_name is a tracking branch for (if any).
  *
+ *   - dry_run causes the branch to be validated but not created.
+ *
  */
 void create_branch(struct repository *r,
                   const char *name, const char *start_name,
                   int force, int clobber_head_ok,
-                  int reflog, int quiet, enum branch_track track);
+                  int reflog, int quiet, enum branch_track track,
+                  int dry_run);
 
 /*
  * Check if 'name' can be a valid name for a branch; die otherwise.
index 676825242ab34faa5eed9a4ede57ef36f6a11268..0a49de0281f81e4a53fae45d62ad79f36df981b3 100644 (file)
@@ -859,7 +859,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
                create_branch(the_repository,
                              argv[0], (argc == 2) ? argv[1] : head,
-                             force, 0, reflog, quiet, track);
+                             force, 0, reflog, quiet, track, 0);
 
        } else
                usage_with_options(builtin_branch_usage, options);
index 1809ac12dfe853ddc3317ee6650d5fbf6e36822b..86008606298ef039844de39eca088a5032da02bb 100644 (file)
@@ -893,7 +893,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
                                      opts->new_branch_force ? 1 : 0,
                                      opts->new_branch_log,
                                      opts->quiet,
-                                     opts->track);
+                                     opts->track,
+                                     0);
                new_branch_info->name = opts->new_branch;
                setup_branch_path(new_branch_info);
        }