diffparser

package module
v0.0.0-...-cebbf07 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 4 Imported by: 2

README

DiffParser

DiffParser is a Golang package which parse's a git diff.

[!NOTE]

This package is a hard-fork of https://github.com/waigani/diffparser which hasn't been updated in a while.

Expect API changes, bug fixes, etc.

Install

go get github.com/jedevc/diffparser

Usage Example

package main

import (
    "os"

    "github.com/jedevc/diffparser"
)

// error handling left out for brevity
func main() {
    byt, _ := os.ReadFile("example.diff")
    diff, _ := diffparser.Parse(string(byt))

    // You now have a slice of files from the diff,
    file := diff.Files[0]

    // diff hunks in the file,
    hunk := file.Hunks[0]

    // new and old ranges in the hunk
    newRange := hunk.NewRange

    // and lines in the ranges.
    line := newRange.Lines[0]
}

More Examples

See diffparser_test.go for further examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff struct {
	Files []*DiffFile
	Raw   string `sql:"type:text"`

	PullID uint `sql:"index"`
}

Diff is the collection of DiffFiles

func Parse

func Parse(diffString string) (*Diff, error)

Parse takes a diff, such as produced by "git diff", and parses it into a Diff struct.

func (*Diff) Changed

func (d *Diff) Changed() map[string][]int

Changed returns a map of filename to lines changed in that file. Deleted files are ignored.

type DiffFile

type DiffFile struct {
	DiffHeader string
	Mode       FileMode
	OrigName   string
	NewName    string
	Hunks      []*DiffHunk
}

DiffFile is the sum of diffhunks and holds the changes of the file features

type DiffHunk

type DiffHunk struct {
	HunkHeader string
	OrigRange  DiffRange
	NewRange   DiffRange
	WholeRange DiffRange
}

DiffHunk is a group of difflines

func (*DiffHunk) Length

func (hunk *DiffHunk) Length() int

Length returns the hunks line length

type DiffLine

type DiffLine struct {
	Mode     DiffLineMode
	Number   int
	Content  string
	Position int // the line in the diff
}

DiffLine is the least part of an actual diff

type DiffLineMode

type DiffLineMode int

DiffLineMode tells the line if added, removed or unchanged

const (
	// ADDED if the line is added (shown green in diff)
	ADDED DiffLineMode = iota
	// REMOVED if the line is deleted (shown red in diff)
	REMOVED
	// UNCHANGED if the line is unchanged (not colored in diff)
	UNCHANGED
)

func (DiffLineMode) String

func (dlm DiffLineMode) String() string

type DiffRange

type DiffRange struct {
	// starting line number
	Start int

	// the number of lines the change diffHunk applies to
	Length int

	// Each line of the hunk range.
	Lines []*DiffLine
}

DiffRange contains the DiffLine's

type FileMode

type FileMode int

FileMode represents the file status in a diff

const (
	// DELETED if the file is deleted
	DELETED FileMode = iota
	// MODIFIED if the file is modified
	MODIFIED
	// NEW if the file is created and there is no diff
	NEW
	// RENAMED if the file is renamed
	RENAMED
)

func (FileMode) String

func (fm FileMode) String() string

Jump to

Keyboard shortcuts

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