sourcegit-scm / sourcegit

Windows/macOS/Linux GUI client for GIT users
MIT License
1.12k stars 109 forks source link

SourceGit ignores git-hooks #530

Closed SebastianSchumann closed 1 hour ago

SebastianSchumann commented 2 hours ago

We use hooks, for example, to ensure branch names and to comply with some code guidelines. Unfortunately, SourceGit does not consider these.

It looks as if the --no-verify option is active by default. But this cannot be configured.

The hooks should be considered or an option should be added to control the behavior.

love-linger commented 2 hours ago

Did you talking about Create Branch? In fact, SourceGit has not used the --no-verify parameter.

SebastianSchumann commented 2 hours ago

No. We're using git-hooks to prevent users from committing directly to main. Everyone has to create a branch and create a pull-request.

But via SourceGit you can simply commit to main without getting an error message.

If the same operation is done with git commit or SourceTree, I get the desired error message.

love-linger commented 2 hours ago

SourceGit doesn't care about any git hooks, which is something the git process needs to care about. In addition, the implementation of git commit in the project is very simple:

namespace SourceGit.Commands
{
    public class Commit : Command
    {
        public Commit(string repo, string message, bool amend, bool allowEmpty = false)
        {
            var file = Path.GetTempFileName();
            File.WriteAllText(file, message);

            WorkingDirectory = repo;
            Context = repo;
            TraitErrorAsOutput = true;
            Args = $"commit --file=\"{file}\"";
            if (amend)
                Args += " --amend --no-edit";
            if (allowEmpty)
                Args += " --allow-empty";
        }
    }
}
SebastianSchumann commented 2 hours ago

Our pre-commit-hock (<repo>/.githooks/pre-commit) is:

#!/usr/bin/env bash
LC_ALL=C

local_branch="$(git rev-parse --abbrev-ref HEAD)"
user_name=$(echo $USERNAME | tr '[:upper:]' '[:lower:]')

valid_branch_regex="^(dev/${user_name}/\#[0-9]{4,}.*|${user_name}-patch-[0-9]+|project/.+)$"

message="There is something wrong with your branch name. Branch names in this project must adhere to this contract: $valid_branch_regex. Your commit will be rejected. You should rename your branch to a valid name and try again."

if [[ ! $local_branch =~ $valid_branch_regex ]]
then
    echo "$message"
    exit 1
fi

This script should enfoce a branch name of def/<username>/#<ticket>... or project/.... Even if this file exists you're able to commit directly to main without any error message.

love-linger commented 2 hours ago

image

I can NOT reproduce this issue. You said that you hooks file exists in <repo>/.githooks/pre-commit. Can you change it to git standard path <repo>/.git/hooks/pre-commit

love-linger commented 2 hours ago

Or just check if git config core.hooksPath points to your <repo>/.githooks?

SebastianSchumann commented 2 hours ago

Yes - copying the hook to .get/hooks/pre-commit works as expected. git config core.hooksPath points to <repo>/.githooks - that's why git commit and SourceTree also works.

love-linger commented 2 hours ago

That's strange, this project never changes the core.hooksPath……

love-linger commented 2 hours ago

I have moved the pre-commit to <repo>/.githooks and manually run git config core.hooksPath <repo>/.githooks. But it works fine on my PC: image

love-linger commented 2 hours ago

Yes - copying the hook to .get/hooks/pre-commit works as expected. git config core.hooksPath points to /.githooks - that's why git commit and SourceTree also works.

If it works fine with hooks under .git/hooks/, it means that the git process did not repect the value of core.hooksPath

SebastianSchumann commented 2 hours ago

Interesting - creating a new repo, setting the path to .githooks also works as expected.

love-linger commented 2 hours ago

😂

love-linger commented 1 hour ago

You can try these two commands under the repo that has these issue:

  1. git config --unset core.hooksPath reset the repo's local setting of core.hooksPath to default
  2. git config core.hooksPath <repo>/.githooks
SebastianSchumann commented 1 hour ago

Mea culpa.

Thank you very much - unset and re-configure did the job.