1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.transport;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14 import static org.eclipse.jgit.lib.Constants.HEAD;
15 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_ATOMIC;
16 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_DELETE_REFS;
17 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_OFS_DELTA;
18 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_PUSH_OPTIONS;
19 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_QUIET;
20 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS;
21 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SIDE_BAND_64K;
22 import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
23 import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA;
24 import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR;
25 import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS;
26 import static org.eclipse.jgit.transport.SideBandOutputStream.MAX_BUF;
27
28 import java.io.EOFException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.io.UncheckedIOException;
33 import java.text.MessageFormat;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40 import java.util.concurrent.TimeUnit;
41 import java.util.function.Function;
42 import java.util.stream.Collectors;
43
44 import org.eclipse.jgit.annotations.Nullable;
45 import org.eclipse.jgit.errors.InvalidObjectIdException;
46 import org.eclipse.jgit.errors.LargeObjectException;
47 import org.eclipse.jgit.errors.PackProtocolException;
48 import org.eclipse.jgit.errors.TooLargePackException;
49 import org.eclipse.jgit.errors.UnpackException;
50 import org.eclipse.jgit.internal.JGitText;
51 import org.eclipse.jgit.internal.submodule.SubmoduleValidator;
52 import org.eclipse.jgit.internal.submodule.SubmoduleValidator.SubmoduleValidationException;
53 import org.eclipse.jgit.internal.transport.connectivity.FullConnectivityChecker;
54 import org.eclipse.jgit.internal.transport.parser.FirstCommand;
55 import org.eclipse.jgit.lib.AnyObjectId;
56 import org.eclipse.jgit.lib.BatchRefUpdate;
57 import org.eclipse.jgit.lib.Config;
58 import org.eclipse.jgit.lib.ConfigConstants;
59 import org.eclipse.jgit.lib.Constants;
60 import org.eclipse.jgit.lib.GitmoduleEntry;
61 import org.eclipse.jgit.lib.NullProgressMonitor;
62 import org.eclipse.jgit.lib.ObjectChecker;
63 import org.eclipse.jgit.lib.ObjectDatabase;
64 import org.eclipse.jgit.lib.ObjectId;
65 import org.eclipse.jgit.lib.ObjectInserter;
66 import org.eclipse.jgit.lib.ObjectLoader;
67 import org.eclipse.jgit.lib.PersonIdent;
68 import org.eclipse.jgit.lib.ProgressMonitor;
69 import org.eclipse.jgit.lib.Ref;
70 import org.eclipse.jgit.lib.Repository;
71 import org.eclipse.jgit.revwalk.RevCommit;
72 import org.eclipse.jgit.revwalk.RevObject;
73 import org.eclipse.jgit.revwalk.RevWalk;
74 import org.eclipse.jgit.transport.ConnectivityChecker.ConnectivityCheckInfo;
75 import org.eclipse.jgit.transport.PacketLineIn.InputOverLimitIOException;
76 import org.eclipse.jgit.transport.ReceiveCommand.Result;
77 import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
78 import org.eclipse.jgit.util.io.InterruptTimer;
79 import org.eclipse.jgit.util.io.LimitedInputStream;
80 import org.eclipse.jgit.util.io.TimeoutInputStream;
81 import org.eclipse.jgit.util.io.TimeoutOutputStream;
82
83
84
85
86 public class ReceivePack {
87
88
89
90
91
92
93 @Deprecated
94 public static class FirstLine {
95 private final FirstCommand command;
96
97
98
99
100
101
102
103 public FirstLine(String line) {
104 command = FirstCommand.fromLine(line);
105 }
106
107
108 public String getLine() {
109 return command.getLine();
110 }
111
112
113 public Set<String> getCapabilities() {
114 return command.getCapabilities();
115 }
116 }
117
118
119 private final Repository db;
120
121
122 private final RevWalk walk;
123
124
125
126
127
128
129
130
131
132
133
134
135 private boolean biDirectionalPipe = true;
136
137
138 private boolean expectDataAfterPackFooter;
139
140
141 private ObjectChecker objectChecker;
142
143
144 private boolean allowCreates;
145
146
147 private boolean allowAnyDeletes;
148
149 private boolean allowBranchDeletes;
150
151
152 private boolean allowNonFastForwards;
153
154
155 private boolean allowPushOptions;
156
157
158
159
160
161 private boolean atomic;
162
163 private boolean allowOfsDelta;
164
165 private boolean allowQuiet = true;
166
167
168 private PersonIdent refLogIdent;
169
170
171 private AdvertiseRefsHook advertiseRefsHook;
172
173
174 private RefFilter refFilter;
175
176
177 private int timeout;
178
179
180 private InterruptTimer timer;
181
182 private TimeoutInputStream timeoutIn;
183
184
185
186 private OutputStream origOut;
187
188
189 private InputStream rawIn;
190
191
192 private OutputStream rawOut;
193
194
195 private OutputStream msgOut;
196
197 private SideBandOutputStream errOut;
198
199
200 private PacketLineIn pckIn;
201
202
203 private PacketLineOut pckOut;
204
205 private final MessageOutputWrapper msgOutWrapper = new MessageOutputWrapper();
206
207 private PackParser parser;
208
209
210 private Map<String, Ref> refs;
211
212
213 private Set<ObjectId> advertisedHaves;
214
215
216 private Set<String> enabledCapabilities;
217
218 String userAgent;
219
220 private Set<ObjectId> clientShallowCommits;
221
222 private List<ReceiveCommand> commands;
223
224 private long maxCommandBytes;
225
226 private long maxDiscardBytes;
227
228 private StringBuilder advertiseError;
229
230
231
232
233 private boolean sideBand;
234
235 private boolean quiet;
236
237
238 private PackLock packLock;
239
240 private boolean checkReferencedAreReachable;
241
242
243 private long maxObjectSizeLimit;
244
245
246 private long maxPackSizeLimit = -1;
247
248
249 private Long packSize;
250
251 private PushCertificateParser pushCertificateParser;
252
253 private SignedPushConfig signedPushConfig;
254
255 private PushCertificate pushCert;
256
257 private ReceivedPackStatistics stats;
258
259
260
261
262
263 protected ConnectivityChecker connectivityChecker = new FullConnectivityChecker();
264
265
266 private PreReceiveHook preReceive;
267
268 private ReceiveCommandErrorHandler receiveCommandErrorHandler = new ReceiveCommandErrorHandler() {
269
270 };
271
272 private UnpackErrorHandler unpackErrorHandler = new DefaultUnpackErrorHandler();
273
274
275 private PostReceiveHook postReceive;
276
277
278 private boolean reportStatus;
279
280
281 private boolean usePushOptions;
282 private List<String> pushOptions;
283
284
285
286
287
288
289
290 public ReceivePack(Repository into) {
291 db = into;
292 walk = new RevWalk(db);
293 walk.setRetainBody(false);
294
295 TransferConfig tc = db.getConfig().get(TransferConfig.KEY);
296 objectChecker = tc.newReceiveObjectChecker();
297
298 ReceiveConfig rc = db.getConfig().get(ReceiveConfig::new);
299 allowCreates = rc.allowCreates;
300 allowAnyDeletes = true;
301 allowBranchDeletes = rc.allowDeletes;
302 allowNonFastForwards = rc.allowNonFastForwards;
303 allowOfsDelta = rc.allowOfsDelta;
304 allowPushOptions = rc.allowPushOptions;
305 maxCommandBytes = rc.maxCommandBytes;
306 maxDiscardBytes = rc.maxDiscardBytes;
307 advertiseRefsHook = AdvertiseRefsHook.DEFAULT;
308 refFilter = RefFilter.DEFAULT;
309 advertisedHaves = new HashSet<>();
310 clientShallowCommits = new HashSet<>();
311 signedPushConfig = rc.signedPush;
312 preReceive = PreReceiveHook.NULL;
313 postReceive = PostReceiveHook.NULL;
314 }
315
316
317 private static class ReceiveConfig {
318 final boolean allowCreates;
319
320 final boolean allowDeletes;
321
322 final boolean allowNonFastForwards;
323
324 final boolean allowOfsDelta;
325
326 final boolean allowPushOptions;
327
328 final long maxCommandBytes;
329
330 final long maxDiscardBytes;
331
332 final SignedPushConfig signedPush;
333
334 ReceiveConfig(Config config) {
335 allowCreates = true;
336 allowDeletes = !config.getBoolean("receive", "denydeletes", false);
337 allowNonFastForwards = !config.getBoolean("receive",
338 "denynonfastforwards", false);
339 allowOfsDelta = config.getBoolean("repack", "usedeltabaseoffset",
340 true);
341 allowPushOptions = config.getBoolean("receive", "pushoptions",
342 false);
343 maxCommandBytes = config.getLong("receive",
344 "maxCommandBytes",
345 3 << 20);
346 maxDiscardBytes = config.getLong("receive",
347 "maxCommandDiscardBytes",
348 -1);
349 signedPush = SignedPushConfig.KEY.parse(config);
350 }
351 }
352
353
354
355
356
357
358
359 class MessageOutputWrapper extends OutputStream {
360 @Override
361 public void write(int ch) {
362 if (msgOut != null) {
363 try {
364 msgOut.write(ch);
365 } catch (IOException e) {
366
367 }
368 }
369 }
370
371 @Override
372 public void write(byte[] b, int off, int len) {
373 if (msgOut != null) {
374 try {
375 msgOut.write(b, off, len);
376 } catch (IOException e) {
377
378 }
379 }
380 }
381
382 @Override
383 public void write(byte[] b) {
384 write(b, 0, b.length);
385 }
386
387 @Override
388 public void flush() {
389 if (msgOut != null) {
390 try {
391 msgOut.flush();
392 } catch (IOException e) {
393
394 }
395 }
396 }
397 }
398
399
400
401
402
403
404 public Repository getRepository() {
405 return db;
406 }
407
408
409
410
411
412
413 public RevWalk getRevWalk() {
414 return walk;
415 }
416
417
418
419
420
421
422
423 public Map<String, Ref> getAdvertisedRefs() {
424 return refs;
425 }
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 public void setAdvertisedRefs(Map<String, Ref> allRefs,
446 Set<ObjectId> additionalHaves) throws IOException {
447 refs = allRefs != null ? allRefs : getAllRefs();
448 refs = refFilter.filter(refs);
449 advertisedHaves.clear();
450
451 Ref head = refs.get(HEAD);
452 if (head != null && head.isSymbolic()) {
453 refs.remove(HEAD);
454 }
455
456 for (Ref ref : refs.values()) {
457 if (ref.getObjectId() != null) {
458 advertisedHaves.add(ref.getObjectId());
459 }
460 }
461 if (additionalHaves != null) {
462 advertisedHaves.addAll(additionalHaves);
463 } else {
464 advertisedHaves.addAll(db.getAdditionalHaves());
465 }
466 }
467
468
469
470
471
472
473
474
475 public final Set<ObjectId> getAdvertisedObjects() {
476 return advertisedHaves;
477 }
478
479
480
481
482
483
484
485
486
487 public boolean isCheckReferencedObjectsAreReachable() {
488 return checkReferencedAreReachable;
489 }
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512 public void setCheckReferencedObjectsAreReachable(boolean b) {
513 this.checkReferencedAreReachable = b;
514 }
515
516
517
518
519
520
521
522
523 public boolean isBiDirectionalPipe() {
524 return biDirectionalPipe;
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540 public void setBiDirectionalPipe(boolean twoWay) {
541 biDirectionalPipe = twoWay;
542 }
543
544
545
546
547
548
549 public boolean isExpectDataAfterPackFooter() {
550 return expectDataAfterPackFooter;
551 }
552
553
554
555
556
557
558
559
560 public void setExpectDataAfterPackFooter(boolean e) {
561 expectDataAfterPackFooter = e;
562 }
563
564
565
566
567
568
569
570
571
572 public boolean isCheckReceivedObjects() {
573 return objectChecker != null;
574 }
575
576
577
578
579
580
581
582
583
584 public void setCheckReceivedObjects(boolean check) {
585 if (check && objectChecker == null)
586 setObjectChecker(new ObjectChecker());
587 else if (!check && objectChecker != null)
588 setObjectChecker(null);
589 }
590
591
592
593
594
595
596
597
598
599 public void setObjectChecker(ObjectChecker impl) {
600 objectChecker = impl;
601 }
602
603
604
605
606
607
608 public boolean isAllowCreates() {
609 return allowCreates;
610 }
611
612
613
614
615
616
617
618 public void setAllowCreates(boolean canCreate) {
619 allowCreates = canCreate;
620 }
621
622
623
624
625
626
627 public boolean isAllowDeletes() {
628 return allowAnyDeletes;
629 }
630
631
632
633
634
635
636
637 public void setAllowDeletes(boolean canDelete) {
638 allowAnyDeletes = canDelete;
639 }
640
641
642
643
644
645
646
647 public boolean isAllowBranchDeletes() {
648 return allowBranchDeletes;
649 }
650
651
652
653
654
655
656
657
658
659
660 public void setAllowBranchDeletes(boolean canDelete) {
661 allowBranchDeletes = canDelete;
662 }
663
664
665
666
667
668
669
670
671 public boolean isAllowNonFastForwards() {
672 return allowNonFastForwards;
673 }
674
675
676
677
678
679
680
681
682
683 public void setAllowNonFastForwards(boolean canRewind) {
684 allowNonFastForwards = canRewind;
685 }
686
687
688
689
690
691
692
693
694
695 public boolean isAtomic() {
696 return atomic;
697 }
698
699
700
701
702
703
704
705
706
707
708 public void setAtomic(boolean atomic) {
709 this.atomic = atomic;
710 }
711
712
713
714
715
716
717 public PersonIdent getRefLogIdent() {
718 return refLogIdent;
719 }
720
721
722
723
724
725
726
727
728
729
730
731
732
733 public void setRefLogIdent(PersonIdent pi) {
734 refLogIdent = pi;
735 }
736
737
738
739
740
741
742 public AdvertiseRefsHook getAdvertiseRefsHook() {
743 return advertiseRefsHook;
744 }
745
746
747
748
749
750
751 public RefFilter getRefFilter() {
752 return refFilter;
753 }
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770 public void setAdvertiseRefsHook(AdvertiseRefsHook advertiseRefsHook) {
771 if (advertiseRefsHook != null)
772 this.advertiseRefsHook = advertiseRefsHook;
773 else
774 this.advertiseRefsHook = AdvertiseRefsHook.DEFAULT;
775 }
776
777
778
779
780
781
782
783
784
785
786
787 public void setRefFilter(RefFilter refFilter) {
788 this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
789 }
790
791
792
793
794
795
796 public int getTimeout() {
797 return timeout;
798 }
799
800
801
802
803
804
805
806
807
808 public void setTimeout(int seconds) {
809 timeout = seconds;
810 }
811
812
813
814
815
816
817
818
819 public void setMaxCommandBytes(long limit) {
820 maxCommandBytes = limit;
821 }
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840 public void setMaxCommandDiscardBytes(long limit) {
841 maxDiscardBytes = limit;
842 }
843
844
845
846
847
848
849
850
851
852
853 public void setMaxObjectSizeLimit(long limit) {
854 maxObjectSizeLimit = limit;
855 }
856
857
858
859
860
861
862
863
864
865
866 public void setMaxPackSizeLimit(long limit) {
867 if (limit < 0)
868 throw new IllegalArgumentException(
869 MessageFormat.format(JGitText.get().receivePackInvalidLimit,
870 Long.valueOf(limit)));
871 maxPackSizeLimit = limit;
872 }
873
874
875
876
877
878
879
880
881
882
883
884
885 public boolean isSideBand() throws RequestNotYetReadException {
886 checkRequestWasRead();
887 return enabledCapabilities.contains(CAPABILITY_SIDE_BAND_64K);
888 }
889
890
891
892
893
894
895
896 public boolean isAllowQuiet() {
897 return allowQuiet;
898 }
899
900
901
902
903
904
905
906
907
908
909
910 public void setAllowQuiet(boolean allow) {
911 allowQuiet = allow;
912 }
913
914
915
916
917
918
919
920 public boolean isAllowPushOptions() {
921 return allowPushOptions;
922 }
923
924
925
926
927
928
929
930
931 public void setAllowPushOptions(boolean allow) {
932 allowPushOptions = allow;
933 }
934
935
936
937
938
939
940
941
942
943
944
945
946 public boolean isQuiet() throws RequestNotYetReadException {
947 checkRequestWasRead();
948 return quiet;
949 }
950
951
952
953
954
955
956
957
958
959
960 public void setSignedPushConfig(SignedPushConfig cfg) {
961 signedPushConfig = cfg;
962 }
963
964 private PushCertificateParser getPushCertificateParser() {
965 if (pushCertificateParser == null) {
966 pushCertificateParser = new PushCertificateParser(db,
967 signedPushConfig);
968 }
969 return pushCertificateParser;
970 }
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987 public String getPeerUserAgent() {
988 return UserAgent.getAgent(enabledCapabilities, userAgent);
989 }
990
991
992
993
994
995
996 public List<ReceiveCommand> getAllCommands() {
997 return Collections.unmodifiableList(commands);
998 }
999
1000
1001
1002
1003
1004
1005
1006 public void setReceiveCommandErrorHandler(
1007 ReceiveCommandErrorHandler receiveCommandErrorHandler) {
1008 this.receiveCommandErrorHandler = receiveCommandErrorHandler;
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036 public void sendError(String what) {
1037 if (refs == null) {
1038 if (advertiseError == null)
1039 advertiseError = new StringBuilder();
1040 advertiseError.append(what).append('\n');
1041 } else {
1042 msgOutWrapper.write(Constants.encode("error: " + what + "\n"));
1043 }
1044 }
1045
1046 private void fatalError(String msg) {
1047 if (errOut != null) {
1048 try {
1049 errOut.write(Constants.encode(msg));
1050 errOut.flush();
1051 } catch (IOException e) {
1052
1053 }
1054 } else {
1055 sendError(msg);
1056 }
1057 }
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069 public void sendMessage(String what) {
1070 msgOutWrapper.write(Constants.encode(what + "\n"));
1071 }
1072
1073
1074
1075
1076
1077
1078 public OutputStream getMessageOutputStream() {
1079 return msgOutWrapper;
1080 }
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 public boolean hasReceivedPack() {
1093 return packSize != null;
1094 }
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106 public long getPackSize() {
1107 if (packSize != null)
1108 return packSize.longValue();
1109 throw new IllegalStateException(JGitText.get().packSizeNotSetYet);
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119 private Set<ObjectId> getClientShallowCommits() {
1120 return clientShallowCommits;
1121 }
1122
1123
1124
1125
1126
1127
1128 private boolean hasCommands() {
1129 return !commands.isEmpty();
1130 }
1131
1132
1133
1134
1135
1136
1137 private boolean hasError() {
1138 return advertiseError != null;
1139 }
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 protected void init(final InputStream input, final OutputStream output,
1162 final OutputStream messages) {
1163 origOut = output;
1164 rawIn = input;
1165 rawOut = output;
1166 msgOut = messages;
1167
1168 if (timeout > 0) {
1169 final Thread caller = Thread.currentThread();
1170 timer = new InterruptTimer(caller.getName() + "-Timer");
1171 timeoutIn = new TimeoutInputStream(rawIn, timer);
1172 TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
1173 timeoutIn.setTimeout(timeout * 1000);
1174 o.setTimeout(timeout * 1000);
1175 rawIn = timeoutIn;
1176 rawOut = o;
1177 }
1178
1179 pckIn = new PacketLineIn(rawIn);
1180 pckOut = new PacketLineOut(rawOut);
1181 pckOut.setFlushOnEnd(false);
1182
1183 enabledCapabilities = new HashSet<>();
1184 commands = new ArrayList<>();
1185 }
1186
1187
1188
1189
1190
1191
1192
1193 private Map<String, Ref> getAdvertisedOrDefaultRefs() throws IOException {
1194 if (refs == null)
1195 setAdvertisedRefs(null, null);
1196 return refs;
1197 }
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214 protected void receivePackAndCheckConnectivity() throws IOException,
1215 LargeObjectException, SubmoduleValidationException {
1216 receivePack();
1217 if (needCheckConnectivity()) {
1218 checkSubmodules();
1219 checkConnectivity();
1220 }
1221 parser = null;
1222 }
1223
1224
1225
1226
1227
1228
1229
1230 private void unlockPack() throws IOException {
1231 if (packLock != null) {
1232 packLock.unlock();
1233 packLock = null;
1234 }
1235 }
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247 public void sendAdvertisedRefs(RefAdvertiser adv)
1248 throws IOException, ServiceMayNotContinueException {
1249 if (advertiseError != null) {
1250 adv.writeOne("ERR " + advertiseError);
1251 return;
1252 }
1253
1254 try {
1255 advertiseRefsHook.advertiseRefs(this);
1256 } catch (ServiceMayNotContinueException fail) {
1257 if (fail.getMessage() != null) {
1258 adv.writeOne("ERR " + fail.getMessage());
1259 fail.setOutput();
1260 }
1261 throw fail;
1262 }
1263
1264 adv.init(db);
1265 adv.advertiseCapability(CAPABILITY_SIDE_BAND_64K);
1266 adv.advertiseCapability(CAPABILITY_DELETE_REFS);
1267 adv.advertiseCapability(CAPABILITY_REPORT_STATUS);
1268 if (allowQuiet)
1269 adv.advertiseCapability(CAPABILITY_QUIET);
1270 String nonce = getPushCertificateParser().getAdvertiseNonce();
1271 if (nonce != null) {
1272 adv.advertiseCapability(nonce);
1273 }
1274 if (db.getRefDatabase().performsAtomicTransactions())
1275 adv.advertiseCapability(CAPABILITY_ATOMIC);
1276 if (allowOfsDelta)
1277 adv.advertiseCapability(CAPABILITY_OFS_DELTA);
1278 if (allowPushOptions) {
1279 adv.advertiseCapability(CAPABILITY_PUSH_OPTIONS);
1280 }
1281 adv.advertiseCapability(OPTION_AGENT, UserAgent.get());
1282 adv.send(getAdvertisedOrDefaultRefs().values());
1283 for (ObjectId obj : advertisedHaves)
1284 adv.advertiseHave(obj);
1285 if (adv.isEmpty())
1286 adv.advertiseId(ObjectId.zeroId(), "capabilities^{}");
1287 adv.end();
1288 }
1289
1290
1291
1292
1293
1294
1295
1296
1297 @Nullable
1298 public ReceivedPackStatistics getReceivedPackStatistics() {
1299 return stats;
1300 }
1301
1302
1303
1304
1305
1306
1307 private Map<String, Ref> getAllRefs() {
1308 try {
1309 return db.getRefDatabase().getRefs().stream()
1310 .collect(Collectors.toMap(Ref::getName,
1311 Function.identity()));
1312 } catch (IOException e) {
1313 throw new UncheckedIOException(e);
1314 }
1315 }
1316
1317
1318
1319
1320
1321
1322 private void recvCommands() throws IOException {
1323 PacketLineIn pck = maxCommandBytes > 0
1324 ? new PacketLineIn(rawIn, maxCommandBytes)
1325 : pckIn;
1326 PushCertificateParser certParser = getPushCertificateParser();
1327 boolean firstPkt = true;
1328 try {
1329 for (;;) {
1330 String line;
1331 try {
1332 line = pck.readString();
1333 } catch (EOFException eof) {
1334 if (commands.isEmpty())
1335 return;
1336 throw eof;
1337 }
1338 if (PacketLineIn.isEnd(line)) {
1339 break;
1340 }
1341
1342 if (line.length() >= 48 && line.startsWith("shallow ")) {
1343 parseShallow(line.substring(8, 48));
1344 continue;
1345 }
1346
1347 if (firstPkt) {
1348 firstPkt = false;
1349 FirstCommand firstLine = FirstCommand.fromLine(line);
1350 enabledCapabilities = firstLine.getCapabilities();
1351 line = firstLine.getLine();
1352 enableCapabilities();
1353
1354 if (line.equals(GitProtocolConstants.OPTION_PUSH_CERT)) {
1355 certParser.receiveHeader(pck, !isBiDirectionalPipe());
1356 continue;
1357 }
1358 }
1359
1360 if (line.equals(PushCertificateParser.BEGIN_SIGNATURE)) {
1361 certParser.receiveSignature(pck);
1362 continue;
1363 }
1364
1365 ReceiveCommand cmd = parseCommand(line);
1366 if (cmd.getRefName().equals(Constants.HEAD)) {
1367 cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
1368 } else {
1369 cmd.setRef(refs.get(cmd.getRefName()));
1370 }
1371 commands.add(cmd);
1372 if (certParser.enabled()) {
1373 certParser.addCommand(cmd);
1374 }
1375 }
1376 pushCert = certParser.build();
1377 if (hasCommands()) {
1378 readPostCommands(pck);
1379 }
1380 } catch (Throwable t) {
1381 discardCommands();
1382 throw t;
1383 }
1384 }
1385
1386 private void discardCommands() {
1387 if (sideBand) {
1388 long max = maxDiscardBytes;
1389 if (max < 0) {
1390 max = Math.max(3 * maxCommandBytes, 3L << 20);
1391 }
1392 try {
1393 new PacketLineIn(rawIn, max).discardUntilEnd();
1394 } catch (IOException e) {
1395
1396 }
1397 }
1398 }
1399
1400 private void parseShallow(String idStr) throws PackProtocolException {
1401 ObjectId id;
1402 try {
1403 id = ObjectId.fromString(idStr);
1404 } catch (InvalidObjectIdException e) {
1405 throw new PackProtocolException(e.getMessage(), e);
1406 }
1407 clientShallowCommits.add(id);
1408 }
1409
1410
1411
1412
1413
1414
1415
1416 void readPostCommands(PacketLineIn in) throws IOException {
1417 if (usePushOptions) {
1418 pushOptions = new ArrayList<>(4);
1419 for (;;) {
1420 String option = in.readString();
1421 if (PacketLineIn.isEnd(option)) {
1422 break;
1423 }
1424 pushOptions.add(option);
1425 }
1426 }
1427 }
1428
1429
1430
1431
1432 private void enableCapabilities() {
1433 reportStatus = isCapabilityEnabled(CAPABILITY_REPORT_STATUS);
1434 usePushOptions = isCapabilityEnabled(CAPABILITY_PUSH_OPTIONS);
1435 sideBand = isCapabilityEnabled(CAPABILITY_SIDE_BAND_64K);
1436 quiet = allowQuiet && isCapabilityEnabled(CAPABILITY_QUIET);
1437 if (sideBand) {
1438 OutputStream out = rawOut;
1439
1440 rawOut = new SideBandOutputStream(CH_DATA, MAX_BUF, out);
1441 msgOut = new SideBandOutputStream(CH_PROGRESS, MAX_BUF, out);
1442 errOut = new SideBandOutputStream(CH_ERROR, MAX_BUF, out);
1443
1444 pckOut = new PacketLineOut(rawOut);
1445 pckOut.setFlushOnEnd(false);
1446 }
1447 }
1448
1449
1450
1451
1452
1453
1454
1455
1456 private boolean isCapabilityEnabled(String name) {
1457 return enabledCapabilities.contains(name);
1458 }
1459
1460 private void checkRequestWasRead() {
1461 if (enabledCapabilities == null)
1462 throw new RequestNotYetReadException();
1463 }
1464
1465
1466
1467
1468
1469
1470 private boolean needPack() {
1471 for (ReceiveCommand cmd : commands) {
1472 if (cmd.getType() != ReceiveCommand.Type.DELETE)
1473 return true;
1474 }
1475 return false;
1476 }
1477
1478
1479
1480
1481
1482
1483
1484 private void receivePack() throws IOException {
1485
1486
1487
1488
1489 if (timeoutIn != null)
1490 timeoutIn.setTimeout(10 * timeout * 1000);
1491
1492 ProgressMonitor receiving = NullProgressMonitor.INSTANCE;
1493 ProgressMonitor resolving = NullProgressMonitor.INSTANCE;
1494 if (sideBand && !quiet)
1495 resolving = new SideBandProgressMonitor(msgOut);
1496
1497 try (ObjectInserter ins = db.newObjectInserter()) {
1498 String lockMsg = "jgit receive-pack";
1499 if (getRefLogIdent() != null)
1500 lockMsg += " from " + getRefLogIdent().toExternalString();
1501
1502 parser = ins.newPackParser(packInputStream());
1503 parser.setAllowThin(true);
1504 parser.setNeedNewObjectIds(checkReferencedAreReachable);
1505 parser.setNeedBaseObjectIds(checkReferencedAreReachable);
1506 parser.setCheckEofAfterPackFooter(!biDirectionalPipe
1507 && !isExpectDataAfterPackFooter());
1508 parser.setExpectDataAfterPackFooter(isExpectDataAfterPackFooter());
1509 parser.setObjectChecker(objectChecker);
1510 parser.setLockMessage(lockMsg);
1511 parser.setMaxObjectSizeLimit(maxObjectSizeLimit);
1512 packLock = parser.parse(receiving, resolving);
1513 packSize = Long.valueOf(parser.getPackSize());
1514 stats = parser.getReceivedPackStatistics();
1515 ins.flush();
1516 }
1517
1518 if (timeoutIn != null)
1519 timeoutIn.setTimeout(timeout * 1000);
1520 }
1521
1522 private InputStream packInputStream() {
1523 InputStream packIn = rawIn;
1524 if (maxPackSizeLimit >= 0) {
1525 packIn = new LimitedInputStream(packIn, maxPackSizeLimit) {
1526 @Override
1527 protected void limitExceeded() throws TooLargePackException {
1528 throw new TooLargePackException(limit);
1529 }
1530 };
1531 }
1532 return packIn;
1533 }
1534
1535 private boolean needCheckConnectivity() {
1536 return isCheckReceivedObjects()
1537 || isCheckReferencedObjectsAreReachable()
1538 || !getClientShallowCommits().isEmpty();
1539 }
1540
1541 private void checkSubmodules() throws IOException, LargeObjectException,
1542 SubmoduleValidationException {
1543 ObjectDatabase odb = db.getObjectDatabase();
1544 if (objectChecker == null) {
1545 return;
1546 }
1547 for (GitmoduleEntry entry : objectChecker.getGitsubmodules()) {
1548 AnyObjectId blobId = entry.getBlobId();
1549 ObjectLoader blob = odb.open(blobId, Constants.OBJ_BLOB);
1550
1551 SubmoduleValidator.assertValidGitModulesFile(
1552 new String(blob.getBytes(), UTF_8));
1553 }
1554 }
1555
1556 private void checkConnectivity() throws IOException {
1557 ProgressMonitor checking = NullProgressMonitor.INSTANCE;
1558 if (sideBand && !quiet) {
1559 SideBandProgressMonitor m = new SideBandProgressMonitor(msgOut);
1560 m.setDelayStart(750, TimeUnit.MILLISECONDS);
1561 checking = m;
1562 }
1563
1564 connectivityChecker.checkConnectivity(createConnectivityCheckInfo(),
1565 advertisedHaves, checking);
1566 }
1567
1568 private ConnectivityCheckInfo createConnectivityCheckInfo() {
1569 ConnectivityCheckInfo info = new ConnectivityCheckInfo();
1570 info.setCheckObjects(checkReferencedAreReachable);
1571 info.setCommands(getAllCommands());
1572 info.setRepository(db);
1573 info.setParser(parser);
1574 info.setWalk(walk);
1575 return info;
1576 }
1577
1578
1579
1580
1581 private void validateCommands() {
1582 for (ReceiveCommand cmd : commands) {
1583 final Ref ref = cmd.getRef();
1584 if (cmd.getResult() != Result.NOT_ATTEMPTED)
1585 continue;
1586
1587 if (cmd.getType() == ReceiveCommand.Type.DELETE) {
1588 if (!isAllowDeletes()) {
1589
1590 cmd.setResult(Result.REJECTED_NODELETE);
1591 continue;
1592 }
1593 if (!isAllowBranchDeletes()
1594 && ref.getName().startsWith(Constants.R_HEADS)) {
1595
1596 cmd.setResult(Result.REJECTED_NODELETE);
1597 continue;
1598 }
1599 }
1600
1601 if (cmd.getType() == ReceiveCommand.Type.CREATE) {
1602 if (!isAllowCreates()) {
1603 cmd.setResult(Result.REJECTED_NOCREATE);
1604 continue;
1605 }
1606
1607 if (ref != null && !isAllowNonFastForwards()) {
1608
1609
1610
1611 cmd.setResult(Result.REJECTED_NONFASTFORWARD);
1612 continue;
1613 }
1614
1615 if (ref != null) {
1616
1617
1618
1619 cmd.setResult(Result.REJECTED_OTHER_REASON,
1620 JGitText.get().refAlreadyExists);
1621 continue;
1622 }
1623 }
1624
1625 if (cmd.getType() == ReceiveCommand.Type.DELETE && ref != null) {
1626 ObjectId id = ref.getObjectId();
1627 if (id == null) {
1628 id = ObjectId.zeroId();
1629 }
1630 if (!ObjectId.zeroId().equals(cmd.getOldId())
1631 && !id.equals(cmd.getOldId())) {
1632
1633
1634
1635
1636 cmd.setResult(Result.REJECTED_OTHER_REASON,
1637 JGitText.get().invalidOldIdSent);
1638 continue;
1639 }
1640 }
1641
1642 if (cmd.getType() == ReceiveCommand.Type.UPDATE) {
1643 if (ref == null) {
1644
1645
1646 cmd.setResult(Result.REJECTED_OTHER_REASON,
1647 JGitText.get().noSuchRef);
1648 continue;
1649 }
1650 ObjectId id = ref.getObjectId();
1651 if (id == null) {
1652
1653 cmd.setResult(Result.REJECTED_OTHER_REASON,
1654 JGitText.get().cannotUpdateUnbornBranch);
1655 continue;
1656 }
1657
1658 if (!id.equals(cmd.getOldId())) {
1659
1660
1661
1662 cmd.setResult(Result.REJECTED_OTHER_REASON,
1663 JGitText.get().invalidOldIdSent);
1664 continue;
1665 }
1666
1667
1668
1669 RevObject oldObj, newObj;
1670 try {
1671 oldObj = walk.parseAny(cmd.getOldId());
1672 } catch (IOException e) {
1673 receiveCommandErrorHandler
1674 .handleOldIdValidationException(cmd, e);
1675 continue;
1676 }
1677
1678 try {
1679 newObj = walk.parseAny(cmd.getNewId());
1680 } catch (IOException e) {
1681 receiveCommandErrorHandler
1682 .handleNewIdValidationException(cmd, e);
1683 continue;
1684 }
1685
1686 if (oldObj instanceof RevCommit
1687 && newObj instanceof RevCommit) {
1688 try {
1689 if (walk.isMergedInto((RevCommit) oldObj,
1690 (RevCommit) newObj)) {
1691 cmd.setTypeFastForwardUpdate();
1692 } else {
1693 cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
1694 }
1695 } catch (IOException e) {
1696 receiveCommandErrorHandler
1697 .handleFastForwardCheckException(cmd, e);
1698 }
1699 } else {
1700 cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
1701 }
1702
1703 if (cmd.getType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD
1704 && !isAllowNonFastForwards()) {
1705 cmd.setResult(Result.REJECTED_NONFASTFORWARD);
1706 continue;
1707 }
1708 }
1709
1710 if (!cmd.getRefName().startsWith(Constants.R_REFS)
1711 || !Repository.isValidRefName(cmd.getRefName())) {
1712 cmd.setResult(Result.REJECTED_OTHER_REASON,
1713 JGitText.get().funnyRefname);
1714 }
1715 }
1716 }
1717
1718
1719
1720
1721
1722
1723 private boolean anyRejects() {
1724 for (ReceiveCommand cmd : commands) {
1725 if (cmd.getResult() != Result.NOT_ATTEMPTED
1726 && cmd.getResult() != Result.OK)
1727 return true;
1728 }
1729 return false;
1730 }
1731
1732
1733
1734
1735
1736 private void failPendingCommands() {
1737 ReceiveCommand.abort(commands);
1738 }
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749 protected List<ReceiveCommand> filterCommands(Result want) {
1750 return ReceiveCommand.filter(commands, want);
1751 }
1752
1753
1754
1755
1756
1757 protected void executeCommands() {
1758 List<ReceiveCommand> toApply = filterCommands(Result.NOT_ATTEMPTED);
1759 if (toApply.isEmpty())
1760 return;
1761
1762 ProgressMonitor updating = NullProgressMonitor.INSTANCE;
1763 if (sideBand) {
1764 SideBandProgressMonitor pm = new SideBandProgressMonitor(msgOut);
1765 pm.setDelayStart(250, TimeUnit.MILLISECONDS);
1766 updating = pm;
1767 }
1768
1769 BatchRefUpdate batch = db.getRefDatabase().newBatchUpdate();
1770 batch.setAllowNonFastForwards(isAllowNonFastForwards());
1771 batch.setAtomic(isAtomic());
1772 batch.setRefLogIdent(getRefLogIdent());
1773 batch.setRefLogMessage("push", true);
1774 batch.addCommand(toApply);
1775 try {
1776 batch.setPushCertificate(getPushCertificate());
1777 batch.execute(walk, updating);
1778 } catch (IOException e) {
1779 receiveCommandErrorHandler.handleBatchRefUpdateException(toApply,
1780 e);
1781 }
1782 }
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793 private void sendStatusReport(Throwable unpackError) throws IOException {
1794 Reporter out = new Reporter() {
1795 @Override
1796 void sendString(String s) throws IOException {
1797 if (reportStatus) {
1798 pckOut.writeString(s + "\n");
1799 } else if (msgOut != null) {
1800 msgOut.write(Constants.encode(s + "\n"));
1801 }
1802 }
1803 };
1804
1805 try {
1806 if (unpackError != null) {
1807 out.sendString("unpack error " + unpackError.getMessage());
1808 if (reportStatus) {
1809 for (ReceiveCommand cmd : commands) {
1810 out.sendString("ng " + cmd.getRefName()
1811 + " n/a (unpacker error)");
1812 }
1813 }
1814 return;
1815 }
1816
1817 if (reportStatus) {
1818 out.sendString("unpack ok");
1819 }
1820 for (ReceiveCommand cmd : commands) {
1821 if (cmd.getResult() == Result.OK) {
1822 if (reportStatus) {
1823 out.sendString("ok " + cmd.getRefName());
1824 }
1825 continue;
1826 }
1827
1828 final StringBuilder r = new StringBuilder();
1829 if (reportStatus) {
1830 r.append("ng ").append(cmd.getRefName()).append(" ");
1831 } else {
1832 r.append(" ! [rejected] ").append(cmd.getRefName())
1833 .append(" (");
1834 }
1835
1836 if (cmd.getResult() == Result.REJECTED_MISSING_OBJECT) {
1837 if (cmd.getMessage() == null)
1838 r.append("missing object(s)");
1839 else if (cmd.getMessage()
1840 .length() == Constants.OBJECT_ID_STRING_LENGTH) {
1841
1842
1843 r.append("object ");
1844 r.append(cmd.getMessage());
1845 r.append(" missing");
1846 } else {
1847 r.append(cmd.getMessage());
1848 }
1849 } else if (cmd.getMessage() != null) {
1850 r.append(cmd.getMessage());
1851 } else {
1852 switch (cmd.getResult()) {
1853 case NOT_ATTEMPTED:
1854 r.append("server bug; ref not processed");
1855 break;
1856
1857 case REJECTED_NOCREATE:
1858 r.append("creation prohibited");
1859 break;
1860
1861 case REJECTED_NODELETE:
1862 r.append("deletion prohibited");
1863 break;
1864
1865 case REJECTED_NONFASTFORWARD:
1866 r.append("non-fast forward");
1867 break;
1868
1869 case REJECTED_CURRENT_BRANCH:
1870 r.append("branch is currently checked out");
1871 break;
1872
1873 case REJECTED_OTHER_REASON:
1874 r.append("unspecified reason");
1875 break;
1876
1877 case LOCK_FAILURE:
1878 r.append("failed to lock");
1879 break;
1880
1881 case REJECTED_MISSING_OBJECT:
1882 case OK:
1883
1884
1885 throw new AssertionError();
1886 }
1887 }
1888
1889 if (!reportStatus) {
1890 r.append(")");
1891 }
1892 out.sendString(r.toString());
1893 }
1894 } finally {
1895 if (reportStatus) {
1896 pckOut.end();
1897 }
1898 }
1899 }
1900
1901
1902
1903
1904
1905
1906 private void close() throws IOException {
1907 if (sideBand) {
1908
1909
1910
1911
1912
1913
1914 ((SideBandOutputStream) msgOut).flushBuffer();
1915 ((SideBandOutputStream) rawOut).flushBuffer();
1916
1917 PacketLineOut plo = new PacketLineOut(origOut);
1918 plo.setFlushOnEnd(false);
1919 plo.end();
1920 }
1921
1922 if (biDirectionalPipe) {
1923
1924
1925
1926
1927 if (!sideBand && msgOut != null)
1928 msgOut.flush();
1929 rawOut.flush();
1930 }
1931 }
1932
1933
1934
1935
1936
1937
1938
1939 private void release() throws IOException {
1940 walk.close();
1941 unlockPack();
1942 timeoutIn = null;
1943 rawIn = null;
1944 rawOut = null;
1945 msgOut = null;
1946 pckIn = null;
1947 pckOut = null;
1948 refs = null;
1949
1950
1951
1952 commands = null;
1953 if (timer != null) {
1954 try {
1955 timer.terminate();
1956 } finally {
1957 timer = null;
1958 }
1959 }
1960 }
1961
1962
1963 abstract static class Reporter {
1964 abstract void sendString(String s) throws IOException;
1965 }
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976 public PushCertificate getPushCertificate() {
1977 return pushCert;
1978 }
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990 public void setPushCertificate(PushCertificate cert) {
1991 pushCert = cert;
1992 }
1993
1994
1995
1996
1997
1998
1999
2000 @Nullable
2001 public List<String> getPushOptions() {
2002 if (isAllowPushOptions() && usePushOptions) {
2003 return Collections.unmodifiableList(pushOptions);
2004 }
2005
2006
2007
2008
2009 return null;
2010 }
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025 public void setPushOptions(@Nullable List<String> options) {
2026 usePushOptions = options != null;
2027 pushOptions = options;
2028 }
2029
2030
2031
2032
2033
2034
2035 public PreReceiveHook getPreReceiveHook() {
2036 return preReceive;
2037 }
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054 public void setPreReceiveHook(PreReceiveHook h) {
2055 preReceive = h != null ? h : PreReceiveHook.NULL;
2056 }
2057
2058
2059
2060
2061
2062
2063 public PostReceiveHook getPostReceiveHook() {
2064 return postReceive;
2065 }
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078 public void setPostReceiveHook(PostReceiveHook h) {
2079 postReceive = h != null ? h : PostReceiveHook.NULL;
2080 }
2081
2082
2083
2084
2085
2086
2087
2088 public UnpackErrorHandler getUnpackErrorHandler() {
2089 return unpackErrorHandler;
2090 }
2091
2092
2093
2094
2095
2096
2097 public void setUnpackErrorHandler(UnpackErrorHandler unpackErrorHandler) {
2098 this.unpackErrorHandler = unpackErrorHandler;
2099 }
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112 @Deprecated
2113 public void setEchoCommandFailures(boolean echo) {
2114
2115 }
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135 public void receive(final InputStream input, final OutputStream output,
2136 final OutputStream messages) throws IOException {
2137 init(input, output, messages);
2138 try {
2139 service();
2140 } catch (PackProtocolException e) {
2141 fatalError(e.getMessage());
2142 throw e;
2143 } catch (InputOverLimitIOException e) {
2144 String msg = JGitText.get().tooManyCommands;
2145 fatalError(msg);
2146 throw new PackProtocolException(msg, e);
2147 } finally {
2148 try {
2149 close();
2150 } finally {
2151 release();
2152 }
2153 }
2154 }
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179 public void receiveWithExceptionPropagation(InputStream input,
2180 OutputStream output, OutputStream messages) throws IOException {
2181 init(input, output, messages);
2182 try {
2183 service();
2184 } finally {
2185 try {
2186 close();
2187 } finally {
2188 release();
2189 }
2190 }
2191 }
2192
2193 private void service() throws IOException {
2194 if (isBiDirectionalPipe()) {
2195 sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
2196 pckOut.flush();
2197 } else
2198 getAdvertisedOrDefaultRefs();
2199 if (hasError())
2200 return;
2201
2202 recvCommands();
2203
2204 if (hasCommands()) {
2205 try (PostReceiveExecutor e = new PostReceiveExecutor()) {
2206 if (needPack()) {
2207 try {
2208 receivePackAndCheckConnectivity();
2209 } catch (IOException | RuntimeException
2210 | SubmoduleValidationException | Error err) {
2211 unlockPack();
2212 unpackErrorHandler.handleUnpackException(err);
2213 throw new UnpackException(err);
2214 }
2215 }
2216
2217 try {
2218 setAtomic(isCapabilityEnabled(CAPABILITY_ATOMIC));
2219
2220 validateCommands();
2221 if (atomic && anyRejects()) {
2222 failPendingCommands();
2223 }
2224
2225 preReceive.onPreReceive(
2226 this, filterCommands(Result.NOT_ATTEMPTED));
2227 if (atomic && anyRejects()) {
2228 failPendingCommands();
2229 }
2230 executeCommands();
2231 } finally {
2232 unlockPack();
2233 }
2234
2235 sendStatusReport(null);
2236 }
2237 autoGc();
2238 }
2239 }
2240
2241 private void autoGc() {
2242 Repository repo = getRepository();
2243 if (!repo.getConfig().getBoolean(ConfigConstants.CONFIG_RECEIVE_SECTION,
2244 ConfigConstants.CONFIG_KEY_AUTOGC, true)) {
2245 return;
2246 }
2247 repo.autoGC(NullProgressMonitor.INSTANCE);
2248 }
2249
2250 static ReceiveCommand parseCommand(String line)
2251 throws PackProtocolException {
2252 if (line == null || line.length() < 83) {
2253 throw new PackProtocolException(
2254 JGitText.get().errorInvalidProtocolWantedOldNewRef);
2255 }
2256 String oldStr = line.substring(0, 40);
2257 String newStr = line.substring(41, 81);
2258 ObjectId oldId, newId;
2259 try {
2260 oldId = ObjectId.fromString(oldStr);
2261 newId = ObjectId.fromString(newStr);
2262 } catch (InvalidObjectIdException e) {
2263 throw new PackProtocolException(
2264 JGitText.get().errorInvalidProtocolWantedOldNewRef, e);
2265 }
2266 String name = line.substring(82);
2267 if (!Repository.isValidRefName(name)) {
2268 throw new PackProtocolException(
2269 JGitText.get().errorInvalidProtocolWantedOldNewRef);
2270 }
2271 return new ReceiveCommand(oldId, newId, name);
2272 }
2273
2274 private class PostReceiveExecutor implements AutoCloseable {
2275 @Override
2276 public void close() {
2277 postReceive.onPostReceive(ReceivePack.this,
2278 filterCommands(Result.OK));
2279 }
2280 }
2281
2282 private class DefaultUnpackErrorHandler implements UnpackErrorHandler {
2283 @Override
2284 public void handleUnpackException(Throwable t) throws IOException {
2285 sendStatusReport(t);
2286 }
2287 }
2288 }