Add String() methods to parsed types (#48)

This enables clients to move back and forth between parsed objects and
text patches. The generated patches are semantically equal to the parsed
object and should re-parse to the same object, but may not be
byte-for-byte identical to the original input.

In my testing, formatted text patches are usually identical to the
input, but there may be cases where this is not true. Binary patches
always differ. This is because Go's 'compress/flate' package ends
streams with an empty block instead of adding the end-of-stream flag to
the last non-empty block, like Git's C implementation. Since the streams
will always be different for this reason, I chose to also enable default
compression (the test patches I generated with Git used no compression.)

The main tests for this feature involve parsing, formatting, and then
re-parsing a patch to make sure we get equal objects.

Formatting is handled by a new internal formatter type, which allows
writing all data to the same stream. This isn't exposed publicly right
now, but will be useful if there's a need for more flexible formatting
functions in the future, like formatting to a user-provided io.Writer.
This commit is contained in:
Billy Keyes
2024-08-11 14:57:07 -04:00
committed by GitHub
parent 9e0997ef65
commit 8584cd59af
20 changed files with 746 additions and 4 deletions
+9
View File
@@ -0,0 +1,9 @@
diff --git a/file.bin b/file.bin
index a7f4d5d6975ec021016c02b6d58345ebf434f38c..bdc9a70f055892146612dcdb413f0e339faaa0df 100644
GIT binary patch
delta 66
QcmeZhVVvM$!$1K50C&Ox;s5{u
delta 5
McmZo+^qAlQ00i9urT_o{
+11
View File
@@ -0,0 +1,11 @@
diff --git a/file.bin b/file.bin
new file mode 100644
index 0000000000000000000000000000000000000000..a7f4d5d6975ec021016c02b6d58345ebf434f38c
GIT binary patch
literal 72
zcmV-O0Jr~td-`u6JcK&{KDK=<a#;v1^LR5&K)zQ0=Goz82(?nJ6_nD`f#8O9p}}{P
eiXim+rDI+BDadMQmMsO5Sw@;DbrCA+PamP;Ng_@F
literal 0
HcmV?d00001
+4
View File
@@ -0,0 +1,4 @@
diff --git a/file.txt b/numbers.txt
similarity index 100%
copy from file.txt
copy to numbers.txt
+21
View File
@@ -0,0 +1,21 @@
diff --git a/file.txt b/numbers.txt
similarity index 57%
copy from file.txt
copy to numbers.txt
index c9e9e05..6c4a3e0 100644
--- a/file.txt
+++ b/numbers.txt
@@ -1,6 +1,6 @@
one
two
-three
+three three three
four
five
six
@@ -8,3 +8,5 @@ seven
eight
nine
ten
+eleven
+twelve
+16
View File
@@ -0,0 +1,16 @@
diff --git a/file.txt b/file.txt
deleted file mode 100644
index c9e9e05..0000000
--- a/file.txt
+++ /dev/null
@@ -1,10 +0,0 @@
-one
-two
-three
-four
-five
-six
-seven
-eight
-nine
-ten
+3
View File
@@ -0,0 +1,3 @@
diff --git a/file.txt b/file.txt
old mode 100644
new mode 100755
+10
View File
@@ -0,0 +1,10 @@
diff --git a/script.sh b/script.sh
old mode 100644
new mode 100755
index 7a870bd..68d501e
--- a/script.sh
+++ b/script.sh
@@ -1,2 +1,2 @@
#!/bin/bash
-echo "Hello World"
+echo "Hello, World!"
+16
View File
@@ -0,0 +1,16 @@
diff --git a/file.txt b/file.txt
index c9e9e05..7d5fdc6 100644
--- a/file.txt
+++ b/file.txt
@@ -3,8 +3,10 @@ two
three
four
five
-six
+six six six six six six
seven
eight
nine
ten
+eleven
+twelve
+16
View File
@@ -0,0 +1,16 @@
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..c9e9e05
--- /dev/null
+++ b/file.txt
@@ -0,0 +1,10 @@
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+3
View File
@@ -0,0 +1,3 @@
diff --git a/file.txt b/file.txt
new file mode 100644
index 0000000..e69de29
+16
View File
@@ -0,0 +1,16 @@
diff --git a/file.sh b/file.sh
new file mode 100755
index 0000000..c9e9e05
--- /dev/null
+++ b/file.sh
@@ -0,0 +1,10 @@
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+4
View File
@@ -0,0 +1,4 @@
diff --git a/file.txt b/numbers.txt
similarity index 100%
rename from file.txt
rename to numbers.txt
+18
View File
@@ -0,0 +1,18 @@
diff --git a/file.txt b/numbers.txt
similarity index 77%
rename from file.txt
rename to numbers.txt
index c9e9e05..a6b31d6 100644
--- a/file.txt
+++ b/numbers.txt
@@ -3,8 +3,9 @@ two
three
four
five
-six
+ six
seven
eight
nine
ten
+eleven