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
;
124 WHENSPEC_RAW_PERMISSIVE
,
129 struct recent_command
{
130 struct recent_command
*prev
;
131 struct recent_command
*next
;
135 /* Configured limits on output */
136 static unsigned long max_depth
= 50;
137 static off_t max_packsize
;
138 static int unpack_limit
= 100;
139 static int force_update
;
141 /* Stats and misc. counters */
142 static uintmax_t alloc_count
;
143 static uintmax_t marks_set_count
;
144 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
145 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
146 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
147 static uintmax_t delta_count_attempts_by_type
[1 << TYPE_BITS
];
148 static unsigned long object_count
;
149 static unsigned long branch_count
;
150 static unsigned long branch_load_count
;
152 static FILE *pack_edges
;
153 static unsigned int show_stats
= 1;
154 static int global_argc
;
155 static const char **global_argv
;
158 static struct mem_pool fi_mem_pool
= {NULL
, 2*1024*1024 -
159 sizeof(struct mp_block
), 0 };
161 /* Atom management */
162 static unsigned int atom_table_sz
= 4451;
163 static unsigned int atom_cnt
;
164 static struct atom_str
**atom_table
;
166 /* The .pack file being generated */
167 static struct pack_idx_option pack_idx_opts
;
168 static unsigned int pack_id
;
169 static struct hashfile
*pack_file
;
170 static struct packed_git
*pack_data
;
171 static struct packed_git
**all_packs
;
172 static off_t pack_size
;
174 /* Table of objects we've written. */
175 static unsigned int object_entry_alloc
= 5000;
176 static struct object_entry_pool
*blocks
;
177 static struct object_entry
*object_table
[1 << 16];
178 static struct mark_set
*marks
;
179 static const char *export_marks_file
;
180 static const char *import_marks_file
;
181 static int import_marks_file_from_stream
;
182 static int import_marks_file_ignore_missing
;
183 static int import_marks_file_done
;
184 static int relative_marks_paths
;
187 static struct last_object last_blob
= { STRBUF_INIT
, 0, 0, 0 };
189 /* Tree management */
190 static unsigned int tree_entry_alloc
= 1000;
191 static void *avail_tree_entry
;
192 static unsigned int avail_tree_table_sz
= 100;
193 static struct avail_tree_content
**avail_tree_table
;
194 static size_t tree_entry_allocd
;
195 static struct strbuf old_tree
= STRBUF_INIT
;
196 static struct strbuf new_tree
= STRBUF_INIT
;
199 static unsigned long max_active_branches
= 5;
200 static unsigned long cur_active_branches
;
201 static unsigned long branch_table_sz
= 1039;
202 static struct branch
**branch_table
;
203 static struct branch
*active_branches
;
206 static struct tag
*first_tag
;
207 static struct tag
*last_tag
;
209 /* Input stream parsing */
210 static whenspec_type whenspec
= WHENSPEC_RAW
;
211 static struct strbuf command_buf
= STRBUF_INIT
;
212 static int unread_command_buf
;
213 static struct recent_command cmd_hist
= {&cmd_hist
, &cmd_hist
, NULL
};
214 static struct recent_command
*cmd_tail
= &cmd_hist
;
215 static struct recent_command
*rc_free
;
216 static unsigned int cmd_save
= 100;
217 static uintmax_t next_mark
;
218 static struct strbuf new_data
= STRBUF_INIT
;
219 static int seen_data_command
;
220 static int require_explicit_termination
;
221 static int allow_unsafe_features
;
223 /* Signal handling */
224 static volatile sig_atomic_t checkpoint_requested
;
226 /* Where to write output of cat-blob commands */
227 static int cat_blob_fd
= STDOUT_FILENO
;
229 static void parse_argv(void);
230 static void parse_get_mark(const char *p
);
231 static void parse_cat_blob(const char *p
);
232 static void parse_ls(const char *p
, struct branch
*b
);
234 static void write_branch_report(FILE *rpt
, struct branch
*b
)
236 fprintf(rpt
, "%s:\n", b
->name
);
238 fprintf(rpt
, " status :");
240 fputs(" active", rpt
);
241 if (b
->branch_tree
.tree
)
242 fputs(" loaded", rpt
);
243 if (is_null_oid(&b
->branch_tree
.versions
[1].oid
))
244 fputs(" dirty", rpt
);
247 fprintf(rpt
, " tip commit : %s\n", oid_to_hex(&b
->oid
));
248 fprintf(rpt
, " old tree : %s\n",
249 oid_to_hex(&b
->branch_tree
.versions
[0].oid
));
250 fprintf(rpt
, " cur tree : %s\n",
251 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
252 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
254 fputs(" last pack : ", rpt
);
255 if (b
->pack_id
< MAX_PACK_ID
)
256 fprintf(rpt
, "%u", b
->pack_id
);
262 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set
*);
264 static void write_crash_report(const char *err
)
266 char *loc
= git_pathdup("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
267 FILE *rpt
= fopen(loc
, "w");
270 struct recent_command
*rc
;
273 error_errno("can't write crash report %s", loc
);
278 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
280 fprintf(rpt
, "fast-import crash report:\n");
281 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
282 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
283 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_MODE(ISO8601
)));
286 fputs("fatal: ", rpt
);
291 fputs("Most Recent Commands Before Crash\n", rpt
);
292 fputs("---------------------------------\n", rpt
);
293 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
294 if (rc
->next
== &cmd_hist
)
303 fputs("Active Branch LRU\n", rpt
);
304 fputs("-----------------\n", rpt
);
305 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
307 max_active_branches
);
309 fputs(" pos clock name\n", rpt
);
310 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
311 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
312 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
313 ++lu
, b
->last_commit
, b
->name
);
316 fputs("Inactive Branches\n", rpt
);
317 fputs("-----------------\n", rpt
);
318 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
319 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
320 write_branch_report(rpt
, b
);
326 fputs("Annotated Tags\n", rpt
);
327 fputs("--------------\n", rpt
);
328 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
329 fputs(oid_to_hex(&tg
->oid
), rpt
);
331 fputs(tg
->name
, rpt
);
337 fputs("Marks\n", rpt
);
338 fputs("-----\n", rpt
);
339 if (export_marks_file
)
340 fprintf(rpt
, " exported to %s\n", export_marks_file
);
342 dump_marks_helper(rpt
, 0, marks
);
345 fputs("-------------------\n", rpt
);
346 fputs("END OF CRASH REPORT\n", rpt
);
351 static void end_packfile(void);
352 static void unkeep_all_packs(void);
353 static void dump_marks(void);
355 static NORETURN
void die_nicely(const char *err
, va_list params
)
358 char message
[2 * PATH_MAX
];
360 vsnprintf(message
, sizeof(message
), err
, params
);
361 fputs("fatal: ", stderr
);
362 fputs(message
, stderr
);
367 write_crash_report(message
);
375 #ifndef SIGUSR1 /* Windows, for example */
377 static void set_checkpoint_signal(void)
383 static void checkpoint_signal(int signo
)
385 checkpoint_requested
= 1;
388 static void set_checkpoint_signal(void)
392 memset(&sa
, 0, sizeof(sa
));
393 sa
.sa_handler
= checkpoint_signal
;
394 sigemptyset(&sa
.sa_mask
);
395 sa
.sa_flags
= SA_RESTART
;
396 sigaction(SIGUSR1
, &sa
, NULL
);
401 static void alloc_objects(unsigned int cnt
)
403 struct object_entry_pool
*b
;
405 b
= xmalloc(sizeof(struct object_entry_pool
)
406 + cnt
* sizeof(struct object_entry
));
407 b
->next_pool
= blocks
;
408 b
->next_free
= b
->entries
;
409 b
->end
= b
->entries
+ cnt
;
414 static struct object_entry
*new_object(struct object_id
*oid
)
416 struct object_entry
*e
;
418 if (blocks
->next_free
== blocks
->end
)
419 alloc_objects(object_entry_alloc
);
421 e
= blocks
->next_free
++;
422 oidcpy(&e
->idx
.oid
, oid
);
426 static struct object_entry
*find_object(struct object_id
*oid
)
428 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
429 struct object_entry
*e
;
430 for (e
= object_table
[h
]; e
; e
= e
->next
)
431 if (oideq(oid
, &e
->idx
.oid
))
436 static struct object_entry
*insert_object(struct object_id
*oid
)
438 unsigned int h
= oid
->hash
[0] << 8 | oid
->hash
[1];
439 struct object_entry
*e
= object_table
[h
];
442 if (oideq(oid
, &e
->idx
.oid
))
448 e
->next
= object_table
[h
];
454 static void invalidate_pack_id(unsigned int id
)
460 for (h
= 0; h
< ARRAY_SIZE(object_table
); h
++) {
461 struct object_entry
*e
;
463 for (e
= object_table
[h
]; e
; e
= e
->next
)
464 if (e
->pack_id
== id
)
465 e
->pack_id
= MAX_PACK_ID
;
468 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
471 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
472 if (b
->pack_id
== id
)
473 b
->pack_id
= MAX_PACK_ID
;
476 for (t
= first_tag
; t
; t
= t
->next_tag
)
477 if (t
->pack_id
== id
)
478 t
->pack_id
= MAX_PACK_ID
;
481 static unsigned int hc_str(const char *s
, size_t len
)
489 static char *pool_strdup(const char *s
)
491 size_t len
= strlen(s
) + 1;
492 char *r
= mem_pool_alloc(&fi_mem_pool
, len
);
497 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
499 struct mark_set
*s
= marks
;
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 struct object_entry
*find_mark(uintmax_t idnum
)
522 uintmax_t orig_idnum
= idnum
;
523 struct mark_set
*s
= marks
;
524 struct object_entry
*oe
= NULL
;
525 if ((idnum
>> s
->shift
) < 1024) {
526 while (s
&& s
->shift
) {
527 uintmax_t i
= idnum
>> s
->shift
;
528 idnum
-= i
<< s
->shift
;
532 oe
= s
->data
.marked
[idnum
];
535 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
539 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
541 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
544 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
545 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
548 c
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct atom_str
) + len
+ 1);
550 memcpy(c
->str_dat
, s
, len
);
552 c
->next_atom
= atom_table
[hc
];
558 static struct branch
*lookup_branch(const char *name
)
560 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
563 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
564 if (!strcmp(name
, b
->name
))
569 static struct branch
*new_branch(const char *name
)
571 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
572 struct branch
*b
= lookup_branch(name
);
575 die("Invalid attempt to create duplicate branch: %s", name
);
576 if (check_refname_format(name
, REFNAME_ALLOW_ONELEVEL
))
577 die("Branch name doesn't conform to GIT standards: %s", name
);
579 b
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct branch
));
580 b
->name
= pool_strdup(name
);
581 b
->table_next_branch
= branch_table
[hc
];
582 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
583 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
586 b
->pack_id
= MAX_PACK_ID
;
587 branch_table
[hc
] = b
;
592 static unsigned int hc_entries(unsigned int cnt
)
594 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
595 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
598 static struct tree_content
*new_tree_content(unsigned int cnt
)
600 struct avail_tree_content
*f
, *l
= NULL
;
601 struct tree_content
*t
;
602 unsigned int hc
= hc_entries(cnt
);
604 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
605 if (f
->entry_capacity
>= cnt
)
610 l
->next_avail
= f
->next_avail
;
612 avail_tree_table
[hc
] = f
->next_avail
;
614 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
615 f
= mem_pool_alloc(&fi_mem_pool
, sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
616 f
->entry_capacity
= cnt
;
619 t
= (struct tree_content
*)f
;
625 static void release_tree_entry(struct tree_entry
*e
);
626 static void release_tree_content(struct tree_content
*t
)
628 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
629 unsigned int hc
= hc_entries(f
->entry_capacity
);
630 f
->next_avail
= avail_tree_table
[hc
];
631 avail_tree_table
[hc
] = f
;
634 static void release_tree_content_recursive(struct tree_content
*t
)
637 for (i
= 0; i
< t
->entry_count
; i
++)
638 release_tree_entry(t
->entries
[i
]);
639 release_tree_content(t
);
642 static struct tree_content
*grow_tree_content(
643 struct tree_content
*t
,
646 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
647 r
->entry_count
= t
->entry_count
;
648 r
->delta_depth
= t
->delta_depth
;
649 COPY_ARRAY(r
->entries
, t
->entries
, t
->entry_count
);
650 release_tree_content(t
);
654 static struct tree_entry
*new_tree_entry(void)
656 struct tree_entry
*e
;
658 if (!avail_tree_entry
) {
659 unsigned int n
= tree_entry_alloc
;
660 tree_entry_allocd
+= n
* sizeof(struct tree_entry
);
662 avail_tree_entry
= e
;
664 *((void**)e
) = e
+ 1;
670 e
= avail_tree_entry
;
671 avail_tree_entry
= *((void**)e
);
675 static void release_tree_entry(struct tree_entry
*e
)
678 release_tree_content_recursive(e
->tree
);
679 *((void**)e
) = avail_tree_entry
;
680 avail_tree_entry
= e
;
683 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
685 struct tree_content
*d
;
686 struct tree_entry
*a
, *b
;
691 d
= new_tree_content(s
->entry_count
);
692 for (i
= 0; i
< s
->entry_count
; i
++) {
694 b
= new_tree_entry();
695 memcpy(b
, a
, sizeof(*a
));
696 if (a
->tree
&& is_null_oid(&b
->versions
[1].oid
))
697 b
->tree
= dup_tree_content(a
->tree
);
702 d
->entry_count
= s
->entry_count
;
703 d
->delta_depth
= s
->delta_depth
;
708 static void start_packfile(void)
710 struct strbuf tmp_file
= STRBUF_INIT
;
711 struct packed_git
*p
;
712 struct pack_header hdr
;
715 pack_fd
= odb_mkstemp(&tmp_file
, "pack/tmp_pack_XXXXXX");
716 FLEX_ALLOC_STR(p
, pack_name
, tmp_file
.buf
);
717 strbuf_release(&tmp_file
);
719 p
->pack_fd
= pack_fd
;
721 pack_file
= hashfd(pack_fd
, p
->pack_name
);
723 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
724 hdr
.hdr_version
= htonl(2);
726 hashwrite(pack_file
, &hdr
, sizeof(hdr
));
729 pack_size
= sizeof(hdr
);
732 REALLOC_ARRAY(all_packs
, pack_id
+ 1);
733 all_packs
[pack_id
] = p
;
736 static const char *create_index(void)
739 struct pack_idx_entry
**idx
, **c
, **last
;
740 struct object_entry
*e
;
741 struct object_entry_pool
*o
;
743 /* Build the table of object IDs. */
744 ALLOC_ARRAY(idx
, object_count
);
746 for (o
= blocks
; o
; o
= o
->next_pool
)
747 for (e
= o
->next_free
; e
-- != o
->entries
;)
748 if (pack_id
== e
->pack_id
)
750 last
= idx
+ object_count
;
752 die("internal consistency error creating the index");
754 tmpfile
= write_idx_file(NULL
, idx
, object_count
, &pack_idx_opts
,
760 static char *keep_pack(const char *curr_index_name
)
762 static const char *keep_msg
= "fast-import";
763 struct strbuf name
= STRBUF_INIT
;
766 odb_pack_name(&name
, pack_data
->hash
, "keep");
767 keep_fd
= odb_pack_keep(name
.buf
);
769 die_errno("cannot create keep file");
770 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
772 die_errno("failed to write keep file");
774 odb_pack_name(&name
, pack_data
->hash
, "pack");
775 if (finalize_object_file(pack_data
->pack_name
, name
.buf
))
776 die("cannot store pack file");
778 odb_pack_name(&name
, pack_data
->hash
, "idx");
779 if (finalize_object_file(curr_index_name
, name
.buf
))
780 die("cannot store index file");
781 free((void *)curr_index_name
);
782 return strbuf_detach(&name
, NULL
);
785 static void unkeep_all_packs(void)
787 struct strbuf name
= STRBUF_INIT
;
790 for (k
= 0; k
< pack_id
; k
++) {
791 struct packed_git
*p
= all_packs
[k
];
792 odb_pack_name(&name
, p
->hash
, "keep");
793 unlink_or_warn(name
.buf
);
795 strbuf_release(&name
);
798 static int loosen_small_pack(const struct packed_git
*p
)
800 struct child_process unpack
= CHILD_PROCESS_INIT
;
802 if (lseek(p
->pack_fd
, 0, SEEK_SET
) < 0)
803 die_errno("Failed seeking to start of '%s'", p
->pack_name
);
805 unpack
.in
= p
->pack_fd
;
807 unpack
.stdout_to_stderr
= 1;
808 argv_array_push(&unpack
.args
, "unpack-objects");
810 argv_array_push(&unpack
.args
, "-q");
812 return run_command(&unpack
);
815 static void end_packfile(void)
819 if (running
|| !pack_data
)
823 clear_delta_base_cache();
825 struct packed_git
*new_p
;
826 struct object_id cur_pack_oid
;
832 close_pack_windows(pack_data
);
833 finalize_hashfile(pack_file
, cur_pack_oid
.hash
, 0);
834 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->hash
,
835 pack_data
->pack_name
, object_count
,
836 cur_pack_oid
.hash
, pack_size
);
838 if (object_count
<= unpack_limit
) {
839 if (!loosen_small_pack(pack_data
)) {
840 invalidate_pack_id(pack_id
);
845 close(pack_data
->pack_fd
);
846 idx_name
= keep_pack(create_index());
848 /* Register the packfile with core git's machinery. */
849 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
851 die("core git rejected index %s", idx_name
);
852 all_packs
[pack_id
] = new_p
;
853 install_packed_git(the_repository
, new_p
);
856 /* Print the boundary */
858 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
859 for (i
= 0; i
< branch_table_sz
; i
++) {
860 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
861 if (b
->pack_id
== pack_id
)
862 fprintf(pack_edges
, " %s",
863 oid_to_hex(&b
->oid
));
866 for (t
= first_tag
; t
; t
= t
->next_tag
) {
867 if (t
->pack_id
== pack_id
)
868 fprintf(pack_edges
, " %s",
869 oid_to_hex(&t
->oid
));
871 fputc('\n', pack_edges
);
879 close(pack_data
->pack_fd
);
880 unlink_or_warn(pack_data
->pack_name
);
882 FREE_AND_NULL(pack_data
);
885 /* We can't carry a delta across packfiles. */
886 strbuf_release(&last_blob
.data
);
887 last_blob
.offset
= 0;
891 static void cycle_packfile(void)
897 static int store_object(
898 enum object_type type
,
900 struct last_object
*last
,
901 struct object_id
*oidout
,
905 struct object_entry
*e
;
906 unsigned char hdr
[96];
907 struct object_id oid
;
908 unsigned long hdrlen
, deltalen
;
912 hdrlen
= xsnprintf((char *)hdr
, sizeof(hdr
), "%s %lu",
913 type_name(type
), (unsigned long)dat
->len
) + 1;
914 the_hash_algo
->init_fn(&c
);
915 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
916 the_hash_algo
->update_fn(&c
, dat
->buf
, dat
->len
);
917 the_hash_algo
->final_fn(oid
.hash
, &c
);
919 oidcpy(oidout
, &oid
);
921 e
= insert_object(&oid
);
923 insert_mark(mark
, e
);
925 duplicate_count_by_type
[type
]++;
927 } else if (find_sha1_pack(oid
.hash
,
928 get_all_packs(the_repository
))) {
930 e
->pack_id
= MAX_PACK_ID
;
931 e
->idx
.offset
= 1; /* just not zero! */
932 duplicate_count_by_type
[type
]++;
936 if (last
&& last
->data
.len
&& last
->data
.buf
&& last
->depth
< max_depth
937 && dat
->len
> the_hash_algo
->rawsz
) {
939 delta_count_attempts_by_type
[type
]++;
940 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
942 &deltalen
, dat
->len
- the_hash_algo
->rawsz
);
946 git_deflate_init(&s
, pack_compression_level
);
949 s
.avail_in
= deltalen
;
951 s
.next_in
= (void *)dat
->buf
;
952 s
.avail_in
= dat
->len
;
954 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
955 s
.next_out
= out
= xmalloc(s
.avail_out
);
956 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
960 /* Determine if we should auto-checkpoint. */
962 && (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) > max_packsize
)
963 || (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) < pack_size
) {
965 /* This new object needs to *not* have the current pack_id. */
966 e
->pack_id
= pack_id
+ 1;
969 /* We cannot carry a delta into the new pack. */
971 FREE_AND_NULL(delta
);
973 git_deflate_init(&s
, pack_compression_level
);
974 s
.next_in
= (void *)dat
->buf
;
975 s
.avail_in
= dat
->len
;
976 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
977 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
978 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
985 e
->pack_id
= pack_id
;
986 e
->idx
.offset
= pack_size
;
988 object_count_by_type
[type
]++;
990 crc32_begin(pack_file
);
993 off_t ofs
= e
->idx
.offset
- last
->offset
;
994 unsigned pos
= sizeof(hdr
) - 1;
996 delta_count_by_type
[type
]++;
997 e
->depth
= last
->depth
+ 1;
999 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1000 OBJ_OFS_DELTA
, deltalen
);
1001 hashwrite(pack_file
, hdr
, hdrlen
);
1002 pack_size
+= hdrlen
;
1004 hdr
[pos
] = ofs
& 127;
1006 hdr
[--pos
] = 128 | (--ofs
& 127);
1007 hashwrite(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
1008 pack_size
+= sizeof(hdr
) - pos
;
1011 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1013 hashwrite(pack_file
, hdr
, hdrlen
);
1014 pack_size
+= hdrlen
;
1017 hashwrite(pack_file
, out
, s
.total_out
);
1018 pack_size
+= s
.total_out
;
1020 e
->idx
.crc32
= crc32_end(pack_file
);
1025 if (last
->no_swap
) {
1028 strbuf_swap(&last
->data
, dat
);
1030 last
->offset
= e
->idx
.offset
;
1031 last
->depth
= e
->depth
;
1036 static void truncate_pack(struct hashfile_checkpoint
*checkpoint
)
1038 if (hashfile_truncate(pack_file
, checkpoint
))
1039 die_errno("cannot truncate pack to skip duplicate");
1040 pack_size
= checkpoint
->offset
;
1043 static void stream_blob(uintmax_t len
, struct object_id
*oidout
, uintmax_t mark
)
1045 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1046 unsigned char *in_buf
= xmalloc(in_sz
);
1047 unsigned char *out_buf
= xmalloc(out_sz
);
1048 struct object_entry
*e
;
1049 struct object_id oid
;
1050 unsigned long hdrlen
;
1054 struct hashfile_checkpoint checkpoint
;
1057 /* Determine if we should auto-checkpoint. */
1059 && (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) > max_packsize
)
1060 || (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) < pack_size
)
1063 hashfile_checkpoint(pack_file
, &checkpoint
);
1064 offset
= checkpoint
.offset
;
1066 hdrlen
= xsnprintf((char *)out_buf
, out_sz
, "blob %" PRIuMAX
, len
) + 1;
1068 the_hash_algo
->init_fn(&c
);
1069 the_hash_algo
->update_fn(&c
, out_buf
, hdrlen
);
1071 crc32_begin(pack_file
);
1073 git_deflate_init(&s
, pack_compression_level
);
1075 hdrlen
= encode_in_pack_object_header(out_buf
, out_sz
, OBJ_BLOB
, len
);
1077 s
.next_out
= out_buf
+ hdrlen
;
1078 s
.avail_out
= out_sz
- hdrlen
;
1080 while (status
!= Z_STREAM_END
) {
1081 if (0 < len
&& !s
.avail_in
) {
1082 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1083 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1084 if (!n
&& feof(stdin
))
1085 die("EOF in data (%" PRIuMAX
" bytes remaining)", len
);
1087 the_hash_algo
->update_fn(&c
, in_buf
, n
);
1093 status
= git_deflate(&s
, len
? 0 : Z_FINISH
);
1095 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1096 size_t n
= s
.next_out
- out_buf
;
1097 hashwrite(pack_file
, out_buf
, n
);
1099 s
.next_out
= out_buf
;
1100 s
.avail_out
= out_sz
;
1109 die("unexpected deflate failure: %d", status
);
1112 git_deflate_end(&s
);
1113 the_hash_algo
->final_fn(oid
.hash
, &c
);
1116 oidcpy(oidout
, &oid
);
1118 e
= insert_object(&oid
);
1121 insert_mark(mark
, e
);
1123 if (e
->idx
.offset
) {
1124 duplicate_count_by_type
[OBJ_BLOB
]++;
1125 truncate_pack(&checkpoint
);
1127 } else if (find_sha1_pack(oid
.hash
,
1128 get_all_packs(the_repository
))) {
1130 e
->pack_id
= MAX_PACK_ID
;
1131 e
->idx
.offset
= 1; /* just not zero! */
1132 duplicate_count_by_type
[OBJ_BLOB
]++;
1133 truncate_pack(&checkpoint
);
1138 e
->pack_id
= pack_id
;
1139 e
->idx
.offset
= offset
;
1140 e
->idx
.crc32
= crc32_end(pack_file
);
1142 object_count_by_type
[OBJ_BLOB
]++;
1149 /* All calls must be guarded by find_object() or find_mark() to
1150 * ensure the 'struct object_entry' passed was written by this
1151 * process instance. We unpack the entry by the offset, avoiding
1152 * the need for the corresponding .idx file. This unpacking rule
1153 * works because we only use OBJ_REF_DELTA within the packfiles
1154 * created by fast-import.
1156 * oe must not be NULL. Such an oe usually comes from giving
1157 * an unknown SHA-1 to find_object() or an undefined mark to
1158 * find_mark(). Callers must test for this condition and use
1159 * the standard read_sha1_file() when it happens.
1161 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1162 * find_mark(), where the mark was reloaded from an existing marks
1163 * file and is referencing an object that this fast-import process
1164 * instance did not write out to a packfile. Callers must test for
1165 * this condition and use read_sha1_file() instead.
1167 static void *gfi_unpack_entry(
1168 struct object_entry
*oe
,
1169 unsigned long *sizep
)
1171 enum object_type type
;
1172 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1173 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ the_hash_algo
->rawsz
)) {
1174 /* The object is stored in the packfile we are writing to
1175 * and we have modified it since the last time we scanned
1176 * back to read a previously written object. If an old
1177 * window covered [p->pack_size, p->pack_size + rawsz) its
1178 * data is stale and is not valid. Closing all windows
1179 * and updating the packfile length ensures we can read
1180 * the newly written data.
1182 close_pack_windows(p
);
1183 hashflush(pack_file
);
1185 /* We have to offer rawsz bytes additional on the end of
1186 * the packfile as the core unpacker code assumes the
1187 * footer is present at the file end and must promise
1188 * at least rawsz bytes within any window it maps. But
1189 * we don't actually create the footer here.
1191 p
->pack_size
= pack_size
+ the_hash_algo
->rawsz
;
1193 return unpack_entry(the_repository
, p
, oe
->idx
.offset
, &type
, sizep
);
1196 static const char *get_mode(const char *str
, uint16_t *modep
)
1201 while ((c
= *str
++) != ' ') {
1202 if (c
< '0' || c
> '7')
1204 mode
= (mode
<< 3) + (c
- '0');
1210 static void load_tree(struct tree_entry
*root
)
1212 struct object_id
*oid
= &root
->versions
[1].oid
;
1213 struct object_entry
*myoe
;
1214 struct tree_content
*t
;
1219 root
->tree
= t
= new_tree_content(8);
1220 if (is_null_oid(oid
))
1223 myoe
= find_object(oid
);
1224 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1225 if (myoe
->type
!= OBJ_TREE
)
1226 die("Not a tree: %s", oid_to_hex(oid
));
1227 t
->delta_depth
= myoe
->depth
;
1228 buf
= gfi_unpack_entry(myoe
, &size
);
1230 die("Can't load tree %s", oid_to_hex(oid
));
1232 enum object_type type
;
1233 buf
= read_object_file(oid
, &type
, &size
);
1234 if (!buf
|| type
!= OBJ_TREE
)
1235 die("Can't load tree %s", oid_to_hex(oid
));
1239 while (c
!= (buf
+ size
)) {
1240 struct tree_entry
*e
= new_tree_entry();
1242 if (t
->entry_count
== t
->entry_capacity
)
1243 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1244 t
->entries
[t
->entry_count
++] = e
;
1247 c
= get_mode(c
, &e
->versions
[1].mode
);
1249 die("Corrupt mode in %s", oid_to_hex(oid
));
1250 e
->versions
[0].mode
= e
->versions
[1].mode
;
1251 e
->name
= to_atom(c
, strlen(c
));
1252 c
+= e
->name
->str_len
+ 1;
1253 hashcpy(e
->versions
[0].oid
.hash
, (unsigned char *)c
);
1254 hashcpy(e
->versions
[1].oid
.hash
, (unsigned char *)c
);
1255 c
+= the_hash_algo
->rawsz
;
1260 static int tecmp0 (const void *_a
, const void *_b
)
1262 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1263 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1264 return base_name_compare(
1265 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1266 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1269 static int tecmp1 (const void *_a
, const void *_b
)
1271 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1272 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1273 return base_name_compare(
1274 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1275 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1278 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1284 QSORT(t
->entries
, t
->entry_count
, tecmp0
);
1286 QSORT(t
->entries
, t
->entry_count
, tecmp1
);
1288 for (i
= 0; i
< t
->entry_count
; i
++) {
1289 if (t
->entries
[i
]->versions
[v
].mode
)
1290 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1294 strbuf_grow(b
, maxlen
);
1295 for (i
= 0; i
< t
->entry_count
; i
++) {
1296 struct tree_entry
*e
= t
->entries
[i
];
1297 if (!e
->versions
[v
].mode
)
1299 strbuf_addf(b
, "%o %s%c",
1300 (unsigned int)(e
->versions
[v
].mode
& ~NO_DELTA
),
1301 e
->name
->str_dat
, '\0');
1302 strbuf_add(b
, e
->versions
[v
].oid
.hash
, the_hash_algo
->rawsz
);
1306 static void store_tree(struct tree_entry
*root
)
1308 struct tree_content
*t
;
1309 unsigned int i
, j
, del
;
1310 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1311 struct object_entry
*le
= NULL
;
1313 if (!is_null_oid(&root
->versions
[1].oid
))
1320 for (i
= 0; i
< t
->entry_count
; i
++) {
1321 if (t
->entries
[i
]->tree
)
1322 store_tree(t
->entries
[i
]);
1325 if (!(root
->versions
[0].mode
& NO_DELTA
))
1326 le
= find_object(&root
->versions
[0].oid
);
1327 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1328 mktree(t
, 0, &old_tree
);
1330 lo
.offset
= le
->idx
.offset
;
1331 lo
.depth
= t
->delta_depth
;
1334 mktree(t
, 1, &new_tree
);
1335 store_object(OBJ_TREE
, &new_tree
, &lo
, &root
->versions
[1].oid
, 0);
1337 t
->delta_depth
= lo
.depth
;
1338 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1339 struct tree_entry
*e
= t
->entries
[i
];
1340 if (e
->versions
[1].mode
) {
1341 e
->versions
[0].mode
= e
->versions
[1].mode
;
1342 oidcpy(&e
->versions
[0].oid
, &e
->versions
[1].oid
);
1343 t
->entries
[j
++] = e
;
1345 release_tree_entry(e
);
1349 t
->entry_count
-= del
;
1352 static void tree_content_replace(
1353 struct tree_entry
*root
,
1354 const struct object_id
*oid
,
1355 const uint16_t mode
,
1356 struct tree_content
*newtree
)
1359 die("Root cannot be a non-directory");
1360 oidclr(&root
->versions
[0].oid
);
1361 oidcpy(&root
->versions
[1].oid
, oid
);
1363 release_tree_content_recursive(root
->tree
);
1364 root
->tree
= newtree
;
1367 static int tree_content_set(
1368 struct tree_entry
*root
,
1370 const struct object_id
*oid
,
1371 const uint16_t mode
,
1372 struct tree_content
*subtree
)
1374 struct tree_content
*t
;
1377 struct tree_entry
*e
;
1379 slash1
= strchrnul(p
, '/');
1382 die("Empty path component found in input");
1383 if (!*slash1
&& !S_ISDIR(mode
) && subtree
)
1384 die("Non-directories cannot have subtrees");
1389 for (i
= 0; i
< t
->entry_count
; i
++) {
1391 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1394 && e
->versions
[1].mode
== mode
1395 && oideq(&e
->versions
[1].oid
, oid
))
1397 e
->versions
[1].mode
= mode
;
1398 oidcpy(&e
->versions
[1].oid
, oid
);
1400 release_tree_content_recursive(e
->tree
);
1404 * We need to leave e->versions[0].sha1 alone
1405 * to avoid modifying the preimage tree used
1406 * when writing out the parent directory.
1407 * But after replacing the subdir with a
1408 * completely different one, it's not a good
1409 * delta base any more, and besides, we've
1410 * thrown away the tree entries needed to
1411 * make a delta against it.
1413 * So let's just explicitly disable deltas
1416 if (S_ISDIR(e
->versions
[0].mode
))
1417 e
->versions
[0].mode
|= NO_DELTA
;
1419 oidclr(&root
->versions
[1].oid
);
1422 if (!S_ISDIR(e
->versions
[1].mode
)) {
1423 e
->tree
= new_tree_content(8);
1424 e
->versions
[1].mode
= S_IFDIR
;
1428 if (tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
)) {
1429 oidclr(&root
->versions
[1].oid
);
1436 if (t
->entry_count
== t
->entry_capacity
)
1437 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1438 e
= new_tree_entry();
1439 e
->name
= to_atom(p
, n
);
1440 e
->versions
[0].mode
= 0;
1441 oidclr(&e
->versions
[0].oid
);
1442 t
->entries
[t
->entry_count
++] = e
;
1444 e
->tree
= new_tree_content(8);
1445 e
->versions
[1].mode
= S_IFDIR
;
1446 tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
);
1449 e
->versions
[1].mode
= mode
;
1450 oidcpy(&e
->versions
[1].oid
, oid
);
1452 oidclr(&root
->versions
[1].oid
);
1456 static int tree_content_remove(
1457 struct tree_entry
*root
,
1459 struct tree_entry
*backup_leaf
,
1462 struct tree_content
*t
;
1465 struct tree_entry
*e
;
1467 slash1
= strchrnul(p
, '/');
1473 if (!*p
&& allow_root
) {
1479 for (i
= 0; i
< t
->entry_count
; i
++) {
1481 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1482 if (*slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1484 * If p names a file in some subdirectory, and a
1485 * file or symlink matching the name of the
1486 * parent directory of p exists, then p cannot
1487 * exist and need not be deleted.
1490 if (!*slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1494 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
, 0)) {
1495 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1496 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1497 oidclr(&root
->versions
[1].oid
);
1511 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1513 release_tree_content_recursive(e
->tree
);
1515 e
->versions
[1].mode
= 0;
1516 oidclr(&e
->versions
[1].oid
);
1517 oidclr(&root
->versions
[1].oid
);
1521 static int tree_content_get(
1522 struct tree_entry
*root
,
1524 struct tree_entry
*leaf
,
1527 struct tree_content
*t
;
1530 struct tree_entry
*e
;
1532 slash1
= strchrnul(p
, '/');
1534 if (!n
&& !allow_root
)
1535 die("Empty path component found in input");
1546 for (i
= 0; i
< t
->entry_count
; i
++) {
1548 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1551 if (!S_ISDIR(e
->versions
[1].mode
))
1555 return tree_content_get(e
, slash1
+ 1, leaf
, 0);
1561 memcpy(leaf
, e
, sizeof(*leaf
));
1562 if (e
->tree
&& is_null_oid(&e
->versions
[1].oid
))
1563 leaf
->tree
= dup_tree_content(e
->tree
);
1569 static int update_branch(struct branch
*b
)
1571 static const char *msg
= "fast-import";
1572 struct ref_transaction
*transaction
;
1573 struct object_id old_oid
;
1574 struct strbuf err
= STRBUF_INIT
;
1576 if (is_null_oid(&b
->oid
)) {
1578 delete_ref(NULL
, b
->name
, NULL
, 0);
1581 if (read_ref(b
->name
, &old_oid
))
1583 if (!force_update
&& !is_null_oid(&old_oid
)) {
1584 struct commit
*old_cmit
, *new_cmit
;
1586 old_cmit
= lookup_commit_reference_gently(the_repository
,
1588 new_cmit
= lookup_commit_reference_gently(the_repository
,
1590 if (!old_cmit
|| !new_cmit
)
1591 return error("Branch %s is missing commits.", b
->name
);
1593 if (!in_merge_bases(old_cmit
, new_cmit
)) {
1594 warning("Not updating %s"
1595 " (new tip %s does not contain %s)",
1596 b
->name
, oid_to_hex(&b
->oid
),
1597 oid_to_hex(&old_oid
));
1601 transaction
= ref_transaction_begin(&err
);
1603 ref_transaction_update(transaction
, b
->name
, &b
->oid
, &old_oid
,
1605 ref_transaction_commit(transaction
, &err
)) {
1606 ref_transaction_free(transaction
);
1607 error("%s", err
.buf
);
1608 strbuf_release(&err
);
1611 ref_transaction_free(transaction
);
1612 strbuf_release(&err
);
1616 static void dump_branches(void)
1621 for (i
= 0; i
< branch_table_sz
; i
++) {
1622 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1623 failure
|= update_branch(b
);
1627 static void dump_tags(void)
1629 static const char *msg
= "fast-import";
1631 struct strbuf ref_name
= STRBUF_INIT
;
1632 struct strbuf err
= STRBUF_INIT
;
1633 struct ref_transaction
*transaction
;
1635 transaction
= ref_transaction_begin(&err
);
1637 failure
|= error("%s", err
.buf
);
1640 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1641 strbuf_reset(&ref_name
);
1642 strbuf_addf(&ref_name
, "refs/tags/%s", t
->name
);
1644 if (ref_transaction_update(transaction
, ref_name
.buf
,
1645 &t
->oid
, NULL
, 0, msg
, &err
)) {
1646 failure
|= error("%s", err
.buf
);
1650 if (ref_transaction_commit(transaction
, &err
))
1651 failure
|= error("%s", err
.buf
);
1654 ref_transaction_free(transaction
);
1655 strbuf_release(&ref_name
);
1656 strbuf_release(&err
);
1659 static void dump_marks_helper(FILE *f
,
1665 for (k
= 0; k
< 1024; k
++) {
1666 if (m
->data
.sets
[k
])
1667 dump_marks_helper(f
, base
+ (k
<< m
->shift
),
1671 for (k
= 0; k
< 1024; k
++) {
1672 if (m
->data
.marked
[k
])
1673 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1674 oid_to_hex(&m
->data
.marked
[k
]->idx
.oid
));
1679 static void dump_marks(void)
1681 struct lock_file mark_lock
= LOCK_INIT
;
1684 if (!export_marks_file
|| (import_marks_file
&& !import_marks_file_done
))
1687 if (safe_create_leading_directories_const(export_marks_file
)) {
1688 failure
|= error_errno("unable to create leading directories of %s",
1693 if (hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0) < 0) {
1694 failure
|= error_errno("Unable to write marks file %s",
1699 f
= fdopen_lock_file(&mark_lock
, "w");
1701 int saved_errno
= errno
;
1702 rollback_lock_file(&mark_lock
);
1703 failure
|= error("Unable to write marks file %s: %s",
1704 export_marks_file
, strerror(saved_errno
));
1708 dump_marks_helper(f
, 0, marks
);
1709 if (commit_lock_file(&mark_lock
)) {
1710 failure
|= error_errno("Unable to write file %s",
1716 static void read_marks(void)
1719 FILE *f
= fopen(import_marks_file
, "r");
1722 else if (import_marks_file_ignore_missing
&& errno
== ENOENT
)
1723 goto done
; /* Marks file does not exist */
1725 die_errno("cannot read '%s'", import_marks_file
);
1726 while (fgets(line
, sizeof(line
), f
)) {
1729 struct object_id oid
;
1730 struct object_entry
*e
;
1732 end
= strchr(line
, '\n');
1733 if (line
[0] != ':' || !end
)
1734 die("corrupt mark line: %s", line
);
1736 mark
= strtoumax(line
+ 1, &end
, 10);
1737 if (!mark
|| end
== line
+ 1
1738 || *end
!= ' ' || get_oid_hex(end
+ 1, &oid
))
1739 die("corrupt mark line: %s", line
);
1740 e
= find_object(&oid
);
1742 enum object_type type
= oid_object_info(the_repository
,
1745 die("object not found: %s", oid_to_hex(&oid
));
1746 e
= insert_object(&oid
);
1748 e
->pack_id
= MAX_PACK_ID
;
1749 e
->idx
.offset
= 1; /* just not zero! */
1751 insert_mark(mark
, e
);
1755 import_marks_file_done
= 1;
1759 static int read_next_command(void)
1761 static int stdin_eof
= 0;
1764 unread_command_buf
= 0;
1769 if (unread_command_buf
) {
1770 unread_command_buf
= 0;
1772 struct recent_command
*rc
;
1774 stdin_eof
= strbuf_getline_lf(&command_buf
, stdin
);
1778 if (!seen_data_command
1779 && !starts_with(command_buf
.buf
, "feature ")
1780 && !starts_with(command_buf
.buf
, "option ")) {
1789 cmd_hist
.next
= rc
->next
;
1790 cmd_hist
.next
->prev
= &cmd_hist
;
1794 rc
->buf
= xstrdup(command_buf
.buf
);
1795 rc
->prev
= cmd_tail
;
1796 rc
->next
= cmd_hist
.prev
;
1797 rc
->prev
->next
= rc
;
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
);
1844 if (strbuf_getline_lf(&command_buf
, stdin
) == EOF
)
1845 die("EOF in data (terminator '%s' not found)", term
);
1846 if (term_len
== command_buf
.len
1847 && !strcmp(term
, command_buf
.buf
))
1849 strbuf_addbuf(sb
, &command_buf
);
1850 strbuf_addch(sb
, '\n');
1855 uintmax_t len
= strtoumax(data
, NULL
, 10);
1856 size_t n
= 0, length
= (size_t)len
;
1858 if (limit
&& limit
< len
) {
1863 die("data is too large to use in this context");
1865 while (n
< length
) {
1866 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1867 if (!s
&& feof(stdin
))
1868 die("EOF in data (%lu bytes remaining)",
1869 (unsigned long)(length
- n
));
1878 static int validate_raw_date(const char *src
, struct strbuf
*result
, int strict
)
1880 const char *orig_src
= src
;
1886 num
= strtoul(src
, &endp
, 10);
1888 * NEEDSWORK: perhaps check for reasonable values? For example, we
1889 * could error on values representing times more than a
1890 * day in the future.
1892 if (errno
|| endp
== src
|| *endp
!= ' ')
1896 if (*src
!= '-' && *src
!= '+')
1899 num
= strtoul(src
+ 1, &endp
, 10);
1901 * NEEDSWORK: check for brokenness other than num > 1400, such as
1902 * (num % 100) >= 60, or ((num % 100) % 15) != 0 ?
1904 if (errno
|| endp
== src
+ 1 || *endp
|| /* did not parse */
1905 (strict
&& (1400 < num
)) /* parsed a broken timezone */
1909 strbuf_addstr(result
, orig_src
);
1913 static char *parse_ident(const char *buf
)
1917 struct strbuf ident
= STRBUF_INIT
;
1919 /* ensure there is a space delimiter even if there is no name */
1923 ltgt
= buf
+ strcspn(buf
, "<>");
1925 die("Missing < in ident string: %s", buf
);
1926 if (ltgt
!= buf
&& ltgt
[-1] != ' ')
1927 die("Missing space before < in ident string: %s", buf
);
1928 ltgt
= ltgt
+ 1 + strcspn(ltgt
+ 1, "<>");
1930 die("Missing > in ident string: %s", buf
);
1933 die("Missing space after > in ident string: %s", buf
);
1935 name_len
= ltgt
- buf
;
1936 strbuf_add(&ident
, buf
, name_len
);
1940 if (validate_raw_date(ltgt
, &ident
, 1) < 0)
1941 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
1943 case WHENSPEC_RAW_PERMISSIVE
:
1944 if (validate_raw_date(ltgt
, &ident
, 0) < 0)
1945 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
1947 case WHENSPEC_RFC2822
:
1948 if (parse_date(ltgt
, &ident
) < 0)
1949 die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt
, buf
);
1952 if (strcmp("now", ltgt
))
1953 die("Date in ident must be 'now': %s", buf
);
1958 return strbuf_detach(&ident
, NULL
);
1961 static void parse_and_store_blob(
1962 struct last_object
*last
,
1963 struct object_id
*oidout
,
1966 static struct strbuf buf
= STRBUF_INIT
;
1969 if (parse_data(&buf
, big_file_threshold
, &len
))
1970 store_object(OBJ_BLOB
, &buf
, last
, oidout
, mark
);
1973 strbuf_release(&last
->data
);
1977 stream_blob(len
, oidout
, mark
);
1982 static void parse_new_blob(void)
1984 read_next_command();
1986 parse_original_identifier();
1987 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
1990 static void unload_one_branch(void)
1992 while (cur_active_branches
1993 && cur_active_branches
>= max_active_branches
) {
1994 uintmax_t min_commit
= ULONG_MAX
;
1995 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1997 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1998 if (e
->last_commit
< min_commit
) {
2000 min_commit
= e
->last_commit
;
2006 e
= p
->active_next_branch
;
2007 p
->active_next_branch
= e
->active_next_branch
;
2009 e
= active_branches
;
2010 active_branches
= e
->active_next_branch
;
2013 e
->active_next_branch
= NULL
;
2014 if (e
->branch_tree
.tree
) {
2015 release_tree_content_recursive(e
->branch_tree
.tree
);
2016 e
->branch_tree
.tree
= NULL
;
2018 cur_active_branches
--;
2022 static void load_branch(struct branch
*b
)
2024 load_tree(&b
->branch_tree
);
2027 b
->active_next_branch
= active_branches
;
2028 active_branches
= b
;
2029 cur_active_branches
++;
2030 branch_load_count
++;
2034 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2036 unsigned char fanout
= 0;
2037 while ((num_notes
>>= 8))
2042 static void construct_path_with_fanout(const char *hex_sha1
,
2043 unsigned char fanout
, char *path
)
2045 unsigned int i
= 0, j
= 0;
2046 if (fanout
>= the_hash_algo
->rawsz
)
2047 die("Too large fanout (%u)", fanout
);
2049 path
[i
++] = hex_sha1
[j
++];
2050 path
[i
++] = hex_sha1
[j
++];
2054 memcpy(path
+ i
, hex_sha1
+ j
, the_hash_algo
->hexsz
- j
);
2055 path
[i
+ the_hash_algo
->hexsz
- j
] = '\0';
2058 static uintmax_t do_change_note_fanout(
2059 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2060 char *hex_oid
, unsigned int hex_oid_len
,
2061 char *fullpath
, unsigned int fullpath_len
,
2062 unsigned char fanout
)
2064 struct tree_content
*t
;
2065 struct tree_entry
*e
, leaf
;
2066 unsigned int i
, tmp_hex_oid_len
, tmp_fullpath_len
;
2067 uintmax_t num_notes
= 0;
2068 struct object_id oid
;
2069 /* hex oid + '/' between each pair of hex digits + NUL */
2070 char realpath
[GIT_MAX_HEXSZ
+ ((GIT_MAX_HEXSZ
/ 2) - 1) + 1];
2071 const unsigned hexsz
= the_hash_algo
->hexsz
;
2077 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2079 tmp_hex_oid_len
= hex_oid_len
+ e
->name
->str_len
;
2080 tmp_fullpath_len
= fullpath_len
;
2083 * We're interested in EITHER existing note entries (entries
2084 * with exactly 40 hex chars in path, not including directory
2085 * separators), OR directory entries that may contain note
2086 * entries (with < 40 hex chars in path).
2087 * Also, each path component in a note entry must be a multiple
2090 if (!e
->versions
[1].mode
||
2091 tmp_hex_oid_len
> hexsz
||
2092 e
->name
->str_len
% 2)
2095 /* This _may_ be a note entry, or a subdir containing notes */
2096 memcpy(hex_oid
+ hex_oid_len
, e
->name
->str_dat
,
2098 if (tmp_fullpath_len
)
2099 fullpath
[tmp_fullpath_len
++] = '/';
2100 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2102 tmp_fullpath_len
+= e
->name
->str_len
;
2103 fullpath
[tmp_fullpath_len
] = '\0';
2105 if (tmp_hex_oid_len
== hexsz
&& !get_oid_hex(hex_oid
, &oid
)) {
2106 /* This is a note entry */
2107 if (fanout
== 0xff) {
2108 /* Counting mode, no rename */
2112 construct_path_with_fanout(hex_oid
, fanout
, realpath
);
2113 if (!strcmp(fullpath
, realpath
)) {
2114 /* Note entry is in correct location */
2119 /* Rename fullpath to realpath */
2120 if (!tree_content_remove(orig_root
, fullpath
, &leaf
, 0))
2121 die("Failed to remove path %s", fullpath
);
2122 tree_content_set(orig_root
, realpath
,
2123 &leaf
.versions
[1].oid
,
2124 leaf
.versions
[1].mode
,
2126 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2127 /* This is a subdir that may contain note entries */
2128 num_notes
+= do_change_note_fanout(orig_root
, e
,
2129 hex_oid
, tmp_hex_oid_len
,
2130 fullpath
, tmp_fullpath_len
, fanout
);
2133 /* The above may have reallocated the current tree_content */
2139 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2140 unsigned char fanout
)
2143 * The size of path is due to one slash between every two hex digits,
2144 * plus the terminating NUL. Note that there is no slash at the end, so
2145 * the number of slashes is one less than half the number of hex
2148 char hex_oid
[GIT_MAX_HEXSZ
], path
[GIT_MAX_HEXSZ
+ (GIT_MAX_HEXSZ
/ 2) - 1 + 1];
2149 return do_change_note_fanout(root
, root
, hex_oid
, 0, path
, 0, fanout
);
2153 * Given a pointer into a string, parse a mark reference:
2155 * idnum ::= ':' bigint;
2157 * Return the first character after the value in *endptr.
2159 * Complain if the following character is not what is expected,
2160 * either a space or end of the string.
2162 static uintmax_t parse_mark_ref(const char *p
, char **endptr
)
2168 mark
= strtoumax(p
, endptr
, 10);
2170 die("No value after ':' in mark: %s", command_buf
.buf
);
2175 * Parse the mark reference, and complain if this is not the end of
2178 static uintmax_t parse_mark_ref_eol(const char *p
)
2183 mark
= parse_mark_ref(p
, &end
);
2185 die("Garbage after mark: %s", command_buf
.buf
);
2190 * Parse the mark reference, demanding a trailing space. Return a
2191 * pointer to the space.
2193 static uintmax_t parse_mark_ref_space(const char **p
)
2198 mark
= parse_mark_ref(*p
, &end
);
2200 die("Missing space after mark: %s", command_buf
.buf
);
2205 static void file_change_m(const char *p
, struct branch
*b
)
2207 static struct strbuf uq
= STRBUF_INIT
;
2209 struct object_entry
*oe
;
2210 struct object_id oid
;
2211 uint16_t mode
, inline_data
= 0;
2213 p
= get_mode(p
, &mode
);
2215 die("Corrupt mode: %s", command_buf
.buf
);
2220 case S_IFREG
| 0644:
2221 case S_IFREG
| 0755:
2228 die("Corrupt mode: %s", command_buf
.buf
);
2232 oe
= find_mark(parse_mark_ref_space(&p
));
2233 oidcpy(&oid
, &oe
->idx
.oid
);
2234 } else if (skip_prefix(p
, "inline ", &p
)) {
2236 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2238 if (parse_oid_hex(p
, &oid
, &p
))
2239 die("Invalid dataref: %s", command_buf
.buf
);
2240 oe
= find_object(&oid
);
2242 die("Missing space after SHA1: %s", command_buf
.buf
);
2246 if (!unquote_c_style(&uq
, p
, &endp
)) {
2248 die("Garbage after path in: %s", command_buf
.buf
);
2252 /* Git does not track empty, non-toplevel directories. */
2253 if (S_ISDIR(mode
) && is_empty_tree_oid(&oid
) && *p
) {
2254 tree_content_remove(&b
->branch_tree
, p
, NULL
, 0);
2258 if (S_ISGITLINK(mode
)) {
2260 die("Git links cannot be specified 'inline': %s",
2263 if (oe
->type
!= OBJ_COMMIT
)
2264 die("Not a commit (actually a %s): %s",
2265 type_name(oe
->type
), command_buf
.buf
);
2268 * Accept the sha1 without checking; it expected to be in
2269 * another repository.
2271 } else if (inline_data
) {
2273 die("Directories cannot be specified 'inline': %s",
2276 strbuf_addstr(&uq
, p
);
2279 while (read_next_command() != EOF
) {
2281 if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2284 parse_and_store_blob(&last_blob
, &oid
, 0);
2289 enum object_type expected
= S_ISDIR(mode
) ?
2291 enum object_type type
= oe
? oe
->type
:
2292 oid_object_info(the_repository
, &oid
,
2295 die("%s not found: %s",
2296 S_ISDIR(mode
) ? "Tree" : "Blob",
2298 if (type
!= expected
)
2299 die("Not a %s (actually a %s): %s",
2300 type_name(expected
), type_name(type
),
2305 tree_content_replace(&b
->branch_tree
, &oid
, mode
, NULL
);
2308 tree_content_set(&b
->branch_tree
, p
, &oid
, mode
, NULL
);
2311 static void file_change_d(const char *p
, struct branch
*b
)
2313 static struct strbuf uq
= STRBUF_INIT
;
2317 if (!unquote_c_style(&uq
, p
, &endp
)) {
2319 die("Garbage after path in: %s", command_buf
.buf
);
2322 tree_content_remove(&b
->branch_tree
, p
, NULL
, 1);
2325 static void file_change_cr(const char *s
, struct branch
*b
, int rename
)
2328 static struct strbuf s_uq
= STRBUF_INIT
;
2329 static struct strbuf d_uq
= STRBUF_INIT
;
2331 struct tree_entry leaf
;
2333 strbuf_reset(&s_uq
);
2334 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2336 die("Missing space after source: %s", command_buf
.buf
);
2338 endp
= strchr(s
, ' ');
2340 die("Missing space after source: %s", command_buf
.buf
);
2341 strbuf_add(&s_uq
, s
, endp
- s
);
2347 die("Missing dest: %s", command_buf
.buf
);
2350 strbuf_reset(&d_uq
);
2351 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2353 die("Garbage after dest in: %s", command_buf
.buf
);
2357 memset(&leaf
, 0, sizeof(leaf
));
2359 tree_content_remove(&b
->branch_tree
, s
, &leaf
, 1);
2361 tree_content_get(&b
->branch_tree
, s
, &leaf
, 1);
2362 if (!leaf
.versions
[1].mode
)
2363 die("Path %s not in branch", s
);
2364 if (!*d
) { /* C "path/to/subdir" "" */
2365 tree_content_replace(&b
->branch_tree
,
2366 &leaf
.versions
[1].oid
,
2367 leaf
.versions
[1].mode
,
2371 tree_content_set(&b
->branch_tree
, d
,
2372 &leaf
.versions
[1].oid
,
2373 leaf
.versions
[1].mode
,
2377 static void note_change_n(const char *p
, struct branch
*b
, unsigned char *old_fanout
)
2379 static struct strbuf uq
= STRBUF_INIT
;
2380 struct object_entry
*oe
;
2382 struct object_id oid
, commit_oid
;
2383 char path
[GIT_MAX_RAWSZ
* 3];
2384 uint16_t inline_data
= 0;
2385 unsigned char new_fanout
;
2388 * When loading a branch, we don't traverse its tree to count the real
2389 * number of notes (too expensive to do this for all non-note refs).
2390 * This means that recently loaded notes refs might incorrectly have
2391 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2393 * Fix this by traversing the tree and counting the number of notes
2394 * when b->num_notes == 0. If the notes tree is truly empty, the
2395 * calculation should not take long.
2397 if (b
->num_notes
== 0 && *old_fanout
== 0) {
2398 /* Invoke change_note_fanout() in "counting mode". */
2399 b
->num_notes
= change_note_fanout(&b
->branch_tree
, 0xff);
2400 *old_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2403 /* Now parse the notemodify command. */
2404 /* <dataref> or 'inline' */
2406 oe
= find_mark(parse_mark_ref_space(&p
));
2407 oidcpy(&oid
, &oe
->idx
.oid
);
2408 } else if (skip_prefix(p
, "inline ", &p
)) {
2410 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2412 if (parse_oid_hex(p
, &oid
, &p
))
2413 die("Invalid dataref: %s", command_buf
.buf
);
2414 oe
= find_object(&oid
);
2416 die("Missing space after SHA1: %s", command_buf
.buf
);
2420 s
= lookup_branch(p
);
2422 if (is_null_oid(&s
->oid
))
2423 die("Can't add a note on empty branch.");
2424 oidcpy(&commit_oid
, &s
->oid
);
2425 } else if (*p
== ':') {
2426 uintmax_t commit_mark
= parse_mark_ref_eol(p
);
2427 struct object_entry
*commit_oe
= find_mark(commit_mark
);
2428 if (commit_oe
->type
!= OBJ_COMMIT
)
2429 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2430 oidcpy(&commit_oid
, &commit_oe
->idx
.oid
);
2431 } else if (!get_oid(p
, &commit_oid
)) {
2433 char *buf
= read_object_with_reference(the_repository
,
2437 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2438 die("Not a valid commit: %s", p
);
2441 die("Invalid ref name or SHA1 expression: %s", p
);
2445 strbuf_addstr(&uq
, p
);
2448 read_next_command();
2449 parse_and_store_blob(&last_blob
, &oid
, 0);
2451 if (oe
->type
!= OBJ_BLOB
)
2452 die("Not a blob (actually a %s): %s",
2453 type_name(oe
->type
), command_buf
.buf
);
2454 } else if (!is_null_oid(&oid
)) {
2455 enum object_type type
= oid_object_info(the_repository
, &oid
,
2458 die("Blob not found: %s", command_buf
.buf
);
2459 if (type
!= OBJ_BLOB
)
2460 die("Not a blob (actually a %s): %s",
2461 type_name(type
), command_buf
.buf
);
2464 construct_path_with_fanout(oid_to_hex(&commit_oid
), *old_fanout
, path
);
2465 if (tree_content_remove(&b
->branch_tree
, path
, NULL
, 0))
2468 if (is_null_oid(&oid
))
2469 return; /* nothing to insert */
2472 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2473 construct_path_with_fanout(oid_to_hex(&commit_oid
), new_fanout
, path
);
2474 tree_content_set(&b
->branch_tree
, path
, &oid
, S_IFREG
| 0644, NULL
);
2477 static void file_change_deleteall(struct branch
*b
)
2479 release_tree_content_recursive(b
->branch_tree
.tree
);
2480 oidclr(&b
->branch_tree
.versions
[0].oid
);
2481 oidclr(&b
->branch_tree
.versions
[1].oid
);
2482 load_tree(&b
->branch_tree
);
2486 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2488 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2489 die("Not a valid commit: %s", oid_to_hex(&b
->oid
));
2490 if (memcmp("tree ", buf
, 5)
2491 || get_oid_hex(buf
+ 5, &b
->branch_tree
.versions
[1].oid
))
2492 die("The commit %s is corrupt", oid_to_hex(&b
->oid
));
2493 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2494 &b
->branch_tree
.versions
[1].oid
);
2497 static void parse_from_existing(struct branch
*b
)
2499 if (is_null_oid(&b
->oid
)) {
2500 oidclr(&b
->branch_tree
.versions
[0].oid
);
2501 oidclr(&b
->branch_tree
.versions
[1].oid
);
2506 buf
= read_object_with_reference(the_repository
,
2507 &b
->oid
, commit_type
, &size
,
2509 parse_from_commit(b
, buf
, size
);
2514 static int parse_objectish(struct branch
*b
, const char *objectish
)
2517 struct object_id oid
;
2519 oidcpy(&oid
, &b
->branch_tree
.versions
[1].oid
);
2521 s
= lookup_branch(objectish
);
2523 die("Can't create a branch from itself: %s", b
->name
);
2525 struct object_id
*t
= &s
->branch_tree
.versions
[1].oid
;
2526 oidcpy(&b
->oid
, &s
->oid
);
2527 oidcpy(&b
->branch_tree
.versions
[0].oid
, t
);
2528 oidcpy(&b
->branch_tree
.versions
[1].oid
, t
);
2529 } else if (*objectish
== ':') {
2530 uintmax_t idnum
= parse_mark_ref_eol(objectish
);
2531 struct object_entry
*oe
= find_mark(idnum
);
2532 if (oe
->type
!= OBJ_COMMIT
)
2533 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2534 if (!oideq(&b
->oid
, &oe
->idx
.oid
)) {
2535 oidcpy(&b
->oid
, &oe
->idx
.oid
);
2536 if (oe
->pack_id
!= MAX_PACK_ID
) {
2538 char *buf
= gfi_unpack_entry(oe
, &size
);
2539 parse_from_commit(b
, buf
, size
);
2542 parse_from_existing(b
);
2544 } else if (!get_oid(objectish
, &b
->oid
)) {
2545 parse_from_existing(b
);
2546 if (is_null_oid(&b
->oid
))
2550 die("Invalid ref name or SHA1 expression: %s", objectish
);
2552 if (b
->branch_tree
.tree
&& !oideq(&oid
, &b
->branch_tree
.versions
[1].oid
)) {
2553 release_tree_content_recursive(b
->branch_tree
.tree
);
2554 b
->branch_tree
.tree
= NULL
;
2557 read_next_command();
2561 static int parse_from(struct branch
*b
)
2565 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2568 return parse_objectish(b
, from
);
2571 static int parse_objectish_with_prefix(struct branch
*b
, const char *prefix
)
2575 if (!skip_prefix(command_buf
.buf
, prefix
, &base
))
2578 return parse_objectish(b
, base
);
2581 static struct hash_list
*parse_merge(unsigned int *count
)
2583 struct hash_list
*list
= NULL
, **tail
= &list
, *n
;
2588 while (skip_prefix(command_buf
.buf
, "merge ", &from
)) {
2589 n
= xmalloc(sizeof(*n
));
2590 s
= lookup_branch(from
);
2592 oidcpy(&n
->oid
, &s
->oid
);
2593 else if (*from
== ':') {
2594 uintmax_t idnum
= parse_mark_ref_eol(from
);
2595 struct object_entry
*oe
= find_mark(idnum
);
2596 if (oe
->type
!= OBJ_COMMIT
)
2597 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2598 oidcpy(&n
->oid
, &oe
->idx
.oid
);
2599 } else if (!get_oid(from
, &n
->oid
)) {
2601 char *buf
= read_object_with_reference(the_repository
,
2605 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2606 die("Not a valid commit: %s", from
);
2609 die("Invalid ref name or SHA1 expression: %s", from
);
2616 read_next_command();
2621 static void parse_new_commit(const char *arg
)
2623 static struct strbuf msg
= STRBUF_INIT
;
2625 char *author
= NULL
;
2626 char *committer
= NULL
;
2627 char *encoding
= NULL
;
2628 struct hash_list
*merge_list
= NULL
;
2629 unsigned int merge_count
;
2630 unsigned char prev_fanout
, new_fanout
;
2633 b
= lookup_branch(arg
);
2635 b
= new_branch(arg
);
2637 read_next_command();
2639 parse_original_identifier();
2640 if (skip_prefix(command_buf
.buf
, "author ", &v
)) {
2641 author
= parse_ident(v
);
2642 read_next_command();
2644 if (skip_prefix(command_buf
.buf
, "committer ", &v
)) {
2645 committer
= parse_ident(v
);
2646 read_next_command();
2649 die("Expected committer but didn't get one");
2650 if (skip_prefix(command_buf
.buf
, "encoding ", &v
)) {
2651 encoding
= xstrdup(v
);
2652 read_next_command();
2654 parse_data(&msg
, 0, NULL
);
2655 read_next_command();
2657 merge_list
= parse_merge(&merge_count
);
2659 /* ensure the branch is active/loaded */
2660 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2661 unload_one_branch();
2665 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2668 while (command_buf
.len
> 0) {
2669 if (skip_prefix(command_buf
.buf
, "M ", &v
))
2670 file_change_m(v
, b
);
2671 else if (skip_prefix(command_buf
.buf
, "D ", &v
))
2672 file_change_d(v
, b
);
2673 else if (skip_prefix(command_buf
.buf
, "R ", &v
))
2674 file_change_cr(v
, b
, 1);
2675 else if (skip_prefix(command_buf
.buf
, "C ", &v
))
2676 file_change_cr(v
, b
, 0);
2677 else if (skip_prefix(command_buf
.buf
, "N ", &v
))
2678 note_change_n(v
, b
, &prev_fanout
);
2679 else if (!strcmp("deleteall", command_buf
.buf
))
2680 file_change_deleteall(b
);
2681 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
2683 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2686 unread_command_buf
= 1;
2689 if (read_next_command() == EOF
)
2693 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2694 if (new_fanout
!= prev_fanout
)
2695 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2697 /* build the tree and the commit */
2698 store_tree(&b
->branch_tree
);
2699 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2700 &b
->branch_tree
.versions
[1].oid
);
2702 strbuf_reset(&new_data
);
2703 strbuf_addf(&new_data
, "tree %s\n",
2704 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
2705 if (!is_null_oid(&b
->oid
))
2706 strbuf_addf(&new_data
, "parent %s\n",
2707 oid_to_hex(&b
->oid
));
2708 while (merge_list
) {
2709 struct hash_list
*next
= merge_list
->next
;
2710 strbuf_addf(&new_data
, "parent %s\n",
2711 oid_to_hex(&merge_list
->oid
));
2715 strbuf_addf(&new_data
,
2718 author
? author
: committer
, committer
);
2720 strbuf_addf(&new_data
,
2723 strbuf_addch(&new_data
, '\n');
2724 strbuf_addbuf(&new_data
, &msg
);
2729 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, &b
->oid
, next_mark
))
2730 b
->pack_id
= pack_id
;
2731 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2734 static void parse_new_tag(const char *arg
)
2736 static struct strbuf msg
= STRBUF_INIT
;
2741 uintmax_t from_mark
= 0;
2742 struct object_id oid
;
2743 enum object_type type
;
2746 t
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct tag
));
2747 memset(t
, 0, sizeof(struct tag
));
2748 t
->name
= pool_strdup(arg
);
2750 last_tag
->next_tag
= t
;
2754 read_next_command();
2758 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2759 die("Expected from command, got %s", command_buf
.buf
);
2760 s
= lookup_branch(from
);
2762 if (is_null_oid(&s
->oid
))
2763 die("Can't tag an empty branch.");
2764 oidcpy(&oid
, &s
->oid
);
2766 } else if (*from
== ':') {
2767 struct object_entry
*oe
;
2768 from_mark
= parse_mark_ref_eol(from
);
2769 oe
= find_mark(from_mark
);
2771 oidcpy(&oid
, &oe
->idx
.oid
);
2772 } else if (!get_oid(from
, &oid
)) {
2773 struct object_entry
*oe
= find_object(&oid
);
2775 type
= oid_object_info(the_repository
, &oid
, NULL
);
2777 die("Not a valid object: %s", from
);
2781 die("Invalid ref name or SHA1 expression: %s", from
);
2782 read_next_command();
2784 /* original-oid ... */
2785 parse_original_identifier();
2788 if (skip_prefix(command_buf
.buf
, "tagger ", &v
)) {
2789 tagger
= parse_ident(v
);
2790 read_next_command();
2794 /* tag payload/message */
2795 parse_data(&msg
, 0, NULL
);
2797 /* build the tag object */
2798 strbuf_reset(&new_data
);
2800 strbuf_addf(&new_data
,
2804 oid_to_hex(&oid
), type_name(type
), t
->name
);
2806 strbuf_addf(&new_data
,
2807 "tagger %s\n", tagger
);
2808 strbuf_addch(&new_data
, '\n');
2809 strbuf_addbuf(&new_data
, &msg
);
2812 if (store_object(OBJ_TAG
, &new_data
, NULL
, &t
->oid
, next_mark
))
2813 t
->pack_id
= MAX_PACK_ID
;
2815 t
->pack_id
= pack_id
;
2818 static void parse_reset_branch(const char *arg
)
2821 const char *tag_name
;
2823 b
= lookup_branch(arg
);
2826 oidclr(&b
->branch_tree
.versions
[0].oid
);
2827 oidclr(&b
->branch_tree
.versions
[1].oid
);
2828 if (b
->branch_tree
.tree
) {
2829 release_tree_content_recursive(b
->branch_tree
.tree
);
2830 b
->branch_tree
.tree
= NULL
;
2834 b
= new_branch(arg
);
2835 read_next_command();
2837 if (b
->delete && skip_prefix(b
->name
, "refs/tags/", &tag_name
)) {
2839 * Elsewhere, we call dump_branches() before dump_tags(),
2840 * and dump_branches() will handle ref deletions first, so
2841 * in order to make sure the deletion actually takes effect,
2842 * we need to remove the tag from our list of tags to update.
2844 * NEEDSWORK: replace list of tags with hashmap for faster
2847 struct tag
*t
, *prev
= NULL
;
2848 for (t
= first_tag
; t
; t
= t
->next_tag
) {
2849 if (!strcmp(t
->name
, tag_name
))
2855 prev
->next_tag
= t
->next_tag
;
2857 first_tag
= t
->next_tag
;
2860 /* There is no mem_pool_free(t) function to call. */
2863 if (command_buf
.len
> 0)
2864 unread_command_buf
= 1;
2867 static void cat_blob_write(const char *buf
, unsigned long size
)
2869 if (write_in_full(cat_blob_fd
, buf
, size
) < 0)
2870 die_errno("Write to frontend failed");
2873 static void cat_blob(struct object_entry
*oe
, struct object_id
*oid
)
2875 struct strbuf line
= STRBUF_INIT
;
2877 enum object_type type
= 0;
2880 if (!oe
|| oe
->pack_id
== MAX_PACK_ID
) {
2881 buf
= read_object_file(oid
, &type
, &size
);
2884 buf
= gfi_unpack_entry(oe
, &size
);
2888 * Output based on batch_one_object() from cat-file.c.
2891 strbuf_reset(&line
);
2892 strbuf_addf(&line
, "%s missing\n", oid_to_hex(oid
));
2893 cat_blob_write(line
.buf
, line
.len
);
2894 strbuf_release(&line
);
2899 die("Can't read object %s", oid_to_hex(oid
));
2900 if (type
!= OBJ_BLOB
)
2901 die("Object %s is a %s but a blob was expected.",
2902 oid_to_hex(oid
), type_name(type
));
2903 strbuf_reset(&line
);
2904 strbuf_addf(&line
, "%s %s %"PRIuMAX
"\n", oid_to_hex(oid
),
2905 type_name(type
), (uintmax_t)size
);
2906 cat_blob_write(line
.buf
, line
.len
);
2907 strbuf_release(&line
);
2908 cat_blob_write(buf
, size
);
2909 cat_blob_write("\n", 1);
2910 if (oe
&& oe
->pack_id
== pack_id
) {
2911 last_blob
.offset
= oe
->idx
.offset
;
2912 strbuf_attach(&last_blob
.data
, buf
, size
, size
);
2913 last_blob
.depth
= oe
->depth
;
2918 static void parse_get_mark(const char *p
)
2920 struct object_entry
*oe
;
2921 char output
[GIT_MAX_HEXSZ
+ 2];
2923 /* get-mark SP <object> LF */
2925 die("Not a mark: %s", p
);
2927 oe
= find_mark(parse_mark_ref_eol(p
));
2929 die("Unknown mark: %s", command_buf
.buf
);
2931 xsnprintf(output
, sizeof(output
), "%s\n", oid_to_hex(&oe
->idx
.oid
));
2932 cat_blob_write(output
, the_hash_algo
->hexsz
+ 1);
2935 static void parse_cat_blob(const char *p
)
2937 struct object_entry
*oe
;
2938 struct object_id oid
;
2940 /* cat-blob SP <object> LF */
2942 oe
= find_mark(parse_mark_ref_eol(p
));
2944 die("Unknown mark: %s", command_buf
.buf
);
2945 oidcpy(&oid
, &oe
->idx
.oid
);
2947 if (parse_oid_hex(p
, &oid
, &p
))
2948 die("Invalid dataref: %s", command_buf
.buf
);
2950 die("Garbage after SHA1: %s", command_buf
.buf
);
2951 oe
= find_object(&oid
);
2957 static struct object_entry
*dereference(struct object_entry
*oe
,
2958 struct object_id
*oid
)
2962 const unsigned hexsz
= the_hash_algo
->hexsz
;
2965 enum object_type type
= oid_object_info(the_repository
, oid
,
2968 die("object not found: %s", oid_to_hex(oid
));
2970 oe
= insert_object(oid
);
2972 oe
->pack_id
= MAX_PACK_ID
;
2976 case OBJ_TREE
: /* easy case. */
2982 die("Not a tree-ish: %s", command_buf
.buf
);
2985 if (oe
->pack_id
!= MAX_PACK_ID
) { /* in a pack being written */
2986 buf
= gfi_unpack_entry(oe
, &size
);
2988 enum object_type unused
;
2989 buf
= read_object_file(oid
, &unused
, &size
);
2992 die("Can't load object %s", oid_to_hex(oid
));
2994 /* Peel one layer. */
2997 if (size
< hexsz
+ strlen("object ") ||
2998 get_oid_hex(buf
+ strlen("object "), oid
))
2999 die("Invalid SHA1 in tag: %s", command_buf
.buf
);
3002 if (size
< hexsz
+ strlen("tree ") ||
3003 get_oid_hex(buf
+ strlen("tree "), oid
))
3004 die("Invalid SHA1 in commit: %s", command_buf
.buf
);
3008 return find_object(oid
);
3011 static struct object_entry
*parse_treeish_dataref(const char **p
)
3013 struct object_id oid
;
3014 struct object_entry
*e
;
3016 if (**p
== ':') { /* <mark> */
3017 e
= find_mark(parse_mark_ref_space(p
));
3019 die("Unknown mark: %s", command_buf
.buf
);
3020 oidcpy(&oid
, &e
->idx
.oid
);
3021 } else { /* <sha1> */
3022 if (parse_oid_hex(*p
, &oid
, p
))
3023 die("Invalid dataref: %s", command_buf
.buf
);
3024 e
= find_object(&oid
);
3026 die("Missing space after tree-ish: %s", command_buf
.buf
);
3029 while (!e
|| e
->type
!= OBJ_TREE
)
3030 e
= dereference(e
, &oid
);
3034 static void print_ls(int mode
, const unsigned char *hash
, const char *path
)
3036 static struct strbuf line
= STRBUF_INIT
;
3038 /* See show_tree(). */
3040 S_ISGITLINK(mode
) ? commit_type
:
3041 S_ISDIR(mode
) ? tree_type
:
3045 /* missing SP path LF */
3046 strbuf_reset(&line
);
3047 strbuf_addstr(&line
, "missing ");
3048 quote_c_style(path
, &line
, NULL
, 0);
3049 strbuf_addch(&line
, '\n');
3051 /* mode SP type SP object_name TAB path LF */
3052 strbuf_reset(&line
);
3053 strbuf_addf(&line
, "%06o %s %s\t",
3054 mode
& ~NO_DELTA
, type
, hash_to_hex(hash
));
3055 quote_c_style(path
, &line
, NULL
, 0);
3056 strbuf_addch(&line
, '\n');
3058 cat_blob_write(line
.buf
, line
.len
);
3061 static void parse_ls(const char *p
, struct branch
*b
)
3063 struct tree_entry
*root
= NULL
;
3064 struct tree_entry leaf
= {NULL
};
3066 /* ls SP (<tree-ish> SP)? <path> */
3069 die("Not in a commit: %s", command_buf
.buf
);
3070 root
= &b
->branch_tree
;
3072 struct object_entry
*e
= parse_treeish_dataref(&p
);
3073 root
= new_tree_entry();
3074 oidcpy(&root
->versions
[1].oid
, &e
->idx
.oid
);
3075 if (!is_null_oid(&root
->versions
[1].oid
))
3076 root
->versions
[1].mode
= S_IFDIR
;
3080 static struct strbuf uq
= STRBUF_INIT
;
3083 if (unquote_c_style(&uq
, p
, &endp
))
3084 die("Invalid path: %s", command_buf
.buf
);
3086 die("Garbage after path in: %s", command_buf
.buf
);
3089 tree_content_get(root
, p
, &leaf
, 1);
3091 * A directory in preparation would have a sha1 of zero
3092 * until it is saved. Save, for simplicity.
3094 if (S_ISDIR(leaf
.versions
[1].mode
))
3097 print_ls(leaf
.versions
[1].mode
, leaf
.versions
[1].oid
.hash
, p
);
3099 release_tree_content_recursive(leaf
.tree
);
3100 if (!b
|| root
!= &b
->branch_tree
)
3101 release_tree_entry(root
);
3104 static void checkpoint(void)
3106 checkpoint_requested
= 0;
3115 static void parse_checkpoint(void)
3117 checkpoint_requested
= 1;
3121 static void parse_progress(void)
3123 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
3124 fputc('\n', stdout
);
3129 static void parse_alias(void)
3131 struct object_entry
*e
;
3135 read_next_command();
3140 die(_("Expected 'mark' command, got %s"), command_buf
.buf
);
3143 memset(&b
, 0, sizeof(b
));
3144 if (!parse_objectish_with_prefix(&b
, "to "))
3145 die(_("Expected 'to' command, got %s"), command_buf
.buf
);
3146 e
= find_object(&b
.oid
);
3148 insert_mark(next_mark
, e
);
3151 static char* make_fast_import_path(const char *path
)
3153 if (!relative_marks_paths
|| is_absolute_path(path
))
3154 return xstrdup(path
);
3155 return git_pathdup("info/fast-import/%s", path
);
3158 static void option_import_marks(const char *marks
,
3159 int from_stream
, int ignore_missing
)
3161 if (import_marks_file
) {
3163 die("Only one import-marks command allowed per stream");
3165 /* read previous mark file */
3166 if(!import_marks_file_from_stream
)
3170 import_marks_file
= make_fast_import_path(marks
);
3171 import_marks_file_from_stream
= from_stream
;
3172 import_marks_file_ignore_missing
= ignore_missing
;
3175 static void option_date_format(const char *fmt
)
3177 if (!strcmp(fmt
, "raw"))
3178 whenspec
= WHENSPEC_RAW
;
3179 else if (!strcmp(fmt
, "raw-permissive"))
3180 whenspec
= WHENSPEC_RAW_PERMISSIVE
;
3181 else if (!strcmp(fmt
, "rfc2822"))
3182 whenspec
= WHENSPEC_RFC2822
;
3183 else if (!strcmp(fmt
, "now"))
3184 whenspec
= WHENSPEC_NOW
;
3186 die("unknown --date-format argument %s", fmt
);
3189 static unsigned long ulong_arg(const char *option
, const char *arg
)
3192 unsigned long rv
= strtoul(arg
, &endptr
, 0);
3193 if (strchr(arg
, '-') || endptr
== arg
|| *endptr
)
3194 die("%s: argument must be a non-negative integer", option
);
3198 static void option_depth(const char *depth
)
3200 max_depth
= ulong_arg("--depth", depth
);
3201 if (max_depth
> MAX_DEPTH
)
3202 die("--depth cannot exceed %u", MAX_DEPTH
);
3205 static void option_active_branches(const char *branches
)
3207 max_active_branches
= ulong_arg("--active-branches", branches
);
3210 static void option_export_marks(const char *marks
)
3212 export_marks_file
= make_fast_import_path(marks
);
3215 static void option_cat_blob_fd(const char *fd
)
3217 unsigned long n
= ulong_arg("--cat-blob-fd", fd
);
3218 if (n
> (unsigned long) INT_MAX
)
3219 die("--cat-blob-fd cannot exceed %d", INT_MAX
);
3220 cat_blob_fd
= (int) n
;
3223 static void option_export_pack_edges(const char *edges
)
3227 pack_edges
= xfopen(edges
, "a");
3230 static int parse_one_option(const char *option
)
3232 if (skip_prefix(option
, "max-pack-size=", &option
)) {
3234 if (!git_parse_ulong(option
, &v
))
3237 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v
);
3239 } else if (v
< 1024 * 1024) {
3240 warning("minimum max-pack-size is 1 MiB");
3244 } else if (skip_prefix(option
, "big-file-threshold=", &option
)) {
3246 if (!git_parse_ulong(option
, &v
))
3248 big_file_threshold
= v
;
3249 } else if (skip_prefix(option
, "depth=", &option
)) {
3250 option_depth(option
);
3251 } else if (skip_prefix(option
, "active-branches=", &option
)) {
3252 option_active_branches(option
);
3253 } else if (skip_prefix(option
, "export-pack-edges=", &option
)) {
3254 option_export_pack_edges(option
);
3255 } else if (!strcmp(option
, "quiet")) {
3257 } else if (!strcmp(option
, "stats")) {
3259 } else if (!strcmp(option
, "allow-unsafe-features")) {
3260 ; /* already handled during early option parsing */
3268 static void check_unsafe_feature(const char *feature
, int from_stream
)
3270 if (from_stream
&& !allow_unsafe_features
)
3271 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3275 static int parse_one_feature(const char *feature
, int from_stream
)
3279 if (skip_prefix(feature
, "date-format=", &arg
)) {
3280 option_date_format(arg
);
3281 } else if (skip_prefix(feature
, "import-marks=", &arg
)) {
3282 check_unsafe_feature("import-marks", from_stream
);
3283 option_import_marks(arg
, from_stream
, 0);
3284 } else if (skip_prefix(feature
, "import-marks-if-exists=", &arg
)) {
3285 check_unsafe_feature("import-marks-if-exists", from_stream
);
3286 option_import_marks(arg
, from_stream
, 1);
3287 } else if (skip_prefix(feature
, "export-marks=", &arg
)) {
3288 check_unsafe_feature(feature
, from_stream
);
3289 option_export_marks(arg
);
3290 } else if (!strcmp(feature
, "alias")) {
3291 ; /* Don't die - this feature is supported */
3292 } else if (!strcmp(feature
, "get-mark")) {
3293 ; /* Don't die - this feature is supported */
3294 } else if (!strcmp(feature
, "cat-blob")) {
3295 ; /* Don't die - this feature is supported */
3296 } else if (!strcmp(feature
, "relative-marks")) {
3297 relative_marks_paths
= 1;
3298 } else if (!strcmp(feature
, "no-relative-marks")) {
3299 relative_marks_paths
= 0;
3300 } else if (!strcmp(feature
, "done")) {
3301 require_explicit_termination
= 1;
3302 } else if (!strcmp(feature
, "force")) {
3304 } else if (!strcmp(feature
, "notes") || !strcmp(feature
, "ls")) {
3305 ; /* do nothing; we have the feature */
3313 static void parse_feature(const char *feature
)
3315 if (seen_data_command
)
3316 die("Got feature command '%s' after data command", feature
);
3318 if (parse_one_feature(feature
, 1))
3321 die("This version of fast-import does not support feature %s.", feature
);
3324 static void parse_option(const char *option
)
3326 if (seen_data_command
)
3327 die("Got option command '%s' after data command", option
);
3329 if (parse_one_option(option
))
3332 die("This version of fast-import does not support option: %s", option
);
3335 static void git_pack_config(void)
3337 int indexversion_value
;
3339 unsigned long packsizelimit_value
;
3341 if (!git_config_get_ulong("pack.depth", &max_depth
)) {
3342 if (max_depth
> MAX_DEPTH
)
3343 max_depth
= MAX_DEPTH
;
3345 if (!git_config_get_int("pack.indexversion", &indexversion_value
)) {
3346 pack_idx_opts
.version
= indexversion_value
;
3347 if (pack_idx_opts
.version
> 2)
3348 git_die_config("pack.indexversion",
3349 "bad pack.indexversion=%"PRIu32
, pack_idx_opts
.version
);
3351 if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value
))
3352 max_packsize
= packsizelimit_value
;
3354 if (!git_config_get_int("fastimport.unpacklimit", &limit
))
3355 unpack_limit
= limit
;
3356 else if (!git_config_get_int("transfer.unpacklimit", &limit
))
3357 unpack_limit
= limit
;
3359 git_config(git_default_config
, NULL
);
3362 static const char fast_import_usage
[] =
3363 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3365 static void parse_argv(void)
3369 for (i
= 1; i
< global_argc
; i
++) {
3370 const char *a
= global_argv
[i
];
3372 if (*a
!= '-' || !strcmp(a
, "--"))
3375 if (!skip_prefix(a
, "--", &a
))
3376 die("unknown option %s", a
);
3378 if (parse_one_option(a
))
3381 if (parse_one_feature(a
, 0))
3384 if (skip_prefix(a
, "cat-blob-fd=", &a
)) {
3385 option_cat_blob_fd(a
);
3389 die("unknown option --%s", a
);
3391 if (i
!= global_argc
)
3392 usage(fast_import_usage
);
3394 seen_data_command
= 1;
3395 if (import_marks_file
)
3399 int cmd_main(int argc
, const char **argv
)
3403 if (argc
== 2 && !strcmp(argv
[1], "-h"))
3404 usage(fast_import_usage
);
3406 setup_git_directory();
3407 reset_pack_idx_option(&pack_idx_opts
);
3410 alloc_objects(object_entry_alloc
);
3411 strbuf_init(&command_buf
, 0);
3412 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
3413 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
3414 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
3415 marks
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
3418 * We don't parse most options until after we've seen the set of
3419 * "feature" lines at the start of the stream (which allows the command
3420 * line to override stream data). But we must do an early parse of any
3421 * command-line options that impact how we interpret the feature lines.
3423 for (i
= 1; i
< argc
; i
++) {
3424 const char *arg
= argv
[i
];
3425 if (*arg
!= '-' || !strcmp(arg
, "--"))
3427 if (!strcmp(arg
, "--allow-unsafe-features"))
3428 allow_unsafe_features
= 1;
3434 rc_free
= mem_pool_alloc(&fi_mem_pool
, cmd_save
* sizeof(*rc_free
));
3435 for (i
= 0; i
< (cmd_save
- 1); i
++)
3436 rc_free
[i
].next
= &rc_free
[i
+ 1];
3437 rc_free
[cmd_save
- 1].next
= NULL
;
3440 set_die_routine(die_nicely
);
3441 set_checkpoint_signal();
3442 while (read_next_command() != EOF
) {
3444 if (!strcmp("blob", command_buf
.buf
))
3446 else if (skip_prefix(command_buf
.buf
, "commit ", &v
))
3447 parse_new_commit(v
);
3448 else if (skip_prefix(command_buf
.buf
, "tag ", &v
))
3450 else if (skip_prefix(command_buf
.buf
, "reset ", &v
))
3451 parse_reset_branch(v
);
3452 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
3454 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
3456 else if (skip_prefix(command_buf
.buf
, "get-mark ", &v
))
3458 else if (!strcmp("checkpoint", command_buf
.buf
))
3460 else if (!strcmp("done", command_buf
.buf
))
3462 else if (!strcmp("alias", command_buf
.buf
))
3464 else if (starts_with(command_buf
.buf
, "progress "))
3466 else if (skip_prefix(command_buf
.buf
, "feature ", &v
))
3468 else if (skip_prefix(command_buf
.buf
, "option git ", &v
))
3470 else if (starts_with(command_buf
.buf
, "option "))
3471 /* ignore non-git options*/;
3473 die("Unsupported command: %s", command_buf
.buf
);
3475 if (checkpoint_requested
)
3479 /* argv hasn't been parsed yet, do so */
3480 if (!seen_data_command
)
3483 if (require_explicit_termination
&& feof(stdin
))
3484 die("stream ends early");
3497 uintmax_t total_count
= 0, duplicate_count
= 0;
3498 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3499 total_count
+= object_count_by_type
[i
];
3500 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3501 duplicate_count
+= duplicate_count_by_type
[i
];
3503 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3504 fprintf(stderr
, "---------------------------------------------------------------------\n");
3505 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3506 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3507 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
]);
3508 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
]);
3509 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
]);
3510 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
]);
3511 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3512 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3513 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3514 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (tree_entry_allocd
+ fi_mem_pool
.pool_alloc
+ alloc_count
*sizeof(struct object_entry
))/1024);
3515 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd
+ fi_mem_pool
.pool_alloc
) /1024));
3516 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3517 fprintf(stderr
, "---------------------------------------------------------------------\n");
3519 fprintf(stderr
, "---------------------------------------------------------------------\n");
3520 fprintf(stderr
, "\n");
3523 return failure
? 1 : 0;