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_GIT_ATTRIBUTES = ".gitattributes";
394
395
396
397
398
399
400 public static final String ATTR_FILTER = "filter";
401
402
403
404
405
406
407 public static final String ATTR_FILTER_TYPE_CLEAN = "clean";
408
409
410
411
412
413
414 public static final String ATTR_FILTER_TYPE_SMUDGE = "smudge";
415
416
417
418
419
420
421 public static final String BUILTIN_FILTER_PREFIX = "jgit://builtin/";
422
423
424 public static final String DOT_GIT_IGNORE = ".gitignore";
425
426
427 public static final String DOT_GIT_MODULES = ".gitmodules";
428
429
430 public static final String SHALLOW = "shallow";
431
432
433
434
435
436
437 public static final String GITDIR = "gitdir: ";
438
439
440
441
442
443
444 public static final String MODULES = "modules";
445
446
447
448
449
450
451 public static final String HOOKS = "hooks";
452
453
454
455
456
457
458 public static final String ATTR_MERGE = "merge";
459
460
461
462
463
464
465 public static final String ATTR_DIFF = "diff";
466
467
468
469
470
471
472 public static final String ATTR_BUILTIN_BINARY_MERGER = "binary";
473
474
475
476
477
478
479
480
481
482
483 public static MessageDigest newMessageDigest() {
484 try {
485 return MessageDigest.getInstance(HASH_FUNCTION);
486 } catch (NoSuchAlgorithmException nsae) {
487 throw new RuntimeException(MessageFormat.format(
488 JGitText.get().requiredHashFunctionNotAvailable, HASH_FUNCTION), nsae);
489 }
490 }
491
492
493
494
495
496
497
498 public static String typeString(int typeCode) {
499 switch (typeCode) {
500 case OBJ_COMMIT:
501 return TYPE_COMMIT;
502 case OBJ_TREE:
503 return TYPE_TREE;
504 case OBJ_BLOB:
505 return TYPE_BLOB;
506 case OBJ_TAG:
507 return TYPE_TAG;
508 default:
509 throw new IllegalArgumentException(MessageFormat.format(
510 JGitText.get().badObjectType, Integer.valueOf(typeCode)));
511 }
512 }
513
514
515
516
517
518
519
520
521
522
523 public static byte[] encodedTypeString(int typeCode) {
524 switch (typeCode) {
525 case OBJ_COMMIT:
526 return ENCODED_TYPE_COMMIT;
527 case OBJ_TREE:
528 return ENCODED_TYPE_TREE;
529 case OBJ_BLOB:
530 return ENCODED_TYPE_BLOB;
531 case OBJ_TAG:
532 return ENCODED_TYPE_TAG;
533 default:
534 throw new IllegalArgumentException(MessageFormat.format(
535 JGitText.get().badObjectType, Integer.valueOf(typeCode)));
536 }
537 }
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559 public static int decodeTypeString(final AnyObjectId id,
560 final byte[] typeString, final byte endMark,
561 final MutableInteger offset) throws CorruptObjectException {
562 try {
563 int position = offset.value;
564 switch (typeString[position]) {
565 case 'b':
566 if (typeString[position + 1] != 'l'
567 || typeString[position + 2] != 'o'
568 || typeString[position + 3] != 'b'
569 || typeString[position + 4] != endMark)
570 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
571 offset.value = position + 5;
572 return Constants.OBJ_BLOB;
573
574 case 'c':
575 if (typeString[position + 1] != 'o'
576 || typeString[position + 2] != 'm'
577 || typeString[position + 3] != 'm'
578 || typeString[position + 4] != 'i'
579 || typeString[position + 5] != 't'
580 || typeString[position + 6] != endMark)
581 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
582 offset.value = position + 7;
583 return Constants.OBJ_COMMIT;
584
585 case 't':
586 switch (typeString[position + 1]) {
587 case 'a':
588 if (typeString[position + 2] != 'g'
589 || typeString[position + 3] != endMark)
590 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
591 offset.value = position + 4;
592 return Constants.OBJ_TAG;
593
594 case 'r':
595 if (typeString[position + 2] != 'e'
596 || typeString[position + 3] != 'e'
597 || typeString[position + 4] != endMark)
598 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
599 offset.value = position + 5;
600 return Constants.OBJ_TREE;
601
602 default:
603 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
604 }
605
606 default:
607 throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
608 }
609 } catch (ArrayIndexOutOfBoundsException bad) {
610 CorruptObjectException coe = new CorruptObjectException(id,
611 JGitText.get().corruptObjectInvalidType);
612 coe.initCause(bad);
613 throw coe;
614 }
615 }
616
617
618
619
620
621
622
623
624
625 public static byte[] encodeASCII(long s) {
626 return encodeASCII(Long.toString(s));
627 }
628
629
630
631
632
633
634
635
636
637
638
639
640
641 public static byte[] encodeASCII(String s) {
642 final byte[] r = new byte[s.length()];
643 for (int k = r.length - 1; k >= 0; k--) {
644 final char c = s.charAt(k);
645 if (c > 127)
646 throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notASCIIString, s));
647 r[k] = (byte) c;
648 }
649 return r;
650 }
651
652
653
654
655
656
657
658
659
660
661 public static byte[] encode(String str) {
662 final ByteBuffer bb = UTF_8.encode(str);
663 final int len = bb.limit();
664 if (bb.hasArray() && bb.arrayOffset() == 0) {
665 final byte[] arr = bb.array();
666 if (arr.length == len)
667 return arr;
668 }
669
670 final byte[] arr = new byte[len];
671 bb.get(arr);
672 return arr;
673 }
674
675 static {
676 if (OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
677 throw new LinkageError(JGitText.get().incorrectOBJECT_ID_LENGTH);
678 CHARSET = UTF_8;
679 CHARACTER_ENCODING = UTF_8.name();
680 }
681
682
683 public static final String MERGE_MSG = "MERGE_MSG";
684
685
686 public static final String MERGE_HEAD = "MERGE_HEAD";
687
688
689 public static final String CHERRY_PICK_HEAD = "CHERRY_PICK_HEAD";
690
691
692 public static final String SQUASH_MSG = "SQUASH_MSG";
693
694
695 public static final String REVERT_HEAD = "REVERT_HEAD";
696
697
698
699
700
701 public static final String ORIG_HEAD = "ORIG_HEAD";
702
703
704
705
706
707
708
709 public static final String COMMIT_EDITMSG = "COMMIT_EDITMSG";
710
711
712
713
714
715
716 public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_BLOB_ID = ObjectId
717 .fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
718
719
720
721
722
723
724 public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_TREE_ID = ObjectId
725 .fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904");
726
727
728
729
730
731
732 public static final String LOCK_SUFFIX = ".lock";
733
734 private Constants() {
735
736 }
737 }