-
Notifications
You must be signed in to change notification settings - Fork 917
Fixes all deprecated API usage in libs.jgit and update gitignore IO code #7093
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,20 +22,19 @@ | |
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.io.OutputStreamWriter; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.Arrays; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.ListIterator; | ||
| import java.util.Set; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.eclipse.jgit.ignore.IgnoreNode.MatchResult; | ||
| import org.eclipse.jgit.lib.Constants; | ||
| import org.eclipse.jgit.lib.CoreConfig; | ||
|
|
@@ -64,7 +63,7 @@ public IgnoreUnignoreCommand (Repository repository, GitClassFactory gitFactory, | |
| super(repository, gitFactory, monitor); | ||
| this.files = files; | ||
| this.monitor = monitor; | ||
| this.ignoreFiles = new LinkedHashSet<File>(); | ||
| this.ignoreFiles = new LinkedHashSet<>(); | ||
| this.listener = listener; | ||
| } | ||
|
|
||
|
|
@@ -104,7 +103,7 @@ protected void run () { | |
| private void changeIgnoreStatus (File f) throws IOException { | ||
| File parent = f; | ||
| boolean isDirectory = f.isDirectory() && (! Files.isSymbolicLink(f.toPath())); | ||
| StringBuilder sb = new StringBuilder('/'); | ||
| StringBuilder sb = new StringBuilder(); | ||
| if (isDirectory) { | ||
| sb.append('/'); | ||
| } | ||
|
|
@@ -129,68 +128,56 @@ private boolean addStatement (File gitIgnore, String path, boolean isDirectory, | |
| return addStatement(ignoreRules, gitIgnore, path, isDirectory, forceWrite, true) == MatchResult.CHECK_PARENT; | ||
| } | ||
|
|
||
| protected final void save (File gitIgnore, List<IgnoreRule> ignoreRules) throws IOException { | ||
| BufferedWriter bw = null; | ||
| File tmpFile = Files.createTempFile(gitIgnore.getParentFile().toPath(), Constants.DOT_GIT_IGNORE, "tmp").toFile(); //NOI18N | ||
| protected final void save(File gitIgnore, List<IgnoreRule> ignoreRules) throws IOException { | ||
| try { | ||
| bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpFile), Constants.CHARSET)); | ||
| for (ListIterator<IgnoreRule> it = ignoreRules.listIterator(); it.hasNext(); ) { | ||
| String s = it.next().getPattern(false); | ||
| bw.write(s, 0, s.length()); | ||
| bw.newLine(); | ||
| } | ||
| } finally { | ||
| if (bw != null) { | ||
| try { | ||
| bw.close(); | ||
| } catch (IOException ex) { } | ||
| } | ||
| if (!tmpFile.renameTo(gitIgnore)) { | ||
| // cannot rename directly, try backup and delete te original .gitignore | ||
| File tmpCopy = generateTempFile(Constants.DOT_GIT_IGNORE, gitIgnore.getParentFile()); //NOI18N | ||
| boolean success = false; | ||
| if (gitIgnore.renameTo(tmpCopy)) { | ||
| // and try to rename again | ||
| success = tmpFile.renameTo(gitIgnore); | ||
| if (!success) { | ||
| // restore te original .gitignore file | ||
| tmpCopy.renameTo(gitIgnore); | ||
| Path tmpFile = Files.createTempFile(gitIgnore.getParentFile().toPath(), Constants.DOT_GIT_IGNORE, "tmp"); //NOI18N | ||
| try { | ||
| String lineSeparator = probeLineSeparator(gitIgnore.toPath()); | ||
| try (BufferedWriter writer = Files.newBufferedWriter(tmpFile)) { | ||
| for (IgnoreRule rule : ignoreRules) { | ||
| writer.write(rule.getPattern(false)); | ||
| writer.write(lineSeparator); | ||
| } | ||
| tmpCopy.delete(); | ||
| } | ||
| if (!success) { | ||
| tmpFile.delete(); | ||
| throw new IOException("Cannot write to " + gitIgnore.getAbsolutePath()); | ||
| } | ||
| Files.move(tmpFile, gitIgnore.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
| } finally { | ||
| Files.deleteIfExists(tmpFile); | ||
| } | ||
|
|
||
| } catch (IOException ex) { | ||
| throw new IOException("Cannot update .gitignore at " + gitIgnore.getAbsolutePath(), ex); | ||
| } | ||
| ignoreFiles.add(gitIgnore); | ||
| } | ||
|
|
||
| private List<IgnoreRule> parse (File gitIgnore) throws IOException { | ||
| List<IgnoreRule> rules = new LinkedList<IgnoreRule>(); | ||
| private List<IgnoreRule> parse(File gitIgnore) throws IOException { | ||
| if (gitIgnore.exists()) { | ||
| BufferedReader br = null; | ||
| try { | ||
| br = new BufferedReader(new InputStreamReader(new FileInputStream(gitIgnore), Constants.CHARSET)); | ||
| String txt; | ||
| while ((txt = br.readLine()) != null) { | ||
| rules.add(new IgnoreRule(txt)); | ||
| } | ||
| } finally { | ||
| if (br != null) { | ||
| try { | ||
| br.close(); | ||
| } catch (IOException ex) { } | ||
| try (Stream<String> lines = Files.lines(gitIgnore.toPath())) { | ||
| return lines.map(IgnoreRule::new) | ||
| .collect(Collectors.toCollection(LinkedList::new)); | ||
| } | ||
| } | ||
| return new LinkedList<>(); | ||
| } | ||
|
|
||
| @SuppressWarnings("NestedAssignment") | ||
| private static String probeLineSeparator(Path file) throws IOException { | ||
| if (Files.exists(file)) { | ||
| try (BufferedReader br = Files.newBufferedReader(file)) { | ||
| int current; | ||
| int last = -1; | ||
| while ((current = br.read()) != -1) { | ||
| if (current == '\n') { | ||
| return last == '\r' ? "\r\n" : "\n"; | ||
| } | ||
| last = current; | ||
| } | ||
| } | ||
| } | ||
| return rules; | ||
| return System.lineSeparator(); | ||
|
Comment on lines
+162
to
+176
Member
Author
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. @matthiasblaesing could you check if this works for you? |
||
| } | ||
|
|
||
| public File[] getModifiedIgnoreFiles () { | ||
| return ignoreFiles.toArray(new File[0]); | ||
| return ignoreFiles.toArray(File[]::new); | ||
| } | ||
|
|
||
| protected abstract MatchResult addStatement (List<IgnoreRule> ignoreRules, File gitIgnore, String path, boolean isDirectory, boolean forceWrite, boolean writable) throws IOException; | ||
|
|
@@ -214,14 +201,6 @@ protected final MatchResult checkGlobalExcludeFile (String path, boolean directo | |
| return MatchResult.NOT_IGNORED; | ||
| } | ||
|
|
||
| private File generateTempFile (String basename, File parent) { | ||
| File tempFile = new File(parent, basename); | ||
| while (tempFile.exists()) { | ||
| tempFile = new File(parent, basename + Long.toString(System.currentTimeMillis())); | ||
| } | ||
| return tempFile; | ||
| } | ||
|
|
||
| private File getGlobalExcludeFile () { | ||
| Repository repository = getRepository(); | ||
| String path = repository.getConfig().get(CoreConfig.KEY).getExcludesFile(); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this did set the capacity of the SB. The char was cast to int. The code did still work though since it didn't need that slash.