From 5ce8d14dc3f86d5cb0c728f5737acdfd57a47cc7 Mon Sep 17 00:00:00 2001
From: kpym <kpym.tzanev@gmail.com>
Date: Thu, 25 Jan 2024 21:17:43 +0100
Subject: [PATCH] skip gm executable when moving files

---
 HOWTO.md    |  2 +-
 README.md   |  2 +-
 gm_build.go | 23 +++++++++++++++++++++--
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/HOWTO.md b/HOWTO.md
index 0ed2dc9..5e3828e 100644
--- a/HOWTO.md
+++ b/HOWTO.md
@@ -64,7 +64,7 @@ Here is an example of possible `.gitlab-ci.yml`:
 pages:
   image: alpine
   script:
-    - wget -c https://github.com/kpym/gm/releases/download/v0.16.0/gm_0.16.0_Linux_64bit.tar.gz -O - | tar -xz gm
+    - wget -c https://github.com/kpym/gm/releases/download/v0.16.1/gm_0.16.1_Linux_64bit.tar.gz -O - | tar -xz gm
     - ./gm --pages '**/*'
   artifacts:
     paths:
diff --git a/README.md b/README.md
index 534216e..4fc689a 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,7 @@ This tool is a thin wrapper around the [github.com/yuin/goldmark](https://github
 
 ```
 > gm -h
-gm (version: 0.16.0): a goldmark cli tool which is a thin wrapper around github.com/yuin/goldmark (versio: v1.5.6).
+gm (version: 0.16.1): a goldmark cli tool which is a thin wrapper around github.com/yuin/goldmark (versio: v1.5.6).
 
   If not serving (no '--serve' or '-s' option is used):
   - the .md files are converted and saved as .html with the same base name;
diff --git a/gm_build.go b/gm_build.go
index d66b813..ab0a344 100644
--- a/gm_build.go
+++ b/gm_build.go
@@ -59,6 +59,21 @@ func buildMd(infile string) {
 	}
 }
 
+var thisExecutable string
+
+func setThisExecutable(cwd string) {
+	var err error
+	thisExecutable, err = filepath.Rel(cwd, filepath.Clean(os.Args[0]))
+	check(err, "Problem getting the current executable.")
+}
+
+// isGM returns true if the path is equal to the current executable
+// path is should be cleaned before calling this function
+func isGM(path string) bool {
+	return path == thisExecutable
+}
+
+// pathHasDot returns true if the path contains a folder or file name starting with a dot
 func pathHasDot(path string) bool {
 	wasSeparator := true
 	for i := 0; i < len(path); i++ {
@@ -72,11 +87,15 @@ func pathHasDot(path string) bool {
 
 // buildFiles convert all .md files verifying one of the patterns to .html
 func buildFiles() {
-	// get the current directory as a filesystem, needed for doublestar.Glob
+	// get the current directory as a filesystem
 	cwd, err := os.Getwd()
 	check(err, "Problem getting the current directory.")
+	// set fs needed for doublestar.Glob
 	dirFS := os.DirFS(cwd)
+	// check if we need to move files
 	movefiles := move && filepath.Clean(outdir) != filepath.Clean(cwd)
+	// set the current executable (to skip if necessary)
+	setThisExecutable(cwd)
 	// check all patterns
 	for _, pattern := range inpatterns {
 		info("Looking for '%s'.\n", pattern)
@@ -95,7 +114,7 @@ func buildFiles() {
 		}
 		for _, infile := range allfiles {
 			infile = filepath.Clean(infile)
-			if skipdot && pathHasDot(infile) {
+			if isGM(infile) || skipdot && pathHasDot(infile) {
 				info("  Skipping %s...\n", infile)
 				continue
 			}