From 4a7e8357902c16986c4426a3ffe85a1972a1bac2 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Wed, 1 Jul 2020 22:58:21 +0200 Subject: [PATCH 1/2] Use the util method to get the base path --- cmd/autoupdate/main.go | 2 +- compress/css.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/autoupdate/main.go b/cmd/autoupdate/main.go index 10c04e61..5b6cc756 100644 --- a/cmd/autoupdate/main.go +++ b/cmd/autoupdate/main.go @@ -22,7 +22,7 @@ func init() { } var ( - basePath = util.GetEnv("BOT_BASE_PATH") + basePath = util.GetBotBasePath() packagesPath = path.Join(basePath, "packages", "packages") cdnjsPath = path.Join(basePath, "cdnjs") diff --git a/compress/css.go b/compress/css.go index 0a849a2a..20920c9c 100644 --- a/compress/css.go +++ b/compress/css.go @@ -16,7 +16,7 @@ var ( ".css": true, } - basePath = util.GetEnv("BOT_BASE_PATH") + basePath = util.GetBotBasePath() cleanCSS = path.Join(basePath, "tools", "node_modules/clean-css-cli/bin/cleancss") ) From 63bc6831190e39a8bce504b53938bd6caac03769 Mon Sep 17 00:00:00 2001 From: Kristian Klausen Date: Fri, 3 Jul 2020 17:16:03 +0200 Subject: [PATCH 2/2] Don't add versions ignored by git autoupdate would panic as `git add` exit with a non-zero exit status if the path is ignored. Fix #58 --- cmd/autoupdate/git.go | 5 +++++ cmd/autoupdate/main.go | 2 +- cmd/autoupdate/npm.go | 5 +++++ util/env.go | 7 ++++++- util/git.go | 21 +++++++++++++++++++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/cmd/autoupdate/git.go b/cmd/autoupdate/git.go index 0afb950e..dd6b13b8 100644 --- a/cmd/autoupdate/git.go +++ b/cmd/autoupdate/git.go @@ -115,6 +115,11 @@ func doUpdateGit(ctx context.Context, pckg *packages.Package, gitpath string, ve continue } + if util.IsPathIgnoredByGit(ctx, util.GetCDNJSPath(), pckgpath) { + util.Debugf(ctx, "%s is ignored by git; aborting\n", pckgpath) + continue + } + util.Check(os.MkdirAll(pckgpath, os.ModePerm)) if len(filesToCopy) > 0 { diff --git a/cmd/autoupdate/main.go b/cmd/autoupdate/main.go index 5b6cc756..7ed0c718 100644 --- a/cmd/autoupdate/main.go +++ b/cmd/autoupdate/main.go @@ -24,7 +24,7 @@ func init() { var ( basePath = util.GetBotBasePath() packagesPath = path.Join(basePath, "packages", "packages") - cdnjsPath = path.Join(basePath, "cdnjs") + cdnjsPath = util.GetCDNJSPath() // initialize standard debug logger logger = util.GetStandardLogger() diff --git a/cmd/autoupdate/npm.go b/cmd/autoupdate/npm.go index 4ef25282..545d4a7c 100644 --- a/cmd/autoupdate/npm.go +++ b/cmd/autoupdate/npm.go @@ -83,6 +83,11 @@ func doUpdateNpm(ctx context.Context, pckg *packages.Package, versions []npm.Ver continue } + if util.IsPathIgnoredByGit(ctx, util.GetCDNJSPath(), pckgpath) { + util.Debugf(ctx, "%s is ignored by git; aborting\n", pckgpath) + continue + } + util.Check(os.MkdirAll(pckgpath, os.ModePerm)) tarballDir := npm.DownloadTar(ctx, version.Tarball) diff --git a/util/env.go b/util/env.go index 5b2ddc03..a7101ad2 100644 --- a/util/env.go +++ b/util/env.go @@ -36,9 +36,14 @@ func GetBotBasePath() string { return GetEnv("BOT_BASE_PATH") } +// GetCDNJSPath gets the path to the cdnjs repo. +func GetCDNJSPath() string { + return path.Join(GetBotBasePath(), "cdnjs") +} + // GetCDNJSPackages gets the path to the cdnjs libraries. func GetCDNJSPackages() string { - return path.Join(GetBotBasePath(), "cdnjs", "ajax", "libs") + return path.Join(GetCDNJSPath(), "ajax", "libs") } // HasHTTPProxy returns true if the http proxy environment diff --git a/util/git.go b/util/git.go index 29907303..5665d772 100644 --- a/util/git.go +++ b/util/git.go @@ -3,6 +3,7 @@ package util import ( "context" "os/exec" + "strings" ) // UpdateGitRepo git pulls and rebases the repository. @@ -14,3 +15,23 @@ func UpdateGitRepo(ctx context.Context, gitpath string) { Debugf(ctx, "%s: run %s\n", gitpath, cmd) CheckCmd(cmd.CombinedOutput()) } + +// IsPathIgnoredByGit determines if a path is git ignored. +func IsPathIgnoredByGit(ctx context.Context, gitpath string, path string) bool { + // We don't know if "path" is a file or a directory, so let's try with and without / + return isPathIgnoredByGit(ctx, gitpath, path) || isPathIgnoredByGit(ctx, gitpath, path+"/") +} + +func isPathIgnoredByGit(ctx context.Context, gitpath string, path string) bool { + // We need a relative path, so let's remove "gitpath" + path = strings.TrimPrefix(path, gitpath) + // In case "path" is a absolute path, we need to remove "/" afterwards to get a relative path + path = strings.TrimPrefix(path, "/") + args := []string{"check-ignore", "--quiet", "--no-index", path} + + cmd := exec.Command("git", args...) + cmd.Dir = gitpath + Debugf(ctx, "%s: run %s\n", gitpath, cmd) + + return cmd.Run() == nil +}