diffparser

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

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

Go to latest
Published: Nov 29, 2023 License: MIT Imports: 4 Imported by: 0

README

DiffParser

GoDoc

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

Install

go get github.com/Nomango/diffparser

Usage Example

package main

import (
	"fmt"
	"github.com/Nomango/diffparser"
)

// error handling left out for brevity
func main() {
	byt, _ := ioutil.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

View Source
const (
	// DELETED if the file is deleted
	// Deprecated: use FileModeDeleted instead.
	DELETED = FileModeDeleted
	// MODIFIED if the file is modified
	// Deprecated: use FileModeModified instead.
	MODIFIED = FileModeModified
	// NEW if the file is created and there is no diff
	// Deprecated: use FileModeNew instead.
	NEW = FileModeNew
)
View Source
const (
	// ADDED if the line is added (shown green in diff)
	// Deprecated: use DiffLineModeAdded instead.
	ADDED = DiffLineModeAdded
	// REMOVED if the line is deleted (shown red in diff)
	// Deprecated: use DiffLineModeRemoved instead.
	REMOVED = DiffLineModeRemoved
	// UNCHANGED if the line is unchanged (not colored in diff)
	// Deprecated: use DiffLineModeUnchanged instead.
	UNCHANGED = DiffLineModeUnchanged
)

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
	// SimilarityIndex only valid when the mode is FileModeRenamed, ranging from 0 to 100
	SimilarityIndex int
}

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 rune

DiffLineMode tells the line if added, removed or unchanged

const (
	// DiffLineModeAdded if the line is added (shown green in diff)
	DiffLineModeAdded DiffLineMode = iota
	// DiffLineModeRemoved if the line is deleted (shown red in diff)
	DiffLineModeRemoved
	// DiffLineModeUnchanged if the line is unchanged (not colored in diff)
	DiffLineModeUnchanged
)

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 (
	// FileModeDeleted if the file is deleted
	FileModeDeleted FileMode = iota
	// FileModeModified if the file is modified
	FileModeModified
	// FileModeNew if the file is created and there is no diff
	FileModeNew
	// FileModeRenamed if the file is renamed
	FileModeRenamed
)

Jump to

Keyboard shortcuts

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