Skip to content

Commit 15dbcee

Browse files
committed
Fixed normalized paths bug that prevented copying class in IDE. Issue 8497.
If both repo and project paths were symlinks or network drive paths, copying with refactoring caused exceptions to be thrown. Other actions may have resulted in the same. A normalized file path canNOT be passed into getIgnores because getIgnores creates and executes a StatusCommand. Executing the StatusCommand passes the repository and the source file. StatusCommand gets the repository path by calling repository.getWorkTree(), which returns a non-normalized path. If both the repository and the source file are referenced via a symlink or network drive, referencing file paths via normalizing and non-normaling causes file path mismatches. The normalized file path points to another location on the system, but repository.getWorkTree() will provide a non-normalized file path. Signed-off-by: Hunter Schoonover <[email protected]>
1 parent 5780a4c commit 15dbcee

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

ide/libs.git/src/org/netbeans/libs/git/jgit/commands/MoveTreeCommand.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,29 @@ protected void run() throws GitException {
7777
}
7878
sourceFile = tryNormalizeSymlink(sourceFile);
7979
targetFile = tryNormalizeSymlink(targetFile);
80+
8081
Repository repository = getRepository();
82+
83+
File repoWorkTreeNormalized = tryNormalizeSymlink(repository.getWorkTree());
84+
8185
try {
8286
DirCache cache = repository.lockDirCache();
8387
try {
84-
List<String> ignoredTargets = getIgnores(targetFile);
88+
89+
List<String> ignoredTargets = getIgnores(this.source);
90+
8591
boolean retried = false;
8692
DirCacheBuilder builder = cache.builder();
8793
TreeWalk treeWalk = new TreeWalk(repository);
88-
PathFilter sourceFilter = PathFilter.create(Utils.getRelativePath(repository.getWorkTree(), sourceFile));
89-
PathFilter targetFilter = PathFilter.create(Utils.getRelativePath(repository.getWorkTree(), targetFile));
94+
PathFilter sourceFilter = PathFilter.create(Utils.getRelativePath(repoWorkTreeNormalized, sourceFile));
95+
PathFilter targetFilter = PathFilter.create(Utils.getRelativePath(repoWorkTreeNormalized, targetFile));
9096
treeWalk.setFilter(PathFilterGroup.create(Arrays.asList(sourceFilter, targetFilter)));
9197
treeWalk.setRecursive(true);
9298
treeWalk.reset();
9399
treeWalk.addTree(new DirCacheBuildIterator(builder));
94100
while (treeWalk.next() && !monitor.isCanceled()) {
95101
String path = treeWalk.getPathString();
96-
File file = new File(repository.getWorkTree().getAbsolutePath() + File.separator + path);
102+
File file = new File(repoWorkTreeNormalized.getAbsolutePath() + File.separator + path);
97103
DirCacheEntry e = treeWalk.getTree(0, DirCacheBuildIterator.class).getDirCacheEntry();
98104
if (e != null) {
99105
if (targetFilter.include(treeWalk)) {
@@ -118,8 +124,8 @@ protected void run() throws GitException {
118124
// reset whole iterator and start from the beginning
119125
sourceFile = sourceFile.getCanonicalFile();
120126
targetFile = targetFile.getCanonicalFile();
121-
sourceFilter = PathFilter.create(Utils.getRelativePath(repository.getWorkTree(), sourceFile));
122-
targetFilter = PathFilter.create(Utils.getRelativePath(repository.getWorkTree(), targetFile));
127+
sourceFilter = PathFilter.create(Utils.getRelativePath(repoWorkTreeNormalized, sourceFile));
128+
targetFilter = PathFilter.create(Utils.getRelativePath(repoWorkTreeNormalized, targetFile));
123129
treeWalk.setFilter(PathFilterGroup.create(Arrays.asList(sourceFilter, targetFilter)));
124130
treeWalk.reset();
125131
builder = cache.builder();
@@ -185,7 +191,9 @@ private void rename () throws GitException {
185191

186192
private String getRelativePath (File file, File ancestor, File target) {
187193
String relativePathToAncestor = Utils.getRelativePath(ancestor, file);
188-
StringBuilder relativePathToSource = new StringBuilder(Utils.getRelativePath(getRepository().getWorkTree(), target));
194+
StringBuilder relativePathToSource = new StringBuilder(
195+
Utils.getRelativePath(tryNormalizeSymlink(getRepository().getWorkTree()), target)
196+
);
189197
if (!relativePathToAncestor.isEmpty()) {
190198
if (relativePathToSource.length() > 0) {
191199
relativePathToSource.append("/"); //NOI18N

0 commit comments

Comments
 (0)