11package org .jenkinsci .plugins .gitserver ;
22
3+ import jakarta .servlet .http .HttpServletRequest ;
34import jenkins .model .Jenkins ;
4- import org .acegisecurity .Authentication ;
55import org .eclipse .jgit .api .AddCommand ;
66import org .eclipse .jgit .api .CommitCommand ;
77import org .eclipse .jgit .api .Git ;
1111import org .eclipse .jgit .lib .PersonIdent ;
1212import org .eclipse .jgit .lib .Repository ;
1313import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
14- import org .eclipse .jgit .transport .PostReceiveHook ;
15- import org .eclipse .jgit .transport .ReceiveCommand ;
1614import org .eclipse .jgit .transport .ReceivePack ;
1715import org .eclipse .jgit .transport .UploadPack ;
1816import org .eclipse .jgit .transport .resolver .ServiceNotAuthorizedException ;
1917import org .eclipse .jgit .transport .resolver .ServiceNotEnabledException ;
18+ import org .springframework .security .core .Authentication ;
2019
21- import javax .servlet .http .HttpServletRequest ;
2220import java .io .File ;
2321import java .io .IOException ;
2422import java .io .PrintWriter ;
2523import 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 ;
2727import java .util .logging .Level ;
2828import java .util .logging .Logger ;
2929
@@ -39,19 +39,27 @@ public abstract class FileBackedHttpGitRepository extends HttpGitRepository {
3939 * Directory of the local workspace on the controller.
4040 * There will be "./.git" that hosts the actual repository.
4141 */
42- public final File workspace ;
42+ public final Path workspace ;
4343
44- protected FileBackedHttpGitRepository (File workspace ) {
44+ protected FileBackedHttpGitRepository (Path workspace ) {
4545 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 );
4852 }
4953 }
5054
55+ protected FileBackedHttpGitRepository (File workspace ) {
56+ this (workspace .toPath ());
57+ }
58+
5159 @ Override
5260 public Repository openRepository () throws IOException {
5361 checkPullPermission ();
54- Repository r = new FileRepositoryBuilder ().setWorkTree (workspace ).build ();
62+ Repository r = new FileRepositoryBuilder ().setWorkTree (workspace . toFile () ).build ();
5563
5664 // if the repository doesn't exist, create it
5765 if (!r .getObjectDatabase ().exists ()) {
@@ -63,6 +71,7 @@ public Repository openRepository() throws IOException {
6371 /**
6472 * Called when there's no .git directory to create one.
6573 *
74+ * <p>
6675 * This implementation also imports whatever currently in there into the repository.
6776 */
6877 protected void createInitialRepository (Repository r ) throws IOException {
@@ -80,14 +89,15 @@ protected void createInitialRepository(Repository r) throws IOException {
8089 co .setMessage ("Initial import of the existing contents" );
8190 co .call ();
8291 } 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" );
8493 }
8594 }
8695
8796 /**
8897 * This default implementation allows read access to anyone
8998 * who can access the HTTP URL this repository is bound to.
9099 *
100+ * <p>
91101 * For example, if this object is used as a project action,
92102 * and the project isn't readable to Alice, then Alice won't be
93103 * able to pull from this repository (think of a POSIX file system
@@ -103,7 +113,7 @@ public UploadPack createUploadPack(HttpServletRequest context, Repository db) th
103113 */
104114 @ Override
105115 public ReceivePack createReceivePack (HttpServletRequest context , Repository db ) throws ServiceNotEnabledException , ServiceNotAuthorizedException {
106- Authentication a = Jenkins .getAuthentication ();
116+ Authentication a = Jenkins .getAuthentication2 ();
107117
108118 ReceivePack rp = createReceivePack (db );
109119
@@ -118,15 +128,13 @@ public ReceivePack createReceivePack(Repository db) {
118128 ReceivePack rp = new ReceivePack (db );
119129
120130 // 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 );
130138 }
131139 });
132140 return rp ;
0 commit comments