@@ -1293,13 +1293,38 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
12931293 HANDLE h ;
12941294 DWORD ret ;
12951295 int len ;
1296+ const char * last_component = NULL ;
12961297
12971298 if (xutftowcs_path (wpath , path ) < 0 )
12981299 return NULL ;
12991300
13001301 h = CreateFileW (wpath , 0 ,
13011302 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE , NULL ,
13021303 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+
13031328 if (h == INVALID_HANDLE_VALUE )
13041329 return NULL ;
13051330
@@ -1314,6 +1339,13 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
13141339 if (len < 0 )
13151340 return NULL ;
13161341 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+
13171349 return resolved -> buf ;
13181350
13191351}
0 commit comments