@@ -42,7 +42,10 @@ internal sealed record Options(
4242 string ? Hostname = null ,
4343 string ? Guidance = null ,
4444 string ? GuidanceRoot = null ,
45- bool Check = false ) ;
45+ bool Check = false ,
46+ string ? BaseTip = null ,
47+ string ? MergeBase = null ,
48+ string ? BaseRef = null ) ;
4649
4750 internal sealed record Dependencies (
4851 Func < string , string ? , Task < JsonNode ? > > ? Api = null ,
@@ -98,6 +101,7 @@ internal static async Task<int> RunAsync(string[] args, Dependencies? dependenci
98101 private static Options ParseArguments ( string [ ] args )
99102 {
100103 string ? repo = null , pr = null , output = null , head = null , hostname = null , guidance = null , guidanceRoot = null ;
104+ string ? baseTip = null , mergeBase = null , baseRef = null ;
101105 var check = false ;
102106 for ( var index = 0 ; index < args . Length ; index ++ )
103107 {
@@ -107,7 +111,7 @@ private static Options ParseArguments(string[] args)
107111 check = true ;
108112 continue ;
109113 }
110- Require ( name is "--repo" or "--pr" or "--output" or "--head" or "--hostname" or "--guidance" or "--guidance-root"
114+ Require ( name is "--repo" or "--pr" or "--output" or "--head" or "--hostname" or "--guidance" or "--guidance-root" or "--base-tip" or "--merge-base" or "--base-ref"
111115 && index + 1 < args . Length , $ "Unknown or incomplete option: { name } ") ;
112116 var value = args [ ++ index ] ;
113117 switch ( name )
@@ -119,13 +123,38 @@ private static Options ParseArguments(string[] args)
119123 case "--hostname" : hostname = value ; break ;
120124 case "--guidance" : guidance = value ; break ;
121125 case "--guidance-root" : guidanceRoot = value ; break ;
126+ case "--base-tip" : baseTip = value ; break ;
127+ case "--merge-base" : mergeBase = value ; break ;
128+ case "--base-ref" : baseRef = value ; break ;
122129 }
123130 }
124- return new ( repo , pr , output ?? string . Empty , head , hostname , guidance , guidanceRoot , check ) ;
131+ return new ( repo , pr , output ?? string . Empty , head , hostname , guidance , guidanceRoot , check , baseTip , mergeBase , baseRef ) ;
125132 }
126133
127134 internal static string [ ] GitArguments ( params string [ ] args ) => [ "-c" , "core.longpaths=true" , .. args ] ;
128135
136+ internal static byte [ ] ExpandBinaryIndexLines ( byte [ ] diff , IReadOnlySet < string > knownIds )
137+ {
138+ return Encoding . Latin1 . GetBytes ( BinaryIndexLine ( ) . Replace ( Encoding . Latin1 . GetString ( diff ) , match =>
139+ $ "index { Expand ( match . Groups [ 1 ] . Value ) } ..{ Expand ( match . Groups [ 2 ] . Value ) } { match . Groups [ 3 ] . Value } { match . Groups [ 4 ] . Value } ") ) ;
140+
141+ string Expand ( string prefix )
142+ {
143+ if ( prefix . Length == 40 )
144+ {
145+ return prefix ;
146+ }
147+ if ( prefix . All ( character => character == '0' ) )
148+ {
149+ return new string ( '0' , 40 ) ;
150+ }
151+ var matches = knownIds . Where ( id => id . StartsWith ( prefix , StringComparison . Ordinal ) ) . Take ( 2 ) . ToArray ( ) ;
152+ Require ( matches . Length == 1 ,
153+ $ "The authoritative binary diff has an { ( matches . Length == 0 ? "unknown" : "ambiguous" ) } object prefix: { prefix } ") ;
154+ return matches [ 0 ] ;
155+ }
156+ }
157+
129158 private static byte [ ] Git ( string directory , params string [ ] args ) =>
130159 Run ( "git" , GitArguments ( [ "-C" , directory , .. args ] ) ) ;
131160
@@ -1297,6 +1326,11 @@ async Task<T> Timed<T>(string name, Func<Task<T>> action)
12971326 "Cannot resolve a single target repository; specify --repo OWNER/REPO and --pr NUMBER." ) ;
12981327 Require ( options . Output . Length > 0 , "Specify a new --output directory, or --check an existing prepared directory." ) ;
12991328 Require ( options . Head is null || FullShaRegex ( ) . IsMatch ( options . Head ) , "--head must be a full immutable commit." ) ;
1329+ Require ( options . BaseTip is null && options . MergeBase is null && options . BaseRef is null
1330+ || options . Head is not null && options . BaseTip is not null && FullShaRegex ( ) . IsMatch ( options . BaseTip )
1331+ && options . MergeBase is not null && FullShaRegex ( ) . IsMatch ( options . MergeBase )
1332+ && ! string . IsNullOrEmpty ( options . BaseRef ) ,
1333+ "A frozen base requires --head, --base-tip, --merge-base, and --base-ref with full immutable commits." ) ;
13001334 Require ( options . Guidance is null || options . GuidanceRoot is null , "Select either --guidance or --guidance-root." ) ;
13011335 var producer = ProducerHash ( dependencies . ProducerSourcePath ) ;
13021336 var host = options . Hostname ?? "github.com" ;
@@ -1334,14 +1368,21 @@ async Task<T> Timed<T>(string name, Func<Task<T>> action)
13341368 var head = pull [ "head" ] ! [ "sha" ] ! . GetValue < string > ( ) ;
13351369 Require ( options . Head is null || options . Head == head , "The live PR head differs from the expected frozen head." ) ;
13361370 var baseRef = pull [ "base" ] ! [ "ref" ] ! . GetValue < string > ( ) ;
1337- var encodedRef = Uri . EscapeDataString ( baseRef ) ;
1338- var branch = ( await Api ( $ "{ endpoint } /git/ref/heads/{ encodedRef } ") ) ! . AsObject ( ) ;
1339- var baseTip = branch [ "object" ] ? [ "sha" ] ? . GetValue < string > ( ) ?? "" ;
1371+ Require ( options . BaseRef is null || options . BaseRef == baseRef , "The live PR base ref differs from the expected frozen base." ) ;
1372+ var baseTip = options . BaseTip ?? "" ;
1373+ if ( options . BaseTip is null )
1374+ {
1375+ var encodedRef = Uri . EscapeDataString ( baseRef ) ;
1376+ var branch = ( await Api ( $ "{ endpoint } /git/ref/heads/{ encodedRef } ") ) ! . AsObject ( ) ;
1377+ baseTip = branch [ "object" ] ? [ "sha" ] ? . GetValue < string > ( ) ?? "" ;
1378+ }
13401379 Require ( FullShaRegex ( ) . IsMatch ( baseTip ) , "The base branch did not resolve to a full commit." ) ;
13411380 var comparison = ( await Api ( $ "{ endpoint } /compare/{ baseTip } ...{ head } ") ) ! . AsObject ( ) ;
13421381 var mergeBase = comparison [ "merge_base_commit" ] ? [ "sha" ] ? . GetValue < string > ( ) ?? "" ;
13431382 Require ( comparison [ "base_commit" ] ? [ "sha" ] ? . GetValue < string > ( ) == baseTip && FullShaRegex ( ) . IsMatch ( mergeBase ) ,
13441383 "GitHub did not return the expected immutable comparison identities." ) ;
1384+ Require ( options . MergeBase is null || options . MergeBase == mergeBase ,
1385+ "The comparison differs from the expected frozen merge base." ) ;
13451386 return ( new JsonObject
13461387 {
13471388 [ "hostname" ] = host ,
@@ -1480,7 +1521,13 @@ await Timed("diff", async () =>
14801521 Objects ( store , "read-tree" , frozen . Identity [ "mergeBase" ] ! . GetValue < string > ( ) ) ;
14811522 if ( diff . Length > 0 )
14821523 {
1483- Objects ( store , "apply" , "--cached" , "--binary" , "--whitespace=nowarn" , Path . Combine ( output , "diff.patch" ) ) ;
1524+ // REST omits binary payloads; resolve its prefixes against the frozen changed objects, not supplied bytes.
1525+ var knownIds = Utf8NoBom . GetString ( Objects ( store , "diff" , "--raw" , "--no-abbrev" , "-z" , "--no-renames" ,
1526+ "--no-ext-diff" , "--no-textconv" , frozen . Identity [ "mergeBase" ] ! . GetValue < string > ( ) , frozen . Identity [ "head" ] ! . GetValue < string > ( ) ) )
1527+ . Split ( '\0 ' ) . Where ( ( _ , index ) => index % 2 == 0 )
1528+ . SelectMany ( entry => entry . Split ( ' ' ) . Skip ( 2 ) . Take ( 2 ) ) . ToHashSet ( StringComparer . Ordinal ) ;
1529+ Run ( "git" , GitArguments ( [ "--git-dir" , store , "apply" , "--cached" , "--binary" , "--whitespace=nowarn" , "-" ] ) ,
1530+ input : ExpandBinaryIndexLines ( diff , knownIds ) ) ;
14841531 }
14851532 Require ( Utf8NoBom . GetString ( Objects ( store , "write-tree" ) ) . Trim ( )
14861533 == Utf8NoBom . GetString ( Objects ( store , "rev-parse" , $ "{ frozen . Identity [ "head" ] ! . GetValue < string > ( ) } ^{{tree}}") ) . Trim ( ) ,
@@ -2195,6 +2242,8 @@ private static bool TryJsonSafeInteger(JsonNode? value, out long result)
21952242 return true ;
21962243 }
21972244
2245+ [ GeneratedRegex ( "^index ([a-f0-9]{4,40})\\ .\\ .([a-f0-9]{4,40})( [0-7]{6})?(\\ r?\\ n)(?=Binary files [^\\ r\\ n]* differ(?:\\ r?\\ n|$))" , RegexOptions . Multiline | RegexOptions . CultureInvariant ) ]
2246+ private static partial Regex BinaryIndexLine ( ) ;
21982247 [ GeneratedRegex ( "^[a-f0-9]{40}$" , RegexOptions . CultureInvariant ) ]
21992248 private static partial Regex FullShaRegex ( ) ;
22002249 [ GeneratedRegex ( "^[a-z0-9_.-]+/[a-z0-9_.-]+$" , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ) ]
0 commit comments