git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ramkumar Ramachandra <artagnon@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Duy Nguyen <pclouds@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Git List <git@vger.kernel.org>
Subject: [PATCH 2/2] add: refuse to add paths beyond repository boundaries
Date: Tue,  9 Apr 2013 14:51:37 +0530	[thread overview]
Message-ID: <1365499297-8667-3-git-send-email-artagnon@gmail.com> (raw)
In-Reply-To: <1365499297-8667-1-git-send-email-artagnon@gmail.com>

Currently, git add has the logic for refusing to add gitlinks using
treat_path(), which in turn calls check_path_for_gitlink().  However,
this only checks for an in-index submodule (or gitlink cache_entry).
A path inside a git repository in the worktree still adds fine, and
this is a bug.  The logic for denying it is very similar to denying
adding paths beyond symbolic links: die_if_path_beyond_symlink().
Follow its example and write a die_if_path_beyond_gitrepo() to fix
this bug.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 builtin/add.c  |  5 +++--
 cache.h        |  2 ++
 pathspec.c     | 12 ++++++++++++
 pathspec.h     |  1 +
 symlinks.c     | 43 +++++++++++++++++++++++++++++++++++++------
 t/t3700-add.sh |  2 +-
 6 files changed, 56 insertions(+), 9 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index ab1c9e8..1538129 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -155,8 +155,8 @@ static void refresh(int verbose, const char **pathspec)
 
 /*
  * Normalizes argv relative to prefix, via get_pathspec(), and then
- * runs die_if_path_beyond_symlink() on each path in the normalized
- * list.
+ * runs die_if_path_beyond_symlink() and die_if_path_beyond_repository()
+ * on each path in the normalized list.
  */
 static const char **validate_pathspec(const char **argv, const char *prefix)
 {
@@ -166,6 +166,7 @@ static const char **validate_pathspec(const char **argv, const char *prefix)
 		const char **p;
 		for (p = pathspec; *p; p++) {
 			die_if_path_beyond_symlink(*p, prefix);
+			die_if_path_beyond_gitrepo(*p, prefix);
 		}
 	}
 
diff --git a/cache.h b/cache.h
index e1e8ce8..987d7f3 100644
--- a/cache.h
+++ b/cache.h
@@ -962,6 +962,8 @@ struct cache_def {
 
 extern int has_symlink_leading_path(const char *name, int len);
 extern int threaded_has_symlink_leading_path(struct cache_def *, const char *, int);
+extern int has_gitrepo_leading_path(const char *name, int len);
+extern int threaded_has_gitrepo_leading_path(struct cache_def *, const char *, int);
 extern int check_leading_path(const char *name, int len);
 extern int has_dirs_only_path(const char *name, int len, int prefix_len);
 extern void schedule_dir_for_removal(const char *name, int len);
diff --git a/pathspec.c b/pathspec.c
index 284f397..142631d 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -99,3 +99,15 @@ void die_if_path_beyond_symlink(const char *path, const char *prefix)
 		die(_("'%s' is beyond a symbolic link"), path + len);
 	}
 }
+
+/*
+ * Dies if the given path refers to a file inside a directory with a
+ * git repository in it.
+ */
+void die_if_path_beyond_gitrepo(const char *path, const char *prefix)
+{
+	if (has_gitrepo_leading_path(path, strlen(path))) {
+		int len = prefix ? strlen(prefix) : 0;
+		die(_("'%s' is beyond a git repository"), path + len);
+	}
+}
diff --git a/pathspec.h b/pathspec.h
index db0184a..c201c7b 100644
--- a/pathspec.h
+++ b/pathspec.h
@@ -5,5 +5,6 @@ extern char *find_pathspecs_matching_against_index(const char **pathspec);
 extern void add_pathspec_matches_against_index(const char **pathspec, char *seen, int specs);
 extern const char *check_path_for_gitlink(const char *path);
 extern void die_if_path_beyond_symlink(const char *path, const char *prefix);
+extern void die_if_path_beyond_gitrepo(const char *path, const char *prefix);
 
 #endif /* PATHSPEC_H */
diff --git a/symlinks.c b/symlinks.c
index c2b41a8..e551dae 100644
--- a/symlinks.c
+++ b/symlinks.c
@@ -54,6 +54,7 @@ static inline void reset_lstat_cache(struct cache_def *cache)
 #define FL_LSTATERR (1 << 3)
 #define FL_ERR      (1 << 4)
 #define FL_FULLPATH (1 << 5)
+#define FL_GITREPO  (1 << 6)
 
 /*
  * Check if name 'name' of length 'len' has a symlink leading
@@ -142,8 +143,22 @@ static int lstat_cache_matchlen(struct cache_def *cache,
 			if (errno == ENOENT)
 				*ret_flags |= FL_NOENT;
 		} else if (S_ISDIR(st.st_mode)) {
-			last_slash_dir = last_slash;
-			continue;
+			/* Check to see if the directory contains a
+			   git repository */
+			struct stat st;
+			struct strbuf dotgitentry = STRBUF_INIT;
+			strbuf_addf(&dotgitentry, "%s/.git", cache->path);
+			if (lstat(dotgitentry.buf, &st) < 0) {
+				if (errno == ENOENT) {
+					strbuf_release(&dotgitentry);
+					last_slash_dir = last_slash;
+					continue;
+				}
+				*ret_flags = FL_LSTATERR;
+			}
+			else
+				*ret_flags = FL_GITREPO;
+			strbuf_release(&dotgitentry);
 		} else if (S_ISLNK(st.st_mode)) {
 			*ret_flags = FL_SYMLINK;
 		} else {
@@ -153,11 +168,11 @@ static int lstat_cache_matchlen(struct cache_def *cache,
 	}
 
 	/*
-	 * At the end update the cache.  Note that max 3 different
-	 * path types, FL_NOENT, FL_SYMLINK and FL_DIR, can be cached
-	 * for the moment!
+	 * At the end update the cache.  Note that max 4 different
+	 * path types: FL_NOENT, FL_SYMLINK, FL_GITREPO, and
+	 * FL_DIR.
 	 */
-	save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK);
+	save_flags = *ret_flags & track_flags & (FL_NOENT|FL_SYMLINK|FL_GITREPO);
 	if (save_flags && last_slash > 0 && last_slash <= PATH_MAX) {
 		cache->path[last_slash] = '\0';
 		cache->len = last_slash;
@@ -204,6 +219,14 @@ int threaded_has_symlink_leading_path(struct cache_def *cache, const char *name,
 }
 
 /*
+ * Return non-zero if path 'name' has a leading gitrepo component
+ */
+int threaded_has_gitrepo_leading_path(struct cache_def *cache, const char *name, int len)
+{
+	return lstat_cache(cache, name, len, FL_GITREPO|FL_DIR, USE_ONLY_LSTAT) & FL_GITREPO;
+}
+
+/*
  * Return non-zero if path 'name' has a leading symlink component
  */
 int has_symlink_leading_path(const char *name, int len)
@@ -212,6 +235,14 @@ int has_symlink_leading_path(const char *name, int len)
 }
 
 /*
+ * Return non-zero if path 'name' has a leading gitrepo component
+ */
+int has_gitrepo_leading_path(const char *name, int len)
+{
+	return threaded_has_gitrepo_leading_path(&default_cache, name, len);
+}
+
+/*
  * Return zero if path 'name' has a leading symlink component or
  * if some leading path component does not exists.
  *
diff --git a/t/t3700-add.sh b/t/t3700-add.sh
index 1ad2331..4714734 100755
--- a/t/t3700-add.sh
+++ b/t/t3700-add.sh
@@ -327,7 +327,7 @@ test_expect_success 'git add should not go past gitlink boundaries' '
 	test_must_fail git add submodule_dir/foo
 '
 
-test_expect_failure 'git add should not go past git repository boundaries' '
+test_expect_success 'git add should not go past git repository boundaries' '
 	rm -rf submodule_dir &&
 	mkdir submodule_dir &&
 	(
-- 
1.8.2.1.347.gdd82260.dirty

  parent reply	other threads:[~2013-04-09  9:20 UTC|newest]

Thread overview: 140+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-04 18:30 [RFC/PATCH 0/7] Rework git core for native submodules Ramkumar Ramachandra
2013-04-04 18:30 ` [PATCH 1/7] link.c, link.h: introduce fifth object type Ramkumar Ramachandra
2013-04-04 18:30 ` [PATCH 2/7] sha1_file, link: write link objects to the database Ramkumar Ramachandra
2013-04-05  7:11   ` Ramkumar Ramachandra
2013-04-05  7:59     ` Ramkumar Ramachandra
2013-04-04 18:30 ` [PATCH 3/7] teach ce_compare_gitlink() about OBJ_LINK Ramkumar Ramachandra
2013-04-04 18:30 ` [PATCH 4/7] builtin/log: teach show " Ramkumar Ramachandra
2013-04-04 18:30 ` [PATCH 5/7] edit-link: add new builtin Ramkumar Ramachandra
2013-04-04 18:30 ` [PATCH 6/7] clone: introduce clone.submodulegitdir Ramkumar Ramachandra
2013-04-05  7:07   ` Ramkumar Ramachandra
2013-04-04 18:30 ` [PATCH 7/7] sha1_file: write ref_name to link object Ramkumar Ramachandra
2013-04-05  7:03   ` Ramkumar Ramachandra
2013-04-04 18:40 ` [RFC/PATCH 0/7] Rework git core for native submodules Linus Torvalds
2013-04-04 18:52   ` Ramkumar Ramachandra
2013-04-04 19:04     ` Linus Torvalds
2013-04-04 19:17       ` Junio C Hamano
2013-04-04 19:59         ` Ramkumar Ramachandra
2013-04-04 20:28         ` Jens Lehmann
2013-04-04 19:36       ` Ramkumar Ramachandra
2013-04-04 19:44         ` Linus Torvalds
2013-04-04 19:52           ` Ramkumar Ramachandra
2013-04-04 20:08             ` Ramkumar Ramachandra
2013-04-04 20:04           ` Ramkumar Ramachandra
2013-04-05 16:02             ` Linus Torvalds
2013-04-05 16:37               ` Ramkumar Ramachandra
2013-04-04 19:42       ` Ramkumar Ramachandra
2013-04-04 21:20       ` Jens Lehmann
2013-04-04 21:35         ` Ramkumar Ramachandra
2013-04-04 22:13         ` Junio C Hamano
2013-04-04 22:18           ` Ramkumar Ramachandra
2013-04-04 22:26             ` Junio C Hamano
2013-04-04 22:32               ` Ramkumar Ramachandra
2013-04-04 23:08                 ` Junio C Hamano
2013-04-04 23:14                   ` Ramkumar Ramachandra
2013-04-05 17:07                     ` Junio C Hamano
2013-04-05 17:23                       ` Ramkumar Ramachandra
2013-04-05  6:53     ` Ramkumar Ramachandra
2013-04-04 18:47 ` Jonathan Nieder
2013-04-04 18:58   ` Jonathan Nieder
2013-04-04 18:55 ` Jonathan Nieder
2013-04-08 10:10   ` Duy Nguyen
2013-04-08 10:26     ` [PATCH] t3700 (add): add failing test for add with submodules Ramkumar Ramachandra
2013-04-08 11:04       ` Duy Nguyen
2013-04-08 15:07         ` Junio C Hamano
2013-04-08 21:30       ` Jeff King
2013-04-08 22:03         ` Junio C Hamano
2013-04-08 22:07           ` Jeff King
2013-04-09  9:19         ` Ramkumar Ramachandra
2013-04-09  9:21           ` [PATCH 0/2] Fix git " Ramkumar Ramachandra
2013-04-09  9:21             ` [PATCH 1/2] t3700 (add): add two tests for testing " Ramkumar Ramachandra
2013-04-09  9:21             ` Ramkumar Ramachandra [this message]
2013-04-09 16:50               ` [PATCH 2/2] add: refuse to add paths beyond repository boundaries Jeff King
2013-04-09 17:09               ` Junio C Hamano
2013-04-09 17:34                 ` Junio C Hamano
2013-04-09 17:41                   ` Ramkumar Ramachandra
2013-04-09 17:54                     ` Junio C Hamano
2013-04-09 18:17                       ` Ramkumar Ramachandra
2013-04-09 18:50                         ` Junio C Hamano
2013-04-09 19:09                           ` Junio C Hamano
2013-04-09 20:31                         ` Junio C Hamano
2013-04-10 13:25                           ` Ramkumar Ramachandra
2013-04-10 16:25                             ` Junio C Hamano
2013-04-09 17:41                   ` Junio C Hamano
2013-04-09 17:56                     ` Ramkumar Ramachandra
2013-04-09 18:48                       ` Junio C Hamano
2013-04-10 13:38                         ` Ramkumar Ramachandra
2013-04-09 18:32                   ` Jakub Narębski
2013-04-09 18:51                     ` Junio C Hamano
2013-04-09 18:58                       ` Jakub Narębski
2013-04-09 19:10                         ` Junio C Hamano
2013-04-09 16:27           ` [PATCH] t3700 (add): add failing test for add with submodules Jeff King
2013-04-09 11:43         ` Jakub Narębski
2013-04-09 11:54           ` Ramkumar Ramachandra
2013-04-09 13:49             ` Jakub Narębski
2013-04-06 20:10 ` [RFC/PATCH 0/7] Rework git core for native submodules Ramkumar Ramachandra
2013-04-07  3:31   ` Junio C Hamano
2013-04-07  7:27     ` Ramkumar Ramachandra
2013-04-07  9:00       ` Junio C Hamano
2013-04-07 10:58         ` Ramkumar Ramachandra
2013-04-07 15:51         ` Ramkumar Ramachandra
2013-04-07 16:12           ` John Keeping
2013-04-07 16:42             ` Ramkumar Ramachandra
2013-04-07 17:02               ` John Keeping
2013-04-07 17:22                 ` Ramkumar Ramachandra
2013-04-07 17:52                   ` John Keeping
2013-04-07 18:07                     ` Ramkumar Ramachandra
2013-04-07 18:21                       ` John Keeping
2013-04-07 18:34                         ` Jens Lehmann
2013-04-07 18:44                           ` Ramkumar Ramachandra
2013-04-07 20:15                             ` Jens Lehmann
2013-04-07 20:49                               ` Ramkumar Ramachandra
2013-04-07 21:02                                 ` John Keeping
2013-04-07 21:11                                   ` Ramkumar Ramachandra
2013-04-07 20:57                               ` Ramkumar Ramachandra
2013-04-07 21:23                                 ` Jonathan Nieder
2013-04-07 21:30                                   ` Ramkumar Ramachandra
2013-04-08  7:48                                     ` Jens Lehmann
2013-04-08  8:07                                       ` Ramkumar Ramachandra
2013-04-08  8:19                                         ` Jonathan Nieder
2013-04-08  9:08                                           ` Ramkumar Ramachandra
2013-04-08 10:29                                             ` Duy Nguyen
2013-04-08 11:06                                               ` Ramkumar Ramachandra
2013-04-08 11:29                                                 ` Duy Nguyen
2013-04-08 11:53                                                   ` Ramkumar Ramachandra
2013-04-08 15:06                                                     ` Junio C Hamano
2013-04-08 16:08                                                       ` Ramkumar Ramachandra
2013-04-08 18:10                                                         ` Junio C Hamano
2013-04-08 19:03                                                           ` Ramkumar Ramachandra
2013-04-08 19:48                                                             ` Junio C Hamano
2013-04-08 19:54                                                               ` Ramkumar Ramachandra
2013-04-08 20:30                                                                 ` Junio C Hamano
2013-04-08 21:03                                                                   ` Ramkumar Ramachandra
2013-04-10  7:23                                                                     ` Philip Oakley
2013-04-08 21:59                                                                   ` Ramkumar Ramachandra
2013-04-09 11:51                                                           ` Jakub Narębski
2013-04-08 11:10                                               ` Ramkumar Ramachandra
2013-04-08  8:37                                         ` Jonathan Nieder
2013-04-08  9:14                                           ` Ramkumar Ramachandra
2013-04-08 14:46                                           ` Junio C Hamano
2013-04-08 17:12                                             ` Junio C Hamano
2013-04-17 10:37                                   ` Duy Nguyen
2013-04-17 11:06                                     ` Ramkumar Ramachandra
2013-04-17 11:27                                       ` Duy Nguyen
2013-04-17 11:56                                         ` Ramkumar Ramachandra
2013-04-17 12:06                                           ` Duy Nguyen
2013-04-17 12:14                                             ` Ramkumar Ramachandra
     [not found]                                         ` <CALkWK0m9QmZaSDruY=+2F-Kkw+fd6E1TYC TBpVQHRJrzq2VjCQ@mail.gmail.com>
2013-04-17 23:17                                           ` Philip Oakley
2013-04-18  7:50                                             ` Ramkumar Ramachandra
2013-04-19 17:08                                             ` Jens Lehmann
2013-04-17 16:01                                     ` Junio C Hamano
2013-04-08 20:41                               ` Jens Lehmann
2013-04-08 21:36                                 ` Jeff King
2013-04-07 18:59                           ` John Keeping
2013-04-07 19:06                             ` Ramkumar Ramachandra
2013-04-07 19:17                               ` Ramkumar Ramachandra
2013-04-07 18:37                         ` Ramkumar Ramachandra
2013-04-07 18:22                       ` Ramkumar Ramachandra
2013-04-07 19:26           ` Ramkumar Ramachandra
     [not found]             ` <CAP8UFD3i2vc3OSAHRERpiPY7cRjqhkqcBN9hVW0QmMksnCPccw@mail.gmail.com>
2013-04-07 21:24               ` Ramkumar Ramachandra
     [not found]                 ` <CAP8UFD16gwWjE7T75D7kUM-VOXhtZaSRGtEg8fW5kmuKDLTQHQ@mail.gmail.com>
2013-04-08 17:04                   ` Ramkumar Ramachandra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1365499297-8667-3-git-send-email-artagnon@gmail.com \
    --to=artagnon@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).