0

Few of my team members sometimes commit code without building project/solution which gives error to other team members when they get latest and build project.

Example:-

Error in code Error in Code

Still commit option is enable in Visual Studio Commit option enable

So I want to prevent this scenario by some way like developer should not be able to commit their code until project build successfully in visual studio.

I have already gone through different settings available for GIT in Visual Studio, but can't find any setting for this scenario.

I have also checked in DevOps for different available Repository Policies, but non of them is useful in this scenario:

Repository Policies

Any one have any idea to prevent this type of commit (with build error in project)?

3
  • 1
    Use (optional) pre commit hooks and have checks run on the code on any MR / PR before it is allowed to be merged.
    – luk2302
    Commented Dec 22, 2024 at 11:47
  • 1
    On a local workstation: you may look at pre-commit and pre-push hooks. I would rather recommend (as in PMF's answer) to use the CI on the hosting platform of your remote repository though.
    – LeGEC
    Commented Dec 22, 2024 at 12:59
  • 1
    As mentioned below, Continuous Integration is what you need. Quoting: "CI is a software development practice where each member of a team merges their changes into a codebase together with their colleagues changes at least daily. Each of these integrations is verified by an automated build (including test) to detect integration errors as quickly as possible." Commented Dec 22, 2024 at 13:16

3 Answers 3

5

That is what CI (continuous integration) is for. You set up a specific server that runs the build after every change and reports success or failure. Most of the time, you would set it up to run on pull requests, and allow that these only be merged on a successful build.

You didn't specify what tools you're using other than VS (which is not really relevant here, as you care on the state of the committed code, not how it was written). On Github for instance, you can get such automatic builds for free, they're called "Github Actions".

2

So I want to prevent this scenario by some way like developer should not be able to commit their code until project build successfully in visual studio.

Your end goal of only having pushed commits that build and test successfully is a good goal, however preventing developers from creating local, unpushed commits that does not build and test successfully is not a valid means to achieve that. In some scenarios creating "failing" commits is essential and required.

So forget about restricting (local) commits while developers are working on some feature and focus on having good commits when they push them. As already mentioned you can (and should) use CI to enforce this, however that should not be the only mechanism used because a) it has a way too long feedback round trip time and b) fixing any commits after they are already pushed requires forced pushing1.

Luckily there already exists a program that does exactly what you need, git test written by Michael Haggerty. This program lets you define one or more test commands (e.g. "dotnet test mysolution.sln"), and then you can run such a test against a range of commits. The result for such a test run is saved in git notes, so that running a test on a commit already tested just fetches the cached result and takes less than 1 second.

So this should be your primary focus, get all developers to use git test. Also add git test as a step to your CI pipeline.


1 Forced pushing has two aspects that has potential for problems, accidental discard of commits and that other developers need to "recover" from the changed remote branch.

1

Although git has pre-commit hooks, it is usually not recommended to force an entire solution build (including tests?) at every commit.

The same is true for an IDE like VS or Rider: it could be done, but with the chance of serious delays. People might start to postpone their commits because of this.

Regardless of how it's done, it might turn into one of the most hated features because it would be impossible to do a commit until everything builds. What if the main branch needs a very urgent fix NOW?

The next best thing that IS possible is to do it on a PR level: a PR can be blocked from merging if the CI-build failed for the PR branch, see e.g. How to block the merge of Current PR if the previous CI build is failed and it should pass the PR if the previous build is successful?

(and train, guide and coach your team in doing the right thing instead of messing around...)

1
  • You can take advantage of the pre-commit hook to avoid a commit from taking place but running a build there would be an overkill. Things like code format checks are more like it, normally.
    – eftshift0
    Commented Dec 22, 2024 at 13:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.