@@ -8,11 +8,13 @@ import (
8
8
"errors"
9
9
"fmt"
10
10
"io"
11
+ "io/fs"
11
12
"math/rand"
12
13
"net/http"
13
14
"net/url"
14
15
"os"
15
16
"path"
17
+ "path/filepath"
16
18
"regexp"
17
19
"strings"
18
20
"testing"
@@ -58,6 +60,100 @@ func doCreateRemoteAnnexRepository(t *testing.T, u *url.URL, ctx APITestContext,
58
60
return nil
59
61
}
60
62
63
+ func doGitAnnexPushToCreateTest (t * testing.T , u * url.URL , ctx APITestContext , repo string ) {
64
+ // create a local repository
65
+ repoPath , err := os .MkdirTemp (os .TempDir (), "git-annex-repo-*" )
66
+ require .NoError (t , err )
67
+ _ , _ , err = git .NewCommandContextNoGlobals (git .DefaultContext , "init" , "." ).RunStdString (& git.RunOpts {Dir : repoPath })
68
+ require .NoError (t , err )
69
+ err = doInitAnnexRepository (repoPath )
70
+ require .NoError (t , err )
71
+
72
+ // add a remote
73
+ repoURL := createSSHUrl (repo , u )
74
+ _ , _ , err = git .NewCommandContextNoGlobals (git .DefaultContext , "remote" , "add" , "origin" ).AddDynamicArguments (repoURL .String ()).RunStdString (& git.RunOpts {Dir : repoPath })
75
+ require .NoError (t , err )
76
+
77
+ // sync to gitea
78
+ withAnnexCtxKeyFile (t , ctx , func () {
79
+ _ , _ , err = git .NewCommandContextNoGlobals (git .DefaultContext , "annex" , "sync" , "--content" ).RunStdString (& git.RunOpts {Dir : repoPath })
80
+ require .NoError (t , err )
81
+ })
82
+
83
+ // check that the repo was created and synced as expected
84
+ // // repo exists on the gitea server
85
+ remoteRepoPath := path .Join (setting .RepoRootPath , repo + ".git" )
86
+ require .DirExists (t , remoteRepoPath )
87
+
88
+ // // default branch matches
89
+ localRepo , err := git .OpenRepository (git .DefaultContext , repoPath )
90
+ require .NoError (t , err )
91
+ expectedDefaultBranch , err := localRepo .GetDefaultBranch ()
92
+ require .NoError (t , err )
93
+
94
+ remoteRepo , err := git .OpenRepository (git .DefaultContext , remoteRepoPath )
95
+ require .NoError (t , err )
96
+ actualDefaultBranch , err := remoteRepo .GetDefaultBranch ()
97
+ require .NoError (t , err )
98
+
99
+ require .Equal (t , expectedDefaultBranch , actualDefaultBranch )
100
+
101
+ // // repo contains the same annexed files
102
+ err = filepath .WalkDir (repoPath , func (localPath string , d fs.DirEntry , err error ) error {
103
+ if err != nil {
104
+ return err
105
+ }
106
+
107
+ // skip anything below .git/
108
+ if d .IsDir () && strings .HasSuffix (localPath , "/.git" ) {
109
+ return filepath .SkipDir
110
+ }
111
+
112
+ if d .IsDir () {
113
+ return nil
114
+ }
115
+
116
+ // local (expected) content
117
+ f , err := os .Open (localPath )
118
+ require .NoError (t , err )
119
+ defer f .Close ()
120
+ expectedContent , err := io .ReadAll (f )
121
+ require .NoError (t , err )
122
+
123
+ // remote (actual) content
124
+ remotePath , err := contentLocation (remoteRepoPath , strings .TrimPrefix (localPath , repoPath + "/" ))
125
+ if err == annex .ErrInvalidPointer {
126
+ // skip non-annex files
127
+ return nil
128
+ }
129
+ require .FileExists (t , remotePath )
130
+ f , err = os .Open (remotePath )
131
+ require .NoError (t , err )
132
+ defer f .Close ()
133
+ actualContent , err := io .ReadAll (f )
134
+ require .NoError (t , err )
135
+
136
+ require .Equal (t , expectedContent , actualContent )
137
+
138
+ return nil
139
+ })
140
+ require .NoError (t , err )
141
+ }
142
+
143
+ func TestGitAnnexPushToCreate (t * testing.T ) {
144
+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
145
+ t .Run ("User" , func (t * testing.T ) {
146
+ ctx := NewAPITestContext (t , "user2" , "annex-push-to-create-test" , auth_model .AccessTokenScopeWriteRepository )
147
+ doGitAnnexPushToCreateTest (t , u , ctx , "user2/annex-push-to-create-test" )
148
+ })
149
+
150
+ t .Run ("Org" , func (t * testing.T ) {
151
+ ctx := NewAPITestContext (t , "user2" , "annex-push-to-create-test" , auth_model .AccessTokenScopeWriteRepository )
152
+ doGitAnnexPushToCreateTest (t , u , ctx , "org3/annex-push-to-create-test" )
153
+ })
154
+ })
155
+ }
156
+
61
157
func TestGitAnnexMedia (t * testing.T ) {
62
158
if ! setting .Annex .Enabled {
63
159
t .Skip ("Skipping since annex support is disabled." )
0 commit comments