diff options
author | liwenqiu <liwenqiu@gmail.com> | 2023-05-07 22:37:08 +0800 |
---|---|---|
committer | liwenqiu <liwenqiu@gmail.com> | 2023-10-04 13:12:38 +0800 |
commit | 3dbb11f2d99839e618cf8e100f0e8f77f543637a (patch) | |
tree | 6c19c18d24477fd7c8d8febe473ff1ed9dd5f4df | |
parent | 52c2972976737a00fce91d7deb1278a6a460cae6 (diff) | |
download | go-git-3dbb11f2d99839e618cf8e100f0e8f77f543637a.tar.gz |
plumbing: parse the encoding header of the commit object
other part can re-code the commit message according to the encoding to this encoding info
-rw-r--r-- | plumbing/object/commit.go | 23 | ||||
-rw-r--r-- | plumbing/object/commit_test.go | 6 |
2 files changed, 25 insertions, 4 deletions
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go index 508e93c..ceed5d0 100644 --- a/plumbing/object/commit.go +++ b/plumbing/object/commit.go @@ -17,19 +17,25 @@ import ( ) const ( - beginpgp string = "-----BEGIN PGP SIGNATURE-----" - endpgp string = "-----END PGP SIGNATURE-----" - headerpgp string = "gpgsig" + beginpgp string = "-----BEGIN PGP SIGNATURE-----" + endpgp string = "-----END PGP SIGNATURE-----" + headerpgp string = "gpgsig" + headerencoding string = "encoding" // https://github.com/git/git/blob/bcb6cae2966cc407ca1afc77413b3ef11103c175/Documentation/gitformat-signature.txt#L153 // When a merge commit is created from a signed tag, the tag is embedded in // the commit with the "mergetag" header. headermergetag string = "mergetag" + + defaultUtf8CommitMesageEncoding MessageEncoding = "UTF-8" ) // Hash represents the hash of an object type Hash plumbing.Hash +// MessageEncoding represents the encoding of a commit +type MessageEncoding string + // Commit points to a single tree, marking it as what the project looked like // at a certain point in time. It contains meta-information about that point // in time, such as a timestamp, the author of the changes since the last @@ -54,6 +60,8 @@ type Commit struct { TreeHash plumbing.Hash // ParentHashes are the hashes of the parent commits of the commit. ParentHashes []plumbing.Hash + // Encoding is the encoding of the commit. + Encoding MessageEncoding s storer.EncodedObjectStorer } @@ -181,6 +189,7 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) { } c.Hash = o.Hash() + c.Encoding = defaultUtf8CommitMesageEncoding reader, err := o.Reader() if err != nil { @@ -247,6 +256,8 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) { case headermergetag: c.MergeTag += string(data) + "\n" mergetag = true + case headerencoding: + c.Encoding = MessageEncoding(data) case headerpgp: c.PGPSignature += string(data) + "\n" pgpsig = true @@ -324,6 +335,12 @@ func (c *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) { } } + if string(c.Encoding) != "" && c.Encoding != defaultUtf8CommitMesageEncoding { + if _, err = fmt.Fprintf(w, "\n%s %s", headerencoding, c.Encoding); err != nil { + return err + } + } + if c.PGPSignature != "" && includeSig { if _, err = fmt.Fprint(w, "\n"+headerpgp+" "); err != nil { return err diff --git a/plumbing/object/commit_test.go b/plumbing/object/commit_test.go index f1a3447..3e1fe1b 100644 --- a/plumbing/object/commit_test.go +++ b/plumbing/object/commit_test.go @@ -228,6 +228,7 @@ change Message: "Message\n\nFoo\nBar\nWith trailing blank lines\n\n", TreeHash: plumbing.NewHash("f000000000000000000000000000000000000001"), ParentHashes: []plumbing.Hash{plumbing.NewHash("f000000000000000000000000000000000000002")}, + Encoding: defaultUtf8CommitMesageEncoding, }, { Author: Signature{Name: "Foo", Email: "foo@example.local", When: ts}, @@ -240,6 +241,7 @@ change plumbing.NewHash("f000000000000000000000000000000000000006"), plumbing.NewHash("f000000000000000000000000000000000000007"), }, + Encoding: MessageEncoding("ISO-8859-1"), }, { Author: Signature{Name: "Foo", Email: "foo@example.local", When: ts}, @@ -251,6 +253,7 @@ change plumbing.NewHash("f000000000000000000000000000000000000003"), }, MergeTag: tag, + Encoding: defaultUtf8CommitMesageEncoding, }, { Author: Signature{Name: "Foo", Email: "foo@example.local", When: ts}, @@ -263,6 +266,7 @@ change }, MergeTag: tag, PGPSignature: pgpsignature, + Encoding: defaultUtf8CommitMesageEncoding, }, } for _, commit := range commits { @@ -530,7 +534,7 @@ func (s *SuiteCommit) TestMalformedHeader(c *C) { } func (s *SuiteCommit) TestEncodeWithoutSignature(c *C) { - //Similar to TestString since no signature + // Similar to TestString since no signature encoded := &plumbing.MemoryObject{} err := s.Commit.EncodeWithoutSignature(encoded) c.Assert(err, IsNil) |