2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 * This handles basic git sha1 object files - packing, unpacking,
16 #include "tree-walk.h"
18 #include "pack-revindex.h"
19 #include "sha1-lookup.h"
22 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
23 #define O_NOATIME 01000000
29 #define SZ_FMT PRIuMAX
30 static inline uintmax_t sz_fmt(size_t s
) { return s
; }
32 const unsigned char null_sha1
[20];
34 static int git_open_noatime(const char *name
, struct packed_git
*p
);
37 * This is meant to hold a *small* number of objects that you would
38 * want read_sha1_file() to be able to return, but yet you do not want
39 * to write them into the object store (e.g. a browse-only
42 static struct cached_object
{
43 unsigned char sha1
[20];
44 enum object_type type
;
48 static int cached_object_nr
, cached_object_alloc
;
50 static struct cached_object empty_tree
= {
51 EMPTY_TREE_SHA1_BIN_LITERAL
,
57 static struct cached_object
*find_cached_object(const unsigned char *sha1
)
60 struct cached_object
*co
= cached_objects
;
62 for (i
= 0; i
< cached_object_nr
; i
++, co
++) {
63 if (!hashcmp(co
->sha1
, sha1
))
66 if (!hashcmp(sha1
, empty_tree
.sha1
))
71 int safe_create_leading_directories(char *path
)
73 char *pos
= path
+ offset_1st_component(path
);
77 pos
= strchr(pos
, '/');
85 if (!stat(path
, &st
)) {
87 if (!S_ISDIR(st
.st_mode
)) {
92 else if (mkdir(path
, 0777)) {
96 else if (adjust_shared_perm(path
)) {
105 int safe_create_leading_directories_const(const char *path
)
107 /* path points to cache entries, so xstrdup before messing with it */
108 char *buf
= xstrdup(path
);
109 int result
= safe_create_leading_directories(buf
);
114 static void fill_sha1_path(char *pathbuf
, const unsigned char *sha1
)
117 for (i
= 0; i
< 20; i
++) {
118 static char hex
[] = "0123456789abcdef";
119 unsigned int val
= sha1
[i
];
120 char *pos
= pathbuf
+ i
*2 + (i
> 0);
121 *pos
++ = hex
[val
>> 4];
122 *pos
= hex
[val
& 0xf];
127 * NOTE! This returns a statically allocated buffer, so you have to be
128 * careful about using it. Do an "xstrdup()" if you need to save the
131 * Also note that this returns the location for creating. Reading
132 * SHA1 file can happen from any alternate directory listed in the
133 * DB_ENVIRONMENT environment variable if it is not found in
134 * the primary object database.
136 char *sha1_file_name(const unsigned char *sha1
)
138 static char buf
[PATH_MAX
];
142 objdir
= get_object_directory();
143 len
= strlen(objdir
);
145 /* '/' + sha1(2) + '/' + sha1(38) + '\0' */
146 if (len
+ 43 > PATH_MAX
)
147 die("insanely long object directory %s", objdir
);
148 memcpy(buf
, objdir
, len
);
152 fill_sha1_path(buf
+ len
+ 1, sha1
);
156 static char *sha1_get_pack_name(const unsigned char *sha1
,
157 char **name
, char **base
, const char *which
)
159 static const char hex
[] = "0123456789abcdef";
164 const char *sha1_file_directory
= get_object_directory();
165 int len
= strlen(sha1_file_directory
);
166 *base
= xmalloc(len
+ 60);
167 sprintf(*base
, "%s/pack/pack-1234567890123456789012345678901234567890.%s",
168 sha1_file_directory
, which
);
169 *name
= *base
+ len
+ 11;
174 for (i
= 0; i
< 20; i
++) {
175 unsigned int val
= *sha1
++;
176 *buf
++ = hex
[val
>> 4];
177 *buf
++ = hex
[val
& 0xf];
183 char *sha1_pack_name(const unsigned char *sha1
)
185 static char *name
, *base
;
187 return sha1_get_pack_name(sha1
, &name
, &base
, "pack");
190 char *sha1_pack_index_name(const unsigned char *sha1
)
192 static char *name
, *base
;
194 return sha1_get_pack_name(sha1
, &name
, &base
, "idx");
197 struct alternate_object_database
*alt_odb_list
;
198 static struct alternate_object_database
**alt_odb_tail
;
200 static void read_info_alternates(const char * alternates
, int depth
);
203 * Prepare alternate object database registry.
205 * The variable alt_odb_list points at the list of struct
206 * alternate_object_database. The elements on this list come from
207 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
208 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
209 * whose contents is similar to that environment variable but can be
210 * LF separated. Its base points at a statically allocated buffer that
211 * contains "/the/directory/corresponding/to/.git/objects/...", while
212 * its name points just after the slash at the end of ".git/objects/"
213 * in the example above, and has enough space to hold 40-byte hex
214 * SHA1, an extra slash for the first level indirection, and the
217 static int link_alt_odb_entry(const char * entry
, int len
, const char * relative_base
, int depth
)
219 const char *objdir
= get_object_directory();
220 struct alternate_object_database
*ent
;
221 struct alternate_object_database
*alt
;
222 /* 43 = 40-byte + 2 '/' + terminating NUL */
224 int entlen
= pfxlen
+ 43;
227 if (!is_absolute_path(entry
) && relative_base
) {
228 /* Relative alt-odb */
230 base_len
= strlen(relative_base
) + 1;
234 ent
= xmalloc(sizeof(*ent
) + entlen
);
236 if (!is_absolute_path(entry
) && relative_base
) {
237 memcpy(ent
->base
, relative_base
, base_len
- 1);
238 ent
->base
[base_len
- 1] = '/';
239 memcpy(ent
->base
+ base_len
, entry
, len
);
242 memcpy(ent
->base
, entry
, pfxlen
);
244 ent
->name
= ent
->base
+ pfxlen
+ 1;
245 ent
->base
[pfxlen
+ 3] = '/';
246 ent
->base
[pfxlen
] = ent
->base
[entlen
-1] = 0;
248 /* Detect cases where alternate disappeared */
249 if (!is_directory(ent
->base
)) {
250 error("object directory %s does not exist; "
251 "check .git/objects/info/alternates.",
257 /* Prevent the common mistake of listing the same
258 * thing twice, or object directory itself.
260 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
261 if (!memcmp(ent
->base
, alt
->base
, pfxlen
)) {
266 if (!memcmp(ent
->base
, objdir
, pfxlen
)) {
271 /* add the alternate entry */
273 alt_odb_tail
= &(ent
->next
);
276 /* recursively add alternates */
277 read_info_alternates(ent
->base
, depth
+ 1);
279 ent
->base
[pfxlen
] = '/';
284 static void link_alt_odb_entries(const char *alt
, const char *ep
, int sep
,
285 const char *relative_base
, int depth
)
287 const char *cp
, *last
;
290 error("%s: ignoring alternate object stores, nesting too deep.",
298 if (cp
< ep
&& *cp
== '#') {
299 while (cp
< ep
&& *cp
!= sep
)
304 while (cp
< ep
&& *cp
!= sep
)
307 if (!is_absolute_path(last
) && depth
) {
308 error("%s: ignoring relative alternate object store %s",
309 relative_base
, last
);
311 link_alt_odb_entry(last
, cp
- last
,
312 relative_base
, depth
);
315 while (cp
< ep
&& *cp
== sep
)
321 static void read_info_alternates(const char * relative_base
, int depth
)
326 const char alt_file_name
[] = "info/alternates";
327 /* Given that relative_base is no longer than PATH_MAX,
328 ensure that "path" has enough space to append "/", the
329 file name, "info/alternates", and a trailing NUL. */
330 char path
[PATH_MAX
+ 1 + sizeof alt_file_name
];
333 sprintf(path
, "%s/%s", relative_base
, alt_file_name
);
334 fd
= git_open_noatime(path
, NULL
);
337 if (fstat(fd
, &st
) || (st
.st_size
== 0)) {
341 mapsz
= xsize_t(st
.st_size
);
342 map
= xmmap(NULL
, mapsz
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
345 link_alt_odb_entries(map
, map
+ mapsz
, '\n', relative_base
, depth
);
350 void add_to_alternates_file(const char *reference
)
352 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
353 int fd
= hold_lock_file_for_append(lock
, git_path("objects/info/alternates"), LOCK_DIE_ON_ERROR
);
354 char *alt
= mkpath("%s/objects\n", reference
);
355 write_or_die(fd
, alt
, strlen(alt
));
356 if (commit_lock_file(lock
))
357 die("could not close alternates file");
359 link_alt_odb_entries(alt
, alt
+ strlen(alt
), '\n', NULL
, 0);
362 void foreach_alt_odb(alt_odb_fn fn
, void *cb
)
364 struct alternate_object_database
*ent
;
367 for (ent
= alt_odb_list
; ent
; ent
= ent
->next
)
372 void prepare_alt_odb(void)
379 alt
= getenv(ALTERNATE_DB_ENVIRONMENT
);
382 alt_odb_tail
= &alt_odb_list
;
383 link_alt_odb_entries(alt
, alt
+ strlen(alt
), PATH_SEP
, NULL
, 0);
385 read_info_alternates(get_object_directory(), 0);
388 static int has_loose_object_local(const unsigned char *sha1
)
390 char *name
= sha1_file_name(sha1
);
391 return !access(name
, F_OK
);
394 int has_loose_object_nonlocal(const unsigned char *sha1
)
396 struct alternate_object_database
*alt
;
398 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
399 fill_sha1_path(alt
->name
, sha1
);
400 if (!access(alt
->base
, F_OK
))
406 static int has_loose_object(const unsigned char *sha1
)
408 return has_loose_object_local(sha1
) ||
409 has_loose_object_nonlocal(sha1
);
412 static unsigned int pack_used_ctr
;
413 static unsigned int pack_mmap_calls
;
414 static unsigned int peak_pack_open_windows
;
415 static unsigned int pack_open_windows
;
416 static unsigned int pack_open_fds
;
417 static unsigned int pack_max_fds
;
418 static size_t peak_pack_mapped
;
419 static size_t pack_mapped
;
420 struct packed_git
*packed_git
;
422 void pack_report(void)
425 "pack_report: getpagesize() = %10" SZ_FMT
"\n"
426 "pack_report: core.packedGitWindowSize = %10" SZ_FMT
"\n"
427 "pack_report: core.packedGitLimit = %10" SZ_FMT
"\n",
428 sz_fmt(getpagesize()),
429 sz_fmt(packed_git_window_size
),
430 sz_fmt(packed_git_limit
));
432 "pack_report: pack_used_ctr = %10u\n"
433 "pack_report: pack_mmap_calls = %10u\n"
434 "pack_report: pack_open_windows = %10u / %10u\n"
435 "pack_report: pack_mapped = "
436 "%10" SZ_FMT
" / %10" SZ_FMT
"\n",
439 pack_open_windows
, peak_pack_open_windows
,
440 sz_fmt(pack_mapped
), sz_fmt(peak_pack_mapped
));
443 static int check_packed_git_idx(const char *path
, struct packed_git
*p
)
446 struct pack_idx_header
*hdr
;
448 uint32_t version
, nr
, i
, *index
;
449 int fd
= git_open_noatime(path
, p
);
454 if (fstat(fd
, &st
)) {
458 idx_size
= xsize_t(st
.st_size
);
459 if (idx_size
< 4 * 256 + 20 + 20) {
461 return error("index file %s is too small", path
);
463 idx_map
= xmmap(NULL
, idx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
467 if (hdr
->idx_signature
== htonl(PACK_IDX_SIGNATURE
)) {
468 version
= ntohl(hdr
->idx_version
);
469 if (version
< 2 || version
> 2) {
470 munmap(idx_map
, idx_size
);
471 return error("index file %s is version %"PRIu32
472 " and is not supported by this binary"
473 " (try upgrading GIT to a newer version)",
482 index
+= 2; /* skip index header */
483 for (i
= 0; i
< 256; i
++) {
484 uint32_t n
= ntohl(index
[i
]);
486 munmap(idx_map
, idx_size
);
487 return error("non-monotonic index %s", path
);
495 * - 256 index entries 4 bytes each
496 * - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
497 * - 20-byte SHA1 of the packfile
498 * - 20-byte SHA1 file checksum
500 if (idx_size
!= 4*256 + nr
* 24 + 20 + 20) {
501 munmap(idx_map
, idx_size
);
502 return error("wrong index v1 file size in %s", path
);
504 } else if (version
== 2) {
507 * - 8 bytes of header
508 * - 256 index entries 4 bytes each
509 * - 20-byte sha1 entry * nr
510 * - 4-byte crc entry * nr
511 * - 4-byte offset entry * nr
512 * - 20-byte SHA1 of the packfile
513 * - 20-byte SHA1 file checksum
514 * And after the 4-byte offset table might be a
515 * variable sized table containing 8-byte entries
516 * for offsets larger than 2^31.
518 unsigned long min_size
= 8 + 4*256 + nr
*(20 + 4 + 4) + 20 + 20;
519 unsigned long max_size
= min_size
;
521 max_size
+= (nr
- 1)*8;
522 if (idx_size
< min_size
|| idx_size
> max_size
) {
523 munmap(idx_map
, idx_size
);
524 return error("wrong index v2 file size in %s", path
);
526 if (idx_size
!= min_size
&&
528 * make sure we can deal with large pack offsets.
529 * 31-bit signed offset won't be enough, neither
530 * 32-bit unsigned one will be.
532 (sizeof(off_t
) <= 4)) {
533 munmap(idx_map
, idx_size
);
534 return error("pack too large for current definition of off_t in %s", path
);
538 p
->index_version
= version
;
539 p
->index_data
= idx_map
;
540 p
->index_size
= idx_size
;
545 int open_pack_index(struct packed_git
*p
)
553 idx_name
= xstrdup(p
->pack_name
);
554 strcpy(idx_name
+ strlen(idx_name
) - strlen(".pack"), ".idx");
555 ret
= check_packed_git_idx(idx_name
, p
);
560 static void scan_windows(struct packed_git
*p
,
561 struct packed_git
**lru_p
,
562 struct pack_window
**lru_w
,
563 struct pack_window
**lru_l
)
565 struct pack_window
*w
, *w_l
;
567 for (w_l
= NULL
, w
= p
->windows
; w
; w
= w
->next
) {
569 if (!*lru_w
|| w
->last_used
< (*lru_w
)->last_used
) {
579 static int unuse_one_window(struct packed_git
*current
, int keep_fd
)
581 struct packed_git
*p
, *lru_p
= NULL
;
582 struct pack_window
*lru_w
= NULL
, *lru_l
= NULL
;
585 scan_windows(current
, &lru_p
, &lru_w
, &lru_l
);
586 for (p
= packed_git
; p
; p
= p
->next
)
587 scan_windows(p
, &lru_p
, &lru_w
, &lru_l
);
589 munmap(lru_w
->base
, lru_w
->len
);
590 pack_mapped
-= lru_w
->len
;
592 lru_l
->next
= lru_w
->next
;
594 lru_p
->windows
= lru_w
->next
;
595 if (!lru_p
->windows
&& lru_p
->pack_fd
!= -1
596 && lru_p
->pack_fd
!= keep_fd
) {
597 close(lru_p
->pack_fd
);
609 void release_pack_memory(size_t need
, int fd
)
611 size_t cur
= pack_mapped
;
612 while (need
>= (cur
- pack_mapped
) && unuse_one_window(NULL
, fd
))
616 void *xmmap(void *start
, size_t length
,
617 int prot
, int flags
, int fd
, off_t offset
)
619 void *ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
620 if (ret
== MAP_FAILED
) {
623 release_pack_memory(length
, fd
);
624 ret
= mmap(start
, length
, prot
, flags
, fd
, offset
);
625 if (ret
== MAP_FAILED
)
626 die_errno("Out of memory? mmap failed");
631 void close_pack_windows(struct packed_git
*p
)
634 struct pack_window
*w
= p
->windows
;
637 die("pack '%s' still has open windows to it",
639 munmap(w
->base
, w
->len
);
640 pack_mapped
-= w
->len
;
642 p
->windows
= w
->next
;
647 void unuse_pack(struct pack_window
**w_cursor
)
649 struct pack_window
*w
= *w_cursor
;
656 void close_pack_index(struct packed_git
*p
)
659 munmap((void *)p
->index_data
, p
->index_size
);
660 p
->index_data
= NULL
;
665 * This is used by git-repack in case a newly created pack happens to
666 * contain the same set of objects as an existing one. In that case
667 * the resulting file might be different even if its name would be the
668 * same. It is best to close any reference to the old pack before it is
669 * replaced on disk. Of course no index pointers nor windows for given pack
670 * must subsist at this point. If ever objects from this pack are requested
671 * again, the new version of the pack will be reinitialized through
672 * reprepare_packed_git().
674 void free_pack_by_name(const char *pack_name
)
676 struct packed_git
*p
, **pp
= &packed_git
;
680 if (strcmp(pack_name
, p
->pack_name
) == 0) {
681 clear_delta_base_cache();
682 close_pack_windows(p
);
683 if (p
->pack_fd
!= -1) {
688 free(p
->bad_object_sha1
);
698 * Do not call this directly as this leaks p->pack_fd on error return;
699 * call open_packed_git() instead.
701 static int open_packed_git_1(struct packed_git
*p
)
704 struct pack_header hdr
;
705 unsigned char sha1
[20];
706 unsigned char *idx_sha1
;
709 if (!p
->index_data
&& open_pack_index(p
))
710 return error("packfile %s index unavailable", p
->pack_name
);
714 unsigned int max_fds
;
716 if (getrlimit(RLIMIT_NOFILE
, &lim
))
717 die_errno("cannot get RLIMIT_NOFILE");
719 max_fds
= lim
.rlim_cur
;
721 /* Save 3 for stdin/stdout/stderr, 22 for work */
723 pack_max_fds
= max_fds
- 25;
728 while (pack_max_fds
<= pack_open_fds
&& unuse_one_window(NULL
, -1))
731 p
->pack_fd
= git_open_noatime(p
->pack_name
, p
);
732 if (p
->pack_fd
< 0 || fstat(p
->pack_fd
, &st
))
736 /* If we created the struct before we had the pack we lack size. */
738 if (!S_ISREG(st
.st_mode
))
739 return error("packfile %s not a regular file", p
->pack_name
);
740 p
->pack_size
= st
.st_size
;
741 } else if (p
->pack_size
!= st
.st_size
)
742 return error("packfile %s size changed", p
->pack_name
);
744 /* We leave these file descriptors open with sliding mmap;
745 * there is no point keeping them open across exec(), though.
747 fd_flag
= fcntl(p
->pack_fd
, F_GETFD
, 0);
749 return error("cannot determine file descriptor flags");
750 fd_flag
|= FD_CLOEXEC
;
751 if (fcntl(p
->pack_fd
, F_SETFD
, fd_flag
) == -1)
752 return error("cannot set FD_CLOEXEC");
754 /* Verify we recognize this pack file format. */
755 if (read_in_full(p
->pack_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
756 return error("file %s is far too short to be a packfile", p
->pack_name
);
757 if (hdr
.hdr_signature
!= htonl(PACK_SIGNATURE
))
758 return error("file %s is not a GIT packfile", p
->pack_name
);
759 if (!pack_version_ok(hdr
.hdr_version
))
760 return error("packfile %s is version %"PRIu32
" and not"
761 " supported (try upgrading GIT to a newer version)",
762 p
->pack_name
, ntohl(hdr
.hdr_version
));
764 /* Verify the pack matches its index. */
765 if (p
->num_objects
!= ntohl(hdr
.hdr_entries
))
766 return error("packfile %s claims to have %"PRIu32
" objects"
767 " while index indicates %"PRIu32
" objects",
768 p
->pack_name
, ntohl(hdr
.hdr_entries
),
770 if (lseek(p
->pack_fd
, p
->pack_size
- sizeof(sha1
), SEEK_SET
) == -1)
771 return error("end of packfile %s is unavailable", p
->pack_name
);
772 if (read_in_full(p
->pack_fd
, sha1
, sizeof(sha1
)) != sizeof(sha1
))
773 return error("packfile %s signature is unavailable", p
->pack_name
);
774 idx_sha1
= ((unsigned char *)p
->index_data
) + p
->index_size
- 40;
775 if (hashcmp(sha1
, idx_sha1
))
776 return error("packfile %s does not match index", p
->pack_name
);
780 static int open_packed_git(struct packed_git
*p
)
782 if (!open_packed_git_1(p
))
784 if (p
->pack_fd
!= -1) {
792 static int in_window(struct pack_window
*win
, off_t offset
)
794 /* We must promise at least 20 bytes (one hash) after the
795 * offset is available from this window, otherwise the offset
796 * is not actually in this window and a different window (which
797 * has that one hash excess) must be used. This is to support
798 * the object header and delta base parsing routines below.
800 off_t win_off
= win
->offset
;
801 return win_off
<= offset
802 && (offset
+ 20) <= (win_off
+ win
->len
);
805 unsigned char *use_pack(struct packed_git
*p
,
806 struct pack_window
**w_cursor
,
810 struct pack_window
*win
= *w_cursor
;
812 /* Since packfiles end in a hash of their content and it's
813 * pointless to ask for an offset into the middle of that
814 * hash, and the in_window function above wouldn't match
815 * don't allow an offset too close to the end of the file.
817 if (!p
->pack_size
&& p
->pack_fd
== -1 && open_packed_git(p
))
818 die("packfile %s cannot be accessed", p
->pack_name
);
819 if (offset
> (p
->pack_size
- 20))
820 die("offset beyond end of packfile (truncated pack?)");
822 if (!win
|| !in_window(win
, offset
)) {
825 for (win
= p
->windows
; win
; win
= win
->next
) {
826 if (in_window(win
, offset
))
830 size_t window_align
= packed_git_window_size
/ 2;
833 if (p
->pack_fd
== -1 && open_packed_git(p
))
834 die("packfile %s cannot be accessed", p
->pack_name
);
836 win
= xcalloc(1, sizeof(*win
));
837 win
->offset
= (offset
/ window_align
) * window_align
;
838 len
= p
->pack_size
- win
->offset
;
839 if (len
> packed_git_window_size
)
840 len
= packed_git_window_size
;
841 win
->len
= (size_t)len
;
842 pack_mapped
+= win
->len
;
843 while (packed_git_limit
< pack_mapped
844 && unuse_one_window(p
, p
->pack_fd
))
846 win
->base
= xmmap(NULL
, win
->len
,
847 PROT_READ
, MAP_PRIVATE
,
848 p
->pack_fd
, win
->offset
);
849 if (win
->base
== MAP_FAILED
)
850 die("packfile %s cannot be mapped: %s",
853 if (!win
->offset
&& win
->len
== p
->pack_size
854 && !p
->do_not_close
) {
861 if (pack_mapped
> peak_pack_mapped
)
862 peak_pack_mapped
= pack_mapped
;
863 if (pack_open_windows
> peak_pack_open_windows
)
864 peak_pack_open_windows
= pack_open_windows
;
865 win
->next
= p
->windows
;
869 if (win
!= *w_cursor
) {
870 win
->last_used
= pack_used_ctr
++;
874 offset
-= win
->offset
;
876 *left
= win
->len
- xsize_t(offset
);
877 return win
->base
+ offset
;
880 static struct packed_git
*alloc_packed_git(int extra
)
882 struct packed_git
*p
= xmalloc(sizeof(*p
) + extra
);
883 memset(p
, 0, sizeof(*p
));
888 static void try_to_free_pack_memory(size_t size
)
890 release_pack_memory(size
, -1);
893 struct packed_git
*add_packed_git(const char *path
, int path_len
, int local
)
895 static int have_set_try_to_free_routine
;
897 struct packed_git
*p
= alloc_packed_git(path_len
+ 2);
899 if (!have_set_try_to_free_routine
) {
900 have_set_try_to_free_routine
= 1;
901 set_try_to_free_routine(try_to_free_pack_memory
);
905 * Make sure a corresponding .pack file exists and that
906 * the index looks sane.
908 path_len
-= strlen(".idx");
913 memcpy(p
->pack_name
, path
, path_len
);
915 strcpy(p
->pack_name
+ path_len
, ".keep");
916 if (!access(p
->pack_name
, F_OK
))
919 strcpy(p
->pack_name
+ path_len
, ".pack");
920 if (stat(p
->pack_name
, &st
) || !S_ISREG(st
.st_mode
)) {
925 /* ok, it looks sane as far as we can check without
926 * actually mapping the pack file.
928 p
->pack_size
= st
.st_size
;
929 p
->pack_local
= local
;
930 p
->mtime
= st
.st_mtime
;
931 if (path_len
< 40 || get_sha1_hex(path
+ path_len
- 40, p
->sha1
))
936 struct packed_git
*parse_pack_index(unsigned char *sha1
, const char *idx_path
)
938 const char *path
= sha1_pack_name(sha1
);
939 struct packed_git
*p
= alloc_packed_git(strlen(path
) + 1);
941 strcpy(p
->pack_name
, path
);
942 hashcpy(p
->sha1
, sha1
);
943 if (check_packed_git_idx(idx_path
, p
)) {
951 void install_packed_git(struct packed_git
*pack
)
953 if (pack
->pack_fd
!= -1)
956 pack
->next
= packed_git
;
960 static void prepare_packed_git_one(char *objdir
, int local
)
962 /* Ensure that this buffer is large enough so that we can
963 append "/pack/" without clobbering the stack even if
964 strlen(objdir) were PATH_MAX. */
965 char path
[PATH_MAX
+ 1 + 4 + 1 + 1];
970 sprintf(path
, "%s/pack", objdir
);
975 error("unable to open object pack directory: %s: %s",
976 path
, strerror(errno
));
980 while ((de
= readdir(dir
)) != NULL
) {
981 int namelen
= strlen(de
->d_name
);
982 struct packed_git
*p
;
984 if (!has_extension(de
->d_name
, ".idx"))
987 if (len
+ namelen
+ 1 > sizeof(path
))
990 /* Don't reopen a pack we already have. */
991 strcpy(path
+ len
, de
->d_name
);
992 for (p
= packed_git
; p
; p
= p
->next
) {
993 if (!memcmp(path
, p
->pack_name
, len
+ namelen
- 4))
998 /* See if it really is a valid .idx file with corresponding
999 * .pack file that we can map.
1001 p
= add_packed_git(path
, len
+ namelen
, local
);
1004 install_packed_git(p
);
1009 static int sort_pack(const void *a_
, const void *b_
)
1011 struct packed_git
*a
= *((struct packed_git
**)a_
);
1012 struct packed_git
*b
= *((struct packed_git
**)b_
);
1016 * Local packs tend to contain objects specific to our
1017 * variant of the project than remote ones. In addition,
1018 * remote ones could be on a network mounted filesystem.
1019 * Favor local ones for these reasons.
1021 st
= a
->pack_local
- b
->pack_local
;
1026 * Younger packs tend to contain more recent objects,
1027 * and more recent objects tend to get accessed more
1030 if (a
->mtime
< b
->mtime
)
1032 else if (a
->mtime
== b
->mtime
)
1037 static void rearrange_packed_git(void)
1039 struct packed_git
**ary
, *p
;
1042 for (n
= 0, p
= packed_git
; p
; p
= p
->next
)
1047 /* prepare an array of packed_git for easier sorting */
1048 ary
= xcalloc(n
, sizeof(struct packed_git
*));
1049 for (n
= 0, p
= packed_git
; p
; p
= p
->next
)
1052 qsort(ary
, n
, sizeof(struct packed_git
*), sort_pack
);
1054 /* link them back again */
1055 for (i
= 0; i
< n
- 1; i
++)
1056 ary
[i
]->next
= ary
[i
+ 1];
1057 ary
[n
- 1]->next
= NULL
;
1058 packed_git
= ary
[0];
1063 static int prepare_packed_git_run_once
= 0;
1064 void prepare_packed_git(void)
1066 struct alternate_object_database
*alt
;
1068 if (prepare_packed_git_run_once
)
1070 prepare_packed_git_one(get_object_directory(), 1);
1072 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
1074 prepare_packed_git_one(alt
->base
, 0);
1075 alt
->name
[-1] = '/';
1077 rearrange_packed_git();
1078 prepare_packed_git_run_once
= 1;
1081 void reprepare_packed_git(void)
1084 prepare_packed_git_run_once
= 0;
1085 prepare_packed_git();
1088 static void mark_bad_packed_object(struct packed_git
*p
,
1089 const unsigned char *sha1
)
1092 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1093 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1095 p
->bad_object_sha1
= xrealloc(p
->bad_object_sha1
, 20 * (p
->num_bad_objects
+ 1));
1096 hashcpy(p
->bad_object_sha1
+ 20 * p
->num_bad_objects
, sha1
);
1097 p
->num_bad_objects
++;
1100 static const struct packed_git
*has_packed_and_bad(const unsigned char *sha1
)
1102 struct packed_git
*p
;
1105 for (p
= packed_git
; p
; p
= p
->next
)
1106 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1107 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1112 int check_sha1_signature(const unsigned char *sha1
, void *map
, unsigned long size
, const char *type
)
1114 unsigned char real_sha1
[20];
1115 hash_sha1_file(map
, size
, type
, real_sha1
);
1116 return hashcmp(sha1
, real_sha1
) ? -1 : 0;
1119 static int git_open_noatime(const char *name
, struct packed_git
*p
)
1121 static int sha1_file_open_flag
= O_NOATIME
;
1124 int fd
= open(name
, O_RDONLY
| sha1_file_open_flag
);
1128 /* Might the failure be due to O_NOATIME? */
1129 if (errno
!= ENOENT
&& sha1_file_open_flag
) {
1130 sha1_file_open_flag
= 0;
1138 static int open_sha1_file(const unsigned char *sha1
)
1141 char *name
= sha1_file_name(sha1
);
1142 struct alternate_object_database
*alt
;
1144 fd
= git_open_noatime(name
, NULL
);
1150 for (alt
= alt_odb_list
; alt
; alt
= alt
->next
) {
1152 fill_sha1_path(name
, sha1
);
1153 fd
= git_open_noatime(alt
->base
, NULL
);
1160 static void *map_sha1_file(const unsigned char *sha1
, unsigned long *size
)
1165 fd
= open_sha1_file(sha1
);
1170 if (!fstat(fd
, &st
)) {
1171 *size
= xsize_t(st
.st_size
);
1172 map
= xmmap(NULL
, *size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1179 static int legacy_loose_object(unsigned char *map
)
1184 * Is it a zlib-compressed buffer? If so, the first byte
1185 * must be 0x78 (15-bit window size, deflated), and the
1186 * first 16-bit word is evenly divisible by 31
1188 word
= (map
[0] << 8) + map
[1];
1189 if (map
[0] == 0x78 && !(word
% 31))
1195 unsigned long unpack_object_header_buffer(const unsigned char *buf
,
1196 unsigned long len
, enum object_type
*type
, unsigned long *sizep
)
1199 unsigned long size
, c
;
1200 unsigned long used
= 0;
1203 *type
= (c
>> 4) & 7;
1207 if (len
<= used
|| bitsizeof(long) <= shift
) {
1208 error("bad object header");
1212 size
+= (c
& 0x7f) << shift
;
1219 static int unpack_sha1_header(z_stream
*stream
, unsigned char *map
, unsigned long mapsize
, void *buffer
, unsigned long bufsiz
)
1221 unsigned long size
, used
;
1222 static const char valid_loose_object_type
[8] = {
1224 1, 1, 1, 1, /* "commit", "tree", "blob", "tag" */
1225 0, /* "delta" and others are invalid in a loose object */
1227 enum object_type type
;
1229 /* Get the data stream */
1230 memset(stream
, 0, sizeof(*stream
));
1231 stream
->next_in
= map
;
1232 stream
->avail_in
= mapsize
;
1233 stream
->next_out
= buffer
;
1234 stream
->avail_out
= bufsiz
;
1236 if (legacy_loose_object(map
)) {
1237 git_inflate_init(stream
);
1238 return git_inflate(stream
, 0);
1243 * There used to be a second loose object header format which
1244 * was meant to mimic the in-pack format, allowing for direct
1245 * copy of the object data. This format turned up not to be
1246 * really worth it and we don't write it any longer. But we
1247 * can still read it.
1249 used
= unpack_object_header_buffer(map
, mapsize
, &type
, &size
);
1250 if (!used
|| !valid_loose_object_type
[type
])
1255 /* Set up the stream for the rest.. */
1256 stream
->next_in
= map
;
1257 stream
->avail_in
= mapsize
;
1258 git_inflate_init(stream
);
1260 /* And generate the fake traditional header */
1261 stream
->total_out
= 1 + snprintf(buffer
, bufsiz
, "%s %lu",
1262 typename(type
), size
);
1266 static void *unpack_sha1_rest(z_stream
*stream
, void *buffer
, unsigned long size
, const unsigned char *sha1
)
1268 int bytes
= strlen(buffer
) + 1;
1269 unsigned char *buf
= xmallocz(size
);
1273 n
= stream
->total_out
- bytes
;
1276 memcpy(buf
, (char *) buffer
+ bytes
, n
);
1278 if (bytes
<= size
) {
1280 * The above condition must be (bytes <= size), not
1281 * (bytes < size). In other words, even though we
1282 * expect no more output and set avail_out to zer0,
1283 * the input zlib stream may have bytes that express
1284 * "this concludes the stream", and we *do* want to
1287 * Otherwise we would not be able to test that we
1288 * consumed all the input to reach the expected size;
1289 * we also want to check that zlib tells us that all
1290 * went well with status == Z_STREAM_END at the end.
1292 stream
->next_out
= buf
+ bytes
;
1293 stream
->avail_out
= size
- bytes
;
1294 while (status
== Z_OK
)
1295 status
= git_inflate(stream
, Z_FINISH
);
1297 if (status
== Z_STREAM_END
&& !stream
->avail_in
) {
1298 git_inflate_end(stream
);
1303 error("corrupt loose object '%s'", sha1_to_hex(sha1
));
1304 else if (stream
->avail_in
)
1305 error("garbage at end of loose object '%s'",
1312 * We used to just use "sscanf()", but that's actually way
1313 * too permissive for what we want to check. So do an anal
1314 * object header parse by hand.
1316 static int parse_sha1_header(const char *hdr
, unsigned long *sizep
)
1323 * The type can be at most ten bytes (including the
1324 * terminating '\0' that we add), and is followed by
1333 if (i
>= sizeof(type
))
1339 * The length must follow immediately, and be in canonical
1340 * decimal format (ie "010" is not valid).
1342 size
= *hdr
++ - '0';
1347 unsigned long c
= *hdr
- '0';
1351 size
= size
* 10 + c
;
1357 * The length must be followed by a zero byte
1359 return *hdr
? -1 : type_from_string(type
);
1362 static void *unpack_sha1_file(void *map
, unsigned long mapsize
, enum object_type
*type
, unsigned long *size
, const unsigned char *sha1
)
1368 ret
= unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
));
1369 if (ret
< Z_OK
|| (*type
= parse_sha1_header(hdr
, size
)) < 0)
1372 return unpack_sha1_rest(&stream
, hdr
, *size
, sha1
);
1375 unsigned long get_size_from_delta(struct packed_git
*p
,
1376 struct pack_window
**w_curs
,
1379 const unsigned char *data
;
1380 unsigned char delta_head
[20], *in
;
1384 memset(&stream
, 0, sizeof(stream
));
1385 stream
.next_out
= delta_head
;
1386 stream
.avail_out
= sizeof(delta_head
);
1388 git_inflate_init(&stream
);
1390 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1391 stream
.next_in
= in
;
1392 st
= git_inflate(&stream
, Z_FINISH
);
1393 curpos
+= stream
.next_in
- in
;
1394 } while ((st
== Z_OK
|| st
== Z_BUF_ERROR
) &&
1395 stream
.total_out
< sizeof(delta_head
));
1396 git_inflate_end(&stream
);
1397 if ((st
!= Z_STREAM_END
) && stream
.total_out
!= sizeof(delta_head
)) {
1398 error("delta data unpack-initial failed");
1402 /* Examine the initial part of the delta to figure out
1407 /* ignore base size */
1408 get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1410 /* Read the result size */
1411 return get_delta_hdr_size(&data
, delta_head
+sizeof(delta_head
));
1414 static off_t
get_delta_base(struct packed_git
*p
,
1415 struct pack_window
**w_curs
,
1417 enum object_type type
,
1418 off_t delta_obj_offset
)
1420 unsigned char *base_info
= use_pack(p
, w_curs
, *curpos
, NULL
);
1423 /* use_pack() assured us we have [base_info, base_info + 20)
1424 * as a range that we can look at without walking off the
1425 * end of the mapped window. Its actually the hash size
1426 * that is assured. An OFS_DELTA longer than the hash size
1427 * is stupid, as then a REF_DELTA would be smaller to store.
1429 if (type
== OBJ_OFS_DELTA
) {
1431 unsigned char c
= base_info
[used
++];
1432 base_offset
= c
& 127;
1435 if (!base_offset
|| MSB(base_offset
, 7))
1436 return 0; /* overflow */
1437 c
= base_info
[used
++];
1438 base_offset
= (base_offset
<< 7) + (c
& 127);
1440 base_offset
= delta_obj_offset
- base_offset
;
1441 if (base_offset
<= 0 || base_offset
>= delta_obj_offset
)
1442 return 0; /* out of bound */
1444 } else if (type
== OBJ_REF_DELTA
) {
1445 /* The base entry _must_ be in the same pack */
1446 base_offset
= find_pack_entry_one(base_info
, p
);
1449 die("I am totally screwed");
1453 /* forward declaration for a mutually recursive function */
1454 static int packed_object_info(struct packed_git
*p
, off_t offset
,
1455 unsigned long *sizep
);
1457 static int packed_delta_info(struct packed_git
*p
,
1458 struct pack_window
**w_curs
,
1460 enum object_type type
,
1462 unsigned long *sizep
)
1466 base_offset
= get_delta_base(p
, w_curs
, &curpos
, type
, obj_offset
);
1469 type
= packed_object_info(p
, base_offset
, NULL
);
1470 if (type
<= OBJ_NONE
) {
1471 struct revindex_entry
*revidx
;
1472 const unsigned char *base_sha1
;
1473 revidx
= find_pack_revindex(p
, base_offset
);
1476 base_sha1
= nth_packed_object_sha1(p
, revidx
->nr
);
1477 mark_bad_packed_object(p
, base_sha1
);
1478 type
= sha1_object_info(base_sha1
, NULL
);
1479 if (type
<= OBJ_NONE
)
1483 /* We choose to only get the type of the base object and
1484 * ignore potentially corrupt pack file that expects the delta
1485 * based on a base with a wrong size. This saves tons of
1489 *sizep
= get_size_from_delta(p
, w_curs
, curpos
);
1497 static int unpack_object_header(struct packed_git
*p
,
1498 struct pack_window
**w_curs
,
1500 unsigned long *sizep
)
1502 unsigned char *base
;
1505 enum object_type type
;
1507 /* use_pack() assures us we have [base, base + 20) available
1508 * as a range that we can look at at. (Its actually the hash
1509 * size that is assured.) With our object header encoding
1510 * the maximum deflated object size is 2^137, which is just
1511 * insane, so we know won't exceed what we have been given.
1513 base
= use_pack(p
, w_curs
, *curpos
, &left
);
1514 used
= unpack_object_header_buffer(base
, left
, &type
, sizep
);
1523 const char *packed_object_info_detail(struct packed_git
*p
,
1525 unsigned long *size
,
1526 unsigned long *store_size
,
1527 unsigned int *delta_chain_length
,
1528 unsigned char *base_sha1
)
1530 struct pack_window
*w_curs
= NULL
;
1532 unsigned long dummy
;
1533 unsigned char *next_sha1
;
1534 enum object_type type
;
1535 struct revindex_entry
*revidx
;
1537 *delta_chain_length
= 0;
1538 curpos
= obj_offset
;
1539 type
= unpack_object_header(p
, &w_curs
, &curpos
, size
);
1541 revidx
= find_pack_revindex(p
, obj_offset
);
1542 *store_size
= revidx
[1].offset
- obj_offset
;
1547 die("pack %s contains unknown object type %d",
1548 p
->pack_name
, type
);
1553 unuse_pack(&w_curs
);
1554 return typename(type
);
1556 obj_offset
= get_delta_base(p
, &w_curs
, &curpos
, type
, obj_offset
);
1558 die("pack %s contains bad delta base reference of type %s",
1559 p
->pack_name
, typename(type
));
1560 if (*delta_chain_length
== 0) {
1561 revidx
= find_pack_revindex(p
, obj_offset
);
1562 hashcpy(base_sha1
, nth_packed_object_sha1(p
, revidx
->nr
));
1566 next_sha1
= use_pack(p
, &w_curs
, curpos
, NULL
);
1567 if (*delta_chain_length
== 0)
1568 hashcpy(base_sha1
, next_sha1
);
1569 obj_offset
= find_pack_entry_one(next_sha1
, p
);
1572 (*delta_chain_length
)++;
1573 curpos
= obj_offset
;
1574 type
= unpack_object_header(p
, &w_curs
, &curpos
, &dummy
);
1578 static int packed_object_info(struct packed_git
*p
, off_t obj_offset
,
1579 unsigned long *sizep
)
1581 struct pack_window
*w_curs
= NULL
;
1583 off_t curpos
= obj_offset
;
1584 enum object_type type
;
1586 type
= unpack_object_header(p
, &w_curs
, &curpos
, &size
);
1591 type
= packed_delta_info(p
, &w_curs
, curpos
,
1592 type
, obj_offset
, sizep
);
1602 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1603 type
, (uintmax_t)obj_offset
, p
->pack_name
);
1606 unuse_pack(&w_curs
);
1610 static void *unpack_compressed_entry(struct packed_git
*p
,
1611 struct pack_window
**w_curs
,
1617 unsigned char *buffer
, *in
;
1619 buffer
= xmallocz(size
);
1620 memset(&stream
, 0, sizeof(stream
));
1621 stream
.next_out
= buffer
;
1622 stream
.avail_out
= size
+ 1;
1624 git_inflate_init(&stream
);
1626 in
= use_pack(p
, w_curs
, curpos
, &stream
.avail_in
);
1627 stream
.next_in
= in
;
1628 st
= git_inflate(&stream
, Z_FINISH
);
1629 if (!stream
.avail_out
)
1630 break; /* the payload is larger than it should be */
1631 curpos
+= stream
.next_in
- in
;
1632 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
1633 git_inflate_end(&stream
);
1634 if ((st
!= Z_STREAM_END
) || stream
.total_out
!= size
) {
1642 #define MAX_DELTA_CACHE (256)
1644 static size_t delta_base_cached
;
1646 static struct delta_base_cache_lru_list
{
1647 struct delta_base_cache_lru_list
*prev
;
1648 struct delta_base_cache_lru_list
*next
;
1649 } delta_base_cache_lru
= { &delta_base_cache_lru
, &delta_base_cache_lru
};
1651 static struct delta_base_cache_entry
{
1652 struct delta_base_cache_lru_list lru
;
1654 struct packed_git
*p
;
1657 enum object_type type
;
1658 } delta_base_cache
[MAX_DELTA_CACHE
];
1660 static unsigned long pack_entry_hash(struct packed_git
*p
, off_t base_offset
)
1664 hash
= (unsigned long)p
+ (unsigned long)base_offset
;
1665 hash
+= (hash
>> 8) + (hash
>> 16);
1666 return hash
% MAX_DELTA_CACHE
;
1669 static void *cache_or_unpack_entry(struct packed_git
*p
, off_t base_offset
,
1670 unsigned long *base_size
, enum object_type
*type
, int keep_cache
)
1673 unsigned long hash
= pack_entry_hash(p
, base_offset
);
1674 struct delta_base_cache_entry
*ent
= delta_base_cache
+ hash
;
1677 if (!ret
|| ent
->p
!= p
|| ent
->base_offset
!= base_offset
)
1678 return unpack_entry(p
, base_offset
, type
, base_size
);
1682 ent
->lru
.next
->prev
= ent
->lru
.prev
;
1683 ent
->lru
.prev
->next
= ent
->lru
.next
;
1684 delta_base_cached
-= ent
->size
;
1686 ret
= xmemdupz(ent
->data
, ent
->size
);
1689 *base_size
= ent
->size
;
1693 static inline void release_delta_base_cache(struct delta_base_cache_entry
*ent
)
1698 ent
->lru
.next
->prev
= ent
->lru
.prev
;
1699 ent
->lru
.prev
->next
= ent
->lru
.next
;
1700 delta_base_cached
-= ent
->size
;
1704 void clear_delta_base_cache(void)
1707 for (p
= 0; p
< MAX_DELTA_CACHE
; p
++)
1708 release_delta_base_cache(&delta_base_cache
[p
]);
1711 static void add_delta_base_cache(struct packed_git
*p
, off_t base_offset
,
1712 void *base
, unsigned long base_size
, enum object_type type
)
1714 unsigned long hash
= pack_entry_hash(p
, base_offset
);
1715 struct delta_base_cache_entry
*ent
= delta_base_cache
+ hash
;
1716 struct delta_base_cache_lru_list
*lru
;
1718 release_delta_base_cache(ent
);
1719 delta_base_cached
+= base_size
;
1721 for (lru
= delta_base_cache_lru
.next
;
1722 delta_base_cached
> delta_base_cache_limit
1723 && lru
!= &delta_base_cache_lru
;
1725 struct delta_base_cache_entry
*f
= (void *)lru
;
1726 if (f
->type
== OBJ_BLOB
)
1727 release_delta_base_cache(f
);
1729 for (lru
= delta_base_cache_lru
.next
;
1730 delta_base_cached
> delta_base_cache_limit
1731 && lru
!= &delta_base_cache_lru
;
1733 struct delta_base_cache_entry
*f
= (void *)lru
;
1734 release_delta_base_cache(f
);
1738 ent
->base_offset
= base_offset
;
1741 ent
->size
= base_size
;
1742 ent
->lru
.next
= &delta_base_cache_lru
;
1743 ent
->lru
.prev
= delta_base_cache_lru
.prev
;
1744 delta_base_cache_lru
.prev
->next
= &ent
->lru
;
1745 delta_base_cache_lru
.prev
= &ent
->lru
;
1748 static void *read_object(const unsigned char *sha1
, enum object_type
*type
,
1749 unsigned long *size
);
1751 static void *unpack_delta_entry(struct packed_git
*p
,
1752 struct pack_window
**w_curs
,
1754 unsigned long delta_size
,
1756 enum object_type
*type
,
1757 unsigned long *sizep
)
1759 void *delta_data
, *result
, *base
;
1760 unsigned long base_size
;
1763 base_offset
= get_delta_base(p
, w_curs
, &curpos
, *type
, obj_offset
);
1765 error("failed to validate delta base reference "
1766 "at offset %"PRIuMAX
" from %s",
1767 (uintmax_t)curpos
, p
->pack_name
);
1771 base
= cache_or_unpack_entry(p
, base_offset
, &base_size
, type
, 0);
1774 * We're probably in deep shit, but let's try to fetch
1775 * the required base anyway from another pack or loose.
1776 * This is costly but should happen only in the presence
1777 * of a corrupted pack, and is better than failing outright.
1779 struct revindex_entry
*revidx
;
1780 const unsigned char *base_sha1
;
1781 revidx
= find_pack_revindex(p
, base_offset
);
1784 base_sha1
= nth_packed_object_sha1(p
, revidx
->nr
);
1785 error("failed to read delta base object %s"
1786 " at offset %"PRIuMAX
" from %s",
1787 sha1_to_hex(base_sha1
), (uintmax_t)base_offset
,
1789 mark_bad_packed_object(p
, base_sha1
);
1790 base
= read_object(base_sha1
, type
, &base_size
);
1795 delta_data
= unpack_compressed_entry(p
, w_curs
, curpos
, delta_size
);
1797 error("failed to unpack compressed delta "
1798 "at offset %"PRIuMAX
" from %s",
1799 (uintmax_t)curpos
, p
->pack_name
);
1803 result
= patch_delta(base
, base_size
,
1804 delta_data
, delta_size
,
1807 die("failed to apply delta");
1809 add_delta_base_cache(p
, base_offset
, base
, base_size
, *type
);
1813 int do_check_packed_object_crc
;
1815 void *unpack_entry(struct packed_git
*p
, off_t obj_offset
,
1816 enum object_type
*type
, unsigned long *sizep
)
1818 struct pack_window
*w_curs
= NULL
;
1819 off_t curpos
= obj_offset
;
1822 if (do_check_packed_object_crc
&& p
->index_version
> 1) {
1823 struct revindex_entry
*revidx
= find_pack_revindex(p
, obj_offset
);
1824 unsigned long len
= revidx
[1].offset
- obj_offset
;
1825 if (check_pack_crc(p
, &w_curs
, obj_offset
, len
, revidx
->nr
)) {
1826 const unsigned char *sha1
=
1827 nth_packed_object_sha1(p
, revidx
->nr
);
1828 error("bad packed object CRC for %s",
1830 mark_bad_packed_object(p
, sha1
);
1831 unuse_pack(&w_curs
);
1836 *type
= unpack_object_header(p
, &w_curs
, &curpos
, sizep
);
1840 data
= unpack_delta_entry(p
, &w_curs
, curpos
, *sizep
,
1841 obj_offset
, type
, sizep
);
1847 data
= unpack_compressed_entry(p
, &w_curs
, curpos
, *sizep
);
1851 error("unknown object type %i at offset %"PRIuMAX
" in %s",
1852 *type
, (uintmax_t)obj_offset
, p
->pack_name
);
1854 unuse_pack(&w_curs
);
1858 const unsigned char *nth_packed_object_sha1(struct packed_git
*p
,
1861 const unsigned char *index
= p
->index_data
;
1863 if (open_pack_index(p
))
1865 index
= p
->index_data
;
1867 if (n
>= p
->num_objects
)
1870 if (p
->index_version
== 1) {
1871 return index
+ 24 * n
+ 4;
1874 return index
+ 20 * n
;
1878 off_t
nth_packed_object_offset(const struct packed_git
*p
, uint32_t n
)
1880 const unsigned char *index
= p
->index_data
;
1882 if (p
->index_version
== 1) {
1883 return ntohl(*((uint32_t *)(index
+ 24 * n
)));
1886 index
+= 8 + p
->num_objects
* (20 + 4);
1887 off
= ntohl(*((uint32_t *)(index
+ 4 * n
)));
1888 if (!(off
& 0x80000000))
1890 index
+= p
->num_objects
* 4 + (off
& 0x7fffffff) * 8;
1891 return (((uint64_t)ntohl(*((uint32_t *)(index
+ 0)))) << 32) |
1892 ntohl(*((uint32_t *)(index
+ 4)));
1896 off_t
find_pack_entry_one(const unsigned char *sha1
,
1897 struct packed_git
*p
)
1899 const uint32_t *level1_ofs
= p
->index_data
;
1900 const unsigned char *index
= p
->index_data
;
1901 unsigned hi
, lo
, stride
;
1902 static int use_lookup
= -1;
1903 static int debug_lookup
= -1;
1905 if (debug_lookup
< 0)
1906 debug_lookup
= !!getenv("GIT_DEBUG_LOOKUP");
1909 if (open_pack_index(p
))
1911 level1_ofs
= p
->index_data
;
1912 index
= p
->index_data
;
1914 if (p
->index_version
> 1) {
1919 hi
= ntohl(level1_ofs
[*sha1
]);
1920 lo
= ((*sha1
== 0x0) ? 0 : ntohl(level1_ofs
[*sha1
- 1]));
1921 if (p
->index_version
> 1) {
1929 printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32
"\n",
1930 sha1
[0], sha1
[1], sha1
[2], lo
, hi
, p
->num_objects
);
1933 use_lookup
= !!getenv("GIT_USE_LOOKUP");
1935 int pos
= sha1_entry_pos(index
, stride
, 0,
1936 lo
, hi
, p
->num_objects
, sha1
);
1939 return nth_packed_object_offset(p
, pos
);
1943 unsigned mi
= (lo
+ hi
) / 2;
1944 int cmp
= hashcmp(index
+ mi
* stride
, sha1
);
1947 printf("lo %u hi %u rg %u mi %u\n",
1948 lo
, hi
, hi
- lo
, mi
);
1950 return nth_packed_object_offset(p
, mi
);
1959 static int is_pack_valid(struct packed_git
*p
)
1961 /* An already open pack is known to be valid. */
1962 if (p
->pack_fd
!= -1)
1965 /* If the pack has one window completely covering the
1966 * file size, the pack is known to be valid even if
1967 * the descriptor is not currently open.
1970 struct pack_window
*w
= p
->windows
;
1972 if (!w
->offset
&& w
->len
== p
->pack_size
)
1976 /* Force the pack to open to prove its valid. */
1977 return !open_packed_git(p
);
1980 static int find_pack_entry(const unsigned char *sha1
, struct pack_entry
*e
)
1982 static struct packed_git
*last_found
= (void *)1;
1983 struct packed_git
*p
;
1986 prepare_packed_git();
1989 p
= (last_found
== (void *)1) ? packed_git
: last_found
;
1992 if (p
->num_bad_objects
) {
1994 for (i
= 0; i
< p
->num_bad_objects
; i
++)
1995 if (!hashcmp(sha1
, p
->bad_object_sha1
+ 20 * i
))
1999 offset
= find_pack_entry_one(sha1
, p
);
2002 * We are about to tell the caller where they can
2003 * locate the requested object. We better make
2004 * sure the packfile is still here and can be
2005 * accessed before supplying that answer, as
2006 * it may have been deleted since the index
2009 if (!is_pack_valid(p
)) {
2010 error("packfile %s cannot be accessed", p
->pack_name
);
2015 hashcpy(e
->sha1
, sha1
);
2021 if (p
== last_found
)
2025 if (p
== last_found
)
2031 struct packed_git
*find_sha1_pack(const unsigned char *sha1
,
2032 struct packed_git
*packs
)
2034 struct packed_git
*p
;
2036 for (p
= packs
; p
; p
= p
->next
) {
2037 if (find_pack_entry_one(sha1
, p
))
2044 static int sha1_loose_object_info(const unsigned char *sha1
, unsigned long *sizep
)
2047 unsigned long mapsize
, size
;
2052 map
= map_sha1_file(sha1
, &mapsize
);
2054 return error("unable to find %s", sha1_to_hex(sha1
));
2055 if (unpack_sha1_header(&stream
, map
, mapsize
, hdr
, sizeof(hdr
)) < 0)
2056 status
= error("unable to unpack %s header",
2058 else if ((status
= parse_sha1_header(hdr
, &size
)) < 0)
2059 status
= error("unable to parse %s header", sha1_to_hex(sha1
));
2062 git_inflate_end(&stream
);
2063 munmap(map
, mapsize
);
2067 int sha1_object_info(const unsigned char *sha1
, unsigned long *sizep
)
2069 struct cached_object
*co
;
2070 struct pack_entry e
;
2073 co
= find_cached_object(sha1
);
2080 if (!find_pack_entry(sha1
, &e
)) {
2081 /* Most likely it's a loose object. */
2082 status
= sha1_loose_object_info(sha1
, sizep
);
2086 /* Not a loose object; someone else may have just packed it. */
2087 reprepare_packed_git();
2088 if (!find_pack_entry(sha1
, &e
))
2092 status
= packed_object_info(e
.p
, e
.offset
, sizep
);
2094 mark_bad_packed_object(e
.p
, sha1
);
2095 status
= sha1_object_info(sha1
, sizep
);
2101 static void *read_packed_sha1(const unsigned char *sha1
,
2102 enum object_type
*type
, unsigned long *size
)
2104 struct pack_entry e
;
2107 if (!find_pack_entry(sha1
, &e
))
2109 data
= cache_or_unpack_entry(e
.p
, e
.offset
, size
, type
, 1);
2112 * We're probably in deep shit, but let's try to fetch
2113 * the required object anyway from another pack or loose.
2114 * This should happen only in the presence of a corrupted
2115 * pack, and is better than failing outright.
2117 error("failed to read object %s at offset %"PRIuMAX
" from %s",
2118 sha1_to_hex(sha1
), (uintmax_t)e
.offset
, e
.p
->pack_name
);
2119 mark_bad_packed_object(e
.p
, sha1
);
2120 data
= read_object(sha1
, type
, size
);
2125 int pretend_sha1_file(void *buf
, unsigned long len
, enum object_type type
,
2126 unsigned char *sha1
)
2128 struct cached_object
*co
;
2130 hash_sha1_file(buf
, len
, typename(type
), sha1
);
2131 if (has_sha1_file(sha1
) || find_cached_object(sha1
))
2133 if (cached_object_alloc
<= cached_object_nr
) {
2134 cached_object_alloc
= alloc_nr(cached_object_alloc
);
2135 cached_objects
= xrealloc(cached_objects
,
2136 sizeof(*cached_objects
) *
2137 cached_object_alloc
);
2139 co
= &cached_objects
[cached_object_nr
++];
2142 co
->buf
= xmalloc(len
);
2143 memcpy(co
->buf
, buf
, len
);
2144 hashcpy(co
->sha1
, sha1
);
2148 static void *read_object(const unsigned char *sha1
, enum object_type
*type
,
2149 unsigned long *size
)
2151 unsigned long mapsize
;
2153 struct cached_object
*co
;
2155 co
= find_cached_object(sha1
);
2159 return xmemdupz(co
->buf
, co
->size
);
2162 buf
= read_packed_sha1(sha1
, type
, size
);
2165 map
= map_sha1_file(sha1
, &mapsize
);
2167 buf
= unpack_sha1_file(map
, mapsize
, type
, size
, sha1
);
2168 munmap(map
, mapsize
);
2171 reprepare_packed_git();
2172 return read_packed_sha1(sha1
, type
, size
);
2176 * This function dies on corrupt objects; the callers who want to
2177 * deal with them should arrange to call read_object() and give error
2178 * messages themselves.
2180 void *read_sha1_file_repl(const unsigned char *sha1
,
2181 enum object_type
*type
,
2182 unsigned long *size
,
2183 const unsigned char **replacement
)
2185 const unsigned char *repl
= lookup_replace_object(sha1
);
2188 const struct packed_git
*p
;
2191 data
= read_object(repl
, type
, size
);
2194 *replacement
= repl
;
2198 if (errno
&& errno
!= ENOENT
)
2199 die_errno("failed to read object %s", sha1_to_hex(sha1
));
2201 /* die if we replaced an object with one that does not exist */
2203 die("replacement %s not found for %s",
2204 sha1_to_hex(repl
), sha1_to_hex(sha1
));
2206 if (has_loose_object(repl
)) {
2207 path
= sha1_file_name(sha1
);
2208 die("loose object %s (stored in %s) is corrupt",
2209 sha1_to_hex(repl
), path
);
2212 if ((p
= has_packed_and_bad(repl
)) != NULL
)
2213 die("packed object %s (stored in %s) is corrupt",
2214 sha1_to_hex(repl
), p
->pack_name
);
2219 void *read_object_with_reference(const unsigned char *sha1
,
2220 const char *required_type_name
,
2221 unsigned long *size
,
2222 unsigned char *actual_sha1_return
)
2224 enum object_type type
, required_type
;
2226 unsigned long isize
;
2227 unsigned char actual_sha1
[20];
2229 required_type
= type_from_string(required_type_name
);
2230 hashcpy(actual_sha1
, sha1
);
2232 int ref_length
= -1;
2233 const char *ref_type
= NULL
;
2235 buffer
= read_sha1_file(actual_sha1
, &type
, &isize
);
2238 if (type
== required_type
) {
2240 if (actual_sha1_return
)
2241 hashcpy(actual_sha1_return
, actual_sha1
);
2244 /* Handle references */
2245 else if (type
== OBJ_COMMIT
)
2247 else if (type
== OBJ_TAG
)
2248 ref_type
= "object ";
2253 ref_length
= strlen(ref_type
);
2255 if (ref_length
+ 40 > isize
||
2256 memcmp(buffer
, ref_type
, ref_length
) ||
2257 get_sha1_hex((char *) buffer
+ ref_length
, actual_sha1
)) {
2262 /* Now we have the ID of the referred-to object in
2263 * actual_sha1. Check again. */
2267 static void write_sha1_file_prepare(const void *buf
, unsigned long len
,
2268 const char *type
, unsigned char *sha1
,
2269 char *hdr
, int *hdrlen
)
2273 /* Generate the header */
2274 *hdrlen
= sprintf(hdr
, "%s %lu", type
, len
)+1;
2278 git_SHA1_Update(&c
, hdr
, *hdrlen
);
2279 git_SHA1_Update(&c
, buf
, len
);
2280 git_SHA1_Final(sha1
, &c
);
2284 * Move the just written object into its final resting place.
2285 * NEEDSWORK: this should be renamed to finalize_temp_file() as
2286 * "moving" is only a part of what it does, when no patch between
2287 * master to pu changes the call sites of this function.
2289 int move_temp_to_file(const char *tmpfile
, const char *filename
)
2293 if (object_creation_mode
== OBJECT_CREATION_USES_RENAMES
)
2295 else if (link(tmpfile
, filename
))
2299 * Coda hack - coda doesn't like cross-directory links,
2300 * so we fall back to a rename, which will mean that it
2301 * won't be able to check collisions, but that's not a
2304 * The same holds for FAT formatted media.
2306 * When this succeeds, we just return. We have nothing
2309 if (ret
&& ret
!= EEXIST
) {
2311 if (!rename(tmpfile
, filename
))
2315 unlink_or_warn(tmpfile
);
2317 if (ret
!= EEXIST
) {
2318 return error("unable to write sha1 filename %s: %s\n", filename
, strerror(ret
));
2320 /* FIXME!!! Collision check here ? */
2324 if (adjust_shared_perm(filename
))
2325 return error("unable to set permission to '%s'", filename
);
2329 static int write_buffer(int fd
, const void *buf
, size_t len
)
2331 if (write_in_full(fd
, buf
, len
) < 0)
2332 return error("file write error (%s)", strerror(errno
));
2336 int hash_sha1_file(const void *buf
, unsigned long len
, const char *type
,
2337 unsigned char *sha1
)
2341 write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
2345 /* Finalize a file on disk, and close it. */
2346 static void close_sha1_file(int fd
)
2348 if (fsync_object_files
)
2349 fsync_or_die(fd
, "sha1 file");
2351 die_errno("error when closing sha1 file");
2354 /* Size of directory component, including the ending '/' */
2355 static inline int directory_size(const char *filename
)
2357 const char *s
= strrchr(filename
, '/');
2360 return s
- filename
+ 1;
2364 * This creates a temporary file in the same directory as the final
2367 * We want to avoid cross-directory filename renames, because those
2368 * can have problems on various filesystems (FAT, NFS, Coda).
2370 static int create_tmpfile(char *buffer
, size_t bufsiz
, const char *filename
)
2372 int fd
, dirlen
= directory_size(filename
);
2374 if (dirlen
+ 20 > bufsiz
) {
2375 errno
= ENAMETOOLONG
;
2378 memcpy(buffer
, filename
, dirlen
);
2379 strcpy(buffer
+ dirlen
, "tmp_obj_XXXXXX");
2380 fd
= git_mkstemp_mode(buffer
, 0444);
2381 if (fd
< 0 && dirlen
&& errno
== ENOENT
) {
2382 /* Make sure the directory exists */
2383 memcpy(buffer
, filename
, dirlen
);
2384 buffer
[dirlen
-1] = 0;
2385 if (mkdir(buffer
, 0777) || adjust_shared_perm(buffer
))
2389 strcpy(buffer
+ dirlen
- 1, "/tmp_obj_XXXXXX");
2390 fd
= git_mkstemp_mode(buffer
, 0444);
2395 static int write_loose_object(const unsigned char *sha1
, char *hdr
, int hdrlen
,
2396 const void *buf
, unsigned long len
, time_t mtime
)
2399 unsigned char compressed
[4096];
2402 unsigned char parano_sha1
[20];
2404 static char tmpfile
[PATH_MAX
];
2406 filename
= sha1_file_name(sha1
);
2407 fd
= create_tmpfile(tmpfile
, sizeof(tmpfile
), filename
);
2409 if (errno
== EACCES
)
2410 return error("insufficient permission for adding an object to repository database %s\n", get_object_directory());
2412 return error("unable to create temporary sha1 filename %s: %s\n", tmpfile
, strerror(errno
));
2416 memset(&stream
, 0, sizeof(stream
));
2417 deflateInit(&stream
, zlib_compression_level
);
2418 stream
.next_out
= compressed
;
2419 stream
.avail_out
= sizeof(compressed
);
2422 /* First header.. */
2423 stream
.next_in
= (unsigned char *)hdr
;
2424 stream
.avail_in
= hdrlen
;
2425 while (deflate(&stream
, 0) == Z_OK
)
2427 git_SHA1_Update(&c
, hdr
, hdrlen
);
2429 /* Then the data itself.. */
2430 stream
.next_in
= (void *)buf
;
2431 stream
.avail_in
= len
;
2433 unsigned char *in0
= stream
.next_in
;
2434 ret
= deflate(&stream
, Z_FINISH
);
2435 git_SHA1_Update(&c
, in0
, stream
.next_in
- in0
);
2436 if (write_buffer(fd
, compressed
, stream
.next_out
- compressed
) < 0)
2437 die("unable to write sha1 file");
2438 stream
.next_out
= compressed
;
2439 stream
.avail_out
= sizeof(compressed
);
2440 } while (ret
== Z_OK
);
2442 if (ret
!= Z_STREAM_END
)
2443 die("unable to deflate new object %s (%d)", sha1_to_hex(sha1
), ret
);
2444 ret
= deflateEnd(&stream
);
2446 die("deflateEnd on object %s failed (%d)", sha1_to_hex(sha1
), ret
);
2447 git_SHA1_Final(parano_sha1
, &c
);
2448 if (hashcmp(sha1
, parano_sha1
) != 0)
2449 die("confused by unstable object source data for %s", sha1_to_hex(sha1
));
2451 close_sha1_file(fd
);
2456 utb
.modtime
= mtime
;
2457 if (utime(tmpfile
, &utb
) < 0)
2458 warning("failed utime() on %s: %s",
2459 tmpfile
, strerror(errno
));
2462 return move_temp_to_file(tmpfile
, filename
);
2465 int write_sha1_file(const void *buf
, unsigned long len
, const char *type
, unsigned char *returnsha1
)
2467 unsigned char sha1
[20];
2471 /* Normally if we have it in the pack then we do not bother writing
2472 * it out into .git/objects/??/?{38} file.
2474 write_sha1_file_prepare(buf
, len
, type
, sha1
, hdr
, &hdrlen
);
2476 hashcpy(returnsha1
, sha1
);
2477 if (has_sha1_file(sha1
))
2479 return write_loose_object(sha1
, hdr
, hdrlen
, buf
, len
, 0);
2482 int force_object_loose(const unsigned char *sha1
, time_t mtime
)
2486 enum object_type type
;
2491 if (has_loose_object(sha1
))
2493 buf
= read_packed_sha1(sha1
, &type
, &len
);
2495 return error("cannot read sha1_file for %s", sha1_to_hex(sha1
));
2496 hdrlen
= sprintf(hdr
, "%s %lu", typename(type
), len
) + 1;
2497 ret
= write_loose_object(sha1
, hdr
, hdrlen
, buf
, len
, mtime
);
2503 int has_pack_index(const unsigned char *sha1
)
2506 if (stat(sha1_pack_index_name(sha1
), &st
))
2511 int has_sha1_pack(const unsigned char *sha1
)
2513 struct pack_entry e
;
2514 return find_pack_entry(sha1
, &e
);
2517 int has_sha1_file(const unsigned char *sha1
)
2519 struct pack_entry e
;
2521 if (find_pack_entry(sha1
, &e
))
2523 return has_loose_object(sha1
);
2526 static void check_tree(const void *buf
, size_t size
)
2528 struct tree_desc desc
;
2529 struct name_entry entry
;
2531 init_tree_desc(&desc
, buf
, size
);
2532 while (tree_entry(&desc
, &entry
))
2534 * tree_entry() will die() on malformed entries */
2538 static void check_commit(const void *buf
, size_t size
)
2541 memset(&c
, 0, sizeof(c
));
2542 if (parse_commit_buffer(&c
, buf
, size
))
2543 die("corrupt commit");
2546 static void check_tag(const void *buf
, size_t size
)
2549 memset(&t
, 0, sizeof(t
));
2550 if (parse_tag_buffer(&t
, buf
, size
))
2554 static int index_mem(unsigned char *sha1
, void *buf
, size_t size
,
2555 int write_object
, enum object_type type
,
2556 const char *path
, int format_check
)
2558 int ret
, re_allocated
= 0;
2564 * Convert blobs to git internal format
2566 if ((type
== OBJ_BLOB
) && path
) {
2567 struct strbuf nbuf
= STRBUF_INIT
;
2568 if (convert_to_git(path
, buf
, size
, &nbuf
,
2569 write_object
? safe_crlf
: 0)) {
2570 buf
= strbuf_detach(&nbuf
, &size
);
2575 if (type
== OBJ_TREE
)
2576 check_tree(buf
, size
);
2577 if (type
== OBJ_COMMIT
)
2578 check_commit(buf
, size
);
2579 if (type
== OBJ_TAG
)
2580 check_tag(buf
, size
);
2584 ret
= write_sha1_file(buf
, size
, typename(type
), sha1
);
2586 ret
= hash_sha1_file(buf
, size
, typename(type
), sha1
);
2592 #define SMALL_FILE_SIZE (32*1024)
2594 int index_fd(unsigned char *sha1
, int fd
, struct stat
*st
, int write_object
,
2595 enum object_type type
, const char *path
, int format_check
)
2598 size_t size
= xsize_t(st
->st_size
);
2600 if (!S_ISREG(st
->st_mode
)) {
2601 struct strbuf sbuf
= STRBUF_INIT
;
2602 if (strbuf_read(&sbuf
, fd
, 4096) >= 0)
2603 ret
= index_mem(sha1
, sbuf
.buf
, sbuf
.len
, write_object
,
2604 type
, path
, format_check
);
2607 strbuf_release(&sbuf
);
2609 ret
= index_mem(sha1
, NULL
, size
, write_object
, type
, path
,
2611 } else if (size
<= SMALL_FILE_SIZE
) {
2612 char *buf
= xmalloc(size
);
2613 if (size
== read_in_full(fd
, buf
, size
))
2614 ret
= index_mem(sha1
, buf
, size
, write_object
, type
,
2615 path
, format_check
);
2617 ret
= error("short read %s", strerror(errno
));
2620 void *buf
= xmmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2621 ret
= index_mem(sha1
, buf
, size
, write_object
, type
, path
,
2629 int index_path(unsigned char *sha1
, const char *path
, struct stat
*st
, int write_object
)
2632 struct strbuf sb
= STRBUF_INIT
;
2634 switch (st
->st_mode
& S_IFMT
) {
2636 fd
= open(path
, O_RDONLY
);
2638 return error("open(\"%s\"): %s", path
,
2640 if (index_fd(sha1
, fd
, st
, write_object
, OBJ_BLOB
, path
, 0) < 0)
2641 return error("%s: failed to insert into database",
2645 if (strbuf_readlink(&sb
, path
, st
->st_size
)) {
2646 char *errstr
= strerror(errno
);
2647 return error("readlink(\"%s\"): %s", path
,
2651 hash_sha1_file(sb
.buf
, sb
.len
, blob_type
, sha1
);
2652 else if (write_sha1_file(sb
.buf
, sb
.len
, blob_type
, sha1
))
2653 return error("%s: failed to insert into database",
2655 strbuf_release(&sb
);
2658 return resolve_gitlink_ref(path
, "HEAD", sha1
);
2660 return error("%s: unsupported file type", path
);
2665 int read_pack_header(int fd
, struct pack_header
*header
)
2667 if (read_in_full(fd
, header
, sizeof(*header
)) < sizeof(*header
))
2668 /* "eof before pack header was fully read" */
2669 return PH_ERROR_EOF
;
2671 if (header
->hdr_signature
!= htonl(PACK_SIGNATURE
))
2672 /* "protocol error (pack signature mismatch detected)" */
2673 return PH_ERROR_PACK_SIGNATURE
;
2674 if (!pack_version_ok(header
->hdr_version
))
2675 /* "protocol error (pack version unsupported)" */
2676 return PH_ERROR_PROTOCOL
;
2680 void assert_sha1_type(const unsigned char *sha1
, enum object_type expect
)
2682 enum object_type type
= sha1_object_info(sha1
, NULL
);
2684 die("%s is not a valid object", sha1_to_hex(sha1
));
2686 die("%s is not a valid '%s' object", sha1_to_hex(sha1
),