git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/8] sha1_file: mark alt object database from add_submodule_odb()

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Apr 30, 2013, 03:42 UTC
Message-ID
<1367293372-1958-4-git-send-email-pclouds@gmail.com>
In-Reply-To
<1367293372-1958-1-git-send-email-pclouds@gmail.com>

A submodule's object database may be imported to in-core object pool for a quick peek without paying the price of running a separate git command. These databases are marked in for stricter checks later to avoid accidentially refering to a submodule's SHA-1 from main repo (except in gitlinks).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 cache.h       |  4 +++-
 environment.c |  2 ++
 sha1_file.c   | 37 ++++++++++++++++++++++++++++---------
 submodule.c   |  2 +-
 4 files changed, 34 insertions(+), 11 deletions(-)
diff --git a/cache.h b/cache.h
index dfb78ca..b8d8e03 100644
--- a/cache.h
+++ b/cache.h
@@ -571,6 +571,7 @@ extern int fsync_object_files;
 extern int core_preload_index;
 extern int core_apply_sparse_checkout;
 extern int precomposed_unicode;
+extern int object_database_contaminated;
 
 /*
  * The character that begins a commented line in user-editable file
@@ -993,11 +994,12 @@ extern void remove_scheduled_dirs(void);
 
 extern struct alternate_object_database {
 	struct alternate_object_database *next;
+	int external;
 	char *name;
 	char base[FLEX_ARRAY]; /* more */
 } *alt_odb_list;
 extern void prepare_alt_odb(void);
-extern void read_info_alternates(const char * relative_base, int depth);
+extern void read_external_info_alternates(const char * relative_base, int depth);
 extern void add_to_alternates_file(const char *reference);
 typedef int alt_odb_fn(struct alternate_object_database *, void *);
 extern void foreach_alt_odb(alt_odb_fn, void*);
diff --git a/environment.c b/environment.c
index e2e75c1..5bed7fc 100644
--- a/environment.c
+++ b/environment.c
@@ -72,6 +72,8 @@ char comment_line_char = '#';
 /* Parallel index stat data preload? */
 int core_preload_index = 0;
 
+int object_database_contaminated;
+
 /* This is set by setup_git_dir_gently() and/or git_default_config() */
 char *git_work_tree_cfg;
 static char *work_tree;
diff --git a/sha1_file.c b/sha1_file.c
index 1a744ae..eb682b3 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -65,6 +65,8 @@ static struct cached_object empty_tree = {
 
 static struct packed_git *last_found_pack;
 
+static void read_info_alternates(const char * relative_base,
+				 int depth, int external);
 static struct cached_object *find_cached_object(const unsigned char *sha1,
 						unsigned int origin)
 {
@@ -263,7 +265,10 @@ static int git_open_noatime(const char *name);
  * SHA1, an extra slash for the first level indirection, and the
  * terminating NUL.
  */
-static int link_alt_odb_entry(const char *entry, const char *relative_base, int depth)
+static int link_alt_odb_entry(const char *entry,
+			      const char *relative_base,
+			      int depth,
+			      int external)
 {
 	const char *objdir = get_object_directory();
 	struct alternate_object_database *ent;
@@ -293,6 +298,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base, int
 	memcpy(ent->base, pathbuf.buf, pfxlen);
 	strbuf_release(&pathbuf);
 
+	ent->external = external;
 	ent->name = ent->base + pfxlen + 1;
 	ent->base[pfxlen + 3] = '/';
 	ent->base[pfxlen] = ent->base[entlen-1] = 0;
@@ -326,15 +332,19 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base, int
 	ent->next = NULL;
 
 	/* recursively add alternates */
-	read_info_alternates(ent->base, depth + 1);
+	read_info_alternates(ent->base, depth + 1, 0);
 
 	ent->base[pfxlen] = '/';
 
+	if (external)
+		object_database_contaminated = 1;
+
 	return 0;
 }
 
 static void link_alt_odb_entries(const char *alt, int len, int sep,
-				 const char *relative_base, int depth)
+				 const char *relative_base,
+				 int depth, int external)
 {
 	struct string_list entries = STRING_LIST_INIT_NODUP;
 	char *alt_copy;
@@ -356,14 +366,16 @@ static void link_alt_odb_entries(const char *alt, int len, int sep,
 			error("%s: ignoring relative alternate object store %s",
 					relative_base, entry);
 		} else {
-			link_alt_odb_entry(entry, relative_base, depth);
+			link_alt_odb_entry(entry, relative_base,
+					   depth, external);
 		}
 	}
 	string_list_clear(&entries, 0);
 	free(alt_copy);
 }
 
-void read_info_alternates(const char * relative_base, int depth)
+static void read_info_alternates(const char * relative_base,
+				 int depth, int external)
 {
 	char *map;
 	size_t mapsz;
@@ -387,11 +399,18 @@ void read_info_alternates(const char * relative_base, int depth)
 	map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
 	close(fd);
 
-	link_alt_odb_entries(map, mapsz, '\n', relative_base, depth);
+	link_alt_odb_entries(map, mapsz, '\n', relative_base,
+			     depth, external);
 
 	munmap(map, mapsz);
 }
 
+void read_external_info_alternates(const char *relative_base,
+				   int depth)
+{
+	read_info_alternates(relative_base, depth, 1);
+}
+
 void add_to_alternates_file(const char *reference)
 {
 	struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
@@ -401,7 +420,7 @@ void add_to_alternates_file(const char *reference)
 	if (commit_lock_file(lock))
 		die("could not close alternates file");
 	if (alt_odb_tail)
-		link_alt_odb_entries(alt, strlen(alt), '\n', NULL, 0);
+		link_alt_odb_entries(alt, strlen(alt), '\n', NULL, 0, 0);
 }
 
 void foreach_alt_odb(alt_odb_fn fn, void *cb)
@@ -431,9 +450,9 @@ void prepare_alt_odb(void)
 	if (!alt) alt = "";
 
 	alt_odb_tail = &alt_odb_list;
-	link_alt_odb_entries(alt, strlen(alt), PATH_SEP, NULL, 0);
+	link_alt_odb_entries(alt, strlen(alt), PATH_SEP, NULL, 0, 0);
 
-	read_info_alternates(get_object_directory(), 0);
+	read_info_alternates(get_object_directory(), 0, 0);
 }
 
 static int has_loose_object_extended(const unsigned char *sha1,
diff --git a/submodule.c b/submodule.c
index e728025..3cb63f7 100644
--- a/submodule.c
+++ b/submodule.c
@@ -65,7 +65,7 @@ static int add_submodule_odb(const char *path)
 	alt_odb_list = alt_odb;
 
 	/* add possible alternates from the submodule */
-	read_info_alternates(objects_directory.buf, 0);
+	read_external_info_alternates(objects_directory.buf, 0);
 	prepare_alt_odb();
 done:
 	strbuf_release(&objects_directory);
-- 
1.8.2.83.gc99314b
Previous: Nguyễn Thái Ngọc DuyNext: Eric Sunshine
Message 5 of 14 in “Some object db protection when add_submodule_odb is used”
  1. 0/8 Some object db protection when add_submodule_odb is usedNguyễn Thái Ngọc Duy, Apr 30, 2013
  2. 1/8 sha1_file: allow to select pack origin when looking up an objectNguyễn Thái Ngọc Duy, Apr 30, 2013
  3. Eric SunshineApr 30, 2013
  4. 2/8 sha1_file: keep track of alternate source of objectsNguyễn Thái Ngọc Duy, Apr 30, 2013
  5. 3/8 sha1_file: mark alt object database from add_submodule_odb()Nguyễn Thái Ngọc Duy, Apr 30, 2013
  6. Eric SunshineApr 30, 2013
  7. 4/8 sha1_file: new object source for submodule's alt object databaseNguyễn Thái Ngọc Duy, Apr 30, 2013
  8. Eric SunshineApr 30, 2013
  9. 5/8 commit.c: refuse to write commits referring to external objectsNguyễn Thái Ngọc Duy, Apr 30, 2013
  10. 6/8 cache-tree.c: refuse to write trees referring to external objectsNguyễn Thái Ngọc Duy, Apr 30, 2013
  11. 7/8 mktag: refuse to write tags referring to external objectsNguyễn Thái Ngọc Duy, Apr 30, 2013
  12. 8/8 sha1_file: do write objects even if found in ODB_EXTALT databaseNguyễn Thái Ngọc Duy, Apr 30, 2013
  13. Thomas RastApr 30, 2013
  14. Duy NguyenApr 30, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.