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