3 #include "repository.h"
13 #include "csum-file.h"
16 #include "run-command.h"
18 #include "object-store.h"
20 #include "commit-reach.h"
22 #define PACK_ID_BITS 16
23 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
25 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
28 * We abuse the setuid bit on directories to mean "do not delta".
30 #define NO_DELTA S_ISUID
33 * The amount of additional space required in order to write an object into the
34 * current pack. This is the hash lengths at the end of the pack, plus the
35 * length of one object ID.
37 #define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3)
40 struct pack_idx_entry idx
;
41 struct object_entry
*next
;
42 uint32_t type
: TYPE_BITS
,
43 pack_id
: PACK_ID_BITS
,
47 struct object_entry_pool
{
48 struct object_entry_pool
*next_pool
;
49 struct object_entry
*next_free
;
50 struct object_entry
*end
;
51 struct object_entry entries
[FLEX_ARRAY
]; /* more */
56 struct object_entry
*marked
[1024];
57 struct mark_set
*sets
[1024];
70 struct atom_str
*next_atom
;
71 unsigned short str_len
;
72 char str_dat
[FLEX_ARRAY
]; /* more */
77 struct tree_content
*tree
;
78 struct atom_str
*name
;
79 struct tree_entry_ms
{
86 unsigned int entry_capacity
; /* must match avail_tree_content */
87 unsigned int entry_count
;
88 unsigned int delta_depth
;
89 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
92 struct avail_tree_content
{
93 unsigned int entry_capacity
; /* must match tree_content */
94 struct avail_tree_content
*next_avail
;
98 struct branch
*table_next_branch
;
99 struct branch
*active_next_branch
;
101 struct tree_entry branch_tree
;
102 uintmax_t last_commit
;
106 unsigned pack_id
: PACK_ID_BITS
;
107 struct object_id oid
;
111 struct tag
*next_tag
;
113 unsigned int pack_id
;
114 struct object_id oid
;
118 struct hash_list
*next
;
119 struct object_id oid
;
128 struct recent_command
{
129 struct recent_command
*prev
;
130 struct recent_command
*next
;
134 /* Configured limits on output */
135 static unsigned long max_depth
= 50;
136 static off_t max_packsize
;
137 static int unpack_limit
= 100;
138 static int force_update
;
140 /* Stats and misc. counters */
141 static uintmax_t alloc_count
;
142 static uintmax_t marks_set_count
;
143 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
144 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
145 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
146 static uintmax_t delta_count_attempts_by_type
[1 << TYPE_BITS
];
147 static unsigned long object_count
;
148 static unsigned long branch_count
;
149 static unsigned long branch_load_count
;
151 static FILE *pack_edges
;
152 static unsigned int show_stats
= 1;
153 static int global_argc
;
154 static const char **global_argv
;
157 static struct mem_pool fi_mem_pool
= {NULL
, 2*1024*1024 -
158 sizeof(struct mp_block
), 0 };
160 /* Atom management */
161 static unsigned int atom_table_sz
= 4451;
162 static unsigned int atom_cnt
;
163 static struct atom_str
**atom_table
;
165 /* The .pack file being generated */
166 static struct pack_idx_option pack_idx_opts
;
167 static unsigned int pack_id
;
168 static struct hashfile
*pack_file
;
169 static struct packed_git
*pack_data
;
170 static struct packed_git
**all_packs
;
171 static off_t pack_size
;
173 /* Table of objects we've written. */
174 static unsigned int object_entry_alloc
= 5000;
175 static struct object_entry_pool
*blocks
;
176 static struct object_entry
*object_table
[1 << 16];
177 static struct mark_set
*marks
;
178 static const char *export_marks_file
;
179 static const char *import_marks_file
;
180 static int import_marks_file_from_stream
;
181 static int import_marks_file_ignore_missing
;
182 static int import_marks_file_done
;
183 static int relative_marks_paths
;
186 static struct last_object last_blob
= { STRBUF_INIT
, 0, 0, 0 };
188 /* Tree management */
189 static unsigned int tree_entry_alloc
= 1000;
190 static void *avail_tree_entry
;
191 static unsigned int avail_tree_table_sz
= 100;
192 static struct avail_tree_content
**avail_tree_table
;
193 static size_t tree_entry_allocd
;
194 static struct strbuf old_tree
= STRBUF_INIT
;
195 static struct strbuf new_tree
= STRBUF_INIT
;
198 static unsigned long max_active_branches
= 5;
199 static unsigned long cur_active_branches
;
200 static unsigned long branch_table_sz
= 1039;
201 static struct branch
**branch_table
;
202 static struct branch
*active_branches
;
205 static struct tag
*first_tag
;
206 static struct tag
*last_tag
;
208 /* Input stream parsing */
209 static whenspec_type whenspec
= WHENSPEC_RAW
;
210 static struct strbuf command_buf
= STRBUF_INIT
;
211 static int unread_command_buf
;
212 static struct recent_command cmd_hist
= {&cmd_hist
, &cmd_hist
, NULL
};
213 static struct recent_command
*cmd_tail
= &cmd_hist
;
214 static struct recent_command
*rc_free
;
215 static unsigned int cmd_save
= 100;
216 static uintmax_t next_mark
;
217 static struct strbuf new_data
= STRBUF_INIT
;
218 static int seen_data_command
;
219 static int require_explicit_termination
;
221 /* Signal handling */
222 static volatile sig_atomic_t checkpoint_requested
;
224 /* Where to write output of cat-blob commands */
225 static int cat_blob_fd
= STDOUT_FILENO
;
227 static void parse_argv(void);
228 static void parse_get_mark(const char *p
);
229 static void parse_cat_blob(const char *p
);
230 static void parse_ls(const char *p
, struct branch
*b
);
232 static void write_branch_report(FILE *rpt
, struct branch
*b
)
234 fprintf(rpt
, "%s:\n", b
->name
);
236 fprintf(rpt
, " status :");
238 fputs(" active", rpt
);
239 if (b
->branch_tree
.tree
)
240 fputs(" loaded", rpt
);
241 if (is_null_oid(&b
->branch_tree
.versions
[1].oid
))
242 fputs(" dirty", rpt
);
245 fprintf(rpt
, " tip commit : %s\n", oid_to_hex(&b
->oid
));
246 fprintf(rpt
, " old tree : %s\n",
247 oid_to_hex(&b
->branch_tree
.versions
[0].oid
));
248 fprintf(rpt
, " cur tree : %s\n",
249 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
250 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
252 fputs(" last pack : ", rpt
);
253 if (b
->pack_id
< MAX_PACK_ID
)
254 fprintf(rpt
, "%u", b
->pack_id
);
260 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set
*);
262 static void write_crash_report(const char *err
)
264 char *loc
= git_pathdup("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
265 FILE *rpt
= fopen(loc
, "w");
268 struct recent_command
*rc
;
271 error_errno("can't write crash report %s", loc
);
276 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
278 fprintf(rpt
, "fast-import crash report:\n");
279 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
280 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
281 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_MODE(ISO8601
)));
284 fputs("fatal: ", rpt
);
289 fputs("Most Recent Commands Before Crash\n", rpt
);
290 fputs("---------------------------------\n", rpt
);
291 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
292 if (rc
->next
== &cmd_hist
)
301 fputs("Active Branch LRU\n", rpt
);
302 fputs("-----------------\n", rpt
);
303 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
305 max_active_branches
);
307 fputs(" pos clock name\n", rpt
);
308 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
309 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
310 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
311 ++lu
, b
->last_commit
, b
->name
);
314 fputs("Inactive Branches\n", rpt
);
315 fputs("-----------------\n", rpt
);
316 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
317 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
318 write_branch_report(rpt
, b
);
324 fputs("Annotated Tags\n", rpt
);
325 fputs("--------------\n", rpt
);
326 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
327 fputs(oid_to_hex(&tg
->oid
), rpt
);
329 fputs(tg
->name
, rpt
);
335 fputs("Marks\n", rpt
);
336 fputs("-----\n", rpt
);
337 if (export_marks_file
)
338 fprintf(rpt
, " exported to %s\n", export_marks_file
);
340 dump_marks_helper(rpt
, 0, marks
);
343 fputs("-------------------\n", rpt
);
344 fputs("END OF CRASH REPORT\n", rpt
);
349 static void end_packfile(void);
350 static void unkeep_all_packs(void);
351 static void dump_marks(void);
353 static NORETURN
void die_nicely(const char *err
, va_list params
)
356 char message
[2 * PATH_MAX
];
358 vsnprintf(message
, sizeof(message
), err
, params
);
359 fputs("fatal: ", stderr
);
360 fputs(message
, stderr
);
365 write_crash_report(message
);
373 #ifndef SIGUSR1 /* Windows, for example */
375 static void set_checkpoint_signal(void)
381 static void checkpoint_signal(int signo
)
383 checkpoint_requested
= 1;
386 static void set_checkpoint_signal(void)
390 memset(&sa
, 0, sizeof(sa
));
391 sa
.sa_handler
= checkpoint_signal
;
392 sigemptyset(&sa
.sa_mask
);
393 sa
.sa_flags
= SA_RESTART
;
394 sigaction(SIGUSR1
, &sa
, NULL
);
399 static void alloc_objects(unsigned int cnt
)
401 struct object_entry_pool
*b
;
403 b
= xmalloc(sizeof(struct object_entry_pool
)
404 + cnt
* sizeof(struct object_entry
));
405 b
->next_pool
= blocks
;
406 b
->next_free
= b
->entries
;
407 b
->end
= b
->entries
+ cnt
;
412 static struct object_entry
*new_object(struct object_id
*oid
)
414 struct object_entry
*e
;
416 if (blocks
->next_free
== blocks
->end
)
417 alloc_objects(object_entry_alloc
);
419 e
= blocks
->next_free
++;
420 oidcpy(&e
->idx
.oid
, oid
);
424 static struct object_entry
*find_object(struct object_id
*oid
)
426 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
427 struct object_entry
*e
;
428 for (e
= object_table
[h
]; e
; e
= e
->next
)
429 if (oideq(oid
, &e
->idx
.oid
))
434 static struct object_entry
*insert_object(struct object_id
*oid
)
436 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
437 struct object_entry
*e
= object_table
[h
];
440 if (oideq(oid
, &e
->idx
.oid
))
446 e
->next
= object_table
[h
];
452 static void invalidate_pack_id(unsigned int id
)
458 for (h
= 0; h
< ARRAY_SIZE(object_table
); h
++) {
459 struct object_entry
*e
;
461 for (e
= object_table
[h
]; e
; e
= e
->next
)
462 if (e
->pack_id
== id
)
463 e
->pack_id
= MAX_PACK_ID
;
466 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
469 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
470 if (b
->pack_id
== id
)
471 b
->pack_id
= MAX_PACK_ID
;
474 for (t
= first_tag
; t
; t
= t
->next_tag
)
475 if (t
->pack_id
== id
)
476 t
->pack_id
= MAX_PACK_ID
;
479 static unsigned int hc_str(const char *s
, size_t len
)
487 static char *pool_strdup(const char *s
)
489 size_t len
= strlen(s
) + 1;
490 char *r
= mem_pool_alloc(&fi_mem_pool
, len
);
495 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
497 struct mark_set
*s
= marks
;
498 while ((idnum
>> s
->shift
) >= 1024) {
499 s
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
500 s
->shift
= marks
->shift
+ 10;
501 s
->data
.sets
[0] = marks
;
505 uintmax_t i
= idnum
>> s
->shift
;
506 idnum
-= i
<< s
->shift
;
507 if (!s
->data
.sets
[i
]) {
508 s
->data
.sets
[i
] = mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
509 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
513 if (!s
->data
.marked
[idnum
])
515 s
->data
.marked
[idnum
] = oe
;
518 static struct object_entry
*find_mark(uintmax_t idnum
)
520 uintmax_t orig_idnum
= idnum
;
521 struct mark_set
*s
= marks
;
522 struct object_entry
*oe
= NULL
;
523 if ((idnum
>> s
->shift
) < 1024) {
524 while (s
&& s
->shift
) {
525 uintmax_t i
= idnum
>> s
->shift
;
526 idnum
-= i
<< s
->shift
;
530 oe
= s
->data
.marked
[idnum
];
533 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
537 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
539 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
542 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
543 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
546 c
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct atom_str
) + len
+ 1);
548 memcpy(c
->str_dat
, s
, len
);
550 c
->next_atom
= atom_table
[hc
];
556 static struct branch
*lookup_branch(const char *name
)
558 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
561 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
562 if (!strcmp(name
, b
->name
))
567 static struct branch
*new_branch(const char *name
)
569 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
570 struct branch
*b
= lookup_branch(name
);
573 die("Invalid attempt to create duplicate branch: %s", name
);
574 if (check_refname_format(name
, REFNAME_ALLOW_ONELEVEL
))
575 die("Branch name doesn't conform to GIT standards: %s", name
);
577 b
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct branch
));
578 b
->name
= pool_strdup(name
);
579 b
->table_next_branch
= branch_table
[hc
];
580 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
581 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
584 b
->pack_id
= MAX_PACK_ID
;
585 branch_table
[hc
] = b
;
590 static unsigned int hc_entries(unsigned int cnt
)
592 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
593 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
596 static struct tree_content
*new_tree_content(unsigned int cnt
)
598 struct avail_tree_content
*f
, *l
= NULL
;
599 struct tree_content
*t
;
600 unsigned int hc
= hc_entries(cnt
);
602 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
603 if (f
->entry_capacity
>= cnt
)
608 l
->next_avail
= f
->next_avail
;
610 avail_tree_table
[hc
] = f
->next_avail
;
612 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
613 f
= mem_pool_alloc(&fi_mem_pool
, sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
614 f
->entry_capacity
= cnt
;
617 t
= (struct tree_content
*)f
;
623 static void release_tree_entry(struct tree_entry
*e
);
624 static void release_tree_content(struct tree_content
*t
)
626 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
627 unsigned int hc
= hc_entries(f
->entry_capacity
);
628 f
->next_avail
= avail_tree_table
[hc
];
629 avail_tree_table
[hc
] = f
;
632 static void release_tree_content_recursive(struct tree_content
*t
)
635 for (i
= 0; i
< t
->entry_count
; i
++)
636 release_tree_entry(t
->entries
[i
]);
637 release_tree_content(t
);
640 static struct tree_content
*grow_tree_content(
641 struct tree_content
*t
,
644 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
645 r
->entry_count
= t
->entry_count
;
646 r
->delta_depth
= t
->delta_depth
;
647 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
648 release_tree_content(t
);
652 static struct tree_entry
*new_tree_entry(void)
654 struct tree_entry
*e
;
656 if (!avail_tree_entry
) {
657 unsigned int n
= tree_entry_alloc
;
658 tree_entry_allocd
+= n
* sizeof(struct tree_entry
);
660 avail_tree_entry
= e
;
662 *((void**)e
) = e
+ 1;
668 e
= avail_tree_entry
;
669 avail_tree_entry
= *((void**)e
);
673 static void release_tree_entry(struct tree_entry
*e
)
676 release_tree_content_recursive(e
->tree
);
677 *((void**)e
) = avail_tree_entry
;
678 avail_tree_entry
= e
;
681 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
683 struct tree_content
*d
;
684 struct tree_entry
*a
, *b
;
689 d
= new_tree_content(s
->entry_count
);
690 for (i
= 0; i
< s
->entry_count
; i
++) {
692 b
= new_tree_entry();
693 memcpy(b
, a
, sizeof(*a
));
694 if (a
->tree
&& is_null_oid(&b
->versions
[1].oid
))
695 b
->tree
= dup_tree_content(a
->tree
);
700 d
->entry_count
= s
->entry_count
;
701 d
->delta_depth
= s
->delta_depth
;
706 static void start_packfile(void)
708 struct strbuf tmp_file
= STRBUF_INIT
;
709 struct packed_git
*p
;
710 struct pack_header hdr
;
713 pack_fd
= odb_mkstemp(&tmp_file
, "pack/tmp_pack_XXXXXX");
714 FLEX_ALLOC_STR(p
, pack_name
, tmp_file
.buf
);
715 strbuf_release(&tmp_file
);
717 p
->pack_fd
= pack_fd
;
719 pack_file
= hashfd(pack_fd
, p
->pack_name
);
721 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
722 hdr
.hdr_version
= htonl(2);
724 hashwrite(pack_file
, &hdr
, sizeof(hdr
));
727 pack_size
= sizeof(hdr
);
730 REALLOC_ARRAY(all_packs
, pack_id
+ 1);
731 all_packs
[pack_id
] = p
;
734 static const char *create_index(void)
737 struct pack_idx_entry
**idx
, **c
, **last
;
738 struct object_entry
*e
;
739 struct object_entry_pool
*o
;
741 /* Build the table of object IDs. */
742 ALLOC_ARRAY(idx
, object_count
);
744 for (o
= blocks
; o
; o
= o
->next_pool
)
745 for (e
= o
->next_free
; e
-- != o
->entries
;)
746 if (pack_id
== e
->pack_id
)
748 last
= idx
+ object_count
;
750 die("internal consistency error creating the index");
752 tmpfile
= write_idx_file(NULL
, idx
, object_count
, &pack_idx_opts
,
758 static char *keep_pack(const char *curr_index_name
)
760 static const char *keep_msg
= "fast-import";
761 struct strbuf name
= STRBUF_INIT
;
764 odb_pack_name(&name
, pack_data
->hash
, "keep");
765 keep_fd
= odb_pack_keep(name
.buf
);
767 die_errno("cannot create keep file");
768 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
770 die_errno("failed to write keep file");
772 odb_pack_name(&name
, pack_data
->hash
, "pack");
773 if (finalize_object_file(pack_data
->pack_name
, name
.buf
))
774 die("cannot store pack file");
776 odb_pack_name(&name
, pack_data
->hash
, "idx");
777 if (finalize_object_file(curr_index_name
, name
.buf
))
778 die("cannot store index file");
779 free((void *)curr_index_name
);
780 return strbuf_detach(&name
, NULL
);
783 static void unkeep_all_packs(void)
785 struct strbuf name
= STRBUF_INIT
;
788 for (k
= 0; k
< pack_id
; k
++) {
789 struct packed_git
*p
= all_packs
[k
];
790 odb_pack_name(&name
, p
->hash
, "keep");
791 unlink_or_warn(name
.buf
);
793 strbuf_release(&name
);
796 static int loosen_small_pack(const struct packed_git
*p
)
798 struct child_process unpack
= CHILD_PROCESS_INIT
;
800 if (lseek(p
->pack_fd
, 0, SEEK_SET
) < 0)
801 die_errno("Failed seeking to start of '%s'", p
->pack_name
);
803 unpack
.in
= p
->pack_fd
;
805 unpack
.stdout_to_stderr
= 1;
806 argv_array_push(&unpack
.args
, "unpack-objects");
808 argv_array_push(&unpack
.args
, "-q");
810 return run_command(&unpack
);
813 static void end_packfile(void)
817 if (running
|| !pack_data
)
821 clear_delta_base_cache();
823 struct packed_git
*new_p
;
824 struct object_id cur_pack_oid
;
830 close_pack_windows(pack_data
);
831 finalize_hashfile(pack_file
, cur_pack_oid
.hash
, 0);
832 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->hash
,
833 pack_data
->pack_name
, object_count
,
834 cur_pack_oid
.hash
, pack_size
);
836 if (object_count
<= unpack_limit
) {
837 if (!loosen_small_pack(pack_data
)) {
838 invalidate_pack_id(pack_id
);
843 close(pack_data
->pack_fd
);
844 idx_name
= keep_pack(create_index());
846 /* Register the packfile with core git's machinery. */
847 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
849 die("core git rejected index %s", idx_name
);
850 all_packs
[pack_id
] = new_p
;
851 install_packed_git(the_repository
, new_p
);
854 /* Print the boundary */
856 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
857 for (i
= 0; i
< branch_table_sz
; i
++) {
858 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
859 if (b
->pack_id
== pack_id
)
860 fprintf(pack_edges
, " %s",
861 oid_to_hex(&b
->oid
));
864 for (t
= first_tag
; t
; t
= t
->next_tag
) {
865 if (t
->pack_id
== pack_id
)
866 fprintf(pack_edges
, " %s",
867 oid_to_hex(&t
->oid
));
869 fputc('\n', pack_edges
);
877 close(pack_data
->pack_fd
);
878 unlink_or_warn(pack_data
->pack_name
);
880 FREE_AND_NULL(pack_data
);
883 /* We can't carry a delta across packfiles. */
884 strbuf_release(&last_blob
.data
);
885 last_blob
.offset
= 0;
889 static void cycle_packfile(void)
895 static int store_object(
896 enum object_type type
,
898 struct last_object
*last
,
899 struct object_id
*oidout
,
903 struct object_entry
*e
;
904 unsigned char hdr
[96];
905 struct object_id oid
;
906 unsigned long hdrlen
, deltalen
;
910 hdrlen
= xsnprintf((char *)hdr
, sizeof(hdr
), "%s %lu",
911 type_name(type
), (unsigned long)dat
->len
) + 1;
912 the_hash_algo
->init_fn(&c
);
913 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
914 the_hash_algo
->update_fn(&c
, dat
->buf
, dat
->len
);
915 the_hash_algo
->final_fn(oid
.hash
, &c
);
917 oidcpy(oidout
, &oid
);
919 e
= insert_object(&oid
);
921 insert_mark(mark
, e
);
923 duplicate_count_by_type
[type
]++;
925 } else if (find_sha1_pack(oid
.hash
,
926 get_all_packs(the_repository
))) {
928 e
->pack_id
= MAX_PACK_ID
;
929 e
->idx
.offset
= 1; /* just not zero! */
930 duplicate_count_by_type
[type
]++;
934 if (last
&& last
->data
.len
&& last
->data
.buf
&& last
->depth
< max_depth
935 && dat
->len
> the_hash_algo
->rawsz
) {
937 delta_count_attempts_by_type
[type
]++;
938 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
940 &deltalen
, dat
->len
- the_hash_algo
->rawsz
);
944 git_deflate_init(&s
, pack_compression_level
);
947 s
.avail_in
= deltalen
;
949 s
.next_in
= (void *)dat
->buf
;
950 s
.avail_in
= dat
->len
;
952 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
953 s
.next_out
= out
= xmalloc(s
.avail_out
);
954 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
958 /* Determine if we should auto-checkpoint. */
960 && (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) > max_packsize
)
961 || (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) < pack_size
) {
963 /* This new object needs to *not* have the current pack_id. */
964 e
->pack_id
= pack_id
+ 1;
967 /* We cannot carry a delta into the new pack. */
969 FREE_AND_NULL(delta
);
971 git_deflate_init(&s
, pack_compression_level
);
972 s
.next_in
= (void *)dat
->buf
;
973 s
.avail_in
= dat
->len
;
974 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
975 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
976 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
983 e
->pack_id
= pack_id
;
984 e
->idx
.offset
= pack_size
;
986 object_count_by_type
[type
]++;
988 crc32_begin(pack_file
);
991 off_t ofs
= e
->idx
.offset
- last
->offset
;
992 unsigned pos
= sizeof(hdr
) - 1;
994 delta_count_by_type
[type
]++;
995 e
->depth
= last
->depth
+ 1;
997 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
998 OBJ_OFS_DELTA
, deltalen
);
999 hashwrite(pack_file
, hdr
, hdrlen
);
1000 pack_size
+= hdrlen
;
1002 hdr
[pos
] = ofs
& 127;
1004 hdr
[--pos
] = 128 | (--ofs
& 127);
1005 hashwrite(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
1006 pack_size
+= sizeof(hdr
) - pos
;
1009 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1011 hashwrite(pack_file
, hdr
, hdrlen
);
1012 pack_size
+= hdrlen
;
1015 hashwrite(pack_file
, out
, s
.total_out
);
1016 pack_size
+= s
.total_out
;
1018 e
->idx
.crc32
= crc32_end(pack_file
);
1023 if (last
->no_swap
) {
1026 strbuf_swap(&last
->data
, dat
);
1028 last
->offset
= e
->idx
.offset
;
1029 last
->depth
= e
->depth
;
1034 static void truncate_pack(struct hashfile_checkpoint
*checkpoint
)
1036 if (hashfile_truncate(pack_file
, checkpoint
))
1037 die_errno("cannot truncate pack to skip duplicate");
1038 pack_size
= checkpoint
->offset
;
1041 static void stream_blob(uintmax_t len
, struct object_id
*oidout
, uintmax_t mark
)
1043 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1044 unsigned char *in_buf
= xmalloc(in_sz
);
1045 unsigned char *out_buf
= xmalloc(out_sz
);
1046 struct object_entry
*e
;
1047 struct object_id oid
;
1048 unsigned long hdrlen
;
1052 struct hashfile_checkpoint checkpoint
;
1055 /* Determine if we should auto-checkpoint. */
1057 && (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) > max_packsize
)
1058 || (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) < pack_size
)
1061 hashfile_checkpoint(pack_file
, &checkpoint
);
1062 offset
= checkpoint
.offset
;
1064 hdrlen
= xsnprintf((char *)out_buf
, out_sz
, "blob %" PRIuMAX
, len
) + 1;
1066 the_hash_algo
->init_fn(&c
);
1067 the_hash_algo
->update_fn(&c
, out_buf
, hdrlen
);
1069 crc32_begin(pack_file
);
1071 git_deflate_init(&s
, pack_compression_level
);
1073 hdrlen
= encode_in_pack_object_header(out_buf
, out_sz
, OBJ_BLOB
, len
);
1075 s
.next_out
= out_buf
+ hdrlen
;
1076 s
.avail_out
= out_sz
- hdrlen
;
1078 while (status
!= Z_STREAM_END
) {
1079 if (0 < len
&& !s
.avail_in
) {
1080 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1081 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1082 if (!n
&& feof(stdin
))
1083 die("EOF in data (%" PRIuMAX
" bytes remaining)", len
);
1085 the_hash_algo
->update_fn(&c
, in_buf
, n
);
1091 status
= git_deflate(&s
, len
? 0 : Z_FINISH
);
1093 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1094 size_t n
= s
.next_out
- out_buf
;
1095 hashwrite(pack_file
, out_buf
, n
);
1097 s
.next_out
= out_buf
;
1098 s
.avail_out
= out_sz
;
1107 die("unexpected deflate failure: %d", status
);
1110 git_deflate_end(&s
);
1111 the_hash_algo
->final_fn(oid
.hash
, &c
);
1114 oidcpy(oidout
, &oid
);
1116 e
= insert_object(&oid
);
1119 insert_mark(mark
, e
);
1121 if (e
->idx
.offset
) {
1122 duplicate_count_by_type
[OBJ_BLOB
]++;
1123 truncate_pack(&checkpoint
);
1125 } else if (find_sha1_pack(oid
.hash
,
1126 get_all_packs(the_repository
))) {
1128 e
->pack_id
= MAX_PACK_ID
;
1129 e
->idx
.offset
= 1; /* just not zero! */
1130 duplicate_count_by_type
[OBJ_BLOB
]++;
1131 truncate_pack(&checkpoint
);
1136 e
->pack_id
= pack_id
;
1137 e
->idx
.offset
= offset
;
1138 e
->idx
.crc32
= crc32_end(pack_file
);
1140 object_count_by_type
[OBJ_BLOB
]++;
1147 /* All calls must be guarded by find_object() or find_mark() to
1148 * ensure the 'struct object_entry' passed was written by this
1149 * process instance. We unpack the entry by the offset, avoiding
1150 * the need for the corresponding .idx file. This unpacking rule
1151 * works because we only use OBJ_REF_DELTA within the packfiles
1152 * created by fast-import.
1154 * oe must not be NULL. Such an oe usually comes from giving
1155 * an unknown SHA-1 to find_object() or an undefined mark to
1156 * find_mark(). Callers must test for this condition and use
1157 * the standard read_sha1_file() when it happens.
1159 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1160 * find_mark(), where the mark was reloaded from an existing marks
1161 * file and is referencing an object that this fast-import process
1162 * instance did not write out to a packfile. Callers must test for
1163 * this condition and use read_sha1_file() instead.
1165 static void *gfi_unpack_entry(
1166 struct object_entry
*oe
,
1167 unsigned long *sizep
)
1169 enum object_type type
;
1170 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1171 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ the_hash_algo
->rawsz
)) {
1172 /* The object is stored in the packfile we are writing to
1173 * and we have modified it since the last time we scanned
1174 * back to read a previously written object. If an old
1175 * window covered [p->pack_size, p->pack_size + rawsz) its
1176 * data is stale and is not valid. Closing all windows
1177 * and updating the packfile length ensures we can read
1178 * the newly written data.
1180 close_pack_windows(p
);
1181 hashflush(pack_file
);
1183 /* We have to offer rawsz bytes additional on the end of
1184 * the packfile as the core unpacker code assumes the
1185 * footer is present at the file end and must promise
1186 * at least rawsz bytes within any window it maps. But
1187 * we don't actually create the footer here.
1189 p
->pack_size
= pack_size
+ the_hash_algo
->rawsz
;
1191 return unpack_entry(the_repository
, p
, oe
->idx
.offset
, &type
, sizep
);
1194 static const char *get_mode(const char *str
, uint16_t *modep
)
1199 while ((c
= *str
++) != ' ') {
1200 if (c
< '0' || c
> '7')
1202 mode
= (mode
<< 3) + (c
- '0');
1208 static void load_tree(struct tree_entry
*root
)
1210 struct object_id
*oid
= &root
->versions
[1].oid
;
1211 struct object_entry
*myoe
;
1212 struct tree_content
*t
;
1217 root
->tree
= t
= new_tree_content(8);
1218 if (is_null_oid(oid
))
1221 myoe
= find_object(oid
);
1222 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1223 if (myoe
->type
!= OBJ_TREE
)
1224 die("Not a tree: %s", oid_to_hex(oid
));
1225 t
->delta_depth
= myoe
->depth
;
1226 buf
= gfi_unpack_entry(myoe
, &size
);
1228 die("Can't load tree %s", oid_to_hex(oid
));
1230 enum object_type type
;
1231 buf
= read_object_file(oid
, &type
, &size
);
1232 if (!buf
|| type
!= OBJ_TREE
)
1233 die("Can't load tree %s", oid_to_hex(oid
));
1237 while (c
!= (buf
+ size
)) {
1238 struct tree_entry
*e
= new_tree_entry();
1240 if (t
->entry_count
== t
->entry_capacity
)
1241 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1242 t
->entries
[t
->entry_count
++] = e
;
1245 c
= get_mode(c
, &e
->versions
[1].mode
);
1247 die("Corrupt mode in %s", oid_to_hex(oid
));
1248 e
->versions
[0].mode
= e
->versions
[1].mode
;
1249 e
->name
= to_atom(c
, strlen(c
));
1250 c
+= e
->name
->str_len
+ 1;
1251 hashcpy(e
->versions
[0].oid
.hash
, (unsigned char *)c
);
1252 hashcpy(e
->versions
[1].oid
.hash
, (unsigned char *)c
);
1253 c
+= the_hash_algo
->rawsz
;
1258 static int tecmp0 (const void *_a
, const void *_b
)
1260 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1261 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1262 return base_name_compare(
1263 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1264 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1267 static int tecmp1 (const void *_a
, const void *_b
)
1269 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1270 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1271 return base_name_compare(
1272 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1273 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1276 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1282 QSORT(t
->entries
, t
->entry_count
, tecmp0
);
1284 QSORT(t
->entries
, t
->entry_count
, tecmp1
);
1286 for (i
= 0; i
< t
->entry_count
; i
++) {
1287 if (t
->entries
[i
]->versions
[v
].mode
)
1288 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1292 strbuf_grow(b
, maxlen
);
1293 for (i
= 0; i
< t
->entry_count
; i
++) {
1294 struct tree_entry
*e
= t
->entries
[i
];
1295 if (!e
->versions
[v
].mode
)
1297 strbuf_addf(b
, "%o %s%c",
1298 (unsigned int)(e
->versions
[v
].mode
& ~NO_DELTA
),
1299 e
->name
->str_dat
, '\0');
1300 strbuf_add(b
, e
->versions
[v
].oid
.hash
, the_hash_algo
->rawsz
);
1304 static void store_tree(struct tree_entry
*root
)
1306 struct tree_content
*t
;
1307 unsigned int i
, j
, del
;
1308 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1309 struct object_entry
*le
= NULL
;
1311 if (!is_null_oid(&root
->versions
[1].oid
))
1318 for (i
= 0; i
< t
->entry_count
; i
++) {
1319 if (t
->entries
[i
]->tree
)
1320 store_tree(t
->entries
[i
]);
1323 if (!(root
->versions
[0].mode
& NO_DELTA
))
1324 le
= find_object(&root
->versions
[0].oid
);
1325 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1326 mktree(t
, 0, &old_tree
);
1328 lo
.offset
= le
->idx
.offset
;
1329 lo
.depth
= t
->delta_depth
;
1332 mktree(t
, 1, &new_tree
);
1333 store_object(OBJ_TREE
, &new_tree
, &lo
, &root
->versions
[1].oid
, 0);
1335 t
->delta_depth
= lo
.depth
;
1336 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1337 struct tree_entry
*e
= t
->entries
[i
];
1338 if (e
->versions
[1].mode
) {
1339 e
->versions
[0].mode
= e
->versions
[1].mode
;
1340 oidcpy(&e
->versions
[0].oid
, &e
->versions
[1].oid
);
1341 t
->entries
[j
++] = e
;
1343 release_tree_entry(e
);
1347 t
->entry_count
-= del
;
1350 static void tree_content_replace(
1351 struct tree_entry
*root
,
1352 const struct object_id
*oid
,
1353 const uint16_t mode
,
1354 struct tree_content
*newtree
)
1357 die("Root cannot be a non-directory");
1358 oidclr(&root
->versions
[0].oid
);
1359 oidcpy(&root
->versions
[1].oid
, oid
);
1361 release_tree_content_recursive(root
->tree
);
1362 root
->tree
= newtree
;
1365 static int tree_content_set(
1366 struct tree_entry
*root
,
1368 const struct object_id
*oid
,
1369 const uint16_t mode
,
1370 struct tree_content
*subtree
)
1372 struct tree_content
*t
;
1375 struct tree_entry
*e
;
1377 slash1
= strchrnul(p
, '/');
1380 die("Empty path component found in input");
1381 if (!*slash1
&& !S_ISDIR(mode
) && subtree
)
1382 die("Non-directories cannot have subtrees");
1387 for (i
= 0; i
< t
->entry_count
; i
++) {
1389 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1392 && e
->versions
[1].mode
== mode
1393 && oideq(&e
->versions
[1].oid
, oid
))
1395 e
->versions
[1].mode
= mode
;
1396 oidcpy(&e
->versions
[1].oid
, oid
);
1398 release_tree_content_recursive(e
->tree
);
1402 * We need to leave e->versions[0].sha1 alone
1403 * to avoid modifying the preimage tree used
1404 * when writing out the parent directory.
1405 * But after replacing the subdir with a
1406 * completely different one, it's not a good
1407 * delta base any more, and besides, we've
1408 * thrown away the tree entries needed to
1409 * make a delta against it.
1411 * So let's just explicitly disable deltas
1414 if (S_ISDIR(e
->versions
[0].mode
))
1415 e
->versions
[0].mode
|= NO_DELTA
;
1417 oidclr(&root
->versions
[1].oid
);
1420 if (!S_ISDIR(e
->versions
[1].mode
)) {
1421 e
->tree
= new_tree_content(8);
1422 e
->versions
[1].mode
= S_IFDIR
;
1426 if (tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
)) {
1427 oidclr(&root
->versions
[1].oid
);
1434 if (t
->entry_count
== t
->entry_capacity
)
1435 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1436 e
= new_tree_entry();
1437 e
->name
= to_atom(p
, n
);
1438 e
->versions
[0].mode
= 0;
1439 oidclr(&e
->versions
[0].oid
);
1440 t
->entries
[t
->entry_count
++] = e
;
1442 e
->tree
= new_tree_content(8);
1443 e
->versions
[1].mode
= S_IFDIR
;
1444 tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
);
1447 e
->versions
[1].mode
= mode
;
1448 oidcpy(&e
->versions
[1].oid
, oid
);
1450 oidclr(&root
->versions
[1].oid
);
1454 static int tree_content_remove(
1455 struct tree_entry
*root
,
1457 struct tree_entry
*backup_leaf
,
1460 struct tree_content
*t
;
1463 struct tree_entry
*e
;
1465 slash1
= strchrnul(p
, '/');
1471 if (!*p
&& allow_root
) {
1477 for (i
= 0; i
< t
->entry_count
; i
++) {
1479 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1480 if (*slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1482 * If p names a file in some subdirectory, and a
1483 * file or symlink matching the name of the
1484 * parent directory of p exists, then p cannot
1485 * exist and need not be deleted.
1488 if (!*slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1492 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
, 0)) {
1493 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1494 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1495 oidclr(&root
->versions
[1].oid
);
1509 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1511 release_tree_content_recursive(e
->tree
);
1513 e
->versions
[1].mode
= 0;
1514 oidclr(&e
->versions
[1].oid
);
1515 oidclr(&root
->versions
[1].oid
);
1519 static int tree_content_get(
1520 struct tree_entry
*root
,
1522 struct tree_entry
*leaf
,
1525 struct tree_content
*t
;
1528 struct tree_entry
*e
;
1530 slash1
= strchrnul(p
, '/');
1532 if (!n
&& !allow_root
)
1533 die("Empty path component found in input");
1544 for (i
= 0; i
< t
->entry_count
; i
++) {
1546 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1549 if (!S_ISDIR(e
->versions
[1].mode
))
1553 return tree_content_get(e
, slash1
+ 1, leaf
, 0);
1559 memcpy(leaf
, e
, sizeof(*leaf
));
1560 if (e
->tree
&& is_null_oid(&e
->versions
[1].oid
))
1561 leaf
->tree
= dup_tree_content(e
->tree
);
1567 static int update_branch(struct branch
*b
)
1569 static const char *msg
= "fast-import";
1570 struct ref_transaction
*transaction
;
1571 struct object_id old_oid
;
1572 struct strbuf err
= STRBUF_INIT
;
1574 if (is_null_oid(&b
->oid
)) {
1576 delete_ref(NULL
, b
->name
, NULL
, 0);
1579 if (read_ref(b
->name
, &old_oid
))
1581 if (!force_update
&& !is_null_oid(&old_oid
)) {
1582 struct commit
*old_cmit
, *new_cmit
;
1584 old_cmit
= lookup_commit_reference_gently(the_repository
,
1586 new_cmit
= lookup_commit_reference_gently(the_repository
,
1588 if (!old_cmit
|| !new_cmit
)
1589 return error("Branch %s is missing commits.", b
->name
);
1591 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1592 warning("Not updating %s"
1593 " (new tip %s does not contain %s)",
1594 b
->name
, oid_to_hex(&b
->oid
),
1595 oid_to_hex(&old_oid
));
1599 transaction
= ref_transaction_begin(&err
);
1601 ref_transaction_update(transaction
, b
->name
, &b
->oid
, &old_oid
,
1603 ref_transaction_commit(transaction
, &err
)) {
1604 ref_transaction_free(transaction
);
1605 error("%s", err
.buf
);
1606 strbuf_release(&err
);
1609 ref_transaction_free(transaction
);
1610 strbuf_release(&err
);
1614 static void dump_branches(void)
1619 for (i
= 0; i
< branch_table_sz
; i
++) {
1620 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1621 failure
|= update_branch(b
);
1625 static void dump_tags(void)
1627 static const char *msg
= "fast-import";
1629 struct strbuf ref_name
= STRBUF_INIT
;
1630 struct strbuf err
= STRBUF_INIT
;
1631 struct ref_transaction
*transaction
;
1633 transaction
= ref_transaction_begin(&err
);
1635 failure
|= error("%s", err
.buf
);
1638 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1639 strbuf_reset(&ref_name
);
1640 strbuf_addf(&ref_name
, "refs/tags/%s", t
->name
);
1642 if (ref_transaction_update(transaction
, ref_name
.buf
,
1643 &t
->oid
, NULL
, 0, msg
, &err
)) {
1644 failure
|= error("%s", err
.buf
);
1648 if (ref_transaction_commit(transaction
, &err
))
1649 failure
|= error("%s", err
.buf
);
1652 ref_transaction_free(transaction
);
1653 strbuf_release(&ref_name
);
1654 strbuf_release(&err
);
1657 static void dump_marks_helper(FILE *f
,
1663 for (k
= 0; k
< 1024; k
++) {
1664 if (m
->data
.sets
[k
])
1665 dump_marks_helper(f
, base
+ (k
<< m
->shift
),
1669 for (k
= 0; k
< 1024; k
++) {
1670 if (m
->data
.marked
[k
])
1671 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1672 oid_to_hex(&m
->data
.marked
[k
]->idx
.oid
));
1677 static void dump_marks(void)
1679 struct lock_file mark_lock
= LOCK_INIT
;
1682 if (!export_marks_file
|| (import_marks_file
&& !import_marks_file_done
))
1685 if (hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0) < 0) {
1686 failure
|= error_errno("Unable to write marks file %s",
1691 f
= fdopen_lock_file(&mark_lock
, "w");
1693 int saved_errno
= errno
;
1694 rollback_lock_file(&mark_lock
);
1695 failure
|= error("Unable to write marks file %s: %s",
1696 export_marks_file
, strerror(saved_errno
));
1700 dump_marks_helper(f
, 0, marks
);
1701 if (commit_lock_file(&mark_lock
)) {
1702 failure
|= error_errno("Unable to write file %s",
1708 static void read_marks(void)
1711 FILE *f
= fopen(import_marks_file
, "r");
1714 else if (import_marks_file_ignore_missing
&& errno
== ENOENT
)
1715 goto done
; /* Marks file does not exist */
1717 die_errno("cannot read '%s'", import_marks_file
);
1718 while (fgets(line
, sizeof(line
), f
)) {
1721 struct object_id oid
;
1722 struct object_entry
*e
;
1724 end
= strchr(line
, '\n');
1725 if (line
[0] != ':' || !end
)
1726 die("corrupt mark line: %s", line
);
1728 mark
= strtoumax(line
+ 1, &end
, 10);
1729 if (!mark
|| end
== line
+ 1
1730 || *end
!= ' ' || get_oid_hex(end
+ 1, &oid
))
1731 die("corrupt mark line: %s", line
);
1732 e
= find_object(&oid
);
1734 enum object_type type
= oid_object_info(the_repository
,
1737 die("object not found: %s", oid_to_hex(&oid
));
1738 e
= insert_object(&oid
);
1740 e
->pack_id
= MAX_PACK_ID
;
1741 e
->idx
.offset
= 1; /* just not zero! */
1743 insert_mark(mark
, e
);
1747 import_marks_file_done
= 1;
1751 static int read_next_command(void)
1753 static int stdin_eof
= 0;
1756 unread_command_buf
= 0;
1761 if (unread_command_buf
) {
1762 unread_command_buf
= 0;
1764 struct recent_command
*rc
;
1766 strbuf_detach(&command_buf
, NULL
);
1767 stdin_eof
= strbuf_getline_lf(&command_buf
, stdin
);
1771 if (!seen_data_command
1772 && !starts_with(command_buf
.buf
, "feature ")
1773 && !starts_with(command_buf
.buf
, "option ")) {
1782 cmd_hist
.next
= rc
->next
;
1783 cmd_hist
.next
->prev
= &cmd_hist
;
1787 rc
->buf
= command_buf
.buf
;
1788 rc
->prev
= cmd_tail
;
1789 rc
->next
= cmd_hist
.prev
;
1790 rc
->prev
->next
= rc
;
1793 if (command_buf
.buf
[0] == '#')
1799 static void skip_optional_lf(void)
1801 int term_char
= fgetc(stdin
);
1802 if (term_char
!= '\n' && term_char
!= EOF
)
1803 ungetc(term_char
, stdin
);
1806 static void parse_mark(void)
1809 if (skip_prefix(command_buf
.buf
, "mark :", &v
)) {
1810 next_mark
= strtoumax(v
, NULL
, 10);
1811 read_next_command();
1817 static void parse_original_identifier(void)
1820 if (skip_prefix(command_buf
.buf
, "original-oid ", &v
))
1821 read_next_command();
1824 static int parse_data(struct strbuf
*sb
, uintmax_t limit
, uintmax_t *len_res
)
1829 if (!skip_prefix(command_buf
.buf
, "data ", &data
))
1830 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1832 if (skip_prefix(data
, "<<", &data
)) {
1833 char *term
= xstrdup(data
);
1834 size_t term_len
= command_buf
.len
- (data
- command_buf
.buf
);
1836 strbuf_detach(&command_buf
, NULL
);
1838 if (strbuf_getline_lf(&command_buf
, stdin
) == EOF
)
1839 die("EOF in data (terminator '%s' not found)", term
);
1840 if (term_len
== command_buf
.len
1841 && !strcmp(term
, command_buf
.buf
))
1843 strbuf_addbuf(sb
, &command_buf
);
1844 strbuf_addch(sb
, '\n');
1849 uintmax_t len
= strtoumax(data
, NULL
, 10);
1850 size_t n
= 0, length
= (size_t)len
;
1852 if (limit
&& limit
< len
) {
1857 die("data is too large to use in this context");
1859 while (n
< length
) {
1860 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1861 if (!s
&& feof(stdin
))
1862 die("EOF in data (%lu bytes remaining)",
1863 (unsigned long)(length
- n
));
1872 static int validate_raw_date(const char *src
, struct strbuf
*result
)
1874 const char *orig_src
= src
;
1880 num
= strtoul(src
, &endp
, 10);
1881 /* NEEDSWORK: perhaps check for reasonable values? */
1882 if (errno
|| endp
== src
|| *endp
!= ' ')
1886 if (*src
!= '-' && *src
!= '+')
1889 num
= strtoul(src
+ 1, &endp
, 10);
1890 if (errno
|| endp
== src
+ 1 || *endp
|| 1400 < num
)
1893 strbuf_addstr(result
, orig_src
);
1897 static char *parse_ident(const char *buf
)
1901 struct strbuf ident
= STRBUF_INIT
;
1903 /* ensure there is a space delimiter even if there is no name */
1907 ltgt
= buf
+ strcspn(buf
, "<>");
1909 die("Missing < in ident string: %s", buf
);
1910 if (ltgt
!= buf
&& ltgt
[-1] != ' ')
1911 die("Missing space before < in ident string: %s", buf
);
1912 ltgt
= ltgt
+ 1 + strcspn(ltgt
+ 1, "<>");
1914 die("Missing > in ident string: %s", buf
);
1917 die("Missing space after > in ident string: %s", buf
);
1919 name_len
= ltgt
- buf
;
1920 strbuf_add(&ident
, buf
, name_len
);
1924 if (validate_raw_date(ltgt
, &ident
) < 0)
1925 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
1927 case WHENSPEC_RFC2822
:
1928 if (parse_date(ltgt
, &ident
) < 0)
1929 die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt
, buf
);
1932 if (strcmp("now", ltgt
))
1933 die("Date in ident must be 'now': %s", buf
);
1938 return strbuf_detach(&ident
, NULL
);
1941 static void parse_and_store_blob(
1942 struct last_object
*last
,
1943 struct object_id
*oidout
,
1946 static struct strbuf buf
= STRBUF_INIT
;
1949 if (parse_data(&buf
, big_file_threshold
, &len
))
1950 store_object(OBJ_BLOB
, &buf
, last
, oidout
, mark
);
1953 strbuf_release(&last
->data
);
1957 stream_blob(len
, oidout
, mark
);
1962 static void parse_new_blob(void)
1964 read_next_command();
1966 parse_original_identifier();
1967 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
1970 static void unload_one_branch(void)
1972 while (cur_active_branches
1973 && cur_active_branches
>= max_active_branches
) {
1974 uintmax_t min_commit
= ULONG_MAX
;
1975 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1977 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1978 if (e
->last_commit
< min_commit
) {
1980 min_commit
= e
->last_commit
;
1986 e
= p
->active_next_branch
;
1987 p
->active_next_branch
= e
->active_next_branch
;
1989 e
= active_branches
;
1990 active_branches
= e
->active_next_branch
;
1993 e
->active_next_branch
= NULL
;
1994 if (e
->branch_tree
.tree
) {
1995 release_tree_content_recursive(e
->branch_tree
.tree
);
1996 e
->branch_tree
.tree
= NULL
;
1998 cur_active_branches
--;
2002 static void load_branch(struct branch
*b
)
2004 load_tree(&b
->branch_tree
);
2007 b
->active_next_branch
= active_branches
;
2008 active_branches
= b
;
2009 cur_active_branches
++;
2010 branch_load_count
++;
2014 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2016 unsigned char fanout
= 0;
2017 while ((num_notes
>>= 8))
2022 static void construct_path_with_fanout(const char *hex_sha1
,
2023 unsigned char fanout
, char *path
)
2025 unsigned int i
= 0, j
= 0;
2026 if (fanout
>= the_hash_algo
->rawsz
)
2027 die("Too large fanout (%u)", fanout
);
2029 path
[i
++] = hex_sha1
[j
++];
2030 path
[i
++] = hex_sha1
[j
++];
2034 memcpy(path
+ i
, hex_sha1
+ j
, the_hash_algo
->hexsz
- j
);
2035 path
[i
+ the_hash_algo
->hexsz
- j
] = '\0';
2038 static uintmax_t do_change_note_fanout(
2039 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2040 char *hex_oid
, unsigned int hex_oid_len
,
2041 char *fullpath
, unsigned int fullpath_len
,
2042 unsigned char fanout
)
2044 struct tree_content
*t
;
2045 struct tree_entry
*e
, leaf
;
2046 unsigned int i
, tmp_hex_oid_len
, tmp_fullpath_len
;
2047 uintmax_t num_notes
= 0;
2048 struct object_id oid
;
2049 /* hex oid + '/' between each pair of hex digits + NUL */
2050 char realpath
[GIT_MAX_HEXSZ
+ ((GIT_MAX_HEXSZ
/ 2) - 1) + 1];
2051 const unsigned hexsz
= the_hash_algo
->hexsz
;
2057 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2059 tmp_hex_oid_len
= hex_oid_len
+ e
->name
->str_len
;
2060 tmp_fullpath_len
= fullpath_len
;
2063 * We're interested in EITHER existing note entries (entries
2064 * with exactly 40 hex chars in path, not including directory
2065 * separators), OR directory entries that may contain note
2066 * entries (with < 40 hex chars in path).
2067 * Also, each path component in a note entry must be a multiple
2070 if (!e
->versions
[1].mode
||
2071 tmp_hex_oid_len
> hexsz
||
2072 e
->name
->str_len
% 2)
2075 /* This _may_ be a note entry, or a subdir containing notes */
2076 memcpy(hex_oid
+ hex_oid_len
, e
->name
->str_dat
,
2078 if (tmp_fullpath_len
)
2079 fullpath
[tmp_fullpath_len
++] = '/';
2080 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2082 tmp_fullpath_len
+= e
->name
->str_len
;
2083 fullpath
[tmp_fullpath_len
] = '\0';
2085 if (tmp_hex_oid_len
== hexsz
&& !get_oid_hex(hex_oid
, &oid
)) {
2086 /* This is a note entry */
2087 if (fanout
== 0xff) {
2088 /* Counting mode, no rename */
2092 construct_path_with_fanout(hex_oid
, fanout
, realpath
);
2093 if (!strcmp(fullpath
, realpath
)) {
2094 /* Note entry is in correct location */
2099 /* Rename fullpath to realpath */
2100 if (!tree_content_remove(orig_root
, fullpath
, &leaf
, 0))
2101 die("Failed to remove path %s", fullpath
);
2102 tree_content_set(orig_root
, realpath
,
2103 &leaf
.versions
[1].oid
,
2104 leaf
.versions
[1].mode
,
2106 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2107 /* This is a subdir that may contain note entries */
2108 num_notes
+= do_change_note_fanout(orig_root
, e
,
2109 hex_oid
, tmp_hex_oid_len
,
2110 fullpath
, tmp_fullpath_len
, fanout
);
2113 /* The above may have reallocated the current tree_content */
2119 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2120 unsigned char fanout
)
2123 * The size of path is due to one slash between every two hex digits,
2124 * plus the terminating NUL. Note that there is no slash at the end, so
2125 * the number of slashes is one less than half the number of hex
2128 char hex_oid
[GIT_MAX_HEXSZ
], path
[GIT_MAX_HEXSZ
+ (GIT_MAX_HEXSZ
/ 2) - 1 + 1];
2129 return do_change_note_fanout(root
, root
, hex_oid
, 0, path
, 0, fanout
);
2133 * Given a pointer into a string, parse a mark reference:
2135 * idnum ::= ':' bigint;
2137 * Return the first character after the value in *endptr.
2139 * Complain if the following character is not what is expected,
2140 * either a space or end of the string.
2142 static uintmax_t parse_mark_ref(const char *p
, char **endptr
)
2148 mark
= strtoumax(p
, endptr
, 10);
2150 die("No value after ':' in mark: %s", command_buf
.buf
);
2155 * Parse the mark reference, and complain if this is not the end of
2158 static uintmax_t parse_mark_ref_eol(const char *p
)
2163 mark
= parse_mark_ref(p
, &end
);
2165 die("Garbage after mark: %s", command_buf
.buf
);
2170 * Parse the mark reference, demanding a trailing space. Return a
2171 * pointer to the space.
2173 static uintmax_t parse_mark_ref_space(const char **p
)
2178 mark
= parse_mark_ref(*p
, &end
);
2180 die("Missing space after mark: %s", command_buf
.buf
);
2185 static void file_change_m(const char *p
, struct branch
*b
)
2187 static struct strbuf uq
= STRBUF_INIT
;
2189 struct object_entry
*oe
;
2190 struct object_id oid
;
2191 uint16_t mode
, inline_data
= 0;
2193 p
= get_mode(p
, &mode
);
2195 die("Corrupt mode: %s", command_buf
.buf
);
2200 case S_IFREG
| 0644:
2201 case S_IFREG
| 0755:
2208 die("Corrupt mode: %s", command_buf
.buf
);
2212 oe
= find_mark(parse_mark_ref_space(&p
));
2213 oidcpy(&oid
, &oe
->idx
.oid
);
2214 } else if (skip_prefix(p
, "inline ", &p
)) {
2216 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2218 if (parse_oid_hex(p
, &oid
, &p
))
2219 die("Invalid dataref: %s", command_buf
.buf
);
2220 oe
= find_object(&oid
);
2222 die("Missing space after SHA1: %s", command_buf
.buf
);
2226 if (!unquote_c_style(&uq
, p
, &endp
)) {
2228 die("Garbage after path in: %s", command_buf
.buf
);
2232 /* Git does not track empty, non-toplevel directories. */
2233 if (S_ISDIR(mode
) && is_empty_tree_oid(&oid
) && *p
) {
2234 tree_content_remove(&b
->branch_tree
, p
, NULL
, 0);
2238 if (S_ISGITLINK(mode
)) {
2240 die("Git links cannot be specified 'inline': %s",
2243 if (oe
->type
!= OBJ_COMMIT
)
2244 die("Not a commit (actually a %s): %s",
2245 type_name(oe
->type
), command_buf
.buf
);
2248 * Accept the sha1 without checking; it expected to be in
2249 * another repository.
2251 } else if (inline_data
) {
2253 die("Directories cannot be specified 'inline': %s",
2256 strbuf_addstr(&uq
, p
);
2259 while (read_next_command() != EOF
) {
2261 if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2264 parse_and_store_blob(&last_blob
, &oid
, 0);
2269 enum object_type expected
= S_ISDIR(mode
) ?
2271 enum object_type type
= oe
? oe
->type
:
2272 oid_object_info(the_repository
, &oid
,
2275 die("%s not found: %s",
2276 S_ISDIR(mode
) ? "Tree" : "Blob",
2278 if (type
!= expected
)
2279 die("Not a %s (actually a %s): %s",
2280 type_name(expected
), type_name(type
),
2285 tree_content_replace(&b
->branch_tree
, &oid
, mode
, NULL
);
2288 tree_content_set(&b
->branch_tree
, p
, &oid
, mode
, NULL
);
2291 static void file_change_d(const char *p
, struct branch
*b
)
2293 static struct strbuf uq
= STRBUF_INIT
;
2297 if (!unquote_c_style(&uq
, p
, &endp
)) {
2299 die("Garbage after path in: %s", command_buf
.buf
);
2302 tree_content_remove(&b
->branch_tree
, p
, NULL
, 1);
2305 static void file_change_cr(const char *s
, struct branch
*b
, int rename
)
2308 static struct strbuf s_uq
= STRBUF_INIT
;
2309 static struct strbuf d_uq
= STRBUF_INIT
;
2311 struct tree_entry leaf
;
2313 strbuf_reset(&s_uq
);
2314 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2316 die("Missing space after source: %s", command_buf
.buf
);
2318 endp
= strchr(s
, ' ');
2320 die("Missing space after source: %s", command_buf
.buf
);
2321 strbuf_add(&s_uq
, s
, endp
- s
);
2327 die("Missing dest: %s", command_buf
.buf
);
2330 strbuf_reset(&d_uq
);
2331 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2333 die("Garbage after dest in: %s", command_buf
.buf
);
2337 memset(&leaf
, 0, sizeof(leaf
));
2339 tree_content_remove(&b
->branch_tree
, s
, &leaf
, 1);
2341 tree_content_get(&b
->branch_tree
, s
, &leaf
, 1);
2342 if (!leaf
.versions
[1].mode
)
2343 die("Path %s not in branch", s
);
2344 if (!*d
) { /* C "path/to/subdir" "" */
2345 tree_content_replace(&b
->branch_tree
,
2346 &leaf
.versions
[1].oid
,
2347 leaf
.versions
[1].mode
,
2351 tree_content_set(&b
->branch_tree
, d
,
2352 &leaf
.versions
[1].oid
,
2353 leaf
.versions
[1].mode
,
2357 static void note_change_n(const char *p
, struct branch
*b
, unsigned char *old_fanout
)
2359 static struct strbuf uq
= STRBUF_INIT
;
2360 struct object_entry
*oe
;
2362 struct object_id oid
, commit_oid
;
2363 char path
[GIT_MAX_RAWSZ
* 3];
2364 uint16_t inline_data
= 0;
2365 unsigned char new_fanout
;
2368 * When loading a branch, we don't traverse its tree to count the real
2369 * number of notes (too expensive to do this for all non-note refs).
2370 * This means that recently loaded notes refs might incorrectly have
2371 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2373 * Fix this by traversing the tree and counting the number of notes
2374 * when b->num_notes == 0. If the notes tree is truly empty, the
2375 * calculation should not take long.
2377 if (b
->num_notes
== 0 && *old_fanout
== 0) {
2378 /* Invoke change_note_fanout() in "counting mode". */
2379 b
->num_notes
= change_note_fanout(&b
->branch_tree
, 0xff);
2380 *old_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2383 /* Now parse the notemodify command. */
2384 /* <dataref> or 'inline' */
2386 oe
= find_mark(parse_mark_ref_space(&p
));
2387 oidcpy(&oid
, &oe
->idx
.oid
);
2388 } else if (skip_prefix(p
, "inline ", &p
)) {
2390 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2392 if (parse_oid_hex(p
, &oid
, &p
))
2393 die("Invalid dataref: %s", command_buf
.buf
);
2394 oe
= find_object(&oid
);
2396 die("Missing space after SHA1: %s", command_buf
.buf
);
2400 s
= lookup_branch(p
);
2402 if (is_null_oid(&s
->oid
))
2403 die("Can't add a note on empty branch.");
2404 oidcpy(&commit_oid
, &s
->oid
);
2405 } else if (*p
== ':') {
2406 uintmax_t commit_mark
= parse_mark_ref_eol(p
);
2407 struct object_entry
*commit_oe
= find_mark(commit_mark
);
2408 if (commit_oe
->type
!= OBJ_COMMIT
)
2409 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2410 oidcpy(&commit_oid
, &commit_oe
->idx
.oid
);
2411 } else if (!get_oid(p
, &commit_oid
)) {
2413 char *buf
= read_object_with_reference(the_repository
,
2417 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2418 die("Not a valid commit: %s", p
);
2421 die("Invalid ref name or SHA1 expression: %s", p
);
2425 strbuf_addstr(&uq
, p
);
2428 read_next_command();
2429 parse_and_store_blob(&last_blob
, &oid
, 0);
2431 if (oe
->type
!= OBJ_BLOB
)
2432 die("Not a blob (actually a %s): %s",
2433 type_name(oe
->type
), command_buf
.buf
);
2434 } else if (!is_null_oid(&oid
)) {
2435 enum object_type type
= oid_object_info(the_repository
, &oid
,
2438 die("Blob not found: %s", command_buf
.buf
);
2439 if (type
!= OBJ_BLOB
)
2440 die("Not a blob (actually a %s): %s",
2441 type_name(type
), command_buf
.buf
);
2444 construct_path_with_fanout(oid_to_hex(&commit_oid
), *old_fanout
, path
);
2445 if (tree_content_remove(&b
->branch_tree
, path
, NULL
, 0))
2448 if (is_null_oid(&oid
))
2449 return; /* nothing to insert */
2452 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2453 construct_path_with_fanout(oid_to_hex(&commit_oid
), new_fanout
, path
);
2454 tree_content_set(&b
->branch_tree
, path
, &oid
, S_IFREG
| 0644, NULL
);
2457 static void file_change_deleteall(struct branch
*b
)
2459 release_tree_content_recursive(b
->branch_tree
.tree
);
2460 oidclr(&b
->branch_tree
.versions
[0].oid
);
2461 oidclr(&b
->branch_tree
.versions
[1].oid
);
2462 load_tree(&b
->branch_tree
);
2466 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2468 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2469 die("Not a valid commit: %s", oid_to_hex(&b
->oid
));
2470 if (memcmp("tree ", buf
, 5)
2471 || get_oid_hex(buf
+ 5, &b
->branch_tree
.versions
[1].oid
))
2472 die("The commit %s is corrupt", oid_to_hex(&b
->oid
));
2473 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2474 &b
->branch_tree
.versions
[1].oid
);
2477 static void parse_from_existing(struct branch
*b
)
2479 if (is_null_oid(&b
->oid
)) {
2480 oidclr(&b
->branch_tree
.versions
[0].oid
);
2481 oidclr(&b
->branch_tree
.versions
[1].oid
);
2486 buf
= read_object_with_reference(the_repository
,
2487 &b
->oid
, commit_type
, &size
,
2489 parse_from_commit(b
, buf
, size
);
2494 static int parse_from(struct branch
*b
)
2498 struct object_id oid
;
2500 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2503 oidcpy(&oid
, &b
->branch_tree
.versions
[1].oid
);
2505 s
= lookup_branch(from
);
2507 die("Can't create a branch from itself: %s", b
->name
);
2509 struct object_id
*t
= &s
->branch_tree
.versions
[1].oid
;
2510 oidcpy(&b
->oid
, &s
->oid
);
2511 oidcpy(&b
->branch_tree
.versions
[0].oid
, t
);
2512 oidcpy(&b
->branch_tree
.versions
[1].oid
, t
);
2513 } else if (*from
== ':') {
2514 uintmax_t idnum
= parse_mark_ref_eol(from
);
2515 struct object_entry
*oe
= find_mark(idnum
);
2516 if (oe
->type
!= OBJ_COMMIT
)
2517 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2518 if (!oideq(&b
->oid
, &oe
->idx
.oid
)) {
2519 oidcpy(&b
->oid
, &oe
->idx
.oid
);
2520 if (oe
->pack_id
!= MAX_PACK_ID
) {
2522 char *buf
= gfi_unpack_entry(oe
, &size
);
2523 parse_from_commit(b
, buf
, size
);
2526 parse_from_existing(b
);
2528 } else if (!get_oid(from
, &b
->oid
)) {
2529 parse_from_existing(b
);
2530 if (is_null_oid(&b
->oid
))
2534 die("Invalid ref name or SHA1 expression: %s", from
);
2536 if (b
->branch_tree
.tree
&& !oideq(&oid
, &b
->branch_tree
.versions
[1].oid
)) {
2537 release_tree_content_recursive(b
->branch_tree
.tree
);
2538 b
->branch_tree
.tree
= NULL
;
2541 read_next_command();
2545 static struct hash_list
*parse_merge(unsigned int *count
)
2547 struct hash_list
*list
= NULL
, **tail
= &list
, *n
;
2552 while (skip_prefix(command_buf
.buf
, "merge ", &from
)) {
2553 n
= xmalloc(sizeof(*n
));
2554 s
= lookup_branch(from
);
2556 oidcpy(&n
->oid
, &s
->oid
);
2557 else if (*from
== ':') {
2558 uintmax_t idnum
= parse_mark_ref_eol(from
);
2559 struct object_entry
*oe
= find_mark(idnum
);
2560 if (oe
->type
!= OBJ_COMMIT
)
2561 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2562 oidcpy(&n
->oid
, &oe
->idx
.oid
);
2563 } else if (!get_oid(from
, &n
->oid
)) {
2565 char *buf
= read_object_with_reference(the_repository
,
2569 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2570 die("Not a valid commit: %s", from
);
2573 die("Invalid ref name or SHA1 expression: %s", from
);
2580 read_next_command();
2585 static void parse_new_commit(const char *arg
)
2587 static struct strbuf msg
= STRBUF_INIT
;
2589 char *author
= NULL
;
2590 char *committer
= NULL
;
2591 const char *encoding
= NULL
;
2592 struct hash_list
*merge_list
= NULL
;
2593 unsigned int merge_count
;
2594 unsigned char prev_fanout
, new_fanout
;
2597 b
= lookup_branch(arg
);
2599 b
= new_branch(arg
);
2601 read_next_command();
2603 parse_original_identifier();
2604 if (skip_prefix(command_buf
.buf
, "author ", &v
)) {
2605 author
= parse_ident(v
);
2606 read_next_command();
2608 if (skip_prefix(command_buf
.buf
, "committer ", &v
)) {
2609 committer
= parse_ident(v
);
2610 read_next_command();
2613 die("Expected committer but didn't get one");
2614 if (skip_prefix(command_buf
.buf
, "encoding ", &encoding
))
2615 read_next_command();
2616 parse_data(&msg
, 0, NULL
);
2617 read_next_command();
2619 merge_list
= parse_merge(&merge_count
);
2621 /* ensure the branch is active/loaded */
2622 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2623 unload_one_branch();
2627 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2630 while (command_buf
.len
> 0) {
2631 if (skip_prefix(command_buf
.buf
, "M ", &v
))
2632 file_change_m(v
, b
);
2633 else if (skip_prefix(command_buf
.buf
, "D ", &v
))
2634 file_change_d(v
, b
);
2635 else if (skip_prefix(command_buf
.buf
, "R ", &v
))
2636 file_change_cr(v
, b
, 1);
2637 else if (skip_prefix(command_buf
.buf
, "C ", &v
))
2638 file_change_cr(v
, b
, 0);
2639 else if (skip_prefix(command_buf
.buf
, "N ", &v
))
2640 note_change_n(v
, b
, &prev_fanout
);
2641 else if (!strcmp("deleteall", command_buf
.buf
))
2642 file_change_deleteall(b
);
2643 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
2645 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2648 unread_command_buf
= 1;
2651 if (read_next_command() == EOF
)
2655 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2656 if (new_fanout
!= prev_fanout
)
2657 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2659 /* build the tree and the commit */
2660 store_tree(&b
->branch_tree
);
2661 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2662 &b
->branch_tree
.versions
[1].oid
);
2664 strbuf_reset(&new_data
);
2665 strbuf_addf(&new_data
, "tree %s\n",
2666 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
2667 if (!is_null_oid(&b
->oid
))
2668 strbuf_addf(&new_data
, "parent %s\n",
2669 oid_to_hex(&b
->oid
));
2670 while (merge_list
) {
2671 struct hash_list
*next
= merge_list
->next
;
2672 strbuf_addf(&new_data
, "parent %s\n",
2673 oid_to_hex(&merge_list
->oid
));
2677 strbuf_addf(&new_data
,
2680 author
? author
: committer
, committer
);
2682 strbuf_addf(&new_data
,
2685 strbuf_addch(&new_data
, '\n');
2686 strbuf_addbuf(&new_data
, &msg
);
2690 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, &b
->oid
, next_mark
))
2691 b
->pack_id
= pack_id
;
2692 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2695 static void parse_new_tag(const char *arg
)
2697 static struct strbuf msg
= STRBUF_INIT
;
2702 uintmax_t from_mark
= 0;
2703 struct object_id oid
;
2704 enum object_type type
;
2707 t
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct tag
));
2708 memset(t
, 0, sizeof(struct tag
));
2709 t
->name
= pool_strdup(arg
);
2711 last_tag
->next_tag
= t
;
2715 read_next_command();
2718 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2719 die("Expected from command, got %s", command_buf
.buf
);
2720 s
= lookup_branch(from
);
2722 if (is_null_oid(&s
->oid
))
2723 die("Can't tag an empty branch.");
2724 oidcpy(&oid
, &s
->oid
);
2726 } else if (*from
== ':') {
2727 struct object_entry
*oe
;
2728 from_mark
= parse_mark_ref_eol(from
);
2729 oe
= find_mark(from_mark
);
2731 oidcpy(&oid
, &oe
->idx
.oid
);
2732 } else if (!get_oid(from
, &oid
)) {
2733 struct object_entry
*oe
= find_object(&oid
);
2735 type
= oid_object_info(the_repository
, &oid
, NULL
);
2737 die("Not a valid object: %s", from
);
2741 die("Invalid ref name or SHA1 expression: %s", from
);
2742 read_next_command();
2744 /* original-oid ... */
2745 parse_original_identifier();
2748 if (skip_prefix(command_buf
.buf
, "tagger ", &v
)) {
2749 tagger
= parse_ident(v
);
2750 read_next_command();
2754 /* tag payload/message */
2755 parse_data(&msg
, 0, NULL
);
2757 /* build the tag object */
2758 strbuf_reset(&new_data
);
2760 strbuf_addf(&new_data
,
2764 oid_to_hex(&oid
), type_name(type
), t
->name
);
2766 strbuf_addf(&new_data
,
2767 "tagger %s\n", tagger
);
2768 strbuf_addch(&new_data
, '\n');
2769 strbuf_addbuf(&new_data
, &msg
);
2772 if (store_object(OBJ_TAG
, &new_data
, NULL
, &t
->oid
, 0))
2773 t
->pack_id
= MAX_PACK_ID
;
2775 t
->pack_id
= pack_id
;
2778 static void parse_reset_branch(const char *arg
)
2782 b
= lookup_branch(arg
);
2785 oidclr(&b
->branch_tree
.versions
[0].oid
);
2786 oidclr(&b
->branch_tree
.versions
[1].oid
);
2787 if (b
->branch_tree
.tree
) {
2788 release_tree_content_recursive(b
->branch_tree
.tree
);
2789 b
->branch_tree
.tree
= NULL
;
2793 b
= new_branch(arg
);
2794 read_next_command();
2796 if (command_buf
.len
> 0)
2797 unread_command_buf
= 1;
2800 static void cat_blob_write(const char *buf
, unsigned long size
)
2802 if (write_in_full(cat_blob_fd
, buf
, size
) < 0)
2803 die_errno("Write to frontend failed");
2806 static void cat_blob(struct object_entry
*oe
, struct object_id
*oid
)
2808 struct strbuf line
= STRBUF_INIT
;
2810 enum object_type type
= 0;
2813 if (!oe
|| oe
->pack_id
== MAX_PACK_ID
) {
2814 buf
= read_object_file(oid
, &type
, &size
);
2817 buf
= gfi_unpack_entry(oe
, &size
);
2821 * Output based on batch_one_object() from cat-file.c.
2824 strbuf_reset(&line
);
2825 strbuf_addf(&line
, "%s missing\n", oid_to_hex(oid
));
2826 cat_blob_write(line
.buf
, line
.len
);
2827 strbuf_release(&line
);
2832 die("Can't read object %s", oid_to_hex(oid
));
2833 if (type
!= OBJ_BLOB
)
2834 die("Object %s is a %s but a blob was expected.",
2835 oid_to_hex(oid
), type_name(type
));
2836 strbuf_reset(&line
);
2837 strbuf_addf(&line
, "%s %s %"PRIuMAX
"\n", oid_to_hex(oid
),
2838 type_name(type
), (uintmax_t)size
);
2839 cat_blob_write(line
.buf
, line
.len
);
2840 strbuf_release(&line
);
2841 cat_blob_write(buf
, size
);
2842 cat_blob_write("\n", 1);
2843 if (oe
&& oe
->pack_id
== pack_id
) {
2844 last_blob
.offset
= oe
->idx
.offset
;
2845 strbuf_attach(&last_blob
.data
, buf
, size
, size
);
2846 last_blob
.depth
= oe
->depth
;
2851 static void parse_get_mark(const char *p
)
2853 struct object_entry
*oe
;
2854 char output
[GIT_MAX_HEXSZ
+ 2];
2856 /* get-mark SP <object> LF */
2858 die("Not a mark: %s", p
);
2860 oe
= find_mark(parse_mark_ref_eol(p
));
2862 die("Unknown mark: %s", command_buf
.buf
);
2864 xsnprintf(output
, sizeof(output
), "%s\n", oid_to_hex(&oe
->idx
.oid
));
2865 cat_blob_write(output
, the_hash_algo
->hexsz
+ 1);
2868 static void parse_cat_blob(const char *p
)
2870 struct object_entry
*oe
;
2871 struct object_id oid
;
2873 /* cat-blob SP <object> LF */
2875 oe
= find_mark(parse_mark_ref_eol(p
));
2877 die("Unknown mark: %s", command_buf
.buf
);
2878 oidcpy(&oid
, &oe
->idx
.oid
);
2880 if (parse_oid_hex(p
, &oid
, &p
))
2881 die("Invalid dataref: %s", command_buf
.buf
);
2883 die("Garbage after SHA1: %s", command_buf
.buf
);
2884 oe
= find_object(&oid
);
2890 static struct object_entry
*dereference(struct object_entry
*oe
,
2891 struct object_id
*oid
)
2895 const unsigned hexsz
= the_hash_algo
->hexsz
;
2898 enum object_type type
= oid_object_info(the_repository
, oid
,
2901 die("object not found: %s", oid_to_hex(oid
));
2903 oe
= insert_object(oid
);
2905 oe
->pack_id
= MAX_PACK_ID
;
2909 case OBJ_TREE
: /* easy case. */
2915 die("Not a tree-ish: %s", command_buf
.buf
);
2918 if (oe
->pack_id
!= MAX_PACK_ID
) { /* in a pack being written */
2919 buf
= gfi_unpack_entry(oe
, &size
);
2921 enum object_type unused
;
2922 buf
= read_object_file(oid
, &unused
, &size
);
2925 die("Can't load object %s", oid_to_hex(oid
));
2927 /* Peel one layer. */
2930 if (size
< hexsz
+ strlen("object ") ||
2931 get_oid_hex(buf
+ strlen("object "), oid
))
2932 die("Invalid SHA1 in tag: %s", command_buf
.buf
);
2935 if (size
< hexsz
+ strlen("tree ") ||
2936 get_oid_hex(buf
+ strlen("tree "), oid
))
2937 die("Invalid SHA1 in commit: %s", command_buf
.buf
);
2941 return find_object(oid
);
2944 static struct object_entry
*parse_treeish_dataref(const char **p
)
2946 struct object_id oid
;
2947 struct object_entry
*e
;
2949 if (**p
== ':') { /* <mark> */
2950 e
= find_mark(parse_mark_ref_space(p
));
2952 die("Unknown mark: %s", command_buf
.buf
);
2953 oidcpy(&oid
, &e
->idx
.oid
);
2954 } else { /* <sha1> */
2955 if (parse_oid_hex(*p
, &oid
, p
))
2956 die("Invalid dataref: %s", command_buf
.buf
);
2957 e
= find_object(&oid
);
2959 die("Missing space after tree-ish: %s", command_buf
.buf
);
2962 while (!e
|| e
->type
!= OBJ_TREE
)
2963 e
= dereference(e
, &oid
);
2967 static void print_ls(int mode
, const unsigned char *hash
, const char *path
)
2969 static struct strbuf line
= STRBUF_INIT
;
2971 /* See show_tree(). */
2973 S_ISGITLINK(mode
) ? commit_type
:
2974 S_ISDIR(mode
) ? tree_type
:
2978 /* missing SP path LF */
2979 strbuf_reset(&line
);
2980 strbuf_addstr(&line
, "missing ");
2981 quote_c_style(path
, &line
, NULL
, 0);
2982 strbuf_addch(&line
, '\n');
2984 /* mode SP type SP object_name TAB path LF */
2985 strbuf_reset(&line
);
2986 strbuf_addf(&line
, "%06o %s %s\t",
2987 mode
& ~NO_DELTA
, type
, hash_to_hex(hash
));
2988 quote_c_style(path
, &line
, NULL
, 0);
2989 strbuf_addch(&line
, '\n');
2991 cat_blob_write(line
.buf
, line
.len
);
2994 static void parse_ls(const char *p
, struct branch
*b
)
2996 struct tree_entry
*root
= NULL
;
2997 struct tree_entry leaf
= {NULL
};
2999 /* ls SP (<tree-ish> SP)? <path> */
3002 die("Not in a commit: %s", command_buf
.buf
);
3003 root
= &b
->branch_tree
;
3005 struct object_entry
*e
= parse_treeish_dataref(&p
);
3006 root
= new_tree_entry();
3007 oidcpy(&root
->versions
[1].oid
, &e
->idx
.oid
);
3008 if (!is_null_oid(&root
->versions
[1].oid
))
3009 root
->versions
[1].mode
= S_IFDIR
;
3013 static struct strbuf uq
= STRBUF_INIT
;
3016 if (unquote_c_style(&uq
, p
, &endp
))
3017 die("Invalid path: %s", command_buf
.buf
);
3019 die("Garbage after path in: %s", command_buf
.buf
);
3022 tree_content_get(root
, p
, &leaf
, 1);
3024 * A directory in preparation would have a sha1 of zero
3025 * until it is saved. Save, for simplicity.
3027 if (S_ISDIR(leaf
.versions
[1].mode
))
3030 print_ls(leaf
.versions
[1].mode
, leaf
.versions
[1].oid
.hash
, p
);
3032 release_tree_content_recursive(leaf
.tree
);
3033 if (!b
|| root
!= &b
->branch_tree
)
3034 release_tree_entry(root
);
3037 static void checkpoint(void)
3039 checkpoint_requested
= 0;
3048 static void parse_checkpoint(void)
3050 checkpoint_requested
= 1;
3054 static void parse_progress(void)
3056 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
3057 fputc('\n', stdout
);
3062 static char* make_fast_import_path(const char *path
)
3064 if (!relative_marks_paths
|| is_absolute_path(path
))
3065 return xstrdup(path
);
3066 return git_pathdup("info/fast-import/%s", path
);
3069 static void option_import_marks(const char *marks
,
3070 int from_stream
, int ignore_missing
)
3072 if (import_marks_file
) {
3074 die("Only one import-marks command allowed per stream");
3076 /* read previous mark file */
3077 if(!import_marks_file_from_stream
)
3081 import_marks_file
= make_fast_import_path(marks
);
3082 safe_create_leading_directories_const(import_marks_file
);
3083 import_marks_file_from_stream
= from_stream
;
3084 import_marks_file_ignore_missing
= ignore_missing
;
3087 static void option_date_format(const char *fmt
)
3089 if (!strcmp(fmt
, "raw"))
3090 whenspec
= WHENSPEC_RAW
;
3091 else if (!strcmp(fmt
, "rfc2822"))
3092 whenspec
= WHENSPEC_RFC2822
;
3093 else if (!strcmp(fmt
, "now"))
3094 whenspec
= WHENSPEC_NOW
;
3096 die("unknown --date-format argument %s", fmt
);
3099 static unsigned long ulong_arg(const char *option
, const char *arg
)
3102 unsigned long rv
= strtoul(arg
, &endptr
, 0);
3103 if (strchr(arg
, '-') || endptr
== arg
|| *endptr
)
3104 die("%s: argument must be a non-negative integer", option
);
3108 static void option_depth(const char *depth
)
3110 max_depth
= ulong_arg("--depth", depth
);
3111 if (max_depth
> MAX_DEPTH
)
3112 die("--depth cannot exceed %u", MAX_DEPTH
);
3115 static void option_active_branches(const char *branches
)
3117 max_active_branches
= ulong_arg("--active-branches", branches
);
3120 static void option_export_marks(const char *marks
)
3122 export_marks_file
= make_fast_import_path(marks
);
3123 safe_create_leading_directories_const(export_marks_file
);
3126 static void option_cat_blob_fd(const char *fd
)
3128 unsigned long n
= ulong_arg("--cat-blob-fd", fd
);
3129 if (n
> (unsigned long) INT_MAX
)
3130 die("--cat-blob-fd cannot exceed %d", INT_MAX
);
3131 cat_blob_fd
= (int) n
;
3134 static void option_export_pack_edges(const char *edges
)
3138 pack_edges
= xfopen(edges
, "a");
3141 static int parse_one_option(const char *option
)
3143 if (skip_prefix(option
, "max-pack-size=", &option
)) {
3145 if (!git_parse_ulong(option
, &v
))
3148 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v
);
3150 } else if (v
< 1024 * 1024) {
3151 warning("minimum max-pack-size is 1 MiB");
3155 } else if (skip_prefix(option
, "big-file-threshold=", &option
)) {
3157 if (!git_parse_ulong(option
, &v
))
3159 big_file_threshold
= v
;
3160 } else if (skip_prefix(option
, "depth=", &option
)) {
3161 option_depth(option
);
3162 } else if (skip_prefix(option
, "active-branches=", &option
)) {
3163 option_active_branches(option
);
3164 } else if (skip_prefix(option
, "export-pack-edges=", &option
)) {
3165 option_export_pack_edges(option
);
3166 } else if (starts_with(option
, "quiet")) {
3168 } else if (starts_with(option
, "stats")) {
3177 static int parse_one_feature(const char *feature
, int from_stream
)
3181 if (skip_prefix(feature
, "date-format=", &arg
)) {
3182 option_date_format(arg
);
3183 } else if (skip_prefix(feature
, "import-marks=", &arg
)) {
3184 option_import_marks(arg
, from_stream
, 0);
3185 } else if (skip_prefix(feature
, "import-marks-if-exists=", &arg
)) {
3186 option_import_marks(arg
, from_stream
, 1);
3187 } else if (skip_prefix(feature
, "export-marks=", &arg
)) {
3188 option_export_marks(arg
);
3189 } else if (!strcmp(feature
, "get-mark")) {
3190 ; /* Don't die - this feature is supported */
3191 } else if (!strcmp(feature
, "cat-blob")) {
3192 ; /* Don't die - this feature is supported */
3193 } else if (!strcmp(feature
, "relative-marks")) {
3194 relative_marks_paths
= 1;
3195 } else if (!strcmp(feature
, "no-relative-marks")) {
3196 relative_marks_paths
= 0;
3197 } else if (!strcmp(feature
, "done")) {
3198 require_explicit_termination
= 1;
3199 } else if (!strcmp(feature
, "force")) {
3201 } else if (!strcmp(feature
, "notes") || !strcmp(feature
, "ls")) {
3202 ; /* do nothing; we have the feature */
3210 static void parse_feature(const char *feature
)
3212 if (seen_data_command
)
3213 die("Got feature command '%s' after data command", feature
);
3215 if (parse_one_feature(feature
, 1))
3218 die("This version of fast-import does not support feature %s.", feature
);
3221 static void parse_option(const char *option
)
3223 if (seen_data_command
)
3224 die("Got option command '%s' after data command", option
);
3226 if (parse_one_option(option
))
3229 die("This version of fast-import does not support option: %s", option
);
3232 static void git_pack_config(void)
3234 int indexversion_value
;
3236 unsigned long packsizelimit_value
;
3238 if (!git_config_get_ulong("pack.depth", &max_depth
)) {
3239 if (max_depth
> MAX_DEPTH
)
3240 max_depth
= MAX_DEPTH
;
3242 if (!git_config_get_int("pack.indexversion", &indexversion_value
)) {
3243 pack_idx_opts
.version
= indexversion_value
;
3244 if (pack_idx_opts
.version
> 2)
3245 git_die_config("pack.indexversion",
3246 "bad pack.indexversion=%"PRIu32
, pack_idx_opts
.version
);
3248 if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value
))
3249 max_packsize
= packsizelimit_value
;
3251 if (!git_config_get_int("fastimport.unpacklimit", &limit
))
3252 unpack_limit
= limit
;
3253 else if (!git_config_get_int("transfer.unpacklimit", &limit
))
3254 unpack_limit
= limit
;
3256 git_config(git_default_config
, NULL
);
3259 static const char fast_import_usage
[] =
3260 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3262 static void parse_argv(void)
3266 for (i
= 1; i
< global_argc
; i
++) {
3267 const char *a
= global_argv
[i
];
3269 if (*a
!= '-' || !strcmp(a
, "--"))
3272 if (!skip_prefix(a
, "--", &a
))
3273 die("unknown option %s", a
);
3275 if (parse_one_option(a
))
3278 if (parse_one_feature(a
, 0))
3281 if (skip_prefix(a
, "cat-blob-fd=", &a
)) {
3282 option_cat_blob_fd(a
);
3286 die("unknown option --%s", a
);
3288 if (i
!= global_argc
)
3289 usage(fast_import_usage
);
3291 seen_data_command
= 1;
3292 if (import_marks_file
)
3296 int cmd_main(int argc
, const char **argv
)
3300 if (argc
== 2 && !strcmp(argv
[1], "-h"))
3301 usage(fast_import_usage
);
3303 setup_git_directory();
3304 reset_pack_idx_option(&pack_idx_opts
);
3307 alloc_objects(object_entry_alloc
);
3308 strbuf_init(&command_buf
, 0);
3309 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
3310 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
3311 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
3312 marks
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
3317 rc_free
= mem_pool_alloc(&fi_mem_pool
, cmd_save
* sizeof(*rc_free
));
3318 for (i
= 0; i
< (cmd_save
- 1); i
++)
3319 rc_free
[i
].next
= &rc_free
[i
+ 1];
3320 rc_free
[cmd_save
- 1].next
= NULL
;
3323 set_die_routine(die_nicely
);
3324 set_checkpoint_signal();
3325 while (read_next_command() != EOF
) {
3327 if (!strcmp("blob", command_buf
.buf
))
3329 else if (skip_prefix(command_buf
.buf
, "commit ", &v
))
3330 parse_new_commit(v
);
3331 else if (skip_prefix(command_buf
.buf
, "tag ", &v
))
3333 else if (skip_prefix(command_buf
.buf
, "reset ", &v
))
3334 parse_reset_branch(v
);
3335 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
3337 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
3339 else if (skip_prefix(command_buf
.buf
, "get-mark ", &v
))
3341 else if (!strcmp("checkpoint", command_buf
.buf
))
3343 else if (!strcmp("done", command_buf
.buf
))
3345 else if (starts_with(command_buf
.buf
, "progress "))
3347 else if (skip_prefix(command_buf
.buf
, "feature ", &v
))
3349 else if (skip_prefix(command_buf
.buf
, "option git ", &v
))
3351 else if (starts_with(command_buf
.buf
, "option "))
3352 /* ignore non-git options*/;
3354 die("Unsupported command: %s", command_buf
.buf
);
3356 if (checkpoint_requested
)
3360 /* argv hasn't been parsed yet, do so */
3361 if (!seen_data_command
)
3364 if (require_explicit_termination
&& feof(stdin
))
3365 die("stream ends early");
3378 uintmax_t total_count
= 0, duplicate_count
= 0;
3379 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3380 total_count
+= object_count_by_type
[i
];
3381 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3382 duplicate_count
+= duplicate_count_by_type
[i
];
3384 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3385 fprintf(stderr
, "---------------------------------------------------------------------\n");
3386 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3387 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3388 fprintf(stderr
, " blobs : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas of %10" PRIuMAX
" attempts)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
], delta_count_attempts_by_type
[OBJ_BLOB
]);
3389 fprintf(stderr
, " trees : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas of %10" PRIuMAX
" attempts)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
], delta_count_attempts_by_type
[OBJ_TREE
]);
3390 fprintf(stderr
, " commits: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas of %10" PRIuMAX
" attempts)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
], delta_count_attempts_by_type
[OBJ_COMMIT
]);
3391 fprintf(stderr
, " tags : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas of %10" PRIuMAX
" attempts)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
], delta_count_attempts_by_type
[OBJ_TAG
]);
3392 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3393 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3394 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3395 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (tree_entry_allocd
+ fi_mem_pool
.pool_alloc
+ alloc_count
*sizeof(struct object_entry
))/1024);
3396 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd
+ fi_mem_pool
.pool_alloc
) /1024));
3397 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3398 fprintf(stderr
, "---------------------------------------------------------------------\n");
3400 fprintf(stderr
, "---------------------------------------------------------------------\n");
3401 fprintf(stderr
, "\n");
3404 return failure
? 1 : 0;