Don't try to clone projects that have clone-depth set.

Change-Id: I223d028a881a4bdf838b971634e3e6ab6487fd04
diff --git a/fs/api.go b/fs/api.go
index c95fbc4..87361ae 100644
--- a/fs/api.go
+++ b/fs/api.go
@@ -39,13 +39,19 @@
 
 // ManifestOptions holds options for a Manifest file system.
 type ManifestOptions struct {
-	Manifest        *manifest.Manifest
+	Manifest *manifest.Manifest
+
+	// RepoCloneOption matches against the Path field of the
+	// repository within a manifest.
 	RepoCloneOption []CloneOption
 	FileCloneOption []CloneOption
 }
 
 // MultiFSOptions holds options for a file system with multiple manifests.
 type MultiFSOptions struct {
+
+	// RepoCloneOption matches against the Path field of the
+	// repository within a manifest.
 	RepoCloneOption []CloneOption
 	FileCloneOption []CloneOption
 }
diff --git a/fs/gitilesfs_test.go b/fs/gitilesfs_test.go
index 4411575..b19789c 100644
--- a/fs/gitilesfs_test.go
+++ b/fs/gitilesfs_test.go
@@ -252,6 +252,53 @@
 	}
 }
 
+func TestManifestFSCloneOption(t *testing.T) {
+	fix, err := newTestFixture()
+	if err != nil {
+		t.Fatal("newTestFixture", err)
+	}
+	defer fix.cleanup()
+
+	mf, err := manifest.Parse([]byte(testManifest))
+	if err != nil {
+		t.Fatal("manifest.Parse:", err)
+	}
+
+	for i := range mf.Project {
+		mf.Project[i].CloneDepth = "1"
+	}
+
+	opts := ManifestOptions{
+		Manifest: mf,
+	}
+
+	fs, err := NewManifestFS(fix.service, fix.cache, opts)
+	if err != nil {
+		t.Fatalf("NewManifestFS: %v", err)
+	}
+	if err := fix.mount(fs); err != nil {
+		log.Fatalf("MountFileSystem: %v", err)
+	}
+
+	ch := fs.Inode()
+	for _, n := range []string{"build", "kati", "AUTHORS"} {
+		newCh := ch.GetChild(n)
+		if ch == nil {
+			t.Fatalf("node for %q not found. Have %s", n, ch.Children())
+		}
+		ch = newCh
+	}
+
+	giNode, ok := ch.Node().(*gitilesNode)
+	if !ok {
+		t.Fatalf("got node type %T, want *gitilesNode", ch.Node())
+	}
+
+	if giNode.clone {
+		t.Errorf("file had clone set.")
+	}
+}
+
 func TestManifestFS(t *testing.T) {
 	fix, err := newTestFixture()
 	if err != nil {
diff --git a/fs/manifestfs.go b/fs/manifestfs.go
index fe759c9..3d5bce5 100644
--- a/fs/manifestfs.go
+++ b/fs/manifestfs.go
@@ -33,6 +33,8 @@
 	service *gitiles.Service
 
 	cache *cache.Cache
+
+	// trees is Path => Tree map.
 	trees map[string]*gitiles.Tree
 
 	options ManifestOptions
@@ -95,9 +97,14 @@
 		byDepth[d] = append(byDepth[d], p)
 	}
 
+	clonablePaths := map[string]bool{}
 	revmap := map[string]*manifest.Project{}
 	for i, p := range fs.options.Manifest.Project {
 		revmap[p.Path] = &fs.options.Manifest.Project[i]
+
+		if p.CloneDepth == "" {
+			clonablePaths[p.Path] = true
+		}
 	}
 
 	// TODO(hanwen): use parallelism here.
@@ -111,11 +118,13 @@
 				parent = ch
 			}
 
-			clone := true
-			for _, e := range fs.options.RepoCloneOption {
-				if e.RE.FindString(p) != "" {
-					clone = e.Clone
-					break
+			clone, ok := clonablePaths[p]
+			if !ok {
+				for _, e := range fs.options.RepoCloneOption {
+					if e.RE.FindString(p) != "" {
+						clone = e.Clone
+						break
+					}
 				}
 			}
 
@@ -231,7 +240,6 @@
 		result = append(result, r)
 	}
 
-	//
 	resmap := map[string]*gitiles.Tree{}
 	for _, r := range result {
 		if r.err != nil {
diff --git a/fs/multifs.go b/fs/multifs.go
index c9887b0..57e9f98 100644
--- a/fs/multifs.go
+++ b/fs/multifs.go
@@ -112,6 +112,7 @@
 		RepoCloneOption: c.root.options.RepoCloneOption,
 		FileCloneOption: c.root.options.FileCloneOption,
 	}
+
 	fs, err := NewManifestFS(c.root.gitiles, c.root.cache, options)
 	if err != nil {
 		log.Printf("NewManifestFS(%s): %v", string(content), err)