Skip to content

Commit 21b138d

Browse files
committed
receive-pack: refactor updateInstead codepath
Keep the "there is nothing to update in a bare repository", "when the check and update process runs, here are the GIT_DIR and GIT_WORK_TREE" logic, which will be common regardless of how the decision to update and the actual update are done, in the original update_worktree() function, and split out the "working tree and the index must match the original HEAD exactly" and "use two-way read-tree to update the working tree" into a new push_to_deploy() helper function. This will allow customizing the logic more cleanly and easily. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d7a5ce commit 21b138d

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

builtin/receive-pack.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,9 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
733733
return 0;
734734
}
735735

736-
static const char *update_worktree(unsigned char *sha1)
736+
static const char *push_to_deploy(unsigned char *sha1,
737+
struct argv_array *env,
738+
const char *work_tree)
737739
{
738740
const char *update_refresh[] = {
739741
"update-index", "-q", "--ignore-submodules", "--refresh", NULL
@@ -748,69 +750,70 @@ static const char *update_worktree(unsigned char *sha1)
748750
const char *read_tree[] = {
749751
"read-tree", "-u", "-m", NULL, NULL
750752
};
751-
const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
752-
struct argv_array env = ARGV_ARRAY_INIT;
753753
struct child_process child = CHILD_PROCESS_INIT;
754754

755-
if (is_bare_repository())
756-
return "denyCurrentBranch = updateInstead needs a worktree";
757-
758-
argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
759-
760755
child.argv = update_refresh;
761-
child.env = env.argv;
756+
child.env = env->argv;
762757
child.dir = work_tree;
763758
child.no_stdin = 1;
764759
child.stdout_to_stderr = 1;
765760
child.git_cmd = 1;
766-
if (run_command(&child)) {
767-
argv_array_clear(&env);
761+
if (run_command(&child))
768762
return "Up-to-date check failed";
769-
}
770763

771764
/* run_command() does not clean up completely; reinitialize */
772765
child_process_init(&child);
773766
child.argv = diff_files;
774-
child.env = env.argv;
767+
child.env = env->argv;
775768
child.dir = work_tree;
776769
child.no_stdin = 1;
777770
child.stdout_to_stderr = 1;
778771
child.git_cmd = 1;
779-
if (run_command(&child)) {
780-
argv_array_clear(&env);
772+
if (run_command(&child))
781773
return "Working directory has unstaged changes";
782-
}
783774

784775
child_process_init(&child);
785776
child.argv = diff_index;
786-
child.env = env.argv;
777+
child.env = env->argv;
787778
child.no_stdin = 1;
788779
child.no_stdout = 1;
789780
child.stdout_to_stderr = 0;
790781
child.git_cmd = 1;
791-
if (run_command(&child)) {
792-
argv_array_clear(&env);
782+
if (run_command(&child))
793783
return "Working directory has staged changes";
794-
}
795784

796785
read_tree[3] = sha1_to_hex(sha1);
797786
child_process_init(&child);
798787
child.argv = read_tree;
799-
child.env = env.argv;
788+
child.env = env->argv;
800789
child.dir = work_tree;
801790
child.no_stdin = 1;
802791
child.no_stdout = 1;
803792
child.stdout_to_stderr = 0;
804793
child.git_cmd = 1;
805-
if (run_command(&child)) {
806-
argv_array_clear(&env);
794+
if (run_command(&child))
807795
return "Could not update working tree to new HEAD";
808-
}
809796

810-
argv_array_clear(&env);
811797
return NULL;
812798
}
813799

800+
static const char *update_worktree(unsigned char *sha1)
801+
{
802+
const char *retval;
803+
const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
804+
struct argv_array env = ARGV_ARRAY_INIT;
805+
806+
if (is_bare_repository())
807+
return "denyCurrentBranch = updateInstead needs a worktree";
808+
809+
argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
810+
811+
retval = push_to_deploy(sha1, &env, work_tree);
812+
813+
argv_array_clear(&env);
814+
return retval;
815+
}
816+
814817
static const char *update(struct command *cmd, struct shallow_info *si)
815818
{
816819
const char *name = cmd->ref_name;

0 commit comments

Comments
 (0)