Skip to content

Commit

Permalink
CreateMiesVersion: Also try git from PATH
Browse files Browse the repository at this point in the history
Accompanying fix for 13e8467 (GetPossiblePathsToGit: Fix paths on
windows, 2024-09-27) as we now fallback to use git from PATH if none of
the candidate locations is usable.
  • Loading branch information
t-b committed Oct 14, 2024
1 parent 44af1aa commit ec2d9a0
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions Packages/MIES/MIES_GlobalStringAndVariableAccess.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ static Function/S CreateMiesVersion()
string path, topDir, version, gitPath
string gitDir, fullVersionPath
variable refNum
variable ret = 1

// set path to the toplevel directory in the mies folder structure
path = ParseFilePath(1, FunctionPath(""), ":", 1, 2)
Expand All @@ -213,9 +214,14 @@ static Function/S CreateMiesVersion()
continue
endif

ExecuteGitForMIESVersion(gitPath, gitDir, topDir, fullVersionPath)
ret = ExecuteGitForMIESVersion(gitPath, gitDir, topDir, fullVersionPath)
break
endfor

if(ret)
// none of the candidates worked, fallback to use git from PATH
ExecuteGitForMIESVersion("git", gitDir, topDir, fullVersionPath)
endif
endif

open/R/Z refNum as fullVersionPath
Expand Down Expand Up @@ -248,19 +254,21 @@ End

/// @brief Run some git commands to generate version.txt from the MIES repository
///
/// @param gitPath full path to a git binary on the system
/// @param gitPath full path to a git binary on the system or plain git for using the one in PATH
/// @param gitDir full path to .git in the MIES repository
/// @param topDir full path to the toplevel folder of the MIES repository
static Function ExecuteGitForMIESVersion(string gitPath, string gitDir, string topDir, string fullVersionPath)
///
/// @return zero on success, aborts on failure
static Function ExecuteGitForMIESVersion(string gitPathOrName, string gitDir, string topDir, string fullVersionPath)

string cmd, userName

gitPath = HFSPathToNative(gitPath)
gitDir = HFSPathToNative(gitDir)
topDir = HFSPathToNative(topDir)
gitPathOrName = HFSPathToNative(gitPathOrName)
gitDir = HFSPathToNative(gitDir)
topDir = HFSPathToNative(topDir)

// git is installed, try to regenerate version.txt
DEBUGPRINT("Found git at: ", str = gitPath)
DEBUGPRINT("Found git at: ", str = gitPathOrName)

// delete the old version.txt so that we can be sure to get the correct one afterwards
DeleteFile/Z fullVersionPath
Expand All @@ -269,7 +277,7 @@ static Function ExecuteGitForMIESVersion(string gitPath, string gitDir, string t
#if defined(WINDOWS)
// explanation:
// cmd /C "<full path to git.exe> --git-dir=<mies repository .git> describe <options> redirect everything into <mies respository>/version.txt"
sprintf cmd, "cmd.exe /C \"\"%s\" --git-dir=\"%s\" describe --always --tags --match \"Release_*\" > \"%sversion.txt\" 2>&1\"", gitPath, gitDir, topDir
sprintf cmd, "cmd.exe /C \"\"%s\" --git-dir=\"%s\" describe --always --tags --match \"Release_*\" > \"%sversion.txt\" 2>&1\"", gitPathOrName, gitDir, topDir
DEBUGPRINT("Cmd to execute: ", str = cmd)
ExecuteScriptText/B/Z cmd
ASSERT(!V_flag, "We have git installed but could not regenerate version.txt")
Expand All @@ -279,7 +287,7 @@ static Function ExecuteGitForMIESVersion(string gitPath, string gitDir, string t
ExecuteScriptText/B/Z cmd
ASSERT(!V_flag, "We have git installed but could not regenerate version.txt")

sprintf cmd, "cmd.exe /C \"\"%s\" --git-dir=\"%s\" log -1 --pretty=format:%%cI%%n >> \"%sversion.txt\" 2>&1\"", gitPath, gitDir, topDir
sprintf cmd, "cmd.exe /C \"\"%s\" --git-dir=\"%s\" log -1 --pretty=format:%%cI%%n >> \"%sversion.txt\" 2>&1\"", gitPathOrName, gitDir, topDir
DEBUGPRINT("Cmd to execute: ", str = cmd)
ExecuteScriptText/B/Z cmd
ASSERT(!V_flag, "We have git installed but could not regenerate version.txt")
Expand All @@ -292,12 +300,12 @@ static Function ExecuteGitForMIESVersion(string gitPath, string gitDir, string t
// git submodule status can not be used here as submodule is currently a sh script and executing that with --git-dir does not work
// but we can use the helper command which outputs a slightly uglier version, but is much faster
// the submodule helper is shipped with git 2.7 and later, therefore its failed execution is not fatal
sprintf cmd, "cmd.exe /C \"\"%s\" --git-dir=\"%s\" submodule--helper status >> \"%sversion.txt\" 2>&1\"", gitPath, gitDir, topDir
sprintf cmd, "cmd.exe /C \"\"%s\" --git-dir=\"%s\" submodule--helper status >> \"%sversion.txt\" 2>&1\"", gitPathOrName, gitDir, topDir
DEBUGPRINT("Cmd to execute: ", str = cmd)
ExecuteScriptText/B/Z cmd
#elif defined(MACINTOSH)

sprintf cmd, "do shell script \"%s --version\"", gitPath
sprintf cmd, "do shell script \"%s --version\"", gitPathOrName
DEBUGPRINT("Cmd to execute: ", str = cmd)
ExecuteScriptText/UNQ/Z cmd
if(V_flag)
Expand All @@ -306,7 +314,7 @@ static Function ExecuteGitForMIESVersion(string gitPath, string gitDir, string t
break
endif

sprintf cmd, "do shell script \"%s --git-dir='%s' describe --always --tags --match 'Release_*' > '%sversion.txt' 2>&1\"", gitPath, gitDir, topDir
sprintf cmd, "do shell script \"%s --git-dir='%s' describe --always --tags --match 'Release_*' > '%sversion.txt' 2>&1\"", gitPathOrName, gitDir, topDir
DEBUGPRINT("Cmd to execute: ", str = cmd)
ExecuteScriptText/UNQ/Z cmd
ASSERT(!V_flag, "We have git installed but could not regenerate version.txt")
Expand All @@ -316,7 +324,7 @@ static Function ExecuteGitForMIESVersion(string gitPath, string gitDir, string t
ExecuteScriptText/UNQ/Z cmd
ASSERT(!V_flag, "We have git installed but could not regenerate version.txt")

sprintf cmd, "do shell script \"%s --git-dir='%s' log -1 --pretty=format:%%cI%%n >> '%sversion.txt' 2>&1\"", gitPath, gitDir, topDir
sprintf cmd, "do shell script \"%s --git-dir='%s' log -1 --pretty=format:%%cI%%n >> '%sversion.txt' 2>&1\"", gitPathOrName, gitDir, topDir
DEBUGPRINT("Cmd to execute: ", str = cmd)
ExecuteScriptText/UNQ/Z cmd
ASSERT(!V_flag, "We have git installed but could not regenerate version.txt")
Expand All @@ -327,12 +335,14 @@ static Function ExecuteGitForMIESVersion(string gitPath, string gitDir, string t
ASSERT(!V_flag, "We have git installed but could not regenerate version.txt")

// see comment in WINDOWS branch
sprintf cmd, "do shell script \"%s --git-dir='%s' submodule--helper status >> '%sversion.txt' 2>&1\"", gitPath, gitDir, topDir
sprintf cmd, "do shell script \"%s --git-dir='%s' submodule--helper status >> '%sversion.txt' 2>&1\"", gitPathOrName, gitDir, topDir
DEBUGPRINT("Cmd to execute: ", str = cmd)
ExecuteScriptText/UNQ/Z cmd
#else
ASSERT(0, "Unsupported OS")
#endif

return 0
End

/// @brief Returns the absolute path to the variable `runMode`
Expand Down

0 comments on commit ec2d9a0

Please sign in to comment.