27
27
#include "object-store.h"
28
28
#include "protocol.h"
29
29
#include "commit-reach.h"
30
+ #include "worktree.h"
30
31
31
32
static const char * const receive_pack_usage [] = {
32
33
N_ ("git receive-pack <git-dir>" ),
@@ -816,16 +817,6 @@ static int run_update_hook(struct command *cmd)
816
817
return finish_command (& proc );
817
818
}
818
819
819
- static int is_ref_checked_out (const char * ref )
820
- {
821
- if (is_bare_repository ())
822
- return 0 ;
823
-
824
- if (!head_name )
825
- return 0 ;
826
- return !strcmp (head_name , ref );
827
- }
828
-
829
820
static char * refuse_unconfigured_deny_msg =
830
821
N_ ("By default, updating the current branch in a non-bare repository\n"
831
822
"is denied, because it will make the index and work tree inconsistent\n"
@@ -997,16 +988,26 @@ static const char *push_to_checkout(unsigned char *hash,
997
988
return NULL ;
998
989
}
999
990
1000
- static const char * update_worktree (unsigned char * sha1 )
991
+ static const char * update_worktree (unsigned char * sha1 , const struct worktree * worktree )
1001
992
{
1002
- const char * retval ;
1003
- const char * work_tree = git_work_tree_cfg ? git_work_tree_cfg : ".." ;
993
+ const char * retval , * work_tree , * git_dir = NULL ;
1004
994
struct argv_array env = ARGV_ARRAY_INIT ;
1005
995
996
+ if (worktree && worktree -> path )
997
+ work_tree = worktree -> path ;
998
+ else if (git_work_tree_cfg )
999
+ work_tree = git_work_tree_cfg ;
1000
+ else
1001
+ work_tree = ".." ;
1002
+
1006
1003
if (is_bare_repository ())
1007
1004
return "denyCurrentBranch = updateInstead needs a worktree" ;
1005
+ if (worktree )
1006
+ git_dir = get_worktree_git_dir (worktree );
1007
+ if (!git_dir )
1008
+ git_dir = get_git_dir ();
1008
1009
1009
- argv_array_pushf (& env , "GIT_DIR=%s" , absolute_path (get_git_dir () ));
1010
+ argv_array_pushf (& env , "GIT_DIR=%s" , absolute_path (git_dir ));
1010
1011
1011
1012
if (!find_hook (push_to_checkout_hook ))
1012
1013
retval = push_to_deploy (sha1 , & env , work_tree );
@@ -1026,6 +1027,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1026
1027
struct object_id * old_oid = & cmd -> old_oid ;
1027
1028
struct object_id * new_oid = & cmd -> new_oid ;
1028
1029
int do_update_worktree = 0 ;
1030
+ const struct worktree * worktree = is_bare_repository () ? NULL : find_shared_symref ("HEAD" , name );
1029
1031
1030
1032
/* only refs/... are allowed */
1031
1033
if (!starts_with (name , "refs/" ) || check_refname_format (name + 5 , 0 )) {
@@ -1037,7 +1039,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1037
1039
free (namespaced_name );
1038
1040
namespaced_name = strbuf_detach (& namespaced_name_buf , NULL );
1039
1041
1040
- if (is_ref_checked_out ( namespaced_name ) ) {
1042
+ if (worktree ) {
1041
1043
switch (deny_current_branch ) {
1042
1044
case DENY_IGNORE :
1043
1045
break ;
@@ -1069,7 +1071,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1069
1071
return "deletion prohibited" ;
1070
1072
}
1071
1073
1072
- if (head_name && !strcmp (namespaced_name , head_name )) {
1074
+ if (worktree || ( head_name && !strcmp (namespaced_name , head_name ) )) {
1073
1075
switch (deny_delete_current ) {
1074
1076
case DENY_IGNORE :
1075
1077
break ;
@@ -1118,7 +1120,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1118
1120
}
1119
1121
1120
1122
if (do_update_worktree ) {
1121
- ret = update_worktree (new_oid -> hash );
1123
+ ret = update_worktree (new_oid -> hash , find_shared_symref ( "HEAD" , name ) );
1122
1124
if (ret )
1123
1125
return ret ;
1124
1126
}
0 commit comments