aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/diff/unified_encoder.go
diff options
context:
space:
mode:
authorAndrew Nelson <andrew2nelson@arista.com>2021-03-10 23:13:26 -0800
committerGitHub <noreply@github.com>2021-03-11 08:13:26 +0100
commit55ba7b2e0f7a1c542c88022d65b16ca79a300e6c (patch)
treee735eae5a44cafff79b91ef1b95527c87b631824 /plumbing/format/diff/unified_encoder.go
parent4519f59b5fc258653027b89705bb2ac832d25a17 (diff)
downloadgo-git-55ba7b2e0f7a1c542c88022d65b16ca79a300e6c.tar.gz
diff: Allow srcPrefix and dstPrefix to be configured (#265)
* diff: Allow srcPrefix and dstPrefix to be configured The default behavior here remains the same, but this change does allow consumers of the UnifiedEncoder to set their own path prefixes which will override the defaults of a/ and b/. * Add unit test for src/dstPrefix in encoder
Diffstat (limited to 'plumbing/format/diff/unified_encoder.go')
-rw-r--r--plumbing/format/diff/unified_encoder.go31
1 files changed, 25 insertions, 6 deletions
diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go
index 413984a..fa605b1 100644
--- a/plumbing/format/diff/unified_encoder.go
+++ b/plumbing/format/diff/unified_encoder.go
@@ -38,6 +38,10 @@ type UnifiedEncoder struct {
// a change.
contextLines int
+ // srcPrefix and dstPrefix are prepended to file paths when encoding a diff.
+ srcPrefix string
+ dstPrefix string
+
// colorConfig is the color configuration. The default is no color.
color ColorConfig
}
@@ -46,6 +50,8 @@ type UnifiedEncoder struct {
func NewUnifiedEncoder(w io.Writer, contextLines int) *UnifiedEncoder {
return &UnifiedEncoder{
Writer: w,
+ srcPrefix: "a/",
+ dstPrefix: "b/",
contextLines: contextLines,
}
}
@@ -56,6 +62,18 @@ func (e *UnifiedEncoder) SetColor(colorConfig ColorConfig) *UnifiedEncoder {
return e
}
+// SetSrcPrefix sets e's srcPrefix and returns e.
+func (e *UnifiedEncoder) SetSrcPrefix(prefix string) *UnifiedEncoder {
+ e.srcPrefix = prefix
+ return e
+}
+
+// SetDstPrefix sets e's dstPrefix and returns e.
+func (e *UnifiedEncoder) SetDstPrefix(prefix string) *UnifiedEncoder {
+ e.dstPrefix = prefix
+ return e
+}
+
// Encode encodes patch.
func (e *UnifiedEncoder) Encode(patch Patch) error {
sb := &strings.Builder{}
@@ -91,7 +109,8 @@ func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch Fil
case from != nil && to != nil:
hashEquals := from.Hash() == to.Hash()
lines = append(lines,
- fmt.Sprintf("diff --git a/%s b/%s", from.Path(), to.Path()),
+ fmt.Sprintf("diff --git %s%s %s%s",
+ e.srcPrefix, from.Path(), e.dstPrefix, to.Path()),
)
if from.Mode() != to.Mode() {
lines = append(lines,
@@ -115,22 +134,22 @@ func (e *UnifiedEncoder) writeFilePatchHeader(sb *strings.Builder, filePatch Fil
)
}
if !hashEquals {
- lines = e.appendPathLines(lines, "a/"+from.Path(), "b/"+to.Path(), isBinary)
+ lines = e.appendPathLines(lines, e.srcPrefix+from.Path(), e.dstPrefix+to.Path(), isBinary)
}
case from == nil:
lines = append(lines,
- fmt.Sprintf("diff --git a/%s b/%s", to.Path(), to.Path()),
+ fmt.Sprintf("diff --git %s %s", e.srcPrefix+to.Path(), e.dstPrefix+to.Path()),
fmt.Sprintf("new file mode %o", to.Mode()),
fmt.Sprintf("index %s..%s", plumbing.ZeroHash, to.Hash()),
)
- lines = e.appendPathLines(lines, "/dev/null", "b/"+to.Path(), isBinary)
+ lines = e.appendPathLines(lines, "/dev/null", e.dstPrefix+to.Path(), isBinary)
case to == nil:
lines = append(lines,
- fmt.Sprintf("diff --git a/%s b/%s", from.Path(), from.Path()),
+ fmt.Sprintf("diff --git %s %s", e.srcPrefix+from.Path(), e.dstPrefix+from.Path()),
fmt.Sprintf("deleted file mode %o", from.Mode()),
fmt.Sprintf("index %s..%s", from.Hash(), plumbing.ZeroHash),
)
- lines = e.appendPathLines(lines, "a/"+from.Path(), "/dev/null", isBinary)
+ lines = e.appendPathLines(lines, e.srcPrefix+from.Path(), "/dev/null", isBinary)
}
sb.WriteString(e.color[Meta])