Skip to content

Commit 04adc9c

Browse files
author
Roberto Sora
committedNov 21, 2018
findBaseDir code replaced with find common path function
1 parent 44ec0cc commit 04adc9c

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed
 

‎tools/download.go

+50-18
Original file line numberDiff line numberDiff line change
@@ -319,29 +319,61 @@ func stringInSlice(str string, list []string) bool {
319319
return false
320320
}
321321

322-
func findBaseDir(dirList []string) string {
323-
if len(dirList) == 1 {
324-
return filepath.Dir(dirList[0]) + "/"
325-
}
326-
baseDir := ""
327-
// https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
328-
dontdiff := []string{"pax_global_header"}
329-
for index, _ := range dirList {
330-
if stringInSlice(dirList[index], dontdiff) {
331-
continue
322+
func CommonPrefix(sep byte, paths []string) string {
323+
// Handle special cases.
324+
switch len(paths) {
325+
case 0:
326+
return ""
327+
case 1:
328+
return path.Clean(paths[0])
329+
}
330+
331+
c := []byte(path.Clean(paths[0]))
332+
333+
// We add a trailing sep to handle: common prefix directory is included in the path list
334+
// (e.g. /home/user1, /home/user1/foo, /home/user1/bar).
335+
// path.Clean will have cleaned off trailing / separators with
336+
// the exception of the root directory, "/" making it "//"
337+
// but this will get fixed up to "/" below).
338+
c = append(c, sep)
339+
340+
// Ignore the first path since it's already in c
341+
for _, v := range paths[1:] {
342+
// Clean up each path before testing it
343+
v = path.Clean(v) + string(sep)
344+
345+
// Find the first non-common byte and truncate c
346+
if len(v) < len(c) {
347+
c = c[:len(v)]
332348
}
333-
candidateBaseDir := dirList[index]
334-
for i := index; i < len(dirList); i++ {
335-
if !strings.Contains(dirList[i], candidateBaseDir) {
336-
return baseDir
349+
for i := 0; i < len(c); i++ {
350+
if v[i] != c[i] {
351+
c = c[:i]
352+
break
337353
}
338354
}
339-
// avoid setting the candidate if it is the last file
340-
if dirList[len(dirList)-1] != candidateBaseDir {
341-
baseDir = candidateBaseDir
355+
}
356+
357+
// Remove trailing non-separator characters and the final separator
358+
for i := len(c) - 1; i >= 0; i-- {
359+
if c[i] == sep {
360+
c = c[:i]
361+
break
342362
}
343363
}
344-
return baseDir
364+
365+
return string(c)
366+
}
367+
368+
func findBaseDir(dirList []string) string {
369+
if len(dirList) == 1 {
370+
return filepath.Dir(dirList[0]) + "/"
371+
}
372+
commonBaseDir := CommonPrefix(os.PathSeparator, dirList)
373+
if commonBaseDir != "" {
374+
commonBaseDir = commonBaseDir + "/"
375+
}
376+
return commonBaseDir
345377
}
346378

347379
func extractZip(body []byte, location string) (string, error) {

‎tools/download_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ func Test_findBaseDir(t *testing.T) {
1313
{[]string{"bin/bossac"}, "bin/"},
1414
{[]string{"bin/", "bin/bossac"}, "bin/"},
1515
{[]string{"bin/", "bin/bossac", "example"}, ""},
16-
{[]string{"avrdude/bin/avrdude", "avrdude/etc/avrdude.conf"}, "avrdude"},
16+
{[]string{"avrdude/bin/avrdude", "avrdude/etc/avrdude.conf"}, "avrdude/"},
1717
}
1818
for _, tt := range cases {
1919
t.Run(fmt.Sprintln(tt.dirList), func(t *testing.T) {
2020
if got := findBaseDir(tt.dirList); got != tt.want {
21-
t.Errorf("findBaseDir() = %v, want %v", got, tt.want)
21+
t.Errorf("findBaseDir() = got %v, want %v", got, tt.want)
2222
}
2323
})
2424
}

0 commit comments

Comments
 (0)
Please sign in to comment.