1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import org.eclipse.jgit.api.errors.JGitInternalException;
47 import org.eclipse.jgit.lib.ConfigConstants;
48 import org.eclipse.jgit.util.GitDateParser;
49 import org.eclipse.jgit.util.SystemReader;
50
51 import static java.nio.charset.StandardCharsets.UTF_8;
52
53 import java.io.File;
54 import java.io.IOException;
55 import java.nio.file.Files;
56 import java.nio.file.NoSuchFileException;
57 import java.nio.file.attribute.FileTime;
58 import java.text.ParseException;
59 import java.time.Instant;
60
61
62
63
64 class GcLog {
65 private final FileRepository repo;
66
67 private final File logFile;
68
69 private final LockFile lock;
70
71 private Instant gcLogExpire;
72
73 private static final String LOG_EXPIRY_DEFAULT = "1.day.ago";
74
75 private boolean nonEmpty = false;
76
77
78
79
80
81
82
83 GcLog(FileRepository repo) {
84 this.repo = repo;
85 logFile = new File(repo.getDirectory(), "gc.log");
86 lock = new LockFile(logFile);
87 }
88
89 private Instant getLogExpiry() throws ParseException {
90 if (gcLogExpire == null) {
91 String logExpiryStr = repo.getConfig().getString(
92 ConfigConstants.CONFIG_GC_SECTION, null,
93 ConfigConstants.CONFIG_KEY_LOGEXPIRY);
94 if (logExpiryStr == null) {
95 logExpiryStr = LOG_EXPIRY_DEFAULT;
96 }
97 gcLogExpire = GitDateParser.parse(logExpiryStr, null,
98 SystemReader.getInstance().getLocale()).toInstant();
99 }
100 return gcLogExpire;
101 }
102
103 private boolean autoGcBlockedByOldLockFile() {
104 try {
105 FileTime lastModified = Files.getLastModifiedTime(logFile.toPath());
106 if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) {
107
108 return true;
109 }
110 } catch (NoSuchFileException e) {
111
112 } catch (IOException | ParseException e) {
113 throw new JGitInternalException(e.getMessage(), e);
114 }
115 return false;
116 }
117
118
119
120
121
122
123 boolean lock() {
124 try {
125 if (!lock.lock()) {
126 return false;
127 }
128 } catch (IOException e) {
129 throw new JGitInternalException(e.getMessage(), e);
130 }
131 if (autoGcBlockedByOldLockFile()) {
132 lock.unlock();
133 return false;
134 }
135 return true;
136 }
137
138
139
140
141 void unlock() {
142 lock.unlock();
143 }
144
145
146
147
148
149
150
151 boolean commit() {
152 if (nonEmpty) {
153 return lock.commit();
154 } else {
155 logFile.delete();
156 lock.unlock();
157 return true;
158 }
159 }
160
161
162
163
164
165
166
167
168
169 void write(String content) throws IOException {
170 if (content.length() > 0) {
171 nonEmpty = true;
172 }
173 lock.write(content.getBytes(UTF_8));
174 }
175 }