1
1
package org .jenkinsci .plugins .gitserver ;
2
2
3
+ import jakarta .servlet .http .HttpServletRequest ;
3
4
import jenkins .model .Jenkins ;
4
- import org .acegisecurity .Authentication ;
5
5
import org .eclipse .jgit .api .AddCommand ;
6
6
import org .eclipse .jgit .api .CommitCommand ;
7
7
import org .eclipse .jgit .api .Git ;
11
11
import org .eclipse .jgit .lib .PersonIdent ;
12
12
import org .eclipse .jgit .lib .Repository ;
13
13
import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
14
- import org .eclipse .jgit .transport .PostReceiveHook ;
15
- import org .eclipse .jgit .transport .ReceiveCommand ;
16
14
import org .eclipse .jgit .transport .ReceivePack ;
17
15
import org .eclipse .jgit .transport .UploadPack ;
18
16
import org .eclipse .jgit .transport .resolver .ServiceNotAuthorizedException ;
19
17
import org .eclipse .jgit .transport .resolver .ServiceNotEnabledException ;
18
+ import org .springframework .security .core .Authentication ;
20
19
21
- import javax .servlet .http .HttpServletRequest ;
22
20
import java .io .File ;
23
21
import java .io .IOException ;
24
22
import java .io .PrintWriter ;
25
23
import java .io .StringWriter ;
26
- import java .util .Collection ;
24
+ import java .nio .file .FileAlreadyExistsException ;
25
+ import java .nio .file .Files ;
26
+ import java .nio .file .Path ;
27
27
import java .util .logging .Level ;
28
28
import java .util .logging .Logger ;
29
29
@@ -39,19 +39,27 @@ public abstract class FileBackedHttpGitRepository extends HttpGitRepository {
39
39
* Directory of the local workspace on the controller.
40
40
* There will be "./.git" that hosts the actual repository.
41
41
*/
42
- public final File workspace ;
42
+ public final Path workspace ;
43
43
44
- protected FileBackedHttpGitRepository (File workspace ) {
44
+ protected FileBackedHttpGitRepository (Path workspace ) {
45
45
this .workspace = workspace ;
46
- if (!workspace .exists () && !workspace .mkdirs ()) {
47
- LOGGER .log (Level .WARNING , "Cannot create a workspace in {0}" , workspace );
46
+ try {
47
+ Files .createDirectory (workspace );
48
+ } catch (FileAlreadyExistsException ignored ) {
49
+ // don't need to worry about this; if it already exists, we don't care!
50
+ } catch (IOException e ) {
51
+ LOGGER .log (Level .WARNING , e , () -> "Cannot create a workspace in " + workspace );
48
52
}
49
53
}
50
54
55
+ protected FileBackedHttpGitRepository (File workspace ) {
56
+ this (workspace .toPath ());
57
+ }
58
+
51
59
@ Override
52
60
public Repository openRepository () throws IOException {
53
61
checkPullPermission ();
54
- Repository r = new FileRepositoryBuilder ().setWorkTree (workspace ).build ();
62
+ Repository r = new FileRepositoryBuilder ().setWorkTree (workspace . toFile () ).build ();
55
63
56
64
// if the repository doesn't exist, create it
57
65
if (!r .getObjectDatabase ().exists ()) {
@@ -63,6 +71,7 @@ public Repository openRepository() throws IOException {
63
71
/**
64
72
* Called when there's no .git directory to create one.
65
73
*
74
+ * <p>
66
75
* This implementation also imports whatever currently in there into the repository.
67
76
*/
68
77
protected void createInitialRepository (Repository r ) throws IOException {
@@ -80,14 +89,15 @@ protected void createInitialRepository(Repository r) throws IOException {
80
89
co .setMessage ("Initial import of the existing contents" );
81
90
co .call ();
82
91
} catch (GitAPIException e ) {
83
- LOGGER .log (Level .WARNING , "Initial import of " +workspace +" into Git repository failed" , e );
92
+ LOGGER .log (Level .WARNING , e , () -> "Initial import of " +workspace +" into Git repository failed" );
84
93
}
85
94
}
86
95
87
96
/**
88
97
* This default implementation allows read access to anyone
89
98
* who can access the HTTP URL this repository is bound to.
90
99
*
100
+ * <p>
91
101
* For example, if this object is used as a project action,
92
102
* and the project isn't readable to Alice, then Alice won't be
93
103
* able to pull from this repository (think of a POSIX file system
@@ -103,7 +113,7 @@ public UploadPack createUploadPack(HttpServletRequest context, Repository db) th
103
113
*/
104
114
@ Override
105
115
public ReceivePack createReceivePack (HttpServletRequest context , Repository db ) throws ServiceNotEnabledException , ServiceNotAuthorizedException {
106
- Authentication a = Jenkins .getAuthentication ();
116
+ Authentication a = Jenkins .getAuthentication2 ();
107
117
108
118
ReceivePack rp = createReceivePack (db );
109
119
@@ -118,15 +128,13 @@ public ReceivePack createReceivePack(Repository db) {
118
128
ReceivePack rp = new ReceivePack (db );
119
129
120
130
// update userContent after the push
121
- rp .setPostReceiveHook (new PostReceiveHook () {
122
- public void onPostReceive (ReceivePack rp , Collection <ReceiveCommand > commands ) {
123
- try {
124
- updateWorkspace (rp .getRepository ());
125
- } catch (Exception e ) {
126
- StringWriter sw = new StringWriter ();
127
- e .printStackTrace (new PrintWriter (sw ));
128
- rp .sendMessage ("Failed to update workspace: " +sw );
129
- }
131
+ rp .setPostReceiveHook ((rp1 , commands ) -> {
132
+ try {
133
+ updateWorkspace (rp1 .getRepository ());
134
+ } catch (Exception e ) {
135
+ StringWriter sw = new StringWriter ();
136
+ e .printStackTrace (new PrintWriter (sw ));
137
+ rp1 .sendMessage ("Failed to update workspace: " +sw );
130
138
}
131
139
});
132
140
return rp ;
0 commit comments