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 struct pack_idx_entry idx
;
34 struct object_entry
*next
;
35 uint32_t type
: TYPE_BITS
,
36 pack_id
: PACK_ID_BITS
,
40 struct object_entry_pool
{
41 struct object_entry_pool
*next_pool
;
42 struct object_entry
*next_free
;
43 struct object_entry
*end
;
44 struct object_entry entries
[FLEX_ARRAY
]; /* more */
49 struct object_entry
*marked
[1024];
50 struct mark_set
*sets
[1024];
63 struct atom_str
*next_atom
;
64 unsigned short str_len
;
65 char str_dat
[FLEX_ARRAY
]; /* more */
70 struct tree_content
*tree
;
71 struct atom_str
*name
;
72 struct tree_entry_ms
{
79 unsigned int entry_capacity
; /* must match avail_tree_content */
80 unsigned int entry_count
;
81 unsigned int delta_depth
;
82 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
85 struct avail_tree_content
{
86 unsigned int entry_capacity
; /* must match tree_content */
87 struct avail_tree_content
*next_avail
;
91 struct branch
*table_next_branch
;
92 struct branch
*active_next_branch
;
94 struct tree_entry branch_tree
;
95 uintmax_t last_commit
;
99 unsigned pack_id
: PACK_ID_BITS
;
100 struct object_id oid
;
104 struct tag
*next_tag
;
106 unsigned int pack_id
;
107 struct object_id oid
;
111 struct hash_list
*next
;
112 struct object_id oid
;
121 struct recent_command
{
122 struct recent_command
*prev
;
123 struct recent_command
*next
;
127 /* Configured limits on output */
128 static unsigned long max_depth
= 50;
129 static off_t max_packsize
;
130 static int unpack_limit
= 100;
131 static int force_update
;
133 /* Stats and misc. counters */
134 static uintmax_t alloc_count
;
135 static uintmax_t marks_set_count
;
136 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
137 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
138 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
139 static uintmax_t delta_count_attempts_by_type
[1 << TYPE_BITS
];
140 static unsigned long object_count
;
141 static unsigned long branch_count
;
142 static unsigned long branch_load_count
;
144 static FILE *pack_edges
;
145 static unsigned int show_stats
= 1;
146 static int global_argc
;
147 static const char **global_argv
;
150 static struct mem_pool fi_mem_pool
= {NULL
, 2*1024*1024 -
151 sizeof(struct mp_block
), 0 };
153 /* Atom management */
154 static unsigned int atom_table_sz
= 4451;
155 static unsigned int atom_cnt
;
156 static struct atom_str
**atom_table
;
158 /* The .pack file being generated */
159 static struct pack_idx_option pack_idx_opts
;
160 static unsigned int pack_id
;
161 static struct hashfile
*pack_file
;
162 static struct packed_git
*pack_data
;
163 static struct packed_git
**all_packs
;
164 static off_t pack_size
;
166 /* Table of objects we've written. */
167 static unsigned int object_entry_alloc
= 5000;
168 static struct object_entry_pool
*blocks
;
169 static struct object_entry
*object_table
[1 << 16];
170 static struct mark_set
*marks
;
171 static const char *export_marks_file
;
172 static const char *import_marks_file
;
173 static int import_marks_file_from_stream
;
174 static int import_marks_file_ignore_missing
;
175 static int import_marks_file_done
;
176 static int relative_marks_paths
;
179 static struct last_object last_blob
= { STRBUF_INIT
, 0, 0, 0 };
181 /* Tree management */
182 static unsigned int tree_entry_alloc
= 1000;
183 static void *avail_tree_entry
;
184 static unsigned int avail_tree_table_sz
= 100;
185 static struct avail_tree_content
**avail_tree_table
;
186 static size_t tree_entry_allocd
;
187 static struct strbuf old_tree
= STRBUF_INIT
;
188 static struct strbuf new_tree
= STRBUF_INIT
;
191 static unsigned long max_active_branches
= 5;
192 static unsigned long cur_active_branches
;
193 static unsigned long branch_table_sz
= 1039;
194 static struct branch
**branch_table
;
195 static struct branch
*active_branches
;
198 static struct tag
*first_tag
;
199 static struct tag
*last_tag
;
201 /* Input stream parsing */
202 static whenspec_type whenspec
= WHENSPEC_RAW
;
203 static struct strbuf command_buf
= STRBUF_INIT
;
204 static int unread_command_buf
;
205 static struct recent_command cmd_hist
= {&cmd_hist
, &cmd_hist
, NULL
};
206 static struct recent_command
*cmd_tail
= &cmd_hist
;
207 static struct recent_command
*rc_free
;
208 static unsigned int cmd_save
= 100;
209 static uintmax_t next_mark
;
210 static struct strbuf new_data
= STRBUF_INIT
;
211 static int seen_data_command
;
212 static int require_explicit_termination
;
213 static int allow_unsafe_features
;
215 /* Signal handling */
216 static volatile sig_atomic_t checkpoint_requested
;
218 /* Where to write output of cat-blob commands */
219 static int cat_blob_fd
= STDOUT_FILENO
;
221 static void parse_argv(void);
222 static void parse_get_mark(const char *p
);
223 static void parse_cat_blob(const char *p
);
224 static void parse_ls(const char *p
, struct branch
*b
);
226 static void write_branch_report(FILE *rpt
, struct branch
*b
)
228 fprintf(rpt
, "%s:\n", b
->name
);
230 fprintf(rpt
, " status :");
232 fputs(" active", rpt
);
233 if (b
->branch_tree
.tree
)
234 fputs(" loaded", rpt
);
235 if (is_null_oid(&b
->branch_tree
.versions
[1].oid
))
236 fputs(" dirty", rpt
);
239 fprintf(rpt
, " tip commit : %s\n", oid_to_hex(&b
->oid
));
240 fprintf(rpt
, " old tree : %s\n",
241 oid_to_hex(&b
->branch_tree
.versions
[0].oid
));
242 fprintf(rpt
, " cur tree : %s\n",
243 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
244 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
246 fputs(" last pack : ", rpt
);
247 if (b
->pack_id
< MAX_PACK_ID
)
248 fprintf(rpt
, "%u", b
->pack_id
);
254 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set
*);
256 static void write_crash_report(const char *err
)
258 char *loc
= git_pathdup("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
259 FILE *rpt
= fopen(loc
, "w");
262 struct recent_command
*rc
;
265 error_errno("can't write crash report %s", loc
);
270 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
272 fprintf(rpt
, "fast-import crash report:\n");
273 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
274 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
275 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_MODE(ISO8601
)));
278 fputs("fatal: ", rpt
);
283 fputs("Most Recent Commands Before Crash\n", rpt
);
284 fputs("---------------------------------\n", rpt
);
285 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
286 if (rc
->next
== &cmd_hist
)
295 fputs("Active Branch LRU\n", rpt
);
296 fputs("-----------------\n", rpt
);
297 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
299 max_active_branches
);
301 fputs(" pos clock name\n", rpt
);
302 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
303 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
304 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
305 ++lu
, b
->last_commit
, b
->name
);
308 fputs("Inactive Branches\n", rpt
);
309 fputs("-----------------\n", rpt
);
310 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
311 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
312 write_branch_report(rpt
, b
);
318 fputs("Annotated Tags\n", rpt
);
319 fputs("--------------\n", rpt
);
320 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
321 fputs(oid_to_hex(&tg
->oid
), rpt
);
323 fputs(tg
->name
, rpt
);
329 fputs("Marks\n", rpt
);
330 fputs("-----\n", rpt
);
331 if (export_marks_file
)
332 fprintf(rpt
, " exported to %s\n", export_marks_file
);
334 dump_marks_helper(rpt
, 0, marks
);
337 fputs("-------------------\n", rpt
);
338 fputs("END OF CRASH REPORT\n", rpt
);
343 static void end_packfile(void);
344 static void unkeep_all_packs(void);
345 static void dump_marks(void);
347 static NORETURN
void die_nicely(const char *err
, va_list params
)
350 char message
[2 * PATH_MAX
];
352 vsnprintf(message
, sizeof(message
), err
, params
);
353 fputs("fatal: ", stderr
);
354 fputs(message
, stderr
);
359 write_crash_report(message
);
367 #ifndef SIGUSR1 /* Windows, for example */
369 static void set_checkpoint_signal(void)
375 static void checkpoint_signal(int signo
)
377 checkpoint_requested
= 1;
380 static void set_checkpoint_signal(void)
384 memset(&sa
, 0, sizeof(sa
));
385 sa
.sa_handler
= checkpoint_signal
;
386 sigemptyset(&sa
.sa_mask
);
387 sa
.sa_flags
= SA_RESTART
;
388 sigaction(SIGUSR1
, &sa
, NULL
);
393 static void alloc_objects(unsigned int cnt
)
395 struct object_entry_pool
*b
;
397 b
= xmalloc(sizeof(struct object_entry_pool
)
398 + cnt
* sizeof(struct object_entry
));
399 b
->next_pool
= blocks
;
400 b
->next_free
= b
->entries
;
401 b
->end
= b
->entries
+ cnt
;
406 static struct object_entry
*new_object(struct object_id
*oid
)
408 struct object_entry
*e
;
410 if (blocks
->next_free
== blocks
->end
)
411 alloc_objects(object_entry_alloc
);
413 e
= blocks
->next_free
++;
414 oidcpy(&e
->idx
.oid
, oid
);
418 static struct object_entry
*find_object(struct object_id
*oid
)
420 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
421 struct object_entry
*e
;
422 for (e
= object_table
[h
]; e
; e
= e
->next
)
423 if (oideq(oid
, &e
->idx
.oid
))
428 static struct object_entry
*insert_object(struct object_id
*oid
)
430 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
431 struct object_entry
*e
= object_table
[h
];
434 if (oideq(oid
, &e
->idx
.oid
))
440 e
->next
= object_table
[h
];
446 static void invalidate_pack_id(unsigned int id
)
452 for (h
= 0; h
< ARRAY_SIZE(object_table
); h
++) {
453 struct object_entry
*e
;
455 for (e
= object_table
[h
]; e
; e
= e
->next
)
456 if (e
->pack_id
== id
)
457 e
->pack_id
= MAX_PACK_ID
;
460 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
463 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
464 if (b
->pack_id
== id
)
465 b
->pack_id
= MAX_PACK_ID
;
468 for (t
= first_tag
; t
; t
= t
->next_tag
)
469 if (t
->pack_id
== id
)
470 t
->pack_id
= MAX_PACK_ID
;
473 static unsigned int hc_str(const char *s
, size_t len
)
481 static char *pool_strdup(const char *s
)
483 size_t len
= strlen(s
) + 1;
484 char *r
= mem_pool_alloc(&fi_mem_pool
, len
);
489 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
491 struct mark_set
*s
= marks
;
492 while ((idnum
>> s
->shift
) >= 1024) {
493 s
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
494 s
->shift
= marks
->shift
+ 10;
495 s
->data
.sets
[0] = marks
;
499 uintmax_t i
= idnum
>> s
->shift
;
500 idnum
-= i
<< s
->shift
;
501 if (!s
->data
.sets
[i
]) {
502 s
->data
.sets
[i
] = mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
503 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
507 if (!s
->data
.marked
[idnum
])
509 s
->data
.marked
[idnum
] = oe
;
512 static struct object_entry
*find_mark(uintmax_t idnum
)
514 uintmax_t orig_idnum
= idnum
;
515 struct mark_set
*s
= marks
;
516 struct object_entry
*oe
= NULL
;
517 if ((idnum
>> s
->shift
) < 1024) {
518 while (s
&& s
->shift
) {
519 uintmax_t i
= idnum
>> s
->shift
;
520 idnum
-= i
<< s
->shift
;
524 oe
= s
->data
.marked
[idnum
];
527 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
531 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
533 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
536 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
537 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
540 c
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct atom_str
) + len
+ 1);
542 memcpy(c
->str_dat
, s
, len
);
544 c
->next_atom
= atom_table
[hc
];
550 static struct branch
*lookup_branch(const char *name
)
552 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
555 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
556 if (!strcmp(name
, b
->name
))
561 static struct branch
*new_branch(const char *name
)
563 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
564 struct branch
*b
= lookup_branch(name
);
567 die("Invalid attempt to create duplicate branch: %s", name
);
568 if (check_refname_format(name
, REFNAME_ALLOW_ONELEVEL
))
569 die("Branch name doesn't conform to GIT standards: %s", name
);
571 b
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct branch
));
572 b
->name
= pool_strdup(name
);
573 b
->table_next_branch
= branch_table
[hc
];
574 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
575 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
578 b
->pack_id
= MAX_PACK_ID
;
579 branch_table
[hc
] = b
;
584 static unsigned int hc_entries(unsigned int cnt
)
586 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
587 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
590 static struct tree_content
*new_tree_content(unsigned int cnt
)
592 struct avail_tree_content
*f
, *l
= NULL
;
593 struct tree_content
*t
;
594 unsigned int hc
= hc_entries(cnt
);
596 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
597 if (f
->entry_capacity
>= cnt
)
602 l
->next_avail
= f
->next_avail
;
604 avail_tree_table
[hc
] = f
->next_avail
;
606 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
607 f
= mem_pool_alloc(&fi_mem_pool
, sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
608 f
->entry_capacity
= cnt
;
611 t
= (struct tree_content
*)f
;
617 static void release_tree_entry(struct tree_entry
*e
);
618 static void release_tree_content(struct tree_content
*t
)
620 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
621 unsigned int hc
= hc_entries(f
->entry_capacity
);
622 f
->next_avail
= avail_tree_table
[hc
];
623 avail_tree_table
[hc
] = f
;
626 static void release_tree_content_recursive(struct tree_content
*t
)
629 for (i
= 0; i
< t
->entry_count
; i
++)
630 release_tree_entry(t
->entries
[i
]);
631 release_tree_content(t
);
634 static struct tree_content
*grow_tree_content(
635 struct tree_content
*t
,
638 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
639 r
->entry_count
= t
->entry_count
;
640 r
->delta_depth
= t
->delta_depth
;
641 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
642 release_tree_content(t
);
646 static struct tree_entry
*new_tree_entry(void)
648 struct tree_entry
*e
;
650 if (!avail_tree_entry
) {
651 unsigned int n
= tree_entry_alloc
;
652 tree_entry_allocd
+= n
* sizeof(struct tree_entry
);
654 avail_tree_entry
= e
;
656 *((void**)e
) = e
+ 1;
662 e
= avail_tree_entry
;
663 avail_tree_entry
= *((void**)e
);
667 static void release_tree_entry(struct tree_entry
*e
)
670 release_tree_content_recursive(e
->tree
);
671 *((void**)e
) = avail_tree_entry
;
672 avail_tree_entry
= e
;
675 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
677 struct tree_content
*d
;
678 struct tree_entry
*a
, *b
;
683 d
= new_tree_content(s
->entry_count
);
684 for (i
= 0; i
< s
->entry_count
; i
++) {
686 b
= new_tree_entry();
687 memcpy(b
, a
, sizeof(*a
));
688 if (a
->tree
&& is_null_oid(&b
->versions
[1].oid
))
689 b
->tree
= dup_tree_content(a
->tree
);
694 d
->entry_count
= s
->entry_count
;
695 d
->delta_depth
= s
->delta_depth
;
700 static void start_packfile(void)
702 struct strbuf tmp_file
= STRBUF_INIT
;
703 struct packed_git
*p
;
704 struct pack_header hdr
;
707 pack_fd
= odb_mkstemp(&tmp_file
, "pack/tmp_pack_XXXXXX");
708 FLEX_ALLOC_STR(p
, pack_name
, tmp_file
.buf
);
709 strbuf_release(&tmp_file
);
711 p
->pack_fd
= pack_fd
;
713 pack_file
= hashfd(pack_fd
, p
->pack_name
);
715 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
716 hdr
.hdr_version
= htonl(2);
718 hashwrite(pack_file
, &hdr
, sizeof(hdr
));
721 pack_size
= sizeof(hdr
);
724 REALLOC_ARRAY(all_packs
, pack_id
+ 1);
725 all_packs
[pack_id
] = p
;
728 static const char *create_index(void)
731 struct pack_idx_entry
**idx
, **c
, **last
;
732 struct object_entry
*e
;
733 struct object_entry_pool
*o
;
735 /* Build the table of object IDs. */
736 ALLOC_ARRAY(idx
, object_count
);
738 for (o
= blocks
; o
; o
= o
->next_pool
)
739 for (e
= o
->next_free
; e
-- != o
->entries
;)
740 if (pack_id
== e
->pack_id
)
742 last
= idx
+ object_count
;
744 die("internal consistency error creating the index");
746 tmpfile
= write_idx_file(NULL
, idx
, object_count
, &pack_idx_opts
, pack_data
->sha1
);
751 static char *keep_pack(const char *curr_index_name
)
753 static const char *keep_msg
= "fast-import";
754 struct strbuf name
= STRBUF_INIT
;
757 odb_pack_name(&name
, pack_data
->sha1
, "keep");
758 keep_fd
= odb_pack_keep(name
.buf
);
760 die_errno("cannot create keep file");
761 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
763 die_errno("failed to write keep file");
765 odb_pack_name(&name
, pack_data
->sha1
, "pack");
766 if (finalize_object_file(pack_data
->pack_name
, name
.buf
))
767 die("cannot store pack file");
769 odb_pack_name(&name
, pack_data
->sha1
, "idx");
770 if (finalize_object_file(curr_index_name
, name
.buf
))
771 die("cannot store index file");
772 free((void *)curr_index_name
);
773 return strbuf_detach(&name
, NULL
);
776 static void unkeep_all_packs(void)
778 struct strbuf name
= STRBUF_INIT
;
781 for (k
= 0; k
< pack_id
; k
++) {
782 struct packed_git
*p
= all_packs
[k
];
783 odb_pack_name(&name
, p
->sha1
, "keep");
784 unlink_or_warn(name
.buf
);
786 strbuf_release(&name
);
789 static int loosen_small_pack(const struct packed_git
*p
)
791 struct child_process unpack
= CHILD_PROCESS_INIT
;
793 if (lseek(p
->pack_fd
, 0, SEEK_SET
) < 0)
794 die_errno("Failed seeking to start of '%s'", p
->pack_name
);
796 unpack
.in
= p
->pack_fd
;
798 unpack
.stdout_to_stderr
= 1;
799 argv_array_push(&unpack
.args
, "unpack-objects");
801 argv_array_push(&unpack
.args
, "-q");
803 return run_command(&unpack
);
806 static void end_packfile(void)
810 if (running
|| !pack_data
)
814 clear_delta_base_cache();
816 struct packed_git
*new_p
;
817 struct object_id cur_pack_oid
;
823 close_pack_windows(pack_data
);
824 finalize_hashfile(pack_file
, cur_pack_oid
.hash
, 0);
825 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->sha1
,
826 pack_data
->pack_name
, object_count
,
827 cur_pack_oid
.hash
, pack_size
);
829 if (object_count
<= unpack_limit
) {
830 if (!loosen_small_pack(pack_data
)) {
831 invalidate_pack_id(pack_id
);
836 close(pack_data
->pack_fd
);
837 idx_name
= keep_pack(create_index());
839 /* Register the packfile with core git's machinery. */
840 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
842 die("core git rejected index %s", idx_name
);
843 all_packs
[pack_id
] = new_p
;
844 install_packed_git(the_repository
, new_p
);
847 /* Print the boundary */
849 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
850 for (i
= 0; i
< branch_table_sz
; i
++) {
851 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
852 if (b
->pack_id
== pack_id
)
853 fprintf(pack_edges
, " %s",
854 oid_to_hex(&b
->oid
));
857 for (t
= first_tag
; t
; t
= t
->next_tag
) {
858 if (t
->pack_id
== pack_id
)
859 fprintf(pack_edges
, " %s",
860 oid_to_hex(&t
->oid
));
862 fputc('\n', pack_edges
);
870 close(pack_data
->pack_fd
);
871 unlink_or_warn(pack_data
->pack_name
);
873 FREE_AND_NULL(pack_data
);
876 /* We can't carry a delta across packfiles. */
877 strbuf_release(&last_blob
.data
);
878 last_blob
.offset
= 0;
882 static void cycle_packfile(void)
888 static int store_object(
889 enum object_type type
,
891 struct last_object
*last
,
892 struct object_id
*oidout
,
896 struct object_entry
*e
;
897 unsigned char hdr
[96];
898 struct object_id oid
;
899 unsigned long hdrlen
, deltalen
;
903 hdrlen
= xsnprintf((char *)hdr
, sizeof(hdr
), "%s %lu",
904 type_name(type
), (unsigned long)dat
->len
) + 1;
905 the_hash_algo
->init_fn(&c
);
906 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
907 the_hash_algo
->update_fn(&c
, dat
->buf
, dat
->len
);
908 the_hash_algo
->final_fn(oid
.hash
, &c
);
910 oidcpy(oidout
, &oid
);
912 e
= insert_object(&oid
);
914 insert_mark(mark
, e
);
916 duplicate_count_by_type
[type
]++;
918 } else if (find_sha1_pack(oid
.hash
,
919 get_all_packs(the_repository
))) {
921 e
->pack_id
= MAX_PACK_ID
;
922 e
->idx
.offset
= 1; /* just not zero! */
923 duplicate_count_by_type
[type
]++;
927 if (last
&& last
->data
.len
&& last
->data
.buf
&& last
->depth
< max_depth
928 && dat
->len
> the_hash_algo
->rawsz
) {
930 delta_count_attempts_by_type
[type
]++;
931 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
933 &deltalen
, dat
->len
- the_hash_algo
->rawsz
);
937 git_deflate_init(&s
, pack_compression_level
);
940 s
.avail_in
= deltalen
;
942 s
.next_in
= (void *)dat
->buf
;
943 s
.avail_in
= dat
->len
;
945 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
946 s
.next_out
= out
= xmalloc(s
.avail_out
);
947 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
951 /* Determine if we should auto-checkpoint. */
952 if ((max_packsize
&& (pack_size
+ 60 + s
.total_out
) > max_packsize
)
953 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
955 /* This new object needs to *not* have the current pack_id. */
956 e
->pack_id
= pack_id
+ 1;
959 /* We cannot carry a delta into the new pack. */
961 FREE_AND_NULL(delta
);
963 git_deflate_init(&s
, pack_compression_level
);
964 s
.next_in
= (void *)dat
->buf
;
965 s
.avail_in
= dat
->len
;
966 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
967 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
968 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
975 e
->pack_id
= pack_id
;
976 e
->idx
.offset
= pack_size
;
978 object_count_by_type
[type
]++;
980 crc32_begin(pack_file
);
983 off_t ofs
= e
->idx
.offset
- last
->offset
;
984 unsigned pos
= sizeof(hdr
) - 1;
986 delta_count_by_type
[type
]++;
987 e
->depth
= last
->depth
+ 1;
989 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
990 OBJ_OFS_DELTA
, deltalen
);
991 hashwrite(pack_file
, hdr
, hdrlen
);
994 hdr
[pos
] = ofs
& 127;
996 hdr
[--pos
] = 128 | (--ofs
& 127);
997 hashwrite(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
998 pack_size
+= sizeof(hdr
) - pos
;
1001 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1003 hashwrite(pack_file
, hdr
, hdrlen
);
1004 pack_size
+= hdrlen
;
1007 hashwrite(pack_file
, out
, s
.total_out
);
1008 pack_size
+= s
.total_out
;
1010 e
->idx
.crc32
= crc32_end(pack_file
);
1015 if (last
->no_swap
) {
1018 strbuf_swap(&last
->data
, dat
);
1020 last
->offset
= e
->idx
.offset
;
1021 last
->depth
= e
->depth
;
1026 static void truncate_pack(struct hashfile_checkpoint
*checkpoint
)
1028 if (hashfile_truncate(pack_file
, checkpoint
))
1029 die_errno("cannot truncate pack to skip duplicate");
1030 pack_size
= checkpoint
->offset
;
1033 static void stream_blob(uintmax_t len
, struct object_id
*oidout
, uintmax_t mark
)
1035 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1036 unsigned char *in_buf
= xmalloc(in_sz
);
1037 unsigned char *out_buf
= xmalloc(out_sz
);
1038 struct object_entry
*e
;
1039 struct object_id oid
;
1040 unsigned long hdrlen
;
1044 struct hashfile_checkpoint checkpoint
;
1047 /* Determine if we should auto-checkpoint. */
1048 if ((max_packsize
&& (pack_size
+ 60 + len
) > max_packsize
)
1049 || (pack_size
+ 60 + len
) < pack_size
)
1052 hashfile_checkpoint(pack_file
, &checkpoint
);
1053 offset
= checkpoint
.offset
;
1055 hdrlen
= xsnprintf((char *)out_buf
, out_sz
, "blob %" PRIuMAX
, len
) + 1;
1057 the_hash_algo
->init_fn(&c
);
1058 the_hash_algo
->update_fn(&c
, out_buf
, hdrlen
);
1060 crc32_begin(pack_file
);
1062 git_deflate_init(&s
, pack_compression_level
);
1064 hdrlen
= encode_in_pack_object_header(out_buf
, out_sz
, OBJ_BLOB
, len
);
1066 s
.next_out
= out_buf
+ hdrlen
;
1067 s
.avail_out
= out_sz
- hdrlen
;
1069 while (status
!= Z_STREAM_END
) {
1070 if (0 < len
&& !s
.avail_in
) {
1071 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1072 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1073 if (!n
&& feof(stdin
))
1074 die("EOF in data (%" PRIuMAX
" bytes remaining)", len
);
1076 the_hash_algo
->update_fn(&c
, in_buf
, n
);
1082 status
= git_deflate(&s
, len
? 0 : Z_FINISH
);
1084 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1085 size_t n
= s
.next_out
- out_buf
;
1086 hashwrite(pack_file
, out_buf
, n
);
1088 s
.next_out
= out_buf
;
1089 s
.avail_out
= out_sz
;
1098 die("unexpected deflate failure: %d", status
);
1101 git_deflate_end(&s
);
1102 the_hash_algo
->final_fn(oid
.hash
, &c
);
1105 oidcpy(oidout
, &oid
);
1107 e
= insert_object(&oid
);
1110 insert_mark(mark
, e
);
1112 if (e
->idx
.offset
) {
1113 duplicate_count_by_type
[OBJ_BLOB
]++;
1114 truncate_pack(&checkpoint
);
1116 } else if (find_sha1_pack(oid
.hash
,
1117 get_all_packs(the_repository
))) {
1119 e
->pack_id
= MAX_PACK_ID
;
1120 e
->idx
.offset
= 1; /* just not zero! */
1121 duplicate_count_by_type
[OBJ_BLOB
]++;
1122 truncate_pack(&checkpoint
);
1127 e
->pack_id
= pack_id
;
1128 e
->idx
.offset
= offset
;
1129 e
->idx
.crc32
= crc32_end(pack_file
);
1131 object_count_by_type
[OBJ_BLOB
]++;
1138 /* All calls must be guarded by find_object() or find_mark() to
1139 * ensure the 'struct object_entry' passed was written by this
1140 * process instance. We unpack the entry by the offset, avoiding
1141 * the need for the corresponding .idx file. This unpacking rule
1142 * works because we only use OBJ_REF_DELTA within the packfiles
1143 * created by fast-import.
1145 * oe must not be NULL. Such an oe usually comes from giving
1146 * an unknown SHA-1 to find_object() or an undefined mark to
1147 * find_mark(). Callers must test for this condition and use
1148 * the standard read_sha1_file() when it happens.
1150 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1151 * find_mark(), where the mark was reloaded from an existing marks
1152 * file and is referencing an object that this fast-import process
1153 * instance did not write out to a packfile. Callers must test for
1154 * this condition and use read_sha1_file() instead.
1156 static void *gfi_unpack_entry(
1157 struct object_entry
*oe
,
1158 unsigned long *sizep
)
1160 enum object_type type
;
1161 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1162 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ the_hash_algo
->rawsz
)) {
1163 /* The object is stored in the packfile we are writing to
1164 * and we have modified it since the last time we scanned
1165 * back to read a previously written object. If an old
1166 * window covered [p->pack_size, p->pack_size + rawsz) its
1167 * data is stale and is not valid. Closing all windows
1168 * and updating the packfile length ensures we can read
1169 * the newly written data.
1171 close_pack_windows(p
);
1172 hashflush(pack_file
);
1174 /* We have to offer rawsz bytes additional on the end of
1175 * the packfile as the core unpacker code assumes the
1176 * footer is present at the file end and must promise
1177 * at least rawsz bytes within any window it maps. But
1178 * we don't actually create the footer here.
1180 p
->pack_size
= pack_size
+ the_hash_algo
->rawsz
;
1182 return unpack_entry(the_repository
, p
, oe
->idx
.offset
, &type
, sizep
);
1185 static const char *get_mode(const char *str
, uint16_t *modep
)
1190 while ((c
= *str
++) != ' ') {
1191 if (c
< '0' || c
> '7')
1193 mode
= (mode
<< 3) + (c
- '0');
1199 static void load_tree(struct tree_entry
*root
)
1201 struct object_id
*oid
= &root
->versions
[1].oid
;
1202 struct object_entry
*myoe
;
1203 struct tree_content
*t
;
1208 root
->tree
= t
= new_tree_content(8);
1209 if (is_null_oid(oid
))
1212 myoe
= find_object(oid
);
1213 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1214 if (myoe
->type
!= OBJ_TREE
)
1215 die("Not a tree: %s", oid_to_hex(oid
));
1216 t
->delta_depth
= myoe
->depth
;
1217 buf
= gfi_unpack_entry(myoe
, &size
);
1219 die("Can't load tree %s", oid_to_hex(oid
));
1221 enum object_type type
;
1222 buf
= read_object_file(oid
, &type
, &size
);
1223 if (!buf
|| type
!= OBJ_TREE
)
1224 die("Can't load tree %s", oid_to_hex(oid
));
1228 while (c
!= (buf
+ size
)) {
1229 struct tree_entry
*e
= new_tree_entry();
1231 if (t
->entry_count
== t
->entry_capacity
)
1232 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1233 t
->entries
[t
->entry_count
++] = e
;
1236 c
= get_mode(c
, &e
->versions
[1].mode
);
1238 die("Corrupt mode in %s", oid_to_hex(oid
));
1239 e
->versions
[0].mode
= e
->versions
[1].mode
;
1240 e
->name
= to_atom(c
, strlen(c
));
1241 c
+= e
->name
->str_len
+ 1;
1242 hashcpy(e
->versions
[0].oid
.hash
, (unsigned char *)c
);
1243 hashcpy(e
->versions
[1].oid
.hash
, (unsigned char *)c
);
1244 c
+= GIT_SHA1_RAWSZ
;
1249 static int tecmp0 (const void *_a
, const void *_b
)
1251 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1252 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1253 return base_name_compare(
1254 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1255 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1258 static int tecmp1 (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
[1].mode
,
1264 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1267 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1273 QSORT(t
->entries
, t
->entry_count
, tecmp0
);
1275 QSORT(t
->entries
, t
->entry_count
, tecmp1
);
1277 for (i
= 0; i
< t
->entry_count
; i
++) {
1278 if (t
->entries
[i
]->versions
[v
].mode
)
1279 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1283 strbuf_grow(b
, maxlen
);
1284 for (i
= 0; i
< t
->entry_count
; i
++) {
1285 struct tree_entry
*e
= t
->entries
[i
];
1286 if (!e
->versions
[v
].mode
)
1288 strbuf_addf(b
, "%o %s%c",
1289 (unsigned int)(e
->versions
[v
].mode
& ~NO_DELTA
),
1290 e
->name
->str_dat
, '\0');
1291 strbuf_add(b
, e
->versions
[v
].oid
.hash
, GIT_SHA1_RAWSZ
);
1295 static void store_tree(struct tree_entry
*root
)
1297 struct tree_content
*t
;
1298 unsigned int i
, j
, del
;
1299 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1300 struct object_entry
*le
= NULL
;
1302 if (!is_null_oid(&root
->versions
[1].oid
))
1309 for (i
= 0; i
< t
->entry_count
; i
++) {
1310 if (t
->entries
[i
]->tree
)
1311 store_tree(t
->entries
[i
]);
1314 if (!(root
->versions
[0].mode
& NO_DELTA
))
1315 le
= find_object(&root
->versions
[0].oid
);
1316 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1317 mktree(t
, 0, &old_tree
);
1319 lo
.offset
= le
->idx
.offset
;
1320 lo
.depth
= t
->delta_depth
;
1323 mktree(t
, 1, &new_tree
);
1324 store_object(OBJ_TREE
, &new_tree
, &lo
, &root
->versions
[1].oid
, 0);
1326 t
->delta_depth
= lo
.depth
;
1327 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1328 struct tree_entry
*e
= t
->entries
[i
];
1329 if (e
->versions
[1].mode
) {
1330 e
->versions
[0].mode
= e
->versions
[1].mode
;
1331 oidcpy(&e
->versions
[0].oid
, &e
->versions
[1].oid
);
1332 t
->entries
[j
++] = e
;
1334 release_tree_entry(e
);
1338 t
->entry_count
-= del
;
1341 static void tree_content_replace(
1342 struct tree_entry
*root
,
1343 const struct object_id
*oid
,
1344 const uint16_t mode
,
1345 struct tree_content
*newtree
)
1348 die("Root cannot be a non-directory");
1349 oidclr(&root
->versions
[0].oid
);
1350 oidcpy(&root
->versions
[1].oid
, oid
);
1352 release_tree_content_recursive(root
->tree
);
1353 root
->tree
= newtree
;
1356 static int tree_content_set(
1357 struct tree_entry
*root
,
1359 const struct object_id
*oid
,
1360 const uint16_t mode
,
1361 struct tree_content
*subtree
)
1363 struct tree_content
*t
;
1366 struct tree_entry
*e
;
1368 slash1
= strchrnul(p
, '/');
1371 die("Empty path component found in input");
1372 if (!*slash1
&& !S_ISDIR(mode
) && subtree
)
1373 die("Non-directories cannot have subtrees");
1378 for (i
= 0; i
< t
->entry_count
; i
++) {
1380 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1383 && e
->versions
[1].mode
== mode
1384 && oideq(&e
->versions
[1].oid
, oid
))
1386 e
->versions
[1].mode
= mode
;
1387 oidcpy(&e
->versions
[1].oid
, oid
);
1389 release_tree_content_recursive(e
->tree
);
1393 * We need to leave e->versions[0].sha1 alone
1394 * to avoid modifying the preimage tree used
1395 * when writing out the parent directory.
1396 * But after replacing the subdir with a
1397 * completely different one, it's not a good
1398 * delta base any more, and besides, we've
1399 * thrown away the tree entries needed to
1400 * make a delta against it.
1402 * So let's just explicitly disable deltas
1405 if (S_ISDIR(e
->versions
[0].mode
))
1406 e
->versions
[0].mode
|= NO_DELTA
;
1408 oidclr(&root
->versions
[1].oid
);
1411 if (!S_ISDIR(e
->versions
[1].mode
)) {
1412 e
->tree
= new_tree_content(8);
1413 e
->versions
[1].mode
= S_IFDIR
;
1417 if (tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
)) {
1418 oidclr(&root
->versions
[1].oid
);
1425 if (t
->entry_count
== t
->entry_capacity
)
1426 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1427 e
= new_tree_entry();
1428 e
->name
= to_atom(p
, n
);
1429 e
->versions
[0].mode
= 0;
1430 oidclr(&e
->versions
[0].oid
);
1431 t
->entries
[t
->entry_count
++] = e
;
1433 e
->tree
= new_tree_content(8);
1434 e
->versions
[1].mode
= S_IFDIR
;
1435 tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
);
1438 e
->versions
[1].mode
= mode
;
1439 oidcpy(&e
->versions
[1].oid
, oid
);
1441 oidclr(&root
->versions
[1].oid
);
1445 static int tree_content_remove(
1446 struct tree_entry
*root
,
1448 struct tree_entry
*backup_leaf
,
1451 struct tree_content
*t
;
1454 struct tree_entry
*e
;
1456 slash1
= strchrnul(p
, '/');
1462 if (!*p
&& allow_root
) {
1468 for (i
= 0; i
< t
->entry_count
; i
++) {
1470 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1471 if (*slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1473 * If p names a file in some subdirectory, and a
1474 * file or symlink matching the name of the
1475 * parent directory of p exists, then p cannot
1476 * exist and need not be deleted.
1479 if (!*slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1483 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
, 0)) {
1484 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1485 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1486 oidclr(&root
->versions
[1].oid
);
1500 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1502 release_tree_content_recursive(e
->tree
);
1504 e
->versions
[1].mode
= 0;
1505 oidclr(&e
->versions
[1].oid
);
1506 oidclr(&root
->versions
[1].oid
);
1510 static int tree_content_get(
1511 struct tree_entry
*root
,
1513 struct tree_entry
*leaf
,
1516 struct tree_content
*t
;
1519 struct tree_entry
*e
;
1521 slash1
= strchrnul(p
, '/');
1523 if (!n
&& !allow_root
)
1524 die("Empty path component found in input");
1535 for (i
= 0; i
< t
->entry_count
; i
++) {
1537 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1540 if (!S_ISDIR(e
->versions
[1].mode
))
1544 return tree_content_get(e
, slash1
+ 1, leaf
, 0);
1550 memcpy(leaf
, e
, sizeof(*leaf
));
1551 if (e
->tree
&& is_null_oid(&e
->versions
[1].oid
))
1552 leaf
->tree
= dup_tree_content(e
->tree
);
1558 static int update_branch(struct branch
*b
)
1560 static const char *msg
= "fast-import";
1561 struct ref_transaction
*transaction
;
1562 struct object_id old_oid
;
1563 struct strbuf err
= STRBUF_INIT
;
1565 if (is_null_oid(&b
->oid
)) {
1567 delete_ref(NULL
, b
->name
, NULL
, 0);
1570 if (read_ref(b
->name
, &old_oid
))
1572 if (!force_update
&& !is_null_oid(&old_oid
)) {
1573 struct commit
*old_cmit
, *new_cmit
;
1575 old_cmit
= lookup_commit_reference_gently(the_repository
,
1577 new_cmit
= lookup_commit_reference_gently(the_repository
,
1579 if (!old_cmit
|| !new_cmit
)
1580 return error("Branch %s is missing commits.", b
->name
);
1582 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1583 warning("Not updating %s"
1584 " (new tip %s does not contain %s)",
1585 b
->name
, oid_to_hex(&b
->oid
),
1586 oid_to_hex(&old_oid
));
1590 transaction
= ref_transaction_begin(&err
);
1592 ref_transaction_update(transaction
, b
->name
, &b
->oid
, &old_oid
,
1594 ref_transaction_commit(transaction
, &err
)) {
1595 ref_transaction_free(transaction
);
1596 error("%s", err
.buf
);
1597 strbuf_release(&err
);
1600 ref_transaction_free(transaction
);
1601 strbuf_release(&err
);
1605 static void dump_branches(void)
1610 for (i
= 0; i
< branch_table_sz
; i
++) {
1611 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1612 failure
|= update_branch(b
);
1616 static void dump_tags(void)
1618 static const char *msg
= "fast-import";
1620 struct strbuf ref_name
= STRBUF_INIT
;
1621 struct strbuf err
= STRBUF_INIT
;
1622 struct ref_transaction
*transaction
;
1624 transaction
= ref_transaction_begin(&err
);
1626 failure
|= error("%s", err
.buf
);
1629 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1630 strbuf_reset(&ref_name
);
1631 strbuf_addf(&ref_name
, "refs/tags/%s", t
->name
);
1633 if (ref_transaction_update(transaction
, ref_name
.buf
,
1634 &t
->oid
, NULL
, 0, msg
, &err
)) {
1635 failure
|= error("%s", err
.buf
);
1639 if (ref_transaction_commit(transaction
, &err
))
1640 failure
|= error("%s", err
.buf
);
1643 ref_transaction_free(transaction
);
1644 strbuf_release(&ref_name
);
1645 strbuf_release(&err
);
1648 static void dump_marks_helper(FILE *f
,
1654 for (k
= 0; k
< 1024; k
++) {
1655 if (m
->data
.sets
[k
])
1656 dump_marks_helper(f
, base
+ (k
<< m
->shift
),
1660 for (k
= 0; k
< 1024; k
++) {
1661 if (m
->data
.marked
[k
])
1662 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1663 oid_to_hex(&m
->data
.marked
[k
]->idx
.oid
));
1668 static void dump_marks(void)
1670 struct lock_file mark_lock
= LOCK_INIT
;
1673 if (!export_marks_file
|| (import_marks_file
&& !import_marks_file_done
))
1676 if (safe_create_leading_directories_const(export_marks_file
)) {
1677 failure
|= error_errno("unable to create leading directories of %s",
1682 if (hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0) < 0) {
1683 failure
|= error_errno("Unable to write marks file %s",
1688 f
= fdopen_lock_file(&mark_lock
, "w");
1690 int saved_errno
= errno
;
1691 rollback_lock_file(&mark_lock
);
1692 failure
|= error("Unable to write marks file %s: %s",
1693 export_marks_file
, strerror(saved_errno
));
1697 dump_marks_helper(f
, 0, marks
);
1698 if (commit_lock_file(&mark_lock
)) {
1699 failure
|= error_errno("Unable to write file %s",
1705 static void read_marks(void)
1708 FILE *f
= fopen(import_marks_file
, "r");
1711 else if (import_marks_file_ignore_missing
&& errno
== ENOENT
)
1712 goto done
; /* Marks file does not exist */
1714 die_errno("cannot read '%s'", import_marks_file
);
1715 while (fgets(line
, sizeof(line
), f
)) {
1718 struct object_id oid
;
1719 struct object_entry
*e
;
1721 end
= strchr(line
, '\n');
1722 if (line
[0] != ':' || !end
)
1723 die("corrupt mark line: %s", line
);
1725 mark
= strtoumax(line
+ 1, &end
, 10);
1726 if (!mark
|| end
== line
+ 1
1727 || *end
!= ' ' || get_oid_hex(end
+ 1, &oid
))
1728 die("corrupt mark line: %s", line
);
1729 e
= find_object(&oid
);
1731 enum object_type type
= oid_object_info(the_repository
,
1734 die("object not found: %s", oid_to_hex(&oid
));
1735 e
= insert_object(&oid
);
1737 e
->pack_id
= MAX_PACK_ID
;
1738 e
->idx
.offset
= 1; /* just not zero! */
1740 insert_mark(mark
, e
);
1744 import_marks_file_done
= 1;
1748 static int read_next_command(void)
1750 static int stdin_eof
= 0;
1753 unread_command_buf
= 0;
1760 if (unread_command_buf
) {
1761 unread_command_buf
= 0;
1763 struct recent_command
*rc
;
1765 strbuf_detach(&command_buf
, NULL
);
1766 stdin_eof
= strbuf_getline_lf(&command_buf
, stdin
);
1770 if (!seen_data_command
1771 && !starts_with(command_buf
.buf
, "feature ")
1772 && !starts_with(command_buf
.buf
, "option ")) {
1781 cmd_hist
.next
= rc
->next
;
1782 cmd_hist
.next
->prev
= &cmd_hist
;
1786 rc
->buf
= command_buf
.buf
;
1787 rc
->prev
= cmd_tail
;
1788 rc
->next
= cmd_hist
.prev
;
1789 rc
->prev
->next
= rc
;
1792 if (skip_prefix(command_buf
.buf
, "get-mark ", &p
)) {
1796 if (skip_prefix(command_buf
.buf
, "cat-blob ", &p
)) {
1800 if (command_buf
.buf
[0] == '#')
1806 static void skip_optional_lf(void)
1808 int term_char
= fgetc(stdin
);
1809 if (term_char
!= '\n' && term_char
!= EOF
)
1810 ungetc(term_char
, stdin
);
1813 static void parse_mark(void)
1816 if (skip_prefix(command_buf
.buf
, "mark :", &v
)) {
1817 next_mark
= strtoumax(v
, NULL
, 10);
1818 read_next_command();
1824 static void parse_original_identifier(void)
1827 if (skip_prefix(command_buf
.buf
, "original-oid ", &v
))
1828 read_next_command();
1831 static int parse_data(struct strbuf
*sb
, uintmax_t limit
, uintmax_t *len_res
)
1836 if (!skip_prefix(command_buf
.buf
, "data ", &data
))
1837 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1839 if (skip_prefix(data
, "<<", &data
)) {
1840 char *term
= xstrdup(data
);
1841 size_t term_len
= command_buf
.len
- (data
- command_buf
.buf
);
1843 strbuf_detach(&command_buf
, NULL
);
1845 if (strbuf_getline_lf(&command_buf
, stdin
) == EOF
)
1846 die("EOF in data (terminator '%s' not found)", term
);
1847 if (term_len
== command_buf
.len
1848 && !strcmp(term
, command_buf
.buf
))
1850 strbuf_addbuf(sb
, &command_buf
);
1851 strbuf_addch(sb
, '\n');
1856 uintmax_t len
= strtoumax(data
, NULL
, 10);
1857 size_t n
= 0, length
= (size_t)len
;
1859 if (limit
&& limit
< len
) {
1864 die("data is too large to use in this context");
1866 while (n
< length
) {
1867 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1868 if (!s
&& feof(stdin
))
1869 die("EOF in data (%lu bytes remaining)",
1870 (unsigned long)(length
- n
));
1879 static int validate_raw_date(const char *src
, struct strbuf
*result
)
1881 const char *orig_src
= src
;
1887 num
= strtoul(src
, &endp
, 10);
1888 /* NEEDSWORK: perhaps check for reasonable values? */
1889 if (errno
|| endp
== src
|| *endp
!= ' ')
1893 if (*src
!= '-' && *src
!= '+')
1896 num
= strtoul(src
+ 1, &endp
, 10);
1897 if (errno
|| endp
== src
+ 1 || *endp
|| 1400 < num
)
1900 strbuf_addstr(result
, orig_src
);
1904 static char *parse_ident(const char *buf
)
1908 struct strbuf ident
= STRBUF_INIT
;
1910 /* ensure there is a space delimiter even if there is no name */
1914 ltgt
= buf
+ strcspn(buf
, "<>");
1916 die("Missing < in ident string: %s", buf
);
1917 if (ltgt
!= buf
&& ltgt
[-1] != ' ')
1918 die("Missing space before < in ident string: %s", buf
);
1919 ltgt
= ltgt
+ 1 + strcspn(ltgt
+ 1, "<>");
1921 die("Missing > in ident string: %s", buf
);
1924 die("Missing space after > in ident string: %s", buf
);
1926 name_len
= ltgt
- buf
;
1927 strbuf_add(&ident
, buf
, name_len
);
1931 if (validate_raw_date(ltgt
, &ident
) < 0)
1932 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
1934 case WHENSPEC_RFC2822
:
1935 if (parse_date(ltgt
, &ident
) < 0)
1936 die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt
, buf
);
1939 if (strcmp("now", ltgt
))
1940 die("Date in ident must be 'now': %s", buf
);
1945 return strbuf_detach(&ident
, NULL
);
1948 static void parse_and_store_blob(
1949 struct last_object
*last
,
1950 struct object_id
*oidout
,
1953 static struct strbuf buf
= STRBUF_INIT
;
1956 if (parse_data(&buf
, big_file_threshold
, &len
))
1957 store_object(OBJ_BLOB
, &buf
, last
, oidout
, mark
);
1960 strbuf_release(&last
->data
);
1964 stream_blob(len
, oidout
, mark
);
1969 static void parse_new_blob(void)
1971 read_next_command();
1973 parse_original_identifier();
1974 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
1977 static void unload_one_branch(void)
1979 while (cur_active_branches
1980 && cur_active_branches
>= max_active_branches
) {
1981 uintmax_t min_commit
= ULONG_MAX
;
1982 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1984 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1985 if (e
->last_commit
< min_commit
) {
1987 min_commit
= e
->last_commit
;
1993 e
= p
->active_next_branch
;
1994 p
->active_next_branch
= e
->active_next_branch
;
1996 e
= active_branches
;
1997 active_branches
= e
->active_next_branch
;
2000 e
->active_next_branch
= NULL
;
2001 if (e
->branch_tree
.tree
) {
2002 release_tree_content_recursive(e
->branch_tree
.tree
);
2003 e
->branch_tree
.tree
= NULL
;
2005 cur_active_branches
--;
2009 static void load_branch(struct branch
*b
)
2011 load_tree(&b
->branch_tree
);
2014 b
->active_next_branch
= active_branches
;
2015 active_branches
= b
;
2016 cur_active_branches
++;
2017 branch_load_count
++;
2021 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2023 unsigned char fanout
= 0;
2024 while ((num_notes
>>= 8))
2029 static void construct_path_with_fanout(const char *hex_sha1
,
2030 unsigned char fanout
, char *path
)
2032 unsigned int i
= 0, j
= 0;
2033 if (fanout
>= the_hash_algo
->rawsz
)
2034 die("Too large fanout (%u)", fanout
);
2036 path
[i
++] = hex_sha1
[j
++];
2037 path
[i
++] = hex_sha1
[j
++];
2041 memcpy(path
+ i
, hex_sha1
+ j
, the_hash_algo
->hexsz
- j
);
2042 path
[i
+ the_hash_algo
->hexsz
- j
] = '\0';
2045 static uintmax_t do_change_note_fanout(
2046 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2047 char *hex_oid
, unsigned int hex_oid_len
,
2048 char *fullpath
, unsigned int fullpath_len
,
2049 unsigned char fanout
)
2051 struct tree_content
*t
;
2052 struct tree_entry
*e
, leaf
;
2053 unsigned int i
, tmp_hex_oid_len
, tmp_fullpath_len
;
2054 uintmax_t num_notes
= 0;
2055 struct object_id oid
;
2062 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2064 tmp_hex_oid_len
= hex_oid_len
+ e
->name
->str_len
;
2065 tmp_fullpath_len
= fullpath_len
;
2068 * We're interested in EITHER existing note entries (entries
2069 * with exactly 40 hex chars in path, not including directory
2070 * separators), OR directory entries that may contain note
2071 * entries (with < 40 hex chars in path).
2072 * Also, each path component in a note entry must be a multiple
2075 if (!e
->versions
[1].mode
||
2076 tmp_hex_oid_len
> GIT_SHA1_HEXSZ
||
2077 e
->name
->str_len
% 2)
2080 /* This _may_ be a note entry, or a subdir containing notes */
2081 memcpy(hex_oid
+ hex_oid_len
, e
->name
->str_dat
,
2083 if (tmp_fullpath_len
)
2084 fullpath
[tmp_fullpath_len
++] = '/';
2085 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2087 tmp_fullpath_len
+= e
->name
->str_len
;
2088 fullpath
[tmp_fullpath_len
] = '\0';
2090 if (tmp_hex_oid_len
== GIT_SHA1_HEXSZ
&& !get_oid_hex(hex_oid
, &oid
)) {
2091 /* This is a note entry */
2092 if (fanout
== 0xff) {
2093 /* Counting mode, no rename */
2097 construct_path_with_fanout(hex_oid
, fanout
, realpath
);
2098 if (!strcmp(fullpath
, realpath
)) {
2099 /* Note entry is in correct location */
2104 /* Rename fullpath to realpath */
2105 if (!tree_content_remove(orig_root
, fullpath
, &leaf
, 0))
2106 die("Failed to remove path %s", fullpath
);
2107 tree_content_set(orig_root
, realpath
,
2108 &leaf
.versions
[1].oid
,
2109 leaf
.versions
[1].mode
,
2111 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2112 /* This is a subdir that may contain note entries */
2113 num_notes
+= do_change_note_fanout(orig_root
, e
,
2114 hex_oid
, tmp_hex_oid_len
,
2115 fullpath
, tmp_fullpath_len
, fanout
);
2118 /* The above may have reallocated the current tree_content */
2124 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2125 unsigned char fanout
)
2128 * The size of path is due to one slash between every two hex digits,
2129 * plus the terminating NUL. Note that there is no slash at the end, so
2130 * the number of slashes is one less than half the number of hex
2133 char hex_oid
[GIT_MAX_HEXSZ
], path
[GIT_MAX_HEXSZ
+ (GIT_MAX_HEXSZ
/ 2) - 1 + 1];
2134 return do_change_note_fanout(root
, root
, hex_oid
, 0, path
, 0, fanout
);
2138 * Given a pointer into a string, parse a mark reference:
2140 * idnum ::= ':' bigint;
2142 * Return the first character after the value in *endptr.
2144 * Complain if the following character is not what is expected,
2145 * either a space or end of the string.
2147 static uintmax_t parse_mark_ref(const char *p
, char **endptr
)
2153 mark
= strtoumax(p
, endptr
, 10);
2155 die("No value after ':' in mark: %s", command_buf
.buf
);
2160 * Parse the mark reference, and complain if this is not the end of
2163 static uintmax_t parse_mark_ref_eol(const char *p
)
2168 mark
= parse_mark_ref(p
, &end
);
2170 die("Garbage after mark: %s", command_buf
.buf
);
2175 * Parse the mark reference, demanding a trailing space. Return a
2176 * pointer to the space.
2178 static uintmax_t parse_mark_ref_space(const char **p
)
2183 mark
= parse_mark_ref(*p
, &end
);
2185 die("Missing space after mark: %s", command_buf
.buf
);
2190 static void file_change_m(const char *p
, struct branch
*b
)
2192 static struct strbuf uq
= STRBUF_INIT
;
2194 struct object_entry
*oe
;
2195 struct object_id oid
;
2196 uint16_t mode
, inline_data
= 0;
2198 p
= get_mode(p
, &mode
);
2200 die("Corrupt mode: %s", command_buf
.buf
);
2205 case S_IFREG
| 0644:
2206 case S_IFREG
| 0755:
2213 die("Corrupt mode: %s", command_buf
.buf
);
2217 oe
= find_mark(parse_mark_ref_space(&p
));
2218 oidcpy(&oid
, &oe
->idx
.oid
);
2219 } else if (skip_prefix(p
, "inline ", &p
)) {
2221 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2223 if (parse_oid_hex(p
, &oid
, &p
))
2224 die("Invalid dataref: %s", command_buf
.buf
);
2225 oe
= find_object(&oid
);
2227 die("Missing space after SHA1: %s", command_buf
.buf
);
2231 if (!unquote_c_style(&uq
, p
, &endp
)) {
2233 die("Garbage after path in: %s", command_buf
.buf
);
2237 /* Git does not track empty, non-toplevel directories. */
2238 if (S_ISDIR(mode
) && is_empty_tree_oid(&oid
) && *p
) {
2239 tree_content_remove(&b
->branch_tree
, p
, NULL
, 0);
2243 if (S_ISGITLINK(mode
)) {
2245 die("Git links cannot be specified 'inline': %s",
2248 if (oe
->type
!= OBJ_COMMIT
)
2249 die("Not a commit (actually a %s): %s",
2250 type_name(oe
->type
), command_buf
.buf
);
2253 * Accept the sha1 without checking; it expected to be in
2254 * another repository.
2256 } else if (inline_data
) {
2258 die("Directories cannot be specified 'inline': %s",
2261 strbuf_addstr(&uq
, p
);
2264 read_next_command();
2265 parse_and_store_blob(&last_blob
, &oid
, 0);
2267 enum object_type expected
= S_ISDIR(mode
) ?
2269 enum object_type type
= oe
? oe
->type
:
2270 oid_object_info(the_repository
, &oid
,
2273 die("%s not found: %s",
2274 S_ISDIR(mode
) ? "Tree" : "Blob",
2276 if (type
!= expected
)
2277 die("Not a %s (actually a %s): %s",
2278 type_name(expected
), type_name(type
),
2283 tree_content_replace(&b
->branch_tree
, &oid
, mode
, NULL
);
2286 tree_content_set(&b
->branch_tree
, p
, &oid
, mode
, NULL
);
2289 static void file_change_d(const char *p
, struct branch
*b
)
2291 static struct strbuf uq
= STRBUF_INIT
;
2295 if (!unquote_c_style(&uq
, p
, &endp
)) {
2297 die("Garbage after path in: %s", command_buf
.buf
);
2300 tree_content_remove(&b
->branch_tree
, p
, NULL
, 1);
2303 static void file_change_cr(const char *s
, struct branch
*b
, int rename
)
2306 static struct strbuf s_uq
= STRBUF_INIT
;
2307 static struct strbuf d_uq
= STRBUF_INIT
;
2309 struct tree_entry leaf
;
2311 strbuf_reset(&s_uq
);
2312 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2314 die("Missing space after source: %s", command_buf
.buf
);
2316 endp
= strchr(s
, ' ');
2318 die("Missing space after source: %s", command_buf
.buf
);
2319 strbuf_add(&s_uq
, s
, endp
- s
);
2325 die("Missing dest: %s", command_buf
.buf
);
2328 strbuf_reset(&d_uq
);
2329 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2331 die("Garbage after dest in: %s", command_buf
.buf
);
2335 memset(&leaf
, 0, sizeof(leaf
));
2337 tree_content_remove(&b
->branch_tree
, s
, &leaf
, 1);
2339 tree_content_get(&b
->branch_tree
, s
, &leaf
, 1);
2340 if (!leaf
.versions
[1].mode
)
2341 die("Path %s not in branch", s
);
2342 if (!*d
) { /* C "path/to/subdir" "" */
2343 tree_content_replace(&b
->branch_tree
,
2344 &leaf
.versions
[1].oid
,
2345 leaf
.versions
[1].mode
,
2349 tree_content_set(&b
->branch_tree
, d
,
2350 &leaf
.versions
[1].oid
,
2351 leaf
.versions
[1].mode
,
2355 static void note_change_n(const char *p
, struct branch
*b
, unsigned char *old_fanout
)
2357 static struct strbuf uq
= STRBUF_INIT
;
2358 struct object_entry
*oe
;
2360 struct object_id oid
, commit_oid
;
2362 uint16_t inline_data
= 0;
2363 unsigned char new_fanout
;
2366 * When loading a branch, we don't traverse its tree to count the real
2367 * number of notes (too expensive to do this for all non-note refs).
2368 * This means that recently loaded notes refs might incorrectly have
2369 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2371 * Fix this by traversing the tree and counting the number of notes
2372 * when b->num_notes == 0. If the notes tree is truly empty, the
2373 * calculation should not take long.
2375 if (b
->num_notes
== 0 && *old_fanout
== 0) {
2376 /* Invoke change_note_fanout() in "counting mode". */
2377 b
->num_notes
= change_note_fanout(&b
->branch_tree
, 0xff);
2378 *old_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2381 /* Now parse the notemodify command. */
2382 /* <dataref> or 'inline' */
2384 oe
= find_mark(parse_mark_ref_space(&p
));
2385 oidcpy(&oid
, &oe
->idx
.oid
);
2386 } else if (skip_prefix(p
, "inline ", &p
)) {
2388 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2390 if (parse_oid_hex(p
, &oid
, &p
))
2391 die("Invalid dataref: %s", command_buf
.buf
);
2392 oe
= find_object(&oid
);
2394 die("Missing space after SHA1: %s", command_buf
.buf
);
2398 s
= lookup_branch(p
);
2400 if (is_null_oid(&s
->oid
))
2401 die("Can't add a note on empty branch.");
2402 oidcpy(&commit_oid
, &s
->oid
);
2403 } else if (*p
== ':') {
2404 uintmax_t commit_mark
= parse_mark_ref_eol(p
);
2405 struct object_entry
*commit_oe
= find_mark(commit_mark
);
2406 if (commit_oe
->type
!= OBJ_COMMIT
)
2407 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2408 oidcpy(&commit_oid
, &commit_oe
->idx
.oid
);
2409 } else if (!get_oid(p
, &commit_oid
)) {
2411 char *buf
= read_object_with_reference(&commit_oid
,
2414 if (!buf
|| size
< 46)
2415 die("Not a valid commit: %s", p
);
2418 die("Invalid ref name or SHA1 expression: %s", p
);
2422 strbuf_addstr(&uq
, p
);
2425 read_next_command();
2426 parse_and_store_blob(&last_blob
, &oid
, 0);
2428 if (oe
->type
!= OBJ_BLOB
)
2429 die("Not a blob (actually a %s): %s",
2430 type_name(oe
->type
), command_buf
.buf
);
2431 } else if (!is_null_oid(&oid
)) {
2432 enum object_type type
= oid_object_info(the_repository
, &oid
,
2435 die("Blob not found: %s", command_buf
.buf
);
2436 if (type
!= OBJ_BLOB
)
2437 die("Not a blob (actually a %s): %s",
2438 type_name(type
), command_buf
.buf
);
2441 construct_path_with_fanout(oid_to_hex(&commit_oid
), *old_fanout
, path
);
2442 if (tree_content_remove(&b
->branch_tree
, path
, NULL
, 0))
2445 if (is_null_oid(&oid
))
2446 return; /* nothing to insert */
2449 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2450 construct_path_with_fanout(oid_to_hex(&commit_oid
), new_fanout
, path
);
2451 tree_content_set(&b
->branch_tree
, path
, &oid
, S_IFREG
| 0644, NULL
);
2454 static void file_change_deleteall(struct branch
*b
)
2456 release_tree_content_recursive(b
->branch_tree
.tree
);
2457 oidclr(&b
->branch_tree
.versions
[0].oid
);
2458 oidclr(&b
->branch_tree
.versions
[1].oid
);
2459 load_tree(&b
->branch_tree
);
2463 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2465 if (!buf
|| size
< GIT_SHA1_HEXSZ
+ 6)
2466 die("Not a valid commit: %s", oid_to_hex(&b
->oid
));
2467 if (memcmp("tree ", buf
, 5)
2468 || get_oid_hex(buf
+ 5, &b
->branch_tree
.versions
[1].oid
))
2469 die("The commit %s is corrupt", oid_to_hex(&b
->oid
));
2470 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2471 &b
->branch_tree
.versions
[1].oid
);
2474 static void parse_from_existing(struct branch
*b
)
2476 if (is_null_oid(&b
->oid
)) {
2477 oidclr(&b
->branch_tree
.versions
[0].oid
);
2478 oidclr(&b
->branch_tree
.versions
[1].oid
);
2483 buf
= read_object_with_reference(&b
->oid
, commit_type
, &size
,
2485 parse_from_commit(b
, buf
, size
);
2490 static int parse_from(struct branch
*b
)
2494 struct object_id oid
;
2496 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2499 oidcpy(&oid
, &b
->branch_tree
.versions
[1].oid
);
2501 s
= lookup_branch(from
);
2503 die("Can't create a branch from itself: %s", b
->name
);
2505 struct object_id
*t
= &s
->branch_tree
.versions
[1].oid
;
2506 oidcpy(&b
->oid
, &s
->oid
);
2507 oidcpy(&b
->branch_tree
.versions
[0].oid
, t
);
2508 oidcpy(&b
->branch_tree
.versions
[1].oid
, t
);
2509 } else if (*from
== ':') {
2510 uintmax_t idnum
= parse_mark_ref_eol(from
);
2511 struct object_entry
*oe
= find_mark(idnum
);
2512 if (oe
->type
!= OBJ_COMMIT
)
2513 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2514 if (!oideq(&b
->oid
, &oe
->idx
.oid
)) {
2515 oidcpy(&b
->oid
, &oe
->idx
.oid
);
2516 if (oe
->pack_id
!= MAX_PACK_ID
) {
2518 char *buf
= gfi_unpack_entry(oe
, &size
);
2519 parse_from_commit(b
, buf
, size
);
2522 parse_from_existing(b
);
2524 } else if (!get_oid(from
, &b
->oid
)) {
2525 parse_from_existing(b
);
2526 if (is_null_oid(&b
->oid
))
2530 die("Invalid ref name or SHA1 expression: %s", from
);
2532 if (b
->branch_tree
.tree
&& !oideq(&oid
, &b
->branch_tree
.versions
[1].oid
)) {
2533 release_tree_content_recursive(b
->branch_tree
.tree
);
2534 b
->branch_tree
.tree
= NULL
;
2537 read_next_command();
2541 static struct hash_list
*parse_merge(unsigned int *count
)
2543 struct hash_list
*list
= NULL
, **tail
= &list
, *n
;
2548 while (skip_prefix(command_buf
.buf
, "merge ", &from
)) {
2549 n
= xmalloc(sizeof(*n
));
2550 s
= lookup_branch(from
);
2552 oidcpy(&n
->oid
, &s
->oid
);
2553 else if (*from
== ':') {
2554 uintmax_t idnum
= parse_mark_ref_eol(from
);
2555 struct object_entry
*oe
= find_mark(idnum
);
2556 if (oe
->type
!= OBJ_COMMIT
)
2557 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2558 oidcpy(&n
->oid
, &oe
->idx
.oid
);
2559 } else if (!get_oid(from
, &n
->oid
)) {
2561 char *buf
= read_object_with_reference(&n
->oid
,
2564 if (!buf
|| size
< 46)
2565 die("Not a valid commit: %s", from
);
2568 die("Invalid ref name or SHA1 expression: %s", from
);
2575 read_next_command();
2580 static void parse_new_commit(const char *arg
)
2582 static struct strbuf msg
= STRBUF_INIT
;
2584 char *author
= NULL
;
2585 char *committer
= NULL
;
2586 struct hash_list
*merge_list
= NULL
;
2587 unsigned int merge_count
;
2588 unsigned char prev_fanout
, new_fanout
;
2591 b
= lookup_branch(arg
);
2593 b
= new_branch(arg
);
2595 read_next_command();
2597 parse_original_identifier();
2598 if (skip_prefix(command_buf
.buf
, "author ", &v
)) {
2599 author
= parse_ident(v
);
2600 read_next_command();
2602 if (skip_prefix(command_buf
.buf
, "committer ", &v
)) {
2603 committer
= parse_ident(v
);
2604 read_next_command();
2607 die("Expected committer but didn't get one");
2608 parse_data(&msg
, 0, NULL
);
2609 read_next_command();
2611 merge_list
= parse_merge(&merge_count
);
2613 /* ensure the branch is active/loaded */
2614 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2615 unload_one_branch();
2619 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2622 while (command_buf
.len
> 0) {
2623 if (skip_prefix(command_buf
.buf
, "M ", &v
))
2624 file_change_m(v
, b
);
2625 else if (skip_prefix(command_buf
.buf
, "D ", &v
))
2626 file_change_d(v
, b
);
2627 else if (skip_prefix(command_buf
.buf
, "R ", &v
))
2628 file_change_cr(v
, b
, 1);
2629 else if (skip_prefix(command_buf
.buf
, "C ", &v
))
2630 file_change_cr(v
, b
, 0);
2631 else if (skip_prefix(command_buf
.buf
, "N ", &v
))
2632 note_change_n(v
, b
, &prev_fanout
);
2633 else if (!strcmp("deleteall", command_buf
.buf
))
2634 file_change_deleteall(b
);
2635 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
2638 unread_command_buf
= 1;
2641 if (read_next_command() == EOF
)
2645 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2646 if (new_fanout
!= prev_fanout
)
2647 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2649 /* build the tree and the commit */
2650 store_tree(&b
->branch_tree
);
2651 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2652 &b
->branch_tree
.versions
[1].oid
);
2654 strbuf_reset(&new_data
);
2655 strbuf_addf(&new_data
, "tree %s\n",
2656 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
2657 if (!is_null_oid(&b
->oid
))
2658 strbuf_addf(&new_data
, "parent %s\n",
2659 oid_to_hex(&b
->oid
));
2660 while (merge_list
) {
2661 struct hash_list
*next
= merge_list
->next
;
2662 strbuf_addf(&new_data
, "parent %s\n",
2663 oid_to_hex(&merge_list
->oid
));
2667 strbuf_addf(&new_data
,
2671 author
? author
: committer
, committer
);
2672 strbuf_addbuf(&new_data
, &msg
);
2676 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, &b
->oid
, next_mark
))
2677 b
->pack_id
= pack_id
;
2678 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2681 static void parse_new_tag(const char *arg
)
2683 static struct strbuf msg
= STRBUF_INIT
;
2688 uintmax_t from_mark
= 0;
2689 struct object_id oid
;
2690 enum object_type type
;
2693 t
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct tag
));
2694 memset(t
, 0, sizeof(struct tag
));
2695 t
->name
= pool_strdup(arg
);
2697 last_tag
->next_tag
= t
;
2701 read_next_command();
2704 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2705 die("Expected from command, got %s", command_buf
.buf
);
2706 s
= lookup_branch(from
);
2708 if (is_null_oid(&s
->oid
))
2709 die("Can't tag an empty branch.");
2710 oidcpy(&oid
, &s
->oid
);
2712 } else if (*from
== ':') {
2713 struct object_entry
*oe
;
2714 from_mark
= parse_mark_ref_eol(from
);
2715 oe
= find_mark(from_mark
);
2717 oidcpy(&oid
, &oe
->idx
.oid
);
2718 } else if (!get_oid(from
, &oid
)) {
2719 struct object_entry
*oe
= find_object(&oid
);
2721 type
= oid_object_info(the_repository
, &oid
, NULL
);
2723 die("Not a valid object: %s", from
);
2727 die("Invalid ref name or SHA1 expression: %s", from
);
2728 read_next_command();
2730 /* original-oid ... */
2731 parse_original_identifier();
2734 if (skip_prefix(command_buf
.buf
, "tagger ", &v
)) {
2735 tagger
= parse_ident(v
);
2736 read_next_command();
2740 /* tag payload/message */
2741 parse_data(&msg
, 0, NULL
);
2743 /* build the tag object */
2744 strbuf_reset(&new_data
);
2746 strbuf_addf(&new_data
,
2750 oid_to_hex(&oid
), type_name(type
), t
->name
);
2752 strbuf_addf(&new_data
,
2753 "tagger %s\n", tagger
);
2754 strbuf_addch(&new_data
, '\n');
2755 strbuf_addbuf(&new_data
, &msg
);
2758 if (store_object(OBJ_TAG
, &new_data
, NULL
, &t
->oid
, 0))
2759 t
->pack_id
= MAX_PACK_ID
;
2761 t
->pack_id
= pack_id
;
2764 static void parse_reset_branch(const char *arg
)
2768 b
= lookup_branch(arg
);
2771 oidclr(&b
->branch_tree
.versions
[0].oid
);
2772 oidclr(&b
->branch_tree
.versions
[1].oid
);
2773 if (b
->branch_tree
.tree
) {
2774 release_tree_content_recursive(b
->branch_tree
.tree
);
2775 b
->branch_tree
.tree
= NULL
;
2779 b
= new_branch(arg
);
2780 read_next_command();
2782 if (command_buf
.len
> 0)
2783 unread_command_buf
= 1;
2786 static void cat_blob_write(const char *buf
, unsigned long size
)
2788 if (write_in_full(cat_blob_fd
, buf
, size
) < 0)
2789 die_errno("Write to frontend failed");
2792 static void cat_blob(struct object_entry
*oe
, struct object_id
*oid
)
2794 struct strbuf line
= STRBUF_INIT
;
2796 enum object_type type
= 0;
2799 if (!oe
|| oe
->pack_id
== MAX_PACK_ID
) {
2800 buf
= read_object_file(oid
, &type
, &size
);
2803 buf
= gfi_unpack_entry(oe
, &size
);
2807 * Output based on batch_one_object() from cat-file.c.
2810 strbuf_reset(&line
);
2811 strbuf_addf(&line
, "%s missing\n", oid_to_hex(oid
));
2812 cat_blob_write(line
.buf
, line
.len
);
2813 strbuf_release(&line
);
2818 die("Can't read object %s", oid_to_hex(oid
));
2819 if (type
!= OBJ_BLOB
)
2820 die("Object %s is a %s but a blob was expected.",
2821 oid_to_hex(oid
), type_name(type
));
2822 strbuf_reset(&line
);
2823 strbuf_addf(&line
, "%s %s %"PRIuMAX
"\n", oid_to_hex(oid
),
2824 type_name(type
), (uintmax_t)size
);
2825 cat_blob_write(line
.buf
, line
.len
);
2826 strbuf_release(&line
);
2827 cat_blob_write(buf
, size
);
2828 cat_blob_write("\n", 1);
2829 if (oe
&& oe
->pack_id
== pack_id
) {
2830 last_blob
.offset
= oe
->idx
.offset
;
2831 strbuf_attach(&last_blob
.data
, buf
, size
, size
);
2832 last_blob
.depth
= oe
->depth
;
2837 static void parse_get_mark(const char *p
)
2839 struct object_entry
*oe
;
2840 char output
[GIT_MAX_HEXSZ
+ 2];
2842 /* get-mark SP <object> LF */
2844 die("Not a mark: %s", p
);
2846 oe
= find_mark(parse_mark_ref_eol(p
));
2848 die("Unknown mark: %s", command_buf
.buf
);
2850 xsnprintf(output
, sizeof(output
), "%s\n", oid_to_hex(&oe
->idx
.oid
));
2851 cat_blob_write(output
, GIT_SHA1_HEXSZ
+ 1);
2854 static void parse_cat_blob(const char *p
)
2856 struct object_entry
*oe
;
2857 struct object_id oid
;
2859 /* cat-blob SP <object> LF */
2861 oe
= find_mark(parse_mark_ref_eol(p
));
2863 die("Unknown mark: %s", command_buf
.buf
);
2864 oidcpy(&oid
, &oe
->idx
.oid
);
2866 if (parse_oid_hex(p
, &oid
, &p
))
2867 die("Invalid dataref: %s", command_buf
.buf
);
2869 die("Garbage after SHA1: %s", command_buf
.buf
);
2870 oe
= find_object(&oid
);
2876 static struct object_entry
*dereference(struct object_entry
*oe
,
2877 struct object_id
*oid
)
2882 enum object_type type
= oid_object_info(the_repository
, oid
,
2885 die("object not found: %s", oid_to_hex(oid
));
2887 oe
= insert_object(oid
);
2889 oe
->pack_id
= MAX_PACK_ID
;
2893 case OBJ_TREE
: /* easy case. */
2899 die("Not a tree-ish: %s", command_buf
.buf
);
2902 if (oe
->pack_id
!= MAX_PACK_ID
) { /* in a pack being written */
2903 buf
= gfi_unpack_entry(oe
, &size
);
2905 enum object_type unused
;
2906 buf
= read_object_file(oid
, &unused
, &size
);
2909 die("Can't load object %s", oid_to_hex(oid
));
2911 /* Peel one layer. */
2914 if (size
< GIT_SHA1_HEXSZ
+ strlen("object ") ||
2915 get_oid_hex(buf
+ strlen("object "), oid
))
2916 die("Invalid SHA1 in tag: %s", command_buf
.buf
);
2919 if (size
< GIT_SHA1_HEXSZ
+ strlen("tree ") ||
2920 get_oid_hex(buf
+ strlen("tree "), oid
))
2921 die("Invalid SHA1 in commit: %s", command_buf
.buf
);
2925 return find_object(oid
);
2928 static struct object_entry
*parse_treeish_dataref(const char **p
)
2930 struct object_id oid
;
2931 struct object_entry
*e
;
2933 if (**p
== ':') { /* <mark> */
2934 e
= find_mark(parse_mark_ref_space(p
));
2936 die("Unknown mark: %s", command_buf
.buf
);
2937 oidcpy(&oid
, &e
->idx
.oid
);
2938 } else { /* <sha1> */
2939 if (parse_oid_hex(*p
, &oid
, p
))
2940 die("Invalid dataref: %s", command_buf
.buf
);
2941 e
= find_object(&oid
);
2943 die("Missing space after tree-ish: %s", command_buf
.buf
);
2946 while (!e
|| e
->type
!= OBJ_TREE
)
2947 e
= dereference(e
, &oid
);
2951 static void print_ls(int mode
, const unsigned char *sha1
, const char *path
)
2953 static struct strbuf line
= STRBUF_INIT
;
2955 /* See show_tree(). */
2957 S_ISGITLINK(mode
) ? commit_type
:
2958 S_ISDIR(mode
) ? tree_type
:
2962 /* missing SP path LF */
2963 strbuf_reset(&line
);
2964 strbuf_addstr(&line
, "missing ");
2965 quote_c_style(path
, &line
, NULL
, 0);
2966 strbuf_addch(&line
, '\n');
2968 /* mode SP type SP object_name TAB path LF */
2969 strbuf_reset(&line
);
2970 strbuf_addf(&line
, "%06o %s %s\t",
2971 mode
& ~NO_DELTA
, type
, sha1_to_hex(sha1
));
2972 quote_c_style(path
, &line
, NULL
, 0);
2973 strbuf_addch(&line
, '\n');
2975 cat_blob_write(line
.buf
, line
.len
);
2978 static void parse_ls(const char *p
, struct branch
*b
)
2980 struct tree_entry
*root
= NULL
;
2981 struct tree_entry leaf
= {NULL
};
2983 /* ls SP (<tree-ish> SP)? <path> */
2986 die("Not in a commit: %s", command_buf
.buf
);
2987 root
= &b
->branch_tree
;
2989 struct object_entry
*e
= parse_treeish_dataref(&p
);
2990 root
= new_tree_entry();
2991 oidcpy(&root
->versions
[1].oid
, &e
->idx
.oid
);
2992 if (!is_null_oid(&root
->versions
[1].oid
))
2993 root
->versions
[1].mode
= S_IFDIR
;
2997 static struct strbuf uq
= STRBUF_INIT
;
3000 if (unquote_c_style(&uq
, p
, &endp
))
3001 die("Invalid path: %s", command_buf
.buf
);
3003 die("Garbage after path in: %s", command_buf
.buf
);
3006 tree_content_get(root
, p
, &leaf
, 1);
3008 * A directory in preparation would have a sha1 of zero
3009 * until it is saved. Save, for simplicity.
3011 if (S_ISDIR(leaf
.versions
[1].mode
))
3014 print_ls(leaf
.versions
[1].mode
, leaf
.versions
[1].oid
.hash
, p
);
3016 release_tree_content_recursive(leaf
.tree
);
3017 if (!b
|| root
!= &b
->branch_tree
)
3018 release_tree_entry(root
);
3021 static void checkpoint(void)
3023 checkpoint_requested
= 0;
3032 static void parse_checkpoint(void)
3034 checkpoint_requested
= 1;
3038 static void parse_progress(void)
3040 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
3041 fputc('\n', stdout
);
3046 static char* make_fast_import_path(const char *path
)
3048 if (!relative_marks_paths
|| is_absolute_path(path
))
3049 return xstrdup(path
);
3050 return git_pathdup("info/fast-import/%s", path
);
3053 static void option_import_marks(const char *marks
,
3054 int from_stream
, int ignore_missing
)
3056 if (import_marks_file
) {
3058 die("Only one import-marks command allowed per stream");
3060 /* read previous mark file */
3061 if(!import_marks_file_from_stream
)
3065 import_marks_file
= make_fast_import_path(marks
);
3066 import_marks_file_from_stream
= from_stream
;
3067 import_marks_file_ignore_missing
= ignore_missing
;
3070 static void option_date_format(const char *fmt
)
3072 if (!strcmp(fmt
, "raw"))
3073 whenspec
= WHENSPEC_RAW
;
3074 else if (!strcmp(fmt
, "rfc2822"))
3075 whenspec
= WHENSPEC_RFC2822
;
3076 else if (!strcmp(fmt
, "now"))
3077 whenspec
= WHENSPEC_NOW
;
3079 die("unknown --date-format argument %s", fmt
);
3082 static unsigned long ulong_arg(const char *option
, const char *arg
)
3085 unsigned long rv
= strtoul(arg
, &endptr
, 0);
3086 if (strchr(arg
, '-') || endptr
== arg
|| *endptr
)
3087 die("%s: argument must be a non-negative integer", option
);
3091 static void option_depth(const char *depth
)
3093 max_depth
= ulong_arg("--depth", depth
);
3094 if (max_depth
> MAX_DEPTH
)
3095 die("--depth cannot exceed %u", MAX_DEPTH
);
3098 static void option_active_branches(const char *branches
)
3100 max_active_branches
= ulong_arg("--active-branches", branches
);
3103 static void option_export_marks(const char *marks
)
3105 export_marks_file
= make_fast_import_path(marks
);
3108 static void option_cat_blob_fd(const char *fd
)
3110 unsigned long n
= ulong_arg("--cat-blob-fd", fd
);
3111 if (n
> (unsigned long) INT_MAX
)
3112 die("--cat-blob-fd cannot exceed %d", INT_MAX
);
3113 cat_blob_fd
= (int) n
;
3116 static void option_export_pack_edges(const char *edges
)
3120 pack_edges
= xfopen(edges
, "a");
3123 static int parse_one_option(const char *option
)
3125 if (skip_prefix(option
, "max-pack-size=", &option
)) {
3127 if (!git_parse_ulong(option
, &v
))
3130 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v
);
3132 } else if (v
< 1024 * 1024) {
3133 warning("minimum max-pack-size is 1 MiB");
3137 } else if (skip_prefix(option
, "big-file-threshold=", &option
)) {
3139 if (!git_parse_ulong(option
, &v
))
3141 big_file_threshold
= v
;
3142 } else if (skip_prefix(option
, "depth=", &option
)) {
3143 option_depth(option
);
3144 } else if (skip_prefix(option
, "active-branches=", &option
)) {
3145 option_active_branches(option
);
3146 } else if (skip_prefix(option
, "export-pack-edges=", &option
)) {
3147 option_export_pack_edges(option
);
3148 } else if (!strcmp(option
, "quiet")) {
3150 } else if (!strcmp(option
, "stats")) {
3152 } else if (!strcmp(option
, "allow-unsafe-features")) {
3153 ; /* already handled during early option parsing */
3161 static void check_unsafe_feature(const char *feature
, int from_stream
)
3163 if (from_stream
&& !allow_unsafe_features
)
3164 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3168 static int parse_one_feature(const char *feature
, int from_stream
)
3172 if (skip_prefix(feature
, "date-format=", &arg
)) {
3173 option_date_format(arg
);
3174 } else if (skip_prefix(feature
, "import-marks=", &arg
)) {
3175 check_unsafe_feature("import-marks", from_stream
);
3176 option_import_marks(arg
, from_stream
, 0);
3177 } else if (skip_prefix(feature
, "import-marks-if-exists=", &arg
)) {
3178 check_unsafe_feature("import-marks-if-exists", from_stream
);
3179 option_import_marks(arg
, from_stream
, 1);
3180 } else if (skip_prefix(feature
, "export-marks=", &arg
)) {
3181 check_unsafe_feature(feature
, from_stream
);
3182 option_export_marks(arg
);
3183 } else if (!strcmp(feature
, "get-mark")) {
3184 ; /* Don't die - this feature is supported */
3185 } else if (!strcmp(feature
, "cat-blob")) {
3186 ; /* Don't die - this feature is supported */
3187 } else if (!strcmp(feature
, "relative-marks")) {
3188 relative_marks_paths
= 1;
3189 } else if (!strcmp(feature
, "no-relative-marks")) {
3190 relative_marks_paths
= 0;
3191 } else if (!strcmp(feature
, "done")) {
3192 require_explicit_termination
= 1;
3193 } else if (!strcmp(feature
, "force")) {
3195 } else if (!strcmp(feature
, "notes") || !strcmp(feature
, "ls")) {
3196 ; /* do nothing; we have the feature */
3204 static void parse_feature(const char *feature
)
3206 if (seen_data_command
)
3207 die("Got feature command '%s' after data command", feature
);
3209 if (parse_one_feature(feature
, 1))
3212 die("This version of fast-import does not support feature %s.", feature
);
3215 static void parse_option(const char *option
)
3217 if (seen_data_command
)
3218 die("Got option command '%s' after data command", option
);
3220 if (parse_one_option(option
))
3223 die("This version of fast-import does not support option: %s", option
);
3226 static void git_pack_config(void)
3228 int indexversion_value
;
3230 unsigned long packsizelimit_value
;
3232 if (!git_config_get_ulong("pack.depth", &max_depth
)) {
3233 if (max_depth
> MAX_DEPTH
)
3234 max_depth
= MAX_DEPTH
;
3236 if (!git_config_get_int("pack.indexversion", &indexversion_value
)) {
3237 pack_idx_opts
.version
= indexversion_value
;
3238 if (pack_idx_opts
.version
> 2)
3239 git_die_config("pack.indexversion",
3240 "bad pack.indexversion=%"PRIu32
, pack_idx_opts
.version
);
3242 if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value
))
3243 max_packsize
= packsizelimit_value
;
3245 if (!git_config_get_int("fastimport.unpacklimit", &limit
))
3246 unpack_limit
= limit
;
3247 else if (!git_config_get_int("transfer.unpacklimit", &limit
))
3248 unpack_limit
= limit
;
3250 git_config(git_default_config
, NULL
);
3253 static const char fast_import_usage
[] =
3254 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3256 static void parse_argv(void)
3260 for (i
= 1; i
< global_argc
; i
++) {
3261 const char *a
= global_argv
[i
];
3263 if (*a
!= '-' || !strcmp(a
, "--"))
3266 if (!skip_prefix(a
, "--", &a
))
3267 die("unknown option %s", a
);
3269 if (parse_one_option(a
))
3272 if (parse_one_feature(a
, 0))
3275 if (skip_prefix(a
, "cat-blob-fd=", &a
)) {
3276 option_cat_blob_fd(a
);
3280 die("unknown option --%s", a
);
3282 if (i
!= global_argc
)
3283 usage(fast_import_usage
);
3285 seen_data_command
= 1;
3286 if (import_marks_file
)
3290 int cmd_main(int argc
, const char **argv
)
3294 if (argc
== 2 && !strcmp(argv
[1], "-h"))
3295 usage(fast_import_usage
);
3297 setup_git_directory();
3298 reset_pack_idx_option(&pack_idx_opts
);
3301 alloc_objects(object_entry_alloc
);
3302 strbuf_init(&command_buf
, 0);
3303 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
3304 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
3305 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
3306 marks
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
3309 * We don't parse most options until after we've seen the set of
3310 * "feature" lines at the start of the stream (which allows the command
3311 * line to override stream data). But we must do an early parse of any
3312 * command-line options that impact how we interpret the feature lines.
3314 for (i
= 1; i
< argc
; i
++) {
3315 const char *arg
= argv
[i
];
3316 if (*arg
!= '-' || !strcmp(arg
, "--"))
3318 if (!strcmp(arg
, "--allow-unsafe-features"))
3319 allow_unsafe_features
= 1;
3325 rc_free
= mem_pool_alloc(&fi_mem_pool
, cmd_save
* sizeof(*rc_free
));
3326 for (i
= 0; i
< (cmd_save
- 1); i
++)
3327 rc_free
[i
].next
= &rc_free
[i
+ 1];
3328 rc_free
[cmd_save
- 1].next
= NULL
;
3331 set_die_routine(die_nicely
);
3332 set_checkpoint_signal();
3333 while (read_next_command() != EOF
) {
3335 if (!strcmp("blob", command_buf
.buf
))
3337 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
3339 else if (skip_prefix(command_buf
.buf
, "commit ", &v
))
3340 parse_new_commit(v
);
3341 else if (skip_prefix(command_buf
.buf
, "tag ", &v
))
3343 else if (skip_prefix(command_buf
.buf
, "reset ", &v
))
3344 parse_reset_branch(v
);
3345 else if (!strcmp("checkpoint", command_buf
.buf
))
3347 else if (!strcmp("done", command_buf
.buf
))
3349 else if (starts_with(command_buf
.buf
, "progress "))
3351 else if (skip_prefix(command_buf
.buf
, "feature ", &v
))
3353 else if (skip_prefix(command_buf
.buf
, "option git ", &v
))
3355 else if (starts_with(command_buf
.buf
, "option "))
3356 /* ignore non-git options*/;
3358 die("Unsupported command: %s", command_buf
.buf
);
3360 if (checkpoint_requested
)
3364 /* argv hasn't been parsed yet, do so */
3365 if (!seen_data_command
)
3368 if (require_explicit_termination
&& feof(stdin
))
3369 die("stream ends early");
3382 uintmax_t total_count
= 0, duplicate_count
= 0;
3383 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3384 total_count
+= object_count_by_type
[i
];
3385 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3386 duplicate_count
+= duplicate_count_by_type
[i
];
3388 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3389 fprintf(stderr
, "---------------------------------------------------------------------\n");
3390 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3391 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3392 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
]);
3393 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
]);
3394 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
]);
3395 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
]);
3396 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3397 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3398 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3399 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (tree_entry_allocd
+ fi_mem_pool
.pool_alloc
+ alloc_count
*sizeof(struct object_entry
))/1024);
3400 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd
+ fi_mem_pool
.pool_alloc
) /1024));
3401 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3402 fprintf(stderr
, "---------------------------------------------------------------------\n");
3404 fprintf(stderr
, "---------------------------------------------------------------------\n");
3405 fprintf(stderr
, "\n");
3408 return failure
? 1 : 0;