travis-ci: record and skip successfully built trees
[gitweb.git] / builtin / add.c
index b1dddb4ac635ab7e2cc55be546002f8e68efa331..e888fb8c5f2a1fa2be33e834724460dec8072726 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) 2006 Linus Torvalds
  */
 #include "cache.h"
+#include "config.h"
 #include "builtin.h"
 #include "lockfile.h"
 #include "dir.h"
@@ -17,6 +18,7 @@
 #include "revision.h"
 #include "bulk-checkin.h"
 #include "argv-array.h"
+#include "submodule.h"
 
 static const char * const builtin_add_usage[] = {
        N_("git add [<options>] [--] <pathspec>..."),
@@ -26,10 +28,25 @@ static int patch_interactive, add_interactive, edit_interactive;
 static int take_worktree_changes;
 
 struct update_callback_data {
-       int flags, force_mode;
+       int flags;
        int add_errors;
 };
 
+static void chmod_pathspec(struct pathspec *pathspec, int force_mode)
+{
+       int i;
+
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+
+               if (pathspec && !ce_path_match(ce, pathspec, NULL))
+                       continue;
+
+               if (chmod_cache_entry(ce, force_mode) < 0)
+                       fprintf(stderr, "cannot chmod '%s'", ce->name);
+       }
+}
+
 static int fix_unmerged_status(struct diff_filepair *p,
                               struct update_callback_data *data)
 {
@@ -65,8 +82,7 @@ static void update_callback(struct diff_queue_struct *q,
                        die(_("unexpected diff status %c"), p->status);
                case DIFF_STATUS_MODIFIED:
                case DIFF_STATUS_TYPE_CHANGED:
-                       if (add_file_to_index(&the_index, path,
-                                       data->flags, data->force_mode)) {
+                       if (add_file_to_index(&the_index, path, data->flags)) {
                                if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
                                        die(_("updating files failed"));
                                data->add_errors++;
@@ -84,15 +100,14 @@ static void update_callback(struct diff_queue_struct *q,
        }
 }
 
-int add_files_to_cache(const char *prefix, const struct pathspec *pathspec,
-       int flags, int force_mode)
+int add_files_to_cache(const char *prefix,
+                      const struct pathspec *pathspec, int flags)
 {
        struct update_callback_data data;
        struct rev_info rev;
 
        memset(&data, 0, sizeof(data));
        data.flags = flags;
-       data.force_mode = force_mode;
 
        init_revisions(&rev, prefix);
        setup_revisions(0, NULL, &rev, NULL);
@@ -122,7 +137,7 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
                        *dst++ = entry;
        }
        dir->nr = dst - dir->entries;
-       add_pathspec_matches_against_index(pathspec, seen);
+       add_pathspec_matches_against_index(pathspec, &the_index, seen);
        return seen;
 }
 
@@ -235,6 +250,7 @@ N_("The following paths are ignored by one of your .gitignore files:\n");
 
 static int verbose, show_only, ignored_too, refresh_only;
 static int ignore_add_errors, intent_to_add, ignore_missing;
+static int warn_on_embedded_repo = 1;
 
 #define ADDREMOVE_DEFAULT 1
 static int addremove = ADDREMOVE_DEFAULT;
@@ -268,6 +284,8 @@ static struct option builtin_add_options[] = {
        OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
        OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
        OPT_STRING( 0 , "chmod", &chmod_arg, N_("(+/-)x"), N_("override the executable bit of the listed files")),
+       OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo,
+                       N_("warn when adding an embedded repository")),
        OPT_END(),
 };
 
@@ -281,7 +299,46 @@ static int add_config(const char *var, const char *value, void *cb)
        return git_default_config(var, value, cb);
 }
 
-static int add_files(struct dir_struct *dir, int flags, int force_mode)
+static const char embedded_advice[] = N_(
+"You've added another git repository inside your current repository.\n"
+"Clones of the outer repository will not contain the contents of\n"
+"the embedded repository and will not know how to obtain it.\n"
+"If you meant to add a submodule, use:\n"
+"\n"
+"      git submodule add <url> %s\n"
+"\n"
+"If you added this path by mistake, you can remove it from the\n"
+"index with:\n"
+"\n"
+"      git rm --cached %s\n"
+"\n"
+"See \"git help submodule\" for more information."
+);
+
+static void check_embedded_repo(const char *path)
+{
+       struct strbuf name = STRBUF_INIT;
+
+       if (!warn_on_embedded_repo)
+               return;
+       if (!ends_with(path, "/"))
+               return;
+
+       /* Drop trailing slash for aesthetics */
+       strbuf_addstr(&name, path);
+       strbuf_strip_suffix(&name, "/");
+
+       warning(_("adding embedded git repository: %s"), name.buf);
+       if (advice_add_embedded_repo) {
+               advise(embedded_advice, name.buf, name.buf);
+               /* there may be multiple entries; advise only once */
+               advice_add_embedded_repo = 0;
+       }
+
+       strbuf_release(&name);
+}
+
+static int add_files(struct dir_struct *dir, int flags)
 {
        int i, exit_status = 0;
 
@@ -293,13 +350,14 @@ static int add_files(struct dir_struct *dir, int flags, int force_mode)
                exit_status = 1;
        }
 
-       for (i = 0; i < dir->nr; i++)
-               if (add_file_to_index(&the_index, dir->entries[i]->name,
-                               flags, force_mode)) {
+       for (i = 0; i < dir->nr; i++) {
+               check_embedded_repo(dir->entries[i]->name);
+               if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) {
                        if (!ignore_add_errors)
                                die(_("adding files failed"));
                        exit_status = 1;
                }
+       }
        return exit_status;
 }
 
@@ -308,7 +366,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        int exit_status = 0;
        struct pathspec pathspec;
        struct dir_struct dir;
-       int flags, force_mode;
+       int flags;
        int add_new_files;
        int require_pathspec;
        char *seen = NULL;
@@ -342,19 +400,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        if (!show_only && ignore_missing)
                die(_("Option --ignore-missing can only be used together with --dry-run"));
 
-       if (!chmod_arg)
-               force_mode = 0;
-       else if (!strcmp(chmod_arg, "-x"))
-               force_mode = 0666;
-       else if (!strcmp(chmod_arg, "+x"))
-               force_mode = 0777;
-       else
+       if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') ||
+                         chmod_arg[1] != 'x' || chmod_arg[2]))
                die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
 
        add_new_files = !take_worktree_changes && !refresh_only;
        require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
 
-       hold_locked_index(&lock_file, 1);
+       hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
 
        flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
                 (show_only ? ADD_CACHE_PRETEND : 0) |
@@ -372,16 +425,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        if (read_cache() < 0)
                die(_("index file corrupt"));
 
+       die_in_unpopulated_submodule(&the_index, prefix);
+
        /*
         * Check the "pathspec '%s' did not match any files" block
         * below before enabling new magic.
         */
        parse_pathspec(&pathspec, 0,
                       PATHSPEC_PREFER_FULL |
-                      PATHSPEC_SYMLINK_LEADING_PATH |
-                      PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,
+                      PATHSPEC_SYMLINK_LEADING_PATH,
                       prefix, argv);
 
+       die_path_inside_submodule(&the_index, &pathspec);
+
        if (add_new_files) {
                int baselen;
 
@@ -393,7 +449,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                }
 
                /* This picks up the paths that are not tracked */
-               baselen = fill_directory(&dir, &pathspec);
+               baselen = fill_directory(&dir, &the_index, &pathspec);
                if (pathspec.nr)
                        seen = prune_directory(&dir, &pathspec, baselen);
        }
@@ -407,7 +463,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                int i;
 
                if (!seen)
-                       seen = find_pathspecs_matching_against_index(&pathspec);
+                       seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
 
                /*
                 * file_exists() assumes exact match
@@ -429,8 +485,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                             !file_exists(path))) {
                                if (ignore_missing) {
                                        int dtype = DT_UNKNOWN;
-                                       if (is_excluded(&dir, path, &dtype))
-                                               dir_add_ignored(&dir, path, pathspec.items[i].len);
+                                       if (is_excluded(&dir, &the_index, path, &dtype))
+                                               dir_add_ignored(&dir, &the_index,
+                                                               path, pathspec.items[i].len);
                                } else
                                        die(_("pathspec '%s' did not match any files"),
                                            pathspec.items[i].original);
@@ -441,11 +498,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
        plug_bulk_checkin();
 
-       exit_status |= add_files_to_cache(prefix, &pathspec, flags, force_mode);
+       exit_status |= add_files_to_cache(prefix, &pathspec, flags);
 
        if (add_new_files)
-               exit_status |= add_files(&dir, flags, force_mode);
+               exit_status |= add_files(&dir, flags);
 
+       if (chmod_arg && pathspec.nr)
+               chmod_pathspec(&pathspec, chmod_arg[0]);
        unplug_bulk_checkin();
 
 finish: