I'm writing a basic postscript to retrieve some informations after the build of a an embedded project, but I'm facing issues with popen(), that won't change the working directory during code execution. The command string is the following, and works fine in prompt/terminal:
>cd C:/path_to_embedded_project_with_git/ && git rev-parse --short HEAD
But when I input the same string into my code and run it, I always get the head of the actual postscript project, not the targeted embedded project.
/**
* @brief Get GIT short identifier (first SHA digits)
*
* @param char* git_repository_path
* uint8_t* git_short_sha
* @retval int retval
*/
int i_Git_GetShortSHA( char* git_repository_path, uint8_t* git_short_sha ){
git_str_buffer[1024] = {0};
// b) Open the pipe to redirect the command output to a file
FILE* pipe = NULL;
if( (pipe = popen("cd C:/path_to_embedded_project_with_git/ && git rev-parse --short HEAD", "r")) == NULL ){
PRINTFFFLUSH("[Git][ERROR] Cannot open pipe!" );
return 0;
}
// c) Read till end of process:
while (!feof(pipe)) {
// use buffer to read and add to result
fgets(git_str_buffer, 1024, pipe);
}
// d) Close the pipe, the process has ended
pclose(pipe);
// e) Copy the retrieved GIT SHA, always at the start of the buffered string
memcpy(git_short_sha, git_str_buffer, GIT_SHORT_SHA_SIZE);
return 1;
}
Thanks a lot, Raggio
I've also tried to create a dedicated .bat that collects:
cd C:/path_to_embedded_project_with_git/
git rev-parse --short HEAD
It agains runs well in prompt, giving out the short SHA of the pointed project, but still returns the SHA of the PostScript Project, when executed via popen()