Home

git-age @main - refs - log - search -
https://git.jolheiser.com/git-age.git
git-crypt, but with age
cmd/smudge.go - raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cmd

import (
	"io"
	"os"
	"path/filepath"

	"filippo.io/age"
	"github.com/urfave/cli/v2"
	"lukechampine.com/blake3"
)

var Smudge = &cli.Command{
	Name:        "smudge",
	Aliases:     []string{"s"},
	Description: "Git smudge filter",
	Hidden:      true,
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "file",
			Usage:   "File being worked on",
			Aliases: []string{"f"},
		},
	},
	Action: actionSmudge,
}

func actionSmudge(ctx *cli.Context) error {
	identities, err := ageIdentities()
	if err != nil {
		return err
	}

	dir, err := gitConfigDir(ctx.String("file"))
	if err != nil {
		return err
	}

	hashFile, err := os.Create(filepath.Join(dir, "blake3"))
	if err != nil {
		return err
	}
	defer hashFile.Close()
	ageFile, err := os.Create(filepath.Join(dir, "age"))
	if err != nil {
		return err
	}
	defer ageFile.Close()
	hasher := blake3.New(64, nil)
	defer hashFile.Write(hasher.Sum(nil))

	ageTee := io.TeeReader(os.Stdin, ageFile)
	decrypted, err := age.Decrypt(ageTee, identities...)
	if err != nil {
		return err
	}
	hashTee := io.TeeReader(decrypted, hasher)
	if _, err := io.Copy(os.Stdout, hashTee); err != nil {
		return err
	}

	return nil
}