From 233b841ad949e969a092d496cfcb3e9d5b67cbe1 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Thu, 18 May 2023 23:17:12 +0200 Subject: [PATCH] Add cat tree snapshot:subpath command This command prints the tree for the given path. --- cmd/restic/cmd_cat.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/cmd/restic/cmd_cat.go b/cmd/restic/cmd_cat.go index ee34c813a..b150c9671 100644 --- a/cmd/restic/cmd_cat.go +++ b/cmd/restic/cmd_cat.go @@ -13,7 +13,7 @@ import ( ) var cmdCat = &cobra.Command{ - Use: "cat [flags] [pack|blob|snapshot|index|key|masterkey|config|lock] ID", + Use: "cat [flags] [masterkey|config|pack ID|blob ID|snapshot ID|index ID|key ID|lock ID|tree snapshot:path]", Short: "Print internal objects to stdout", Long: ` The "cat" command is used to print internal objects to stdout. @@ -55,7 +55,7 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string) error { tpe := args[0] var id restic.ID - if tpe != "masterkey" && tpe != "config" && tpe != "snapshot" { + if tpe != "masterkey" && tpe != "config" && tpe != "snapshot" && tpe != "tree" { id, err = restic.ParseID(args[1]) if err != nil { return errors.Fatalf("unable to parse ID: %v\n", err) @@ -165,6 +165,29 @@ func runCat(ctx context.Context, gopts GlobalOptions, args []string) error { return errors.Fatal("blob not found") + case "tree": + sn, subpath, err := restic.FindSnapshot(ctx, repo.Backend(), repo, args[1]) + if err != nil { + return errors.Fatalf("could not find snapshot: %v\n", err) + } + + err = repo.LoadIndex(ctx) + if err != nil { + return err + } + + sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subpath) + if err != nil { + return err + } + + buf, err := repo.LoadBlob(ctx, restic.TreeBlob, *sn.Tree, nil) + if err != nil { + return err + } + _, err = globalOptions.stdout.Write(buf) + return err + default: return errors.Fatal("invalid type") }