~welt/murse

ref: signing murse/rewrite.go -rw-r--r-- 851 bytes
408fd8d8welt Fix garbage JSON when wrapping revisions. 2 months ago
                                                                                
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
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
)

func rewriteWrapMain(dir string) int {
	for i := 0; ; i++ {
		file, err := os.OpenFile(filepath.Join(dir, "revisions", strconv.Itoa(i)), os.O_RDWR, 0777)
		if err != nil {
			if os.IsNotExist(err) {
				return 0
			}

			errPrintln(err)
		}

		defer file.Close()

		var unwrapped []Change
		err = json.NewDecoder(file).Decode(&unwrapped)
		if err != nil {
			fmt.Println("SKIP", i, "UNAPPLICABLE")
			continue
		}

		new := WrappedRevision{
			No:      i,
			Changes: unwrapped,
		}

		marshalled, err := json.Marshal(new)
		if err != nil {
			panic(err)
		}

		err = file.Truncate(0)
		if err != nil {
			errPrintln(err)
			return 1
		}

		_, err = file.Seek(0, 0)
		if err != nil {
			errPrintln(err)
			return 1
		}

		file.Write(marshalled)

		file.Close()

	}
}