1

We are using branching in our project extensively, and it seems when switching a branch, sometimes artifacts (objects) form the previous branch will remain. I think it is due to the matter that source files might have an older mtime in other branches, so the specific target doesn't get rebuild.

To avoid that I'd like the build system to force a clean, when an other branch is being checked out.

nop_0x00
  • 33
  • 7
  • Just clear build directory when you switch a branch. Or create new build directory for every new branch (this is the best approach in most situations). CMake has no notion about branches, when determine whether the file needs to be rebuilt CMake looks only to the file's timestamp. – Tsyvarev Dec 12 '20 at 23:24
  • I will check to have seperate build dirs, but since we are working with feature branches, which will often endure just a very short time I think it will be difficult. I can Imagaing a solution where I create a file as main dependency, which gets thouched when git branch --show-current changes. Either from CMake Side, or as a git checkout hook (if existing) – nop_0x00 Dec 13 '20 at 07:44
  • 1
    One way could be to create a custom command that depends on the `.git/HEAD` file and runs a cleanup. This file should change when you switch to a different branch or go into a detached head state. Make all your targets depend on the result of this command (probably just a copy of the HEAD file in the build dir on changed content) and the cleanup should be triggered automatically... – fabian Dec 13 '20 at 08:44

1 Answers1

0

What should remain from the previous branch after a git switch are private files that were generated during the last branch build.

Simple try and git clean -fdx to perform a full clean of the working tree after the switch.
The build will then starts from scratch.

You can automate this with a post-checkout hook (a local hook that each developer must chose to active themselves).

This hook example actually saves the current modifications before deleting them.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250