-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Take back control of hooks #1006
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM
cmd/hook.go
Outdated
CmdHook = cli.Command{ | ||
Name: "hook", | ||
Usage: "Delegate commands to corresponding Git hooks", | ||
Description: "All sub-commands should only be called by Git", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"This should only be called by Git"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
models/repo.go
Outdated
return x.Where("id > 0").Iterate(new(Repository), | ||
func(idx int, bean interface{}) error { | ||
if err := createDelegateHooks(bean.(*Repository).RepoPath()); err != nil { | ||
return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe prefix the error with SyncRepositoryHook: %v
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
Agreed :) |
LGTM |
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { | ||
return nil | ||
} | ||
if err := setup("hooks/pre-receive.log"); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function name 'setup' is a bit of vague as to what it actually does. I guess it's about setting up a logger?
But I don't understand why it has anything to do with the database engine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setup is to initialize the command, this function is for serval commands which the different is the log place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's resulted from factoring out the common 'preparing' part of all the commands. Maybe the Before
callback is a suitable place for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before should be the same actions, but this time we have a parameter - log name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Before callback has the context
parameter which has the needed (I guess) information like the command name to construct the path of the log file, if I wasn't misunderstanding something
Actually, I am not sure if putting it in Before is better or not. But there is the possibility. 😉
args := c.Args() | ||
if len(args) != 3 { | ||
fail("Arguments received are not equal to three", "Arguments received are not equal to three") | ||
} else if len(args[0]) == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the two conditions are not mutually exclusive? Not sure if I understand it correctly, but the use of else if
seems suspicious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Here I just move codes from update.go to hook.go. I think it's no problem. Suppose the command hook update "" a b
. The refName is empty.
} | ||
|
||
func runHookUpdate(c *cli.Context) error { | ||
if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why the check is needed, with the nearby context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the SSH_ORIGINAL_COMMAND is not set, that's because it is called by shell on manually.
f132edf
to
39387ec
Compare
} | ||
} | ||
|
||
if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 0777
necessary? Least privilege is prefered in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It seems 0755 is enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If 0700 is enough then 0700 is better. I cannot think of a scenario where 'others' need the permissions to R/W it.
Edit: Okay, it would be much convenient when debugging 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. But I ls -l the repository's root dir, all the permission is 755. Don't know why.
Otherwise LGTM and good job 👍 |
will fix #343 |
cmd/hook.go
Outdated
|
||
"github.com/urfave/cli" | ||
|
||
"code.gitea.io/gitea/models" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coding style?
import (
// stdlib
"encoding/json"
"fmt"
// local packages
"code.gitea.io/gitea/models"
"code.gitea.io/sdk/gitea"
// external packages
"github.com/foo/bar"
"gopkg.io/baz.v1"
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
models/migrations/v19.go
Outdated
"github.com/Unknwon/com" | ||
"github.com/go-xorm/xorm" | ||
|
||
"code.gitea.io/gitea/modules/setting" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same above coding style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
build fails |
@lunny rebase base on master can fix build errors? |
Yes. let me try. |
f42099d
to
9a9f14d
Compare
LGTM |
let L-G-T-M work |
var ( | ||
hookNames = []string{"pre-receive", "update", "post-receive"} | ||
hookTpls = []string{ | ||
fmt.Sprintf("#!/usr/bin/env %s\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/pre-receive.d\"`; do\n sh \"$SHELL_FOLDER/pre-receive.d/$i\"\ndone", setting.ScriptType), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's late but still want to comment it. You don't need the quotes for the ls and you don't need to call ls. Just something like for I in folder/*; do done. Beside that I would use source instead of sh to execute the scripts, than you can also define env variables in scripts that can be used in another script later on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you are right since I'm not familiar with shell script. We also have runServe
could define env variables before these script to be executed. So it's no need here to put another one.
This PR depends on go-gitea/git#34. On this PR, we changed the hooks directory structure.
Now we put all the server-side hooks(pre-receive, update, post-receive) on
hooks/pre-receive.d/
,hooks/update.d/
andhooks/post-receive.d/
.Gitea's hook will be at
hooks/pre-receive.d/gitea
,hooks/update.d/gitea
andhooks/post-receive.d/gitea
.And the old user hooks will be at
hooks/pre-receive.d/pre-receive
andhooks/post-receive.d/post-receive
. Of course, we will support multiple hooks on the UI sometime, it's will be easy if this PR is merged.