| 1028 | } |
| 1029 | |
| 1030 | int init_worktree_config(struct repository *r) |
| 1031 | { |
| 1032 | int res = 0; |
| 1033 | int bare = 0; |
| 1034 | struct config_set cs = { { 0 } }; |
| 1035 | const char *core_worktree; |
| 1036 | char *common_config_file; |
| 1037 | char *main_worktree_file; |
| 1038 | |
| 1039 | /* |
| 1040 | * If the extension is already enabled, then we can skip the |
| 1041 | * upgrade process. |
| 1042 | */ |
| 1043 | if (r->repository_format_worktree_config) |
| 1044 | return 0; |
| 1045 | if ((res = repo_config_set_gently(the_repository, "extensions.worktreeConfig", "true"))) |
| 1046 | return error(_("failed to set extensions.worktreeConfig setting")); |
| 1047 | |
| 1048 | common_config_file = xstrfmt("%s/config", r->commondir); |
| 1049 | main_worktree_file = xstrfmt("%s/config.worktree", r->commondir); |
| 1050 | |
| 1051 | git_configset_init(&cs); |
| 1052 | git_configset_add_file(&cs, common_config_file); |
| 1053 | |
| 1054 | /* |
| 1055 | * If core.bare is true in the common config file, then we need to |
| 1056 | * move it to the main worktree's config file or it will break all |
| 1057 | * worktrees. If it is false, then leave it in place because it |
| 1058 | * _could_ be negating a global core.bare=true. |
| 1059 | */ |
| 1060 | if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) { |
| 1061 | if ((res = move_config_setting("core.bare", "true", |
| 1062 | common_config_file, |
| 1063 | main_worktree_file))) |
| 1064 | goto cleanup; |
| 1065 | } |
| 1066 | /* |
| 1067 | * If core.worktree is set, then the main worktree is located |
| 1068 | * somewhere different than the parent of the common Git dir. |
| 1069 | * Relocate that value to avoid breaking all worktrees with this |
| 1070 | * upgrade to worktree config. |
| 1071 | */ |
| 1072 | if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) { |
| 1073 | if ((res = move_config_setting("core.worktree", core_worktree, |
| 1074 | common_config_file, |
| 1075 | main_worktree_file))) |
| 1076 | goto cleanup; |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Ensure that we use worktree config for the remaining lifetime |
| 1081 | * of the current process. |
| 1082 | */ |
| 1083 | r->repository_format_worktree_config = 1; |
| 1084 | |
| 1085 | cleanup: |
| 1086 | git_configset_clear(&cs); |
| 1087 | free(common_config_file); |
no test coverage detected