1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.lib;
14
15 import static java.nio.charset.StandardCharsets.UTF_8;
16
17 import java.nio.ByteBuffer;
18 import java.nio.charset.Charset;
19 import java.security.MessageDigest;
20 import java.security.NoSuchAlgorithmException;
21 import java.text.MessageFormat;
22
23 import org.eclipse.jgit.errors.CorruptObjectException;
24 import org.eclipse.jgit.internal.JGitText;
25 import org.eclipse.jgit.util.MutableInteger;
26
27
28
29
30 @SuppressWarnings("nls")
31 public final class Constants {
32
33 private static final String HASH_FUNCTION = "SHA-1";
34
35
36
37
38
39
40
41 public static final int OBJECT_ID_LENGTH = 20;
42
43
44
45
46
47
48
49 public static final int OBJECT_ID_STRING_LENGTH = OBJECT_ID_LENGTH * 2;
50
51
52 public static final String HEAD = "HEAD";
53
54
55 public static final String FETCH_HEAD = "FETCH_HEAD";
56
57
58
59
60
61
62
63
64 public static final String TYPE_COMMIT = "commit";
65
66
67
68
69
70
71
72 public static final String TYPE_BLOB = "blob";
73
74
75
76
77
78
79
80 public static final String TYPE_TREE = "tree";
81
82
83
84
85
86
87
88
89 public static final String TYPE_TAG = "tag";
90
91 private static final byte[] ENCODED_TYPE_COMMIT = encodeASCII(TYPE_COMMIT);
92
93 private static final byte[] ENCODED_TYPE_BLOB = encodeASCII(TYPE_BLOB);
94
95 private static final byte[] ENCODED_TYPE_TREE = encodeASCII(TYPE_TREE);
96
97 private static final byte[] ENCODED_TYPE_TAG = encodeASCII(TYPE_TAG);
98
99
100 public static final int OBJ_BAD = -1;
101
102
103
104
105
106
107
108 public static final int OBJ_EXT = 0;
109
110
111
112
113
114
115
116
117
118
119 public static final int OBJ_COMMIT = 1;
120
121
122
123
124
125
126
127
128
129
130 public static final int OBJ_TREE = 2;
131
132
133
134
135
136
137
138
139
140
141 public static final int OBJ_BLOB = 3;
142
143
144
145
146
147
148
149
150
151
152 public static final int OBJ_TAG = 4;
153
154
155 public static final int OBJ_TYPE_5 = 5;
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 public static final int OBJ_OFS_DELTA = 6;
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public static final int OBJ_REF_DELTA = 7;
188
189
190
191
192
193
194
195 public static final byte[] PACK_SIGNATURE = { 'P', 'A', 'C', 'K' };
196
197
198
199
200
201
202
203 @Deprecated
204 public static final Charset CHARSET;
205
206
207
208
209
210
211
212 @Deprecated
213 public static final String CHARACTER_ENCODING;
214
215
216 public static final String MASTER = "master";
217
218
219 public static final String STASH = "stash";
220
221
222 public static final String R_HEADS = "refs/heads/";
223
224
225 public static final String R_REMOTES = "refs/remotes/";
226
227
228 public static final String R_TAGS = "refs/tags/";
229
230
231 public static final String R_NOTES = "refs/notes/";
232
233
234 public static final String R_NOTES_COMMITS = R_NOTES + "commits";
235
236
237 public static final String R_REFS = "refs/";
238
239
240 public static final String R_STASH = R_REFS + STASH;
241
242
243 public static final String LOGS = "logs";
244
245
246
247
248
249 public static final String OBJECTS = "objects";
250
251
252
253
254
255 public static final String REFTABLE = "reftable";
256
257
258
259
260
261 public static final String TABLES_LIST = "tables.list";
262
263
264 public static final String INFO_REFS = "info/refs";
265
266
267
268
269
270 public static final String INFO_ALTERNATES = "info/alternates";
271
272
273
274
275
276 public static final String INFO_HTTP_ALTERNATES = "info/http-alternates";
277
278
279 public static final String PACKED_REFS = "packed-refs";
280
281
282
283
284
285
286 public static final String INFO_EXCLUDE = "info/exclude";
287
288
289
290
291
292
293 public static final String INFO_ATTRIBUTES = "info/attributes";
294
295
296
297
298
299
300 public static final String OS_USER_DIR = "user.dir";
301
302
303 public static final String OS_USER_NAME_KEY = "user.name";
304
305
306 public static final String GIT_AUTHOR_NAME_KEY = "GIT_AUTHOR_NAME";
307
308
309 public static final String GIT_AUTHOR_EMAIL_KEY = "GIT_AUTHOR_EMAIL";
310
311
312 public static final String GIT_COMMITTER_NAME_KEY = "GIT_COMMITTER_NAME";
313
314
315 public static final String GIT_COMMITTER_EMAIL_KEY = "GIT_COMMITTER_EMAIL";
316
317
318
319
320
321
322 public static final String GIT_CONFIG_NOSYSTEM_KEY = "GIT_CONFIG_NOSYSTEM";
323
324
325
326
327
328
329
330
331 public static final String XDG_CONFIG_HOME = "XDG_CONFIG_HOME";
332
333
334
335
336
337 public static final String GIT_CEILING_DIRECTORIES_KEY = "GIT_CEILING_DIRECTORIES";
338
339
340
341
342
343 public static final String GIT_DIR_KEY = "GIT_DIR";
344
345
346
347
348
349 public static final String GIT_WORK_TREE_KEY = "GIT_WORK_TREE";
350
351
352
353
354 public static final String GIT_INDEX_FILE_KEY = "GIT_INDEX_FILE";
355
356
357
358
359 public static final String GIT_OBJECT_DIRECTORY_KEY = "GIT_OBJECT_DIRECTORY";
360
361
362
363
364
365 public static final String GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY = "GIT_ALTERNATE_OBJECT_DIRECTORIES";
366
367
368 public static final String UNKNOWN_USER_DEFAULT = "unknown-user";
369
370
371 public static final String SIGNED_OFF_BY_TAG = "Signed-off-by: ";
372
373
374 public static final String GITIGNORE_FILENAME = ".gitignore";
375
376
377 public static final String DEFAULT_REMOTE_NAME = "origin";
378
379
380 public static final String DOT_GIT = ".git";
381
382
383 public static final String CONFIG = "config";
384
385
386 public static final String DOT_GIT_EXT = ".git";
387
388
389
390
391
392
393 public static final String DOT_BUNDLE_EXT = ".bundle";
394
395
396
397
398
399
400 public static final String DOT_GIT_ATTRIBUTES = ".gitattributes";
401
402
403
404
405
406
407 public static final String ATTR_FILTER = "filter";
408
409
410
411
412
413
414 public static final String ATTR_FILTER_TYPE_CLEAN = "clean";
415
416
417
418
419
420
421 public static final String ATTR_FILTER_TYPE_SMUDGE = "smudge";
422
423
424
425
426
427
428 public static final String BUILTIN_FILTER_PREFIX = "jgit://builtin/";
429
430
431 public static final String DOT_GIT_IGNORE = ".gitignore";
432
433
434 public static final String DOT_GIT_MODULES = ".gitmodules";
435
436
437 public static final String SHALLOW = "shallow";
438
439
440
441
442
443
444 public static final String GITDIR = "gitdir: ";
445
446
447
448
449
450
451 public static final String MODULES = "modules";
452
453
454
455
456
457
458 public static final String HOOKS = "hooks";
459
460
461
462
463
464
465 public static final String ATTR_MERGE = "merge";
466
467
468
469
470
471
472 public static final String ATTR_DIFF = "diff";
473
474
475
476
477
478
479 public static final String ATTR_BUILTIN_BINARY_MERGER = "binary";
480
481
482
483
484
485
486
487
488
489
490 public static MessageDigest newMessageDigest() {
491 try {
492 return MessageDigest.getInstance(HASH_FUNCTION);
493 } catch (NoSuchAlgorithmException nsae) {
494 throw new RuntimeException(MessageFormat.format(
495 JGitText.get().requiredHashFunctionNotAvailable, HASH_FUNCTION), nsae);
496 }
497 }
498
499
500
501
502
503
504
505 public static String typeString(int typeCode) {
506 switch (typeCode) {
507 case OBJ_COMMIT:
508 return TYPE_COMMIT;
509 case OBJ_TREE:
510 return TYPE_TREE;
511 case OBJ_BLOB:
512 return TYPE_BLOB;
513 case OBJ_TAG:
514 return TYPE_TAG;
515 default:
516 throw new IllegalArgumentException(MessageFormat.format(
517 JGitText.get().badObjectType, Integer.valueOf(typeCode)));
518 }
519 }
520
521
522
523
524
525
526
527
528
529
530 public static byte[] encodedTypeString(int typeCode) {
531 switch (typeCode) {
532 case OBJ_COMMIT:
533 return ENCODED_TYPE_COMMIT;
534 case OBJ_TREE:
535 return ENCODED_TYPE_TREE;
536 case OBJ_BLOB:
537 return ENCODED_TYPE_BLOB;
538 case OBJ_TAG:
539 return ENCODED_TYPE_TAG;
540 default:
541 throw new IllegalArgumentException(MessageFormat.format(
542 JGitText.get().badObjectType, Integer.valueOf(typeCode)));
543 }
544 }
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566 public static int decodeTypeString(final AnyObjectId id,
567 final byte[] typeString, final byte endMark,
568 final MutableInteger offset) throws CorruptObjectException {
569 try {
570 int position = offset.value;
571 switch (typeString[position]) {
572 case 'b':
573 if (typeString[position + 1] != 'l'
574 || typeString[position + 2] != 'o'
575 || typeString[position + 3] != 'b'
576 || typeString[position + 4] != endMark)
577 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
578 offset.value = position + 5;
579 return Constants.OBJ_BLOB;
580
581 case 'c':
582 if (typeString[position + 1] != 'o'
583 || typeString[position + 2] != 'm'
584 || typeString[position + 3] != 'm'
585 || typeString[position + 4] != 'i'
586 || typeString[position + 5] != 't'
587 || typeString[position + 6] != endMark)
588 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
589 offset.value = position + 7;
590 return Constants.OBJ_COMMIT;
591
592 case 't':
593 switch (typeString[position + 1]) {
594 case 'a':
595 if (typeString[position + 2] != 'g'
596 || typeString[position + 3] != endMark)
597 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
598 offset.value = position + 4;
599 return Constants.OBJ_TAG;
600
601 case 'r':
602 if (typeString[position + 2] != 'e'
603 || typeString[position + 3] != 'e'
604 || typeString[position + 4] != endMark)
605 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
606 offset.value = position + 5;
607 return Constants.OBJ_TREE;
608
609 default:
610 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
611 }
612
613 default:
614 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
615 }
616 } catch (ArrayIndexOutOfBoundsException bad) {
617 CorruptObjectException coe = new CorruptObjectException(id,
618 JGitText.get().corruptObjectInvalidType);
619 coe.initCause(bad);
620 throw coe;
621 }
622 }
623
624
625
626
627
628
629
630
631
632 public static byte[] encodeASCII(long s) {
633 return encodeASCII(Long.toString(s));
634 }
635
636
637
638
639
640
641
642
643
644
645
646
647
648 public static byte[] encodeASCII(String s) {
649 final byte[] r = new byte[s.length()];
650 for (int k = r.length - 1; k >= 0; k--) {
651 final char c = s.charAt(k);
652 if (c > 127)
653 throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notASCIIString, s));
654 r[k] = (byte) c;
655 }
656 return r;
657 }
658
659
660
661
662
663
664
665
666
667
668 public static byte[] encode(String str) {
669 final ByteBuffer bb = UTF_8.encode(str);
670 final int len = bb.limit();
671 if (bb.hasArray() && bb.arrayOffset() == 0) {
672 final byte[] arr = bb.array();
673 if (arr.length == len)
674 return arr;
675 }
676
677 final byte[] arr = new byte[len];
678 bb.get(arr);
679 return arr;
680 }
681
682 static {
683 if (OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
684 throw new LinkageError(JGitText.get().incorrectOBJECT_ID_LENGTH);
685 CHARSET = UTF_8;
686 CHARACTER_ENCODING = UTF_8.name();
687 }
688
689
690 public static final String MERGE_MSG = "MERGE_MSG";
691
692
693 public static final String MERGE_HEAD = "MERGE_HEAD";
694
695
696 public static final String CHERRY_PICK_HEAD = "CHERRY_PICK_HEAD";
697
698
699 public static final String SQUASH_MSG = "SQUASH_MSG";
700
701
702 public static final String REVERT_HEAD = "REVERT_HEAD";
703
704
705
706
707
708 public static final String ORIG_HEAD = "ORIG_HEAD";
709
710
711
712
713
714
715
716 public static final String COMMIT_EDITMSG = "COMMIT_EDITMSG";
717
718
719
720
721
722
723 public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_BLOB_ID = ObjectId
724 .fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
725
726
727
728
729
730
731 public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_TREE_ID = ObjectId
732 .fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904");
733
734
735
736
737
738
739 public static final String LOCK_SUFFIX = ".lock";
740
741 private Constants() {
742
743 }
744 }