|
| 1 | +//////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// GitReader - Lightweight Git local repository traversal library. |
| 4 | +// Copyright (c) Kouji Matsui (@kozy_kekyo, @[email protected]) |
| 5 | +// |
| 6 | +// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0 |
| 7 | +// |
| 8 | +//////////////////////////////////////////////////////////////////////////// |
| 9 | + |
| 10 | +using GitReader.Primitive; |
| 11 | +using System; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using System.Threading; |
| 14 | +using System.IO; |
| 15 | + |
| 16 | +#if DEBUG |
| 17 | +using Lepracaun; |
| 18 | +#endif |
| 19 | + |
| 20 | +namespace GitReader; |
| 21 | + |
| 22 | +public static class Program |
| 23 | +{ |
| 24 | + private static async Task ExtractObjectAsync( |
| 25 | + TextWriter tw, |
| 26 | + string repositoryPath, string objectId, string toPath) |
| 27 | + { |
| 28 | + var basePath = Path.GetDirectoryName(toPath) switch |
| 29 | + { |
| 30 | + null => Path.DirectorySeparatorChar.ToString(), |
| 31 | + "" => ".", |
| 32 | + string path => path, |
| 33 | + }; |
| 34 | + if (!Directory.Exists(basePath)) |
| 35 | + { |
| 36 | + Directory.CreateDirectory(basePath); |
| 37 | + } |
| 38 | + |
| 39 | + using var repository = |
| 40 | + await Repository.Factory.OpenPrimitiveAsync(repositoryPath); |
| 41 | + |
| 42 | + using var result = await repository.OpenRawObjectStreamAsync(objectId); |
| 43 | + |
| 44 | + using var fs = new FileStream( |
| 45 | + toPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 65536, true); |
| 46 | + |
| 47 | + await result.Stream.CopyToAsync(fs); |
| 48 | + await fs.FlushAsync(); |
| 49 | + |
| 50 | + tw.WriteLine($"Extracted an object: Id={objectId}, Type={result.Type}, Path={toPath}"); |
| 51 | + } |
| 52 | + |
| 53 | + private static Task MainAsync(string[] args) |
| 54 | + { |
| 55 | + if (args.Length == 3) |
| 56 | + { |
| 57 | + return ExtractObjectAsync(Console.Out, args[0], args[1], args[2]); |
| 58 | + } |
| 59 | + |
| 60 | + Console.WriteLine("usage: gitextracttest <repository path> <object id> <to path>"); |
| 61 | + return Task.CompletedTask; |
| 62 | + } |
| 63 | + |
| 64 | +#if DEBUG |
| 65 | + public static void Main(string[] args) |
| 66 | + { |
| 67 | + // Single-threaded for easier debugging of asynchronous operations. |
| 68 | + var sc = new SingleThreadedSynchronizationContext(); |
| 69 | + SynchronizationContext.SetSynchronizationContext(sc); |
| 70 | + |
| 71 | + sc.Run(MainAsync(args)); |
| 72 | + } |
| 73 | +#else |
| 74 | + public static Task Main(string[] args) => |
| 75 | + MainAsync(args); |
| 76 | +#endif |
| 77 | +} |
0 commit comments