From abe59356930dbe69694272ce80e0c66001ce459d Mon Sep 17 00:00:00 2001
From: Michael Eischer <michael.eischer@fau.de>
Date: Fri, 29 Apr 2022 23:12:43 +0200
Subject: [PATCH] repository: unify repository version-specific initialization

Mark the master index as compressed also when initializing a new
repository. This is only relevant for testing.
---
 internal/repository/repository.go | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/internal/repository/repository.go b/internal/repository/repository.go
index 5dcd66e71..d58406c7c 100644
--- a/internal/repository/repository.go
+++ b/internal/repository/repository.go
@@ -116,6 +116,14 @@ func (r *Repository) DisableAutoIndexUpdate() {
 	r.noAutoIndexUpdate = true
 }
 
+// setConfig assigns the given config and updates the repository parameters accordingly
+func (r *Repository) setConfig(cfg restic.Config) {
+	r.cfg = cfg
+	if r.cfg.Version >= 2 {
+		r.idx.markCompressed()
+	}
+}
+
 // Config returns the repository configuration.
 func (r *Repository) Config() restic.Config {
 	return r.cfg
@@ -772,15 +780,14 @@ func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int
 	r.dataPM.key = key.master
 	r.treePM.key = key.master
 	r.keyName = key.Name()
-	r.cfg, err = restic.LoadConfig(ctx, r)
+	cfg, err := restic.LoadConfig(ctx, r)
 	if err == crypto.ErrUnauthenticated {
 		return errors.Fatalf("config or key %v is damaged: %v", key.Name(), err)
 	} else if err != nil {
 		return errors.Fatalf("config cannot be loaded: %v", err)
 	}
-	if r.Config().Version >= 2 {
-		r.idx.markCompressed()
-	}
+
+	r.setConfig(cfg)
 	return nil
 }
 
@@ -826,7 +833,7 @@ func (r *Repository) init(ctx context.Context, password string, cfg restic.Confi
 	r.dataPM.key = key.master
 	r.treePM.key = key.master
 	r.keyName = key.Name()
-	r.cfg = cfg
+	r.setConfig(cfg)
 	_, err = r.SaveJSONUnpacked(ctx, restic.ConfigFile, cfg)
 	return err
 }