Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.git

File JGitRepository.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
31% of files have more coverage

Code metrics

14
93
28
2
276
241
51
0.55
3.32
14
1.82

Classes

Class Line # Actions
JGitRepository 28 79 41
0.6842105468.4%
JGitRepository.RepositoryLock 223 14 10
0.00%
 

Contributing tests

This file is covered by 65 tests. .

Source view

1    package io.sunshower.service.git;
2   
3    import io.sunshower.io.ReaderInputStream;
4    import io.sunshower.model.core.auth.Details;
5    import io.sunshower.model.core.auth.User;
6    import io.sunshower.model.core.faults.SystemException;
7    import io.sunshower.service.revision.model.Repository;
8    import io.sunshower.service.revision.model.Revision;
9    import java.io.*;
10    import java.nio.channels.FileChannel;
11    import java.nio.channels.FileLock;
12    import java.nio.file.Files;
13    import java.nio.file.Paths;
14    import java.nio.file.StandardCopyOption;
15    import java.util.concurrent.TimeUnit;
16    import java.util.concurrent.atomic.AtomicReference;
17    import java.util.concurrent.locks.Condition;
18    import java.util.concurrent.locks.Lock;
19    import org.eclipse.jgit.api.Git;
20    import org.eclipse.jgit.api.errors.GitAPIException;
21    import org.eclipse.jgit.internal.storage.file.LockFile;
22    import org.eclipse.jgit.lib.ObjectId;
23    import org.eclipse.jgit.lib.PersonIdent;
24    import org.eclipse.jgit.revwalk.RevCommit;
25    import org.eclipse.jgit.revwalk.RevWalk;
26    import org.jetbrains.annotations.NotNull;
27   
 
28    public class JGitRepository implements GitRepository {
29   
30    private Git git;
31    private boolean open;
32    private final File local;
33    private final User session;
34    private final Repository repository;
35    private final AtomicReference<Lock> lock;
36   
 
37  151 toggle public JGitRepository(final Repository repository, final User session) {
38  151 this.session = session;
39  151 this.repository = repository;
40  151 this.local = Paths.get(repository.getLocal().getFile().getPath()).toFile();
41  151 this.lock = new AtomicReference<>();
42    }
43   
 
44  20 toggle public File getLocal() {
45  20 return local;
46    }
47   
 
48  10 toggle public boolean isLocked(boolean localOnly) {
49  10 final File parent = new File(local, ".git");
50  10 final File index = new File(parent, "index");
51  10 final LockFile lockFile = new LockFile(index);
52  10 boolean lockedSuccessfully;
53  10 try {
54  10 lockedSuccessfully = lockFile.lock();
55    } catch (IOException e) {
56  0 lockedSuccessfully = false;
57    //// TODO: 11/21/17 log
58    } finally {
59  10 lockFile.unlock();
60    }
61  10 return !lockedSuccessfully;
62    }
63   
 
64  10 toggle @Override
65    public boolean isLocked() {
66  10 return isLocked(false);
67    }
68   
 
69  0 toggle @Override
70    public boolean isClosed() {
71  0 return !open;
72    }
73   
 
74  49 toggle @Override
75    public void open() {
76  49 if (open) {
77  0 throw new IllegalStateException("Attempting to open an opened repository");
78    }
79  49 try {
80  49 git = Git.open(local);
81  49 open = true;
82    } catch (IOException e) {
83  0 throw new RepositoryException(e);
84    }
85    }
86   
 
87  0 toggle @Override
88    public Lock lock() {
89  0 check();
90  0 try (RandomAccessFile file = new RandomAccessFile(local, "w"); ) {
91  0 return setLock(file.getChannel());
92    } catch (IOException e) {
93  0 throw new IllegalStateException(e);
94    }
95    }
96   
 
97  0 toggle @Override
98    public void unlock() {
99  0 Lock lock = this.lock.get();
100  0 if (lock == null) {
101  0 throw new IllegalStateException("Repository is not locked");
102    }
103  0 lock.unlock();
104    }
105   
 
106  45 toggle @Override
107    public void close() throws Exception {
108  45 if (!open) {
109  0 throw new IllegalStateException("Attempting to close a closed repository");
110    }
111  45 git.close();
112  45 git.getRepository().close();
113    }
114   
 
115  5 toggle @Override
116    public File write(String name, Reader reader) {
117  5 return write(name, new ReaderInputStream(reader));
118    }
119   
 
120  54 toggle @Override
121    public File write(String name, InputStream inputStream) {
122   
123  54 final File file = new File(local, name);
124  54 try {
125  54 if (file.exists()) {
126  14 Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
127    } else {
128  40 final File parent = file.getParentFile();
129  40 if (!parent.equals(local)) {
130  36 if (!(parent.exists() || parent.mkdirs())) {
131  0 throw new RuntimeException("Couldn't make parent file");
132    }
133    }
134  40 try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
135  40 io.sunshower.io.Files.copy(inputStream, fileOutputStream);
136  40 fileOutputStream.flush();
137    }
138    }
139  54 git.add().addFilepattern(name).call();
140  54 return file;
141    } catch (Exception e) {
142  0 throw new RepositoryException(e);
143    }
144    }
145   
 
146  96 toggle @Override
147    public void initialize() {
148  96 try {
149  96 Git.init().setDirectory(local).call();
150    } catch (GitAPIException e) {
151  0 throw new RepositoryException(e);
152    }
153    }
154   
 
155  4 toggle @Override
156    public Revision commit() {
157  4 return commit(buildMessage());
158    }
159   
 
160  4 toggle private String buildMessage() {
161  4 return "sup";
162    }
163   
 
164  4 toggle @Override
165    public Revision commit(String message) {
166  4 check();
167  4 Details details = session.getDetails();
168  4 PersonIdent ident =
169    new PersonIdent(
170    details.getFirstname() + " " + details.getLastname(), details.getEmailAddress());
171  4 try {
172  4 RevCommit call = git.commit().setAuthor(ident).setCommitter(ident).setMessage(message).call();
173  4 return RevisionUtils.fromCommit(call);
174    } catch (GitAPIException e) {
175  0 throw new RepositoryException(e);
176    }
177    }
178   
 
179  0 toggle @Override
180    public File read(File p) {
181  0 return null;
182    }
183   
 
184  31 toggle @Override
185    public InputStream read(String path) {
186  31 final File file = new File(local, path);
187  31 try {
188  31 return new BufferedInputStream(new FileInputStream(file));
189    } catch (FileNotFoundException e) {
190  0 throw new RepositoryException(e);
191    }
192    }
193   
 
194  2 toggle @Override
195    public void checkout(Revision commit) {
196  2 check();
197  2 try {
198  2 RevCommit revCommit =
199    new RevWalk(git.getRepository()).parseCommit(ObjectId.fromString(commit.getRevision()));
200  2 git.checkout().setCreateBranch(true).setName("working").setStartPoint(revCommit).call();
201    } catch (Exception e) {
202  0 throw new RepositoryException(e);
203    }
204    }
205   
 
206  22 toggle @Override
207    public boolean exists(String path) {
208  22 return new File(local, path).exists();
209    }
210   
 
211  6 toggle private void check() {
212  6 if (!open) {
213  0 throw new IllegalStateException("Repository must be opened");
214    }
215    }
216   
 
217  0 toggle private Lock setLock(FileChannel channel) {
218  0 final Lock lock = new RepositoryLock(channel);
219  0 this.lock.set(lock);
220  0 return lock;
221    }
222   
 
223    private class RepositoryLock implements Lock {
224   
225    private FileLock lock;
226    final FileChannel channel;
227   
 
228  0 toggle private RepositoryLock(FileChannel channel) {
229  0 this.channel = channel;
230    }
231   
 
232  0 toggle @Override
233    public void lock() {
234  0 try {
235  0 lock = channel.tryLock();
236    } catch (IOException e) {
237  0 throw new SystemException(e);
238    }
239    }
240   
 
241  0 toggle @Override
242    public void lockInterruptibly() throws InterruptedException {
243  0 lock();
244    }
245   
 
246  0 toggle @Override
247    public boolean tryLock() {
248  0 try {
249  0 lock();
250  0 return true;
251    } catch (Exception ex) {
252  0 return false;
253    }
254    }
255   
 
256  0 toggle @Override
257    public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException {
258  0 return tryLock();
259    }
260   
 
261  0 toggle @Override
262    public void unlock() {
263  0 try {
264  0 lock.close();
265    } catch (IOException e) {
266  0 throw new SystemException(e);
267    }
268    }
269   
 
270  0 toggle @NotNull
271    @Override
272    public Condition newCondition() {
273  0 throw new UnsupportedOperationException();
274    }
275    }
276    }