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.transport;
45
46 import static java.nio.charset.StandardCharsets.UTF_8;
47 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_ATOMIC;
48 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_DELETE_REFS;
49 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_OFS_DELTA;
50 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_PUSH_OPTIONS;
51 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_QUIET;
52 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS;
53 import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SIDE_BAND_64K;
54 import static org.eclipse.jgit.transport.GitProtocolConstants.OPTION_AGENT;
55 import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA;
56 import static org.eclipse.jgit.transport.SideBandOutputStream.CH_ERROR;
57 import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS;
58 import static org.eclipse.jgit.transport.SideBandOutputStream.MAX_BUF;
59
60 import java.io.EOFException;
61 import java.io.IOException;
62 import java.io.InputStream;
63 import java.io.OutputStream;
64 import java.text.MessageFormat;
65 import java.util.ArrayList;
66 import java.util.Collections;
67 import java.util.HashSet;
68 import java.util.List;
69 import java.util.Map;
70 import java.util.Set;
71 import java.util.concurrent.TimeUnit;
72
73 import org.eclipse.jgit.annotations.Nullable;
74 import org.eclipse.jgit.errors.InvalidObjectIdException;
75 import org.eclipse.jgit.errors.LargeObjectException;
76 import org.eclipse.jgit.errors.MissingObjectException;
77 import org.eclipse.jgit.errors.PackProtocolException;
78 import org.eclipse.jgit.errors.TooLargePackException;
79 import org.eclipse.jgit.internal.JGitText;
80 import org.eclipse.jgit.internal.storage.file.PackLock;
81 import org.eclipse.jgit.internal.submodule.SubmoduleValidator;
82 import org.eclipse.jgit.internal.submodule.SubmoduleValidator.SubmoduleValidationException;
83 import org.eclipse.jgit.internal.transport.parser.FirstCommand;
84 import org.eclipse.jgit.lib.AnyObjectId;
85 import org.eclipse.jgit.lib.BatchRefUpdate;
86 import org.eclipse.jgit.lib.Config;
87 import org.eclipse.jgit.lib.Constants;
88 import org.eclipse.jgit.lib.GitmoduleEntry;
89 import org.eclipse.jgit.lib.NullProgressMonitor;
90 import org.eclipse.jgit.lib.ObjectChecker;
91 import org.eclipse.jgit.lib.ObjectDatabase;
92 import org.eclipse.jgit.lib.ObjectId;
93 import org.eclipse.jgit.lib.ObjectIdSubclassMap;
94 import org.eclipse.jgit.lib.ObjectInserter;
95 import org.eclipse.jgit.lib.ObjectLoader;
96 import org.eclipse.jgit.lib.PersonIdent;
97 import org.eclipse.jgit.lib.ProgressMonitor;
98 import org.eclipse.jgit.lib.Ref;
99 import org.eclipse.jgit.lib.Repository;
100 import org.eclipse.jgit.revwalk.ObjectWalk;
101 import org.eclipse.jgit.revwalk.RevBlob;
102 import org.eclipse.jgit.revwalk.RevCommit;
103 import org.eclipse.jgit.revwalk.RevFlag;
104 import org.eclipse.jgit.revwalk.RevObject;
105 import org.eclipse.jgit.revwalk.RevSort;
106 import org.eclipse.jgit.revwalk.RevTree;
107 import org.eclipse.jgit.revwalk.RevWalk;
108 import org.eclipse.jgit.transport.PacketLineIn.InputOverLimitIOException;
109 import org.eclipse.jgit.transport.ReceiveCommand.Result;
110 import org.eclipse.jgit.util.io.InterruptTimer;
111 import org.eclipse.jgit.util.io.LimitedInputStream;
112 import org.eclipse.jgit.util.io.TimeoutInputStream;
113 import org.eclipse.jgit.util.io.TimeoutOutputStream;
114
115
116
117
118
119
120
121
122 public abstract class BaseReceivePack {
123
124
125
126
127
128 @Deprecated
129 public static class FirstLine {
130 private final FirstCommand command;
131
132
133
134
135
136
137
138 public FirstLine(String line) {
139 command = FirstCommand.fromLine(line);
140 }
141
142
143 public String getLine() {
144 return command.getLine();
145 }
146
147
148 public Set<String> getCapabilities() {
149 return command.getCapabilities();
150 }
151 }
152
153
154 final Repository db;
155
156
157 final RevWalk walk;
158
159
160
161
162
163
164
165
166
167
168
169
170 private boolean biDirectionalPipe = true;
171
172
173 private boolean expectDataAfterPackFooter;
174
175
176 private ObjectChecker objectChecker;
177
178
179 private boolean allowCreates;
180
181
182 private boolean allowAnyDeletes;
183 private boolean allowBranchDeletes;
184
185
186 private boolean allowNonFastForwards;
187
188
189 private boolean allowPushOptions;
190
191
192
193
194
195 private boolean atomic;
196
197 private boolean allowOfsDelta;
198 private boolean allowQuiet = true;
199
200
201 private PersonIdent refLogIdent;
202
203
204 private AdvertiseRefsHook advertiseRefsHook;
205
206
207 RefFilter refFilter;
208
209
210 private int timeout;
211
212
213 private InterruptTimer timer;
214
215 private TimeoutInputStream timeoutIn;
216
217
218
219 private OutputStream origOut;
220
221
222 protected InputStream rawIn;
223
224
225 protected OutputStream rawOut;
226
227
228 protected OutputStream msgOut;
229 private SideBandOutputStream errOut;
230
231
232 protected PacketLineIn pckIn;
233
234
235 protected PacketLineOut pckOut;
236
237 private final MessageOutputWrapper msgOutWrapper = new MessageOutputWrapper();
238
239 private PackParser parser;
240
241
242 Map<String, Ref> refs;
243
244
245 Set<ObjectId> advertisedHaves;
246
247
248 private Set<String> enabledCapabilities;
249 String userAgent;
250 private Set<ObjectId> clientShallowCommits;
251 private List<ReceiveCommand> commands;
252 private long maxCommandBytes;
253 private long maxDiscardBytes;
254
255 private StringBuilder advertiseError;
256
257
258 private boolean sideBand;
259
260 private boolean quiet;
261
262
263 private PackLock packLock;
264
265 private boolean checkReferencedIsReachable;
266
267
268 private long maxObjectSizeLimit;
269
270
271 private long maxPackSizeLimit = -1;
272
273
274 private Long packSize;
275
276 private PushCertificateParser pushCertificateParser;
277 private SignedPushConfig signedPushConfig;
278 PushCertificate pushCert;
279 private ReceivedPackStatistics stats;
280
281
282
283
284
285
286
287
288
289
290
291 @Deprecated
292 public abstract PushCertificate getPushCertificate();
293
294
295
296
297
298
299
300
301
302
303
304
305 @Deprecated
306 public abstract void setPushCertificate(PushCertificate cert);
307
308
309
310
311
312
313
314 protected BaseReceivePack(Repository into) {
315 db = into;
316 walk = new RevWalk(db);
317
318 TransferConfig tc = db.getConfig().get(TransferConfig.KEY);
319 objectChecker = tc.newReceiveObjectChecker();
320
321 ReceiveConfig rc = db.getConfig().get(ReceiveConfig::new);
322 allowCreates = rc.allowCreates;
323 allowAnyDeletes = true;
324 allowBranchDeletes = rc.allowDeletes;
325 allowNonFastForwards = rc.allowNonFastForwards;
326 allowOfsDelta = rc.allowOfsDelta;
327 allowPushOptions = rc.allowPushOptions;
328 maxCommandBytes = rc.maxCommandBytes;
329 maxDiscardBytes = rc.maxDiscardBytes;
330 advertiseRefsHook = AdvertiseRefsHook.DEFAULT;
331 refFilter = RefFilter.DEFAULT;
332 advertisedHaves = new HashSet<>();
333 clientShallowCommits = new HashSet<>();
334 signedPushConfig = rc.signedPush;
335 }
336
337
338 protected static class ReceiveConfig {
339 final boolean allowCreates;
340 final boolean allowDeletes;
341 final boolean allowNonFastForwards;
342 final boolean allowOfsDelta;
343 final boolean allowPushOptions;
344 final long maxCommandBytes;
345 final long maxDiscardBytes;
346 final SignedPushConfig signedPush;
347
348 ReceiveConfig(Config config) {
349 allowCreates = true;
350 allowDeletes = !config.getBoolean("receive", "denydeletes", false);
351 allowNonFastForwards = !config.getBoolean("receive",
352 "denynonfastforwards", false);
353 allowOfsDelta = config.getBoolean("repack", "usedeltabaseoffset",
354 true);
355 allowPushOptions = config.getBoolean("receive", "pushoptions",
356 false);
357 maxCommandBytes = config.getLong("receive",
358 "maxCommandBytes",
359 3 << 20);
360 maxDiscardBytes = config.getLong("receive",
361 "maxCommandDiscardBytes",
362 -1);
363 signedPush = SignedPushConfig.KEY.parse(config);
364 }
365 }
366
367
368
369
370
371
372
373 class MessageOutputWrapper extends OutputStream {
374 @Override
375 public void write(int ch) {
376 if (msgOut != null) {
377 try {
378 msgOut.write(ch);
379 } catch (IOException e) {
380
381 }
382 }
383 }
384
385 @Override
386 public void write(byte[] b, int off, int len) {
387 if (msgOut != null) {
388 try {
389 msgOut.write(b, off, len);
390 } catch (IOException e) {
391
392 }
393 }
394 }
395
396 @Override
397 public void write(byte[] b) {
398 write(b, 0, b.length);
399 }
400
401 @Override
402 public void flush() {
403 if (msgOut != null) {
404 try {
405 msgOut.flush();
406 } catch (IOException e) {
407
408 }
409 }
410 }
411 }
412
413
414
415
416
417
418 protected abstract String getLockMessageProcessName();
419
420
421
422
423
424
425
426 @Deprecated
427 public abstract Repository getRepository();
428
429
430
431
432
433
434
435 @Deprecated
436 public abstract RevWalk getRevWalk();
437
438
439
440
441
442
443
444
445 @Deprecated
446 public abstract Map<String, Ref> getAdvertisedRefs();
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466 @Deprecated
467 public abstract void setAdvertisedRefs(Map<String, Ref> allRefs, Set<ObjectId> additionalHaves);
468
469
470
471
472
473
474
475
476 public final Set<ObjectId> getAdvertisedObjects() {
477 return advertisedHaves;
478 }
479
480
481
482
483
484
485
486
487
488 public boolean isCheckReferencedObjectsAreReachable() {
489 return checkReferencedIsReachable;
490 }
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513 public void setCheckReferencedObjectsAreReachable(boolean b) {
514 this.checkReferencedIsReachable = b;
515 }
516
517
518
519
520
521
522
523
524 public boolean isBiDirectionalPipe() {
525 return biDirectionalPipe;
526 }
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541 public void setBiDirectionalPipe(boolean twoWay) {
542 biDirectionalPipe = twoWay;
543 }
544
545
546
547
548
549
550 public boolean isExpectDataAfterPackFooter() {
551 return expectDataAfterPackFooter;
552 }
553
554
555
556
557
558
559
560
561 public void setExpectDataAfterPackFooter(boolean e) {
562 expectDataAfterPackFooter = e;
563 }
564
565
566
567
568
569
570
571
572
573 public boolean isCheckReceivedObjects() {
574 return objectChecker != null;
575 }
576
577
578
579
580
581
582
583
584
585 public void setCheckReceivedObjects(boolean check) {
586 if (check && objectChecker == null)
587 setObjectChecker(new ObjectChecker());
588 else if (!check && objectChecker != null)
589 setObjectChecker(null);
590 }
591
592
593
594
595
596
597
598
599
600 public void setObjectChecker(ObjectChecker impl) {
601 objectChecker = impl;
602 }
603
604
605
606
607
608
609 public boolean isAllowCreates() {
610 return allowCreates;
611 }
612
613
614
615
616
617
618
619 public void setAllowCreates(boolean canCreate) {
620 allowCreates = canCreate;
621 }
622
623
624
625
626
627
628 public boolean isAllowDeletes() {
629 return allowAnyDeletes;
630 }
631
632
633
634
635
636
637
638 public void setAllowDeletes(boolean canDelete) {
639 allowAnyDeletes = canDelete;
640 }
641
642
643
644
645
646
647
648 public boolean isAllowBranchDeletes() {
649 return allowBranchDeletes;
650 }
651
652
653
654
655
656
657
658
659
660
661 public void setAllowBranchDeletes(boolean canDelete) {
662 allowBranchDeletes = canDelete;
663 }
664
665
666
667
668
669
670
671
672 public boolean isAllowNonFastForwards() {
673 return allowNonFastForwards;
674 }
675
676
677
678
679
680
681
682
683
684 public void setAllowNonFastForwards(boolean canRewind) {
685 allowNonFastForwards = canRewind;
686 }
687
688
689
690
691
692
693
694
695
696 public boolean isAtomic() {
697 return atomic;
698 }
699
700
701
702
703
704
705
706
707
708
709 public void setAtomic(boolean atomic) {
710 this.atomic = atomic;
711 }
712
713
714
715
716
717
718 public PersonIdent getRefLogIdent() {
719 return refLogIdent;
720 }
721
722
723
724
725
726
727
728
729
730
731
732
733
734 public void setRefLogIdent(PersonIdent pi) {
735 refLogIdent = pi;
736 }
737
738
739
740
741
742
743 public AdvertiseRefsHook getAdvertiseRefsHook() {
744 return advertiseRefsHook;
745 }
746
747
748
749
750
751
752 public RefFilter getRefFilter() {
753 return refFilter;
754 }
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771 public void setAdvertiseRefsHook(AdvertiseRefsHook advertiseRefsHook) {
772 if (advertiseRefsHook != null)
773 this.advertiseRefsHook = advertiseRefsHook;
774 else
775 this.advertiseRefsHook = AdvertiseRefsHook.DEFAULT;
776 }
777
778
779
780
781
782
783
784
785
786
787
788 public void setRefFilter(RefFilter refFilter) {
789 this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
790 }
791
792
793
794
795
796
797 public int getTimeout() {
798 return timeout;
799 }
800
801
802
803
804
805
806
807
808
809 public void setTimeout(int seconds) {
810 timeout = seconds;
811 }
812
813
814
815
816
817
818
819
820 public void setMaxCommandBytes(long limit) {
821 maxCommandBytes = limit;
822 }
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841 public void setMaxCommandDiscardBytes(long limit) {
842 maxDiscardBytes = limit;
843 }
844
845
846
847
848
849
850
851
852
853
854 public void setMaxObjectSizeLimit(long limit) {
855 maxObjectSizeLimit = limit;
856 }
857
858
859
860
861
862
863
864
865
866
867 public void setMaxPackSizeLimit(long limit) {
868 if (limit < 0)
869 throw new IllegalArgumentException(MessageFormat.format(
870 JGitText.get().receivePackInvalidLimit, 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, signedPushConfig);
967 }
968 return pushCertificateParser;
969 }
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986 public String getPeerUserAgent() {
987 return UserAgent.getAgent(enabledCapabilities, userAgent);
988 }
989
990
991
992
993
994
995 public List<ReceiveCommand> getAllCommands() {
996 return Collections.unmodifiableList(commands);
997 }
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 public void sendError(String what) {
1025 if (refs == null) {
1026 if (advertiseError == null)
1027 advertiseError = new StringBuilder();
1028 advertiseError.append(what).append('\n');
1029 } else {
1030 msgOutWrapper.write(Constants.encode("error: " + what + "\n"));
1031 }
1032 }
1033
1034 private void fatalError(String msg) {
1035 if (errOut != null) {
1036 try {
1037 errOut.write(Constants.encode(msg));
1038 errOut.flush();
1039 } catch (IOException e) {
1040
1041 }
1042 } else {
1043 sendError(msg);
1044 }
1045 }
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 public void sendMessage(String what) {
1058 msgOutWrapper.write(Constants.encode(what + "\n"));
1059 }
1060
1061
1062
1063
1064
1065
1066 public OutputStream getMessageOutputStream() {
1067 return msgOutWrapper;
1068 }
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080 public long getPackSize() {
1081 if (packSize != null)
1082 return packSize.longValue();
1083 throw new IllegalStateException(JGitText.get().packSizeNotSetYet);
1084 }
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 protected Set<ObjectId> getClientShallowCommits() {
1095 return clientShallowCommits;
1096 }
1097
1098
1099
1100
1101
1102
1103 protected boolean hasCommands() {
1104 return !commands.isEmpty();
1105 }
1106
1107
1108
1109
1110
1111
1112 protected boolean hasError() {
1113 return advertiseError != null;
1114 }
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133 protected void init(final InputStream input, final OutputStream output,
1134 final OutputStream messages) {
1135 origOut = output;
1136 rawIn = input;
1137 rawOut = output;
1138 msgOut = messages;
1139
1140 if (timeout > 0) {
1141 final Thread caller = Thread.currentThread();
1142 timer = new InterruptTimer(caller.getName() + "-Timer");
1143 timeoutIn = new TimeoutInputStream(rawIn, timer);
1144 TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
1145 timeoutIn.setTimeout(timeout * 1000);
1146 o.setTimeout(timeout * 1000);
1147 rawIn = timeoutIn;
1148 rawOut = o;
1149 }
1150
1151 pckIn = new PacketLineIn(rawIn);
1152 pckOut = new PacketLineOut(rawOut);
1153 pckOut.setFlushOnEnd(false);
1154
1155 enabledCapabilities = new HashSet<>();
1156 commands = new ArrayList<>();
1157 }
1158
1159
1160
1161
1162
1163
1164 protected Map<String, Ref> getAdvertisedOrDefaultRefs() {
1165 if (refs == null)
1166 setAdvertisedRefs(null, null);
1167 return refs;
1168 }
1169
1170
1171
1172
1173
1174
1175
1176 protected void receivePackAndCheckConnectivity() throws IOException {
1177 receivePack();
1178 if (needCheckConnectivity()) {
1179 checkSubmodules();
1180 checkConnectivity();
1181 }
1182 parser = null;
1183 }
1184
1185
1186
1187
1188
1189
1190
1191 protected void unlockPack() throws IOException {
1192 if (packLock != null) {
1193 packLock.unlock();
1194 packLock = null;
1195 }
1196 }
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208 public void sendAdvertisedRefs(RefAdvertiser adv)
1209 throws IOException, ServiceMayNotContinueException {
1210 if (advertiseError != null) {
1211 adv.writeOne("ERR " + advertiseError);
1212 return;
1213 }
1214
1215 try {
1216 advertiseRefsHook.advertiseRefs(this);
1217 } catch (ServiceMayNotContinueException fail) {
1218 if (fail.getMessage() != null) {
1219 adv.writeOne("ERR " + fail.getMessage());
1220 fail.setOutput();
1221 }
1222 throw fail;
1223 }
1224
1225 adv.init(db);
1226 adv.advertiseCapability(CAPABILITY_SIDE_BAND_64K);
1227 adv.advertiseCapability(CAPABILITY_DELETE_REFS);
1228 adv.advertiseCapability(CAPABILITY_REPORT_STATUS);
1229 if (allowQuiet)
1230 adv.advertiseCapability(CAPABILITY_QUIET);
1231 String nonce = getPushCertificateParser().getAdvertiseNonce();
1232 if (nonce != null) {
1233 adv.advertiseCapability(nonce);
1234 }
1235 if (db.getRefDatabase().performsAtomicTransactions())
1236 adv.advertiseCapability(CAPABILITY_ATOMIC);
1237 if (allowOfsDelta)
1238 adv.advertiseCapability(CAPABILITY_OFS_DELTA);
1239 if (allowPushOptions) {
1240 adv.advertiseCapability(CAPABILITY_PUSH_OPTIONS);
1241 }
1242 adv.advertiseCapability(OPTION_AGENT, UserAgent.get());
1243 adv.send(getAdvertisedOrDefaultRefs());
1244 for (ObjectId obj : advertisedHaves)
1245 adv.advertiseHave(obj);
1246 if (adv.isEmpty())
1247 adv.advertiseId(ObjectId.zeroId(), "capabilities^{}");
1248 adv.end();
1249 }
1250
1251
1252
1253
1254
1255
1256
1257
1258 @Nullable
1259 public ReceivedPackStatistics getReceivedPackStatistics() {
1260 return stats;
1261 }
1262
1263
1264
1265
1266
1267
1268 protected void recvCommands() throws IOException {
1269 PacketLineIn pck = maxCommandBytes > 0
1270 ? new PacketLineIn(rawIn, maxCommandBytes)
1271 : pckIn;
1272 PushCertificateParser certParser = getPushCertificateParser();
1273 boolean firstPkt = true;
1274 try {
1275 for (;;) {
1276 String line;
1277 try {
1278 line = pck.readString();
1279 } catch (EOFException eof) {
1280 if (commands.isEmpty())
1281 return;
1282 throw eof;
1283 }
1284 if (line == PacketLineIn.END) {
1285 break;
1286 }
1287
1288 if (line.length() >= 48 && line.startsWith("shallow ")) {
1289 parseShallow(line.substring(8, 48));
1290 continue;
1291 }
1292
1293 if (firstPkt) {
1294 firstPkt = false;
1295 FirstCommand firstLine = FirstCommand.fromLine(line);
1296 enabledCapabilities = firstLine.getCapabilities();
1297 line = firstLine.getLine();
1298 enableCapabilities();
1299
1300 if (line.equals(GitProtocolConstants.OPTION_PUSH_CERT)) {
1301 certParser.receiveHeader(pck, !isBiDirectionalPipe());
1302 continue;
1303 }
1304 }
1305
1306 if (line.equals(PushCertificateParser.BEGIN_SIGNATURE)) {
1307 certParser.receiveSignature(pck);
1308 continue;
1309 }
1310
1311 ReceiveCommand cmd = parseCommand(line);
1312 if (cmd.getRefName().equals(Constants.HEAD)) {
1313 cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
1314 } else {
1315 cmd.setRef(refs.get(cmd.getRefName()));
1316 }
1317 commands.add(cmd);
1318 if (certParser.enabled()) {
1319 certParser.addCommand(cmd);
1320 }
1321 }
1322 pushCert = certParser.build();
1323 if (hasCommands()) {
1324 readPostCommands(pck);
1325 }
1326 } catch (PackProtocolException e) {
1327 discardCommands();
1328 fatalError(e.getMessage());
1329 throw e;
1330 } catch (InputOverLimitIOException e) {
1331 String msg = JGitText.get().tooManyCommands;
1332 discardCommands();
1333 fatalError(msg);
1334 throw new PackProtocolException(msg);
1335 }
1336 }
1337
1338 private void discardCommands() {
1339 if (sideBand) {
1340 long max = maxDiscardBytes;
1341 if (max < 0) {
1342 max = Math.max(3 * maxCommandBytes, 3L << 20);
1343 }
1344 try {
1345 new PacketLineIn(rawIn, max).discardUntilEnd();
1346 } catch (IOException e) {
1347
1348 }
1349 }
1350 }
1351
1352 private void parseShallow(String idStr) throws PackProtocolException {
1353 ObjectId id;
1354 try {
1355 id = ObjectId.fromString(idStr);
1356 } catch (InvalidObjectIdException e) {
1357 throw new PackProtocolException(e.getMessage(), e);
1358 }
1359 clientShallowCommits.add(id);
1360 }
1361
1362 static ReceiveCommand parseCommand(String line) throws PackProtocolException {
1363 if (line == null || line.length() < 83) {
1364 throw new PackProtocolException(
1365 JGitText.get().errorInvalidProtocolWantedOldNewRef);
1366 }
1367 String oldStr = line.substring(0, 40);
1368 String newStr = line.substring(41, 81);
1369 ObjectId oldId, newId;
1370 try {
1371 oldId = ObjectId.fromString(oldStr);
1372 newId = ObjectId.fromString(newStr);
1373 } catch (InvalidObjectIdException e) {
1374 throw new PackProtocolException(
1375 JGitText.get().errorInvalidProtocolWantedOldNewRef, e);
1376 }
1377 String name = line.substring(82);
1378 if (!Repository.isValidRefName(name)) {
1379 throw new PackProtocolException(
1380 JGitText.get().errorInvalidProtocolWantedOldNewRef);
1381 }
1382 return new ReceiveCommand(oldId, newId, name);
1383 }
1384
1385
1386
1387
1388
1389
1390
1391 void readPostCommands(PacketLineIn in) throws IOException {
1392
1393 }
1394
1395
1396
1397
1398 protected void enableCapabilities() {
1399 sideBand = isCapabilityEnabled(CAPABILITY_SIDE_BAND_64K);
1400 quiet = allowQuiet && isCapabilityEnabled(CAPABILITY_QUIET);
1401 if (sideBand) {
1402 OutputStream out = rawOut;
1403
1404 rawOut = new SideBandOutputStream(CH_DATA, MAX_BUF, out);
1405 msgOut = new SideBandOutputStream(CH_PROGRESS, MAX_BUF, out);
1406 errOut = new SideBandOutputStream(CH_ERROR, MAX_BUF, out);
1407
1408 pckOut = new PacketLineOut(rawOut);
1409 pckOut.setFlushOnEnd(false);
1410 }
1411 }
1412
1413
1414
1415
1416
1417
1418
1419
1420 protected boolean isCapabilityEnabled(String name) {
1421 return enabledCapabilities.contains(name);
1422 }
1423
1424 void checkRequestWasRead() {
1425 if (enabledCapabilities == null)
1426 throw new RequestNotYetReadException();
1427 }
1428
1429
1430
1431
1432
1433
1434 protected boolean needPack() {
1435 for (ReceiveCommand cmd : commands) {
1436 if (cmd.getType() != ReceiveCommand.Type.DELETE)
1437 return true;
1438 }
1439 return false;
1440 }
1441
1442
1443
1444
1445
1446
1447
1448 private void receivePack() throws IOException {
1449
1450
1451
1452
1453 if (timeoutIn != null)
1454 timeoutIn.setTimeout(10 * timeout * 1000);
1455
1456 ProgressMonitor receiving = NullProgressMonitor.INSTANCE;
1457 ProgressMonitor resolving = NullProgressMonitor.INSTANCE;
1458 if (sideBand && !quiet)
1459 resolving = new SideBandProgressMonitor(msgOut);
1460
1461 try (ObjectInserter ins = db.newObjectInserter()) {
1462 String lockMsg = "jgit receive-pack";
1463 if (getRefLogIdent() != null)
1464 lockMsg += " from " + getRefLogIdent().toExternalString();
1465
1466 parser = ins.newPackParser(packInputStream());
1467 parser.setAllowThin(true);
1468 parser.setNeedNewObjectIds(checkReferencedIsReachable);
1469 parser.setNeedBaseObjectIds(checkReferencedIsReachable);
1470 parser.setCheckEofAfterPackFooter(!biDirectionalPipe
1471 && !isExpectDataAfterPackFooter());
1472 parser.setExpectDataAfterPackFooter(isExpectDataAfterPackFooter());
1473 parser.setObjectChecker(objectChecker);
1474 parser.setLockMessage(lockMsg);
1475 parser.setMaxObjectSizeLimit(maxObjectSizeLimit);
1476 packLock = parser.parse(receiving, resolving);
1477 packSize = Long.valueOf(parser.getPackSize());
1478 stats = parser.getReceivedPackStatistics();
1479 ins.flush();
1480 }
1481
1482 if (timeoutIn != null)
1483 timeoutIn.setTimeout(timeout * 1000);
1484 }
1485
1486 private InputStream packInputStream() {
1487 InputStream packIn = rawIn;
1488 if (maxPackSizeLimit >= 0) {
1489 packIn = new LimitedInputStream(packIn, maxPackSizeLimit) {
1490 @Override
1491 protected void limitExceeded() throws TooLargePackException {
1492 throw new TooLargePackException(limit);
1493 }
1494 };
1495 }
1496 return packIn;
1497 }
1498
1499 private boolean needCheckConnectivity() {
1500 return isCheckReceivedObjects()
1501 || isCheckReferencedObjectsAreReachable()
1502 || !getClientShallowCommits().isEmpty();
1503 }
1504
1505 private void checkSubmodules()
1506 throws IOException {
1507 ObjectDatabase odb = db.getObjectDatabase();
1508 if (objectChecker == null) {
1509 return;
1510 }
1511 for (GitmoduleEntry entry : objectChecker.getGitsubmodules()) {
1512 AnyObjectId blobId = entry.getBlobId();
1513 ObjectLoader blob = odb.open(blobId, Constants.OBJ_BLOB);
1514
1515 try {
1516 SubmoduleValidator.assertValidGitModulesFile(
1517 new String(blob.getBytes(), UTF_8));
1518 } catch (LargeObjectException | SubmoduleValidationException e) {
1519 throw new IOException(e);
1520 }
1521 }
1522 }
1523
1524 private void checkConnectivity() throws IOException {
1525 ObjectIdSubclassMap<ObjectId> baseObjects = null;
1526 ObjectIdSubclassMap<ObjectId> providedObjects = null;
1527 ProgressMonitor checking = NullProgressMonitor.INSTANCE;
1528 if (sideBand && !quiet) {
1529 SideBandProgressMonitor m = new SideBandProgressMonitor(msgOut);
1530 m.setDelayStart(750, TimeUnit.MILLISECONDS);
1531 checking = m;
1532 }
1533
1534 if (checkReferencedIsReachable) {
1535 baseObjects = parser.getBaseObjectIds();
1536 providedObjects = parser.getNewObjectIds();
1537 }
1538 parser = null;
1539
1540 try (ObjectWalkectWalk.html#ObjectWalk">ObjectWalk ow = new ObjectWalk(db)) {
1541 if (baseObjects != null) {
1542 ow.sort(RevSort.TOPO);
1543 if (!baseObjects.isEmpty())
1544 ow.sort(RevSort.BOUNDARY, true);
1545 }
1546
1547 for (ReceiveCommand cmd : commands) {
1548 if (cmd.getResult() != Result.NOT_ATTEMPTED)
1549 continue;
1550 if (cmd.getType() == ReceiveCommand.Type.DELETE)
1551 continue;
1552 ow.markStart(ow.parseAny(cmd.getNewId()));
1553 }
1554 for (ObjectId have : advertisedHaves) {
1555 RevObject o = ow.parseAny(have);
1556 ow.markUninteresting(o);
1557
1558 if (baseObjects != null && !baseObjects.isEmpty()) {
1559 o = ow.peel(o);
1560 if (o instanceof RevCommit)
1561 o = ((RevCommit) o).getTree();
1562 if (o instanceof RevTree)
1563 ow.markUninteresting(o);
1564 }
1565 }
1566
1567 checking.beginTask(JGitText.get().countingObjects,
1568 ProgressMonitor.UNKNOWN);
1569 RevCommit c;
1570 while ((c = ow.next()) != null) {
1571 checking.update(1);
1572 if (providedObjects != null
1573 && !c.has(RevFlag.UNINTERESTING)
1574 && !providedObjects.contains(c))
1575 throw new MissingObjectException(c, Constants.TYPE_COMMIT);
1576 }
1577
1578 RevObject o;
1579 while ((o = ow.nextObject()) != null) {
1580 checking.update(1);
1581 if (o.has(RevFlag.UNINTERESTING))
1582 continue;
1583
1584 if (providedObjects != null) {
1585 if (providedObjects.contains(o))
1586 continue;
1587 else
1588 throw new MissingObjectException(o, o.getType());
1589 }
1590
1591 if (o instanceof RevBlob && !db.getObjectDatabase().has(o))
1592 throw new MissingObjectException(o, Constants.TYPE_BLOB);
1593 }
1594 checking.endTask();
1595
1596 if (baseObjects != null) {
1597 for (ObjectId id : baseObjects) {
1598 o = ow.parseAny(id);
1599 if (!o.has(RevFlag.UNINTERESTING))
1600 throw new MissingObjectException(o, o.getType());
1601 }
1602 }
1603 }
1604 }
1605
1606
1607
1608
1609 protected void validateCommands() {
1610 for (ReceiveCommand cmd : commands) {
1611 final Ref ref = cmd.getRef();
1612 if (cmd.getResult() != Result.NOT_ATTEMPTED)
1613 continue;
1614
1615 if (cmd.getType() == ReceiveCommand.Type.DELETE) {
1616 if (!isAllowDeletes()) {
1617
1618 cmd.setResult(Result.REJECTED_NODELETE);
1619 continue;
1620 }
1621 if (!isAllowBranchDeletes()
1622 && ref.getName().startsWith(Constants.R_HEADS)) {
1623
1624 cmd.setResult(Result.REJECTED_NODELETE);
1625 continue;
1626 }
1627 }
1628
1629 if (cmd.getType() == ReceiveCommand.Type.CREATE) {
1630 if (!isAllowCreates()) {
1631 cmd.setResult(Result.REJECTED_NOCREATE);
1632 continue;
1633 }
1634
1635 if (ref != null && !isAllowNonFastForwards()) {
1636
1637
1638
1639 cmd.setResult(Result.REJECTED_NONFASTFORWARD);
1640 continue;
1641 }
1642
1643 if (ref != null) {
1644
1645
1646
1647 cmd.setResult(Result.REJECTED_OTHER_REASON,
1648 JGitText.get().refAlreadyExists);
1649 continue;
1650 }
1651 }
1652
1653 if (cmd.getType() == ReceiveCommand.Type.DELETE && ref != null) {
1654 ObjectId id = ref.getObjectId();
1655 if (id == null) {
1656 id = ObjectId.zeroId();
1657 }
1658 if (!ObjectId.zeroId().equals(cmd.getOldId())
1659 && !id.equals(cmd.getOldId())) {
1660
1661
1662
1663
1664 cmd.setResult(Result.REJECTED_OTHER_REASON,
1665 JGitText.get().invalidOldIdSent);
1666 continue;
1667 }
1668 }
1669
1670 if (cmd.getType() == ReceiveCommand.Type.UPDATE) {
1671 if (ref == null) {
1672
1673
1674 cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().noSuchRef);
1675 continue;
1676 }
1677 ObjectId id = ref.getObjectId();
1678 if (id == null) {
1679
1680 cmd.setResult(Result.REJECTED_OTHER_REASON,
1681 JGitText.get().cannotUpdateUnbornBranch);
1682 continue;
1683 }
1684
1685 if (!id.equals(cmd.getOldId())) {
1686
1687
1688
1689 cmd.setResult(Result.REJECTED_OTHER_REASON,
1690 JGitText.get().invalidOldIdSent);
1691 continue;
1692 }
1693
1694
1695
1696 RevObject oldObj, newObj;
1697 try {
1698 oldObj = walk.parseAny(cmd.getOldId());
1699 } catch (IOException e) {
1700 cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
1701 .getOldId().name());
1702 continue;
1703 }
1704
1705 try {
1706 newObj = walk.parseAny(cmd.getNewId());
1707 } catch (IOException e) {
1708 cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
1709 .getNewId().name());
1710 continue;
1711 }
1712
1713 if (oldObj instanceof RevCommite/jgit/revwalk/RevCommit.html#RevCommit">RevCommit && newObj instanceof RevCommit) {
1714 try {
1715 if (walk.isMergedInto((RevCommit) oldObj,
1716 (RevCommit) newObj))
1717 cmd.setTypeFastForwardUpdate();
1718 else
1719 cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
1720 } catch (MissingObjectException e) {
1721 cmd.setResult(Result.REJECTED_MISSING_OBJECT, e
1722 .getMessage());
1723 } catch (IOException e) {
1724 cmd.setResult(Result.REJECTED_OTHER_REASON);
1725 }
1726 } else {
1727 cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
1728 }
1729
1730 if (cmd.getType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD
1731 && !isAllowNonFastForwards()) {
1732 cmd.setResult(Result.REJECTED_NONFASTFORWARD);
1733 continue;
1734 }
1735 }
1736
1737 if (!cmd.getRefName().startsWith(Constants.R_REFS)
1738 || !Repository.isValidRefName(cmd.getRefName())) {
1739 cmd.setResult(Result.REJECTED_OTHER_REASON, JGitText.get().funnyRefname);
1740 }
1741 }
1742 }
1743
1744
1745
1746
1747
1748
1749
1750 protected boolean anyRejects() {
1751 for (ReceiveCommand cmd : commands) {
1752 if (cmd.getResult() != Result.NOT_ATTEMPTED && cmd.getResult() != Result.OK)
1753 return true;
1754 }
1755 return false;
1756 }
1757
1758
1759
1760
1761
1762
1763 protected void failPendingCommands() {
1764 ReceiveCommand.abort(commands);
1765 }
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775 protected List<ReceiveCommand> filterCommands(Result want) {
1776 return ReceiveCommand.filter(commands, want);
1777 }
1778
1779
1780
1781
1782 protected void executeCommands() {
1783 List<ReceiveCommand> toApply = filterCommands(Result.NOT_ATTEMPTED);
1784 if (toApply.isEmpty())
1785 return;
1786
1787 ProgressMonitor updating = NullProgressMonitor.INSTANCE;
1788 if (sideBand) {
1789 SideBandProgressMonitor pm = new SideBandProgressMonitor(msgOut);
1790 pm.setDelayStart(250, TimeUnit.MILLISECONDS);
1791 updating = pm;
1792 }
1793
1794 BatchRefUpdate batch = db.getRefDatabase().newBatchUpdate();
1795 batch.setAllowNonFastForwards(isAllowNonFastForwards());
1796 batch.setAtomic(isAtomic());
1797 batch.setRefLogIdent(getRefLogIdent());
1798 batch.setRefLogMessage("push", true);
1799 batch.addCommand(toApply);
1800 try {
1801 batch.setPushCertificate(getPushCertificate());
1802 batch.execute(walk, updating);
1803 } catch (IOException err) {
1804 for (ReceiveCommand cmd : toApply) {
1805 if (cmd.getResult() == Result.NOT_ATTEMPTED)
1806 cmd.reject(err);
1807 }
1808 }
1809 }
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824 protected void sendStatusReport(final boolean forClient,
1825 final Throwable unpackError, final Reporter out) throws IOException {
1826 if (unpackError != null) {
1827 out.sendString("unpack error " + unpackError.getMessage());
1828 if (forClient) {
1829 for (ReceiveCommand cmd : commands) {
1830 out.sendString("ng " + cmd.getRefName()
1831 + " n/a (unpacker error)");
1832 }
1833 }
1834 return;
1835 }
1836
1837 if (forClient)
1838 out.sendString("unpack ok");
1839 for (ReceiveCommand cmd : commands) {
1840 if (cmd.getResult() == Result.OK) {
1841 if (forClient)
1842 out.sendString("ok " + cmd.getRefName());
1843 continue;
1844 }
1845
1846 final StringBuilder r = new StringBuilder();
1847 if (forClient)
1848 r.append("ng ").append(cmd.getRefName()).append(" ");
1849 else
1850 r.append(" ! [rejected] ").append(cmd.getRefName()).append(" (");
1851
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_MISSING_OBJECT:
1874 if (cmd.getMessage() == null)
1875 r.append("missing object(s)");
1876 else if (cmd.getMessage().length() == Constants.OBJECT_ID_STRING_LENGTH) {
1877 r.append("object ");
1878 r.append(cmd.getMessage());
1879 r.append(" missing");
1880 } else
1881 r.append(cmd.getMessage());
1882 break;
1883
1884 case REJECTED_OTHER_REASON:
1885 if (cmd.getMessage() == null)
1886 r.append("unspecified reason");
1887 else
1888 r.append(cmd.getMessage());
1889 break;
1890
1891 case LOCK_FAILURE:
1892 r.append("failed to lock");
1893 break;
1894
1895 case OK:
1896
1897 continue;
1898 }
1899 if (!forClient)
1900 r.append(")");
1901 out.sendString(r.toString());
1902 }
1903 }
1904
1905
1906
1907
1908
1909
1910 protected void close() throws IOException {
1911 if (sideBand) {
1912
1913
1914
1915
1916
1917
1918 ((SideBandOutputStream) msgOut).flushBuffer();
1919 ((SideBandOutputStream) rawOut).flushBuffer();
1920
1921 PacketLineOut plo = new PacketLineOut(origOut);
1922 plo.setFlushOnEnd(false);
1923 plo.end();
1924 }
1925
1926 if (biDirectionalPipe) {
1927
1928
1929
1930
1931 if (!sideBand && msgOut != null)
1932 msgOut.flush();
1933 rawOut.flush();
1934 }
1935 }
1936
1937
1938
1939
1940
1941
1942
1943 protected void release() throws IOException {
1944 walk.close();
1945 unlockPack();
1946 timeoutIn = null;
1947 rawIn = null;
1948 rawOut = null;
1949 msgOut = null;
1950 pckIn = null;
1951 pckOut = null;
1952 refs = null;
1953
1954
1955
1956 commands = null;
1957 if (timer != null) {
1958 try {
1959 timer.terminate();
1960 } finally {
1961 timer = null;
1962 }
1963 }
1964 }
1965
1966
1967 static abstract class Reporter {
1968 abstract void sendString(String s) throws IOException;
1969 }
1970 }