-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommit.go
More file actions
77 lines (64 loc) · 1.59 KB
/
commit.go
File metadata and controls
77 lines (64 loc) · 1.59 KB
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"time"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/utils"
)
//krgo commit -r rootfs
//commit current changes in a new properly formated branch ready for pushing
func commitChanges(rootfs, message string) error {
if !isGitRepo(rootfs) {
return fmt.Errorf("%v not a git repository", rootfs)
}
gitRepo, _ := newGitRepo(rootfs)
layerData, err := gitRepo.exportUncommitedChangeSet()
if err != nil {
return err
}
defer layerData.Close()
//Load image data
image, err := image.LoadImage(gitRepo.Path) //reading json file in rootfs
if err != nil {
return err
}
//fill new infos
image.Parent = image.ID
image.ID = utils.GenerateRandomID()
image.Created = time.Now()
image.Comment = message
layer, err := archive.NewTempArchive(layerData, "")
if err != nil {
return err
}
image.Size = layer.Size
os.RemoveAll(layer.Name())
if err := image.SaveSize(rootfs); err != nil {
return err
}
jsonRaw, err := json.Marshal(image)
if err != nil {
return err
}
err = ioutil.WriteFile(path.Join(rootfs, "json"), jsonRaw, 0600)
if err != nil {
return err
}
//commit the changes in a new branch
n, _ := gitRepo.countBranch()
br := newBranch(n, image.ID)
if _, err = gitRepo.checkoutB(br); err != nil {
return err
}
if _, err := gitRepo.addAllAndCommit(message); err != nil {
return err
}
fmt.Printf("Changes commited in %v\n", br)
fmt.Printf("Image ID: %v\nParent: %v\nChecksum: %v\nLayer size: %v\n", image.ID, image.Parent, image.Size)
return nil
}