@@ -250,6 +250,7 @@ N_("The following paths are ignored by one of your .gitignore files:\n");
250250
251251static int verbose , show_only , ignored_too , refresh_only ;
252252static int ignore_add_errors , intent_to_add , ignore_missing ;
253+ static int warn_on_embedded_repo = 1 ;
253254
254255#define ADDREMOVE_DEFAULT 1
255256static int addremove = ADDREMOVE_DEFAULT ;
@@ -283,6 +284,8 @@ static struct option builtin_add_options[] = {
283284 OPT_BOOL ( 0 , "ignore-errors" , & ignore_add_errors , N_ ("just skip files which cannot be added because of errors" )),
284285 OPT_BOOL ( 0 , "ignore-missing" , & ignore_missing , N_ ("check if - even missing - files are ignored in dry run" )),
285286 OPT_STRING ( 0 , "chmod" , & chmod_arg , N_ ("(+/-)x" ), N_ ("override the executable bit of the listed files" )),
287+ OPT_HIDDEN_BOOL (0 , "warn-embedded-repo" , & warn_on_embedded_repo ,
288+ N_ ("warn when adding an embedded repository" )),
286289 OPT_END (),
287290};
288291
@@ -296,6 +299,45 @@ static int add_config(const char *var, const char *value, void *cb)
296299 return git_default_config (var , value , cb );
297300}
298301
302+ static const char embedded_advice [] = N_ (
303+ "You've added another git repository inside your current repository.\n"
304+ "Clones of the outer repository will not contain the contents of\n"
305+ "the embedded repository and will not know how to obtain it.\n"
306+ "If you meant to add a submodule, use:\n"
307+ "\n"
308+ " git submodule add <url> %s\n"
309+ "\n"
310+ "If you added this path by mistake, you can remove it from the\n"
311+ "index with:\n"
312+ "\n"
313+ " git rm --cached %s\n"
314+ "\n"
315+ "See \"git help submodule\" for more information."
316+ );
317+
318+ static void check_embedded_repo (const char * path )
319+ {
320+ struct strbuf name = STRBUF_INIT ;
321+
322+ if (!warn_on_embedded_repo )
323+ return ;
324+ if (!ends_with (path , "/" ))
325+ return ;
326+
327+ /* Drop trailing slash for aesthetics */
328+ strbuf_addstr (& name , path );
329+ strbuf_strip_suffix (& name , "/" );
330+
331+ warning (_ ("adding embedded git repository: %s" ), name .buf );
332+ if (advice_add_embedded_repo ) {
333+ advise (embedded_advice , name .buf , name .buf );
334+ /* there may be multiple entries; advise only once */
335+ advice_add_embedded_repo = 0 ;
336+ }
337+
338+ strbuf_release (& name );
339+ }
340+
299341static int add_files (struct dir_struct * dir , int flags )
300342{
301343 int i , exit_status = 0 ;
@@ -308,12 +350,14 @@ static int add_files(struct dir_struct *dir, int flags)
308350 exit_status = 1 ;
309351 }
310352
311- for (i = 0 ; i < dir -> nr ; i ++ )
353+ for (i = 0 ; i < dir -> nr ; i ++ ) {
354+ check_embedded_repo (dir -> entries [i ]-> name );
312355 if (add_file_to_index (& the_index , dir -> entries [i ]-> name , flags )) {
313356 if (!ignore_add_errors )
314357 die (_ ("adding files failed" ));
315358 exit_status = 1 ;
316359 }
360+ }
317361 return exit_status ;
318362}
319363
0 commit comments