@@ -1293,13 +1293,38 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1293
1293
HANDLE h ;
1294
1294
DWORD ret ;
1295
1295
int len ;
1296
+ const char * last_component = NULL ;
1296
1297
1297
1298
if (xutftowcs_path (wpath , path ) < 0 )
1298
1299
return NULL ;
1299
1300
1300
1301
h = CreateFileW (wpath , 0 ,
1301
1302
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
1302
1303
OPEN_EXISTING , FILE_FLAG_BACKUP_SEMANTICS , NULL );
1304
+
1305
+ /*
1306
+ * strbuf_realpath() allows the last path component to not exist. If
1307
+ * that is the case, now it's time to try without last component.
1308
+ */
1309
+ if (h == INVALID_HANDLE_VALUE &&
1310
+ GetLastError () == ERROR_FILE_NOT_FOUND ) {
1311
+ /* cut last component off of `wpath` */
1312
+ wchar_t * p = wpath + wcslen (wpath );
1313
+
1314
+ while (p != wpath )
1315
+ if (* (-- p ) == L'/' || * p == L'\\' )
1316
+ break ; /* found start of last component */
1317
+
1318
+ if (p != wpath && (last_component = find_last_dir_sep (path ))) {
1319
+ last_component ++ ; /* skip directory separator */
1320
+ * p = L'\0' ;
1321
+ h = CreateFileW (wpath , 0 , FILE_SHARE_READ |
1322
+ FILE_SHARE_WRITE | FILE_SHARE_DELETE ,
1323
+ NULL , OPEN_EXISTING ,
1324
+ FILE_FLAG_BACKUP_SEMANTICS , NULL );
1325
+ }
1326
+ }
1327
+
1303
1328
if (h == INVALID_HANDLE_VALUE )
1304
1329
return NULL ;
1305
1330
@@ -1314,6 +1339,13 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1314
1339
if (len < 0 )
1315
1340
return NULL ;
1316
1341
resolved -> len = len ;
1342
+
1343
+ if (last_component ) {
1344
+ /* Use forward-slash, like `normalize_ntpath()` */
1345
+ strbuf_addch (resolved , '/' );
1346
+ strbuf_addstr (resolved , last_component );
1347
+ }
1348
+
1317
1349
return resolved -> buf ;
1318
1350
1319
1351
}
0 commit comments