Skip to contentSkip to conversation

Implement 'git archive --submodules'

PATCH In review Lars Hjemli 5 participants 26 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

cover Implement 'git archive --submodules'

This series teaches read_tree_recursive() how to traverse into gitlinked repositories by automatically adding submodule object databases as alternates during traversal. It is still perfectly legal for a submodule not to be checked out, in which case the submodule will be ignored.

On top of this, the implementation of 'git archive --submodules' simply activates the new feature in read_tree_recursive().

Lars Hjemli (3):
sha1_file: add function to insert alternate object db Teach read_tree_recursive() how to traverse into submodules
git-archive: add support for --submodules

 Documentation/git-archive.txt |    7 +++-
 archive.c                     |    4 ++
 cache.h                       |    3 ++
 environment.c                 |   12 ++++++
 sha1_file.c                   |    5 +++
 t/t5001-archive-submodules.sh |   78 +++++++++++++++++++++++++++++++++++++++
 tree.c                        |   80 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 187 insertions(+), 2 deletions(-)
 create mode 100755 t/t5001-archive-submodules.sh
#1 lore raw

PATCH 1/3 sha1_file: add function to insert alternate object db

This function will be used when implementing traversal into submodules.

---
 cache.h     |    1 +
 sha1_file.c |    5 +++++
 2 files changed, 6 insertions(+), 0 deletions(-)
cache.h +1 -0
index 8e1af26..daa2d4e 100644
@@ -724,6 +724,7 @@ extern struct alternate_object_database {
 	char base[FLEX_ARRAY]; /* more */
 } *alt_odb_list;
 extern void prepare_alt_odb(void);
+extern int add_alt_odb(const char *path);
 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*);
sha1_file.c +5 -0
index f08493f..19f9725 100644
@@ -356,6 +356,11 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
 	}
 }
 
+int add_alt_odb(const char *path)
+{
+	return link_alt_odb_entry(path, strlen(path), NULL, 0);
+}
+
 static void read_info_alternates(const char * relative_base, int depth)
 {
 	char *map;
Signature · 1 line
1.6.1.150.g5e733b
#2 lore raw

PATCH 2/3 Teach read_tree_recursive() how to traverse into submodules

The traversal of submodules is only triggered if the current submodule HEAD commit object is accessible. To this end, read_tree_recursive() will try to insert the submodule odb as an alternate odb but the lack of such an odb is not treated as an error since it is then assumed that the user is not interested in the submodule content. However, if the submodule odb is found it is treated as an error if the HEAD commit object is missing.

---
 cache.h       |    2 +
 environment.c |   12 ++++++++
 tree.c        |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 0 deletions(-)
cache.h +2 -0
index daa2d4e..6728467 100644
@@ -382,6 +382,8 @@ extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
 extern const char *read_gitfile_gently(const char *path);
 extern void set_git_work_tree(const char *tree);
+extern int get_traverse_gitlinks();
+extern void set_traverse_gitlinks(int traverse);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
environment.c +12 -0
index e278bce..35cc557 100644
@@ -53,6 +53,8 @@ static char *work_tree;
 static const char *git_dir;
 static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
 
+static int traverse_gitlinks = 0;
+
 static void setup_git_env(void)
 {
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
@@ -159,3 +161,13 @@ int set_git_dir(const char *path)
 	setup_git_env();
 	return 0;
 }
+
+int get_traverse_gitlinks()
+{
+	return traverse_gitlinks;
+}
+
+void set_traverse_gitlinks(int traverse)
+{
+	traverse_gitlinks = traverse;
+}
tree.c +80 -0

98 lines not on this page · raw message

Signature · 1 line
1.6.1.150.g5e733b
#3 lore raw

PATCH 3/3 git-archive: add support for --submodules

---
 Documentation/git-archive.txt |    7 +++-
 archive.c                     |    4 ++
 t/t5001-archive-submodules.sh |   78 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 2 deletions(-)
 create mode 100755 t/t5001-archive-submodules.sh
Documentation/git-archive.txt +5 -2
index 41cbf9c..84e0b43 100644
@@ -10,8 +10,8 @@ SYNOPSIS
 --------
 [verse]
 'git archive' --format=<fmt> [--list] [--prefix=<prefix>/] [<extra>]
-	      [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
-	      [path...]
+	      [--remote=<repo> [--exec=<git-upload-archive>]] [--submodules]
+	      <tree-ish> [path...]
 
 DESCRIPTION
 -----------
@@ -59,6 +59,9 @@ OPTIONS
 	Used with --remote to specify the path to the
 	'git-upload-archive' on the remote side.
 
+--submodules::
+	Include files from checked out submodules.
+
 <tree-ish>::
 	The tree or commit to produce an archive for.
 
archive.c +4 -0
index 9ac455d..0c024b8 100644
@@ -255,6 +255,7 @@ static int parse_archive_args(int argc, const char **argv,
 	const char *exec = NULL;
 	int compression_level = -1;
 	int verbose = 0;
+	int submodules = 0;
 	int i;
 	int list = 0;
 	struct option opts[] = {
@@ -262,6 +263,8 @@ static int parse_archive_args(int argc, const char **argv,
 		OPT_STRING(0, "format", &format, "fmt", "archive format"),
 		OPT_STRING(0, "prefix", &base, "prefix",
 			"prepend prefix to each pathname in the archive"),
+		OPT_BOOLEAN(0, "submodules", &submodules,
+			"recurse into submodules"),
 		OPT__VERBOSE(&verbose),
 		OPT__COMPR('0', &compression_level, "store only", 0),
 		OPT__COMPR('1', &compression_level, "compress faster", 1),
@@ -320,6 +323,7 @@ static int parse_archive_args(int argc, const char **argv,
 	args->base = base;
 	args->baselen = strlen(base);
 
+	set_traverse_gitlinks(submodules);
 	return argc;
 }
 
t/t5001-archive-submodules.sh new file +78 -0

78 lines not on this page · raw message

Signature · 1 line
1.6.1.150.g5e733b
#4 lore raw

Re: [PATCH 3/3] git-archive: add support for --submodules

Hi,

archive.c static int parse_archive_args(int argc, const char **argv, · line 326

Quoted 5 lines · Lars Hjemli
archive.c +1 -0
@@ -320,6 +323,7 @@ static int parse_archive_args(int argc, const char **argv,
 	args->base = base;
 	args->baselen = strlen(base);
 
+	set_traverse_gitlinks(submodules);

As I said, this is a per-call thing. So you need to add that option to the archiver_args struct and use it in write_archive_entries().

Ciao,
Dscho

#5 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

In reply to Lars Hjemli
Quoted 7 lines · Lars Hjemli

The traversal of submodules is only triggered if the current submodule HEAD commit object is accessible. To this end, read_tree_recursive() will try to insert the submodule odb as an alternate odb but the lack of such an odb is not treated as an error since it is then assumed that the user is not interested in the submodule content. However, if the submodule odb is found it is treated as an error if the HEAD commit object is missing.

Callers of read_tree_recursive() specify a tree to traverse. Unconditionally using the HEAD of submodules feels a bit restrictive, but I don't use submodules, so I have no idea what I'm actually talking about here. :)

Quoted 24 lines

int read_tree_recursive(struct tree *tree,

tree.c +17 -0
 			const char *base, int baselen,
 			int stage, const char **match,
@@ -132,6 +188,30 @@ int read_tree_recursive(struct tree *tree,
 				return -1;
 			continue;
 		}
+		if (S_ISGITLINK(entry.mode) && get_traverse_gitlinks()) {
+			int retval;
+			char *newbase;
+			struct tree *subtree;
+			unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);
+
+			newbase = xmalloc(baselen + 1 + pathlen);
+			memcpy(newbase, base, baselen);
+			memcpy(newbase + baselen, entry.path, pathlen);
+			newbase[baselen + pathlen] = 0;
+			if (!traverse_gitlink(newbase, entry.sha1, &subtree)) {
+				free(newbase);
+				continue;
+			}
+			newbase[baselen + pathlen] = '/';
+			retval = read_tree_recursive(subtree,
+						     newbase,

tree.c int read_tree_recursive(struct tree *tree, · line 217

Quoted 10 lines
tree.c +7 -0
+						     baselen + pathlen + 1,
+						     stage, match, fn, context);
+			free(newbase);
+			if (retval)
+				return -1;
+			continue;
+		}
 	}
 	return 0;
 }

You don't need to call get_traverse_gitlinks() in the if statement above if you make all read_tree_recursive() callback functions return 0 for gitlinks that they don't want to follow and READ_TREE_RECURSIVE for those they do. It's cleaner without the static variable and its accessors and more flexible, too: the callbacks might decide to traverse only certain submodules.

René

#6 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Quoted 9 lines · René Scharfe
Quoted 7 lines · Lars Hjemli

The traversal of submodules is only triggered if the current submodule HEAD commit object is accessible. To this end, read_tree_recursive() will try to insert the submodule odb as an alternate odb but the lack of such an odb is not treated as an error since it is then assumed that the user is not interested in the submodule content. However, if the submodule odb is found it is treated as an error if the HEAD commit object is missing.

Quoted 4 lines

Callers of read_tree_recursive() specify a tree to traverse. Unconditionally using the HEAD of submodules feels a bit restrictive, but I don't use submodules, so I have no idea what I'm actually talking about here. :)

For bare repositories (where the submodule repo is added to objects/info/alternates), following the tree of the linked commit is the only option. And for non-bare repositories with the submodule checked out, I think we should honor the users choice of checked out HEAD in the submodule (especially since we don't have any other way to specify which submodule commit to follow).

Quoted 34 lines
Quoted 34 lines

int read_tree_recursive(struct tree *tree,
const char *base, int baselen, int stage, const char **match,

tree.c +24 -0
@@ -132,6 +188,30 @@ int read_tree_recursive(struct tree *tree,
                              return -1;
                      continue;
              }
+             if (S_ISGITLINK(entry.mode) && get_traverse_gitlinks()) {
+                     int retval;
+                     char *newbase;
+                     struct tree *subtree;
+                     unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);
+
+                     newbase = xmalloc(baselen + 1 + pathlen);
+                     memcpy(newbase, base, baselen);
+                     memcpy(newbase + baselen, entry.path, pathlen);
+                     newbase[baselen + pathlen] = 0;
+                     if (!traverse_gitlink(newbase, entry.sha1, &subtree)) {
+                             free(newbase);
+                             continue;
+                     }
+                     newbase[baselen + pathlen] = '/';
+                     retval = read_tree_recursive(subtree,
+                                                  newbase,
+                                                  baselen + pathlen + 1,
+                                                  stage, match, fn, context);
+                     free(newbase);
+                     if (retval)
+                             return -1;
+                     continue;
+             }
      }
      return 0;
Quoted 8 lines

Review int read_tree_recursive(struct tree *tree, · line 217

Quoted 1 line
(quoted diff) +0 -0
 }

You don't need to call get_traverse_gitlinks() in the if statement above if you make all read_tree_recursive() callback functions return 0 for gitlinks that they don't want to follow and READ_TREE_RECURSIVE for those they do. It's cleaner without the static variable and its accessors and more flexible, too: the callbacks might decide to traverse only certain submodules.

I like the idea, but it will require thorough review of all read_tree_recursive() consumers. So now we've got three different approaches:
* me: global setting
* dscho: parameter to read_tree_recursive()
* you: accept the return value from the callback function

Junio, what would you prefer?

--
larsh

#7 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Quoted 8 lines · Lars Hjemli

I like the idea, but it will require thorough review of all read_tree_recursive() consumers. So now we've got three different approaches:
* me: global setting
* dscho: parameter to read_tree_recursive()
* you: accept the return value from the callback function

Junio, what would you prefer?

As usual René has the best taste in designing things ;-)

#8 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Lars Hjemli

I like the idea, but it will require thorough review of all read_tree_recursive() consumers. So now we've got three different approaches:
* me: global setting
* dscho: parameter to read_tree_recursive()
* you: accept the return value from the callback function

Quoted 3 lines
Quoted 1 line

Junio, what would you prefer?

As usual René has the best taste in designing things ;-)

Agreed. I've pushed an updated series using his approach to the lh/traverse-gitlinks branch in git://hjemli.net/pub/git/git
(http://hjemli.net/git/git/log/?h=lh/traverse-gitlinks), but without a fix for Dscho's concern about the behaviour in tree.c::traverse_gitlink(). Hopefully I'll get to send a reworked series later tonight.

--
larsh

#9 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

In reply to Lars Hjemli

Hi,

Quoted 17 lines · Lars Hjemli
environment.c +2 -0
index e278bce..35cc557 100644
@@ -53,6 +53,8 @@ static char *work_tree;
 static const char *git_dir;
 static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
 
+static int traverse_gitlinks = 0;
+
 static void setup_git_env(void)
 {
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
@@ -159,3 +161,13 @@ int set_git_dir(const char *path)
 	setup_git_env();
 	return 0;
 }

environment.c int set_git_dir(const char *path) · line 173

Quoted 10 lines
environment.c +10 -0
+
+int get_traverse_gitlinks()
+{
+	return traverse_gitlinks;
+}
+
+void set_traverse_gitlinks(int traverse)
+{
+	traverse_gitlinks = traverse;
+}

If you have full accessors anyway, it is much easier and cleaner to make this a global variable to begin with.

However, environment.c is reserved for things that come from the config and can be overridden by the user. That is certainly not the case for traverse_gitlinks.

But let's think about it again: should traverse_gitlinks be a global varible at all? I think not. It should be a per-call decision.

Quoted 13 lines
tree.c +1 -0
index 03e782a..87cf309 100644
@@ -5,6 +5,7 @@
 #include "commit.h"
 #include "tag.h"
 #include "tree-walk.h"
+#include "refs.h"
 
 const char *tree_type = "tree";
 

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 94

Quoted 5 lines
tree.c +2 -0
@@ -89,6 +90,61 @@ static int match_tree_entry(const char *base, int baselen, const char *path, uns
 	return 0;
 }
 
+/* Try to add the objectdb of a submodule */
+int add_gitlink_odb(char *relpath)

This wants to be static.

Quoted 6 lines
tree.c +6 -0
+{
+	const char *odbpath;
+	struct stat st;
+
+	odbpath = read_gitfile_gently(mkpath("%s/.git", relpath));
+	if (!odbpath)

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 110

Quoted 10 lines
tree.c +10 -0
+		odbpath = mkpath("%s/.git/objects", relpath);
+
+	if (stat(odbpath, &st))
+		return 1;
+
+	return add_alt_odb(odbpath);
+}
+
+/* Check if we should recurse into the specified submodule */
+int traverse_gitlink(char *path, const unsigned char *commit_sha1,

This, too.

Quoted 5 lines
tree.c +5 -0
+		     struct tree **subtree)
+{
+	unsigned char sha1[20];
+	int linked_odb = 0;
+	struct commit *commit;

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 125

Quoted 10 lines
tree.c +10 -0
+	void *buffer;
+	enum object_type type;
+	unsigned long size;
+
+	hashcpy(sha1, commit_sha1);
+	if (!add_gitlink_odb(path)) {
+		linked_odb = 1;
+		if (resolve_gitlink_ref(path, "HEAD", sha1))
+			die("Unable to lookup HEAD in %s", path);
+	}

Why would you want to continue if add_gitlink_odb() did not find a checked out submodule?

Seems you want to fall back to look in the superproject's object database. But I think that is wrong, as I have a superproject with many platform dependent submodules, only one of which is checked out, and for convenience, the submodules all live in the superproject's repository.

But I might misunderstand your code.

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 138

Quoted 3 lines
tree.c +3 -0
+	commit = lookup_commit(sha1);
+	if (!commit)
+		die("traverse_gitlink(): internal error");

s/internal error/could not access commit '%s' of submodule '%s'",

			sha1_to_hex(sha1), path);/

tree.c int read_tree_recursive(struct tree *tree, · line 191

Quoted 5 lines
tree.c +1 -0
@@ -132,6 +188,30 @@ int read_tree_recursive(struct tree *tree,
 				return -1;
 			continue;
 		}
+		if (S_ISGITLINK(entry.mode) && get_traverse_gitlinks()) {

Like I said, traverse_gitlinks should be a flag to read_tree_recursive. So preferably, you should add a parameter 'flags' and make that option an enum.

tree.c int read_tree_recursive(struct tree *tree, · line 195

Quoted 4 lines
tree.c +4 -0
+			int retval;
+			char *newbase;
+			struct tree *subtree;
+			unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);

Nit: Long line.

tree.c int read_tree_recursive(struct tree *tree, · line 200

Quoted 5 lines
tree.c +5 -0
+
+			newbase = xmalloc(baselen + 1 + pathlen);
+			memcpy(newbase, base, baselen);
+			memcpy(newbase + baselen, entry.path, pathlen);
+			newbase[baselen + pathlen] = 0;

We have strbufs for that.

tree.c int read_tree_recursive(struct tree *tree, · line 205

Quoted 5 lines
tree.c +5 -0
+			if (!traverse_gitlink(newbase, entry.sha1, &subtree)) {
+				free(newbase);
+				continue;
+			}
+			newbase[baselen + pathlen] = '/';

... to avoid this off-by-one.

tree.c int read_tree_recursive(struct tree *tree, · line 217

Quoted 12 lines
tree.c +9 -0
+			retval = read_tree_recursive(subtree,
+						     newbase,
+						     baselen + pathlen + 1,
+						     stage, match, fn, context);
+			free(newbase);
+			if (retval)
+				return -1;
+			continue;
+		}
 	}
 	return 0;
 }

Ciao,
Dscho

#10 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Quoted 23 lines · Johannes Schindelin

Hi,

Quoted 5 lines · Lars Hjemli
environment.c +0 -0
@@ -159,3 +161,13 @@ int set_git_dir(const char *path)
      setup_git_env();
      return 0;
 }

environment.c int set_git_dir(const char *path) · line 173

Quoted 10 lines
environment.c +10 -0
+
+int get_traverse_gitlinks()
+{
+     return traverse_gitlinks;
+}
+
+void set_traverse_gitlinks(int traverse)
+{
+     traverse_gitlinks = traverse;
+}

If you have full accessors anyway, it is much easier and cleaner to make this a global variable to begin with.

Quoted 6 lines

However, environment.c is reserved for things that come from the config and can be overridden by the user. That is certainly not the case for traverse_gitlinks.

But let's think about it again: should traverse_gitlinks be a global varible at all? I think not. It should be a per-call decision.

Yes, it might be a cleaner solution to add an extra parameter to read_tree_recursive(), and since it currently has only 7 callsites the patch wouldn't be too big either. Junio, what do you think?

Quoted 4 lines

environment.c

Quoted 2 lines
environment.c +2 -0
+/* Try to add the objectdb of a submodule */
+int add_gitlink_odb(char *relpath)

This wants to be static.

Thanks

Quoted 11 lines
Quoted 11 lines
environment.c +10 -0
+{
+     const char *odbpath;
+     struct stat st;
+
+     odbpath = read_gitfile_gently(mkpath("%s/.git", relpath));
+     if (!odbpath)
+             odbpath = mkpath("%s/.git/objects", relpath);
+
+     if (stat(odbpath, &st))
+             return 1;
Quoted 8 lines

environment.c

Quoted 6 lines
environment.c +6 -0
+
+     return add_alt_odb(odbpath);
+}
+
+/* Check if we should recurse into the specified submodule */
+int traverse_gitlink(char *path, const unsigned char *commit_sha1,

This, too.

Thanks again ;-)

Quoted 20 lines
Quoted 6 lines
environment.c +5 -0
+                  struct tree **subtree)
+{
+     unsigned char sha1[20];
+     int linked_odb = 0;
+     struct commit *commit;

environment.c

Quoted 10 lines
environment.c +10 -0
+     void *buffer;
+     enum object_type type;
+     unsigned long size;
+
+     hashcpy(sha1, commit_sha1);
+     if (!add_gitlink_odb(path)) {
+             linked_odb = 1;
+             if (resolve_gitlink_ref(path, "HEAD", sha1))
+                     die("Unable to lookup HEAD in %s", path);
+     }

Why would you want to continue if add_gitlink_odb() did not find a checked out submodule?

Quoted 4 lines

Seems you want to fall back to look in the superproject's object database. But I think that is wrong, as I have a superproject with many platform dependent submodules, only one of which is checked out, and for convenience, the submodules all live in the superproject's repository.

Actually, I want this to work for bare repositories by specifying the submodule odbs in the alternates file. So if the current submodule odb wasn't found my plan was to check if the commit object was accessible anyways but don't die() if it wasn't.

Quoted 6 lines

environment.c

Quoted 3 lines
environment.c +3 -0
+     commit = lookup_commit(sha1);
+     if (!commit)
+             die("traverse_gitlink(): internal error");

s/internal error/could not access commit '%s' of submodule '%s'",

                       sha1_to_hex(sha1), path);/

Ok (I belive this codepath is virtually impossible to hit, hence the "internal error", but I could of course be mistaken).

Quoted 10 lines

environment.c int read_tree_recursive(struct tree *tree, · line 191

Quoted 5 lines
environment.c +1 -0
@@ -132,6 +188,30 @@ int read_tree_recursive(struct tree *tree,
                              return -1;
                      continue;
              }
+             if (S_ISGITLINK(entry.mode) && get_traverse_gitlinks()) {

Like I said, traverse_gitlinks should be a flag to read_tree_recursive. So preferably, you should add a parameter 'flags' and make that option an enum.

Quoted 6 lines

environment.c int read_tree_recursive(struct tree *tree, · line 195

Quoted 4 lines
environment.c +4 -0
+                     int retval;
+                     char *newbase;
+                     struct tree *subtree;
+                     unsigned int pathlen = tree_entry_len(entry.path, entry.sha1);

Nit: Long line.

Will fix

Quoted 9 lines

environment.c int read_tree_recursive(struct tree *tree, · line 200

Quoted 6 lines
environment.c +5 -0
+
+                     newbase = xmalloc(baselen + 1 + pathlen);
+                     memcpy(newbase, base, baselen);
+                     memcpy(newbase + baselen, entry.path, pathlen);
+                     newbase[baselen + pathlen] = 0;

We have strbufs for that.

Quoted 7 lines

environment.c int read_tree_recursive(struct tree *tree, · line 205

Quoted 5 lines
environment.c +5 -0
+                     if (!traverse_gitlink(newbase, entry.sha1, &subtree)) {
+                             free(newbase);
+                             continue;
+                     }
+                     newbase[baselen + pathlen] = '/';

... to avoid this off-by-one.

Actually, I don't think this is off-by-one, since (baselen + pathlen +
1) is passed along to read_tree_recursive() as the new baselen. But using strbufs might be cleaner anyways.

Thanks for the review.

--
larsh

#11 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Hi,

Quoted 29 lines · Lars Hjemli
Quoted 21 lines · Johannes Schindelin
Quoted 5 lines · Lars Hjemli
tree.c +5 -0
+                  struct tree **subtree)
+{
+     unsigned char sha1[20];
+     int linked_odb = 0;
+     struct commit *commit;

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 125

Quoted 10 lines
tree.c +10 -0
+     void *buffer;
+     enum object_type type;
+     unsigned long size;
+
+     hashcpy(sha1, commit_sha1);
+     if (!add_gitlink_odb(path)) {
+             linked_odb = 1;
+             if (resolve_gitlink_ref(path, "HEAD", sha1))
+                     die("Unable to lookup HEAD in %s", path);
+     }

Why would you want to continue if add_gitlink_odb() did not find a checked out submodule?

Quoted 4 lines

Seems you want to fall back to look in the superproject's object database. But I think that is wrong, as I have a superproject with many platform dependent submodules, only one of which is checked out, and for convenience, the submodules all live in the superproject's repository.

Quoted 4 lines

Actually, I want this to work for bare repositories by specifying the submodule odbs in the alternates file. So if the current submodule odb wasn't found my plan was to check if the commit object was accessible anyways but don't die() if it wasn't.

Please make that an explicit option (cannot think of a good name, though), otherwise I will not be able to use your feature. Making it the default would be inconsistent with the rest of our submodules framework.

Quoted 9 lines
Quoted 6 lines

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 138

Quoted 3 lines
tree.c +3 -0
+     commit = lookup_commit(sha1);
+     if (!commit)
+             die("traverse_gitlink(): internal error");

s/internal error/could not access commit '%s' of submodule '%s'",

                       sha1_to_hex(sha1), path);/

Ok (I belive this codepath is virtually impossible to hit, hence the "internal error", but I could of course be mistaken).

You make it a function that is exported to other parts of Git in cache.h. So you might just as well expect it to be used by other parts at some stage.

Ciao,
Dscho

#12 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Quoted 9 lines · Johannes Schindelin
Quoted 4 lines · Lars Hjemli

Actually, I want this to work for bare repositories by specifying the submodule odbs in the alternates file. So if the current submodule odb wasn't found my plan was to check if the commit object was accessible anyways but don't die() if it wasn't.

Please make that an explicit option (cannot think of a good name, though), otherwise I will not be able to use your feature. Making it the default would be inconsistent with the rest of our submodules framework.

Would a test on is_bare_repository() suffice for your use-case? That is, something like this:

	if (!add_gitlink_odb(path->buf)) {
		linked_odb = 1;
		if (resolve_gitlink_ref(path->buf, "HEAD", sha1))
			die("Unable to lookup HEAD in %s", path->buf);
	} else if (!is_bare_repository())
		return 0;

If this isn't good enough, how do you propose it be solved?

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 138

Quoted 4 lines
tree.c +3 -0
+     commit = lookup_commit(sha1);
+     if (!commit)
+             die("traverse_gitlink(): internal error");

s/internal error/could not access commit '%s' of submodule '%s'",

                       sha1_to_hex(sha1), path);/
Quoted 6 lines
Quoted 2 lines

Ok (I belive this codepath is virtually impossible to hit, hence the "internal error", but I could of course be mistaken).

You make it a function that is exported to other parts of Git in cache.h. So you might just as well expect it to be used by other parts at some stage.

This function is local to tree.c, but your point is still valid.

--
larsh

#13 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Hi,

Quoted 8 lines · Lars Hjemli
Quoted 6 lines · Johannes Schindelin
Quoted 4 lines · Lars Hjemli

Actually, I want this to work for bare repositories by specifying the submodule odbs in the alternates file. So if the current submodule odb wasn't found my plan was to check if the commit object was accessible anyways but don't die() if it wasn't.

Quoted 5 lines
Quoted 3 lines

Please make that an explicit option (cannot think of a good name, though), otherwise I will not be able to use your feature. Making it the default would be inconsistent with the rest of our submodules framework.

Would a test on is_bare_repository() suffice for your use-case?

No. Inconsistent is inconsistent.

Quoted 1 line

If this isn't good enough, how do you propose it be solved?

As I said, with an extra option that you _have_ to pass when you want that behavior.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

tree.c static int match_tree_entry(const char *base, int baselen, const char *path, uns · line 138

Quoted 3 lines
tree.c +3 -0
+     commit = lookup_commit(sha1);
+     if (!commit)
+             die("traverse_gitlink(): internal error");

s/internal error/could not access commit '%s' of submodule '%s'",

                       sha1_to_hex(sha1), path);/
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Ok (I belive this codepath is virtually impossible to hit, hence the "internal error", but I could of course be mistaken).

You make it a function that is exported to other parts of Git in cache.h. So you might just as well expect it to be used by other parts at some stage.

This function is local to tree.c, but your point is still valid.

My point is still valid because I never talked about the static function, but the non-static one which calls the static one.

Ciao,
Dscho

#14 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Quoted 16 lines · Johannes Schindelin

Hi,

Quoted 12 lines · Lars Hjemli
Quoted 9 lines · Johannes Schindelin
Quoted 4 lines · Lars Hjemli

Actually, I want this to work for bare repositories by specifying the submodule odbs in the alternates file. So if the current submodule odb wasn't found my plan was to check if the commit object was accessible anyways but don't die() if it wasn't.

Please make that an explicit option (cannot think of a good name, though), otherwise I will not be able to use your feature. Making it the default would be inconsistent with the rest of our submodules framework.

Quoted 8 lines
Quoted 1 line

Would a test on is_bare_repository() suffice for your use-case?

No. Inconsistent is inconsistent.

Quoted 1 line

If this isn't good enough, how do you propose it be solved?

As I said, with an extra option that you _have_ to pass when you want that behavior.

My concern is how to discern between wanted and unwanted submodules in a bare repository.

With my proposed solution git archive --submodules HEAD in a bare repository would only include the content of the submodule repos listed in objects/info/alternates (since the commit referenced by the gitlink would then be reachable).

But you mentioned that you had a repository where all the objects of all the submodules where stored in the odb of the superproject. With my solution, git archive --submodules HEAD in your (bare) repo would then always include the content of all the submodules (since all the objects would always be reachable), and I believe this is the behavior you don't like.

So, would you rather have something like git archive --submodules=foo --submodules=bar HEAD to explicitly tell which submodule paths to include in the archive when executed in a bare repo?

--
larsh

#15 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Hi,

Quoted 3 lines · Lars Hjemli

So, would you rather have something like git archive --submodules=foo --submodules=bar HEAD to explicitly tell which submodule paths to include in the archive when executed in a bare repo?

That does not quite say what you tried to do, does it? You tried to traverse submodules whose commit can be found in the object database.

Setting aside the fact that we usually try to avoid accessing unreachable objects, which your handling does not do, our "git submodule" does not do that either; it only handles submodules that are checked out.

Now, this behavior might be wanted, in bare as well as in non-bare repositories, but I think it should be triggered by an option, such as "--submodules=look-in-superprojects-odb".

I know, I know, the naming is horrible, but I find it just wrong to introduce a behavior that would only confuse users because it introduces inconsistent behavior. As it is, we see too many confused users in #git already with consistent behavior [*1*].

Ciao,
Dscho

[*1*] Maybe we should allow cloning empty repositories (with no default branch at all), disable pushing into checked out branches by default, and make "git add empty-dir/" add a .gitignore and add that -- to squash at least half of the questions inside #git so that we can go back to fooling around there.

#16 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Quoted 11 lines · Johannes Schindelin

Hi,

Quoted 3 lines · Lars Hjemli

So, would you rather have something like git archive --submodules=foo --submodules=bar HEAD to explicitly tell which submodule paths to include in the archive when executed in a bare repo?

That does not quite say what you tried to do, does it? You tried to traverse submodules whose commit can be found in the object database.

Quoted 7 lines

Setting aside the fact that we usually try to avoid accessing unreachable objects, which your handling does not do, our "git submodule" does not do that either; it only handles submodules that are checked out.

Now, this behavior might be wanted, in bare as well as in non-bare repositories, but I think it should be triggered by an option, such as "--submodules=look-in-superprojects-odb".

Sorry, but if your concern is whether to traverse a submodule in a bare repo when the submodule isn't checked out (yeah, contradiction in terms), I just don't see the point.

For non-bare repositories the policy has always been to ignore submodules which isn't checked out, but for bare repositories there is no obvious way (for me, at least) to apply the same policy. Therefore I proposed to traverse all submodules where the linked commit is reachable, but as you pointed out this would be wrong for non-bare repositories.

I then modified my proposal to include a check on is_bare_repository(): If we're not in a bare repository, read_tree_recursive() is only allowed to recurse into checked out submodules. But if we're in a bare repository, read_tree_recursive() is allowed to recurse into any submodule with a reachable commit.

Now then, if --submodules=look-in-superprojects-odb should be required to trigger the latter behavior, running git archive --submodules HEAD in a bare repository would always produce identical output as git archive HEAD and this is why I don't understand the gain of 'look-in-superprojects-odb' (I thought you wanted to limit which of the reachable submodules to recurse into).

--
larsh

#17 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

Hi,

Quoted 3 lines · Lars Hjemli

Sorry, but if your concern is whether to traverse a submodule in a bare repo when the submodule isn't checked out (yeah, contradiction in terms), I just don't see the point.

Obviously.

Quoted 3 lines

For non-bare repositories the policy has always been to ignore submodules which isn't checked out, but for bare repositories there is no obvious way (for me, at least) to apply the same policy.

There is one: we never traverse them in bare repositories.

Never.

You are introducing that contradicts that on purpose. Which I do not like at all.

Sure, what you want is a nifty feature, but you'll have to do it right.

For example, your handling for bare repositories precludes everybody from specifying -- just for this particular call to git archive -- what submodules they want to include. And you preclude anybody from excluding -- just for this particular call to git archive -- certain submodules whose commits just so happen to be present in the superproject.

For me, that is a sign of a bad user interface design.

Ciao,
Dscho

P.S.: if you still don't get the point, I will just shut up, until the question crops up, and redirect every person confused by that behavior to you. Be prepared.

#18 lore raw

PATCH 1/1 bug fix, diff whitespace ignore options

Fixed bug in diff whitespace ignore options.
It is now OK to specify more than one whitespace ignore option on the command line. In unit test 4015, expect success rather than failure for 4 cases.
Note: I do not fully understand why this fix works, but it passes all 68 t4???-* diff test scripts.

The semantics of the three whitespace ignore flags { -w, -b, --ignore-space-at-eol }
obey a relation of transitive implication, i.e. the stronger options imply the weaker options:
-w implies the other two
-b implies --ignore-space-at-eol
--ignore-space-at-eol implies only itself

Therefore it is never necessary to specify more than one of these on the command line. Yet we imagine scenarios where software wrappers (e.g. GUIs, etc) generate command lines that switch on more than one of these flags simultaneously. It is unreasonable to prohibit specifying more than one, since a new user might not immediately discern the implication relation. Now we call such a command line valid and legal.

---
 t/t4015-diff-whitespace.sh |    8 ++++----
 xdiff/xutils.c             |   22 ++++++++++++----------
 2 files changed, 16 insertions(+), 14 deletions(-)
t/t4015-diff-whitespace.sh +4 -4
index dbb608c..6d13da3 100755
@@ -99,11 +99,11 @@ EOF
 git diff -w > out
 test_expect_success 'another test, with -w' 'test_cmp expect out'
 git diff -w -b > out
-test_expect_failure 'another test, with -w -b' 'test_cmp expect out'
+test_expect_success 'another test, with -w -b' 'test_cmp expect out'
 git diff -w --ignore-space-at-eol > out
-test_expect_failure 'another test, with -w --ignore-space-at-eol' 'test_cmp expect out'
+test_expect_success 'another test, with -w --ignore-space-at-eol' 'test_cmp expect out'
 git diff -w -b --ignore-space-at-eol > out
-test_expect_failure 'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out'
+test_expect_success 'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out'
 
 tr 'Q' '\015' << EOF > expect
 diff --git a/x b/x
@@ -123,7 +123,7 @@ EOF
 git diff -b > out
 test_expect_success 'another test, with -b' 'test_cmp expect out'
 git diff -b --ignore-space-at-eol > out
-test_expect_failure 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
+test_expect_success 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
 
 tr 'Q' '\015' << EOF > expect
 diff --git a/x b/x
xdiff/xutils.c +12 -10
index d7974d1..b9bda86 100644
@@ -245,17 +245,19 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
 			while (ptr + 1 < top && isspace(ptr[1])
 					&& ptr[1] != '\n')
 				ptr++;
-			if (flags & XDF_IGNORE_WHITESPACE_CHANGE
-					&& ptr[1] != '\n') {
-				ha += (ha << 5);
-				ha ^= (unsigned long) ' ';
-			}
-			if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
-					&& ptr[1] != '\n') {
-				while (ptr2 != ptr + 1) {
+			if( ! (          flags & XDF_IGNORE_WHITESPACE       )){
+				if(      flags & XDF_IGNORE_WHITESPACE_CHANGE
+						&& ptr[1] != '\n') {
 					ha += (ha << 5);
-					ha ^= (unsigned long) *ptr2;
-					ptr2++;
+					ha ^= (unsigned long) ' ';
+				}
+				else if( flags & XDF_IGNORE_WHITESPACE_AT_EOL
+						&& ptr[1] != '\n') {
+					while (ptr2 != ptr + 1) {
+						ha += (ha << 5);
+						ha ^= (unsigned long) *ptr2;
+						ptr2++;
+					}
 				}
 			}
 			continue;
Signature · 1 line
1.6.1.203.ga83c8.dirty
#19 lore raw

Re: [PATCH/RFC v1 1/1] bug fix, diff whitespace ignore options

Hi,

Quoted 23 lines · Keith Cascio

Fixed bug in diff whitespace ignore options.
It is now OK to specify more than one whitespace ignore option on the command line. In unit test 4015, expect success rather than failure for 4 cases.
Note: I do not fully understand why this fix works, but it passes all 68 t4???-* diff test scripts.

The semantics of the three whitespace ignore flags { -w, -b, --ignore-space-at-eol }
obey a relation of transitive implication, i.e. the stronger options imply the weaker options:

(quoted diff) +0 -3
-w                    implies the other two
-b                    implies --ignore-space-at-eol
--ignore-space-at-eol implies only itself

Therefore it is never necessary to specify more than one of these on the command line. Yet we imagine scenarios where software wrappers (e.g. GUIs, etc) generate command lines that switch on more than one of these flags simultaneously. It is unreasonable to prohibit specifying more than one, since a new user might not immediately discern the implication relation. Now we call such a command line valid and legal.

Quoted 2 lines

---

This does not really look all that similar to other commit messages.

For example, "Note: I do not fully understand why this fix works, but it passes all 68 t4???-* diff test scripts." is rather discouraging. If you are not convinced, how should we be?

However, I almost can excuse that, but...

Quoted 12 lines
 t/t4015-diff-whitespace.sh |    8 ++++----
 xdiff/xutils.c             |   22 ++++++++++++----------
 2 files changed, 16 insertions(+), 14 deletions(-)
xdiff/xutils.c +0 -0
index d7974d1..b9bda86 100644
@@ -245,17 +245,19 @@ static unsigned long
 xdl_hash_record_with_whitespace(char const **data,
 			while (ptr + 1 < top && isspace(ptr[1])
 					&& ptr[1] != '\n')

xdiff/xutils.c static unsigned long · line 249

Quoted 10 lines
xdiff/xutils.c +1 -8
 				ptr++;
-			if (flags & XDF_IGNORE_WHITESPACE_CHANGE
-					&& ptr[1] != '\n') {
-				ha += (ha << 5);
-				ha ^= (unsigned long) ' ';
-			}
-			if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
-					&& ptr[1] != '\n') {
-				while (ptr2 != ptr + 1) {
+			if( ! (          flags & XDF_IGNORE_WHITESPACE

... this is just plain ugly, not to mention breaking the coding style of the surrounding code in a rather blatant way.

Quoted 5 lines
xdiff/xutils.c +2 -1
 )){
+				if(      flags & XDF_IGNORE_WHITESPACE_CHANGE
+						&& ptr[1] != '\n') {
 					ha += (ha << 5);
-					ha ^= (unsigned long) *ptr2;

xdiff/xutils.c static unsigned long xdl_hash_record_with_whitespace(char const **data, · line 260

Quoted 10 lines
xdiff/xutils.c +9 -1
-					ptr2++;
+					ha ^= (unsigned long) ' ';
+				}
+				else if( flags & XDF_IGNORE_WHITESPACE_AT_EOL
+						&& ptr[1] != '\n') {
+					while (ptr2 != ptr + 1) {
+						ha += (ha << 5);
+						ha ^= (unsigned long) *ptr2;
+						ptr2++;
+					}

Besides, I think what you actually wanted is

if (flags & XDF_IGNORE_WHITESPACE)
; /* already handled */
else if (flags & XDF_IGNORE_WHITESPACE_CHANGE)
...
else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL)
...

for improved readability both of the code and the patch.

Ciao,
Dscho

#20 lore raw

PATCH v2 1/1 bug fix, diff whitespace ignore options

Fixed bug in diff whitespace ignore options. It is now OK to specify more than one whitespace ignore option on the command line.

---
Dscho,
You are right. The code and the patch are more readable this way.
-- Keith

 t/t4015-diff-whitespace.sh |    8 ++++----
 xdiff/xutils.c             |    6 ++++--
 2 files changed, 8 insertions(+), 6 deletions(-)
t/t4015-diff-whitespace.sh +4 -4
index dbb608c..6d13da3 100755
@@ -99,11 +99,11 @@ EOF
 git diff -w > out
 test_expect_success 'another test, with -w' 'test_cmp expect out'
 git diff -w -b > out
-test_expect_failure 'another test, with -w -b' 'test_cmp expect out'
+test_expect_success 'another test, with -w -b' 'test_cmp expect out'
 git diff -w --ignore-space-at-eol > out
-test_expect_failure 'another test, with -w --ignore-space-at-eol' 'test_cmp expect out'
+test_expect_success 'another test, with -w --ignore-space-at-eol' 'test_cmp expect out'
 git diff -w -b --ignore-space-at-eol > out
-test_expect_failure 'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out'
+test_expect_success 'another test, with -w -b --ignore-space-at-eol' 'test_cmp expect out'
 
 tr 'Q' '\015' << EOF > expect
 diff --git a/x b/x
@@ -123,7 +123,7 @@ EOF
 git diff -b > out
 test_expect_success 'another test, with -b' 'test_cmp expect out'
 git diff -b --ignore-space-at-eol > out
-test_expect_failure 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
+test_expect_success 'another test, with -b --ignore-space-at-eol' 'test_cmp expect out'
 
 tr 'Q' '\015' << EOF > expect
 diff --git a/x b/x
xdiff/xutils.c +4 -2
index d7974d1..04ad468 100644
@@ -245,12 +245,14 @@ static unsigned long xdl_hash_record_with_whitespace(char const **data,
 			while (ptr + 1 < top && isspace(ptr[1])
 					&& ptr[1] != '\n')
 				ptr++;
-			if (flags & XDF_IGNORE_WHITESPACE_CHANGE
+			if (flags & XDF_IGNORE_WHITESPACE)
+				; /* already handled */
+			else if (flags & XDF_IGNORE_WHITESPACE_CHANGE
 					&& ptr[1] != '\n') {
 				ha += (ha << 5);
 				ha ^= (unsigned long) ' ';
 			}
-			if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
+			else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL
 					&& ptr[1] != '\n') {
 				while (ptr2 != ptr + 1) {
 					ha += (ha << 5);
Signature · 1 line
1.6.1.213.g28da8.dirty
#21 lore raw

Re: [PATCH/RFC v2 1/1] bug fix, diff whitespace ignore options

Quoted 8 lines · Keith Cascio

Fixed bug in diff whitespace ignore options. It is now OK to specify more than one whitespace ignore option on the command line.

---
Dscho,
You are right. The code and the patch are more readable this way.

Thanks; I've fixed it up so there is no need to resend but your patch was whitespace mangled (format=flowed), by the way.

#23 lore raw

Re: [PATCH 2/3] Teach read_tree_recursive() how to traverse into submodules

In reply to Lars Hjemli
Quoted 8 lines · Lars Hjemli

On Sun, Jan 18, 2009 at 16:48, Johannes Schindelin <[email protected]> wrote:
...

Quoted 4 lines

Seems you want to fall back to look in the superproject's object database. But I think that is wrong, as I have a superproject with many platform dependent submodules, only one of which is checked out, and for convenience, the submodules all live in the superproject's repository.

Quoted 4 lines

Actually, I want this to work for bare repositories by specifying the submodule odbs in the alternates file. So if the current submodule odb wasn't found my plan was to check if the commit object was accessible anyways but don't die() if it wasn't.

The current submodule design is "do not recurse into them by default without being told" throughout the Porcelain. We can think of various ways for the users to tell which submodules are of interest and which are uninteresting.

The most general solution would be to give a list of submodules you are interested in recursing into from the command line, something like:

    $ git command --with-submodule=path1 --with-submodule=path2...

That approach would work equally "well" with a bare repository or with a non-bare repository, but if you have N submodules, you need to give up to N extra options, which may be cumbersome (meaning, "works equally well" above actually may mean "works equally awkwardly"). One way to solve awkwardness may be to support a mechanism that allows you to use configuration variables to name a group of submodules.

In addition to such configuration variables, you already have one default group of submodules, defined by the way you set up your work tree, when your superproject does have a work tree. Some submodules have repositories cloned in the work tree, while some don't, and the ones without clones can be defined as "uninteresting ones" (to the work tree owner) that are outside the default group. For many work tree oriented operations, it may even make sense to allow that group to be used with a single "git command --with-submodule" (i.e. when you do not say which submodule you mean, that can default to "cloned" group).

I do not know "has an entry in the superproject's alternate list that points to its object store" is a good basis to define another default group useful especially in a bare repository setting; you seem to be suggesting that, and you might be correct.

For "git archive", however, I suspect the "default group based on work tree checkout state" may make the least sense. "git archive HEAD" is expected to give a reproducible dump of the state recorded by the HEAD commit no matter who runs it in what repository, and I think there should be a conscious and explicit instruction from the end user that says "Here is a dump from this commit in the superproject, *BUT* it was made together with contents from this and that submodule". Command line options that list "this and that submodule" is explicit enough, and a configured nickname given to a known group of submodules from the command line may be so as well, but the group based on the checkout state feels a bit too implicit and magical to my taste. The group based on the "has an entry in superproject's alternates" criterion is not much better in this regard, methinks.

Another worrysome thing about "git archive" is that it marks the resulting archive with the commit object name the tarball was taken from. If you allow recursing into an arbitrary subset of submodule, a project with N submodules can produce 2^N different varieties of archive, all marked with the same commit object name from the superproject. That might be a bit too confusing.

#24 lore raw

Re: [PATCH 1/3] sha1_file: add function to insert alternate object db

In reply to Lars Hjemli

Hi,

Quoted 17 lines · Lars Hjemli
cache.h +1 -0
index 8e1af26..daa2d4e 100644
@@ -724,6 +724,7 @@ extern struct alternate_object_database {
 	char base[FLEX_ARRAY]; /* more */
 } *alt_odb_list;
 extern void prepare_alt_odb(void);
+extern int add_alt_odb(const char *path);
 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*);
sha1_file.c +0 -0
index f08493f..19f9725 100644

sha1_file.c static void link_alt_odb_entries(const char *alt, const char *ep, int sep, · line 361

Quoted 6 lines
sha1_file.c +3 -0
@@ -356,6 +356,11 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
 	}
 }
 
+int add_alt_odb(const char *path)
+{
+	return link_alt_odb_entry(path, strlen(path), NULL, 0);

This function can return the error message "object directory %s does not exist; check .git/objects/info/alternates." Maybe you want to change that, even if the user you are introducing might not hit that code path.

Ciao,
Dscho

#25 lore raw

PATCH 1/1 sha1_file: add function to insert alternate object db

This function will be used when implementing traversal into submodules.

---

Quoted 9 lines · Johannes Schindelin

Review

Quoted 3 lines · Lars Hjemli
(quoted diff) +3 -0
+int add_alt_odb(const char *path)
+{
+     return link_alt_odb_entry(path, strlen(path), NULL, 0);

This function can return the error message "object directory %s does not exist; check .git/objects/info/alternates." Maybe you want to change that, even if the user you are introducing might not hit that code path.

Something like this, maybe?

 cache.h     |    1 +
 sha1_file.c |   17 ++++++++++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)
cache.h +1 -0
index 8e1af26..0dbe2a6 100644
@@ -724,6 +724,7 @@ extern struct alternate_object_database {
 	char base[FLEX_ARRAY]; /* more */
 } *alt_odb_list;
 extern void prepare_alt_odb(void);
+extern int add_alt_odb(const char *path, int quiet);
 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*);
sha1_file.c +12 -5
index f08493f..4b7e691 100644
@@ -252,7 +252,8 @@ static void read_info_alternates(const char * alternates, int depth);
  * SHA1, an extra slash for the first level indirection, and the
  * terminating NUL.
  */
-static int link_alt_odb_entry(const char * entry, int len, const char * relative_base, int depth)
+static int link_alt_odb_entry(const char * entry, int len,
+			      const char * relative_base, int depth, int quiet)
 {
 	const char *objdir = get_object_directory();
 	struct alternate_object_database *ent;
@@ -285,9 +286,10 @@ static int link_alt_odb_entry(const char * entry, int len, const char * relative
 
 	/* Detect cases where alternate disappeared */
 	if (!is_directory(ent->base)) {
-		error("object directory %s does not exist; "
-		      "check .git/objects/info/alternates.",
-		      ent->base);
+		if (!quiet)
+			error("object directory %s does not exist; "
+			      "check .git/objects/info/alternates.",
+			      ent->base);
 		free(ent);
 		return -1;
 	}
@@ -347,7 +349,7 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
 						relative_base, last);
 			} else {
 				link_alt_odb_entry(last, cp - last,
-						relative_base, depth);
+						relative_base, depth, 0);
 			}
 		}
 		while (cp < ep && *cp == sep)
@@ -356,6 +358,11 @@ static void link_alt_odb_entries(const char *alt, const char *ep, int sep,
 	}
 }
 
+int add_alt_odb(const char *path, int quiet)
+{
+	return link_alt_odb_entry(path, strlen(path), NULL, 0, quiet);
+}
+
 static void read_info_alternates(const char * relative_base, int depth)
 {
 	char *map;
Signature · 1 line
1.6.1.150.g5e733b