Skip to content

Commit

Permalink
Complete parsing and constructing array of conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed Dec 28, 2017
1 parent b895d68 commit 3a2c7f4
Showing 1 changed file with 91 additions and 24 deletions.
115 changes: 91 additions & 24 deletions conflict.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,98 @@
package main

import (
"bufio"
"bytes"
"errors"
"io"
"log"
"os"
"os/exec"
"path"
"sort"
"strconv"
"strings"
)

// Conflict represents a single conflict that may have occured
type Conflict struct {
Resolved bool
FileName string
StartLine int
Resolved bool
FileName string
AbsolutePath string
Start int
Middle int
End int

CurrentLines []string
ForeignLines []string

CurrentName string
ForeignName string
}

// Resolve resolves the conflict
func (c *Conflict) Resolve() {
c.Resolved = true
}

var FakeData = [...]Conflict{Conflict{false, "index.js", 100},
Conflict{false, "style.css", 32},
Conflict{false, "hey.js", 69},
func (c *Conflict) ExtractLines() error {
input, err := os.Open(c.AbsolutePath)
if err != nil {
log.Fatal(err)
}
defer input.Close()

line := 0
r := bufio.NewReader(input)
allLines := []string{}

for {
data, err := r.ReadBytes('\n')
line++
if line > c.End && line < c.Start {
continue
}

if err == nil || err == io.EOF {
if len(data) > 0 && data[len(data)-1] == '\n' {
data = data[:len(data)-1]
}
if len(data) > 0 && data[len(data)-1] == '\r' {
data = data[:len(data)-1]
}
allLines = append(allLines, string(data))
}

if err != nil {
if err != io.EOF {
return err
}
break
}
}

relMid, relEnd := c.Middle-c.Start, c.End-c.Start
c.CurrentLines = allLines[1:relMid]
c.ForeignLines = allLines[relMid+1 : relEnd]
c.CurrentName = strings.Split(allLines[0], " ")[1]
c.ForeignName = strings.Split(allLines[c.End-c.Start], " ")[1]
return nil
}

func parse(diff []byte) (Conflict, error) {
func parse(diff []byte, dict map[string][]int) error {
parts := bytes.Split(diff, []byte(":"))
conflict := Conflict{}

if len(parts) != 3 {
return conflict, errors.New("Could not parse line")
if len(parts) < 3 {
return errors.New("Could not parse line")
}

for i, d := range parts {
if i == 0 {
conflict.FileName = string(d)
} else if i == 1 {
if lineNum, err := strconv.Atoi(string(d)); err != nil {
conflict.StartLine = lineNum
}
}
fname, lineData := string(parts[0]), parts[1]

if lineNum, err := strconv.Atoi(string(lineData)); err == nil {
lines := append(dict[fname], lineNum)
dict[fname] = lines
}
conflict.Resolved = false
return conflict, nil
return nil
}

func Find() ([]Conflict, error) {
Expand All @@ -64,12 +113,30 @@ func Find() ([]Conflict, error) {
return nil, err
}

diffs := bytes.Split(cmdOut, []byte("\n"))
output := bytes.Split(cmdOut, []byte("\n"))
diffDict := make(map[string][]int)

for _, line := range output {
_ = parse(line, diffDict)
}

conflicts := []Conflict{}

for _, line := range diffs {
if conflict, err := parse(line); err == nil {
conflicts = append(conflicts, conflict)
for fname := range diffDict {
conf := Conflict{}
sort.Ints(diffDict[fname])
lines := diffDict[fname]
conf.Start, conf.Middle, conf.End = lines[0], lines[1], lines[2]
conf.FileName = fname
conf.AbsolutePath = path.Join(cmd.Dir, fname)
conf.Resolved = false

conflicts = append(conflicts, conf)
}

for i := range conflicts {
if err := conflicts[i].ExtractLines(); err != nil {
log.Panicln(err)
}
}

Expand Down

0 comments on commit 3a2c7f4

Please sign in to comment.