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