-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fixes #13274: Allow cygwin-paths on Windows #13297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
382071f
93087c9
568bb34
2f87d70
0f5cce4
c343086
773a4eb
2334c9e
37c6203
19d92f3
e6aea3b
025a2eb
70cc464
249f38f
5241f3c
385ab81
2c40476
fde43a6
35a84ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
requires info.picocli; | ||
opens org.jabref.cli; | ||
opens org.jabref.cli.converter; | ||
|
||
requires transitive org.jspecify; | ||
requires java.prefs; | ||
|
@@ -22,7 +23,6 @@ | |
requires org.tinylog.impl; | ||
|
||
kvitorr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
requires java.xml; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not modify lines of code not related to the PR if not 90% sure Here, the empty line does make sence to separate blocks. Please undo this change. |
||
// region: other libraries (alphabetically) | ||
requires io.github.adr; | ||
// endregion | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.jabref.cli.converter; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For naming, I think, this should be with "s", because there will be more than one converters in the future.
|
||
|
||
import java.nio.file.Path; | ||
|
||
import org.jabref.logic.util.io.FileUtil; | ||
|
||
import picocli.CommandLine; | ||
|
||
/// Converts Cygwin-style paths to Path objects using platform-specific formatting. | ||
calixtus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class CygWinPathConverter implements CommandLine.ITypeConverter<Path> { | ||
|
||
@Override | ||
public Path convert(String path) { | ||
return FileUtil.convertCygwinPathToWindows(path); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,6 +25,7 @@ | |||||
import org.jabref.logic.FilePreferences; | ||||||
import org.jabref.logic.citationkeypattern.BracketedPattern; | ||||||
import org.jabref.logic.layout.format.RemoveLatexCommandsFormatter; | ||||||
import org.jabref.logic.os.OS; | ||||||
import org.jabref.logic.util.StandardFileType; | ||||||
import org.jabref.model.database.BibDatabase; | ||||||
import org.jabref.model.database.BibDatabaseContext; | ||||||
|
@@ -49,6 +50,9 @@ public class FileUtil { | |||||
private static final String ELLIPSIS = "..."; | ||||||
private static final int ELLIPSIS_LENGTH = ELLIPSIS.length(); | ||||||
private static final RemoveLatexCommandsFormatter REMOVE_LATEX_COMMANDS_FORMATTER = new RemoveLatexCommandsFormatter(); | ||||||
private static final String CYGDRIVE_PREFIX = "/cygdrive/"; | ||||||
private static final String MNT_PREFIX = "/mnt/"; | ||||||
private static final Pattern ROOT_DRIVE_PATTERN = Pattern.compile("^/[a-zA-Z]/.*"); | ||||||
|
||||||
/** | ||||||
* MUST ALWAYS BE A SORTED ARRAY because it is used in a binary search | ||||||
|
@@ -589,4 +593,47 @@ public static String shortenFileName(String fileName, Integer maxLength) { | |||||
public static boolean isCharLegal(char c) { | ||||||
return Arrays.binarySearch(ILLEGAL_CHARS, c) < 0; | ||||||
} | ||||||
|
||||||
/// Converts a Cygwin-style file path to a Windows-style path if the operating system is Windows. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add something like "plainly converted if on non-Windows" |
||||||
/// | ||||||
/// Supported formats: | ||||||
/// - /cygdrive/c/Users/... → C:\Users\... | ||||||
/// - /mnt/c/Users/... → C:\Users\... | ||||||
/// - /c/Users/... → C:\Users\... | ||||||
/// | ||||||
/// @param filePath the input file path | ||||||
/// @return the converted path if running on Windows and path is in Cygwin format; otherwise, returns the original path | ||||||
public static Path convertCygwinPathToWindows(String filePath) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method also works on linux - proposal for another name:
Suggested change
|
||||||
if (filePath == null) { | ||||||
return null; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New public methods should not return null. The method should return Optional instead to better handle the null case and follow Java best practices. |
||||||
} | ||||||
|
||||||
if (!OS.WINDOWS) { | ||||||
return Path.of(filePath); | ||||||
} | ||||||
|
||||||
if (filePath.startsWith(MNT_PREFIX) && filePath.length() > 5) { | ||||||
return buildWindowsPathWithDriveLetterIndex(filePath, 5); | ||||||
} | ||||||
|
||||||
if (filePath.startsWith(CYGDRIVE_PREFIX) && filePath.length() > 10) { | ||||||
return buildWindowsPathWithDriveLetterIndex(filePath, 10); | ||||||
} | ||||||
|
||||||
if (ROOT_DRIVE_PATTERN.matcher(filePath).matches()) { | ||||||
return buildWindowsPathWithDriveLetterIndex(filePath, 1); | ||||||
} | ||||||
|
||||||
return Path.of(filePath); | ||||||
} | ||||||
|
||||||
/// Builds a Windows-style path from a Cygwin-style path using a known prefix index. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Triple-slash comments are used instead of proper JavaDoc format. Additionally, the comment is trivial and doesn't add new information beyond what the code shows. |
||||||
/// @param path the input file path | ||||||
/// @param letterIndex the index driver letter, zero-based indexing | ||||||
/// @return a windows-style path | ||||||
private static Path buildWindowsPathWithDriveLetterIndex(String path, int letterIndex) { | ||||||
String driveLetter = path.substring(letterIndex, letterIndex + 1).toUpperCase(); | ||||||
String windowsPath = path.substring(letterIndex + 1).replace("/", "\\\\"); | ||||||
return Path.of(driveLetter + ":" + windowsPath); | ||||||
} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.