Skip to content

Commit

Permalink
merge-file: fix label name
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Dec 18, 2024
1 parent c77bf09 commit 4ce0db7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
29 changes: 23 additions & 6 deletions pkg/command/command_merge_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func (c *MergeFile) Summary() string {
return fmt.Sprintf(mergeFileSummaryFormat, W("Usage: "))
}

func (c *MergeFile) labelName(i int, n string) string {
if i < len(c.L) {
return c.L[i]
}
return n
}

func readText(p string, textConv bool) (string, error) {
fd, err := os.Open(p)
if err != nil {
Expand Down Expand Up @@ -76,16 +83,17 @@ func (c *MergeFile) mergeExtra(g *Globals) error {
fmt.Fprintf(os.Stderr, "merge-file: open <file2> error: %v\n", err)
return err
}
mergedText, conflict, err := diferenco.Merge(context.Background(), &diferenco.MergeOptions{
opts := &diferenco.MergeOptions{
TextO: textO,
TextA: textA,
TextB: textB,
LabelO: c.O,
LabelA: c.F1,
LabelB: c.F1,
A: a,
Style: style,
})
LabelA: c.labelName(0, c.F1),
LabelO: c.labelName(1, c.O),
LabelB: c.labelName(2, c.F2),
}
mergedText, conflict, err := diferenco.Merge(context.Background(), opts)
if err != nil {
fmt.Fprintf(os.Stderr, "merge-file: merge error: %v\n", err)
return err
Expand Down Expand Up @@ -117,7 +125,16 @@ func (c *MergeFile) Run(g *Globals) error {
case c.ZDiff3:
style = diferenco.STYLE_ZEALOUS_DIFF3
}
if err := r.MergeFile(context.Background(), &zeta.MergeFileOptions{O: c.O, A: c.F1, B: c.F2, Style: style, DiffAlgorithm: c.DiffAlgorithm, Stdout: c.Stdout}); err != nil {
opts := &zeta.MergeFileOptions{
O: c.O, A: c.F1, B: c.F2,
Style: style,
DiffAlgorithm: c.DiffAlgorithm,
Stdout: c.Stdout,
LabelA: c.labelName(0, c.F1),
LabelO: c.labelName(1, c.O),
LabelB: c.labelName(2, c.F2),
}
if err := r.MergeFile(context.Background(), opts); err != nil {
if !zeta.IsExitCode(err, 1) {
diev("merge-file: error: %v", err)
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/zeta/merge_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func (r *Repository) resolveMergeDriver() odb.MergeDriver {
}

type MergeFileOptions struct {
O, A, B string
Style int
DiffAlgorithm string
Stdout bool
TextConv bool
O, A, B string
LabelO, LabelA, LabelB string
Style int
DiffAlgorithm string
Stdout bool
TextConv bool
}

func (opts *MergeFileOptions) diffAlgorithmFromName(defaultDiffAlgorithm string) diferenco.Algorithm {
Expand Down Expand Up @@ -102,13 +103,14 @@ func (r *Repository) MergeFile(ctx context.Context, opts *MergeFileOptions) erro
if err != nil {
return err
}

merged, conflict, err := diferenco.Merge(ctx, &diferenco.MergeOptions{
TextO: textO,
TextA: textA,
TextB: textB,
LabelO: o.String()[0:8],
LabelA: a.String()[0:8],
LabelB: b.String()[0:8],
LabelO: opts.LabelO,
LabelA: opts.LabelA,
LabelB: opts.LabelB,
A: diffAlgorithm,
Style: opts.Style,
})
Expand Down

0 comments on commit 4ce0db7

Please sign in to comment.