aboutsummaryrefslogtreecommitdiff
path: root/git_format_patch.go
blob: c899ed591d0b2ab73ab76024f0c176fa72beebcb (plain) (blame)
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
package main

import (
	"bytes"
	"errors"
	"fmt"
	"strings"
	"time"

	"github.com/go-git/go-git/v5/plumbing/object"
	"go.lindenii.runxiyu.org/lindenii-common/misc"
)

var err_get_patch = errors.New("Failed to get patch from commit")

func format_patch_from_commit(commit *object.Commit) (string, error) {
	parent, err := commit.Parent(0)
	if err != nil {
		return "", err
	}

	var patch *object.Patch
	patch, err = parent.Patch(commit)
	if err != nil {
		return "", misc.Wrap_one_error(err_get_patch, err)
	}

	var buf bytes.Buffer

	author := commit.Author
	date := author.When.Format(time.RFC1123Z)

	commit_msg_title, commit_msg_details, _ := strings.Cut(commit.Message, "\n")

	fmt.Fprintf(&buf, "From %s Mon Sep 17 00:00:00 2001\n", commit.Hash)
	fmt.Fprintf(&buf, "From: %s <%s>\n", author.Name, author.Email)
	fmt.Fprintf(&buf, "Date: %s\n", date)
	fmt.Fprintf(&buf, "Subject: [PATCH] %s\n\n", commit_msg_title)

	if commit_msg_details != "" {
		fmt.Println("fdsafsad")
		commit_msg_details_first_line, commit_msg_details_rest, _ := strings.Cut(commit_msg_details, "\n")
		if strings.TrimSpace(commit_msg_details_first_line) == "" {
			commit_msg_details = commit_msg_details_rest
		}
		buf.WriteString(commit_msg_details)
		buf.WriteString("\n")
	}
	buf.WriteString("---\n")
	fmt.Fprint(&buf, patch.Stats().String())
	fmt.Fprintln(&buf)

	buf.WriteString(patch.String())

	fmt.Fprintf(&buf, "\n-- \n2.48.1\n")

	return buf.String(), nil
}