diff --git a/ide/libs.git/nbproject/project.properties b/ide/libs.git/nbproject/project.properties index e911cc8944c2..8637af8ef988 100644 --- a/ide/libs.git/nbproject/project.properties +++ b/ide/libs.git/nbproject/project.properties @@ -17,7 +17,8 @@ is.autoload=true -javac.source=1.8 +javac.source=11 +javac.target=11 javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml diff --git a/ide/libs.git/src/org/netbeans/libs/git/GitBranch.java b/ide/libs.git/src/org/netbeans/libs/git/GitBranch.java index bba4e2025589..8008f69c100f 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/GitBranch.java +++ b/ide/libs.git/src/org/netbeans/libs/git/GitBranch.java @@ -91,4 +91,16 @@ public GitBranch getTrackedBranch () { void setTrackedBranch (GitBranch trackedBranch) { this.trackedBranch = trackedBranch; } + + @Override + public String toString() { + return "GitBranch{" + + "name=" + name + + ", id=" + getId() + + ", remote=" + remote + + ", active=" + active + + ", trackedBranch=" + trackedBranch + + '}'; + } + } diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/IgnoreRule.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/IgnoreRule.java index 42b7a79b725f..c780604d677a 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/IgnoreRule.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/IgnoreRule.java @@ -29,10 +29,10 @@ public class IgnoreRule extends org.eclipse.jgit.ignore.FastIgnoreRule { private final String noNegationPattern; public IgnoreRule (String pattern) { - super(pattern.trim()); + super(pattern.strip()); this.pattern = pattern; - pattern = pattern.trim(); - this.noNegationPattern = pattern.startsWith("!") ? pattern.substring(1) : null; + String neg = pattern.strip(); + this.noNegationPattern = neg.startsWith("!") ? neg.substring(1) : null; } public String getPattern (boolean preprocess) { @@ -50,7 +50,7 @@ public String getPattern (boolean preprocess) { @Override public boolean isMatch(String target, boolean isDirectory) { - String trimmed = pattern.trim(); + String trimmed = pattern.strip(); if (trimmed.isEmpty() || trimmed.startsWith("#")) { // this is a comment or an empty line return false; diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/Utils.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/Utils.java index 7340848f0713..9a37e9c752c9 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/Utils.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/Utils.java @@ -442,7 +442,7 @@ public static GitBranch getTrackedBranch (Config config, String branchName, Map< } } - public static Map getAllBranches (Repository repository, GitClassFactory fac, ProgressMonitor monitor) throws GitException { + public static Map getAllBranches(Repository repository, GitClassFactory fac, ProgressMonitor monitor) throws GitException { ListBranchCommand cmd = new ListBranchCommand(repository, fac, true, monitor); cmd.execute(); return cmd.getBranches(); diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/AddCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/AddCommand.java index fdb10e5b0e6a..57bfe6e2b99d 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/AddCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/AddCommand.java @@ -110,7 +110,7 @@ protected void run() throws GitException { if (f != null) { // the file exists File file = new File(repository.getWorkTree().getAbsolutePath() + File.separator + path); DirCacheEntry entry = new DirCacheEntry(path); - entry.setLastModified(f.getEntryLastModified()); + entry.setLastModified(f.getEntryLastModifiedInstant()); int fm = f.getEntryFileMode().getBits(); long sz = f.getEntryLength(); Path p = null; @@ -129,7 +129,7 @@ protected void run() throws GitException { entry.setLength(0); BasicFileAttributes attrs = Files.readAttributes(p, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); if (attrs != null) { - entry.setLastModified(attrs.lastModifiedTime().toMillis()); + entry.setLastModified(attrs.lastModifiedTime().toInstant()); } entry.setObjectId(inserter.insert(Constants.OBJ_BLOB, Constants.encode(link.toString()))); } else if ((f.getEntryFileMode().getBits() & FileMode.TYPE_TREE) == FileMode.TYPE_TREE) { @@ -153,7 +153,7 @@ protected void run() throws GitException { } } ObjectId oldId = treeWalk.getObjectId(0); - if (ObjectId.equals(oldId, ObjectId.zeroId()) || !ObjectId.equals(oldId, entry.getObjectId())) { + if (ObjectId.isEqual(oldId, ObjectId.zeroId()) || !ObjectId.isEqual(oldId, entry.getObjectId())) { listener.notifyFile(file, path); } builder.add(entry); diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/CheckoutRevisionCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/CheckoutRevisionCommand.java index 487dad80e4cd..2229e43f9142 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/CheckoutRevisionCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/CheckoutRevisionCommand.java @@ -71,6 +71,8 @@ import org.netbeans.libs.git.progress.FileListener; import org.netbeans.libs.git.progress.ProgressMonitor; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * * @author ondra @@ -325,7 +327,7 @@ private void resolveEntries (MergeAlgorithm merger, String path, DirCacheEntry[] DirCacheEntry e = new DirCacheEntry(path); e.setCreationTime(theirs.getCreationTime()); e.setFileMode(theirs.getFileMode()); - e.setLastModified(theirs.getLastModified()); + e.setLastModified(theirs.getLastModifiedInstant()); e.setLength(theirs.getLength()); e.setObjectId(theirs.getObjectId()); builder.add(e); @@ -360,8 +362,7 @@ private void checkoutFile (MergeResult merge, String path) throws IOExc try (OutputStream fos = opt.getAutoCRLF() != CoreConfig.AutoCRLF.FALSE ? new AutoCRLFOutputStream(new FileOutputStream(file)) : new FileOutputStream(file)) { - format.formatMerge(fos, merge, Arrays.asList(new String[] { "BASE", "OURS", "THEIRS" }), //NOI18N - Constants.CHARACTER_ENCODING); + format.formatMerge(fos, merge, Arrays.asList(new String[] { "BASE", "OURS", "THEIRS" }), UTF_8); //NOI18N } } } diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ConflictCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ConflictCommand.java index 37e97b803719..8f7b9d53db9d 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ConflictCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ConflictCommand.java @@ -46,13 +46,11 @@ public class ConflictCommand extends StatusCommand { private final ProgressMonitor monitor; - private final StatusListener listener; private final File[] roots; public ConflictCommand (Repository repository, GitClassFactory gitFactory, File[] roots, ProgressMonitor monitor, StatusListener listener) { super(repository, Constants.HEAD, roots, gitFactory, monitor, listener); this.monitor = monitor; - this.listener = listener; this.roots = roots; } @@ -93,7 +91,7 @@ protected void run () throws GitException { DirCacheIterator indexIterator = treeWalk.getTree(0, DirCacheIterator.class); DirCacheEntry indexEntry = indexIterator != null ? indexIterator.getDirCacheEntry() : null; int stage = indexEntry == null ? 0 : indexEntry.getStage(); - long indexTS = indexEntry == null ? -1 : indexEntry.getLastModified(); + long indexTS = indexEntry == null ? -1 : indexEntry.getLastModifiedInstant().toEpochMilli(); if (stage != 0) { GitStatus status = getClassFactory().createStatus(true, path, workTreePath, file, diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/DeleteTagCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/DeleteTagCommand.java index 25285c2968fd..21c34db56727 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/DeleteTagCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/DeleteTagCommand.java @@ -19,6 +19,7 @@ package org.netbeans.libs.git.jgit.commands; import java.io.IOException; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate.Result; @@ -35,23 +36,21 @@ */ public class DeleteTagCommand extends GitCommand { private final String tagName; - private GitRefUpdateResult result; - public DeleteTagCommand (Repository repository, GitClassFactory gitFactory, String tagName, ProgressMonitor monitor) { + public DeleteTagCommand(Repository repository, GitClassFactory gitFactory, String tagName, ProgressMonitor monitor) { super(repository, gitFactory, monitor); this.tagName = tagName; } @Override - protected void run () throws GitException { + protected void run() throws GitException { Repository repository = getRepository(); - Ref currentRef = repository.getTags().get(tagName); - if (currentRef == null) { - throw new GitException.MissingObjectException(tagName, GitObjectType.TAG); - } - String fullName = currentRef.getName(); try { - RefUpdate update = repository.updateRef(fullName); + Ref currentRef = repository.exactRef(Constants.R_TAGS + tagName); + if (currentRef == null) { + throw new GitException.MissingObjectException(tagName, GitObjectType.TAG); + } + RefUpdate update = repository.updateRef(currentRef.getName()); update.setRefLogMessage("tag deleted", false); update.setForceUpdate(true); Result deleteResult = update.delete(); @@ -69,7 +68,7 @@ protected void run () throws GitException { } @Override - protected String getCommandDescription () { + protected String getCommandDescription() { return "git tag -d " + tagName; } } diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java index 5d5d56dfba06..c2a11d742fe3 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/IgnoreUnignoreCommand.java @@ -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(); + 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 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 ignoreRules) throws IOException { try { - bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tmpFile), Constants.CHARSET)); - for (ListIterator 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 parse (File gitIgnore) throws IOException { - List rules = new LinkedList(); + private List 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 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(); } public File[] getModifiedIgnoreFiles () { - return ignoreFiles.toArray(new File[0]); + return ignoreFiles.toArray(File[]::new); } protected abstract MatchResult addStatement (List 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(); diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListBranchCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListBranchCommand.java index 6311a583149b..374bdc5db8e9 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListBranchCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListBranchCommand.java @@ -19,13 +19,16 @@ package org.netbeans.libs.git.jgit.commands; +import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.RefComparator; +import org.eclipse.jgit.lib.RefDatabase; import org.eclipse.jgit.lib.Repository; import org.netbeans.libs.git.GitBranch; import org.netbeans.libs.git.GitException; @@ -41,61 +44,57 @@ public class ListBranchCommand extends GitCommand { private final boolean all; private Map branches; - public ListBranchCommand (Repository repository, GitClassFactory gitFactory, boolean all, ProgressMonitor monitor) { + public ListBranchCommand(Repository repository, GitClassFactory gitFactory, boolean all, ProgressMonitor monitor) { super(repository, gitFactory, monitor); this.all = all; } @Override - protected void run () throws GitException { + protected void run() throws GitException { Repository repository = getRepository(); - Map refs; + branches = new LinkedHashMap<>(); try { - refs = repository.getAllRefs(); - } catch (IllegalArgumentException ex) { - throw new GitException("Corrupted repository metadata at " + repository.getWorkTree().getAbsolutePath(), ex); //NOI18N - } - Ref head = refs.get(Constants.HEAD); - branches = new LinkedHashMap(); - Config cfg = repository.getConfig(); - if (head != null) { - String current = head.getLeaf().getName(); - if (current.equals(Constants.HEAD)) { - String name = GitBranch.NO_BRANCH; - branches.put(name, getClassFactory().createBranch(name, false, true, head.getLeaf().getObjectId())); + RefDatabase db = repository.getRefDatabase(); + Ref head = db.exactRef(Constants.HEAD); + if (head != null) { + String current = head.getLeaf().getName(); + if (current.equals(Constants.HEAD)) { + String name = GitBranch.NO_BRANCH; + branches.put(name, getClassFactory().createBranch(name, false, true, head.getLeaf().getObjectId())); + } + branches.putAll(getRefs(db.getRefsByPrefix(Constants.R_HEADS), false, current)); } - branches.putAll(getRefs(refs.values(), Constants.R_HEADS, false, current, cfg)); - } - Map allBranches = getRefs(refs.values(), Constants.R_REMOTES, true, null, cfg); - allBranches.putAll(branches); - setupTracking(branches, allBranches, repository.getConfig()); - if (all) { - branches.putAll(allBranches); + Map allBranches = getRefs(db.getRefsByPrefix(Constants.R_REMOTES), true, null); + allBranches.putAll(branches); + setupTracking(branches, allBranches, repository.getConfig()); + if (all) { + branches.putAll(allBranches); + } + } catch (IOException | IllegalArgumentException ex) { + throw new GitException("Corrupted repository metadata at " + repository.getWorkTree().getAbsolutePath(), ex); //NOI18N } } - private Map getRefs (Collection allRefs, String prefix, boolean isRemote, String activeBranch, Config config) { - Map branches = new LinkedHashMap(); - for (final Ref ref : RefComparator.sort(allRefs)) { + private Map getRefs(Collection allRefs, boolean isRemote, String activeBranch) { + Map branchMap = new LinkedHashMap<>(); + for (Ref ref : RefComparator.sort(allRefs)) { String refName = ref.getLeaf().getName(); - if (refName.startsWith(prefix)) { - String name = refName.substring(refName.indexOf('/', 5) + 1); - branches.put(name, getClassFactory().createBranch(name, isRemote, refName.equals(activeBranch), ref.getLeaf().getObjectId())); - } + String name = refName.substring(refName.indexOf('/', 5) + 1); + branchMap.put(name, getClassFactory().createBranch(name, isRemote, refName.equals(activeBranch), ref.getLeaf().getObjectId())); } - return branches; + return branchMap; } @Override - protected String getCommandDescription () { + protected String getCommandDescription() { return "git branch"; //NOI18N } - public Map getBranches () { + public Map getBranches() { return branches; } - private void setupTracking (Map branches, Map allBranches, Config cfg) { + private void setupTracking(Map branches, Map allBranches, Config cfg) { for (GitBranch b : branches.values()) { getClassFactory().setBranchTracking(b, Utils.getTrackedBranch(cfg, b.getName(), allBranches)); } diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListTagCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListTagCommand.java index c0ee3046da0a..94d781acf461 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListTagCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ListTagCommand.java @@ -21,9 +21,11 @@ import java.io.IOException; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevWalk; @@ -47,18 +49,19 @@ public ListTagCommand (Repository repository, GitClassFactory gitFactory, boolea } @Override - protected void run () throws GitException { + protected void run() throws GitException { Repository repository = getRepository(); - Map tags = repository.getTags(); - allTags = new LinkedHashMap<>(tags.size()); - try (RevWalk walk = new RevWalk(repository);) { - for (Map.Entry e : tags.entrySet()) { + allTags = new LinkedHashMap<>(); + try (RevWalk walk = new RevWalk(repository)) { + for (Ref ref : repository.getRefDatabase().getRefsByPrefix(Constants.R_TAGS)) { GitTag tag; try { - tag = getClassFactory().createTag(walk.parseTag(e.getValue().getLeaf().getObjectId())); + tag = getClassFactory().createTag(walk.parseTag(ref.getLeaf().getObjectId())); } catch (IncorrectObjectTypeException ex) { - tag = getClassFactory().createTag(e.getKey(), - getClassFactory().createRevisionInfo(walk.parseCommit(e.getValue().getLeaf().getObjectId()), repository)); + tag = getClassFactory().createTag( + ref.getName().substring(Constants.R_TAGS.length()), + getClassFactory().createRevisionInfo(walk.parseCommit(ref.getLeaf().getObjectId()), repository) + ); } if (all || tag.getTaggedObjectType() == GitObjectType.COMMIT) { allTags.put(tag.getTagName(), tag); @@ -72,11 +75,11 @@ protected void run () throws GitException { } @Override - protected String getCommandDescription () { + protected String getCommandDescription() { return "git tag -l"; //NOI18N } - public Map getTags () { + public Map getTags() { return allTags; } diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/LogCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/LogCommand.java index f3432c04e21d..8b4ea50d3c41 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/LogCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/LogCommand.java @@ -165,7 +165,7 @@ protected void run () throws GitException { fullWalk.parseCommit(commit), branches, repository) ); if (commit.getParentCount() == 0) { - Ref replace = repository.getAllRefs().get("refs/replace/" + commit.getId().getName()); + Ref replace = repository.exactRef("refs/replace/" + commit.getId().getName()); if (replace != null) { final RevCommit newCommit = Utils.findCommit(repository, replace.getTarget().getName()); if (newCommit != null) { diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ResetCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ResetCommand.java index 94fa527eeeee..7eafc879a102 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ResetCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/ResetCommand.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.text.MessageFormat; +import java.time.Instant; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -32,7 +33,6 @@ import org.eclipse.jgit.dircache.DirCacheBuildIterator; import org.eclipse.jgit.dircache.DirCacheBuilder; import org.eclipse.jgit.dircache.DirCacheEntry; -import org.eclipse.jgit.errors.CorruptObjectException; import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; @@ -128,7 +128,7 @@ protected void run() throws GitException { treeWalk.reset(); treeWalk.addTree(new DirCacheBuildIterator(builder)); treeWalk.addTree(commit.getTree()); - List toDelete = new LinkedList(); + List toDelete = new LinkedList<>(); String lastAddedPath = null; while (treeWalk.next() && !monitor.isCanceled()) { File path = new File(repository.getWorkTree(), treeWalk.getPathString()); @@ -159,7 +159,7 @@ protected void run() throws GitException { DirCacheEntry e = new DirCacheEntry(treeWalk.getPathString()); AbstractTreeIterator it = treeWalk.getTree(1, AbstractTreeIterator.class); e.setFileMode(it.getEntryFileMode()); - e.setLastModified(System.currentTimeMillis()); + e.setLastModified(Instant.now()); e.setObjectId(it.getEntryObjectId()); e.smudgeRacilyClean(); builder.add(e); @@ -216,17 +216,13 @@ protected void run() throws GitException { } } } - } catch (NoWorkTreeException ex) { - throw new GitException(ex); - } catch (CorruptObjectException ex) { - throw new GitException(ex); - } catch (IOException ex) { + } catch (NoWorkTreeException | IOException ex) { throw new GitException(ex); } } private void deleteFile (File file, File[] roots) { - Set rootFiles = new HashSet(Arrays.asList(roots)); + Set rootFiles = new HashSet<>(Arrays.asList(roots)); File[] children; while (file != null && !rootFiles.contains(file) && ((children = file.listFiles()) == null || children.length == 0)) { // file is an empty folder diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/RevertCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/RevertCommand.java index c10072bc8afe..39ea0e0d089a 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/RevertCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/RevertCommand.java @@ -104,7 +104,7 @@ protected void run() throws GitException { ? "Revert \"" + revertedCommit.getShortMessage() + "\"" + "\n\n" + "This reverts commit " + revertedCommit.getId().getName() + "." //NOI18N : message; if (merger.merge(headCommit, srcParent)) { - if (AnyObjectId.equals(headCommit.getTree().getId(), merger.getResultTreeId())) { + if (AnyObjectId.isEqual(headCommit.getTree().getId(), merger.getResultTreeId())) { result = NO_CHANGE_INSTANCE; } else { DirCacheCheckout dco = new DirCacheCheckout(repository, headCommit.getTree(), dc = repository.lockDirCache(), merger.getResultTreeId()); @@ -123,7 +123,7 @@ protected void run() throws GitException { merger.getMergeResults() == null ? null : getFiles(repository.getWorkTree(), merger.getMergeResults().keySet()), getFiles(repository.getWorkTree(), merger.getFailingPaths().keySet())); } else { - String mergeMessageWithConflicts = new MergeMessageFormatter().formatWithConflicts(commitMessage, merger.getUnmergedPaths()); + String mergeMessageWithConflicts = new MergeMessageFormatter().formatWithConflicts(commitMessage, merger.getUnmergedPaths(), '#'); repository.writeMergeCommitMsg(mergeMessageWithConflicts); result = getClassFactory().createRevertResult(GitRevertResult.Status.CONFLICTING, null, merger.getMergeResults() == null ? null : getFiles(repository.getWorkTree(), merger.getMergeResults().keySet()), diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/StatusCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/StatusCommand.java index 39d7a6e31cc4..fd59d4b717d5 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/StatusCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/StatusCommand.java @@ -246,7 +246,7 @@ protected void run () throws GitException { } int stage = indexEntry == null ? 0 : indexEntry.getStage(); - long indexTimestamp = indexEntry == null ? -1 : indexEntry.getLastModified(); + long indexTimestamp = indexEntry == null ? -1 : indexEntry.getLastModifiedInstant().toEpochMilli(); GitStatus status = getClassFactory().createStatus(tracked, path, workTreePath, file, statusHeadIndex, statusIndexWC, statusHeadWC, diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/UpdateRefCommand.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/UpdateRefCommand.java index a5c67b47e98c..688fb647d994 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/UpdateRefCommand.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/commands/UpdateRefCommand.java @@ -46,7 +46,7 @@ public UpdateRefCommand (Repository repository, GitClassFactory gitFactory, Stri } @Override - protected void run () throws GitException { + protected void run() throws GitException { Repository repository = getRepository(); try { @@ -68,7 +68,7 @@ protected void run () throws GitException { } RefUpdate u = repository.updateRef(ref.getName()); - newRef = repository.peel(newRef); + newRef = repository.getRefDatabase().peel(newRef); ObjectId srcObjectId = newRef.getPeeledObjectId(); if (srcObjectId == null) { srcObjectId = newRef.getObjectId(); @@ -85,11 +85,11 @@ protected void run () throws GitException { } @Override - protected String getCommandDescription () { - return new StringBuilder("git update-ref ").append(refName).append(" ").append(revision).toString(); //NOI18N + protected String getCommandDescription() { + return "git update-ref "+ refName + " " + revision; //NOI18N } - public GitRefUpdateResult getResult () { + public GitRefUpdateResult getResult() { return result; } diff --git a/ide/libs.git/src/org/netbeans/libs/git/jgit/utils/CheckoutIndex.java b/ide/libs.git/src/org/netbeans/libs/git/jgit/utils/CheckoutIndex.java index 471b5bd55054..786e627f6e7b 100644 --- a/ide/libs.git/src/org/netbeans/libs/git/jgit/utils/CheckoutIndex.java +++ b/ide/libs.git/src/org/netbeans/libs/git/jgit/utils/CheckoutIndex.java @@ -23,9 +23,8 @@ import java.io.IOException; import java.text.MessageFormat; import java.util.Collection; -import java.util.logging.Logger; +import org.eclipse.jgit.dircache.Checkout; import org.eclipse.jgit.dircache.DirCache; -import org.eclipse.jgit.dircache.DirCacheCheckout; import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.lib.FileMode; @@ -52,7 +51,6 @@ public class CheckoutIndex { private final ProgressMonitor monitor; private final boolean checkContent; private final boolean recursively; - private static final Logger LOG = Logger.getLogger(CheckoutIndex.class.getName()); public CheckoutIndex (Repository repository, DirCache cache, File[] roots, boolean recursively, FileListener listener, ProgressMonitor monitor, boolean checkContent) { this.repository = repository; @@ -118,7 +116,9 @@ public void checkoutEntry (Repository repository, File file, DirCacheEntry e, Ob } file.createNewFile(); if (file.isFile()) { - DirCacheCheckout.checkoutEntry(repository, e, od); + new Checkout(repository) + .setRecursiveDeletion(false) + .checkout(e, null, od, null); } else { monitor.notifyError(MessageFormat.format(Utils.getBundle(CheckoutIndex.class).getString("MSG_Warning_CannotCreateFile"), file.getAbsolutePath())); //NOI18N } @@ -152,7 +152,4 @@ private boolean directChild (File[] roots, File workTree, File path) { return false; } - private static boolean isWindows () { - return System.getProperty("os.name", "").toLowerCase().contains("windows"); //NOI18N - } } diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java index 2fff96b043a4..3f149bb81046 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/AbstractGitTestCase.java @@ -219,8 +219,8 @@ protected static void assertDirCacheEntry (Repository repository, File workDir, DirCacheEntry e = cache.getEntry(relativePath); assertNotNull(e); assertEquals(relativePath, e.getPathString()); - if (f.lastModified() != e.getLastModified()) { - assertEquals((f.lastModified() / 1000) * 1000, (e.getLastModified() / 1000) * 1000); + if (f.lastModified() != e.getLastModifiedInstant().toEpochMilli()) { + assertEquals((f.lastModified() / 1000) * 1000, (e.getLastModifiedInstant().toEpochMilli() / 1000) * 1000); } try (InputStream in = new FileInputStream(f)) { assertEquals(e.getObjectId(), repository.newObjectInserter().idFor(Constants.OBJ_BLOB, f.length(), in)); diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/AddTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/AddTest.java index 6b3e32d4eb0d..06507be53611 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/AddTest.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/AddTest.java @@ -525,7 +525,7 @@ public void testAddSymlink () throws Exception { DirCacheEntry e = repository.readDirCache().getEntry(link.getName()); assertEquals(FileMode.SYMLINK, e.getFileMode()); ObjectId id = e.getObjectId(); - assertTrue((ts - 1000) < e.getLastModified() && (ts + 1000) > e.getLastModified()); + assertTrue((ts - 1000) < e.getLastModifiedInstant().toEpochMilli() && (ts + 1000) > e.getLastModifiedInstant().toEpochMilli()); try(ObjectReader reader = repository.getObjectDatabase().newReader()) { assertTrue(reader.has(e.getObjectId())); byte[] bytes = reader.open(e.getObjectId()).getBytes(); @@ -541,7 +541,7 @@ public void testAddSymlink () throws Exception { assertEquals(FileMode.SYMLINK, e2.getFileMode()); assertEquals(id, e2.getObjectId()); assertEquals(0, e2.getLength()); - assertTrue((ts - 1000) < e2.getLastModified() && (ts + 1000) > e2.getLastModified()); + assertTrue((ts - 1000) < e.getLastModifiedInstant().toEpochMilli() && (ts + 1000) > e.getLastModifiedInstant().toEpochMilli()); assertTrue(reader.has(e2.getObjectId())); bytes = reader.open(e2.getObjectId()).getBytes(); assertEquals(path, RawParseUtils.decode(bytes)); diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CheckoutTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CheckoutTest.java index 307e43fe54f3..34889db08a38 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CheckoutTest.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CheckoutTest.java @@ -34,6 +34,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.dircache.Checkout; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheCheckout; import org.eclipse.jgit.dircache.DirCacheEntry; @@ -357,7 +358,9 @@ public void testLargeFile () throws Exception { WindowCacheConfig cfg = new WindowCacheConfig(); cfg.setStreamFileThreshold((int) large.length() - 1); cfg.install(); - DirCacheCheckout.checkoutEntry(repository, e, repository.newObjectReader()); + new Checkout(repository) + .setRecursiveDeletion(false) + .checkout(e, null, repository.newObjectReader(), null); } public void testCheckoutBranch () throws Exception { diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java index 856325dcbf99..8b7a37b7c1e7 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/CommitTest.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; +import java.time.Instant; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -492,7 +493,7 @@ public void testNeverCommitConflicts () throws Exception { DirCacheBuilder builder = cache.builder(); DirCacheEntry e = new DirCacheEntry(f.getName(), 1); e.setLength(0); - e.setLastModified(0); + e.setLastModified(Instant.ofEpochMilli(0)); e.setFileMode(FileMode.REGULAR_FILE); builder.add(e); builder.finish(); diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/FetchTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/FetchTest.java index 83d3c0192729..7bfb6f259ed7 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/FetchTest.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/FetchTest.java @@ -234,8 +234,8 @@ public void testFetchTags () throws Exception { RevTag tag = new RevWalk(repo).parseTag(ref.getObjectId()); Map updates = client.fetch("origin", NULL_PROGRESS_MONITOR); - Map tags = repository.getTags(); - assertEquals(tag.getId(), tags.get(tag.getTagName()).getTarget().getObjectId()); + Ref tagRef = repository.getRefDatabase().findRef(tag.getTagName()); + assertEquals(tag.getId(), tagRef.getTarget().getObjectId()); assertEquals(1, updates.size()); assertUpdate(updates.get(tag.getTagName()), tag.getTagName(), tag.getTagName(), tag.getId().getName(), null, new URIish(otherWT.toURI().toURL()).toString(), Type.TAG, GitRefUpdateResult.NEW); } diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java index 28234bd24032..af5e752f6eb8 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/IgnoreTest.java @@ -21,6 +21,8 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.Map; import org.eclipse.jgit.lib.ConfigConstants; @@ -598,4 +600,26 @@ public void testGitIgnoreEndsWithNewLine () throws Exception { assertTrue(gitIgnore.exists()); assertTrue("The .gitignore file should ends with an empty new line.",containsCRorLF(gitIgnore)); } + + public void testUpdateRetainsFileSpecificLineSeparators1() throws Exception { + checkLineSeparatorRetention("\r\n"); + } + + public void testUpdateRetainsFileSpecificLineSeparators2() throws Exception { + checkLineSeparatorRetention("\n"); + } + + private void checkLineSeparatorRetention(String lineSeparator) throws Exception { + Path gitignore = workDir.toPath().resolve(Constants.DOT_GIT_IGNORE); + Path toIgnore = workDir.toPath().resolve("file"); + String firstLine = "#comment" + lineSeparator; + String secondLine = "/file" + lineSeparator; + Files.writeString(gitignore, firstLine); + Files.writeString(toIgnore, "ignore"); + getClient(workDir).ignore(new File[] { toIgnore.toFile() }, NULL_PROGRESS_MONITOR); + + String postUpdate = Files.readString(gitignore); + assertEquals(firstLine, postUpdate.substring(0, firstLine.length())); + assertEquals(secondLine, postUpdate.substring(firstLine.length(), firstLine.length() + secondLine.length())); + } } diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/LogTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/LogTest.java index 7b60e52eb0bf..0c398cdd778d 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/LogTest.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/LogTest.java @@ -22,7 +22,6 @@ import java.io.File; import java.io.IOException; import java.util.Date; -import java.util.Iterator; import java.util.Map; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.PersonIdent; @@ -764,6 +763,11 @@ public void testLogWithBranchInfoMoreBranches () throws Exception { client.checkoutRevision("newbranch", true, NULL_PROGRESS_MONITOR); write(f, "modification on branch"); + // git commit timestamp resolution is one second. + // commits with the same time stamp don't seem to influence log order unless branches + // change between commits. In that case the branch name is also affecting the order. + // (renaming "newbranch" to "aaa" would fix this too but obfuscate the problem) + Thread.sleep(1100); add(files); commit(files); diff --git a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/ResetTest.java b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/ResetTest.java index 499a456f005c..d04ab2b5f319 100644 --- a/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/ResetTest.java +++ b/ide/libs.git/test/unit/src/org/netbeans/libs/git/jgit/commands/ResetTest.java @@ -487,7 +487,7 @@ public void testResetConflict () throws Exception { DirCacheEntry e1 = new DirCacheEntry(file.getName(), 1); e1.setCreationTime(e.getCreationTime()); e1.setFileMode(e.getFileMode()); - e1.setLastModified(e.getLastModified()); + e1.setLastModified(e.getLastModifiedInstant()); e1.setLength(e.getLength()); e1.setObjectId(e.getObjectId()); builder.add(e1);