~welt/murse

ref: v0.1.0 murse/versioning.go -rw-r--r-- 1.5 KiB
e0a63434welt Change update to minor version instead of patch. 5 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main

import (
	"encoding/json"
)

const (
	TYPE_WRITE = iota
	TYPE_MKDIR
	TYPE_DEL
)

type Change struct {
	Type   uint   `json:"type"`
	Path   string `json:"path"`
	MD5    string `json:"hash"`
	Object string `json:"object"`
}

func (c *Change) MarshalJSON() ([]byte, error) {
	if c.Type == TYPE_WRITE {
		return json.Marshal(map[string]interface{}{
			"type":   c.Type,
			"path":   c.Path,
			"hash":   c.MD5,
			"object": c.Object,
		})
	}

	return json.Marshal(map[string]interface{}{
		"type": c.Type,
		"path": c.Path,
	})
}

func splitChanges(changes []Change) (writes []Change, mkdirs []Change, dels []Change) {
	for _, v := range changes {
		switch v.Type {
		case TYPE_WRITE:
			writes = append(writes, v)
		case TYPE_MKDIR:
			mkdirs = append(mkdirs, v)
		case TYPE_DEL:
			dels = append(dels, v)
		}
	}

	return
}

func mapToChanges(changeMap map[string]Change) []Change {
	var changes []Change
	for _, v := range changeMap {
		changes = append(changes, v)
	}
	return changes
}

func replayChanges(revisions [][]Change) []Change {
	chgMap := make(map[string]Change)

	for _, revision := range revisions {
		for _, change := range revision {
			chgMap[change.Path] = change
		}
	}

	return mapToChanges(chgMap)
}

func replayChangesNoDel(revisions [][]Change) []Change {
	chgMap := make(map[string]Change)

	for _, revision := range revisions {
		for _, change := range revision {
			if change.Type != TYPE_DEL {
				chgMap[change.Path] = change
			} else {
				delete(chgMap, change.Path)
			}
		}
	}

	return mapToChanges(chgMap)
}