3 #include "environment.h"
6 #include "repository.h"
17 #include "csum-file.h"
20 #include "run-command.h"
22 #include "object-file.h"
23 #include "object-name.h"
24 #include "object-store-ll.h"
26 #include "commit-reach.h"
30 #define PACK_ID_BITS 16
31 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
33 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
36 * We abuse the setuid bit on directories to mean "do not delta".
38 #define NO_DELTA S_ISUID
41 * The amount of additional space required in order to write an object into the
42 * current pack. This is the hash lengths at the end of the pack, plus the
43 * length of one object ID.
45 #define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3)
48 struct pack_idx_entry idx
;
49 struct hashmap_entry ent
;
50 uint32_t type
: TYPE_BITS
,
51 pack_id
: PACK_ID_BITS
,
55 static int object_entry_hashcmp(const void *map_data UNUSED
,
56 const struct hashmap_entry
*eptr
,
57 const struct hashmap_entry
*entry_or_key
,
60 const struct object_id
*oid
= keydata
;
61 const struct object_entry
*e1
, *e2
;
63 e1
= container_of(eptr
, const struct object_entry
, ent
);
65 return oidcmp(&e1
->idx
.oid
, oid
);
67 e2
= container_of(entry_or_key
, const struct object_entry
, ent
);
68 return oidcmp(&e1
->idx
.oid
, &e2
->idx
.oid
);
71 struct object_entry_pool
{
72 struct object_entry_pool
*next_pool
;
73 struct object_entry
*next_free
;
74 struct object_entry
*end
;
75 struct object_entry entries
[FLEX_ARRAY
]; /* more */
80 struct object_id
*oids
[1024];
81 struct object_entry
*marked
[1024];
82 struct mark_set
*sets
[1024];
95 struct atom_str
*next_atom
;
96 unsigned short str_len
;
97 char str_dat
[FLEX_ARRAY
]; /* more */
102 struct tree_content
*tree
;
103 struct atom_str
*name
;
104 struct tree_entry_ms
{
106 struct object_id oid
;
110 struct tree_content
{
111 unsigned int entry_capacity
; /* must match avail_tree_content */
112 unsigned int entry_count
;
113 unsigned int delta_depth
;
114 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
117 struct avail_tree_content
{
118 unsigned int entry_capacity
; /* must match tree_content */
119 struct avail_tree_content
*next_avail
;
123 struct branch
*table_next_branch
;
124 struct branch
*active_next_branch
;
126 struct tree_entry branch_tree
;
127 uintmax_t last_commit
;
131 unsigned pack_id
: PACK_ID_BITS
;
132 struct object_id oid
;
136 struct tag
*next_tag
;
138 unsigned int pack_id
;
139 struct object_id oid
;
143 struct hash_list
*next
;
144 struct object_id oid
;
149 WHENSPEC_RAW_PERMISSIVE
,
154 struct recent_command
{
155 struct recent_command
*prev
;
156 struct recent_command
*next
;
160 typedef void (*mark_set_inserter_t
)(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
);
161 typedef void (*each_mark_fn_t
)(uintmax_t mark
, void *obj
, void *cbp
);
163 /* Configured limits on output */
164 static unsigned long max_depth
= 50;
165 static off_t max_packsize
;
166 static int unpack_limit
= 100;
167 static int force_update
;
169 /* Stats and misc. counters */
170 static uintmax_t alloc_count
;
171 static uintmax_t marks_set_count
;
172 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
173 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
174 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
175 static uintmax_t delta_count_attempts_by_type
[1 << TYPE_BITS
];
176 static unsigned long object_count
;
177 static unsigned long branch_count
;
178 static unsigned long branch_load_count
;
180 static FILE *pack_edges
;
181 static unsigned int show_stats
= 1;
182 static int global_argc
;
183 static const char **global_argv
;
184 static const char *global_prefix
;
187 static struct mem_pool fi_mem_pool
= {
188 .block_alloc
= 2*1024*1024 - sizeof(struct mp_block
),
191 /* Atom management */
192 static unsigned int atom_table_sz
= 4451;
193 static unsigned int atom_cnt
;
194 static struct atom_str
**atom_table
;
196 /* The .pack file being generated */
197 static struct pack_idx_option pack_idx_opts
;
198 static unsigned int pack_id
;
199 static struct hashfile
*pack_file
;
200 static struct packed_git
*pack_data
;
201 static struct packed_git
**all_packs
;
202 static off_t pack_size
;
204 /* Table of objects we've written. */
205 static unsigned int object_entry_alloc
= 5000;
206 static struct object_entry_pool
*blocks
;
207 static struct hashmap object_table
;
208 static struct mark_set
*marks
;
209 static const char *export_marks_file
;
210 static const char *import_marks_file
;
211 static int import_marks_file_from_stream
;
212 static int import_marks_file_ignore_missing
;
213 static int import_marks_file_done
;
214 static int relative_marks_paths
;
217 static struct last_object last_blob
= {
221 /* Tree management */
222 static unsigned int tree_entry_alloc
= 1000;
223 static void *avail_tree_entry
;
224 static unsigned int avail_tree_table_sz
= 100;
225 static struct avail_tree_content
**avail_tree_table
;
226 static size_t tree_entry_allocd
;
227 static struct strbuf old_tree
= STRBUF_INIT
;
228 static struct strbuf new_tree
= STRBUF_INIT
;
231 static unsigned long max_active_branches
= 5;
232 static unsigned long cur_active_branches
;
233 static unsigned long branch_table_sz
= 1039;
234 static struct branch
**branch_table
;
235 static struct branch
*active_branches
;
238 static struct tag
*first_tag
;
239 static struct tag
*last_tag
;
241 /* Input stream parsing */
242 static whenspec_type whenspec
= WHENSPEC_RAW
;
243 static struct strbuf command_buf
= STRBUF_INIT
;
244 static int unread_command_buf
;
245 static struct recent_command cmd_hist
= {
249 static struct recent_command
*cmd_tail
= &cmd_hist
;
250 static struct recent_command
*rc_free
;
251 static unsigned int cmd_save
= 100;
252 static uintmax_t next_mark
;
253 static struct strbuf new_data
= STRBUF_INIT
;
254 static int seen_data_command
;
255 static int require_explicit_termination
;
256 static int allow_unsafe_features
;
258 /* Signal handling */
259 static volatile sig_atomic_t checkpoint_requested
;
261 /* Submodule marks */
262 static struct string_list sub_marks_from
= STRING_LIST_INIT_DUP
;
263 static struct string_list sub_marks_to
= STRING_LIST_INIT_DUP
;
264 static kh_oid_map_t
*sub_oid_map
;
266 /* Where to write output of cat-blob commands */
267 static int cat_blob_fd
= STDOUT_FILENO
;
269 static void parse_argv(void);
270 static void parse_get_mark(const char *p
);
271 static void parse_cat_blob(const char *p
);
272 static void parse_ls(const char *p
, struct branch
*b
);
274 static void for_each_mark(struct mark_set
*m
, uintmax_t base
, each_mark_fn_t callback
, void *p
)
278 for (k
= 0; k
< 1024; k
++) {
280 for_each_mark(m
->data
.sets
[k
], base
+ (k
<< m
->shift
), callback
, p
);
283 for (k
= 0; k
< 1024; k
++) {
284 if (m
->data
.marked
[k
])
285 callback(base
+ k
, m
->data
.marked
[k
], p
);
290 static void dump_marks_fn(uintmax_t mark
, void *object
, void *cbp
) {
291 struct object_entry
*e
= object
;
294 fprintf(f
, ":%" PRIuMAX
" %s\n", mark
, oid_to_hex(&e
->idx
.oid
));
297 static void write_branch_report(FILE *rpt
, struct branch
*b
)
299 fprintf(rpt
, "%s:\n", b
->name
);
301 fprintf(rpt
, " status :");
303 fputs(" active", rpt
);
304 if (b
->branch_tree
.tree
)
305 fputs(" loaded", rpt
);
306 if (is_null_oid(&b
->branch_tree
.versions
[1].oid
))
307 fputs(" dirty", rpt
);
310 fprintf(rpt
, " tip commit : %s\n", oid_to_hex(&b
->oid
));
311 fprintf(rpt
, " old tree : %s\n",
312 oid_to_hex(&b
->branch_tree
.versions
[0].oid
));
313 fprintf(rpt
, " cur tree : %s\n",
314 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
315 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
317 fputs(" last pack : ", rpt
);
318 if (b
->pack_id
< MAX_PACK_ID
)
319 fprintf(rpt
, "%u", b
->pack_id
);
325 static void write_crash_report(const char *err
)
327 char *loc
= git_pathdup("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
328 FILE *rpt
= fopen(loc
, "w");
331 struct recent_command
*rc
;
334 error_errno("can't write crash report %s", loc
);
339 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
341 fprintf(rpt
, "fast-import crash report:\n");
342 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
343 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
344 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_MODE(ISO8601
)));
347 fputs("fatal: ", rpt
);
352 fputs("Most Recent Commands Before Crash\n", rpt
);
353 fputs("---------------------------------\n", rpt
);
354 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
355 if (rc
->next
== &cmd_hist
)
364 fputs("Active Branch LRU\n", rpt
);
365 fputs("-----------------\n", rpt
);
366 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
368 max_active_branches
);
370 fputs(" pos clock name\n", rpt
);
371 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
372 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
373 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
374 ++lu
, b
->last_commit
, b
->name
);
377 fputs("Inactive Branches\n", rpt
);
378 fputs("-----------------\n", rpt
);
379 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
380 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
381 write_branch_report(rpt
, b
);
387 fputs("Annotated Tags\n", rpt
);
388 fputs("--------------\n", rpt
);
389 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
390 fputs(oid_to_hex(&tg
->oid
), rpt
);
392 fputs(tg
->name
, rpt
);
398 fputs("Marks\n", rpt
);
399 fputs("-----\n", rpt
);
400 if (export_marks_file
)
401 fprintf(rpt
, " exported to %s\n", export_marks_file
);
403 for_each_mark(marks
, 0, dump_marks_fn
, rpt
);
406 fputs("-------------------\n", rpt
);
407 fputs("END OF CRASH REPORT\n", rpt
);
412 static void end_packfile(void);
413 static void unkeep_all_packs(void);
414 static void dump_marks(void);
416 static NORETURN
void die_nicely(const char *err
, va_list params
)
420 report_fn die_message_fn
= get_die_message_routine();
423 die_message_fn(err
, params
);
426 char message
[2 * PATH_MAX
];
429 vsnprintf(message
, sizeof(message
), err
, cp
);
430 write_crash_report(message
);
438 #ifndef SIGUSR1 /* Windows, for example */
440 static void set_checkpoint_signal(void)
446 static void checkpoint_signal(int signo UNUSED
)
448 checkpoint_requested
= 1;
451 static void set_checkpoint_signal(void)
455 memset(&sa
, 0, sizeof(sa
));
456 sa
.sa_handler
= checkpoint_signal
;
457 sigemptyset(&sa
.sa_mask
);
458 sa
.sa_flags
= SA_RESTART
;
459 sigaction(SIGUSR1
, &sa
, NULL
);
464 static void alloc_objects(unsigned int cnt
)
466 struct object_entry_pool
*b
;
468 b
= xmalloc(sizeof(struct object_entry_pool
)
469 + cnt
* sizeof(struct object_entry
));
470 b
->next_pool
= blocks
;
471 b
->next_free
= b
->entries
;
472 b
->end
= b
->entries
+ cnt
;
477 static struct object_entry
*new_object(struct object_id
*oid
)
479 struct object_entry
*e
;
481 if (blocks
->next_free
== blocks
->end
)
482 alloc_objects(object_entry_alloc
);
484 e
= blocks
->next_free
++;
485 oidcpy(&e
->idx
.oid
, oid
);
489 static struct object_entry
*find_object(struct object_id
*oid
)
491 return hashmap_get_entry_from_hash(&object_table
, oidhash(oid
), oid
,
492 struct object_entry
, ent
);
495 static struct object_entry
*insert_object(struct object_id
*oid
)
497 struct object_entry
*e
;
498 unsigned int hash
= oidhash(oid
);
500 e
= hashmap_get_entry_from_hash(&object_table
, hash
, oid
,
501 struct object_entry
, ent
);
505 hashmap_entry_init(&e
->ent
, hash
);
506 hashmap_add(&object_table
, &e
->ent
);
512 static void invalidate_pack_id(unsigned int id
)
516 struct hashmap_iter iter
;
517 struct object_entry
*e
;
519 hashmap_for_each_entry(&object_table
, &iter
, e
, ent
) {
520 if (e
->pack_id
== id
)
521 e
->pack_id
= MAX_PACK_ID
;
524 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
527 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
528 if (b
->pack_id
== id
)
529 b
->pack_id
= MAX_PACK_ID
;
532 for (t
= first_tag
; t
; t
= t
->next_tag
)
533 if (t
->pack_id
== id
)
534 t
->pack_id
= MAX_PACK_ID
;
537 static unsigned int hc_str(const char *s
, size_t len
)
545 static void insert_mark(struct mark_set
**top
, uintmax_t idnum
, struct object_entry
*oe
)
547 struct mark_set
*s
= *top
;
549 while ((idnum
>> s
->shift
) >= 1024) {
550 s
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
551 s
->shift
= (*top
)->shift
+ 10;
552 s
->data
.sets
[0] = *top
;
556 uintmax_t i
= idnum
>> s
->shift
;
557 idnum
-= i
<< s
->shift
;
558 if (!s
->data
.sets
[i
]) {
559 s
->data
.sets
[i
] = mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
560 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
564 if (!s
->data
.marked
[idnum
])
566 s
->data
.marked
[idnum
] = oe
;
569 static void *find_mark(struct mark_set
*s
, uintmax_t idnum
)
571 uintmax_t orig_idnum
= idnum
;
572 struct object_entry
*oe
= NULL
;
573 if ((idnum
>> s
->shift
) < 1024) {
574 while (s
&& s
->shift
) {
575 uintmax_t i
= idnum
>> s
->shift
;
576 idnum
-= i
<< s
->shift
;
580 oe
= s
->data
.marked
[idnum
];
583 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
587 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
589 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
592 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
593 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
596 c
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct atom_str
) + len
+ 1);
598 memcpy(c
->str_dat
, s
, len
);
600 c
->next_atom
= atom_table
[hc
];
606 static struct branch
*lookup_branch(const char *name
)
608 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
611 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
612 if (!strcmp(name
, b
->name
))
617 static struct branch
*new_branch(const char *name
)
619 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
620 struct branch
*b
= lookup_branch(name
);
623 die("Invalid attempt to create duplicate branch: %s", name
);
624 if (check_refname_format(name
, REFNAME_ALLOW_ONELEVEL
))
625 die("Branch name doesn't conform to GIT standards: %s", name
);
627 b
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct branch
));
628 b
->name
= mem_pool_strdup(&fi_mem_pool
, name
);
629 b
->table_next_branch
= branch_table
[hc
];
630 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
631 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
634 b
->pack_id
= MAX_PACK_ID
;
635 branch_table
[hc
] = b
;
640 static unsigned int hc_entries(unsigned int cnt
)
642 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
643 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
646 static struct tree_content
*new_tree_content(unsigned int cnt
)
648 struct avail_tree_content
*f
, *l
= NULL
;
649 struct tree_content
*t
;
650 unsigned int hc
= hc_entries(cnt
);
652 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
653 if (f
->entry_capacity
>= cnt
)
658 l
->next_avail
= f
->next_avail
;
660 avail_tree_table
[hc
] = f
->next_avail
;
662 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
663 f
= mem_pool_alloc(&fi_mem_pool
, sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
664 f
->entry_capacity
= cnt
;
667 t
= (struct tree_content
*)f
;
673 static void release_tree_entry(struct tree_entry
*e
);
674 static void release_tree_content(struct tree_content
*t
)
676 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
677 unsigned int hc
= hc_entries(f
->entry_capacity
);
678 f
->next_avail
= avail_tree_table
[hc
];
679 avail_tree_table
[hc
] = f
;
682 static void release_tree_content_recursive(struct tree_content
*t
)
685 for (i
= 0; i
< t
->entry_count
; i
++)
686 release_tree_entry(t
->entries
[i
]);
687 release_tree_content(t
);
690 static struct tree_content
*grow_tree_content(
691 struct tree_content
*t
,
694 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
695 r
->entry_count
= t
->entry_count
;
696 r
->delta_depth
= t
->delta_depth
;
697 COPY_ARRAY(r
->entries
, t
->entries
, t
->entry_count
);
698 release_tree_content(t
);
702 static struct tree_entry
*new_tree_entry(void)
704 struct tree_entry
*e
;
706 if (!avail_tree_entry
) {
707 unsigned int n
= tree_entry_alloc
;
708 tree_entry_allocd
+= n
* sizeof(struct tree_entry
);
710 avail_tree_entry
= e
;
712 *((void**)e
) = e
+ 1;
718 e
= avail_tree_entry
;
719 avail_tree_entry
= *((void**)e
);
723 static void release_tree_entry(struct tree_entry
*e
)
726 release_tree_content_recursive(e
->tree
);
727 *((void**)e
) = avail_tree_entry
;
728 avail_tree_entry
= e
;
731 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
733 struct tree_content
*d
;
734 struct tree_entry
*a
, *b
;
739 d
= new_tree_content(s
->entry_count
);
740 for (i
= 0; i
< s
->entry_count
; i
++) {
742 b
= new_tree_entry();
743 memcpy(b
, a
, sizeof(*a
));
744 if (a
->tree
&& is_null_oid(&b
->versions
[1].oid
))
745 b
->tree
= dup_tree_content(a
->tree
);
750 d
->entry_count
= s
->entry_count
;
751 d
->delta_depth
= s
->delta_depth
;
756 static void start_packfile(void)
758 struct strbuf tmp_file
= STRBUF_INIT
;
759 struct packed_git
*p
;
762 pack_fd
= odb_mkstemp(&tmp_file
, "pack/tmp_pack_XXXXXX");
763 FLEX_ALLOC_STR(p
, pack_name
, tmp_file
.buf
);
764 strbuf_release(&tmp_file
);
766 p
->pack_fd
= pack_fd
;
768 pack_file
= hashfd(pack_fd
, p
->pack_name
);
771 pack_size
= write_pack_header(pack_file
, 0);
774 REALLOC_ARRAY(all_packs
, pack_id
+ 1);
775 all_packs
[pack_id
] = p
;
778 static const char *create_index(void)
781 struct pack_idx_entry
**idx
, **c
, **last
;
782 struct object_entry
*e
;
783 struct object_entry_pool
*o
;
785 /* Build the table of object IDs. */
786 ALLOC_ARRAY(idx
, object_count
);
788 for (o
= blocks
; o
; o
= o
->next_pool
)
789 for (e
= o
->next_free
; e
-- != o
->entries
;)
790 if (pack_id
== e
->pack_id
)
792 last
= idx
+ object_count
;
794 die("internal consistency error creating the index");
796 tmpfile
= write_idx_file(NULL
, idx
, object_count
, &pack_idx_opts
,
802 static char *keep_pack(const char *curr_index_name
)
804 static const char *keep_msg
= "fast-import";
805 struct strbuf name
= STRBUF_INIT
;
808 odb_pack_name(&name
, pack_data
->hash
, "keep");
809 keep_fd
= odb_pack_keep(name
.buf
);
811 die_errno("cannot create keep file");
812 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
814 die_errno("failed to write keep file");
816 odb_pack_name(&name
, pack_data
->hash
, "pack");
817 if (finalize_object_file(pack_data
->pack_name
, name
.buf
))
818 die("cannot store pack file");
820 odb_pack_name(&name
, pack_data
->hash
, "idx");
821 if (finalize_object_file(curr_index_name
, name
.buf
))
822 die("cannot store index file");
823 free((void *)curr_index_name
);
824 return strbuf_detach(&name
, NULL
);
827 static void unkeep_all_packs(void)
829 struct strbuf name
= STRBUF_INIT
;
832 for (k
= 0; k
< pack_id
; k
++) {
833 struct packed_git
*p
= all_packs
[k
];
834 odb_pack_name(&name
, p
->hash
, "keep");
835 unlink_or_warn(name
.buf
);
837 strbuf_release(&name
);
840 static int loosen_small_pack(const struct packed_git
*p
)
842 struct child_process unpack
= CHILD_PROCESS_INIT
;
844 if (lseek(p
->pack_fd
, 0, SEEK_SET
) < 0)
845 die_errno("Failed seeking to start of '%s'", p
->pack_name
);
847 unpack
.in
= p
->pack_fd
;
849 unpack
.stdout_to_stderr
= 1;
850 strvec_push(&unpack
.args
, "unpack-objects");
852 strvec_push(&unpack
.args
, "-q");
854 return run_command(&unpack
);
857 static void end_packfile(void)
861 if (running
|| !pack_data
)
865 clear_delta_base_cache();
867 struct packed_git
*new_p
;
868 struct object_id cur_pack_oid
;
874 close_pack_windows(pack_data
);
875 finalize_hashfile(pack_file
, cur_pack_oid
.hash
, FSYNC_COMPONENT_PACK
, 0);
876 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->hash
,
877 pack_data
->pack_name
, object_count
,
878 cur_pack_oid
.hash
, pack_size
);
880 if (object_count
<= unpack_limit
) {
881 if (!loosen_small_pack(pack_data
)) {
882 invalidate_pack_id(pack_id
);
887 close(pack_data
->pack_fd
);
888 idx_name
= keep_pack(create_index());
890 /* Register the packfile with core git's machinery. */
891 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
893 die("core git rejected index %s", idx_name
);
894 all_packs
[pack_id
] = new_p
;
895 install_packed_git(the_repository
, new_p
);
898 /* Print the boundary */
900 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
901 for (i
= 0; i
< branch_table_sz
; i
++) {
902 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
903 if (b
->pack_id
== pack_id
)
904 fprintf(pack_edges
, " %s",
905 oid_to_hex(&b
->oid
));
908 for (t
= first_tag
; t
; t
= t
->next_tag
) {
909 if (t
->pack_id
== pack_id
)
910 fprintf(pack_edges
, " %s",
911 oid_to_hex(&t
->oid
));
913 fputc('\n', pack_edges
);
921 close(pack_data
->pack_fd
);
922 unlink_or_warn(pack_data
->pack_name
);
924 FREE_AND_NULL(pack_data
);
927 /* We can't carry a delta across packfiles. */
928 strbuf_release(&last_blob
.data
);
929 last_blob
.offset
= 0;
933 static void cycle_packfile(void)
939 static int store_object(
940 enum object_type type
,
942 struct last_object
*last
,
943 struct object_id
*oidout
,
947 struct object_entry
*e
;
948 unsigned char hdr
[96];
949 struct object_id oid
;
950 unsigned long hdrlen
, deltalen
;
954 hdrlen
= format_object_header((char *)hdr
, sizeof(hdr
), type
,
956 the_hash_algo
->init_fn(&c
);
957 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
958 the_hash_algo
->update_fn(&c
, dat
->buf
, dat
->len
);
959 the_hash_algo
->final_oid_fn(&oid
, &c
);
961 oidcpy(oidout
, &oid
);
963 e
= insert_object(&oid
);
965 insert_mark(&marks
, mark
, e
);
967 duplicate_count_by_type
[type
]++;
969 } else if (find_sha1_pack(oid
.hash
,
970 get_all_packs(the_repository
))) {
972 e
->pack_id
= MAX_PACK_ID
;
973 e
->idx
.offset
= 1; /* just not zero! */
974 duplicate_count_by_type
[type
]++;
978 if (last
&& last
->data
.len
&& last
->data
.buf
&& last
->depth
< max_depth
979 && dat
->len
> the_hash_algo
->rawsz
) {
981 delta_count_attempts_by_type
[type
]++;
982 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
984 &deltalen
, dat
->len
- the_hash_algo
->rawsz
);
988 git_deflate_init(&s
, pack_compression_level
);
991 s
.avail_in
= deltalen
;
993 s
.next_in
= (void *)dat
->buf
;
994 s
.avail_in
= dat
->len
;
996 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
997 s
.next_out
= out
= xmalloc(s
.avail_out
);
998 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
1000 git_deflate_end(&s
);
1002 /* Determine if we should auto-checkpoint. */
1004 && (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) > max_packsize
)
1005 || (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) < pack_size
) {
1007 /* This new object needs to *not* have the current pack_id. */
1008 e
->pack_id
= pack_id
+ 1;
1011 /* We cannot carry a delta into the new pack. */
1013 FREE_AND_NULL(delta
);
1015 git_deflate_init(&s
, pack_compression_level
);
1016 s
.next_in
= (void *)dat
->buf
;
1017 s
.avail_in
= dat
->len
;
1018 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
1019 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
1020 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
1022 git_deflate_end(&s
);
1027 e
->pack_id
= pack_id
;
1028 e
->idx
.offset
= pack_size
;
1030 object_count_by_type
[type
]++;
1032 crc32_begin(pack_file
);
1035 off_t ofs
= e
->idx
.offset
- last
->offset
;
1036 unsigned pos
= sizeof(hdr
) - 1;
1038 delta_count_by_type
[type
]++;
1039 e
->depth
= last
->depth
+ 1;
1041 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1042 OBJ_OFS_DELTA
, deltalen
);
1043 hashwrite(pack_file
, hdr
, hdrlen
);
1044 pack_size
+= hdrlen
;
1046 hdr
[pos
] = ofs
& 127;
1048 hdr
[--pos
] = 128 | (--ofs
& 127);
1049 hashwrite(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
1050 pack_size
+= sizeof(hdr
) - pos
;
1053 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1055 hashwrite(pack_file
, hdr
, hdrlen
);
1056 pack_size
+= hdrlen
;
1059 hashwrite(pack_file
, out
, s
.total_out
);
1060 pack_size
+= s
.total_out
;
1062 e
->idx
.crc32
= crc32_end(pack_file
);
1067 if (last
->no_swap
) {
1070 strbuf_swap(&last
->data
, dat
);
1072 last
->offset
= e
->idx
.offset
;
1073 last
->depth
= e
->depth
;
1078 static void truncate_pack(struct hashfile_checkpoint
*checkpoint
)
1080 if (hashfile_truncate(pack_file
, checkpoint
))
1081 die_errno("cannot truncate pack to skip duplicate");
1082 pack_size
= checkpoint
->offset
;
1085 static void stream_blob(uintmax_t len
, struct object_id
*oidout
, uintmax_t mark
)
1087 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1088 unsigned char *in_buf
= xmalloc(in_sz
);
1089 unsigned char *out_buf
= xmalloc(out_sz
);
1090 struct object_entry
*e
;
1091 struct object_id oid
;
1092 unsigned long hdrlen
;
1096 struct hashfile_checkpoint checkpoint
;
1099 /* Determine if we should auto-checkpoint. */
1101 && (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) > max_packsize
)
1102 || (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) < pack_size
)
1105 hashfile_checkpoint(pack_file
, &checkpoint
);
1106 offset
= checkpoint
.offset
;
1108 hdrlen
= format_object_header((char *)out_buf
, out_sz
, OBJ_BLOB
, len
);
1110 the_hash_algo
->init_fn(&c
);
1111 the_hash_algo
->update_fn(&c
, out_buf
, hdrlen
);
1113 crc32_begin(pack_file
);
1115 git_deflate_init(&s
, pack_compression_level
);
1117 hdrlen
= encode_in_pack_object_header(out_buf
, out_sz
, OBJ_BLOB
, len
);
1119 s
.next_out
= out_buf
+ hdrlen
;
1120 s
.avail_out
= out_sz
- hdrlen
;
1122 while (status
!= Z_STREAM_END
) {
1123 if (0 < len
&& !s
.avail_in
) {
1124 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1125 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1126 if (!n
&& feof(stdin
))
1127 die("EOF in data (%" PRIuMAX
" bytes remaining)", len
);
1129 the_hash_algo
->update_fn(&c
, in_buf
, n
);
1135 status
= git_deflate(&s
, len
? 0 : Z_FINISH
);
1137 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1138 size_t n
= s
.next_out
- out_buf
;
1139 hashwrite(pack_file
, out_buf
, n
);
1141 s
.next_out
= out_buf
;
1142 s
.avail_out
= out_sz
;
1151 die("unexpected deflate failure: %d", status
);
1154 git_deflate_end(&s
);
1155 the_hash_algo
->final_oid_fn(&oid
, &c
);
1158 oidcpy(oidout
, &oid
);
1160 e
= insert_object(&oid
);
1163 insert_mark(&marks
, mark
, e
);
1165 if (e
->idx
.offset
) {
1166 duplicate_count_by_type
[OBJ_BLOB
]++;
1167 truncate_pack(&checkpoint
);
1169 } else if (find_sha1_pack(oid
.hash
,
1170 get_all_packs(the_repository
))) {
1172 e
->pack_id
= MAX_PACK_ID
;
1173 e
->idx
.offset
= 1; /* just not zero! */
1174 duplicate_count_by_type
[OBJ_BLOB
]++;
1175 truncate_pack(&checkpoint
);
1180 e
->pack_id
= pack_id
;
1181 e
->idx
.offset
= offset
;
1182 e
->idx
.crc32
= crc32_end(pack_file
);
1184 object_count_by_type
[OBJ_BLOB
]++;
1191 /* All calls must be guarded by find_object() or find_mark() to
1192 * ensure the 'struct object_entry' passed was written by this
1193 * process instance. We unpack the entry by the offset, avoiding
1194 * the need for the corresponding .idx file. This unpacking rule
1195 * works because we only use OBJ_REF_DELTA within the packfiles
1196 * created by fast-import.
1198 * oe must not be NULL. Such an oe usually comes from giving
1199 * an unknown SHA-1 to find_object() or an undefined mark to
1200 * find_mark(). Callers must test for this condition and use
1201 * the standard read_sha1_file() when it happens.
1203 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1204 * find_mark(), where the mark was reloaded from an existing marks
1205 * file and is referencing an object that this fast-import process
1206 * instance did not write out to a packfile. Callers must test for
1207 * this condition and use read_sha1_file() instead.
1209 static void *gfi_unpack_entry(
1210 struct object_entry
*oe
,
1211 unsigned long *sizep
)
1213 enum object_type type
;
1214 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1215 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ the_hash_algo
->rawsz
)) {
1216 /* The object is stored in the packfile we are writing to
1217 * and we have modified it since the last time we scanned
1218 * back to read a previously written object. If an old
1219 * window covered [p->pack_size, p->pack_size + rawsz) its
1220 * data is stale and is not valid. Closing all windows
1221 * and updating the packfile length ensures we can read
1222 * the newly written data.
1224 close_pack_windows(p
);
1225 hashflush(pack_file
);
1227 /* We have to offer rawsz bytes additional on the end of
1228 * the packfile as the core unpacker code assumes the
1229 * footer is present at the file end and must promise
1230 * at least rawsz bytes within any window it maps. But
1231 * we don't actually create the footer here.
1233 p
->pack_size
= pack_size
+ the_hash_algo
->rawsz
;
1235 return unpack_entry(the_repository
, p
, oe
->idx
.offset
, &type
, sizep
);
1238 static const char *get_mode(const char *str
, uint16_t *modep
)
1243 while ((c
= *str
++) != ' ') {
1244 if (c
< '0' || c
> '7')
1246 mode
= (mode
<< 3) + (c
- '0');
1252 static void load_tree(struct tree_entry
*root
)
1254 struct object_id
*oid
= &root
->versions
[1].oid
;
1255 struct object_entry
*myoe
;
1256 struct tree_content
*t
;
1261 root
->tree
= t
= new_tree_content(8);
1262 if (is_null_oid(oid
))
1265 myoe
= find_object(oid
);
1266 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1267 if (myoe
->type
!= OBJ_TREE
)
1268 die("Not a tree: %s", oid_to_hex(oid
));
1269 t
->delta_depth
= myoe
->depth
;
1270 buf
= gfi_unpack_entry(myoe
, &size
);
1272 die("Can't load tree %s", oid_to_hex(oid
));
1274 enum object_type type
;
1275 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
1276 if (!buf
|| type
!= OBJ_TREE
)
1277 die("Can't load tree %s", oid_to_hex(oid
));
1281 while (c
!= (buf
+ size
)) {
1282 struct tree_entry
*e
= new_tree_entry();
1284 if (t
->entry_count
== t
->entry_capacity
)
1285 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1286 t
->entries
[t
->entry_count
++] = e
;
1289 c
= get_mode(c
, &e
->versions
[1].mode
);
1291 die("Corrupt mode in %s", oid_to_hex(oid
));
1292 e
->versions
[0].mode
= e
->versions
[1].mode
;
1293 e
->name
= to_atom(c
, strlen(c
));
1294 c
+= e
->name
->str_len
+ 1;
1295 oidread(&e
->versions
[0].oid
, (unsigned char *)c
);
1296 oidread(&e
->versions
[1].oid
, (unsigned char *)c
);
1297 c
+= the_hash_algo
->rawsz
;
1302 static int tecmp0 (const void *_a
, const void *_b
)
1304 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1305 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1306 return base_name_compare(
1307 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1308 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1311 static int tecmp1 (const void *_a
, const void *_b
)
1313 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1314 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1315 return base_name_compare(
1316 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1317 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1320 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1326 QSORT(t
->entries
, t
->entry_count
, tecmp0
);
1328 QSORT(t
->entries
, t
->entry_count
, tecmp1
);
1330 for (i
= 0; i
< t
->entry_count
; i
++) {
1331 if (t
->entries
[i
]->versions
[v
].mode
)
1332 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1336 strbuf_grow(b
, maxlen
);
1337 for (i
= 0; i
< t
->entry_count
; i
++) {
1338 struct tree_entry
*e
= t
->entries
[i
];
1339 if (!e
->versions
[v
].mode
)
1341 strbuf_addf(b
, "%o %s%c",
1342 (unsigned int)(e
->versions
[v
].mode
& ~NO_DELTA
),
1343 e
->name
->str_dat
, '\0');
1344 strbuf_add(b
, e
->versions
[v
].oid
.hash
, the_hash_algo
->rawsz
);
1348 static void store_tree(struct tree_entry
*root
)
1350 struct tree_content
*t
;
1351 unsigned int i
, j
, del
;
1352 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1353 struct object_entry
*le
= NULL
;
1355 if (!is_null_oid(&root
->versions
[1].oid
))
1362 for (i
= 0; i
< t
->entry_count
; i
++) {
1363 if (t
->entries
[i
]->tree
)
1364 store_tree(t
->entries
[i
]);
1367 if (!(root
->versions
[0].mode
& NO_DELTA
))
1368 le
= find_object(&root
->versions
[0].oid
);
1369 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1370 mktree(t
, 0, &old_tree
);
1372 lo
.offset
= le
->idx
.offset
;
1373 lo
.depth
= t
->delta_depth
;
1376 mktree(t
, 1, &new_tree
);
1377 store_object(OBJ_TREE
, &new_tree
, &lo
, &root
->versions
[1].oid
, 0);
1379 t
->delta_depth
= lo
.depth
;
1380 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1381 struct tree_entry
*e
= t
->entries
[i
];
1382 if (e
->versions
[1].mode
) {
1383 e
->versions
[0].mode
= e
->versions
[1].mode
;
1384 oidcpy(&e
->versions
[0].oid
, &e
->versions
[1].oid
);
1385 t
->entries
[j
++] = e
;
1387 release_tree_entry(e
);
1391 t
->entry_count
-= del
;
1394 static void tree_content_replace(
1395 struct tree_entry
*root
,
1396 const struct object_id
*oid
,
1397 const uint16_t mode
,
1398 struct tree_content
*newtree
)
1401 die("Root cannot be a non-directory");
1402 oidclr(&root
->versions
[0].oid
);
1403 oidcpy(&root
->versions
[1].oid
, oid
);
1405 release_tree_content_recursive(root
->tree
);
1406 root
->tree
= newtree
;
1409 static int tree_content_set(
1410 struct tree_entry
*root
,
1412 const struct object_id
*oid
,
1413 const uint16_t mode
,
1414 struct tree_content
*subtree
)
1416 struct tree_content
*t
;
1419 struct tree_entry
*e
;
1421 slash1
= strchrnul(p
, '/');
1424 die("Empty path component found in input");
1425 if (!*slash1
&& !S_ISDIR(mode
) && subtree
)
1426 die("Non-directories cannot have subtrees");
1431 for (i
= 0; i
< t
->entry_count
; i
++) {
1433 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1436 && e
->versions
[1].mode
== mode
1437 && oideq(&e
->versions
[1].oid
, oid
))
1439 e
->versions
[1].mode
= mode
;
1440 oidcpy(&e
->versions
[1].oid
, oid
);
1442 release_tree_content_recursive(e
->tree
);
1446 * We need to leave e->versions[0].sha1 alone
1447 * to avoid modifying the preimage tree used
1448 * when writing out the parent directory.
1449 * But after replacing the subdir with a
1450 * completely different one, it's not a good
1451 * delta base any more, and besides, we've
1452 * thrown away the tree entries needed to
1453 * make a delta against it.
1455 * So let's just explicitly disable deltas
1458 if (S_ISDIR(e
->versions
[0].mode
))
1459 e
->versions
[0].mode
|= NO_DELTA
;
1461 oidclr(&root
->versions
[1].oid
);
1464 if (!S_ISDIR(e
->versions
[1].mode
)) {
1465 e
->tree
= new_tree_content(8);
1466 e
->versions
[1].mode
= S_IFDIR
;
1470 if (tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
)) {
1471 oidclr(&root
->versions
[1].oid
);
1478 if (t
->entry_count
== t
->entry_capacity
)
1479 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1480 e
= new_tree_entry();
1481 e
->name
= to_atom(p
, n
);
1482 e
->versions
[0].mode
= 0;
1483 oidclr(&e
->versions
[0].oid
);
1484 t
->entries
[t
->entry_count
++] = e
;
1486 e
->tree
= new_tree_content(8);
1487 e
->versions
[1].mode
= S_IFDIR
;
1488 tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
);
1491 e
->versions
[1].mode
= mode
;
1492 oidcpy(&e
->versions
[1].oid
, oid
);
1494 oidclr(&root
->versions
[1].oid
);
1498 static int tree_content_remove(
1499 struct tree_entry
*root
,
1501 struct tree_entry
*backup_leaf
,
1504 struct tree_content
*t
;
1507 struct tree_entry
*e
;
1509 slash1
= strchrnul(p
, '/');
1515 if (!*p
&& allow_root
) {
1521 for (i
= 0; i
< t
->entry_count
; i
++) {
1523 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1524 if (*slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1526 * If p names a file in some subdirectory, and a
1527 * file or symlink matching the name of the
1528 * parent directory of p exists, then p cannot
1529 * exist and need not be deleted.
1532 if (!*slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1536 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
, 0)) {
1537 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1538 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1539 oidclr(&root
->versions
[1].oid
);
1553 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1555 release_tree_content_recursive(e
->tree
);
1557 e
->versions
[1].mode
= 0;
1558 oidclr(&e
->versions
[1].oid
);
1559 oidclr(&root
->versions
[1].oid
);
1563 static int tree_content_get(
1564 struct tree_entry
*root
,
1566 struct tree_entry
*leaf
,
1569 struct tree_content
*t
;
1572 struct tree_entry
*e
;
1574 slash1
= strchrnul(p
, '/');
1576 if (!n
&& !allow_root
)
1577 die("Empty path component found in input");
1588 for (i
= 0; i
< t
->entry_count
; i
++) {
1590 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1593 if (!S_ISDIR(e
->versions
[1].mode
))
1597 return tree_content_get(e
, slash1
+ 1, leaf
, 0);
1603 memcpy(leaf
, e
, sizeof(*leaf
));
1604 if (e
->tree
&& is_null_oid(&e
->versions
[1].oid
))
1605 leaf
->tree
= dup_tree_content(e
->tree
);
1611 static int update_branch(struct branch
*b
)
1613 static const char *msg
= "fast-import";
1614 struct ref_transaction
*transaction
;
1615 struct object_id old_oid
;
1616 struct strbuf err
= STRBUF_INIT
;
1618 if (is_null_oid(&b
->oid
)) {
1620 delete_ref(NULL
, b
->name
, NULL
, 0);
1623 if (read_ref(b
->name
, &old_oid
))
1625 if (!force_update
&& !is_null_oid(&old_oid
)) {
1626 struct commit
*old_cmit
, *new_cmit
;
1628 old_cmit
= lookup_commit_reference_gently(the_repository
,
1630 new_cmit
= lookup_commit_reference_gently(the_repository
,
1632 if (!old_cmit
|| !new_cmit
)
1633 return error("Branch %s is missing commits.", b
->name
);
1635 if (!repo_in_merge_bases(the_repository
, old_cmit
, new_cmit
)) {
1636 warning("Not updating %s"
1637 " (new tip %s does not contain %s)",
1638 b
->name
, oid_to_hex(&b
->oid
),
1639 oid_to_hex(&old_oid
));
1643 transaction
= ref_transaction_begin(&err
);
1645 ref_transaction_update(transaction
, b
->name
, &b
->oid
, &old_oid
,
1647 ref_transaction_commit(transaction
, &err
)) {
1648 ref_transaction_free(transaction
);
1649 error("%s", err
.buf
);
1650 strbuf_release(&err
);
1653 ref_transaction_free(transaction
);
1654 strbuf_release(&err
);
1658 static void dump_branches(void)
1663 for (i
= 0; i
< branch_table_sz
; i
++) {
1664 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1665 failure
|= update_branch(b
);
1669 static void dump_tags(void)
1671 static const char *msg
= "fast-import";
1673 struct strbuf ref_name
= STRBUF_INIT
;
1674 struct strbuf err
= STRBUF_INIT
;
1675 struct ref_transaction
*transaction
;
1677 transaction
= ref_transaction_begin(&err
);
1679 failure
|= error("%s", err
.buf
);
1682 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1683 strbuf_reset(&ref_name
);
1684 strbuf_addf(&ref_name
, "refs/tags/%s", t
->name
);
1686 if (ref_transaction_update(transaction
, ref_name
.buf
,
1687 &t
->oid
, NULL
, 0, msg
, &err
)) {
1688 failure
|= error("%s", err
.buf
);
1692 if (ref_transaction_commit(transaction
, &err
))
1693 failure
|= error("%s", err
.buf
);
1696 ref_transaction_free(transaction
);
1697 strbuf_release(&ref_name
);
1698 strbuf_release(&err
);
1701 static void dump_marks(void)
1703 struct lock_file mark_lock
= LOCK_INIT
;
1706 if (!export_marks_file
|| (import_marks_file
&& !import_marks_file_done
))
1709 if (safe_create_leading_directories_const(export_marks_file
)) {
1710 failure
|= error_errno("unable to create leading directories of %s",
1715 if (hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0) < 0) {
1716 failure
|= error_errno("Unable to write marks file %s",
1721 f
= fdopen_lock_file(&mark_lock
, "w");
1723 int saved_errno
= errno
;
1724 rollback_lock_file(&mark_lock
);
1725 failure
|= error("Unable to write marks file %s: %s",
1726 export_marks_file
, strerror(saved_errno
));
1730 for_each_mark(marks
, 0, dump_marks_fn
, f
);
1731 if (commit_lock_file(&mark_lock
)) {
1732 failure
|= error_errno("Unable to write file %s",
1738 static void insert_object_entry(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
)
1740 struct object_entry
*e
;
1741 e
= find_object(oid
);
1743 enum object_type type
= oid_object_info(the_repository
,
1746 die("object not found: %s", oid_to_hex(oid
));
1747 e
= insert_object(oid
);
1749 e
->pack_id
= MAX_PACK_ID
;
1750 e
->idx
.offset
= 1; /* just not zero! */
1752 insert_mark(s
, mark
, e
);
1755 static void insert_oid_entry(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
)
1757 insert_mark(s
, mark
, xmemdupz(oid
, sizeof(*oid
)));
1760 static void read_mark_file(struct mark_set
**s
, FILE *f
, mark_set_inserter_t inserter
)
1763 while (fgets(line
, sizeof(line
), f
)) {
1766 struct object_id oid
;
1768 /* Ensure SHA-1 objects are padded with zeros. */
1769 memset(oid
.hash
, 0, sizeof(oid
.hash
));
1771 end
= strchr(line
, '\n');
1772 if (line
[0] != ':' || !end
)
1773 die("corrupt mark line: %s", line
);
1775 mark
= strtoumax(line
+ 1, &end
, 10);
1776 if (!mark
|| end
== line
+ 1
1778 || get_oid_hex_any(end
+ 1, &oid
) == GIT_HASH_UNKNOWN
)
1779 die("corrupt mark line: %s", line
);
1780 inserter(s
, &oid
, mark
);
1784 static void read_marks(void)
1786 FILE *f
= fopen(import_marks_file
, "r");
1789 else if (import_marks_file_ignore_missing
&& errno
== ENOENT
)
1790 goto done
; /* Marks file does not exist */
1792 die_errno("cannot read '%s'", import_marks_file
);
1793 read_mark_file(&marks
, f
, insert_object_entry
);
1796 import_marks_file_done
= 1;
1800 static int read_next_command(void)
1802 static int stdin_eof
= 0;
1805 unread_command_buf
= 0;
1810 if (unread_command_buf
) {
1811 unread_command_buf
= 0;
1813 struct recent_command
*rc
;
1815 stdin_eof
= strbuf_getline_lf(&command_buf
, stdin
);
1819 if (!seen_data_command
1820 && !starts_with(command_buf
.buf
, "feature ")
1821 && !starts_with(command_buf
.buf
, "option ")) {
1830 cmd_hist
.next
= rc
->next
;
1831 cmd_hist
.next
->prev
= &cmd_hist
;
1835 rc
->buf
= xstrdup(command_buf
.buf
);
1836 rc
->prev
= cmd_tail
;
1837 rc
->next
= cmd_hist
.prev
;
1838 rc
->prev
->next
= rc
;
1841 if (command_buf
.buf
[0] == '#')
1847 static void skip_optional_lf(void)
1849 int term_char
= fgetc(stdin
);
1850 if (term_char
!= '\n' && term_char
!= EOF
)
1851 ungetc(term_char
, stdin
);
1854 static void parse_mark(void)
1857 if (skip_prefix(command_buf
.buf
, "mark :", &v
)) {
1858 next_mark
= strtoumax(v
, NULL
, 10);
1859 read_next_command();
1865 static void parse_original_identifier(void)
1868 if (skip_prefix(command_buf
.buf
, "original-oid ", &v
))
1869 read_next_command();
1872 static int parse_data(struct strbuf
*sb
, uintmax_t limit
, uintmax_t *len_res
)
1877 if (!skip_prefix(command_buf
.buf
, "data ", &data
))
1878 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1880 if (skip_prefix(data
, "<<", &data
)) {
1881 char *term
= xstrdup(data
);
1882 size_t term_len
= command_buf
.len
- (data
- command_buf
.buf
);
1885 if (strbuf_getline_lf(&command_buf
, stdin
) == EOF
)
1886 die("EOF in data (terminator '%s' not found)", term
);
1887 if (term_len
== command_buf
.len
1888 && !strcmp(term
, command_buf
.buf
))
1890 strbuf_addbuf(sb
, &command_buf
);
1891 strbuf_addch(sb
, '\n');
1896 uintmax_t len
= strtoumax(data
, NULL
, 10);
1897 size_t n
= 0, length
= (size_t)len
;
1899 if (limit
&& limit
< len
) {
1904 die("data is too large to use in this context");
1906 while (n
< length
) {
1907 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1908 if (!s
&& feof(stdin
))
1909 die("EOF in data (%lu bytes remaining)",
1910 (unsigned long)(length
- n
));
1919 static int validate_raw_date(const char *src
, struct strbuf
*result
, int strict
)
1921 const char *orig_src
= src
;
1927 num
= strtoul(src
, &endp
, 10);
1929 * NEEDSWORK: perhaps check for reasonable values? For example, we
1930 * could error on values representing times more than a
1931 * day in the future.
1933 if (errno
|| endp
== src
|| *endp
!= ' ')
1937 if (*src
!= '-' && *src
!= '+')
1940 num
= strtoul(src
+ 1, &endp
, 10);
1942 * NEEDSWORK: check for brokenness other than num > 1400, such as
1943 * (num % 100) >= 60, or ((num % 100) % 15) != 0 ?
1945 if (errno
|| endp
== src
+ 1 || *endp
|| /* did not parse */
1946 (strict
&& (1400 < num
)) /* parsed a broken timezone */
1950 strbuf_addstr(result
, orig_src
);
1954 static char *parse_ident(const char *buf
)
1958 struct strbuf ident
= STRBUF_INIT
;
1960 /* ensure there is a space delimiter even if there is no name */
1964 ltgt
= buf
+ strcspn(buf
, "<>");
1966 die("Missing < in ident string: %s", buf
);
1967 if (ltgt
!= buf
&& ltgt
[-1] != ' ')
1968 die("Missing space before < in ident string: %s", buf
);
1969 ltgt
= ltgt
+ 1 + strcspn(ltgt
+ 1, "<>");
1971 die("Missing > in ident string: %s", buf
);
1974 die("Missing space after > in ident string: %s", buf
);
1976 name_len
= ltgt
- buf
;
1977 strbuf_add(&ident
, buf
, name_len
);
1981 if (validate_raw_date(ltgt
, &ident
, 1) < 0)
1982 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
1984 case WHENSPEC_RAW_PERMISSIVE
:
1985 if (validate_raw_date(ltgt
, &ident
, 0) < 0)
1986 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
1988 case WHENSPEC_RFC2822
:
1989 if (parse_date(ltgt
, &ident
) < 0)
1990 die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt
, buf
);
1993 if (strcmp("now", ltgt
))
1994 die("Date in ident must be 'now': %s", buf
);
1999 return strbuf_detach(&ident
, NULL
);
2002 static void parse_and_store_blob(
2003 struct last_object
*last
,
2004 struct object_id
*oidout
,
2007 static struct strbuf buf
= STRBUF_INIT
;
2010 if (parse_data(&buf
, big_file_threshold
, &len
))
2011 store_object(OBJ_BLOB
, &buf
, last
, oidout
, mark
);
2014 strbuf_release(&last
->data
);
2018 stream_blob(len
, oidout
, mark
);
2023 static void parse_new_blob(void)
2025 read_next_command();
2027 parse_original_identifier();
2028 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
2031 static void unload_one_branch(void)
2033 while (cur_active_branches
2034 && cur_active_branches
>= max_active_branches
) {
2035 uintmax_t min_commit
= ULONG_MAX
;
2036 struct branch
*e
, *l
= NULL
, *p
= NULL
;
2038 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
2039 if (e
->last_commit
< min_commit
) {
2041 min_commit
= e
->last_commit
;
2047 e
= p
->active_next_branch
;
2048 p
->active_next_branch
= e
->active_next_branch
;
2050 e
= active_branches
;
2051 active_branches
= e
->active_next_branch
;
2054 e
->active_next_branch
= NULL
;
2055 if (e
->branch_tree
.tree
) {
2056 release_tree_content_recursive(e
->branch_tree
.tree
);
2057 e
->branch_tree
.tree
= NULL
;
2059 cur_active_branches
--;
2063 static void load_branch(struct branch
*b
)
2065 load_tree(&b
->branch_tree
);
2068 b
->active_next_branch
= active_branches
;
2069 active_branches
= b
;
2070 cur_active_branches
++;
2071 branch_load_count
++;
2075 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2077 unsigned char fanout
= 0;
2078 while ((num_notes
>>= 8))
2083 static void construct_path_with_fanout(const char *hex_sha1
,
2084 unsigned char fanout
, char *path
)
2086 unsigned int i
= 0, j
= 0;
2087 if (fanout
>= the_hash_algo
->rawsz
)
2088 die("Too large fanout (%u)", fanout
);
2090 path
[i
++] = hex_sha1
[j
++];
2091 path
[i
++] = hex_sha1
[j
++];
2095 memcpy(path
+ i
, hex_sha1
+ j
, the_hash_algo
->hexsz
- j
);
2096 path
[i
+ the_hash_algo
->hexsz
- j
] = '\0';
2099 static uintmax_t do_change_note_fanout(
2100 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2101 char *hex_oid
, unsigned int hex_oid_len
,
2102 char *fullpath
, unsigned int fullpath_len
,
2103 unsigned char fanout
)
2105 struct tree_content
*t
;
2106 struct tree_entry
*e
, leaf
;
2107 unsigned int i
, tmp_hex_oid_len
, tmp_fullpath_len
;
2108 uintmax_t num_notes
= 0;
2109 struct object_id oid
;
2110 /* hex oid + '/' between each pair of hex digits + NUL */
2111 char realpath
[GIT_MAX_HEXSZ
+ ((GIT_MAX_HEXSZ
/ 2) - 1) + 1];
2112 const unsigned hexsz
= the_hash_algo
->hexsz
;
2118 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2120 tmp_hex_oid_len
= hex_oid_len
+ e
->name
->str_len
;
2121 tmp_fullpath_len
= fullpath_len
;
2124 * We're interested in EITHER existing note entries (entries
2125 * with exactly 40 hex chars in path, not including directory
2126 * separators), OR directory entries that may contain note
2127 * entries (with < 40 hex chars in path).
2128 * Also, each path component in a note entry must be a multiple
2131 if (!e
->versions
[1].mode
||
2132 tmp_hex_oid_len
> hexsz
||
2133 e
->name
->str_len
% 2)
2136 /* This _may_ be a note entry, or a subdir containing notes */
2137 memcpy(hex_oid
+ hex_oid_len
, e
->name
->str_dat
,
2139 if (tmp_fullpath_len
)
2140 fullpath
[tmp_fullpath_len
++] = '/';
2141 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2143 tmp_fullpath_len
+= e
->name
->str_len
;
2144 fullpath
[tmp_fullpath_len
] = '\0';
2146 if (tmp_hex_oid_len
== hexsz
&& !get_oid_hex(hex_oid
, &oid
)) {
2147 /* This is a note entry */
2148 if (fanout
== 0xff) {
2149 /* Counting mode, no rename */
2153 construct_path_with_fanout(hex_oid
, fanout
, realpath
);
2154 if (!strcmp(fullpath
, realpath
)) {
2155 /* Note entry is in correct location */
2160 /* Rename fullpath to realpath */
2161 if (!tree_content_remove(orig_root
, fullpath
, &leaf
, 0))
2162 die("Failed to remove path %s", fullpath
);
2163 tree_content_set(orig_root
, realpath
,
2164 &leaf
.versions
[1].oid
,
2165 leaf
.versions
[1].mode
,
2167 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2168 /* This is a subdir that may contain note entries */
2169 num_notes
+= do_change_note_fanout(orig_root
, e
,
2170 hex_oid
, tmp_hex_oid_len
,
2171 fullpath
, tmp_fullpath_len
, fanout
);
2174 /* The above may have reallocated the current tree_content */
2180 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2181 unsigned char fanout
)
2184 * The size of path is due to one slash between every two hex digits,
2185 * plus the terminating NUL. Note that there is no slash at the end, so
2186 * the number of slashes is one less than half the number of hex
2189 char hex_oid
[GIT_MAX_HEXSZ
], path
[GIT_MAX_HEXSZ
+ (GIT_MAX_HEXSZ
/ 2) - 1 + 1];
2190 return do_change_note_fanout(root
, root
, hex_oid
, 0, path
, 0, fanout
);
2193 static int parse_mapped_oid_hex(const char *hex
, struct object_id
*oid
, const char **end
)
2198 /* Make SHA-1 object IDs have all-zero padding. */
2199 memset(oid
->hash
, 0, sizeof(oid
->hash
));
2201 algo
= parse_oid_hex_any(hex
, oid
, end
);
2202 if (algo
== GIT_HASH_UNKNOWN
)
2205 it
= kh_get_oid_map(sub_oid_map
, *oid
);
2206 /* No such object? */
2207 if (it
== kh_end(sub_oid_map
)) {
2208 /* If we're using the same algorithm, pass it through. */
2209 if (hash_algos
[algo
].format_id
== the_hash_algo
->format_id
)
2213 oidcpy(oid
, kh_value(sub_oid_map
, it
));
2218 * Given a pointer into a string, parse a mark reference:
2220 * idnum ::= ':' bigint;
2222 * Return the first character after the value in *endptr.
2224 * Complain if the following character is not what is expected,
2225 * either a space or end of the string.
2227 static uintmax_t parse_mark_ref(const char *p
, char **endptr
)
2233 mark
= strtoumax(p
, endptr
, 10);
2235 die("No value after ':' in mark: %s", command_buf
.buf
);
2240 * Parse the mark reference, and complain if this is not the end of
2243 static uintmax_t parse_mark_ref_eol(const char *p
)
2248 mark
= parse_mark_ref(p
, &end
);
2250 die("Garbage after mark: %s", command_buf
.buf
);
2255 * Parse the mark reference, demanding a trailing space. Return a
2256 * pointer to the space.
2258 static uintmax_t parse_mark_ref_space(const char **p
)
2263 mark
= parse_mark_ref(*p
, &end
);
2265 die("Missing space after mark: %s", command_buf
.buf
);
2270 static void file_change_m(const char *p
, struct branch
*b
)
2272 static struct strbuf uq
= STRBUF_INIT
;
2274 struct object_entry
*oe
;
2275 struct object_id oid
;
2276 uint16_t mode
, inline_data
= 0;
2278 p
= get_mode(p
, &mode
);
2280 die("Corrupt mode: %s", command_buf
.buf
);
2285 case S_IFREG
| 0644:
2286 case S_IFREG
| 0755:
2293 die("Corrupt mode: %s", command_buf
.buf
);
2297 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2298 oidcpy(&oid
, &oe
->idx
.oid
);
2299 } else if (skip_prefix(p
, "inline ", &p
)) {
2301 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2303 if (parse_mapped_oid_hex(p
, &oid
, &p
))
2304 die("Invalid dataref: %s", command_buf
.buf
);
2305 oe
= find_object(&oid
);
2307 die("Missing space after SHA1: %s", command_buf
.buf
);
2311 if (!unquote_c_style(&uq
, p
, &endp
)) {
2313 die("Garbage after path in: %s", command_buf
.buf
);
2317 /* Git does not track empty, non-toplevel directories. */
2318 if (S_ISDIR(mode
) && is_empty_tree_oid(&oid
) && *p
) {
2319 tree_content_remove(&b
->branch_tree
, p
, NULL
, 0);
2323 if (S_ISGITLINK(mode
)) {
2325 die("Git links cannot be specified 'inline': %s",
2328 if (oe
->type
!= OBJ_COMMIT
)
2329 die("Not a commit (actually a %s): %s",
2330 type_name(oe
->type
), command_buf
.buf
);
2333 * Accept the sha1 without checking; it expected to be in
2334 * another repository.
2336 } else if (inline_data
) {
2338 die("Directories cannot be specified 'inline': %s",
2341 strbuf_addstr(&uq
, p
);
2344 while (read_next_command() != EOF
) {
2346 if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2349 parse_and_store_blob(&last_blob
, &oid
, 0);
2354 enum object_type expected
= S_ISDIR(mode
) ?
2356 enum object_type type
= oe
? oe
->type
:
2357 oid_object_info(the_repository
, &oid
,
2360 die("%s not found: %s",
2361 S_ISDIR(mode
) ? "Tree" : "Blob",
2363 if (type
!= expected
)
2364 die("Not a %s (actually a %s): %s",
2365 type_name(expected
), type_name(type
),
2370 tree_content_replace(&b
->branch_tree
, &oid
, mode
, NULL
);
2373 tree_content_set(&b
->branch_tree
, p
, &oid
, mode
, NULL
);
2376 static void file_change_d(const char *p
, struct branch
*b
)
2378 static struct strbuf uq
= STRBUF_INIT
;
2382 if (!unquote_c_style(&uq
, p
, &endp
)) {
2384 die("Garbage after path in: %s", command_buf
.buf
);
2387 tree_content_remove(&b
->branch_tree
, p
, NULL
, 1);
2390 static void file_change_cr(const char *s
, struct branch
*b
, int rename
)
2393 static struct strbuf s_uq
= STRBUF_INIT
;
2394 static struct strbuf d_uq
= STRBUF_INIT
;
2396 struct tree_entry leaf
;
2398 strbuf_reset(&s_uq
);
2399 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2401 die("Missing space after source: %s", command_buf
.buf
);
2403 endp
= strchr(s
, ' ');
2405 die("Missing space after source: %s", command_buf
.buf
);
2406 strbuf_add(&s_uq
, s
, endp
- s
);
2412 die("Missing dest: %s", command_buf
.buf
);
2415 strbuf_reset(&d_uq
);
2416 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2418 die("Garbage after dest in: %s", command_buf
.buf
);
2422 memset(&leaf
, 0, sizeof(leaf
));
2424 tree_content_remove(&b
->branch_tree
, s
, &leaf
, 1);
2426 tree_content_get(&b
->branch_tree
, s
, &leaf
, 1);
2427 if (!leaf
.versions
[1].mode
)
2428 die("Path %s not in branch", s
);
2429 if (!*d
) { /* C "path/to/subdir" "" */
2430 tree_content_replace(&b
->branch_tree
,
2431 &leaf
.versions
[1].oid
,
2432 leaf
.versions
[1].mode
,
2436 tree_content_set(&b
->branch_tree
, d
,
2437 &leaf
.versions
[1].oid
,
2438 leaf
.versions
[1].mode
,
2442 static void note_change_n(const char *p
, struct branch
*b
, unsigned char *old_fanout
)
2444 static struct strbuf uq
= STRBUF_INIT
;
2445 struct object_entry
*oe
;
2447 struct object_id oid
, commit_oid
;
2448 char path
[GIT_MAX_RAWSZ
* 3];
2449 uint16_t inline_data
= 0;
2450 unsigned char new_fanout
;
2453 * When loading a branch, we don't traverse its tree to count the real
2454 * number of notes (too expensive to do this for all non-note refs).
2455 * This means that recently loaded notes refs might incorrectly have
2456 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2458 * Fix this by traversing the tree and counting the number of notes
2459 * when b->num_notes == 0. If the notes tree is truly empty, the
2460 * calculation should not take long.
2462 if (b
->num_notes
== 0 && *old_fanout
== 0) {
2463 /* Invoke change_note_fanout() in "counting mode". */
2464 b
->num_notes
= change_note_fanout(&b
->branch_tree
, 0xff);
2465 *old_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2468 /* Now parse the notemodify command. */
2469 /* <dataref> or 'inline' */
2471 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2472 oidcpy(&oid
, &oe
->idx
.oid
);
2473 } else if (skip_prefix(p
, "inline ", &p
)) {
2475 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2477 if (parse_mapped_oid_hex(p
, &oid
, &p
))
2478 die("Invalid dataref: %s", command_buf
.buf
);
2479 oe
= find_object(&oid
);
2481 die("Missing space after SHA1: %s", command_buf
.buf
);
2485 s
= lookup_branch(p
);
2487 if (is_null_oid(&s
->oid
))
2488 die("Can't add a note on empty branch.");
2489 oidcpy(&commit_oid
, &s
->oid
);
2490 } else if (*p
== ':') {
2491 uintmax_t commit_mark
= parse_mark_ref_eol(p
);
2492 struct object_entry
*commit_oe
= find_mark(marks
, commit_mark
);
2493 if (commit_oe
->type
!= OBJ_COMMIT
)
2494 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2495 oidcpy(&commit_oid
, &commit_oe
->idx
.oid
);
2496 } else if (!repo_get_oid(the_repository
, p
, &commit_oid
)) {
2498 char *buf
= read_object_with_reference(the_repository
,
2502 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2503 die("Not a valid commit: %s", p
);
2506 die("Invalid ref name or SHA1 expression: %s", p
);
2510 strbuf_addstr(&uq
, p
);
2513 read_next_command();
2514 parse_and_store_blob(&last_blob
, &oid
, 0);
2516 if (oe
->type
!= OBJ_BLOB
)
2517 die("Not a blob (actually a %s): %s",
2518 type_name(oe
->type
), command_buf
.buf
);
2519 } else if (!is_null_oid(&oid
)) {
2520 enum object_type type
= oid_object_info(the_repository
, &oid
,
2523 die("Blob not found: %s", command_buf
.buf
);
2524 if (type
!= OBJ_BLOB
)
2525 die("Not a blob (actually a %s): %s",
2526 type_name(type
), command_buf
.buf
);
2529 construct_path_with_fanout(oid_to_hex(&commit_oid
), *old_fanout
, path
);
2530 if (tree_content_remove(&b
->branch_tree
, path
, NULL
, 0))
2533 if (is_null_oid(&oid
))
2534 return; /* nothing to insert */
2537 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2538 construct_path_with_fanout(oid_to_hex(&commit_oid
), new_fanout
, path
);
2539 tree_content_set(&b
->branch_tree
, path
, &oid
, S_IFREG
| 0644, NULL
);
2542 static void file_change_deleteall(struct branch
*b
)
2544 release_tree_content_recursive(b
->branch_tree
.tree
);
2545 oidclr(&b
->branch_tree
.versions
[0].oid
);
2546 oidclr(&b
->branch_tree
.versions
[1].oid
);
2547 load_tree(&b
->branch_tree
);
2551 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2553 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2554 die("Not a valid commit: %s", oid_to_hex(&b
->oid
));
2555 if (memcmp("tree ", buf
, 5)
2556 || get_oid_hex(buf
+ 5, &b
->branch_tree
.versions
[1].oid
))
2557 die("The commit %s is corrupt", oid_to_hex(&b
->oid
));
2558 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2559 &b
->branch_tree
.versions
[1].oid
);
2562 static void parse_from_existing(struct branch
*b
)
2564 if (is_null_oid(&b
->oid
)) {
2565 oidclr(&b
->branch_tree
.versions
[0].oid
);
2566 oidclr(&b
->branch_tree
.versions
[1].oid
);
2571 buf
= read_object_with_reference(the_repository
,
2572 &b
->oid
, OBJ_COMMIT
, &size
,
2574 parse_from_commit(b
, buf
, size
);
2579 static int parse_objectish(struct branch
*b
, const char *objectish
)
2582 struct object_id oid
;
2584 oidcpy(&oid
, &b
->branch_tree
.versions
[1].oid
);
2586 s
= lookup_branch(objectish
);
2588 die("Can't create a branch from itself: %s", b
->name
);
2590 struct object_id
*t
= &s
->branch_tree
.versions
[1].oid
;
2591 oidcpy(&b
->oid
, &s
->oid
);
2592 oidcpy(&b
->branch_tree
.versions
[0].oid
, t
);
2593 oidcpy(&b
->branch_tree
.versions
[1].oid
, t
);
2594 } else if (*objectish
== ':') {
2595 uintmax_t idnum
= parse_mark_ref_eol(objectish
);
2596 struct object_entry
*oe
= find_mark(marks
, idnum
);
2597 if (oe
->type
!= OBJ_COMMIT
)
2598 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2599 if (!oideq(&b
->oid
, &oe
->idx
.oid
)) {
2600 oidcpy(&b
->oid
, &oe
->idx
.oid
);
2601 if (oe
->pack_id
!= MAX_PACK_ID
) {
2603 char *buf
= gfi_unpack_entry(oe
, &size
);
2604 parse_from_commit(b
, buf
, size
);
2607 parse_from_existing(b
);
2609 } else if (!repo_get_oid(the_repository
, objectish
, &b
->oid
)) {
2610 parse_from_existing(b
);
2611 if (is_null_oid(&b
->oid
))
2615 die("Invalid ref name or SHA1 expression: %s", objectish
);
2617 if (b
->branch_tree
.tree
&& !oideq(&oid
, &b
->branch_tree
.versions
[1].oid
)) {
2618 release_tree_content_recursive(b
->branch_tree
.tree
);
2619 b
->branch_tree
.tree
= NULL
;
2622 read_next_command();
2626 static int parse_from(struct branch
*b
)
2630 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2633 return parse_objectish(b
, from
);
2636 static int parse_objectish_with_prefix(struct branch
*b
, const char *prefix
)
2640 if (!skip_prefix(command_buf
.buf
, prefix
, &base
))
2643 return parse_objectish(b
, base
);
2646 static struct hash_list
*parse_merge(unsigned int *count
)
2648 struct hash_list
*list
= NULL
, **tail
= &list
, *n
;
2653 while (skip_prefix(command_buf
.buf
, "merge ", &from
)) {
2654 n
= xmalloc(sizeof(*n
));
2655 s
= lookup_branch(from
);
2657 oidcpy(&n
->oid
, &s
->oid
);
2658 else if (*from
== ':') {
2659 uintmax_t idnum
= parse_mark_ref_eol(from
);
2660 struct object_entry
*oe
= find_mark(marks
, idnum
);
2661 if (oe
->type
!= OBJ_COMMIT
)
2662 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2663 oidcpy(&n
->oid
, &oe
->idx
.oid
);
2664 } else if (!repo_get_oid(the_repository
, from
, &n
->oid
)) {
2666 char *buf
= read_object_with_reference(the_repository
,
2670 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2671 die("Not a valid commit: %s", from
);
2674 die("Invalid ref name or SHA1 expression: %s", from
);
2681 read_next_command();
2686 static void parse_new_commit(const char *arg
)
2688 static struct strbuf msg
= STRBUF_INIT
;
2690 char *author
= NULL
;
2691 char *committer
= NULL
;
2692 char *encoding
= NULL
;
2693 struct hash_list
*merge_list
= NULL
;
2694 unsigned int merge_count
;
2695 unsigned char prev_fanout
, new_fanout
;
2698 b
= lookup_branch(arg
);
2700 b
= new_branch(arg
);
2702 read_next_command();
2704 parse_original_identifier();
2705 if (skip_prefix(command_buf
.buf
, "author ", &v
)) {
2706 author
= parse_ident(v
);
2707 read_next_command();
2709 if (skip_prefix(command_buf
.buf
, "committer ", &v
)) {
2710 committer
= parse_ident(v
);
2711 read_next_command();
2714 die("Expected committer but didn't get one");
2715 if (skip_prefix(command_buf
.buf
, "encoding ", &v
)) {
2716 encoding
= xstrdup(v
);
2717 read_next_command();
2719 parse_data(&msg
, 0, NULL
);
2720 read_next_command();
2722 merge_list
= parse_merge(&merge_count
);
2724 /* ensure the branch is active/loaded */
2725 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2726 unload_one_branch();
2730 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2733 while (command_buf
.len
> 0) {
2734 if (skip_prefix(command_buf
.buf
, "M ", &v
))
2735 file_change_m(v
, b
);
2736 else if (skip_prefix(command_buf
.buf
, "D ", &v
))
2737 file_change_d(v
, b
);
2738 else if (skip_prefix(command_buf
.buf
, "R ", &v
))
2739 file_change_cr(v
, b
, 1);
2740 else if (skip_prefix(command_buf
.buf
, "C ", &v
))
2741 file_change_cr(v
, b
, 0);
2742 else if (skip_prefix(command_buf
.buf
, "N ", &v
))
2743 note_change_n(v
, b
, &prev_fanout
);
2744 else if (!strcmp("deleteall", command_buf
.buf
))
2745 file_change_deleteall(b
);
2746 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
2748 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2751 unread_command_buf
= 1;
2754 if (read_next_command() == EOF
)
2758 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2759 if (new_fanout
!= prev_fanout
)
2760 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2762 /* build the tree and the commit */
2763 store_tree(&b
->branch_tree
);
2764 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2765 &b
->branch_tree
.versions
[1].oid
);
2767 strbuf_reset(&new_data
);
2768 strbuf_addf(&new_data
, "tree %s\n",
2769 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
2770 if (!is_null_oid(&b
->oid
))
2771 strbuf_addf(&new_data
, "parent %s\n",
2772 oid_to_hex(&b
->oid
));
2773 while (merge_list
) {
2774 struct hash_list
*next
= merge_list
->next
;
2775 strbuf_addf(&new_data
, "parent %s\n",
2776 oid_to_hex(&merge_list
->oid
));
2780 strbuf_addf(&new_data
,
2783 author
? author
: committer
, committer
);
2785 strbuf_addf(&new_data
,
2788 strbuf_addch(&new_data
, '\n');
2789 strbuf_addbuf(&new_data
, &msg
);
2794 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, &b
->oid
, next_mark
))
2795 b
->pack_id
= pack_id
;
2796 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2799 static void parse_new_tag(const char *arg
)
2801 static struct strbuf msg
= STRBUF_INIT
;
2806 uintmax_t from_mark
= 0;
2807 struct object_id oid
;
2808 enum object_type type
;
2811 t
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct tag
));
2812 memset(t
, 0, sizeof(struct tag
));
2813 t
->name
= mem_pool_strdup(&fi_mem_pool
, arg
);
2815 last_tag
->next_tag
= t
;
2819 read_next_command();
2823 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2824 die("Expected from command, got %s", command_buf
.buf
);
2825 s
= lookup_branch(from
);
2827 if (is_null_oid(&s
->oid
))
2828 die("Can't tag an empty branch.");
2829 oidcpy(&oid
, &s
->oid
);
2831 } else if (*from
== ':') {
2832 struct object_entry
*oe
;
2833 from_mark
= parse_mark_ref_eol(from
);
2834 oe
= find_mark(marks
, from_mark
);
2836 oidcpy(&oid
, &oe
->idx
.oid
);
2837 } else if (!repo_get_oid(the_repository
, from
, &oid
)) {
2838 struct object_entry
*oe
= find_object(&oid
);
2840 type
= oid_object_info(the_repository
, &oid
, NULL
);
2842 die("Not a valid object: %s", from
);
2846 die("Invalid ref name or SHA1 expression: %s", from
);
2847 read_next_command();
2849 /* original-oid ... */
2850 parse_original_identifier();
2853 if (skip_prefix(command_buf
.buf
, "tagger ", &v
)) {
2854 tagger
= parse_ident(v
);
2855 read_next_command();
2859 /* tag payload/message */
2860 parse_data(&msg
, 0, NULL
);
2862 /* build the tag object */
2863 strbuf_reset(&new_data
);
2865 strbuf_addf(&new_data
,
2869 oid_to_hex(&oid
), type_name(type
), t
->name
);
2871 strbuf_addf(&new_data
,
2872 "tagger %s\n", tagger
);
2873 strbuf_addch(&new_data
, '\n');
2874 strbuf_addbuf(&new_data
, &msg
);
2877 if (store_object(OBJ_TAG
, &new_data
, NULL
, &t
->oid
, next_mark
))
2878 t
->pack_id
= MAX_PACK_ID
;
2880 t
->pack_id
= pack_id
;
2883 static void parse_reset_branch(const char *arg
)
2886 const char *tag_name
;
2888 b
= lookup_branch(arg
);
2891 oidclr(&b
->branch_tree
.versions
[0].oid
);
2892 oidclr(&b
->branch_tree
.versions
[1].oid
);
2893 if (b
->branch_tree
.tree
) {
2894 release_tree_content_recursive(b
->branch_tree
.tree
);
2895 b
->branch_tree
.tree
= NULL
;
2899 b
= new_branch(arg
);
2900 read_next_command();
2902 if (b
->delete && skip_prefix(b
->name
, "refs/tags/", &tag_name
)) {
2904 * Elsewhere, we call dump_branches() before dump_tags(),
2905 * and dump_branches() will handle ref deletions first, so
2906 * in order to make sure the deletion actually takes effect,
2907 * we need to remove the tag from our list of tags to update.
2909 * NEEDSWORK: replace list of tags with hashmap for faster
2912 struct tag
*t
, *prev
= NULL
;
2913 for (t
= first_tag
; t
; t
= t
->next_tag
) {
2914 if (!strcmp(t
->name
, tag_name
))
2920 prev
->next_tag
= t
->next_tag
;
2922 first_tag
= t
->next_tag
;
2925 /* There is no mem_pool_free(t) function to call. */
2928 if (command_buf
.len
> 0)
2929 unread_command_buf
= 1;
2932 static void cat_blob_write(const char *buf
, unsigned long size
)
2934 if (write_in_full(cat_blob_fd
, buf
, size
) < 0)
2935 die_errno("Write to frontend failed");
2938 static void cat_blob(struct object_entry
*oe
, struct object_id
*oid
)
2940 struct strbuf line
= STRBUF_INIT
;
2942 enum object_type type
= 0;
2945 if (!oe
|| oe
->pack_id
== MAX_PACK_ID
) {
2946 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
2949 buf
= gfi_unpack_entry(oe
, &size
);
2953 * Output based on batch_one_object() from cat-file.c.
2956 strbuf_reset(&line
);
2957 strbuf_addf(&line
, "%s missing\n", oid_to_hex(oid
));
2958 cat_blob_write(line
.buf
, line
.len
);
2959 strbuf_release(&line
);
2964 die("Can't read object %s", oid_to_hex(oid
));
2965 if (type
!= OBJ_BLOB
)
2966 die("Object %s is a %s but a blob was expected.",
2967 oid_to_hex(oid
), type_name(type
));
2968 strbuf_reset(&line
);
2969 strbuf_addf(&line
, "%s %s %"PRIuMAX
"\n", oid_to_hex(oid
),
2970 type_name(type
), (uintmax_t)size
);
2971 cat_blob_write(line
.buf
, line
.len
);
2972 strbuf_release(&line
);
2973 cat_blob_write(buf
, size
);
2974 cat_blob_write("\n", 1);
2975 if (oe
&& oe
->pack_id
== pack_id
) {
2976 last_blob
.offset
= oe
->idx
.offset
;
2977 strbuf_attach(&last_blob
.data
, buf
, size
, size
);
2978 last_blob
.depth
= oe
->depth
;
2983 static void parse_get_mark(const char *p
)
2985 struct object_entry
*oe
;
2986 char output
[GIT_MAX_HEXSZ
+ 2];
2988 /* get-mark SP <object> LF */
2990 die("Not a mark: %s", p
);
2992 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
2994 die("Unknown mark: %s", command_buf
.buf
);
2996 xsnprintf(output
, sizeof(output
), "%s\n", oid_to_hex(&oe
->idx
.oid
));
2997 cat_blob_write(output
, the_hash_algo
->hexsz
+ 1);
3000 static void parse_cat_blob(const char *p
)
3002 struct object_entry
*oe
;
3003 struct object_id oid
;
3005 /* cat-blob SP <object> LF */
3007 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
3009 die("Unknown mark: %s", command_buf
.buf
);
3010 oidcpy(&oid
, &oe
->idx
.oid
);
3012 if (parse_mapped_oid_hex(p
, &oid
, &p
))
3013 die("Invalid dataref: %s", command_buf
.buf
);
3015 die("Garbage after SHA1: %s", command_buf
.buf
);
3016 oe
= find_object(&oid
);
3022 static struct object_entry
*dereference(struct object_entry
*oe
,
3023 struct object_id
*oid
)
3027 const unsigned hexsz
= the_hash_algo
->hexsz
;
3030 enum object_type type
= oid_object_info(the_repository
, oid
,
3033 die("object not found: %s", oid_to_hex(oid
));
3035 oe
= insert_object(oid
);
3037 oe
->pack_id
= MAX_PACK_ID
;
3041 case OBJ_TREE
: /* easy case. */
3047 die("Not a tree-ish: %s", command_buf
.buf
);
3050 if (oe
->pack_id
!= MAX_PACK_ID
) { /* in a pack being written */
3051 buf
= gfi_unpack_entry(oe
, &size
);
3053 enum object_type unused
;
3054 buf
= repo_read_object_file(the_repository
, oid
, &unused
,
3058 die("Can't load object %s", oid_to_hex(oid
));
3060 /* Peel one layer. */
3063 if (size
< hexsz
+ strlen("object ") ||
3064 get_oid_hex(buf
+ strlen("object "), oid
))
3065 die("Invalid SHA1 in tag: %s", command_buf
.buf
);
3068 if (size
< hexsz
+ strlen("tree ") ||
3069 get_oid_hex(buf
+ strlen("tree "), oid
))
3070 die("Invalid SHA1 in commit: %s", command_buf
.buf
);
3074 return find_object(oid
);
3077 static void insert_mapped_mark(uintmax_t mark
, void *object
, void *cbp
)
3079 struct object_id
*fromoid
= object
;
3080 struct object_id
*tooid
= find_mark(cbp
, mark
);
3084 it
= kh_put_oid_map(sub_oid_map
, *fromoid
, &ret
);
3085 /* We've already seen this object. */
3088 kh_value(sub_oid_map
, it
) = tooid
;
3091 static void build_mark_map_one(struct mark_set
*from
, struct mark_set
*to
)
3093 for_each_mark(from
, 0, insert_mapped_mark
, to
);
3096 static void build_mark_map(struct string_list
*from
, struct string_list
*to
)
3098 struct string_list_item
*fromp
, *top
;
3100 sub_oid_map
= kh_init_oid_map();
3102 for_each_string_list_item(fromp
, from
) {
3103 top
= string_list_lookup(to
, fromp
->string
);
3105 die(_("Missing from marks for submodule '%s'"), fromp
->string
);
3106 } else if (!top
|| !top
->util
) {
3107 die(_("Missing to marks for submodule '%s'"), fromp
->string
);
3109 build_mark_map_one(fromp
->util
, top
->util
);
3113 static struct object_entry
*parse_treeish_dataref(const char **p
)
3115 struct object_id oid
;
3116 struct object_entry
*e
;
3118 if (**p
== ':') { /* <mark> */
3119 e
= find_mark(marks
, parse_mark_ref_space(p
));
3121 die("Unknown mark: %s", command_buf
.buf
);
3122 oidcpy(&oid
, &e
->idx
.oid
);
3123 } else { /* <sha1> */
3124 if (parse_mapped_oid_hex(*p
, &oid
, p
))
3125 die("Invalid dataref: %s", command_buf
.buf
);
3126 e
= find_object(&oid
);
3128 die("Missing space after tree-ish: %s", command_buf
.buf
);
3131 while (!e
|| e
->type
!= OBJ_TREE
)
3132 e
= dereference(e
, &oid
);
3136 static void print_ls(int mode
, const unsigned char *hash
, const char *path
)
3138 static struct strbuf line
= STRBUF_INIT
;
3140 /* See show_tree(). */
3142 S_ISGITLINK(mode
) ? commit_type
:
3143 S_ISDIR(mode
) ? tree_type
:
3147 /* missing SP path LF */
3148 strbuf_reset(&line
);
3149 strbuf_addstr(&line
, "missing ");
3150 quote_c_style(path
, &line
, NULL
, 0);
3151 strbuf_addch(&line
, '\n');
3153 /* mode SP type SP object_name TAB path LF */
3154 strbuf_reset(&line
);
3155 strbuf_addf(&line
, "%06o %s %s\t",
3156 mode
& ~NO_DELTA
, type
, hash_to_hex(hash
));
3157 quote_c_style(path
, &line
, NULL
, 0);
3158 strbuf_addch(&line
, '\n');
3160 cat_blob_write(line
.buf
, line
.len
);
3163 static void parse_ls(const char *p
, struct branch
*b
)
3165 struct tree_entry
*root
= NULL
;
3166 struct tree_entry leaf
= {NULL
};
3168 /* ls SP (<tree-ish> SP)? <path> */
3171 die("Not in a commit: %s", command_buf
.buf
);
3172 root
= &b
->branch_tree
;
3174 struct object_entry
*e
= parse_treeish_dataref(&p
);
3175 root
= new_tree_entry();
3176 oidcpy(&root
->versions
[1].oid
, &e
->idx
.oid
);
3177 if (!is_null_oid(&root
->versions
[1].oid
))
3178 root
->versions
[1].mode
= S_IFDIR
;
3182 static struct strbuf uq
= STRBUF_INIT
;
3185 if (unquote_c_style(&uq
, p
, &endp
))
3186 die("Invalid path: %s", command_buf
.buf
);
3188 die("Garbage after path in: %s", command_buf
.buf
);
3191 tree_content_get(root
, p
, &leaf
, 1);
3193 * A directory in preparation would have a sha1 of zero
3194 * until it is saved. Save, for simplicity.
3196 if (S_ISDIR(leaf
.versions
[1].mode
))
3199 print_ls(leaf
.versions
[1].mode
, leaf
.versions
[1].oid
.hash
, p
);
3201 release_tree_content_recursive(leaf
.tree
);
3202 if (!b
|| root
!= &b
->branch_tree
)
3203 release_tree_entry(root
);
3206 static void checkpoint(void)
3208 checkpoint_requested
= 0;
3217 static void parse_checkpoint(void)
3219 checkpoint_requested
= 1;
3223 static void parse_progress(void)
3225 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
3226 fputc('\n', stdout
);
3231 static void parse_alias(void)
3233 struct object_entry
*e
;
3237 read_next_command();
3242 die(_("Expected 'mark' command, got %s"), command_buf
.buf
);
3245 memset(&b
, 0, sizeof(b
));
3246 if (!parse_objectish_with_prefix(&b
, "to "))
3247 die(_("Expected 'to' command, got %s"), command_buf
.buf
);
3248 e
= find_object(&b
.oid
);
3250 insert_mark(&marks
, next_mark
, e
);
3253 static char* make_fast_import_path(const char *path
)
3255 if (!relative_marks_paths
|| is_absolute_path(path
))
3256 return prefix_filename(global_prefix
, path
);
3257 return git_pathdup("info/fast-import/%s", path
);
3260 static void option_import_marks(const char *marks
,
3261 int from_stream
, int ignore_missing
)
3263 if (import_marks_file
) {
3265 die("Only one import-marks command allowed per stream");
3267 /* read previous mark file */
3268 if(!import_marks_file_from_stream
)
3272 import_marks_file
= make_fast_import_path(marks
);
3273 import_marks_file_from_stream
= from_stream
;
3274 import_marks_file_ignore_missing
= ignore_missing
;
3277 static void option_date_format(const char *fmt
)
3279 if (!strcmp(fmt
, "raw"))
3280 whenspec
= WHENSPEC_RAW
;
3281 else if (!strcmp(fmt
, "raw-permissive"))
3282 whenspec
= WHENSPEC_RAW_PERMISSIVE
;
3283 else if (!strcmp(fmt
, "rfc2822"))
3284 whenspec
= WHENSPEC_RFC2822
;
3285 else if (!strcmp(fmt
, "now"))
3286 whenspec
= WHENSPEC_NOW
;
3288 die("unknown --date-format argument %s", fmt
);
3291 static unsigned long ulong_arg(const char *option
, const char *arg
)
3294 unsigned long rv
= strtoul(arg
, &endptr
, 0);
3295 if (strchr(arg
, '-') || endptr
== arg
|| *endptr
)
3296 die("%s: argument must be a non-negative integer", option
);
3300 static void option_depth(const char *depth
)
3302 max_depth
= ulong_arg("--depth", depth
);
3303 if (max_depth
> MAX_DEPTH
)
3304 die("--depth cannot exceed %u", MAX_DEPTH
);
3307 static void option_active_branches(const char *branches
)
3309 max_active_branches
= ulong_arg("--active-branches", branches
);
3312 static void option_export_marks(const char *marks
)
3314 export_marks_file
= make_fast_import_path(marks
);
3317 static void option_cat_blob_fd(const char *fd
)
3319 unsigned long n
= ulong_arg("--cat-blob-fd", fd
);
3320 if (n
> (unsigned long) INT_MAX
)
3321 die("--cat-blob-fd cannot exceed %d", INT_MAX
);
3322 cat_blob_fd
= (int) n
;
3325 static void option_export_pack_edges(const char *edges
)
3327 char *fn
= prefix_filename(global_prefix
, edges
);
3330 pack_edges
= xfopen(fn
, "a");
3334 static void option_rewrite_submodules(const char *arg
, struct string_list
*list
)
3336 struct mark_set
*ms
;
3338 char *s
= xstrdup(arg
);
3339 char *f
= strchr(s
, ':');
3341 die(_("Expected format name:filename for submodule rewrite option"));
3344 CALLOC_ARRAY(ms
, 1);
3346 f
= prefix_filename(global_prefix
, f
);
3349 die_errno("cannot read '%s'", f
);
3350 read_mark_file(&ms
, fp
, insert_oid_entry
);
3354 string_list_insert(list
, s
)->util
= ms
;
3357 static int parse_one_option(const char *option
)
3359 if (skip_prefix(option
, "max-pack-size=", &option
)) {
3361 if (!git_parse_ulong(option
, &v
))
3364 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v
);
3366 } else if (v
< 1024 * 1024) {
3367 warning("minimum max-pack-size is 1 MiB");
3371 } else if (skip_prefix(option
, "big-file-threshold=", &option
)) {
3373 if (!git_parse_ulong(option
, &v
))
3375 big_file_threshold
= v
;
3376 } else if (skip_prefix(option
, "depth=", &option
)) {
3377 option_depth(option
);
3378 } else if (skip_prefix(option
, "active-branches=", &option
)) {
3379 option_active_branches(option
);
3380 } else if (skip_prefix(option
, "export-pack-edges=", &option
)) {
3381 option_export_pack_edges(option
);
3382 } else if (!strcmp(option
, "quiet")) {
3384 } else if (!strcmp(option
, "stats")) {
3386 } else if (!strcmp(option
, "allow-unsafe-features")) {
3387 ; /* already handled during early option parsing */
3395 static void check_unsafe_feature(const char *feature
, int from_stream
)
3397 if (from_stream
&& !allow_unsafe_features
)
3398 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3402 static int parse_one_feature(const char *feature
, int from_stream
)
3406 if (skip_prefix(feature
, "date-format=", &arg
)) {
3407 option_date_format(arg
);
3408 } else if (skip_prefix(feature
, "import-marks=", &arg
)) {
3409 check_unsafe_feature("import-marks", from_stream
);
3410 option_import_marks(arg
, from_stream
, 0);
3411 } else if (skip_prefix(feature
, "import-marks-if-exists=", &arg
)) {
3412 check_unsafe_feature("import-marks-if-exists", from_stream
);
3413 option_import_marks(arg
, from_stream
, 1);
3414 } else if (skip_prefix(feature
, "export-marks=", &arg
)) {
3415 check_unsafe_feature(feature
, from_stream
);
3416 option_export_marks(arg
);
3417 } else if (!strcmp(feature
, "alias")) {
3418 ; /* Don't die - this feature is supported */
3419 } else if (skip_prefix(feature
, "rewrite-submodules-to=", &arg
)) {
3420 option_rewrite_submodules(arg
, &sub_marks_to
);
3421 } else if (skip_prefix(feature
, "rewrite-submodules-from=", &arg
)) {
3422 option_rewrite_submodules(arg
, &sub_marks_from
);
3423 } else if (!strcmp(feature
, "get-mark")) {
3424 ; /* Don't die - this feature is supported */
3425 } else if (!strcmp(feature
, "cat-blob")) {
3426 ; /* Don't die - this feature is supported */
3427 } else if (!strcmp(feature
, "relative-marks")) {
3428 relative_marks_paths
= 1;
3429 } else if (!strcmp(feature
, "no-relative-marks")) {
3430 relative_marks_paths
= 0;
3431 } else if (!strcmp(feature
, "done")) {
3432 require_explicit_termination
= 1;
3433 } else if (!strcmp(feature
, "force")) {
3435 } else if (!strcmp(feature
, "notes") || !strcmp(feature
, "ls")) {
3436 ; /* do nothing; we have the feature */
3444 static void parse_feature(const char *feature
)
3446 if (seen_data_command
)
3447 die("Got feature command '%s' after data command", feature
);
3449 if (parse_one_feature(feature
, 1))
3452 die("This version of fast-import does not support feature %s.", feature
);
3455 static void parse_option(const char *option
)
3457 if (seen_data_command
)
3458 die("Got option command '%s' after data command", option
);
3460 if (parse_one_option(option
))
3463 die("This version of fast-import does not support option: %s", option
);
3466 static void git_pack_config(void)
3468 int indexversion_value
;
3470 unsigned long packsizelimit_value
;
3472 if (!git_config_get_ulong("pack.depth", &max_depth
)) {
3473 if (max_depth
> MAX_DEPTH
)
3474 max_depth
= MAX_DEPTH
;
3476 if (!git_config_get_int("pack.indexversion", &indexversion_value
)) {
3477 pack_idx_opts
.version
= indexversion_value
;
3478 if (pack_idx_opts
.version
> 2)
3479 git_die_config("pack.indexversion",
3480 "bad pack.indexVersion=%"PRIu32
, pack_idx_opts
.version
);
3482 if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value
))
3483 max_packsize
= packsizelimit_value
;
3485 if (!git_config_get_int("fastimport.unpacklimit", &limit
))
3486 unpack_limit
= limit
;
3487 else if (!git_config_get_int("transfer.unpacklimit", &limit
))
3488 unpack_limit
= limit
;
3490 git_config(git_default_config
, NULL
);
3493 static const char fast_import_usage
[] =
3494 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3496 static void parse_argv(void)
3500 for (i
= 1; i
< global_argc
; i
++) {
3501 const char *a
= global_argv
[i
];
3503 if (*a
!= '-' || !strcmp(a
, "--"))
3506 if (!skip_prefix(a
, "--", &a
))
3507 die("unknown option %s", a
);
3509 if (parse_one_option(a
))
3512 if (parse_one_feature(a
, 0))
3515 if (skip_prefix(a
, "cat-blob-fd=", &a
)) {
3516 option_cat_blob_fd(a
);
3520 die("unknown option --%s", a
);
3522 if (i
!= global_argc
)
3523 usage(fast_import_usage
);
3525 seen_data_command
= 1;
3526 if (import_marks_file
)
3528 build_mark_map(&sub_marks_from
, &sub_marks_to
);
3531 int cmd_fast_import(int argc
, const char **argv
, const char *prefix
)
3535 if (argc
== 2 && !strcmp(argv
[1], "-h"))
3536 usage(fast_import_usage
);
3538 reset_pack_idx_option(&pack_idx_opts
);
3541 alloc_objects(object_entry_alloc
);
3542 strbuf_init(&command_buf
, 0);
3543 CALLOC_ARRAY(atom_table
, atom_table_sz
);
3544 CALLOC_ARRAY(branch_table
, branch_table_sz
);
3545 CALLOC_ARRAY(avail_tree_table
, avail_tree_table_sz
);
3546 marks
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
3548 hashmap_init(&object_table
, object_entry_hashcmp
, NULL
, 0);
3551 * We don't parse most options until after we've seen the set of
3552 * "feature" lines at the start of the stream (which allows the command
3553 * line to override stream data). But we must do an early parse of any
3554 * command-line options that impact how we interpret the feature lines.
3556 for (i
= 1; i
< argc
; i
++) {
3557 const char *arg
= argv
[i
];
3558 if (*arg
!= '-' || !strcmp(arg
, "--"))
3560 if (!strcmp(arg
, "--allow-unsafe-features"))
3561 allow_unsafe_features
= 1;
3566 global_prefix
= prefix
;
3568 rc_free
= mem_pool_alloc(&fi_mem_pool
, cmd_save
* sizeof(*rc_free
));
3569 for (i
= 0; i
< (cmd_save
- 1); i
++)
3570 rc_free
[i
].next
= &rc_free
[i
+ 1];
3571 rc_free
[cmd_save
- 1].next
= NULL
;
3574 set_die_routine(die_nicely
);
3575 set_checkpoint_signal();
3576 while (read_next_command() != EOF
) {
3578 if (!strcmp("blob", command_buf
.buf
))
3580 else if (skip_prefix(command_buf
.buf
, "commit ", &v
))
3581 parse_new_commit(v
);
3582 else if (skip_prefix(command_buf
.buf
, "tag ", &v
))
3584 else if (skip_prefix(command_buf
.buf
, "reset ", &v
))
3585 parse_reset_branch(v
);
3586 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
3588 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
3590 else if (skip_prefix(command_buf
.buf
, "get-mark ", &v
))
3592 else if (!strcmp("checkpoint", command_buf
.buf
))
3594 else if (!strcmp("done", command_buf
.buf
))
3596 else if (!strcmp("alias", command_buf
.buf
))
3598 else if (starts_with(command_buf
.buf
, "progress "))
3600 else if (skip_prefix(command_buf
.buf
, "feature ", &v
))
3602 else if (skip_prefix(command_buf
.buf
, "option git ", &v
))
3604 else if (starts_with(command_buf
.buf
, "option "))
3605 /* ignore non-git options*/;
3607 die("Unsupported command: %s", command_buf
.buf
);
3609 if (checkpoint_requested
)
3613 /* argv hasn't been parsed yet, do so */
3614 if (!seen_data_command
)
3617 if (require_explicit_termination
&& feof(stdin
))
3618 die("stream ends early");
3631 uintmax_t total_count
= 0, duplicate_count
= 0;
3632 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3633 total_count
+= object_count_by_type
[i
];
3634 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3635 duplicate_count
+= duplicate_count_by_type
[i
];
3637 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3638 fprintf(stderr
, "---------------------------------------------------------------------\n");
3639 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3640 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3641 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
]);
3642 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
]);
3643 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
]);
3644 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
]);
3645 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3646 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3647 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3648 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (tree_entry_allocd
+ fi_mem_pool
.pool_alloc
+ alloc_count
*sizeof(struct object_entry
))/1024);
3649 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd
+ fi_mem_pool
.pool_alloc
) /1024));
3650 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3651 fprintf(stderr
, "---------------------------------------------------------------------\n");
3653 fprintf(stderr
, "---------------------------------------------------------------------\n");
3654 fprintf(stderr
, "\n");
3657 return failure
? 1 : 0;