Skip to content

Commit 39758bc

Browse files
committed
Add support for ../
Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>
1 parent 3bd6a30 commit 39758bc

4 files changed

Lines changed: 30 additions & 11 deletions

File tree

args.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ type Args struct {
3030
// in case that it is not, we fall back to the current directory.
3131
func (a Args) Directory() string {
3232
if strings.HasPrefix(a.Last, "~") {
33-
return resolveHomeSymbol(filepath.Dir(a.Last))
33+
return getAbs(filepath.Dir(a.Last))
34+
}
35+
if strings.Contains(a.Last, "..") {
36+
return getAbs(filepath.Dir(a.Last))
3437
}
3538
if info, err := os.Stat(a.Last); err == nil && info.IsDir() {
3639
return fixPathForm(a.Last, a.Last)

match/file.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package match
22

3-
import "strings"
3+
import (
4+
"strings"
5+
)
46

57
// File returns true if prefix can match the file
68
func File(file, prefix string) bool {
@@ -14,6 +16,9 @@ func File(file, prefix string) bool {
1416
if prefix == "~" || strings.HasPrefix(file, "~") {
1517
return true
1618
}
19+
if prefix == ".." || strings.Contains(file, "..") {
20+
return true
21+
}
1722

1823
file = strings.TrimPrefix(file, "./")
1924
prefix = strings.TrimPrefix(prefix, "./")

predict_files.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ func files(pattern string, allowFiles bool) PredictFunc {
4040

4141
path := prediction[0]
4242
if strings.HasPrefix(a.Last, "~") {
43-
path = resolveHomeSymbol(path)
43+
path = getAbs(path)
44+
}
45+
if strings.Contains(a.Last, "..") {
46+
path = getAbs(path)
4447
}
4548
// only try deeper, if the one item is a directory
4649
stat, err := os.Stat(path)
@@ -75,7 +78,7 @@ func PredictFilesSet(files []string) PredictFunc {
7578
f = fixPathForm(a.Last, f)
7679

7780
// test matching of file to the argument
78-
if match.File(resolveHomeSymbol(f), resolveHomeSymbol(a.Last)) {
81+
if match.File(getAbs(f), getAbs(a.Last)) {
7982
prediction = append(prediction, f)
8083
}
8184
}
@@ -87,12 +90,8 @@ func listFiles(dir, pattern string, allowFiles bool) []string {
8790
// set of all file names
8891
m := map[string]bool{}
8992

90-
if strings.HasPrefix(pattern, "~") {
91-
pattern = resolveHomeSymbol(pattern)
92-
}
93-
9493
// list files
95-
if files, err := filepath.Glob(filepath.Join(dir, pattern)); err == nil {
94+
if files, err := filepath.Glob(filepath.Join(dir, getAbs(pattern))); err == nil {
9695
for _, f := range files {
9796
if stat, err := os.Stat(f); err != nil || stat.IsDir() || allowFiles {
9897
m[f] = true

utils.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ func fixPathForm(last string, file string) string {
3939
return file
4040
}
4141

42+
if strings.Contains(rel, "..") {
43+
return fixDirPath(rel)
44+
}
45+
4246
// fix ./ prefix of path
4347
if rel != "." && strings.HasPrefix(last, ".") {
4448
rel = "./" + rel
@@ -47,6 +51,14 @@ func fixPathForm(last string, file string) string {
4751
return fixDirPath(rel)
4852
}
4953

54+
func getAbs(path string) string {
55+
if strings.HasPrefix(path, "~") {
56+
return resolveHomeSymbol(path)
57+
}
58+
path, _ = filepath.Abs(path)
59+
return path
60+
}
61+
5062
func resolveHomeSymbol(path string) string {
5163
usr, err := user.Current()
5264
if err != nil {
@@ -58,8 +70,8 @@ func resolveHomeSymbol(path string) string {
5870

5971
func fixDirPath(path string) string {
6072
info, err := os.Stat(path)
61-
if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") {
62-
path += "/"
73+
if err == nil && info.IsDir() && !strings.HasSuffix(path, filepath.FromSlash("/")) {
74+
path += filepath.FromSlash("/")
6375
}
6476
return path
6577
}

0 commit comments

Comments
 (0)