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