File: prompt.go

package info (click to toggle)
glab 1.53.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,936 kB
  • sloc: sh: 295; makefile: 153; perl: 99; ruby: 68; javascript: 67
file content (51 lines) | stat: -rw-r--r-- 1,390 bytes parent folder | download
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
package save

import (
	"errors"
	"strings"

	"gitlab.com/gitlab-org/cli/commands/cmdutils"
)

// cleanDescription removes comments and trims space for all non-comment lines
func cleanDescription(message string) string {
	var sb strings.Builder
	for _, line := range strings.Split(message, "\n") {
		if strings.HasPrefix(line, "#") {
			continue
		}
		sb.WriteString(line)
		sb.WriteString("\n")
	}
	return strings.TrimSpace(sb.String())
}

func promptForCommit(f *cmdutils.Factory, getText cmdutils.GetTextUsingEditor, defaultValue string) (string, error) {
	message := "\n# Please enter the commit message for this change. Lines starting\n# with '#' will be ignored. A message is required.\n#\n"
	editor, err := cmdutils.GetEditor(f.Config)
	if err != nil {
		return "", err
	}

	if defaultValue != "" {
		message = defaultValue + message
	}

	var cleanedDescription string
	if !f.IO.IsOutputTTY() {
		if defaultValue == "" {
			return "", errors.New("No commit message provided and no TTY. Please provide a commit message with the --message flag.")
		}
		return defaultValue, nil
	}
	description, err = getText(editor, "glab-stack-save-description*.gitcommit", message)
	if err != nil {
		return "", err
	}
	cleanedDescription = cleanDescription(description)

	if cleanedDescription == "" {
		return "", errors.New("Commit message cannot be empty.")
	}
	return cleanedDescription, nil
}