Skip to content

Commit a9c28a7

Browse files
authored
Merge pull request #1165 from adamhathcock/adam/buffer-size-consolidation
(Release) Buffer size consolidation
2 parents b9fc680 + 4d31436 commit a9c28a7

25 files changed

+112
-134
lines changed

build/Program.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ IEnumerable<string> GetFiles(string d)
230230
}
231231
else
232232
{
233-
// Not tagged - create prerelease version based on next minor version
233+
// Not tagged - create prerelease version
234234
var allTags = (await GetGitOutput("tag", "--list"))
235235
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
236236
.Where(tag => Regex.IsMatch(tag.Trim(), @"^\d+\.\d+\.\d+$"))
@@ -240,8 +240,22 @@ IEnumerable<string> GetFiles(string d)
240240
var lastTag = allTags.OrderBy(tag => Version.Parse(tag)).LastOrDefault() ?? "0.0.0";
241241
var lastVersion = Version.Parse(lastTag);
242242

243-
// Increment minor version for next release
244-
var nextVersion = new Version(lastVersion.Major, lastVersion.Minor + 1, 0);
243+
// Determine version increment based on branch
244+
var currentBranch = await GetCurrentBranch();
245+
Version nextVersion;
246+
247+
if (currentBranch == "release")
248+
{
249+
// Release branch: increment patch version
250+
nextVersion = new Version(lastVersion.Major, lastVersion.Minor, lastVersion.Build + 1);
251+
Console.WriteLine($"Building prerelease for release branch (patch increment)");
252+
}
253+
else
254+
{
255+
// Master or other branches: increment minor version
256+
nextVersion = new Version(lastVersion.Major, lastVersion.Minor + 1, 0);
257+
Console.WriteLine($"Building prerelease for {currentBranch} branch (minor increment)");
258+
}
245259

246260
// Use commit count since the last version tag if available; otherwise, fall back to total count
247261
var revListArgs = allTags.Any() ? $"--count {lastTag}..HEAD" : "--count HEAD";
@@ -253,6 +267,28 @@ IEnumerable<string> GetFiles(string d)
253267
}
254268
}
255269

270+
static async Task<string> GetCurrentBranch()
271+
{
272+
// In GitHub Actions, GITHUB_REF_NAME contains the branch name
273+
var githubRefName = Environment.GetEnvironmentVariable("GITHUB_REF_NAME");
274+
if (!string.IsNullOrEmpty(githubRefName))
275+
{
276+
return githubRefName;
277+
}
278+
279+
// Fallback to git command for local builds
280+
try
281+
{
282+
var (output, _) = await ReadAsync("git", "branch --show-current");
283+
return output.Trim();
284+
}
285+
catch (Exception ex)
286+
{
287+
Console.WriteLine($"Warning: Could not determine current branch: {ex.Message}");
288+
return "unknown";
289+
}
290+
}
291+
256292
static async Task<string> GetGitOutput(string command, string args)
257293
{
258294
try

src/SharpCompress/Archives/ArchiveFactory.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,14 @@ private static T FindFactory<T>(Stream stream)
166166
);
167167
}
168168

169-
public static bool IsArchive(
170-
string filePath,
171-
out ArchiveType? type,
172-
int bufferSize = ReaderOptions.DefaultBufferSize
173-
)
169+
public static bool IsArchive(string filePath, out ArchiveType? type)
174170
{
175171
filePath.NotNullOrEmpty(nameof(filePath));
176172
using Stream s = File.OpenRead(filePath);
177-
return IsArchive(s, out type, bufferSize);
173+
return IsArchive(s, out type);
178174
}
179175

180-
public static bool IsArchive(
181-
Stream stream,
182-
out ArchiveType? type,
183-
int bufferSize = ReaderOptions.DefaultBufferSize
184-
)
176+
public static bool IsArchive(Stream stream, out ArchiveType? type)
185177
{
186178
type = null;
187179
stream.NotNull(nameof(stream));

src/SharpCompress/Archives/AutoArchiveFactory.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ class AutoArchiveFactory : IArchiveFactory
1414

1515
public IEnumerable<string> GetSupportedExtensions() => throw new NotSupportedException();
1616

17-
public bool IsArchive(
18-
Stream stream,
19-
string? password = null,
20-
int bufferSize = ReaderOptions.DefaultBufferSize
21-
) => throw new NotSupportedException();
17+
public bool IsArchive(Stream stream, string? password = null) =>
18+
throw new NotSupportedException();
2219

2320
public FileInfo? GetFilePart(int index, FileInfo part1) => throw new NotSupportedException();
2421

src/SharpCompress/Archives/IArchiveEntryExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace SharpCompress.Archives;
99

1010
public static class IArchiveEntryExtensions
1111
{
12-
private const int BufferSize = 81920;
13-
1412
/// <param name="archiveEntry">The archive entry to extract.</param>
1513
extension(IArchiveEntry archiveEntry)
1614
{
@@ -28,7 +26,7 @@ public void WriteTo(Stream streamToWriteTo, IProgress<ProgressReport>? progress
2826

2927
using var entryStream = archiveEntry.OpenEntryStream();
3028
var sourceStream = WrapWithProgress(entryStream, archiveEntry, progress);
31-
sourceStream.CopyTo(streamToWriteTo, BufferSize);
29+
sourceStream.CopyTo(streamToWriteTo, Constants.BufferSize);
3230
}
3331

3432
/// <summary>
@@ -51,7 +49,7 @@ public async Task WriteToAsync(
5149
using var entryStream = await archiveEntry.OpenEntryStreamAsync(cancellationToken);
5250
var sourceStream = WrapWithProgress(entryStream, archiveEntry, progress);
5351
await sourceStream
54-
.CopyToAsync(streamToWriteTo, BufferSize, cancellationToken)
52+
.CopyToAsync(streamToWriteTo, Constants.BufferSize, cancellationToken)
5553
.ConfigureAwait(false);
5654
}
5755
}

src/SharpCompress/Archives/Tar/TarArchive.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ var header in TarHeaderFactory.ReadHeader(
180180
using (var entryStream = entry.OpenEntryStream())
181181
{
182182
using var memoryStream = new MemoryStream();
183-
entryStream.CopyTo(memoryStream);
183+
entryStream.CopyTo(memoryStream, Constants.BufferSize);
184184
memoryStream.Position = 0;
185185
var bytes = memoryStream.ToArray();
186186

src/SharpCompress/Archives/Zip/ZipArchive.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,38 +124,27 @@ public static ZipArchive Open(Stream stream, ReaderOptions? readerOptions = null
124124
);
125125
}
126126

127-
public static bool IsZipFile(
128-
string filePath,
129-
string? password = null,
130-
int bufferSize = ReaderOptions.DefaultBufferSize
131-
) => IsZipFile(new FileInfo(filePath), password, bufferSize);
132-
133-
public static bool IsZipFile(
134-
FileInfo fileInfo,
135-
string? password = null,
136-
int bufferSize = ReaderOptions.DefaultBufferSize
137-
)
127+
public static bool IsZipFile(string filePath, string? password = null) =>
128+
IsZipFile(new FileInfo(filePath), password);
129+
130+
public static bool IsZipFile(FileInfo fileInfo, string? password = null)
138131
{
139132
if (!fileInfo.Exists)
140133
{
141134
return false;
142135
}
143136
using Stream stream = fileInfo.OpenRead();
144-
return IsZipFile(stream, password, bufferSize);
137+
return IsZipFile(stream, password);
145138
}
146139

147-
public static bool IsZipFile(
148-
Stream stream,
149-
string? password = null,
150-
int bufferSize = ReaderOptions.DefaultBufferSize
151-
)
140+
public static bool IsZipFile(Stream stream, string? password = null)
152141
{
153142
var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null);
154143
try
155144
{
156145
if (stream is not SharpCompressStream)
157146
{
158-
stream = new SharpCompressStream(stream, bufferSize: bufferSize);
147+
stream = new SharpCompressStream(stream, bufferSize: Constants.BufferSize);
159148
}
160149

161150
var header = headerFactory
@@ -177,18 +166,14 @@ public static bool IsZipFile(
177166
}
178167
}
179168

180-
public static bool IsZipMulti(
181-
Stream stream,
182-
string? password = null,
183-
int bufferSize = ReaderOptions.DefaultBufferSize
184-
)
169+
public static bool IsZipMulti(Stream stream, string? password = null)
185170
{
186171
var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null);
187172
try
188173
{
189174
if (stream is not SharpCompressStream)
190175
{
191-
stream = new SharpCompressStream(stream, bufferSize: bufferSize);
176+
stream = new SharpCompressStream(stream, bufferSize: Constants.BufferSize);
192177
}
193178

194179
var header = headerFactory
@@ -229,7 +214,7 @@ protected override IEnumerable<ZipVolume> LoadVolumes(SourceStream stream)
229214
if (streams.Count() > 1) //test part 2 - true = multipart not split
230215
{
231216
streams[1].Position += 4; //skip the POST_DATA_DESCRIPTOR to prevent an exception
232-
var isZip = IsZipFile(streams[1], ReaderOptions.Password, ReaderOptions.BufferSize);
217+
var isZip = IsZipFile(streams[1], ReaderOptions.Password);
233218
streams[1].Position -= 4;
234219
if (isZip)
235220
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SharpCompress.Common;
2+
3+
public static class Constants
4+
{
5+
/// <summary>
6+
/// The default buffer size for stream operations, matching .NET's Stream.CopyTo default of 81920 bytes.
7+
/// This can be modified globally at runtime.
8+
/// </summary>
9+
public static int BufferSize { get; set; } = 81920;
10+
}

src/SharpCompress/Factories/AceFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public override IEnumerable<string> GetSupportedExtensions()
2222
yield return "ace";
2323
}
2424

25-
public override bool IsArchive(
26-
Stream stream,
27-
string? password = null,
28-
int bufferSize = ReaderOptions.DefaultBufferSize
29-
)
25+
public override bool IsArchive(Stream stream, string? password = null)
3026
{
3127
return AceHeader.IsArchive(stream);
3228
}

src/SharpCompress/Factories/ArcFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ public override IEnumerable<string> GetSupportedExtensions()
2323
yield return "arc";
2424
}
2525

26-
public override bool IsArchive(
27-
Stream stream,
28-
string? password = null,
29-
int bufferSize = ReaderOptions.DefaultBufferSize
30-
)
26+
public override bool IsArchive(Stream stream, string? password = null)
3127
{
3228
//You may have to use some(paranoid) checks to ensure that you actually are
3329
//processing an ARC file, since other archivers also adopted the idea of putting

src/SharpCompress/Factories/ArjFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ public override IEnumerable<string> GetSupportedExtensions()
2222
yield return "arj";
2323
}
2424

25-
public override bool IsArchive(
26-
Stream stream,
27-
string? password = null,
28-
int bufferSize = ReaderOptions.DefaultBufferSize
29-
)
25+
public override bool IsArchive(Stream stream, string? password = null)
3026
{
3127
return ArjHeader.IsArchive(stream);
3228
}

0 commit comments

Comments
 (0)