[cache] Remove unnecessary remote operations for clone from cache.

Because we update the cache independently from the local checkout that
will be used, we know that it is currently up-to-date and contains all
refs & objects the clone operation will need. We can in this case, use
the cache directly as the remote and avoid unnecessary roundtrips
against the server.

Change-Id: Ie42f70d3ab06c66e588f90ea61cab7139a49f1d8
diff --git a/project/operations.go b/project/operations.go
index c97712b..7bbb7ab 100644
--- a/project/operations.go
+++ b/project/operations.go
@@ -113,16 +113,26 @@
 			}
 		}
 	} else {
-		// Shallow clones can not be used as as local git reference
-		if op.project.HistoryDepth > 0 && cache != "" {
-			err = clone(jirix, cache, op.destination, gitutil.NoCheckoutOpt(true), gitutil.DepthOpt(op.project.HistoryDepth))
+		opts := []gitutil.CloneOpt{gitutil.NoCheckoutOpt(true)}
+		if op.project.HistoryDepth > 0 {
+			opts = append(opts, gitutil.DepthOpt(op.project.HistoryDepth))
 		} else {
-			err = clone(jirix, remote, op.destination, gitutil.ReferenceOpt(cache),
-				gitutil.NoCheckoutOpt(true), gitutil.DepthOpt(op.project.HistoryDepth))
+			// Shallow clones can not be used as as local git reference
+			opts = append(opts, gitutil.ReferenceOpt(cache))
 		}
-	}
-	if err != nil {
-		return err
+		if cache != "" {
+			if err = clone(jirix, cache, op.destination, opts...); err != nil {
+				return err
+			}
+			scm := gitutil.New(jirix, gitutil.RootDirOpt(op.project.Path))
+			if err = scm.AddOrReplaceRemote("origin", remote); err != nil {
+				return err
+			}
+		} else {
+			if err = clone(jirix, remote, op.destination, opts...); err != nil {
+				return err
+			}
+		}
 	}
 
 	if err := os.Chmod(op.destination, os.FileMode(0755)); err != nil {