11package org .jenkinsci .plugins .gitserver ;
22
3+ import jakarta .servlet .http .HttpServletRequest ;
34import java .io .File ;
45import java .io .IOException ;
56import java .io .PrintWriter ;
67import 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 ;
811import java .util .logging .Level ;
912import java .util .logging .Logger ;
10- import javax .servlet .http .HttpServletRequest ;
1113import jenkins .model .Jenkins ;
12- import org .acegisecurity .Authentication ;
1314import org .eclipse .jgit .api .AddCommand ;
1415import org .eclipse .jgit .api .CommitCommand ;
1516import org .eclipse .jgit .api .Git ;
1920import org .eclipse .jgit .lib .PersonIdent ;
2021import org .eclipse .jgit .lib .Repository ;
2122import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
22- import org .eclipse .jgit .transport .PostReceiveHook ;
23- import org .eclipse .jgit .transport .ReceiveCommand ;
2423import org .eclipse .jgit .transport .ReceivePack ;
2524import org .eclipse .jgit .transport .UploadPack ;
2625import org .eclipse .jgit .transport .resolver .ServiceNotAuthorizedException ;
2726import org .eclipse .jgit .transport .resolver .ServiceNotEnabledException ;
27+ import org .springframework .security .core .Authentication ;
2828
2929/**
3030 * Convenient subtype of {@link HttpGitRepository} where the repository
@@ -38,19 +38,28 @@ public abstract class FileBackedHttpGitRepository extends HttpGitRepository {
3838 * Directory of the local workspace on the controller.
3939 * There will be "./.git" that hosts the actual repository.
4040 */
41- public final File workspace ;
41+ public final Path workspace ;
4242
43- protected FileBackedHttpGitRepository (File workspace ) {
43+ protected FileBackedHttpGitRepository (Path workspace ) {
4444 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 );
4751 }
4852 }
4953
54+ protected FileBackedHttpGitRepository (File workspace ) {
55+ this (workspace .toPath ());
56+ }
57+
5058 @ Override
5159 public Repository openRepository () throws IOException {
5260 checkPullPermission ();
53- Repository r = new FileRepositoryBuilder ().setWorkTree (workspace ).build ();
61+ Repository r =
62+ new FileRepositoryBuilder ().setWorkTree (workspace .toFile ()).build ();
5463
5564 // if the repository doesn't exist, create it
5665 if (!r .getObjectDatabase ().exists ()) {
@@ -62,6 +71,7 @@ public Repository openRepository() throws IOException {
6271 /**
6372 * Called when there's no .git directory to create one.
6473 *
74+ * <p>
6575 * This implementation also imports whatever currently in there into the repository.
6676 */
6777 protected void createInitialRepository (Repository r ) throws IOException {
@@ -79,14 +89,15 @@ protected void createInitialRepository(Repository r) throws IOException {
7989 co .setMessage ("Initial import of the existing contents" );
8090 co .call ();
8191 } 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" );
8393 }
8494 }
8595
8696 /**
8797 * This default implementation allows read access to anyone
8898 * who can access the HTTP URL this repository is bound to.
8999 *
100+ * <p>
90101 * For example, if this object is used as a project action,
91102 * and the project isn't readable to Alice, then Alice won't be
92103 * able to pull from this repository (think of a POSIX file system
@@ -104,7 +115,7 @@ public UploadPack createUploadPack(HttpServletRequest context, Repository db)
104115 @ Override
105116 public ReceivePack createReceivePack (HttpServletRequest context , Repository db )
106117 throws ServiceNotEnabledException , ServiceNotAuthorizedException {
107- Authentication a = Jenkins .getAuthentication ();
118+ Authentication a = Jenkins .getAuthentication2 ();
108119
109120 ReceivePack rp = createReceivePack (db );
110121
@@ -119,15 +130,13 @@ public ReceivePack createReceivePack(Repository db) {
119130 ReceivePack rp = new ReceivePack (db );
120131
121132 // 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 );
131140 }
132141 });
133142 return rp ;
0 commit comments