cancel
Showing results for 
Search instead for 
Did you mean: 

HOWTO: Saving versions of a project using git

Tobe
Senior III

I had an issue, that i could not reproduce, because i could not restore the exact state of the project. The CubeIDE has its own version control for each file, which is of no great use, since the project consists of many files. Because of that i developed my own version control principle:

Everytime a successful build happens, the files are committed to a certain branch of the repo. Then after an issue has been fixed, or i want to try out things / old states of a project, i use smartgit to handle those commits.

Here is how to do it:

1. Create a directory outside of the workspace and manually create backups (backups the git repo too)

2. Setup git in the workspace (learn about git before - its quite a big topic)

  • Exclude the ".metadata" directory (reinclude folders that should be version controlled - maven etc.)
  • Create another branch for ongoing backups from the IDE
  • Add .gitignore file (see attached one - remove ".txt" to use it)

3. Create script to commit changesto the specified branch (The parts with "*****" need to be edited)

 

import os
import time
import datetime
import subprocess



#IDE run command (build steps): 
#cmd.exe /k "***** WORKSPACE\...bat"
#cmd.exe /k "..\..\..\gitManager.py"
#../../../gitManager.py	ERROR 193 make


gitPath = "*****WORKSPACE"

def signalError():
	subprocess.run(f"\"*****/nircmd.exe\" setdefaultsounddevice *****", shell=True)
	subprocess.run(f"\"*****nircmd.exe\" beep 1600 800", shell=True)
	subprocess.run(f"\"*****/nircmd.exe\" setdefaultsounddevice \"B*****\"", shell=True)

def checkBranch():
	gitCmd = f"git -C \"{gitPath}\" branch"
	process = subprocess.run(gitCmd, shell=True, capture_output=True, text=True)
	if process.returncode == 0:
		branches = process.stdout.split('\n')
		#print(branches, flush=True)
		if not "* Laufende_Entwicklung" in branches:
			print("Error wrong branch!", flush=True)
			print(f"{process.stdout}", flush=True)
			signalError()
			return False;
		else:
			return True;
		#print(process.stdout, flush=True)
	else:
		print("Error while checking for active branch", flush=True)
		print(f"{process.stdout}", flush=True)
		signalError()
		return False;

def gitAddCommit():
	gitCmd = f"git -C \"{gitPath}\" add ."
	print(f"running: \"{gitCmd}\"", flush=True)
	process = subprocess.run(gitCmd, shell=True, capture_output=True, text=True)
	if process.returncode == 0:
		timeString = f"{datetime.datetime.now():%Y-%m-%d %H:%M}"
		gitCmd = f"git -C \"{gitPath}\" commit -m \"{timeString}\""
		print(f"running: \"{gitCmd}\"", flush=True)
		process = subprocess.run(gitCmd, shell=True, capture_output=True, text=True)
		if process.returncode == 0:
			print(f"Commit done: {timeString}", flush=True)
			return True
		else:
			print("Error on git commit", flush=True)
			print(f"{process.stdout}", flush=True)
			signalError()
			return False
	else:
		print("Error on git add", flush=True)
		print(f"{process.stdout}", flush=True)
		signalError()
		return False
		


print("Git manager running...", flush=True)

if checkBranch():
	gitAddCommit()
else:
	print("Error nothing committed", flush=True)
		
	#lastTime = thisTime
	

 

 

4. Edit properties to have the script execute after successful build (See picture attached)

  • Make sure "All configurations is selected"!
  • Adjust path of the script as needed
  • Install nircmd for acustic signaling or use another way (if another branch is checked out, a backup will fail!)

Note:

  • Special care has to be taken, wenn checking out old commits: A build would create a new commit in the branch that is intended for development, so the branch should be set to another! And also remember to do manual backups.

It may have one or more flaws, since it has just beeing developed. Feel free to contribute or givee feedback.

0 REPLIES 0