@@ -26,12 +26,10 @@ import (
26
26
"time"
27
27
28
28
"code.gitea.io/gitea/modules/avatar"
29
- "code.gitea.io/gitea/modules/git"
30
29
"code.gitea.io/gitea/modules/log"
31
30
"code.gitea.io/gitea/modules/markup"
32
31
"code.gitea.io/gitea/modules/options"
33
32
"code.gitea.io/gitea/modules/setting"
34
- "code.gitea.io/gitea/modules/structs"
35
33
api "code.gitea.io/gitea/modules/structs"
36
34
"code.gitea.io/gitea/modules/timeutil"
37
35
"code.gitea.io/gitea/modules/util"
@@ -147,13 +145,13 @@ type Repository struct {
147
145
ID int64 `xorm:"pk autoincr"`
148
146
OwnerID int64 `xorm:"UNIQUE(s) index"`
149
147
OwnerName string
150
- Owner * User `xorm:"-"`
151
- LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
152
- Name string `xorm:"INDEX NOT NULL"`
153
- Description string `xorm:"TEXT"`
154
- Website string `xorm:"VARCHAR(2048)"`
155
- OriginalServiceType structs .GitServiceType `xorm:"index"`
156
- OriginalURL string `xorm:"VARCHAR(2048)"`
148
+ Owner * User `xorm:"-"`
149
+ LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
150
+ Name string `xorm:"INDEX NOT NULL"`
151
+ Description string `xorm:"TEXT"`
152
+ Website string `xorm:"VARCHAR(2048)"`
153
+ OriginalServiceType api .GitServiceType `xorm:"index"`
154
+ OriginalURL string `xorm:"VARCHAR(2048)"`
157
155
DefaultBranch string
158
156
159
157
NumWatches int
@@ -911,63 +909,12 @@ func CheckCreateRepository(doer, u *User, name string) error {
911
909
return nil
912
910
}
913
911
914
- // CreateDelegateHooks creates all the hooks scripts for the repo
915
- func CreateDelegateHooks (repoPath string ) error {
916
- return createDelegateHooks (repoPath )
917
- }
918
-
919
- // createDelegateHooks creates all the hooks scripts for the repo
920
- func createDelegateHooks (repoPath string ) (err error ) {
921
-
922
- var (
923
- hookNames = []string {"pre-receive" , "update" , "post-receive" }
924
- hookTpls = []string {
925
- fmt .Sprintf ("#!/usr/bin/env %s\n data=$(cat)\n exitcodes=\" \" \n hookname=$(basename $0)\n GIT_DIR=${GIT_DIR:-$(dirname $0)}\n \n for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\n test -x \" ${hook}\" || continue\n echo \" ${data}\" | \" ${hook}\" \n exitcodes=\" ${exitcodes} $?\" \n done\n \n for i in ${exitcodes}; do\n [ ${i} -eq 0 ] || exit ${i}\n done\n " , setting .ScriptType ),
926
- fmt .Sprintf ("#!/usr/bin/env %s\n exitcodes=\" \" \n hookname=$(basename $0)\n GIT_DIR=${GIT_DIR:-$(dirname $0)}\n \n for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\n test -x \" ${hook}\" || continue\n \" ${hook}\" $1 $2 $3\n exitcodes=\" ${exitcodes} $?\" \n done\n \n for i in ${exitcodes}; do\n [ ${i} -eq 0 ] || exit ${i}\n done\n " , setting .ScriptType ),
927
- fmt .Sprintf ("#!/usr/bin/env %s\n data=$(cat)\n exitcodes=\" \" \n hookname=$(basename $0)\n GIT_DIR=${GIT_DIR:-$(dirname $0)}\n \n for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\n test -x \" ${hook}\" || continue\n echo \" ${data}\" | \" ${hook}\" \n exitcodes=\" ${exitcodes} $?\" \n done\n \n for i in ${exitcodes}; do\n [ ${i} -eq 0 ] || exit ${i}\n done\n " , setting .ScriptType ),
928
- }
929
- giteaHookTpls = []string {
930
- fmt .Sprintf ("#!/usr/bin/env %s\n \" %s\" hook --config='%s' pre-receive\n " , setting .ScriptType , setting .AppPath , setting .CustomConf ),
931
- fmt .Sprintf ("#!/usr/bin/env %s\n \" %s\" hook --config='%s' update $1 $2 $3\n " , setting .ScriptType , setting .AppPath , setting .CustomConf ),
932
- fmt .Sprintf ("#!/usr/bin/env %s\n \" %s\" hook --config='%s' post-receive\n " , setting .ScriptType , setting .AppPath , setting .CustomConf ),
933
- }
934
- )
935
-
936
- hookDir := filepath .Join (repoPath , "hooks" )
937
-
938
- for i , hookName := range hookNames {
939
- oldHookPath := filepath .Join (hookDir , hookName )
940
- newHookPath := filepath .Join (hookDir , hookName + ".d" , "gitea" )
941
-
942
- if err := os .MkdirAll (filepath .Join (hookDir , hookName + ".d" ), os .ModePerm ); err != nil {
943
- return fmt .Errorf ("create hooks dir '%s': %v" , filepath .Join (hookDir , hookName + ".d" ), err )
944
- }
945
-
946
- // WARNING: This will override all old server-side hooks
947
- if err = os .Remove (oldHookPath ); err != nil && ! os .IsNotExist (err ) {
948
- return fmt .Errorf ("unable to pre-remove old hook file '%s' prior to rewriting: %v " , oldHookPath , err )
949
- }
950
- if err = ioutil .WriteFile (oldHookPath , []byte (hookTpls [i ]), 0777 ); err != nil {
951
- return fmt .Errorf ("write old hook file '%s': %v" , oldHookPath , err )
952
- }
953
-
954
- if err = os .Remove (newHookPath ); err != nil && ! os .IsNotExist (err ) {
955
- return fmt .Errorf ("unable to pre-remove new hook file '%s' prior to rewriting: %v" , newHookPath , err )
956
- }
957
- if err = ioutil .WriteFile (newHookPath , []byte (giteaHookTpls [i ]), 0777 ); err != nil {
958
- return fmt .Errorf ("write new hook file '%s': %v" , newHookPath , err )
959
- }
960
- }
961
-
962
- return nil
963
- }
964
-
965
912
// CreateRepoOptions contains the create repository options
966
913
type CreateRepoOptions struct {
967
914
Name string
968
915
Description string
969
916
OriginalURL string
970
- GitServiceType structs .GitServiceType
917
+ GitServiceType api .GitServiceType
971
918
Gitignores string
972
919
IssueLabels string
973
920
License string
@@ -1883,138 +1830,6 @@ func deleteOldRepositoryArchives(ctx context.Context, idx int, bean interface{})
1883
1830
return nil
1884
1831
}
1885
1832
1886
- func gatherMissingRepoRecords () ([]* Repository , error ) {
1887
- repos := make ([]* Repository , 0 , 10 )
1888
- if err := x .
1889
- Where ("id > 0" ).
1890
- Iterate (new (Repository ),
1891
- func (idx int , bean interface {}) error {
1892
- repo := bean .(* Repository )
1893
- if ! com .IsDir (repo .RepoPath ()) {
1894
- repos = append (repos , repo )
1895
- }
1896
- return nil
1897
- }); err != nil {
1898
- if err2 := CreateRepositoryNotice (fmt .Sprintf ("gatherMissingRepoRecords: %v" , err )); err2 != nil {
1899
- return nil , fmt .Errorf ("CreateRepositoryNotice: %v" , err )
1900
- }
1901
- }
1902
- return repos , nil
1903
- }
1904
-
1905
- // DeleteMissingRepositories deletes all repository records that lost Git files.
1906
- func DeleteMissingRepositories (doer * User ) error {
1907
- repos , err := gatherMissingRepoRecords ()
1908
- if err != nil {
1909
- return fmt .Errorf ("gatherMissingRepoRecords: %v" , err )
1910
- }
1911
-
1912
- if len (repos ) == 0 {
1913
- return nil
1914
- }
1915
-
1916
- for _ , repo := range repos {
1917
- log .Trace ("Deleting %d/%d..." , repo .OwnerID , repo .ID )
1918
- if err := DeleteRepository (doer , repo .OwnerID , repo .ID ); err != nil {
1919
- if err2 := CreateRepositoryNotice (fmt .Sprintf ("DeleteRepository [%d]: %v" , repo .ID , err )); err2 != nil {
1920
- return fmt .Errorf ("CreateRepositoryNotice: %v" , err )
1921
- }
1922
- }
1923
- }
1924
- return nil
1925
- }
1926
-
1927
- // ReinitMissingRepositories reinitializes all repository records that lost Git files.
1928
- func ReinitMissingRepositories () error {
1929
- repos , err := gatherMissingRepoRecords ()
1930
- if err != nil {
1931
- return fmt .Errorf ("gatherMissingRepoRecords: %v" , err )
1932
- }
1933
-
1934
- if len (repos ) == 0 {
1935
- return nil
1936
- }
1937
-
1938
- for _ , repo := range repos {
1939
- log .Trace ("Initializing %d/%d..." , repo .OwnerID , repo .ID )
1940
- if err := git .InitRepository (repo .RepoPath (), true ); err != nil {
1941
- if err2 := CreateRepositoryNotice (fmt .Sprintf ("InitRepository [%d]: %v" , repo .ID , err )); err2 != nil {
1942
- return fmt .Errorf ("CreateRepositoryNotice: %v" , err )
1943
- }
1944
- }
1945
- }
1946
- return nil
1947
- }
1948
-
1949
- // SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
1950
- // to make sure the binary and custom conf path are up-to-date.
1951
- func SyncRepositoryHooks () error {
1952
- return x .Cols ("owner_id" , "name" ).Where ("id > 0" ).Iterate (new (Repository ),
1953
- func (idx int , bean interface {}) error {
1954
- if err := createDelegateHooks (bean .(* Repository ).RepoPath ()); err != nil {
1955
- return fmt .Errorf ("SyncRepositoryHook: %v" , err )
1956
- }
1957
- if bean .(* Repository ).HasWiki () {
1958
- if err := createDelegateHooks (bean .(* Repository ).WikiPath ()); err != nil {
1959
- return fmt .Errorf ("SyncRepositoryHook: %v" , err )
1960
- }
1961
- }
1962
- return nil
1963
- })
1964
- }
1965
-
1966
- // GitFsck calls 'git fsck' to check repository health.
1967
- func GitFsck (ctx context.Context ) {
1968
- log .Trace ("Doing: GitFsck" )
1969
- if err := x .
1970
- Where ("id>0 AND is_fsck_enabled=?" , true ).BufferSize (setting .Database .IterateBufferSize ).
1971
- Iterate (new (Repository ),
1972
- func (idx int , bean interface {}) error {
1973
- select {
1974
- case <- ctx .Done ():
1975
- return fmt .Errorf ("Aborted due to shutdown" )
1976
- default :
1977
- }
1978
- repo := bean .(* Repository )
1979
- repoPath := repo .RepoPath ()
1980
- log .Trace ("Running health check on repository %s" , repoPath )
1981
- if err := git .Fsck (repoPath , setting .Cron .RepoHealthCheck .Timeout , setting .Cron .RepoHealthCheck .Args ... ); err != nil {
1982
- desc := fmt .Sprintf ("Failed to health check repository (%s): %v" , repoPath , err )
1983
- log .Warn (desc )
1984
- if err = CreateRepositoryNotice (desc ); err != nil {
1985
- log .Error ("CreateRepositoryNotice: %v" , err )
1986
- }
1987
- }
1988
- return nil
1989
- }); err != nil {
1990
- log .Error ("GitFsck: %v" , err )
1991
- }
1992
- log .Trace ("Finished: GitFsck" )
1993
- }
1994
-
1995
- // GitGcRepos calls 'git gc' to remove unnecessary files and optimize the local repository
1996
- func GitGcRepos () error {
1997
- args := append ([]string {"gc" }, setting .Git .GCArgs ... )
1998
- return x .
1999
- Where ("id > 0" ).BufferSize (setting .Database .IterateBufferSize ).
2000
- Iterate (new (Repository ),
2001
- func (idx int , bean interface {}) error {
2002
- repo := bean .(* Repository )
2003
- if err := repo .GetOwner (); err != nil {
2004
- return err
2005
- }
2006
- if stdout , err := git .NewCommand (args ... ).
2007
- SetDescription (fmt .Sprintf ("Repository Garbage Collection: %s" , repo .FullName ())).
2008
- RunInDirTimeout (
2009
- time .Duration (setting .Git .Timeout .GC )* time .Second ,
2010
- repo .RepoPath ()); err != nil {
2011
- log .Error ("Repository garbage collection failed for %v. Stdout: %s\n Error: %v" , repo , stdout , err )
2012
- return fmt .Errorf ("Repository garbage collection failed: Error: %v" , err )
2013
- }
2014
- return nil
2015
- })
2016
- }
2017
-
2018
1833
type repoChecker struct {
2019
1834
querySQL , correctSQL string
2020
1835
desc string
0 commit comments