Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Commit d1d4fdb

Browse files
committed
Removed unneccessary code
1 parent 266f5bf commit d1d4fdb

File tree

11 files changed

+131
-112
lines changed

11 files changed

+131
-112
lines changed

GitRewrite/Delete/DeleteObjects.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ private static ObjectHash RemoveObjectFromTree(
9595

9696
resultingLines.Add(new Tree.TreeLine(line.TextBytes, newTreeHash));
9797
}
98-
else
99-
{
100-
101-
}
10298
}
10399
}
104100
else
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text;
3+
using GitRewrite.GitObjects;
34

45
namespace GitRewrite.Delete
56
{
@@ -9,7 +10,7 @@ public class FileSimpleDeleteStrategy : IFileDeletionStrategy
910

1011
public FileSimpleDeleteStrategy(string fileName) => _fileName = Encoding.UTF8.GetBytes(fileName);
1112

12-
public bool DeleteObject(in ReadOnlySpan<byte> fileName, ReadOnlySpan<byte> currentPath)
13-
=> fileName.SequenceEqual(_fileName);
13+
public bool DeleteObject(in ReadOnlySpan<byte> fileName, ReadOnlySpan<byte> currentPath) =>
14+
fileName.SpanEquals(_fileName);
1415
}
1516
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
using System;
22
using System.Text;
3+
using GitRewrite.GitObjects;
34

45
namespace GitRewrite.Delete
56
{
67
class FolderExactDeletionStrategy : IFolderDeletionStrategy
78
{
8-
private readonly ReadOnlyMemory<byte> _fileName;
9+
private readonly Memory<byte> _folderName;
910

10-
public FolderExactDeletionStrategy(string fileName) => _fileName = Encoding.UTF8.GetBytes(fileName);
11+
public FolderExactDeletionStrategy(string fileName) => _folderName = Encoding.UTF8.GetBytes(fileName);
1112

12-
public bool DeleteObject(in ReadOnlySpan<byte> currentPath) => _fileName.Span.SequenceEqual(currentPath);
13+
public bool DeleteObject(in ReadOnlySpan<byte> currentPath) => _folderName.Span.SpanEquals(currentPath);
1314
}
1415
}

GitRewrite/GitObjects/Commit.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ public sealed class Commit : GitObjectBase, IEquatable<Commit>
1414
private readonly List<ObjectHash> _parents;
1515
private readonly Memory<byte> _treeHash;
1616

17-
private static readonly byte[] TreePrefix = "tree ".Select(x => (byte) x).ToArray();
18-
private static readonly byte[] ParentPrefix = "parent ".Select(x => (byte) x).ToArray();
19-
private static readonly byte[] AuthorPrefix = "author ".Select(x => (byte) x).ToArray();
20-
private static readonly byte[] CommitterPrefix = "committer ".Select(x => (byte) x).ToArray();
21-
private static readonly byte[] GpgSigPrefix = "gpgsig ".Select(x => (byte) x).ToArray();
22-
2317
public Commit(ObjectHash hash, byte[] bytes) : base(hash, GitObjectType.Commit)
2418
{
2519
_content = bytes;
@@ -30,11 +24,11 @@ public Commit(ObjectHash hash, byte[] bytes) : base(hash, GitObjectType.Commit)
3024
while (nextNewLine != -1)
3125
{
3226
var contentSpan = content.Span;
33-
if (contentSpan.StartsWith(TreePrefix))
27+
if (contentSpan.StartsWith(ObjectPrefixes.TreePrefix))
3428
{
3529
_treeHash = content.Slice(0, nextNewLine);
3630
}
37-
else if (contentSpan.StartsWith(ParentPrefix))
31+
else if (contentSpan.StartsWith(ObjectPrefixes.ParentPrefix))
3832
{
3933
_parents.Add(new ObjectHash(content.Span.Slice(7, nextNewLine - 7)));
4034
}
@@ -43,15 +37,15 @@ public Commit(ObjectHash hash, byte[] bytes) : base(hash, GitObjectType.Commit)
4337
_commitMessage = content.Slice(1);
4438
break;
4539
}
46-
else if (contentSpan.StartsWith(AuthorPrefix))
40+
else if (contentSpan.StartsWith(ObjectPrefixes.AuthorPrefix))
4741
{
4842
_authorLine = content.Slice(0, nextNewLine);
4943
}
50-
else if (contentSpan.StartsWith(CommitterPrefix))
44+
else if (contentSpan.StartsWith(ObjectPrefixes.CommitterPrefix))
5145
{
5246
_committerLine = content.Slice(0, nextNewLine);
5347
}
54-
else if (contentSpan.StartsWith(GpgSigPrefix))
48+
else if (contentSpan.StartsWith(ObjectPrefixes.GpgSigPrefix))
5549
{
5650
// gpgsig are not really handled, instead a gpgsig is not written back when rewriting the object
5751
var pgpSignatureEnd = content.Span.IndexOf(PgpSignatureEnd);
@@ -115,7 +109,7 @@ public static byte[] GetSerializedCommitWithChangedTreeAndParents(Commit commit,
115109
IEnumerable<ObjectHash> parents)
116110
{
117111
var tree = new byte[45];
118-
Array.Copy(TreePrefix, tree, 5);
112+
Array.Copy(ObjectPrefixes.TreePrefix, tree, 5);
119113
var hashString = treeHash.ToString();
120114
for (int i = 0; i < hashString.Length; i++)
121115
{
@@ -147,25 +141,20 @@ public static byte[] GetSerializedCommitWithChangedTreeAndParents(Commit commit,
147141
resultBuffer[bytesCopied++] = (byte) '\n';
148142
}
149143

150-
SpanToArrayCopy(author.Span, resultBuffer, bytesCopied);
144+
author.Span.CopyTo(resultBuffer.AsSpan(bytesCopied, author.Length));
151145
bytesCopied += author.Length;
152146
resultBuffer[bytesCopied++] = (byte) '\n';
153147

154-
SpanToArrayCopy(committer.Span, resultBuffer, bytesCopied);
148+
committer.Span.CopyTo(resultBuffer.AsSpan(bytesCopied, committer.Length));
155149
bytesCopied += committer.Length;
156150
resultBuffer[bytesCopied++] = (byte) '\n';
157151
resultBuffer[bytesCopied++] = (byte) '\n';
158152

159-
SpanToArrayCopy(message.Span, resultBuffer, bytesCopied);
153+
message.Span.CopyTo(resultBuffer.AsSpan(bytesCopied, message.Length));
160154

161155
return resultBuffer;
162156
}
163157

164-
private static void SpanToArrayCopy<T>(in ReadOnlySpan<T> source, T[] destination, int destinationIndex)
165-
{
166-
for (var i = 0; i < source.Length; i++) destination[i + destinationIndex] = source[i];
167-
}
168-
169158
public override bool Equals(object obj) => ReferenceEquals(this, obj) || obj is Commit other && Equals(other);
170159

171160
public override int GetHashCode() => base.GetHashCode();

GitRewrite/GitObjects/GitObjectBase.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@ public bool Equals(GitObjectBase other)
2727
return Hash.Equals(other.Hash);
2828
}
2929

30-
protected static int IndexOf(in ReadOnlySpan<byte> byteSpan, char c)
31-
{
32-
for (var i = 0; i < byteSpan.Length; i++)
33-
if (byteSpan[i] == c)
34-
return i;
35-
36-
return -1;
37-
}
38-
39-
protected static bool StartsWith(in ReadOnlyMemory<byte> memory, string str)
40-
{
41-
if (str.Length > memory.Length)
42-
return false;
43-
44-
var span = memory.Span;
45-
46-
for (var i = str.Length - 1; i >= 0; i--)
47-
if (span[i] != str[i])
48-
return false;
49-
50-
return true;
51-
}
52-
5330
public abstract byte[] SerializeToBytes();
5431

5532
public override bool Equals(object obj)

GitRewrite/GitObjects/ObjectHash.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23

34
namespace GitRewrite.GitObjects
45
{
@@ -38,17 +39,7 @@ public ObjectHash(in ReadOnlySpan<byte> hashStringAsBytes)
3839

3940
public override string ToString() => Hash.ByteArrayToHexViaLookup32(Bytes);
4041

41-
public bool Equals(ObjectHash other)
42-
{
43-
var bytes = Bytes;
44-
var otherBytes = other.Bytes;
45-
46-
for (var i = bytes.Length - 1; i >= 0; i--)
47-
if (bytes[i] != otherBytes[i])
48-
return false;
49-
50-
return true;
51-
}
42+
public bool Equals(ObjectHash other) => Bytes.AsSpan().SpanEquals(other.Bytes.AsSpan());
5243

5344
public override bool Equals(object obj) => obj is ObjectHash other && Equals(other);
5445

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Linq;
2+
3+
namespace GitRewrite.GitObjects
4+
{
5+
public static class ObjectPrefixes
6+
{
7+
public static readonly byte[] TreePrefix = "tree ".Select(x => (byte) x).ToArray();
8+
public static readonly byte[] ParentPrefix = "parent ".Select(x => (byte) x).ToArray();
9+
public static readonly byte[] AuthorPrefix = "author ".Select(x => (byte) x).ToArray();
10+
public static readonly byte[] CommitterPrefix = "committer ".Select(x => (byte) x).ToArray();
11+
public static readonly byte[] GpgSigPrefix = "gpgsig ".Select(x => (byte) x).ToArray();
12+
public static readonly byte[] TagPrefix = "tag ".Select(x => (byte) x).ToArray();
13+
public static readonly byte[] TaggerPrefix = "tagger ".Select(x => (byte) x).ToArray();
14+
public static readonly byte[] ObjectPrefix = "object ".Select(x => (byte) x).ToArray();
15+
public static readonly byte[] TypePrefix = "type ".Select(x => (byte) x).ToArray();
16+
}
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace GitRewrite.GitObjects
6+
{
7+
static class SpanExtensions
8+
{
9+
public static bool SpanEquals(this in Span<byte> span1, in ReadOnlySpan<byte> span2)
10+
{
11+
if (span1.Length != span2.Length)
12+
return false;
13+
14+
for (int i = span1.Length - 1; i >= 0; i--)
15+
{
16+
if (span1[i] != span2[i])
17+
return false;
18+
}
19+
20+
return true;
21+
}
22+
23+
public static bool SpanEquals(this in ReadOnlySpan<byte> span1, in ReadOnlySpan<byte> span2)
24+
{
25+
if (span1.Length != span2.Length)
26+
return false;
27+
28+
for (int i = span1.Length - 1; i >= 0; i--)
29+
{
30+
if (span1[i] != span2[i])
31+
return false;
32+
}
33+
34+
return true;
35+
}
36+
}
37+
}

GitRewrite/GitObjects/Tag.cs

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,98 @@
11
using System;
2+
using System.Linq;
23
using System.Text;
34

45
namespace GitRewrite.GitObjects
56
{
67
public sealed class Tag : GitObjectBase
78
{
9+
private static readonly byte[] TagKey = "tag".Select(x => (byte) x).ToArray();
10+
private readonly Memory<byte> _content;
11+
private readonly Memory<byte> _message;
812
private readonly Memory<byte> _object;
9-
private readonly Memory<byte> _type;
1013
private readonly Memory<byte> _tag;
1114
private readonly Memory<byte> _tagger;
12-
private readonly Memory<byte> _message;
13-
private readonly Memory<byte> _content;
15+
private readonly Memory<byte> _type;
16+
17+
public Tag(ObjectHash hash, Memory<byte> content) : base(hash, GitObjectType.Tag)
18+
{
19+
_content = content;
20+
21+
var nextNewLine = content.Span.IndexOf<byte>(10);
22+
while (nextNewLine != -1)
23+
{
24+
var contentSpan = content.Span;
25+
if (contentSpan.StartsWith(ObjectPrefixes.ObjectPrefix))
26+
{
27+
_object = content.Slice(0, nextNewLine);
28+
}
29+
else if (contentSpan.StartsWith(ObjectPrefixes.TypePrefix))
30+
{
31+
_type = content.Slice(0, nextNewLine);
32+
}
33+
else if (contentSpan.StartsWith(ObjectPrefixes.TagPrefix))
34+
{
35+
_tag = content.Slice(0, nextNewLine);
36+
}
37+
else if (contentSpan.StartsWith(ObjectPrefixes.TaggerPrefix))
38+
{
39+
_tagger = content.Slice(0, nextNewLine);
40+
}
41+
else if (content.Span[0] == 10)
42+
{
43+
_message = content.Slice(1);
44+
break;
45+
}
46+
47+
content = content.Slice(nextNewLine + 1);
48+
nextNewLine = content.Span.IndexOf<byte>(10);
49+
}
50+
}
51+
52+
public string Object => Encoding.UTF8.GetString(_object.Span.Slice(7));
53+
public string TypeName => Encoding.UTF8.GetString(_type.Span.Slice(5));
54+
public string TagName => Encoding.UTF8.GetString(_tag.Span.Slice(4));
55+
public string Tagger => Encoding.UTF8.GetString(_tagger.Span.Slice(7));
56+
public string Message => Encoding.UTF8.GetString(_message.Span);
57+
58+
public bool PointsToTag => _type.Span.StartsWith(TagKey);
1459

1560
public Tag WithNewObject(string obj)
1661
{
1762
var resultBuffer = new byte[_content.Length];
1863
var resultIndex = 0;
1964

20-
for (int i = 0; i < 7; i++)
65+
for (var i = 0; i < 7; i++)
2166
resultBuffer[resultIndex++] = _object.Span[i];
2267

2368
var objBytes = Encoding.ASCII.GetBytes(obj);
24-
for (int i = 0; i < objBytes.Length; i++)
69+
for (var i = 0; i < objBytes.Length; i++)
2570
resultBuffer[resultIndex++] = objBytes[i];
2671

2772
resultBuffer[resultIndex++] = 10;
2873

29-
for (int i = 0; i < _type.Length; i++)
74+
for (var i = 0; i < _type.Length; i++)
3075
resultBuffer[resultIndex++] = _type.Span[i];
3176

3277
resultBuffer[resultIndex++] = 10;
3378

34-
for (int i = 0; i < _tag.Length; i++)
79+
for (var i = 0; i < _tag.Length; i++)
3580
resultBuffer[resultIndex++] = _tag.Span[i];
3681

3782
resultBuffer[resultIndex++] = 10;
3883

39-
for (int i = 0; i < _tagger.Length; i++)
84+
for (var i = 0; i < _tagger.Length; i++)
4085
resultBuffer[resultIndex++] = _tagger.Span[i];
4186

4287
resultBuffer[resultIndex++] = 10;
4388
resultBuffer[resultIndex++] = 10;
4489

45-
for (int i = 0; i < _message.Length; i++)
90+
for (var i = 0; i < _message.Length; i++)
4691
resultBuffer[resultIndex++] = _message.Span[i];
4792

4893
return GitObjectFactory.TagFromContentBytes(resultBuffer);
4994
}
5095

51-
public string Object => Encoding.UTF8.GetString(_object.Span.Slice(7));
52-
public string TypeName => Encoding.UTF8.GetString(_type.Span.Slice(5));
53-
public string TagName => Encoding.UTF8.GetString(_tag.Span.Slice(4));
54-
public string Tagger => Encoding.UTF8.GetString(_tagger.Span.Slice(7));
55-
public string Message => Encoding.UTF8.GetString(_message.Span);
56-
57-
public bool PointsToTag => StartsWith(_type.Slice(5), "tag");
58-
59-
public Tag(ObjectHash hash, Memory<byte> content) : base(hash, GitObjectType.Tag)
60-
{
61-
_content = content;
62-
63-
var nextNewLine = content.Span.IndexOf<byte>(10);
64-
while (nextNewLine != -1)
65-
{
66-
if (StartsWith(content, "object "))
67-
_object = content.Slice(0, nextNewLine);
68-
else if (StartsWith(content, "type "))
69-
_type = content.Slice(0, nextNewLine);
70-
else if (StartsWith(content, "tag "))
71-
_tag = content.Slice(0, nextNewLine);
72-
else if (StartsWith(content, "tagger "))
73-
_tagger = content.Slice(0, nextNewLine);
74-
else if (content.Span[0] == 10)
75-
{
76-
_message = content.Slice(1);
77-
break;
78-
}
79-
80-
content = content.Slice(nextNewLine + 1);
81-
nextNewLine = content.Span.IndexOf<byte>(10);
82-
}
83-
}
84-
8596
public override byte[] SerializeToBytes() => _content.ToArray();
8697
}
8798
}

0 commit comments

Comments
 (0)