@@ -93,6 +93,7 @@ static git_config_backend *open_gitmodules(git_repository *repo, int gitmod);
9393static git_config * gitmodules_snapshot (git_repository * repo );
9494static int get_url_base (git_buf * url , git_repository * repo );
9595static int lookup_head_remote_key (git_buf * remote_key , git_repository * repo );
96+ static int lookup_default_remote (git_remote * * remote , git_repository * repo );
9697static int submodule_load_each (const git_config_entry * entry , void * payload );
9798static int submodule_read_config (git_submodule * sm , git_config * cfg );
9899static int submodule_load_from_wd_lite (git_submodule * );
@@ -1131,7 +1132,7 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio
11311132 if (error != GIT_ENOTFOUND )
11321133 goto done ;
11331134
1134- if (error == GIT_ENOTFOUND && !init ) {
1135+ if (!init ) {
11351136 giterr_set (GITERR_SUBMODULE , "Submodule is not initialized." );
11361137 error = GIT_ERROR ;
11371138 goto done ;
@@ -1171,9 +1172,20 @@ int git_submodule_update(git_submodule *sm, int init, git_submodule_update_optio
11711172 * update the workdir contents of the subrepository, and set the subrepository's
11721173 * head to the new commit.
11731174 */
1174- if ((error = git_submodule_open (& sub_repo , sm )) < 0 ||
1175- (error = git_object_lookup (& target_commit , sub_repo , git_submodule_index_id (sm ), GIT_OBJ_COMMIT )) < 0 ||
1176- (error = git_checkout_tree (sub_repo , target_commit , & update_options .checkout_opts )) != 0 ||
1175+ if ((error = git_submodule_open (& sub_repo , sm )) < 0 )
1176+ goto done ;
1177+
1178+ /* Look up the target commit in the submodule. */
1179+ if ((error = git_object_lookup (& target_commit , sub_repo , git_submodule_index_id (sm ), GIT_OBJ_COMMIT )) < 0 ) {
1180+ /* If it isn't found then fetch and try again. */
1181+ if (error != GIT_ENOTFOUND || !update_options .allow_fetch ||
1182+ (error = lookup_default_remote (& remote , sub_repo )) < 0 ||
1183+ (error = git_remote_fetch (remote , NULL , & update_options .fetch_opts , NULL )) < 0 ||
1184+ (error = git_object_lookup (& target_commit , sub_repo , git_submodule_index_id (sm ), GIT_OBJ_COMMIT )) < 0 )
1185+ goto done ;
1186+ }
1187+
1188+ if ((error = git_checkout_tree (sub_repo , target_commit , & update_options .checkout_opts )) != 0 ||
11771189 (error = git_repository_set_head_detached (sub_repo , git_submodule_index_id (sm ))) < 0 )
11781190 goto done ;
11791191
0 commit comments