Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 8 Replies) PreBuild event could not run git command (Read 2545 times)
Cong Quyen Knight
Junior Member
**
Offline


Posts: 22
Joined: Nov 29th, 2021
PreBuild event could not run git command
Aug 2nd, 2023 at 9:30am
Print Post  
Hi Tim,

I'm working on a script to hardcode git commit hash before build project.
I use guideline "Arduino Build Events and Hooks", but it seem the script is run on temp folder as working directory, which does contain my .git folder, so git command return fail.

How could I run git command on my original project directory?
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2697
Joined: Feb 13th, 2019
Re: PreBuild event could not run git command
Reply #1 - Aug 2nd, 2023 at 9:35am
Print Post  
From your description it sounds like you need to set the Working Directory for the command to your project folder.

Documentation: https://www.visualmicro.com/page/Arduino-Build-Events-and-Hooks.aspx

If you still have issues please attach a text file containing the hooks you are trying to use so we can assist more easily.
  
Back to top
IP Logged
 
Cong Quyen Knight
Junior Member
**
Offline


Posts: 22
Joined: Nov 29th, 2021
Re: PreBuild event could not run git command
Reply #2 - Aug 2nd, 2023 at 9:58am
Print Post  
Hi Tim,

This is my board.txt
Code
Select All
recipe.hooks.prebuild.working_directory="D:\\agent_board"
recipe.hooks.prebuild.1.pattern=py {ProjectDir}git_commit_hash.py
 



How can I replace "D:\\agent_board" by a global variable?

This is my git_commit_hash.py

Code
Select All
import subprocess
import re

# Step 1: Retrieve the latest Git commit hash
git_commit_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip().decode()

# Step 2: Read the header file and replace the old Git commit hash with the new one
header_file_path = 'git_commit_hash.h'
temp_file_path = 'git_commit_hash_temp.h'

with open(header_file_path, 'r') as infile, open(temp_file_path, 'w') as outfile:
    for line in infile:
        updated_line = re.sub(r'#define\s+COMMIT_ID\s+0x[a-fA-F0-9]+', f'#define COMMIT_ID 0x{git_commit_hash}', line)
        outfile.write(updated_line)

# Step 3: Replace the original header file with the updated one
import shutil
shutil.move(temp_file_path, header_file_path)
 



This is log when build:
Code
Select All
recipe.hooks.prebuild.1.pattern
py D:\agent_board\git_commit_hash.py
fatal*: not a git repository (or any of the parent directories): .git
   Traceback (most recent call last)
   agent_board\git_commit_hash.py", line 5, in <module>
   git_commit_hash = subprocess.check_output([git, rev-parse, --short, HEAD]).strip().decode()
Error running platform build action hooks.prebuild
 

  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2697
Joined: Feb 13th, 2019
Re: PreBuild event could not run git command
Reply #3 - Aug 2nd, 2023 at 10:52am
Print Post  
Thanks for the detail, do you have your script working when running directly from the command line?

When you say a global variable, would it be the {ProjectDir} which you have used in the pattern?

If not, you can enabled the vMicro > Compiler > Show Build Properties and when you build your project you will see all properties available to be merged into the command.
  
Back to top
IP Logged
 
Cong Quyen Knight
Junior Member
**
Offline


Posts: 22
Joined: Nov 29th, 2021
Re: PreBuild event could not run git command
Reply #4 - Aug 2nd, 2023 at 12:06pm
Print Post  
Hi Tim,

It works when I run directly from cmd

If I update the board.txt like below:
Code
Select All
# agent_board build property overrides
#

recipe.hooks.prebuild.working_directory={ProjectDir}
recipe.hooks.prebuild.1.pattern=py {ProjectDir}git_commit_hash.py
 



I got build log like attached.
  

Please Register or Login to the Forum to see File Attachments
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2697
Joined: Feb 13th, 2019
Re: PreBuild event could not run git command
Reply #5 - Aug 2nd, 2023 at 12:59pm
Print Post  
If you change the first line to specify it belongs to the first pattern it should work as expected:

Code
Select All
recipe.hooks.prebuild.1.working_directory="D:\\agent_board"
recipe.hooks.prebuild.1.pattern=py {ProjectDir}git_commit_hash.py 

  
Back to top
IP Logged
 
Cong Quyen Knight
Junior Member
**
Offline


Posts: 22
Joined: Nov 29th, 2021
Re: PreBuild event could not run git command
Reply #6 - Aug 2nd, 2023 at 1:43pm
Print Post  
Hi Tim,

I tried your solution with a cd command
Code
Select All
recipe.hooks.prebuild.working_directory="D:\\agent_board"
recipe.hooks.prebuild.1.pattern=cmd.exe /c cd 



And I got logs as attached.
It seems the command is run in TEMP folder
« Last Edit: Aug 2nd, 2023 at 1:46pm by Cong Quyen Knight »  

Please Register or Login to the Forum to see File Attachments
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2697
Joined: Feb 13th, 2019
Re: PreBuild event could not run git command
Reply #7 - Aug 2nd, 2023 at 2:52pm
Print Post  
You haven't changed the first entry in your board.txt as detailed in the previous post.

You need to change it to ensure the working directory is set correctly for the .1 hook specifically.
  
Back to top
IP Logged
 
Cong Quyen Knight
Junior Member
**
Offline


Posts: 22
Joined: Nov 29th, 2021
Re: PreBuild event could not run git command
Reply #8 - Aug 3rd, 2023 at 5:12am
Print Post  
Thank Tim,

It works. I thought you talk about '\' => '\\'
  
Back to top
 
IP Logged
 
Cong Quyen Knight
Junior Member
**
Offline


Posts: 22
Joined: Nov 29th, 2021
Re: PreBuild event could not run git command
Reply #9 - Aug 3rd, 2023 at 6:56am
Print Post  
Hi Tim,

I think you should update the guideline on https://www.visualmicro.com/page/Arduino-Build-Events-and-Hooks.aspx

It doesn't mention about Working Directory for each pattern.
  
Back to top
 
IP Logged
 
Simon@Visual Micro
Administrator
*****
Offline


Posts: 2697
Joined: Feb 13th, 2019
Re: PreBuild event could not run git command
Reply #10 - Aug 3rd, 2023 at 8:03am
Print Post  
We've amended the Working Directory on the example page so this is clearer.

All of the elements can be set per-hook (pattern, working directory, shell execute, hide new windows, error dialogue).
  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint