logging

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureLogDir

func EnsureLogDir(logDir string) error

EnsureLogDir ensures the git-undo log directory exists.

func ShouldBeLogged added in v0.2.0

func ShouldBeLogged(gitCmd *githelpers.GitCommand) bool

ShouldBeLogged returns true if the command should be logged.

func ToggleLine added in v0.2.0

func ToggleLine(file *os.File, lineNumber int) error

ToggleLine toggles commenting/uncommenting of a line in the file using # prefix.

Types

type Entry

type Entry struct {
	// Timestamp is parsed timestamp of the entry.
	Timestamp time.Time
	// Ref is reference (branch/tag/commit) where the command was executed.
	Ref Ref
	// Command is just the command part.
	Command string

	// Undoed is true if the entry is undoed.
	Undoed bool

	// IsNavigation is true if this is a navigation command (checkout/switch).
	IsNavigation bool
}

Entry represents a logged git command with its full identifier.

func ParseLogLine added in v0.2.0

func ParseLogLine(line string) (*Entry, error)

ParseLogLine parses a log line into an Entry.

func (*Entry) GetIdentifier

func (e *Entry) GetIdentifier() string

GetIdentifier uses String() representation as the identifier itself But without prefix sign (so undoed command are still found).

func (*Entry) MarshalText

func (e *Entry) MarshalText() ([]byte, error)

func (*Entry) String

func (e *Entry) String() string

String returns a human-readable representation of the entry. This representation goes into the log file as well.

func (*Entry) UnmarshalText

func (e *Entry) UnmarshalText(data []byte) error

type EntryType

type EntryType int

EntryType specifies whether to look for regular or undoed entries.

const (
	NotSpecifiedEntryType EntryType = iota

	// RegularEntry represents a normal, non-undoed entry.
	RegularEntry
	// UndoedEntry represents an entry that has been marked as undoed.
	UndoedEntry
	// NavigationEntry represents a navigation command entry.
	NavigationEntry
)

func (EntryType) String

func (et EntryType) String() string

String returns the string representation of the EntryType.

type GitHelper

type GitHelper interface {
	GetCurrentGitRef() (string, error)
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger manages git command logging operations.

func NewLogger

func NewLogger(repoGitDir string, git GitHelper) *Logger

NewLogger creates a new Logger instance.

func (*Logger) CountConsecutiveUndoneCommands added in v0.2.0

func (l *Logger) CountConsecutiveUndoneCommands(refArg ...Ref) (int, error)

CountConsecutiveUndoneCommands counts consecutive undone mutation commands from the top of the log. It ignores navigation commands (N prefixed) and only counts mutation commands.

func (*Logger) Dump

func (l *Logger) Dump(w io.Writer) error

Dump reads the log file content and writes it directly to the provided writer.

func (*Logger) GetLastCheckoutSwitchEntry added in v0.2.0

func (l *Logger) GetLastCheckoutSwitchEntry(refArg ...Ref) (*Entry, error)

GetLastCheckoutSwitchEntry returns the last checkout or switch command entry for the given ref (or current ref if not specified). This method finds NON-UNDOED navigation commands for git-back.

func (*Logger) GetLastCheckoutSwitchEntryForToggle added in v0.2.0

func (l *Logger) GetLastCheckoutSwitchEntryForToggle(refArg ...Ref) (*Entry, error)

GetLastCheckoutSwitchEntryForToggle returns the last checkout or switch command entry for git-back, including undoed entries. This allows git-back to toggle back and forth. This method finds ANY navigation command (including undoed ones) for toggle behavior.

func (*Logger) GetLastEntry

func (l *Logger) GetLastEntry(refArg ...Ref) (*Entry, error)

GetLastEntry returns last entry for the given ref (or current ref if not specified) regarding of the entry type (undoed or regular). This handles both navigation commands (N prefixed) and mutation commands.

func (*Logger) GetLastRegularEntry added in v0.0.2

func (l *Logger) GetLastRegularEntry(refArg ...Ref) (*Entry, error)

GetLastRegularEntry returns last regular entry (ignoring undoed ones) for the given ref (or current ref if not specified). For git-undo, this skips navigation commands (N prefixed).

func (*Logger) GetLastUndoedEntry added in v0.2.0

func (l *Logger) GetLastUndoedEntry(refArg ...Ref) (*Entry, error)

GetLastUndoedEntry returns the last undoed entry for the given ref (or current ref if not specified). This is used for redo functionality to find the most recent undoed command to re-execute. For git-undo, this skips navigation commands (N prefixed).

func (*Logger) GetLogPath

func (l *Logger) GetLogPath() string

GetLogPath returns the path to the log file.

func (*Logger) IsNavigationCommand added in v0.2.0

func (l *Logger) IsNavigationCommand(command string) bool

IsNavigationCommand checks if a command is a navigation command (checkout, switch, etc.).

func (*Logger) LogCommand

func (l *Logger) LogCommand(strGitCommand string) error

LogCommand logs a git command with timestamp and handles branch-aware logging.

func (*Logger) ProcessLogFile added in v0.2.0

func (l *Logger) ProcessLogFile(processor func(line string) bool) error

ProcessLogFile reads the log file line by line and calls the processor function for each line. This is more efficient than reading the entire file at once, especially when only the first few lines are needed.

func (*Logger) ToggleEntry

func (l *Logger) ToggleEntry(entryIdentifier string) error

ToggleEntry toggles the undo state of an entry by adding or removing the "#" prefix. The entryIdentifier should be in the format "TIMESTAMP|REF|COMMAND" (without the # prefix). Returns true if the entry was marked as undoed, false if it was unmarked.

func (*Logger) TruncateToCurrentBranch added in v0.2.0

func (l *Logger) TruncateToCurrentBranch(refArg ...Ref) error

TruncateToCurrentBranch removes undone mutation commands from the log while preserving all navigation commands. This implements the branch-aware behavior.

type Ref added in v0.2.0

type Ref string
const (
	// RefAny means when the ref (branch/tag/commit) of the line is not respected (any).
	RefAny Ref = "__ANY__"

	// RefCurrent means when the ref (branch/tag/commit) is the current one.
	RefCurrent Ref = "__CURRENT__"

	// RefUnknown means when the ref couldn't be determined. (Non-happy path).
	RefUnknown Ref = "__UNKNOWN__"

	// RefMain represents the main branch (used for convenience).
	RefMain Ref = "main"
)

func (Ref) String added in v0.2.0

func (r Ref) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL