aboutsummaryrefslogtreecommitdiff
path: root/spdxconv.go
diff options
context:
space:
mode:
Diffstat (limited to 'spdxconv.go')
-rw-r--r--spdxconv.go33
1 files changed, 24 insertions, 9 deletions
diff --git a/spdxconv.go b/spdxconv.go
index 5f28e5a..7222eb4 100644
--- a/spdxconv.go
+++ b/spdxconv.go
@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "strings"
"git.sr.ht/~shulhan/pakakeh.go/lib/git"
"git.sr.ht/~shulhan/pakakeh.go/lib/ini"
@@ -16,6 +17,9 @@ import (
type SPDXConv struct {
scm sourceCodeManagement
+ // curDir contains the current working directory.
+ curDir string
+
// dir define the directory that will processed.
// `name` is single file inside `dir` to be processed.
// If `name` is empty means process all files inside `dir`.
@@ -56,7 +60,7 @@ func Apply(path string) (err error) {
return nil
}
-// New initialize new instance of SPDXConv
+// New initialize new instance of SPDXConv.
func New(path string) (conv *SPDXConv, err error) {
var logp = `New`
@@ -66,6 +70,14 @@ func New(path string) (conv *SPDXConv, err error) {
}
conv = &SPDXConv{}
+ conv.curDir, err = os.Getwd()
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+ if !strings.HasPrefix(path, conv.curDir) {
+ return nil, fmt.Errorf(`%s: %s must be under the current directory`, logp, path)
+ }
+
var fi os.FileInfo
fi, err = os.Stat(path)
if err != nil {
@@ -78,12 +90,12 @@ func New(path string) (conv *SPDXConv, err error) {
conv.dir, conv.name = filepath.Split(path)
}
- err = conv.loadConfig(conv.dir)
+ err = conv.loadConfig(conv.curDir)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
- err = conv.scanForSCM(conv.dir)
+ err = conv.scanForSCM(conv.dir, conv.curDir)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
@@ -115,32 +127,35 @@ func (conv *SPDXConv) loadConfig(dir string) (err error) {
return nil
}
-// scanForSCM scan for source-code management (SCM) from directory `dir` up
-// until the root.
+// scanForSCM scan for source-code management (SCM) from directory `dir` until
+// the current working directory.
// Currently, only support git.
-func (conv *SPDXConv) scanForSCM(dir string) (err error) {
+func (conv *SPDXConv) scanForSCM(dir, curDir string) (err error) {
var scmDir string
var fi os.FileInfo
- for dir != `/` {
+ for strings.HasPrefix(dir, curDir) {
scmDir = filepath.Join(dir, `.git`)
fi, err = os.Stat(scmDir)
if err != nil {
if os.IsNotExist(err) {
+ dir = filepath.Dir(dir)
continue
}
return err
}
if !fi.IsDir() {
+ dir = filepath.Dir(dir)
continue
}
- conv.scmDir = dir
conv.scm, err = git.New(dir)
if err != nil {
return err
}
+ conv.scmDir = dir
return nil
}
- conv.scm = &dummySCM{}
+
+ conv.scm = &noSCM{}
return nil
}