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 typedef void (*mark_set_inserter_t
)(struct mark_set
*s
, struct object_id
*oid
, uintmax_t mark
);
136 /* Configured limits on output */
137 static unsigned long max_depth
= 50;
138 static off_t max_packsize
;
139 static int unpack_limit
= 100;
140 static int force_update
;
142 /* Stats and misc. counters */
143 static uintmax_t alloc_count
;
144 static uintmax_t marks_set_count
;
145 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
146 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
147 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
148 static uintmax_t delta_count_attempts_by_type
[1 << TYPE_BITS
];
149 static unsigned long object_count
;
150 static unsigned long branch_count
;
151 static unsigned long branch_load_count
;
153 static FILE *pack_edges
;
154 static unsigned int show_stats
= 1;
155 static int global_argc
;
156 static const char **global_argv
;
159 static struct mem_pool fi_mem_pool
= {NULL
, 2*1024*1024 -
160 sizeof(struct mp_block
), 0 };
162 /* Atom management */
163 static unsigned int atom_table_sz
= 4451;
164 static unsigned int atom_cnt
;
165 static struct atom_str
**atom_table
;
167 /* The .pack file being generated */
168 static struct pack_idx_option pack_idx_opts
;
169 static unsigned int pack_id
;
170 static struct hashfile
*pack_file
;
171 static struct packed_git
*pack_data
;
172 static struct packed_git
**all_packs
;
173 static off_t pack_size
;
175 /* Table of objects we've written. */
176 static unsigned int object_entry_alloc
= 5000;
177 static struct object_entry_pool
*blocks
;
178 static struct object_entry
*object_table
[1 << 16];
179 static struct mark_set
*marks
;
180 static const char *export_marks_file
;
181 static const char *import_marks_file
;
182 static int import_marks_file_from_stream
;
183 static int import_marks_file_ignore_missing
;
184 static int import_marks_file_done
;
185 static int relative_marks_paths
;
188 static struct last_object last_blob
= { STRBUF_INIT
, 0, 0, 0 };
190 /* Tree management */
191 static unsigned int tree_entry_alloc
= 1000;
192 static void *avail_tree_entry
;
193 static unsigned int avail_tree_table_sz
= 100;
194 static struct avail_tree_content
**avail_tree_table
;
195 static size_t tree_entry_allocd
;
196 static struct strbuf old_tree
= STRBUF_INIT
;
197 static struct strbuf new_tree
= STRBUF_INIT
;
200 static unsigned long max_active_branches
= 5;
201 static unsigned long cur_active_branches
;
202 static unsigned long branch_table_sz
= 1039;
203 static struct branch
**branch_table
;
204 static struct branch
*active_branches
;
207 static struct tag
*first_tag
;
208 static struct tag
*last_tag
;
210 /* Input stream parsing */
211 static whenspec_type whenspec
= WHENSPEC_RAW
;
212 static struct strbuf command_buf
= STRBUF_INIT
;
213 static int unread_command_buf
;
214 static struct recent_command cmd_hist
= {&cmd_hist
, &cmd_hist
, NULL
};
215 static struct recent_command
*cmd_tail
= &cmd_hist
;
216 static struct recent_command
*rc_free
;
217 static unsigned int cmd_save
= 100;
218 static uintmax_t next_mark
;
219 static struct strbuf new_data
= STRBUF_INIT
;
220 static int seen_data_command
;
221 static int require_explicit_termination
;
222 static int allow_unsafe_features
;
224 /* Signal handling */
225 static volatile sig_atomic_t checkpoint_requested
;
227 /* Where to write output of cat-blob commands */
228 static int cat_blob_fd
= STDOUT_FILENO
;
230 static void parse_argv(void);
231 static void parse_get_mark(const char *p
);
232 static void parse_cat_blob(const char *p
);
233 static void parse_ls(const char *p
, struct branch
*b
);
235 static void write_branch_report(FILE *rpt
, struct branch
*b
)
237 fprintf(rpt
, "%s:\n", b
->name
);
239 fprintf(rpt
, " status :");
241 fputs(" active", rpt
);
242 if (b
->branch_tree
.tree
)
243 fputs(" loaded", rpt
);
244 if (is_null_oid(&b
->branch_tree
.versions
[1].oid
))
245 fputs(" dirty", rpt
);
248 fprintf(rpt
, " tip commit : %s\n", oid_to_hex(&b
->oid
));
249 fprintf(rpt
, " old tree : %s\n",
250 oid_to_hex(&b
->branch_tree
.versions
[0].oid
));
251 fprintf(rpt
, " cur tree : %s\n",
252 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
253 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
255 fputs(" last pack : ", rpt
);
256 if (b
->pack_id
< MAX_PACK_ID
)
257 fprintf(rpt
, "%u", b
->pack_id
);
263 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set
*);
265 static void write_crash_report(const char *err
)
267 char *loc
= git_pathdup("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
268 FILE *rpt
= fopen(loc
, "w");
271 struct recent_command
*rc
;
274 error_errno("can't write crash report %s", loc
);
279 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
281 fprintf(rpt
, "fast-import crash report:\n");
282 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
283 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
284 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_MODE(ISO8601
)));
287 fputs("fatal: ", rpt
);
292 fputs("Most Recent Commands Before Crash\n", rpt
);
293 fputs("---------------------------------\n", rpt
);
294 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
295 if (rc
->next
== &cmd_hist
)
304 fputs("Active Branch LRU\n", rpt
);
305 fputs("-----------------\n", rpt
);
306 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
308 max_active_branches
);
310 fputs(" pos clock name\n", rpt
);
311 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
312 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
313 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
314 ++lu
, b
->last_commit
, b
->name
);
317 fputs("Inactive Branches\n", rpt
);
318 fputs("-----------------\n", rpt
);
319 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
320 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
321 write_branch_report(rpt
, b
);
327 fputs("Annotated Tags\n", rpt
);
328 fputs("--------------\n", rpt
);
329 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
330 fputs(oid_to_hex(&tg
->oid
), rpt
);
332 fputs(tg
->name
, rpt
);
338 fputs("Marks\n", rpt
);
339 fputs("-----\n", rpt
);
340 if (export_marks_file
)
341 fprintf(rpt
, " exported to %s\n", export_marks_file
);
343 dump_marks_helper(rpt
, 0, marks
);
346 fputs("-------------------\n", rpt
);
347 fputs("END OF CRASH REPORT\n", rpt
);
352 static void end_packfile(void);
353 static void unkeep_all_packs(void);
354 static void dump_marks(void);
356 static NORETURN
void die_nicely(const char *err
, va_list params
)
359 char message
[2 * PATH_MAX
];
361 vsnprintf(message
, sizeof(message
), err
, params
);
362 fputs("fatal: ", stderr
);
363 fputs(message
, stderr
);
368 write_crash_report(message
);
376 #ifndef SIGUSR1 /* Windows, for example */
378 static void set_checkpoint_signal(void)
384 static void checkpoint_signal(int signo
)
386 checkpoint_requested
= 1;
389 static void set_checkpoint_signal(void)
393 memset(&sa
, 0, sizeof(sa
));
394 sa
.sa_handler
= checkpoint_signal
;
395 sigemptyset(&sa
.sa_mask
);
396 sa
.sa_flags
= SA_RESTART
;
397 sigaction(SIGUSR1
, &sa
, NULL
);
402 static void alloc_objects(unsigned int cnt
)
404 struct object_entry_pool
*b
;
406 b
= xmalloc(sizeof(struct object_entry_pool
)
407 + cnt
* sizeof(struct object_entry
));
408 b
->next_pool
= blocks
;
409 b
->next_free
= b
->entries
;
410 b
->end
= b
->entries
+ cnt
;
415 static struct object_entry
*new_object(struct object_id
*oid
)
417 struct object_entry
*e
;
419 if (blocks
->next_free
== blocks
->end
)
420 alloc_objects(object_entry_alloc
);
422 e
= blocks
->next_free
++;
423 oidcpy(&e
->idx
.oid
, oid
);
427 static struct object_entry
*find_object(struct object_id
*oid
)
429 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
430 struct object_entry
*e
;
431 for (e
= object_table
[h
]; e
; e
= e
->next
)
432 if (oideq(oid
, &e
->idx
.oid
))
437 static struct object_entry
*insert_object(struct object_id
*oid
)
439 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
440 struct object_entry
*e
= object_table
[h
];
443 if (oideq(oid
, &e
->idx
.oid
))
449 e
->next
= object_table
[h
];
455 static void invalidate_pack_id(unsigned int id
)
461 for (h
= 0; h
< ARRAY_SIZE(object_table
); h
++) {
462 struct object_entry
*e
;
464 for (e
= object_table
[h
]; e
; e
= e
->next
)
465 if (e
->pack_id
== id
)
466 e
->pack_id
= MAX_PACK_ID
;
469 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
472 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
473 if (b
->pack_id
== id
)
474 b
->pack_id
= MAX_PACK_ID
;
477 for (t
= first_tag
; t
; t
= t
->next_tag
)
478 if (t
->pack_id
== id
)
479 t
->pack_id
= MAX_PACK_ID
;
482 static unsigned int hc_str(const char *s
, size_t len
)
490 static char *pool_strdup(const char *s
)
492 size_t len
= strlen(s
) + 1;
493 char *r
= mem_pool_alloc(&fi_mem_pool
, len
);
498 static void insert_mark(struct mark_set
*s
, uintmax_t idnum
, struct object_entry
*oe
)
500 while ((idnum
>> s
->shift
) >= 1024) {
501 s
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
502 s
->shift
= marks
->shift
+ 10;
503 s
->data
.sets
[0] = marks
;
507 uintmax_t i
= idnum
>> s
->shift
;
508 idnum
-= i
<< s
->shift
;
509 if (!s
->data
.sets
[i
]) {
510 s
->data
.sets
[i
] = mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
511 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
515 if (!s
->data
.marked
[idnum
])
517 s
->data
.marked
[idnum
] = oe
;
520 static void *find_mark(struct mark_set
*s
, uintmax_t idnum
)
522 uintmax_t orig_idnum
= idnum
;
523 struct object_entry
*oe
= NULL
;
524 if ((idnum
>> s
->shift
) < 1024) {
525 while (s
&& s
->shift
) {
526 uintmax_t i
= idnum
>> s
->shift
;
527 idnum
-= i
<< s
->shift
;
531 oe
= s
->data
.marked
[idnum
];
534 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
538 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
540 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
543 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
544 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
547 c
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct atom_str
) + len
+ 1);
549 memcpy(c
->str_dat
, s
, len
);
551 c
->next_atom
= atom_table
[hc
];
557 static struct branch
*lookup_branch(const char *name
)
559 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
562 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
563 if (!strcmp(name
, b
->name
))
568 static struct branch
*new_branch(const char *name
)
570 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
571 struct branch
*b
= lookup_branch(name
);
574 die("Invalid attempt to create duplicate branch: %s", name
);
575 if (check_refname_format(name
, REFNAME_ALLOW_ONELEVEL
))
576 die("Branch name doesn't conform to GIT standards: %s", name
);
578 b
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct branch
));
579 b
->name
= pool_strdup(name
);
580 b
->table_next_branch
= branch_table
[hc
];
581 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
582 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
585 b
->pack_id
= MAX_PACK_ID
;
586 branch_table
[hc
] = b
;
591 static unsigned int hc_entries(unsigned int cnt
)
593 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
594 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
597 static struct tree_content
*new_tree_content(unsigned int cnt
)
599 struct avail_tree_content
*f
, *l
= NULL
;
600 struct tree_content
*t
;
601 unsigned int hc
= hc_entries(cnt
);
603 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
604 if (f
->entry_capacity
>= cnt
)
609 l
->next_avail
= f
->next_avail
;
611 avail_tree_table
[hc
] = f
->next_avail
;
613 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
614 f
= mem_pool_alloc(&fi_mem_pool
, sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
615 f
->entry_capacity
= cnt
;
618 t
= (struct tree_content
*)f
;
624 static void release_tree_entry(struct tree_entry
*e
);
625 static void release_tree_content(struct tree_content
*t
)
627 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
628 unsigned int hc
= hc_entries(f
->entry_capacity
);
629 f
->next_avail
= avail_tree_table
[hc
];
630 avail_tree_table
[hc
] = f
;
633 static void release_tree_content_recursive(struct tree_content
*t
)
636 for (i
= 0; i
< t
->entry_count
; i
++)
637 release_tree_entry(t
->entries
[i
]);
638 release_tree_content(t
);
641 static struct tree_content
*grow_tree_content(
642 struct tree_content
*t
,
645 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
646 r
->entry_count
= t
->entry_count
;
647 r
->delta_depth
= t
->delta_depth
;
648 COPY_ARRAY(r
->entries
, t
->entries
, t
->entry_count
);
649 release_tree_content(t
);
653 static struct tree_entry
*new_tree_entry(void)
655 struct tree_entry
*e
;
657 if (!avail_tree_entry
) {
658 unsigned int n
= tree_entry_alloc
;
659 tree_entry_allocd
+= n
* sizeof(struct tree_entry
);
661 avail_tree_entry
= e
;
663 *((void**)e
) = e
+ 1;
669 e
= avail_tree_entry
;
670 avail_tree_entry
= *((void**)e
);
674 static void release_tree_entry(struct tree_entry
*e
)
677 release_tree_content_recursive(e
->tree
);
678 *((void**)e
) = avail_tree_entry
;
679 avail_tree_entry
= e
;
682 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
684 struct tree_content
*d
;
685 struct tree_entry
*a
, *b
;
690 d
= new_tree_content(s
->entry_count
);
691 for (i
= 0; i
< s
->entry_count
; i
++) {
693 b
= new_tree_entry();
694 memcpy(b
, a
, sizeof(*a
));
695 if (a
->tree
&& is_null_oid(&b
->versions
[1].oid
))
696 b
->tree
= dup_tree_content(a
->tree
);
701 d
->entry_count
= s
->entry_count
;
702 d
->delta_depth
= s
->delta_depth
;
707 static void start_packfile(void)
709 struct strbuf tmp_file
= STRBUF_INIT
;
710 struct packed_git
*p
;
711 struct pack_header hdr
;
714 pack_fd
= odb_mkstemp(&tmp_file
, "pack/tmp_pack_XXXXXX");
715 FLEX_ALLOC_STR(p
, pack_name
, tmp_file
.buf
);
716 strbuf_release(&tmp_file
);
718 p
->pack_fd
= pack_fd
;
720 pack_file
= hashfd(pack_fd
, p
->pack_name
);
722 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
723 hdr
.hdr_version
= htonl(2);
725 hashwrite(pack_file
, &hdr
, sizeof(hdr
));
728 pack_size
= sizeof(hdr
);
731 REALLOC_ARRAY(all_packs
, pack_id
+ 1);
732 all_packs
[pack_id
] = p
;
735 static const char *create_index(void)
738 struct pack_idx_entry
**idx
, **c
, **last
;
739 struct object_entry
*e
;
740 struct object_entry_pool
*o
;
742 /* Build the table of object IDs. */
743 ALLOC_ARRAY(idx
, object_count
);
745 for (o
= blocks
; o
; o
= o
->next_pool
)
746 for (e
= o
->next_free
; e
-- != o
->entries
;)
747 if (pack_id
== e
->pack_id
)
749 last
= idx
+ object_count
;
751 die("internal consistency error creating the index");
753 tmpfile
= write_idx_file(NULL
, idx
, object_count
, &pack_idx_opts
,
759 static char *keep_pack(const char *curr_index_name
)
761 static const char *keep_msg
= "fast-import";
762 struct strbuf name
= STRBUF_INIT
;
765 odb_pack_name(&name
, pack_data
->hash
, "keep");
766 keep_fd
= odb_pack_keep(name
.buf
);
768 die_errno("cannot create keep file");
769 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
771 die_errno("failed to write keep file");
773 odb_pack_name(&name
, pack_data
->hash
, "pack");
774 if (finalize_object_file(pack_data
->pack_name
, name
.buf
))
775 die("cannot store pack file");
777 odb_pack_name(&name
, pack_data
->hash
, "idx");
778 if (finalize_object_file(curr_index_name
, name
.buf
))
779 die("cannot store index file");
780 free((void *)curr_index_name
);
781 return strbuf_detach(&name
, NULL
);
784 static void unkeep_all_packs(void)
786 struct strbuf name
= STRBUF_INIT
;
789 for (k
= 0; k
< pack_id
; k
++) {
790 struct packed_git
*p
= all_packs
[k
];
791 odb_pack_name(&name
, p
->hash
, "keep");
792 unlink_or_warn(name
.buf
);
794 strbuf_release(&name
);
797 static int loosen_small_pack(const struct packed_git
*p
)
799 struct child_process unpack
= CHILD_PROCESS_INIT
;
801 if (lseek(p
->pack_fd
, 0, SEEK_SET
) < 0)
802 die_errno("Failed seeking to start of '%s'", p
->pack_name
);
804 unpack
.in
= p
->pack_fd
;
806 unpack
.stdout_to_stderr
= 1;
807 argv_array_push(&unpack
.args
, "unpack-objects");
809 argv_array_push(&unpack
.args
, "-q");
811 return run_command(&unpack
);
814 static void end_packfile(void)
818 if (running
|| !pack_data
)
822 clear_delta_base_cache();
824 struct packed_git
*new_p
;
825 struct object_id cur_pack_oid
;
831 close_pack_windows(pack_data
);
832 finalize_hashfile(pack_file
, cur_pack_oid
.hash
, 0);
833 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->hash
,
834 pack_data
->pack_name
, object_count
,
835 cur_pack_oid
.hash
, pack_size
);
837 if (object_count
<= unpack_limit
) {
838 if (!loosen_small_pack(pack_data
)) {
839 invalidate_pack_id(pack_id
);
844 close(pack_data
->pack_fd
);
845 idx_name
= keep_pack(create_index());
847 /* Register the packfile with core git's machinery. */
848 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
850 die("core git rejected index %s", idx_name
);
851 all_packs
[pack_id
] = new_p
;
852 install_packed_git(the_repository
, new_p
);
855 /* Print the boundary */
857 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
858 for (i
= 0; i
< branch_table_sz
; i
++) {
859 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
860 if (b
->pack_id
== pack_id
)
861 fprintf(pack_edges
, " %s",
862 oid_to_hex(&b
->oid
));
865 for (t
= first_tag
; t
; t
= t
->next_tag
) {
866 if (t
->pack_id
== pack_id
)
867 fprintf(pack_edges
, " %s",
868 oid_to_hex(&t
->oid
));
870 fputc('\n', pack_edges
);
878 close(pack_data
->pack_fd
);
879 unlink_or_warn(pack_data
->pack_name
);
881 FREE_AND_NULL(pack_data
);
884 /* We can't carry a delta across packfiles. */
885 strbuf_release(&last_blob
.data
);
886 last_blob
.offset
= 0;
890 static void cycle_packfile(void)
896 static int store_object(
897 enum object_type type
,
899 struct last_object
*last
,
900 struct object_id
*oidout
,
904 struct object_entry
*e
;
905 unsigned char hdr
[96];
906 struct object_id oid
;
907 unsigned long hdrlen
, deltalen
;
911 hdrlen
= xsnprintf((char *)hdr
, sizeof(hdr
), "%s %lu",
912 type_name(type
), (unsigned long)dat
->len
) + 1;
913 the_hash_algo
->init_fn(&c
);
914 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
915 the_hash_algo
->update_fn(&c
, dat
->buf
, dat
->len
);
916 the_hash_algo
->final_fn(oid
.hash
, &c
);
918 oidcpy(oidout
, &oid
);
920 e
= insert_object(&oid
);
922 insert_mark(marks
, mark
, e
);
924 duplicate_count_by_type
[type
]++;
926 } else if (find_sha1_pack(oid
.hash
,
927 get_all_packs(the_repository
))) {
929 e
->pack_id
= MAX_PACK_ID
;
930 e
->idx
.offset
= 1; /* just not zero! */
931 duplicate_count_by_type
[type
]++;
935 if (last
&& last
->data
.len
&& last
->data
.buf
&& last
->depth
< max_depth
936 && dat
->len
> the_hash_algo
->rawsz
) {
938 delta_count_attempts_by_type
[type
]++;
939 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
941 &deltalen
, dat
->len
- the_hash_algo
->rawsz
);
945 git_deflate_init(&s
, pack_compression_level
);
948 s
.avail_in
= deltalen
;
950 s
.next_in
= (void *)dat
->buf
;
951 s
.avail_in
= dat
->len
;
953 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
954 s
.next_out
= out
= xmalloc(s
.avail_out
);
955 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
959 /* Determine if we should auto-checkpoint. */
961 && (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) > max_packsize
)
962 || (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) < pack_size
) {
964 /* This new object needs to *not* have the current pack_id. */
965 e
->pack_id
= pack_id
+ 1;
968 /* We cannot carry a delta into the new pack. */
970 FREE_AND_NULL(delta
);
972 git_deflate_init(&s
, pack_compression_level
);
973 s
.next_in
= (void *)dat
->buf
;
974 s
.avail_in
= dat
->len
;
975 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
976 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
977 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
984 e
->pack_id
= pack_id
;
985 e
->idx
.offset
= pack_size
;
987 object_count_by_type
[type
]++;
989 crc32_begin(pack_file
);
992 off_t ofs
= e
->idx
.offset
- last
->offset
;
993 unsigned pos
= sizeof(hdr
) - 1;
995 delta_count_by_type
[type
]++;
996 e
->depth
= last
->depth
+ 1;
998 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
999 OBJ_OFS_DELTA
, deltalen
);
1000 hashwrite(pack_file
, hdr
, hdrlen
);
1001 pack_size
+= hdrlen
;
1003 hdr
[pos
] = ofs
& 127;
1005 hdr
[--pos
] = 128 | (--ofs
& 127);
1006 hashwrite(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
1007 pack_size
+= sizeof(hdr
) - pos
;
1010 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1012 hashwrite(pack_file
, hdr
, hdrlen
);
1013 pack_size
+= hdrlen
;
1016 hashwrite(pack_file
, out
, s
.total_out
);
1017 pack_size
+= s
.total_out
;
1019 e
->idx
.crc32
= crc32_end(pack_file
);
1024 if (last
->no_swap
) {
1027 strbuf_swap(&last
->data
, dat
);
1029 last
->offset
= e
->idx
.offset
;
1030 last
->depth
= e
->depth
;
1035 static void truncate_pack(struct hashfile_checkpoint
*checkpoint
)
1037 if (hashfile_truncate(pack_file
, checkpoint
))
1038 die_errno("cannot truncate pack to skip duplicate");
1039 pack_size
= checkpoint
->offset
;
1042 static void stream_blob(uintmax_t len
, struct object_id
*oidout
, uintmax_t mark
)
1044 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1045 unsigned char *in_buf
= xmalloc(in_sz
);
1046 unsigned char *out_buf
= xmalloc(out_sz
);
1047 struct object_entry
*e
;
1048 struct object_id oid
;
1049 unsigned long hdrlen
;
1053 struct hashfile_checkpoint checkpoint
;
1056 /* Determine if we should auto-checkpoint. */
1058 && (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) > max_packsize
)
1059 || (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) < pack_size
)
1062 hashfile_checkpoint(pack_file
, &checkpoint
);
1063 offset
= checkpoint
.offset
;
1065 hdrlen
= xsnprintf((char *)out_buf
, out_sz
, "blob %" PRIuMAX
, len
) + 1;
1067 the_hash_algo
->init_fn(&c
);
1068 the_hash_algo
->update_fn(&c
, out_buf
, hdrlen
);
1070 crc32_begin(pack_file
);
1072 git_deflate_init(&s
, pack_compression_level
);
1074 hdrlen
= encode_in_pack_object_header(out_buf
, out_sz
, OBJ_BLOB
, len
);
1076 s
.next_out
= out_buf
+ hdrlen
;
1077 s
.avail_out
= out_sz
- hdrlen
;
1079 while (status
!= Z_STREAM_END
) {
1080 if (0 < len
&& !s
.avail_in
) {
1081 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1082 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1083 if (!n
&& feof(stdin
))
1084 die("EOF in data (%" PRIuMAX
" bytes remaining)", len
);
1086 the_hash_algo
->update_fn(&c
, in_buf
, n
);
1092 status
= git_deflate(&s
, len
? 0 : Z_FINISH
);
1094 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1095 size_t n
= s
.next_out
- out_buf
;
1096 hashwrite(pack_file
, out_buf
, n
);
1098 s
.next_out
= out_buf
;
1099 s
.avail_out
= out_sz
;
1108 die("unexpected deflate failure: %d", status
);
1111 git_deflate_end(&s
);
1112 the_hash_algo
->final_fn(oid
.hash
, &c
);
1115 oidcpy(oidout
, &oid
);
1117 e
= insert_object(&oid
);
1120 insert_mark(marks
, mark
, e
);
1122 if (e
->idx
.offset
) {
1123 duplicate_count_by_type
[OBJ_BLOB
]++;
1124 truncate_pack(&checkpoint
);
1126 } else if (find_sha1_pack(oid
.hash
,
1127 get_all_packs(the_repository
))) {
1129 e
->pack_id
= MAX_PACK_ID
;
1130 e
->idx
.offset
= 1; /* just not zero! */
1131 duplicate_count_by_type
[OBJ_BLOB
]++;
1132 truncate_pack(&checkpoint
);
1137 e
->pack_id
= pack_id
;
1138 e
->idx
.offset
= offset
;
1139 e
->idx
.crc32
= crc32_end(pack_file
);
1141 object_count_by_type
[OBJ_BLOB
]++;
1148 /* All calls must be guarded by find_object() or find_mark() to
1149 * ensure the 'struct object_entry' passed was written by this
1150 * process instance. We unpack the entry by the offset, avoiding
1151 * the need for the corresponding .idx file. This unpacking rule
1152 * works because we only use OBJ_REF_DELTA within the packfiles
1153 * created by fast-import.
1155 * oe must not be NULL. Such an oe usually comes from giving
1156 * an unknown SHA-1 to find_object() or an undefined mark to
1157 * find_mark(). Callers must test for this condition and use
1158 * the standard read_sha1_file() when it happens.
1160 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1161 * find_mark(), where the mark was reloaded from an existing marks
1162 * file and is referencing an object that this fast-import process
1163 * instance did not write out to a packfile. Callers must test for
1164 * this condition and use read_sha1_file() instead.
1166 static void *gfi_unpack_entry(
1167 struct object_entry
*oe
,
1168 unsigned long *sizep
)
1170 enum object_type type
;
1171 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1172 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ the_hash_algo
->rawsz
)) {
1173 /* The object is stored in the packfile we are writing to
1174 * and we have modified it since the last time we scanned
1175 * back to read a previously written object. If an old
1176 * window covered [p->pack_size, p->pack_size + rawsz) its
1177 * data is stale and is not valid. Closing all windows
1178 * and updating the packfile length ensures we can read
1179 * the newly written data.
1181 close_pack_windows(p
);
1182 hashflush(pack_file
);
1184 /* We have to offer rawsz bytes additional on the end of
1185 * the packfile as the core unpacker code assumes the
1186 * footer is present at the file end and must promise
1187 * at least rawsz bytes within any window it maps. But
1188 * we don't actually create the footer here.
1190 p
->pack_size
= pack_size
+ the_hash_algo
->rawsz
;
1192 return unpack_entry(the_repository
, p
, oe
->idx
.offset
, &type
, sizep
);
1195 static const char *get_mode(const char *str
, uint16_t *modep
)
1200 while ((c
= *str
++) != ' ') {
1201 if (c
< '0' || c
> '7')
1203 mode
= (mode
<< 3) + (c
- '0');
1209 static void load_tree(struct tree_entry
*root
)
1211 struct object_id
*oid
= &root
->versions
[1].oid
;
1212 struct object_entry
*myoe
;
1213 struct tree_content
*t
;
1218 root
->tree
= t
= new_tree_content(8);
1219 if (is_null_oid(oid
))
1222 myoe
= find_object(oid
);
1223 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1224 if (myoe
->type
!= OBJ_TREE
)
1225 die("Not a tree: %s", oid_to_hex(oid
));
1226 t
->delta_depth
= myoe
->depth
;
1227 buf
= gfi_unpack_entry(myoe
, &size
);
1229 die("Can't load tree %s", oid_to_hex(oid
));
1231 enum object_type type
;
1232 buf
= read_object_file(oid
, &type
, &size
);
1233 if (!buf
|| type
!= OBJ_TREE
)
1234 die("Can't load tree %s", oid_to_hex(oid
));
1238 while (c
!= (buf
+ size
)) {
1239 struct tree_entry
*e
= new_tree_entry();
1241 if (t
->entry_count
== t
->entry_capacity
)
1242 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1243 t
->entries
[t
->entry_count
++] = e
;
1246 c
= get_mode(c
, &e
->versions
[1].mode
);
1248 die("Corrupt mode in %s", oid_to_hex(oid
));
1249 e
->versions
[0].mode
= e
->versions
[1].mode
;
1250 e
->name
= to_atom(c
, strlen(c
));
1251 c
+= e
->name
->str_len
+ 1;
1252 hashcpy(e
->versions
[0].oid
.hash
, (unsigned char *)c
);
1253 hashcpy(e
->versions
[1].oid
.hash
, (unsigned char *)c
);
1254 c
+= the_hash_algo
->rawsz
;
1259 static int tecmp0 (const void *_a
, const void *_b
)
1261 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1262 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1263 return base_name_compare(
1264 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1265 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1268 static int tecmp1 (const void *_a
, const void *_b
)
1270 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1271 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1272 return base_name_compare(
1273 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1274 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1277 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1283 QSORT(t
->entries
, t
->entry_count
, tecmp0
);
1285 QSORT(t
->entries
, t
->entry_count
, tecmp1
);
1287 for (i
= 0; i
< t
->entry_count
; i
++) {
1288 if (t
->entries
[i
]->versions
[v
].mode
)
1289 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1293 strbuf_grow(b
, maxlen
);
1294 for (i
= 0; i
< t
->entry_count
; i
++) {
1295 struct tree_entry
*e
= t
->entries
[i
];
1296 if (!e
->versions
[v
].mode
)
1298 strbuf_addf(b
, "%o %s%c",
1299 (unsigned int)(e
->versions
[v
].mode
& ~NO_DELTA
),
1300 e
->name
->str_dat
, '\0');
1301 strbuf_add(b
, e
->versions
[v
].oid
.hash
, the_hash_algo
->rawsz
);
1305 static void store_tree(struct tree_entry
*root
)
1307 struct tree_content
*t
;
1308 unsigned int i
, j
, del
;
1309 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1310 struct object_entry
*le
= NULL
;
1312 if (!is_null_oid(&root
->versions
[1].oid
))
1319 for (i
= 0; i
< t
->entry_count
; i
++) {
1320 if (t
->entries
[i
]->tree
)
1321 store_tree(t
->entries
[i
]);
1324 if (!(root
->versions
[0].mode
& NO_DELTA
))
1325 le
= find_object(&root
->versions
[0].oid
);
1326 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1327 mktree(t
, 0, &old_tree
);
1329 lo
.offset
= le
->idx
.offset
;
1330 lo
.depth
= t
->delta_depth
;
1333 mktree(t
, 1, &new_tree
);
1334 store_object(OBJ_TREE
, &new_tree
, &lo
, &root
->versions
[1].oid
, 0);
1336 t
->delta_depth
= lo
.depth
;
1337 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1338 struct tree_entry
*e
= t
->entries
[i
];
1339 if (e
->versions
[1].mode
) {
1340 e
->versions
[0].mode
= e
->versions
[1].mode
;
1341 oidcpy(&e
->versions
[0].oid
, &e
->versions
[1].oid
);
1342 t
->entries
[j
++] = e
;
1344 release_tree_entry(e
);
1348 t
->entry_count
-= del
;
1351 static void tree_content_replace(
1352 struct tree_entry
*root
,
1353 const struct object_id
*oid
,
1354 const uint16_t mode
,
1355 struct tree_content
*newtree
)
1358 die("Root cannot be a non-directory");
1359 oidclr(&root
->versions
[0].oid
);
1360 oidcpy(&root
->versions
[1].oid
, oid
);
1362 release_tree_content_recursive(root
->tree
);
1363 root
->tree
= newtree
;
1366 static int tree_content_set(
1367 struct tree_entry
*root
,
1369 const struct object_id
*oid
,
1370 const uint16_t mode
,
1371 struct tree_content
*subtree
)
1373 struct tree_content
*t
;
1376 struct tree_entry
*e
;
1378 slash1
= strchrnul(p
, '/');
1381 die("Empty path component found in input");
1382 if (!*slash1
&& !S_ISDIR(mode
) && subtree
)
1383 die("Non-directories cannot have subtrees");
1388 for (i
= 0; i
< t
->entry_count
; i
++) {
1390 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1393 && e
->versions
[1].mode
== mode
1394 && oideq(&e
->versions
[1].oid
, oid
))
1396 e
->versions
[1].mode
= mode
;
1397 oidcpy(&e
->versions
[1].oid
, oid
);
1399 release_tree_content_recursive(e
->tree
);
1403 * We need to leave e->versions[0].sha1 alone
1404 * to avoid modifying the preimage tree used
1405 * when writing out the parent directory.
1406 * But after replacing the subdir with a
1407 * completely different one, it's not a good
1408 * delta base any more, and besides, we've
1409 * thrown away the tree entries needed to
1410 * make a delta against it.
1412 * So let's just explicitly disable deltas
1415 if (S_ISDIR(e
->versions
[0].mode
))
1416 e
->versions
[0].mode
|= NO_DELTA
;
1418 oidclr(&root
->versions
[1].oid
);
1421 if (!S_ISDIR(e
->versions
[1].mode
)) {
1422 e
->tree
= new_tree_content(8);
1423 e
->versions
[1].mode
= S_IFDIR
;
1427 if (tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
)) {
1428 oidclr(&root
->versions
[1].oid
);
1435 if (t
->entry_count
== t
->entry_capacity
)
1436 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1437 e
= new_tree_entry();
1438 e
->name
= to_atom(p
, n
);
1439 e
->versions
[0].mode
= 0;
1440 oidclr(&e
->versions
[0].oid
);
1441 t
->entries
[t
->entry_count
++] = e
;
1443 e
->tree
= new_tree_content(8);
1444 e
->versions
[1].mode
= S_IFDIR
;
1445 tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
);
1448 e
->versions
[1].mode
= mode
;
1449 oidcpy(&e
->versions
[1].oid
, oid
);
1451 oidclr(&root
->versions
[1].oid
);
1455 static int tree_content_remove(
1456 struct tree_entry
*root
,
1458 struct tree_entry
*backup_leaf
,
1461 struct tree_content
*t
;
1464 struct tree_entry
*e
;
1466 slash1
= strchrnul(p
, '/');
1472 if (!*p
&& allow_root
) {
1478 for (i
= 0; i
< t
->entry_count
; i
++) {
1480 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1481 if (*slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1483 * If p names a file in some subdirectory, and a
1484 * file or symlink matching the name of the
1485 * parent directory of p exists, then p cannot
1486 * exist and need not be deleted.
1489 if (!*slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1493 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
, 0)) {
1494 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1495 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1496 oidclr(&root
->versions
[1].oid
);
1510 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1512 release_tree_content_recursive(e
->tree
);
1514 e
->versions
[1].mode
= 0;
1515 oidclr(&e
->versions
[1].oid
);
1516 oidclr(&root
->versions
[1].oid
);
1520 static int tree_content_get(
1521 struct tree_entry
*root
,
1523 struct tree_entry
*leaf
,
1526 struct tree_content
*t
;
1529 struct tree_entry
*e
;
1531 slash1
= strchrnul(p
, '/');
1533 if (!n
&& !allow_root
)
1534 die("Empty path component found in input");
1545 for (i
= 0; i
< t
->entry_count
; i
++) {
1547 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1550 if (!S_ISDIR(e
->versions
[1].mode
))
1554 return tree_content_get(e
, slash1
+ 1, leaf
, 0);
1560 memcpy(leaf
, e
, sizeof(*leaf
));
1561 if (e
->tree
&& is_null_oid(&e
->versions
[1].oid
))
1562 leaf
->tree
= dup_tree_content(e
->tree
);
1568 static int update_branch(struct branch
*b
)
1570 static const char *msg
= "fast-import";
1571 struct ref_transaction
*transaction
;
1572 struct object_id old_oid
;
1573 struct strbuf err
= STRBUF_INIT
;
1575 if (is_null_oid(&b
->oid
)) {
1577 delete_ref(NULL
, b
->name
, NULL
, 0);
1580 if (read_ref(b
->name
, &old_oid
))
1582 if (!force_update
&& !is_null_oid(&old_oid
)) {
1583 struct commit
*old_cmit
, *new_cmit
;
1585 old_cmit
= lookup_commit_reference_gently(the_repository
,
1587 new_cmit
= lookup_commit_reference_gently(the_repository
,
1589 if (!old_cmit
|| !new_cmit
)
1590 return error("Branch %s is missing commits.", b
->name
);
1592 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1593 warning("Not updating %s"
1594 " (new tip %s does not contain %s)",
1595 b
->name
, oid_to_hex(&b
->oid
),
1596 oid_to_hex(&old_oid
));
1600 transaction
= ref_transaction_begin(&err
);
1602 ref_transaction_update(transaction
, b
->name
, &b
->oid
, &old_oid
,
1604 ref_transaction_commit(transaction
, &err
)) {
1605 ref_transaction_free(transaction
);
1606 error("%s", err
.buf
);
1607 strbuf_release(&err
);
1610 ref_transaction_free(transaction
);
1611 strbuf_release(&err
);
1615 static void dump_branches(void)
1620 for (i
= 0; i
< branch_table_sz
; i
++) {
1621 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1622 failure
|= update_branch(b
);
1626 static void dump_tags(void)
1628 static const char *msg
= "fast-import";
1630 struct strbuf ref_name
= STRBUF_INIT
;
1631 struct strbuf err
= STRBUF_INIT
;
1632 struct ref_transaction
*transaction
;
1634 transaction
= ref_transaction_begin(&err
);
1636 failure
|= error("%s", err
.buf
);
1639 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1640 strbuf_reset(&ref_name
);
1641 strbuf_addf(&ref_name
, "refs/tags/%s", t
->name
);
1643 if (ref_transaction_update(transaction
, ref_name
.buf
,
1644 &t
->oid
, NULL
, 0, msg
, &err
)) {
1645 failure
|= error("%s", err
.buf
);
1649 if (ref_transaction_commit(transaction
, &err
))
1650 failure
|= error("%s", err
.buf
);
1653 ref_transaction_free(transaction
);
1654 strbuf_release(&ref_name
);
1655 strbuf_release(&err
);
1658 static void dump_marks_helper(FILE *f
,
1664 for (k
= 0; k
< 1024; k
++) {
1665 if (m
->data
.sets
[k
])
1666 dump_marks_helper(f
, base
+ (k
<< m
->shift
),
1670 for (k
= 0; k
< 1024; k
++) {
1671 if (m
->data
.marked
[k
])
1672 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1673 oid_to_hex(&m
->data
.marked
[k
]->idx
.oid
));
1678 static void dump_marks(void)
1680 struct lock_file mark_lock
= LOCK_INIT
;
1683 if (!export_marks_file
|| (import_marks_file
&& !import_marks_file_done
))
1686 if (safe_create_leading_directories_const(export_marks_file
)) {
1687 failure
|= error_errno("unable to create leading directories of %s",
1692 if (hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0) < 0) {
1693 failure
|= error_errno("Unable to write marks file %s",
1698 f
= fdopen_lock_file(&mark_lock
, "w");
1700 int saved_errno
= errno
;
1701 rollback_lock_file(&mark_lock
);
1702 failure
|= error("Unable to write marks file %s: %s",
1703 export_marks_file
, strerror(saved_errno
));
1707 dump_marks_helper(f
, 0, marks
);
1708 if (commit_lock_file(&mark_lock
)) {
1709 failure
|= error_errno("Unable to write file %s",
1715 static void insert_object_entry(struct mark_set
*s
, struct object_id
*oid
, uintmax_t mark
)
1717 struct object_entry
*e
;
1718 e
= find_object(oid
);
1720 enum object_type type
= oid_object_info(the_repository
,
1723 die("object not found: %s", oid_to_hex(oid
));
1724 e
= insert_object(oid
);
1726 e
->pack_id
= MAX_PACK_ID
;
1727 e
->idx
.offset
= 1; /* just not zero! */
1729 insert_mark(s
, mark
, e
);
1732 static void read_mark_file(struct mark_set
*s
, FILE *f
, mark_set_inserter_t inserter
)
1735 while (fgets(line
, sizeof(line
), f
)) {
1738 struct object_id oid
;
1740 end
= strchr(line
, '\n');
1741 if (line
[0] != ':' || !end
)
1742 die("corrupt mark line: %s", line
);
1744 mark
= strtoumax(line
+ 1, &end
, 10);
1745 if (!mark
|| end
== line
+ 1
1746 || *end
!= ' ' || get_oid_hex(end
+ 1, &oid
))
1747 die("corrupt mark line: %s", line
);
1748 inserter(s
, &oid
, mark
);
1752 static void read_marks(void)
1754 FILE *f
= fopen(import_marks_file
, "r");
1757 else if (import_marks_file_ignore_missing
&& errno
== ENOENT
)
1758 goto done
; /* Marks file does not exist */
1760 die_errno("cannot read '%s'", import_marks_file
);
1761 read_mark_file(marks
, f
, insert_object_entry
);
1764 import_marks_file_done
= 1;
1768 static int read_next_command(void)
1770 static int stdin_eof
= 0;
1773 unread_command_buf
= 0;
1778 if (unread_command_buf
) {
1779 unread_command_buf
= 0;
1781 struct recent_command
*rc
;
1783 stdin_eof
= strbuf_getline_lf(&command_buf
, stdin
);
1787 if (!seen_data_command
1788 && !starts_with(command_buf
.buf
, "feature ")
1789 && !starts_with(command_buf
.buf
, "option ")) {
1798 cmd_hist
.next
= rc
->next
;
1799 cmd_hist
.next
->prev
= &cmd_hist
;
1803 rc
->buf
= xstrdup(command_buf
.buf
);
1804 rc
->prev
= cmd_tail
;
1805 rc
->next
= cmd_hist
.prev
;
1806 rc
->prev
->next
= rc
;
1809 if (command_buf
.buf
[0] == '#')
1815 static void skip_optional_lf(void)
1817 int term_char
= fgetc(stdin
);
1818 if (term_char
!= '\n' && term_char
!= EOF
)
1819 ungetc(term_char
, stdin
);
1822 static void parse_mark(void)
1825 if (skip_prefix(command_buf
.buf
, "mark :", &v
)) {
1826 next_mark
= strtoumax(v
, NULL
, 10);
1827 read_next_command();
1833 static void parse_original_identifier(void)
1836 if (skip_prefix(command_buf
.buf
, "original-oid ", &v
))
1837 read_next_command();
1840 static int parse_data(struct strbuf
*sb
, uintmax_t limit
, uintmax_t *len_res
)
1845 if (!skip_prefix(command_buf
.buf
, "data ", &data
))
1846 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1848 if (skip_prefix(data
, "<<", &data
)) {
1849 char *term
= xstrdup(data
);
1850 size_t term_len
= command_buf
.len
- (data
- command_buf
.buf
);
1853 if (strbuf_getline_lf(&command_buf
, stdin
) == EOF
)
1854 die("EOF in data (terminator '%s' not found)", term
);
1855 if (term_len
== command_buf
.len
1856 && !strcmp(term
, command_buf
.buf
))
1858 strbuf_addbuf(sb
, &command_buf
);
1859 strbuf_addch(sb
, '\n');
1864 uintmax_t len
= strtoumax(data
, NULL
, 10);
1865 size_t n
= 0, length
= (size_t)len
;
1867 if (limit
&& limit
< len
) {
1872 die("data is too large to use in this context");
1874 while (n
< length
) {
1875 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1876 if (!s
&& feof(stdin
))
1877 die("EOF in data (%lu bytes remaining)",
1878 (unsigned long)(length
- n
));
1887 static int validate_raw_date(const char *src
, struct strbuf
*result
)
1889 const char *orig_src
= src
;
1895 num
= strtoul(src
, &endp
, 10);
1896 /* NEEDSWORK: perhaps check for reasonable values? */
1897 if (errno
|| endp
== src
|| *endp
!= ' ')
1901 if (*src
!= '-' && *src
!= '+')
1904 num
= strtoul(src
+ 1, &endp
, 10);
1905 if (errno
|| endp
== src
+ 1 || *endp
|| 1400 < num
)
1908 strbuf_addstr(result
, orig_src
);
1912 static char *parse_ident(const char *buf
)
1916 struct strbuf ident
= STRBUF_INIT
;
1918 /* ensure there is a space delimiter even if there is no name */
1922 ltgt
= buf
+ strcspn(buf
, "<>");
1924 die("Missing < in ident string: %s", buf
);
1925 if (ltgt
!= buf
&& ltgt
[-1] != ' ')
1926 die("Missing space before < in ident string: %s", buf
);
1927 ltgt
= ltgt
+ 1 + strcspn(ltgt
+ 1, "<>");
1929 die("Missing > in ident string: %s", buf
);
1932 die("Missing space after > in ident string: %s", buf
);
1934 name_len
= ltgt
- buf
;
1935 strbuf_add(&ident
, buf
, name_len
);
1939 if (validate_raw_date(ltgt
, &ident
) < 0)
1940 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
1942 case WHENSPEC_RFC2822
:
1943 if (parse_date(ltgt
, &ident
) < 0)
1944 die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt
, buf
);
1947 if (strcmp("now", ltgt
))
1948 die("Date in ident must be 'now': %s", buf
);
1953 return strbuf_detach(&ident
, NULL
);
1956 static void parse_and_store_blob(
1957 struct last_object
*last
,
1958 struct object_id
*oidout
,
1961 static struct strbuf buf
= STRBUF_INIT
;
1964 if (parse_data(&buf
, big_file_threshold
, &len
))
1965 store_object(OBJ_BLOB
, &buf
, last
, oidout
, mark
);
1968 strbuf_release(&last
->data
);
1972 stream_blob(len
, oidout
, mark
);
1977 static void parse_new_blob(void)
1979 read_next_command();
1981 parse_original_identifier();
1982 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
1985 static void unload_one_branch(void)
1987 while (cur_active_branches
1988 && cur_active_branches
>= max_active_branches
) {
1989 uintmax_t min_commit
= ULONG_MAX
;
1990 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1992 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1993 if (e
->last_commit
< min_commit
) {
1995 min_commit
= e
->last_commit
;
2001 e
= p
->active_next_branch
;
2002 p
->active_next_branch
= e
->active_next_branch
;
2004 e
= active_branches
;
2005 active_branches
= e
->active_next_branch
;
2008 e
->active_next_branch
= NULL
;
2009 if (e
->branch_tree
.tree
) {
2010 release_tree_content_recursive(e
->branch_tree
.tree
);
2011 e
->branch_tree
.tree
= NULL
;
2013 cur_active_branches
--;
2017 static void load_branch(struct branch
*b
)
2019 load_tree(&b
->branch_tree
);
2022 b
->active_next_branch
= active_branches
;
2023 active_branches
= b
;
2024 cur_active_branches
++;
2025 branch_load_count
++;
2029 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2031 unsigned char fanout
= 0;
2032 while ((num_notes
>>= 8))
2037 static void construct_path_with_fanout(const char *hex_sha1
,
2038 unsigned char fanout
, char *path
)
2040 unsigned int i
= 0, j
= 0;
2041 if (fanout
>= the_hash_algo
->rawsz
)
2042 die("Too large fanout (%u)", fanout
);
2044 path
[i
++] = hex_sha1
[j
++];
2045 path
[i
++] = hex_sha1
[j
++];
2049 memcpy(path
+ i
, hex_sha1
+ j
, the_hash_algo
->hexsz
- j
);
2050 path
[i
+ the_hash_algo
->hexsz
- j
] = '\0';
2053 static uintmax_t do_change_note_fanout(
2054 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2055 char *hex_oid
, unsigned int hex_oid_len
,
2056 char *fullpath
, unsigned int fullpath_len
,
2057 unsigned char fanout
)
2059 struct tree_content
*t
;
2060 struct tree_entry
*e
, leaf
;
2061 unsigned int i
, tmp_hex_oid_len
, tmp_fullpath_len
;
2062 uintmax_t num_notes
= 0;
2063 struct object_id oid
;
2064 /* hex oid + '/' between each pair of hex digits + NUL */
2065 char realpath
[GIT_MAX_HEXSZ
+ ((GIT_MAX_HEXSZ
/ 2) - 1) + 1];
2066 const unsigned hexsz
= the_hash_algo
->hexsz
;
2072 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2074 tmp_hex_oid_len
= hex_oid_len
+ e
->name
->str_len
;
2075 tmp_fullpath_len
= fullpath_len
;
2078 * We're interested in EITHER existing note entries (entries
2079 * with exactly 40 hex chars in path, not including directory
2080 * separators), OR directory entries that may contain note
2081 * entries (with < 40 hex chars in path).
2082 * Also, each path component in a note entry must be a multiple
2085 if (!e
->versions
[1].mode
||
2086 tmp_hex_oid_len
> hexsz
||
2087 e
->name
->str_len
% 2)
2090 /* This _may_ be a note entry, or a subdir containing notes */
2091 memcpy(hex_oid
+ hex_oid_len
, e
->name
->str_dat
,
2093 if (tmp_fullpath_len
)
2094 fullpath
[tmp_fullpath_len
++] = '/';
2095 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2097 tmp_fullpath_len
+= e
->name
->str_len
;
2098 fullpath
[tmp_fullpath_len
] = '\0';
2100 if (tmp_hex_oid_len
== hexsz
&& !get_oid_hex(hex_oid
, &oid
)) {
2101 /* This is a note entry */
2102 if (fanout
== 0xff) {
2103 /* Counting mode, no rename */
2107 construct_path_with_fanout(hex_oid
, fanout
, realpath
);
2108 if (!strcmp(fullpath
, realpath
)) {
2109 /* Note entry is in correct location */
2114 /* Rename fullpath to realpath */
2115 if (!tree_content_remove(orig_root
, fullpath
, &leaf
, 0))
2116 die("Failed to remove path %s", fullpath
);
2117 tree_content_set(orig_root
, realpath
,
2118 &leaf
.versions
[1].oid
,
2119 leaf
.versions
[1].mode
,
2121 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2122 /* This is a subdir that may contain note entries */
2123 num_notes
+= do_change_note_fanout(orig_root
, e
,
2124 hex_oid
, tmp_hex_oid_len
,
2125 fullpath
, tmp_fullpath_len
, fanout
);
2128 /* The above may have reallocated the current tree_content */
2134 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2135 unsigned char fanout
)
2138 * The size of path is due to one slash between every two hex digits,
2139 * plus the terminating NUL. Note that there is no slash at the end, so
2140 * the number of slashes is one less than half the number of hex
2143 char hex_oid
[GIT_MAX_HEXSZ
], path
[GIT_MAX_HEXSZ
+ (GIT_MAX_HEXSZ
/ 2) - 1 + 1];
2144 return do_change_note_fanout(root
, root
, hex_oid
, 0, path
, 0, fanout
);
2148 * Given a pointer into a string, parse a mark reference:
2150 * idnum ::= ':' bigint;
2152 * Return the first character after the value in *endptr.
2154 * Complain if the following character is not what is expected,
2155 * either a space or end of the string.
2157 static uintmax_t parse_mark_ref(const char *p
, char **endptr
)
2163 mark
= strtoumax(p
, endptr
, 10);
2165 die("No value after ':' in mark: %s", command_buf
.buf
);
2170 * Parse the mark reference, and complain if this is not the end of
2173 static uintmax_t parse_mark_ref_eol(const char *p
)
2178 mark
= parse_mark_ref(p
, &end
);
2180 die("Garbage after mark: %s", command_buf
.buf
);
2185 * Parse the mark reference, demanding a trailing space. Return a
2186 * pointer to the space.
2188 static uintmax_t parse_mark_ref_space(const char **p
)
2193 mark
= parse_mark_ref(*p
, &end
);
2195 die("Missing space after mark: %s", command_buf
.buf
);
2200 static void file_change_m(const char *p
, struct branch
*b
)
2202 static struct strbuf uq
= STRBUF_INIT
;
2204 struct object_entry
*oe
;
2205 struct object_id oid
;
2206 uint16_t mode
, inline_data
= 0;
2208 p
= get_mode(p
, &mode
);
2210 die("Corrupt mode: %s", command_buf
.buf
);
2215 case S_IFREG
| 0644:
2216 case S_IFREG
| 0755:
2223 die("Corrupt mode: %s", command_buf
.buf
);
2227 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2228 oidcpy(&oid
, &oe
->idx
.oid
);
2229 } else if (skip_prefix(p
, "inline ", &p
)) {
2231 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2233 if (parse_oid_hex(p
, &oid
, &p
))
2234 die("Invalid dataref: %s", command_buf
.buf
);
2235 oe
= find_object(&oid
);
2237 die("Missing space after SHA1: %s", command_buf
.buf
);
2241 if (!unquote_c_style(&uq
, p
, &endp
)) {
2243 die("Garbage after path in: %s", command_buf
.buf
);
2247 /* Git does not track empty, non-toplevel directories. */
2248 if (S_ISDIR(mode
) && is_empty_tree_oid(&oid
) && *p
) {
2249 tree_content_remove(&b
->branch_tree
, p
, NULL
, 0);
2253 if (S_ISGITLINK(mode
)) {
2255 die("Git links cannot be specified 'inline': %s",
2258 if (oe
->type
!= OBJ_COMMIT
)
2259 die("Not a commit (actually a %s): %s",
2260 type_name(oe
->type
), command_buf
.buf
);
2263 * Accept the sha1 without checking; it expected to be in
2264 * another repository.
2266 } else if (inline_data
) {
2268 die("Directories cannot be specified 'inline': %s",
2271 strbuf_addstr(&uq
, p
);
2274 while (read_next_command() != EOF
) {
2276 if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2279 parse_and_store_blob(&last_blob
, &oid
, 0);
2284 enum object_type expected
= S_ISDIR(mode
) ?
2286 enum object_type type
= oe
? oe
->type
:
2287 oid_object_info(the_repository
, &oid
,
2290 die("%s not found: %s",
2291 S_ISDIR(mode
) ? "Tree" : "Blob",
2293 if (type
!= expected
)
2294 die("Not a %s (actually a %s): %s",
2295 type_name(expected
), type_name(type
),
2300 tree_content_replace(&b
->branch_tree
, &oid
, mode
, NULL
);
2303 tree_content_set(&b
->branch_tree
, p
, &oid
, mode
, NULL
);
2306 static void file_change_d(const char *p
, struct branch
*b
)
2308 static struct strbuf uq
= STRBUF_INIT
;
2312 if (!unquote_c_style(&uq
, p
, &endp
)) {
2314 die("Garbage after path in: %s", command_buf
.buf
);
2317 tree_content_remove(&b
->branch_tree
, p
, NULL
, 1);
2320 static void file_change_cr(const char *s
, struct branch
*b
, int rename
)
2323 static struct strbuf s_uq
= STRBUF_INIT
;
2324 static struct strbuf d_uq
= STRBUF_INIT
;
2326 struct tree_entry leaf
;
2328 strbuf_reset(&s_uq
);
2329 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2331 die("Missing space after source: %s", command_buf
.buf
);
2333 endp
= strchr(s
, ' ');
2335 die("Missing space after source: %s", command_buf
.buf
);
2336 strbuf_add(&s_uq
, s
, endp
- s
);
2342 die("Missing dest: %s", command_buf
.buf
);
2345 strbuf_reset(&d_uq
);
2346 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2348 die("Garbage after dest in: %s", command_buf
.buf
);
2352 memset(&leaf
, 0, sizeof(leaf
));
2354 tree_content_remove(&b
->branch_tree
, s
, &leaf
, 1);
2356 tree_content_get(&b
->branch_tree
, s
, &leaf
, 1);
2357 if (!leaf
.versions
[1].mode
)
2358 die("Path %s not in branch", s
);
2359 if (!*d
) { /* C "path/to/subdir" "" */
2360 tree_content_replace(&b
->branch_tree
,
2361 &leaf
.versions
[1].oid
,
2362 leaf
.versions
[1].mode
,
2366 tree_content_set(&b
->branch_tree
, d
,
2367 &leaf
.versions
[1].oid
,
2368 leaf
.versions
[1].mode
,
2372 static void note_change_n(const char *p
, struct branch
*b
, unsigned char *old_fanout
)
2374 static struct strbuf uq
= STRBUF_INIT
;
2375 struct object_entry
*oe
;
2377 struct object_id oid
, commit_oid
;
2378 char path
[GIT_MAX_RAWSZ
* 3];
2379 uint16_t inline_data
= 0;
2380 unsigned char new_fanout
;
2383 * When loading a branch, we don't traverse its tree to count the real
2384 * number of notes (too expensive to do this for all non-note refs).
2385 * This means that recently loaded notes refs might incorrectly have
2386 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2388 * Fix this by traversing the tree and counting the number of notes
2389 * when b->num_notes == 0. If the notes tree is truly empty, the
2390 * calculation should not take long.
2392 if (b
->num_notes
== 0 && *old_fanout
== 0) {
2393 /* Invoke change_note_fanout() in "counting mode". */
2394 b
->num_notes
= change_note_fanout(&b
->branch_tree
, 0xff);
2395 *old_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2398 /* Now parse the notemodify command. */
2399 /* <dataref> or 'inline' */
2401 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2402 oidcpy(&oid
, &oe
->idx
.oid
);
2403 } else if (skip_prefix(p
, "inline ", &p
)) {
2405 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2407 if (parse_oid_hex(p
, &oid
, &p
))
2408 die("Invalid dataref: %s", command_buf
.buf
);
2409 oe
= find_object(&oid
);
2411 die("Missing space after SHA1: %s", command_buf
.buf
);
2415 s
= lookup_branch(p
);
2417 if (is_null_oid(&s
->oid
))
2418 die("Can't add a note on empty branch.");
2419 oidcpy(&commit_oid
, &s
->oid
);
2420 } else if (*p
== ':') {
2421 uintmax_t commit_mark
= parse_mark_ref_eol(p
);
2422 struct object_entry
*commit_oe
= find_mark(marks
, commit_mark
);
2423 if (commit_oe
->type
!= OBJ_COMMIT
)
2424 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2425 oidcpy(&commit_oid
, &commit_oe
->idx
.oid
);
2426 } else if (!get_oid(p
, &commit_oid
)) {
2428 char *buf
= read_object_with_reference(the_repository
,
2432 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2433 die("Not a valid commit: %s", p
);
2436 die("Invalid ref name or SHA1 expression: %s", p
);
2440 strbuf_addstr(&uq
, p
);
2443 read_next_command();
2444 parse_and_store_blob(&last_blob
, &oid
, 0);
2446 if (oe
->type
!= OBJ_BLOB
)
2447 die("Not a blob (actually a %s): %s",
2448 type_name(oe
->type
), command_buf
.buf
);
2449 } else if (!is_null_oid(&oid
)) {
2450 enum object_type type
= oid_object_info(the_repository
, &oid
,
2453 die("Blob not found: %s", command_buf
.buf
);
2454 if (type
!= OBJ_BLOB
)
2455 die("Not a blob (actually a %s): %s",
2456 type_name(type
), command_buf
.buf
);
2459 construct_path_with_fanout(oid_to_hex(&commit_oid
), *old_fanout
, path
);
2460 if (tree_content_remove(&b
->branch_tree
, path
, NULL
, 0))
2463 if (is_null_oid(&oid
))
2464 return; /* nothing to insert */
2467 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2468 construct_path_with_fanout(oid_to_hex(&commit_oid
), new_fanout
, path
);
2469 tree_content_set(&b
->branch_tree
, path
, &oid
, S_IFREG
| 0644, NULL
);
2472 static void file_change_deleteall(struct branch
*b
)
2474 release_tree_content_recursive(b
->branch_tree
.tree
);
2475 oidclr(&b
->branch_tree
.versions
[0].oid
);
2476 oidclr(&b
->branch_tree
.versions
[1].oid
);
2477 load_tree(&b
->branch_tree
);
2481 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2483 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2484 die("Not a valid commit: %s", oid_to_hex(&b
->oid
));
2485 if (memcmp("tree ", buf
, 5)
2486 || get_oid_hex(buf
+ 5, &b
->branch_tree
.versions
[1].oid
))
2487 die("The commit %s is corrupt", oid_to_hex(&b
->oid
));
2488 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2489 &b
->branch_tree
.versions
[1].oid
);
2492 static void parse_from_existing(struct branch
*b
)
2494 if (is_null_oid(&b
->oid
)) {
2495 oidclr(&b
->branch_tree
.versions
[0].oid
);
2496 oidclr(&b
->branch_tree
.versions
[1].oid
);
2501 buf
= read_object_with_reference(the_repository
,
2502 &b
->oid
, commit_type
, &size
,
2504 parse_from_commit(b
, buf
, size
);
2509 static int parse_objectish(struct branch
*b
, const char *objectish
)
2512 struct object_id oid
;
2514 oidcpy(&oid
, &b
->branch_tree
.versions
[1].oid
);
2516 s
= lookup_branch(objectish
);
2518 die("Can't create a branch from itself: %s", b
->name
);
2520 struct object_id
*t
= &s
->branch_tree
.versions
[1].oid
;
2521 oidcpy(&b
->oid
, &s
->oid
);
2522 oidcpy(&b
->branch_tree
.versions
[0].oid
, t
);
2523 oidcpy(&b
->branch_tree
.versions
[1].oid
, t
);
2524 } else if (*objectish
== ':') {
2525 uintmax_t idnum
= parse_mark_ref_eol(objectish
);
2526 struct object_entry
*oe
= find_mark(marks
, idnum
);
2527 if (oe
->type
!= OBJ_COMMIT
)
2528 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2529 if (!oideq(&b
->oid
, &oe
->idx
.oid
)) {
2530 oidcpy(&b
->oid
, &oe
->idx
.oid
);
2531 if (oe
->pack_id
!= MAX_PACK_ID
) {
2533 char *buf
= gfi_unpack_entry(oe
, &size
);
2534 parse_from_commit(b
, buf
, size
);
2537 parse_from_existing(b
);
2539 } else if (!get_oid(objectish
, &b
->oid
)) {
2540 parse_from_existing(b
);
2541 if (is_null_oid(&b
->oid
))
2545 die("Invalid ref name or SHA1 expression: %s", objectish
);
2547 if (b
->branch_tree
.tree
&& !oideq(&oid
, &b
->branch_tree
.versions
[1].oid
)) {
2548 release_tree_content_recursive(b
->branch_tree
.tree
);
2549 b
->branch_tree
.tree
= NULL
;
2552 read_next_command();
2556 static int parse_from(struct branch
*b
)
2560 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2563 return parse_objectish(b
, from
);
2566 static int parse_objectish_with_prefix(struct branch
*b
, const char *prefix
)
2570 if (!skip_prefix(command_buf
.buf
, prefix
, &base
))
2573 return parse_objectish(b
, base
);
2576 static struct hash_list
*parse_merge(unsigned int *count
)
2578 struct hash_list
*list
= NULL
, **tail
= &list
, *n
;
2583 while (skip_prefix(command_buf
.buf
, "merge ", &from
)) {
2584 n
= xmalloc(sizeof(*n
));
2585 s
= lookup_branch(from
);
2587 oidcpy(&n
->oid
, &s
->oid
);
2588 else if (*from
== ':') {
2589 uintmax_t idnum
= parse_mark_ref_eol(from
);
2590 struct object_entry
*oe
= find_mark(marks
, idnum
);
2591 if (oe
->type
!= OBJ_COMMIT
)
2592 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2593 oidcpy(&n
->oid
, &oe
->idx
.oid
);
2594 } else if (!get_oid(from
, &n
->oid
)) {
2596 char *buf
= read_object_with_reference(the_repository
,
2600 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2601 die("Not a valid commit: %s", from
);
2604 die("Invalid ref name or SHA1 expression: %s", from
);
2611 read_next_command();
2616 static void parse_new_commit(const char *arg
)
2618 static struct strbuf msg
= STRBUF_INIT
;
2620 char *author
= NULL
;
2621 char *committer
= NULL
;
2622 char *encoding
= NULL
;
2623 struct hash_list
*merge_list
= NULL
;
2624 unsigned int merge_count
;
2625 unsigned char prev_fanout
, new_fanout
;
2628 b
= lookup_branch(arg
);
2630 b
= new_branch(arg
);
2632 read_next_command();
2634 parse_original_identifier();
2635 if (skip_prefix(command_buf
.buf
, "author ", &v
)) {
2636 author
= parse_ident(v
);
2637 read_next_command();
2639 if (skip_prefix(command_buf
.buf
, "committer ", &v
)) {
2640 committer
= parse_ident(v
);
2641 read_next_command();
2644 die("Expected committer but didn't get one");
2645 if (skip_prefix(command_buf
.buf
, "encoding ", &v
)) {
2646 encoding
= xstrdup(v
);
2647 read_next_command();
2649 parse_data(&msg
, 0, NULL
);
2650 read_next_command();
2652 merge_list
= parse_merge(&merge_count
);
2654 /* ensure the branch is active/loaded */
2655 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2656 unload_one_branch();
2660 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2663 while (command_buf
.len
> 0) {
2664 if (skip_prefix(command_buf
.buf
, "M ", &v
))
2665 file_change_m(v
, b
);
2666 else if (skip_prefix(command_buf
.buf
, "D ", &v
))
2667 file_change_d(v
, b
);
2668 else if (skip_prefix(command_buf
.buf
, "R ", &v
))
2669 file_change_cr(v
, b
, 1);
2670 else if (skip_prefix(command_buf
.buf
, "C ", &v
))
2671 file_change_cr(v
, b
, 0);
2672 else if (skip_prefix(command_buf
.buf
, "N ", &v
))
2673 note_change_n(v
, b
, &prev_fanout
);
2674 else if (!strcmp("deleteall", command_buf
.buf
))
2675 file_change_deleteall(b
);
2676 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
2678 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2681 unread_command_buf
= 1;
2684 if (read_next_command() == EOF
)
2688 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2689 if (new_fanout
!= prev_fanout
)
2690 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2692 /* build the tree and the commit */
2693 store_tree(&b
->branch_tree
);
2694 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2695 &b
->branch_tree
.versions
[1].oid
);
2697 strbuf_reset(&new_data
);
2698 strbuf_addf(&new_data
, "tree %s\n",
2699 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
2700 if (!is_null_oid(&b
->oid
))
2701 strbuf_addf(&new_data
, "parent %s\n",
2702 oid_to_hex(&b
->oid
));
2703 while (merge_list
) {
2704 struct hash_list
*next
= merge_list
->next
;
2705 strbuf_addf(&new_data
, "parent %s\n",
2706 oid_to_hex(&merge_list
->oid
));
2710 strbuf_addf(&new_data
,
2713 author
? author
: committer
, committer
);
2715 strbuf_addf(&new_data
,
2718 strbuf_addch(&new_data
, '\n');
2719 strbuf_addbuf(&new_data
, &msg
);
2724 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, &b
->oid
, next_mark
))
2725 b
->pack_id
= pack_id
;
2726 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2729 static void parse_new_tag(const char *arg
)
2731 static struct strbuf msg
= STRBUF_INIT
;
2736 uintmax_t from_mark
= 0;
2737 struct object_id oid
;
2738 enum object_type type
;
2741 t
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct tag
));
2742 memset(t
, 0, sizeof(struct tag
));
2743 t
->name
= pool_strdup(arg
);
2745 last_tag
->next_tag
= t
;
2749 read_next_command();
2753 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2754 die("Expected from command, got %s", command_buf
.buf
);
2755 s
= lookup_branch(from
);
2757 if (is_null_oid(&s
->oid
))
2758 die("Can't tag an empty branch.");
2759 oidcpy(&oid
, &s
->oid
);
2761 } else if (*from
== ':') {
2762 struct object_entry
*oe
;
2763 from_mark
= parse_mark_ref_eol(from
);
2764 oe
= find_mark(marks
, from_mark
);
2766 oidcpy(&oid
, &oe
->idx
.oid
);
2767 } else if (!get_oid(from
, &oid
)) {
2768 struct object_entry
*oe
= find_object(&oid
);
2770 type
= oid_object_info(the_repository
, &oid
, NULL
);
2772 die("Not a valid object: %s", from
);
2776 die("Invalid ref name or SHA1 expression: %s", from
);
2777 read_next_command();
2779 /* original-oid ... */
2780 parse_original_identifier();
2783 if (skip_prefix(command_buf
.buf
, "tagger ", &v
)) {
2784 tagger
= parse_ident(v
);
2785 read_next_command();
2789 /* tag payload/message */
2790 parse_data(&msg
, 0, NULL
);
2792 /* build the tag object */
2793 strbuf_reset(&new_data
);
2795 strbuf_addf(&new_data
,
2799 oid_to_hex(&oid
), type_name(type
), t
->name
);
2801 strbuf_addf(&new_data
,
2802 "tagger %s\n", tagger
);
2803 strbuf_addch(&new_data
, '\n');
2804 strbuf_addbuf(&new_data
, &msg
);
2807 if (store_object(OBJ_TAG
, &new_data
, NULL
, &t
->oid
, next_mark
))
2808 t
->pack_id
= MAX_PACK_ID
;
2810 t
->pack_id
= pack_id
;
2813 static void parse_reset_branch(const char *arg
)
2816 const char *tag_name
;
2818 b
= lookup_branch(arg
);
2821 oidclr(&b
->branch_tree
.versions
[0].oid
);
2822 oidclr(&b
->branch_tree
.versions
[1].oid
);
2823 if (b
->branch_tree
.tree
) {
2824 release_tree_content_recursive(b
->branch_tree
.tree
);
2825 b
->branch_tree
.tree
= NULL
;
2829 b
= new_branch(arg
);
2830 read_next_command();
2832 if (b
->delete && skip_prefix(b
->name
, "refs/tags/", &tag_name
)) {
2834 * Elsewhere, we call dump_branches() before dump_tags(),
2835 * and dump_branches() will handle ref deletions first, so
2836 * in order to make sure the deletion actually takes effect,
2837 * we need to remove the tag from our list of tags to update.
2839 * NEEDSWORK: replace list of tags with hashmap for faster
2842 struct tag
*t
, *prev
= NULL
;
2843 for (t
= first_tag
; t
; t
= t
->next_tag
) {
2844 if (!strcmp(t
->name
, tag_name
))
2850 prev
->next_tag
= t
->next_tag
;
2852 first_tag
= t
->next_tag
;
2855 /* There is no mem_pool_free(t) function to call. */
2858 if (command_buf
.len
> 0)
2859 unread_command_buf
= 1;
2862 static void cat_blob_write(const char *buf
, unsigned long size
)
2864 if (write_in_full(cat_blob_fd
, buf
, size
) < 0)
2865 die_errno("Write to frontend failed");
2868 static void cat_blob(struct object_entry
*oe
, struct object_id
*oid
)
2870 struct strbuf line
= STRBUF_INIT
;
2872 enum object_type type
= 0;
2875 if (!oe
|| oe
->pack_id
== MAX_PACK_ID
) {
2876 buf
= read_object_file(oid
, &type
, &size
);
2879 buf
= gfi_unpack_entry(oe
, &size
);
2883 * Output based on batch_one_object() from cat-file.c.
2886 strbuf_reset(&line
);
2887 strbuf_addf(&line
, "%s missing\n", oid_to_hex(oid
));
2888 cat_blob_write(line
.buf
, line
.len
);
2889 strbuf_release(&line
);
2894 die("Can't read object %s", oid_to_hex(oid
));
2895 if (type
!= OBJ_BLOB
)
2896 die("Object %s is a %s but a blob was expected.",
2897 oid_to_hex(oid
), type_name(type
));
2898 strbuf_reset(&line
);
2899 strbuf_addf(&line
, "%s %s %"PRIuMAX
"\n", oid_to_hex(oid
),
2900 type_name(type
), (uintmax_t)size
);
2901 cat_blob_write(line
.buf
, line
.len
);
2902 strbuf_release(&line
);
2903 cat_blob_write(buf
, size
);
2904 cat_blob_write("\n", 1);
2905 if (oe
&& oe
->pack_id
== pack_id
) {
2906 last_blob
.offset
= oe
->idx
.offset
;
2907 strbuf_attach(&last_blob
.data
, buf
, size
, size
);
2908 last_blob
.depth
= oe
->depth
;
2913 static void parse_get_mark(const char *p
)
2915 struct object_entry
*oe
;
2916 char output
[GIT_MAX_HEXSZ
+ 2];
2918 /* get-mark SP <object> LF */
2920 die("Not a mark: %s", p
);
2922 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
2924 die("Unknown mark: %s", command_buf
.buf
);
2926 xsnprintf(output
, sizeof(output
), "%s\n", oid_to_hex(&oe
->idx
.oid
));
2927 cat_blob_write(output
, the_hash_algo
->hexsz
+ 1);
2930 static void parse_cat_blob(const char *p
)
2932 struct object_entry
*oe
;
2933 struct object_id oid
;
2935 /* cat-blob SP <object> LF */
2937 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
2939 die("Unknown mark: %s", command_buf
.buf
);
2940 oidcpy(&oid
, &oe
->idx
.oid
);
2942 if (parse_oid_hex(p
, &oid
, &p
))
2943 die("Invalid dataref: %s", command_buf
.buf
);
2945 die("Garbage after SHA1: %s", command_buf
.buf
);
2946 oe
= find_object(&oid
);
2952 static struct object_entry
*dereference(struct object_entry
*oe
,
2953 struct object_id
*oid
)
2957 const unsigned hexsz
= the_hash_algo
->hexsz
;
2960 enum object_type type
= oid_object_info(the_repository
, oid
,
2963 die("object not found: %s", oid_to_hex(oid
));
2965 oe
= insert_object(oid
);
2967 oe
->pack_id
= MAX_PACK_ID
;
2971 case OBJ_TREE
: /* easy case. */
2977 die("Not a tree-ish: %s", command_buf
.buf
);
2980 if (oe
->pack_id
!= MAX_PACK_ID
) { /* in a pack being written */
2981 buf
= gfi_unpack_entry(oe
, &size
);
2983 enum object_type unused
;
2984 buf
= read_object_file(oid
, &unused
, &size
);
2987 die("Can't load object %s", oid_to_hex(oid
));
2989 /* Peel one layer. */
2992 if (size
< hexsz
+ strlen("object ") ||
2993 get_oid_hex(buf
+ strlen("object "), oid
))
2994 die("Invalid SHA1 in tag: %s", command_buf
.buf
);
2997 if (size
< hexsz
+ strlen("tree ") ||
2998 get_oid_hex(buf
+ strlen("tree "), oid
))
2999 die("Invalid SHA1 in commit: %s", command_buf
.buf
);
3003 return find_object(oid
);
3006 static struct object_entry
*parse_treeish_dataref(const char **p
)
3008 struct object_id oid
;
3009 struct object_entry
*e
;
3011 if (**p
== ':') { /* <mark> */
3012 e
= find_mark(marks
, parse_mark_ref_space(p
));
3014 die("Unknown mark: %s", command_buf
.buf
);
3015 oidcpy(&oid
, &e
->idx
.oid
);
3016 } else { /* <sha1> */
3017 if (parse_oid_hex(*p
, &oid
, p
))
3018 die("Invalid dataref: %s", command_buf
.buf
);
3019 e
= find_object(&oid
);
3021 die("Missing space after tree-ish: %s", command_buf
.buf
);
3024 while (!e
|| e
->type
!= OBJ_TREE
)
3025 e
= dereference(e
, &oid
);
3029 static void print_ls(int mode
, const unsigned char *hash
, const char *path
)
3031 static struct strbuf line
= STRBUF_INIT
;
3033 /* See show_tree(). */
3035 S_ISGITLINK(mode
) ? commit_type
:
3036 S_ISDIR(mode
) ? tree_type
:
3040 /* missing SP path LF */
3041 strbuf_reset(&line
);
3042 strbuf_addstr(&line
, "missing ");
3043 quote_c_style(path
, &line
, NULL
, 0);
3044 strbuf_addch(&line
, '\n');
3046 /* mode SP type SP object_name TAB path LF */
3047 strbuf_reset(&line
);
3048 strbuf_addf(&line
, "%06o %s %s\t",
3049 mode
& ~NO_DELTA
, type
, hash_to_hex(hash
));
3050 quote_c_style(path
, &line
, NULL
, 0);
3051 strbuf_addch(&line
, '\n');
3053 cat_blob_write(line
.buf
, line
.len
);
3056 static void parse_ls(const char *p
, struct branch
*b
)
3058 struct tree_entry
*root
= NULL
;
3059 struct tree_entry leaf
= {NULL
};
3061 /* ls SP (<tree-ish> SP)? <path> */
3064 die("Not in a commit: %s", command_buf
.buf
);
3065 root
= &b
->branch_tree
;
3067 struct object_entry
*e
= parse_treeish_dataref(&p
);
3068 root
= new_tree_entry();
3069 oidcpy(&root
->versions
[1].oid
, &e
->idx
.oid
);
3070 if (!is_null_oid(&root
->versions
[1].oid
))
3071 root
->versions
[1].mode
= S_IFDIR
;
3075 static struct strbuf uq
= STRBUF_INIT
;
3078 if (unquote_c_style(&uq
, p
, &endp
))
3079 die("Invalid path: %s", command_buf
.buf
);
3081 die("Garbage after path in: %s", command_buf
.buf
);
3084 tree_content_get(root
, p
, &leaf
, 1);
3086 * A directory in preparation would have a sha1 of zero
3087 * until it is saved. Save, for simplicity.
3089 if (S_ISDIR(leaf
.versions
[1].mode
))
3092 print_ls(leaf
.versions
[1].mode
, leaf
.versions
[1].oid
.hash
, p
);
3094 release_tree_content_recursive(leaf
.tree
);
3095 if (!b
|| root
!= &b
->branch_tree
)
3096 release_tree_entry(root
);
3099 static void checkpoint(void)
3101 checkpoint_requested
= 0;
3110 static void parse_checkpoint(void)
3112 checkpoint_requested
= 1;
3116 static void parse_progress(void)
3118 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
3119 fputc('\n', stdout
);
3124 static void parse_alias(void)
3126 struct object_entry
*e
;
3130 read_next_command();
3135 die(_("Expected 'mark' command, got %s"), command_buf
.buf
);
3138 memset(&b
, 0, sizeof(b
));
3139 if (!parse_objectish_with_prefix(&b
, "to "))
3140 die(_("Expected 'to' command, got %s"), command_buf
.buf
);
3141 e
= find_object(&b
.oid
);
3143 insert_mark(marks
, next_mark
, e
);
3146 static char* make_fast_import_path(const char *path
)
3148 if (!relative_marks_paths
|| is_absolute_path(path
))
3149 return xstrdup(path
);
3150 return git_pathdup("info/fast-import/%s", path
);
3153 static void option_import_marks(const char *marks
,
3154 int from_stream
, int ignore_missing
)
3156 if (import_marks_file
) {
3158 die("Only one import-marks command allowed per stream");
3160 /* read previous mark file */
3161 if(!import_marks_file_from_stream
)
3165 import_marks_file
= make_fast_import_path(marks
);
3166 import_marks_file_from_stream
= from_stream
;
3167 import_marks_file_ignore_missing
= ignore_missing
;
3170 static void option_date_format(const char *fmt
)
3172 if (!strcmp(fmt
, "raw"))
3173 whenspec
= WHENSPEC_RAW
;
3174 else if (!strcmp(fmt
, "rfc2822"))
3175 whenspec
= WHENSPEC_RFC2822
;
3176 else if (!strcmp(fmt
, "now"))
3177 whenspec
= WHENSPEC_NOW
;
3179 die("unknown --date-format argument %s", fmt
);
3182 static unsigned long ulong_arg(const char *option
, const char *arg
)
3185 unsigned long rv
= strtoul(arg
, &endptr
, 0);
3186 if (strchr(arg
, '-') || endptr
== arg
|| *endptr
)
3187 die("%s: argument must be a non-negative integer", option
);
3191 static void option_depth(const char *depth
)
3193 max_depth
= ulong_arg("--depth", depth
);
3194 if (max_depth
> MAX_DEPTH
)
3195 die("--depth cannot exceed %u", MAX_DEPTH
);
3198 static void option_active_branches(const char *branches
)
3200 max_active_branches
= ulong_arg("--active-branches", branches
);
3203 static void option_export_marks(const char *marks
)
3205 export_marks_file
= make_fast_import_path(marks
);
3208 static void option_cat_blob_fd(const char *fd
)
3210 unsigned long n
= ulong_arg("--cat-blob-fd", fd
);
3211 if (n
> (unsigned long) INT_MAX
)
3212 die("--cat-blob-fd cannot exceed %d", INT_MAX
);
3213 cat_blob_fd
= (int) n
;
3216 static void option_export_pack_edges(const char *edges
)
3220 pack_edges
= xfopen(edges
, "a");
3223 static int parse_one_option(const char *option
)
3225 if (skip_prefix(option
, "max-pack-size=", &option
)) {
3227 if (!git_parse_ulong(option
, &v
))
3230 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v
);
3232 } else if (v
< 1024 * 1024) {
3233 warning("minimum max-pack-size is 1 MiB");
3237 } else if (skip_prefix(option
, "big-file-threshold=", &option
)) {
3239 if (!git_parse_ulong(option
, &v
))
3241 big_file_threshold
= v
;
3242 } else if (skip_prefix(option
, "depth=", &option
)) {
3243 option_depth(option
);
3244 } else if (skip_prefix(option
, "active-branches=", &option
)) {
3245 option_active_branches(option
);
3246 } else if (skip_prefix(option
, "export-pack-edges=", &option
)) {
3247 option_export_pack_edges(option
);
3248 } else if (!strcmp(option
, "quiet")) {
3250 } else if (!strcmp(option
, "stats")) {
3252 } else if (!strcmp(option
, "allow-unsafe-features")) {
3253 ; /* already handled during early option parsing */
3261 static void check_unsafe_feature(const char *feature
, int from_stream
)
3263 if (from_stream
&& !allow_unsafe_features
)
3264 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3268 static int parse_one_feature(const char *feature
, int from_stream
)
3272 if (skip_prefix(feature
, "date-format=", &arg
)) {
3273 option_date_format(arg
);
3274 } else if (skip_prefix(feature
, "import-marks=", &arg
)) {
3275 check_unsafe_feature("import-marks", from_stream
);
3276 option_import_marks(arg
, from_stream
, 0);
3277 } else if (skip_prefix(feature
, "import-marks-if-exists=", &arg
)) {
3278 check_unsafe_feature("import-marks-if-exists", from_stream
);
3279 option_import_marks(arg
, from_stream
, 1);
3280 } else if (skip_prefix(feature
, "export-marks=", &arg
)) {
3281 check_unsafe_feature(feature
, from_stream
);
3282 option_export_marks(arg
);
3283 } else if (!strcmp(feature
, "alias")) {
3284 ; /* Don't die - this feature is supported */
3285 } else if (!strcmp(feature
, "get-mark")) {
3286 ; /* Don't die - this feature is supported */
3287 } else if (!strcmp(feature
, "cat-blob")) {
3288 ; /* Don't die - this feature is supported */
3289 } else if (!strcmp(feature
, "relative-marks")) {
3290 relative_marks_paths
= 1;
3291 } else if (!strcmp(feature
, "no-relative-marks")) {
3292 relative_marks_paths
= 0;
3293 } else if (!strcmp(feature
, "done")) {
3294 require_explicit_termination
= 1;
3295 } else if (!strcmp(feature
, "force")) {
3297 } else if (!strcmp(feature
, "notes") || !strcmp(feature
, "ls")) {
3298 ; /* do nothing; we have the feature */
3306 static void parse_feature(const char *feature
)
3308 if (seen_data_command
)
3309 die("Got feature command '%s' after data command", feature
);
3311 if (parse_one_feature(feature
, 1))
3314 die("This version of fast-import does not support feature %s.", feature
);
3317 static void parse_option(const char *option
)
3319 if (seen_data_command
)
3320 die("Got option command '%s' after data command", option
);
3322 if (parse_one_option(option
))
3325 die("This version of fast-import does not support option: %s", option
);
3328 static void git_pack_config(void)
3330 int indexversion_value
;
3332 unsigned long packsizelimit_value
;
3334 if (!git_config_get_ulong("pack.depth", &max_depth
)) {
3335 if (max_depth
> MAX_DEPTH
)
3336 max_depth
= MAX_DEPTH
;
3338 if (!git_config_get_int("pack.indexversion", &indexversion_value
)) {
3339 pack_idx_opts
.version
= indexversion_value
;
3340 if (pack_idx_opts
.version
> 2)
3341 git_die_config("pack.indexversion",
3342 "bad pack.indexversion=%"PRIu32
, pack_idx_opts
.version
);
3344 if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value
))
3345 max_packsize
= packsizelimit_value
;
3347 if (!git_config_get_int("fastimport.unpacklimit", &limit
))
3348 unpack_limit
= limit
;
3349 else if (!git_config_get_int("transfer.unpacklimit", &limit
))
3350 unpack_limit
= limit
;
3352 git_config(git_default_config
, NULL
);
3355 static const char fast_import_usage
[] =
3356 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3358 static void parse_argv(void)
3362 for (i
= 1; i
< global_argc
; i
++) {
3363 const char *a
= global_argv
[i
];
3365 if (*a
!= '-' || !strcmp(a
, "--"))
3368 if (!skip_prefix(a
, "--", &a
))
3369 die("unknown option %s", a
);
3371 if (parse_one_option(a
))
3374 if (parse_one_feature(a
, 0))
3377 if (skip_prefix(a
, "cat-blob-fd=", &a
)) {
3378 option_cat_blob_fd(a
);
3382 die("unknown option --%s", a
);
3384 if (i
!= global_argc
)
3385 usage(fast_import_usage
);
3387 seen_data_command
= 1;
3388 if (import_marks_file
)
3392 int cmd_main(int argc
, const char **argv
)
3396 if (argc
== 2 && !strcmp(argv
[1], "-h"))
3397 usage(fast_import_usage
);
3399 setup_git_directory();
3400 reset_pack_idx_option(&pack_idx_opts
);
3403 alloc_objects(object_entry_alloc
);
3404 strbuf_init(&command_buf
, 0);
3405 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
3406 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
3407 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
3408 marks
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
3411 * We don't parse most options until after we've seen the set of
3412 * "feature" lines at the start of the stream (which allows the command
3413 * line to override stream data). But we must do an early parse of any
3414 * command-line options that impact how we interpret the feature lines.
3416 for (i
= 1; i
< argc
; i
++) {
3417 const char *arg
= argv
[i
];
3418 if (*arg
!= '-' || !strcmp(arg
, "--"))
3420 if (!strcmp(arg
, "--allow-unsafe-features"))
3421 allow_unsafe_features
= 1;
3427 rc_free
= mem_pool_alloc(&fi_mem_pool
, cmd_save
* sizeof(*rc_free
));
3428 for (i
= 0; i
< (cmd_save
- 1); i
++)
3429 rc_free
[i
].next
= &rc_free
[i
+ 1];
3430 rc_free
[cmd_save
- 1].next
= NULL
;
3433 set_die_routine(die_nicely
);
3434 set_checkpoint_signal();
3435 while (read_next_command() != EOF
) {
3437 if (!strcmp("blob", command_buf
.buf
))
3439 else if (skip_prefix(command_buf
.buf
, "commit ", &v
))
3440 parse_new_commit(v
);
3441 else if (skip_prefix(command_buf
.buf
, "tag ", &v
))
3443 else if (skip_prefix(command_buf
.buf
, "reset ", &v
))
3444 parse_reset_branch(v
);
3445 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
3447 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
3449 else if (skip_prefix(command_buf
.buf
, "get-mark ", &v
))
3451 else if (!strcmp("checkpoint", command_buf
.buf
))
3453 else if (!strcmp("done", command_buf
.buf
))
3455 else if (!strcmp("alias", command_buf
.buf
))
3457 else if (starts_with(command_buf
.buf
, "progress "))
3459 else if (skip_prefix(command_buf
.buf
, "feature ", &v
))
3461 else if (skip_prefix(command_buf
.buf
, "option git ", &v
))
3463 else if (starts_with(command_buf
.buf
, "option "))
3464 /* ignore non-git options*/;
3466 die("Unsupported command: %s", command_buf
.buf
);
3468 if (checkpoint_requested
)
3472 /* argv hasn't been parsed yet, do so */
3473 if (!seen_data_command
)
3476 if (require_explicit_termination
&& feof(stdin
))
3477 die("stream ends early");
3490 uintmax_t total_count
= 0, duplicate_count
= 0;
3491 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3492 total_count
+= object_count_by_type
[i
];
3493 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3494 duplicate_count
+= duplicate_count_by_type
[i
];
3496 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3497 fprintf(stderr
, "---------------------------------------------------------------------\n");
3498 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3499 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3500 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
]);
3501 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
]);
3502 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
]);
3503 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
]);
3504 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3505 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3506 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3507 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (tree_entry_allocd
+ fi_mem_pool
.pool_alloc
+ alloc_count
*sizeof(struct object_entry
))/1024);
3508 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd
+ fi_mem_pool
.pool_alloc
) /1024));
3509 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3510 fprintf(stderr
, "---------------------------------------------------------------------\n");
3512 fprintf(stderr
, "---------------------------------------------------------------------\n");
3513 fprintf(stderr
, "\n");
3516 return failure
? 1 : 0;