Debian Codegraph @

cmd/diff.go

raw
1 package cmd
2
3 import (
4 "fmt"
5 "path/filepath"
6 "strconv"
7 "strings"
8
9 "github.com/Masterminds/semver/v3"
10 "github.com/spf13/cobra"
11
12 "github.com/miniscruff/changie/core"
13 )
14
15 type Diff struct {
16 *cobra.Command
17
18 // CLI args
19 SkipPrereleases bool
20 Project string
21 }
22
23 func NewDiff() *Diff {
24 diff := &Diff{}
25
26 cmd := &cobra.Command{
27 Use: "diff N|>,>=,<,<=version|versiona - versionb|start...end",
28 Short: "diff outputs the release notes between versions.",
29 Long: `diff outputs to stdout the release notes between two versions.
30
31 If the argument is a number, we simply output that many previous versions.
32 If the argument is two versions split between triple dots similar to how GitHub
33 supports as a comparison then we output the release notes between those two.
34
35 Finally one last option is any of the constraints options from the semver package:
36 https://github.com/Masterminds/semver#checking-version-constraints.
37
38 Between versions we also add an amount of newlines specified by the AfterChangelogVersion value.`,
39 Example: `v1.20.0...v1.21.1`,
40 Args: cobra.MatchAll(cobra.ExactArgs(1)),
41 RunE: diff.Run,
42 }
43 cmd.Flags().BoolVarP(
44 &diff.SkipPrereleases,
45 "skip-prereleases", "",
46 false,
47 "Excludes prereleases to determine the diff",
48 )
49 cmd.Flags().StringVarP(
50 &diff.Project,
51 "project", "j",
52 "",
53 "Specify which project we are interested in",
54 )
55
56 diff.Command = cmd
57
58 return diff
59 }
60
61 func (d *Diff) Run(cmd *cobra.Command, args []string) error {
62 writer := cmd.OutOrStdout()
63 versionRange := args[0]
64
65 config, err := core.LoadConfig()
66 if err != nil {
67 return err
68 }
69
70 if len(config.Projects) > 0 {
71 var pc *core.ProjectConfig
72
73 pc, err = config.Project(d.Project)
74 if err != nil {
75 return err
76 }
77
78 d.Project = pc.Key
79 }
80
81 vers, err := core.GetAllVersions(config, d.SkipPrereleases, d.Project)
82 if err != nil {
83 return err
84 }
85
86 firstOutput := true
87
88 versionCount, err := strconv.ParseInt(versionRange, 10, 64)
89 if err == nil {
90 for i := range min(int(versionCount), len(vers)) {
91 if !firstOutput {
92 err := core.WriteNewlines(writer, config.Newlines.AfterChangelogVersion)
93 if err != nil {
94 return err
95 }
96 }
97
98 firstOutput = false
99
100 versionPath := filepath.Join(
101 config.ChangesDir,
102 d.Project,
103 vers[i].Original()+"."+config.VersionExt,
104 )
105
106 err = core.AppendFile(writer, versionPath)
107 if err != nil {
108 return err
109 }
110 }
111
112 return nil
113 }
114
115 before, after, found := strings.Cut(versionRange, "...")
116 if found {
117 versionRange = before + " - " + after
118 }
119
120 con, err := semver.NewConstraint(versionRange)
121 if err != nil {
122 return fmt.Errorf("version range: %w", err)
123 }
124
125 for _, ver := range vers {
126 if !con.Check(ver) {
127 continue
128 }
129
130 if !firstOutput {
131 err := core.WriteNewlines(writer, config.Newlines.AfterChangelogVersion)
132 if err != nil {
133 return err
134 }
135 }
136
137 firstOutput = false
138
139 versionPath := filepath.Join(
140 config.ChangesDir,
141 d.Project,
142 ver.Original()+"."+config.VersionExt,
143 )
144
145 err = core.AppendFile(writer, versionPath)
146 if err != nil {
147 return err
148 }
149 }
150
151 return err
152 }