~kornellapacz/gmnigit

ref: 279d605b0769c48612e55c5a174f8d68b628fbc7 gmnigit/commits.go -rw-r--r-- 2.9 KiB
279d605b — Omar Polo handle annotated tags too a month 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"text/template"

	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/plumbing/object"
)

var commitTemplate = template.Must(
	template.New("commit").Parse(`## {{.Hash}}
Author:  {{.Author.String}}
Date:    {{.Author.When.Format "Mon Jan 02 15:04:05 2006 -0700"}}
=> {{.Hash}}.patch Patch
Message: {{ .Message }}
`))

func iterateOverCommits(r *git.Repository, callback func(*object.Commit, *object.Patch) error) {
	ref, err := r.Head()
	check(err)

	cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
	check(err)

	err = cIter.ForEach(func(c *object.Commit) error {
		commitTree, err := c.Tree()
		check(err)

		// get direct parrent
		parent, err := c.Parent(0)

		if err == object.ErrParentNotFound {
			// probably first commit, generate patch for that
			emptyTree := r.EmptyTreeObject()

			patch, err := emptyTree.Patch(commitTree)
			check(err)

			return callback(c, patch)
		}

		check(err)

		parentTree, err := parent.Tree()
		check(err)

		patch, err := parentTree.Patch(commitTree)
		check(err)

		return callback(c, patch)
	})

	check(err)
}

func createCommitsFiles(repository *git.Repository) {
	commitsPath := filepath.Join(distPath, commitsSubPath)
	check(os.Mkdir(commitsPath, os.ModePerm))

	commitsIndex, err := os.Create(filepath.Join(commitsPath, "index.gmi"))
	check(err)
	commitsIndex.WriteString("# Commits \n\n")

	iterateOverCommits(repository, func(commit *object.Commit, patch *object.Patch) error {
		commitTemplate.Execute(commitsIndex, commit)

		patchFile, err := os.Create(filepath.Join(commitsPath, commit.Hash.String()+".patch"))
		check(err)

		patchFile.WriteString(patch.String())
		check(patchFile.Close())

		return nil
	})

	check(commitsIndex.Close())
	wg.Done()
}

func createRefsFile(r *git.Repository) {
	tags := []string{}
	iter, err := r.Tags()
	check(err)
	err = iter.ForEach(func(ref *plumbing.Reference) error {
		var c *object.Commit

		hash := ref.Hash()
		obj, err := r.TagObject(hash)
		switch err {
		case nil:
			// an annotated tag
			if c, err = r.CommitObject(obj.Target); err != nil {
				return err
			}
			hash = obj.Target
		case plumbing.ErrObjectNotFound:
			// a lightweight tag
			if c, err = r.CommitObject(hash); err != nil {
				return err
			}
		default:
			return err
		}

		tag := fmt.Sprintf(
			"=> ../%s/%s.patch %s - %s",
			commitsSubPath,
			hash,
			c.Author.When.Format("2006-01-02"),
			filepath.Base(ref.Name().String()),
		)
		tags = append(tags, tag)

		return nil
	})
	check(err)

	refsPath := filepath.Join(distPath, refsSubPath)
	check(os.Mkdir(refsPath, os.ModePerm))

	refsFile, err := os.Create(filepath.Join(refsPath, "index.gmi"))
	check(err)
	defer refsFile.Close()

	fmt.Fprintf(refsFile, "# %s tags\n\n", projectName)

	if len(tags) == 0 {
		fmt.Fprintln(refsFile, "No tags yet.")
		return
	}

	for i := len(tags) - 1; i >= 0; i-- {
		fmt.Fprintln(refsFile, tags[i])
	}
}