1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
6 #include "environment.h"
18 #include "read-cache-ll.h"
20 #include "csum-file.h"
23 #include "run-command.h"
25 #include "object-file.h"
26 #include "object-name.h"
29 #include "commit-reach.h"
32 #include "gpg-interface.h"
34 #define PACK_ID_BITS 16
35 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
37 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
40 * We abuse the setuid bit on directories to mean "do not delta".
42 #define NO_DELTA S_ISUID
45 * The amount of additional space required in order to write an object into the
46 * current pack. This is the hash lengths at the end of the pack, plus the
47 * length of one object ID.
49 #define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3)
52 struct pack_idx_entry idx
;
53 struct hashmap_entry ent
;
54 uint32_t type
: TYPE_BITS
,
55 pack_id
: PACK_ID_BITS
,
59 static int object_entry_hashcmp(const void *map_data UNUSED
,
60 const struct hashmap_entry
*eptr
,
61 const struct hashmap_entry
*entry_or_key
,
64 const struct object_id
*oid
= keydata
;
65 const struct object_entry
*e1
, *e2
;
67 e1
= container_of(eptr
, const struct object_entry
, ent
);
69 return oidcmp(&e1
->idx
.oid
, oid
);
71 e2
= container_of(entry_or_key
, const struct object_entry
, ent
);
72 return oidcmp(&e1
->idx
.oid
, &e2
->idx
.oid
);
75 struct object_entry_pool
{
76 struct object_entry_pool
*next_pool
;
77 struct object_entry
*next_free
;
78 struct object_entry
*end
;
79 struct object_entry entries
[FLEX_ARRAY
]; /* more */
84 struct object_id
*oids
[1024];
85 struct object_entry
*marked
[1024];
86 struct mark_set
*sets
[1024];
99 struct atom_str
*next_atom
;
100 unsigned short str_len
;
101 char str_dat
[FLEX_ARRAY
]; /* more */
106 struct tree_content
*tree
;
107 struct atom_str
*name
;
108 struct tree_entry_ms
{
110 struct object_id oid
;
114 struct tree_content
{
115 unsigned int entry_capacity
; /* must match avail_tree_content */
116 unsigned int entry_count
;
117 unsigned int delta_depth
;
118 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
121 struct avail_tree_content
{
122 unsigned int entry_capacity
; /* must match tree_content */
123 struct avail_tree_content
*next_avail
;
127 struct branch
*table_next_branch
;
128 struct branch
*active_next_branch
;
130 struct tree_entry branch_tree
;
131 uintmax_t last_commit
;
135 unsigned pack_id
: PACK_ID_BITS
;
136 struct object_id oid
;
140 struct tag
*next_tag
;
142 unsigned int pack_id
;
143 struct object_id oid
;
147 struct hash_list
*next
;
148 struct object_id oid
;
153 WHENSPEC_RAW_PERMISSIVE
,
158 struct recent_command
{
159 struct recent_command
*prev
;
160 struct recent_command
*next
;
164 typedef void (*mark_set_inserter_t
)(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
);
165 typedef void (*each_mark_fn_t
)(uintmax_t mark
, void *obj
, void *cbp
);
167 /* Configured limits on output */
168 static unsigned long max_depth
= 50;
169 static off_t max_packsize
;
170 static int unpack_limit
= 100;
171 static int force_update
;
173 /* Stats and misc. counters */
174 static uintmax_t alloc_count
;
175 static uintmax_t marks_set_count
;
176 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
177 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
178 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
179 static uintmax_t delta_count_attempts_by_type
[1 << TYPE_BITS
];
180 static unsigned long object_count
;
181 static unsigned long branch_count
;
182 static unsigned long branch_load_count
;
184 static FILE *pack_edges
;
185 static unsigned int show_stats
= 1;
186 static unsigned int quiet
;
187 static int global_argc
;
188 static const char **global_argv
;
189 static const char *global_prefix
;
191 static enum sign_mode signed_tag_mode
= SIGN_VERBATIM
;
192 static enum sign_mode signed_commit_mode
= SIGN_VERBATIM
;
195 static struct mem_pool fi_mem_pool
= {
196 .block_alloc
= 2*1024*1024 - sizeof(struct mp_block
),
199 /* Atom management */
200 static unsigned int atom_table_sz
= 4451;
201 static unsigned int atom_cnt
;
202 static struct atom_str
**atom_table
;
204 /* The .pack file being generated */
205 static struct pack_idx_option pack_idx_opts
;
206 static unsigned int pack_id
;
207 static struct hashfile
*pack_file
;
208 static struct packed_git
*pack_data
;
209 static struct packed_git
**all_packs
;
210 static off_t pack_size
;
212 /* Table of objects we've written. */
213 static unsigned int object_entry_alloc
= 5000;
214 static struct object_entry_pool
*blocks
;
215 static struct hashmap object_table
;
216 static struct mark_set
*marks
;
217 static char *export_marks_file
;
218 static char *import_marks_file
;
219 static int import_marks_file_from_stream
;
220 static int import_marks_file_ignore_missing
;
221 static int import_marks_file_done
;
222 static int relative_marks_paths
;
225 static struct last_object last_blob
= {
229 /* Tree management */
230 static unsigned int tree_entry_alloc
= 1000;
231 static void *avail_tree_entry
;
232 static unsigned int avail_tree_table_sz
= 100;
233 static struct avail_tree_content
**avail_tree_table
;
234 static size_t tree_entry_allocd
;
235 static struct strbuf old_tree
= STRBUF_INIT
;
236 static struct strbuf new_tree
= STRBUF_INIT
;
239 static unsigned long max_active_branches
= 5;
240 static unsigned long cur_active_branches
;
241 static unsigned long branch_table_sz
= 1039;
242 static struct branch
**branch_table
;
243 static struct branch
*active_branches
;
246 static struct tag
*first_tag
;
247 static struct tag
*last_tag
;
249 /* Input stream parsing */
250 static whenspec_type whenspec
= WHENSPEC_RAW
;
251 static struct strbuf command_buf
= STRBUF_INIT
;
252 static int unread_command_buf
;
253 static struct recent_command cmd_hist
= {
257 static struct recent_command
*cmd_tail
= &cmd_hist
;
258 static struct recent_command
*rc_free
;
259 static unsigned int cmd_save
= 100;
260 static uintmax_t next_mark
;
261 static struct strbuf new_data
= STRBUF_INIT
;
262 static int seen_data_command
;
263 static int require_explicit_termination
;
264 static int allow_unsafe_features
;
266 /* Signal handling */
267 static volatile sig_atomic_t checkpoint_requested
;
269 /* Submodule marks */
270 static struct string_list sub_marks_from
= STRING_LIST_INIT_DUP
;
271 static struct string_list sub_marks_to
= STRING_LIST_INIT_DUP
;
272 static kh_oid_map_t
*sub_oid_map
;
274 /* Where to write output of cat-blob commands */
275 static int cat_blob_fd
= STDOUT_FILENO
;
277 static void parse_argv(void);
278 static void parse_get_mark(const char *p
);
279 static void parse_cat_blob(const char *p
);
280 static void parse_ls(const char *p
, struct branch
*b
);
282 static void for_each_mark(struct mark_set
*m
, uintmax_t base
, each_mark_fn_t callback
, void *p
)
286 for (k
= 0; k
< 1024; k
++) {
288 for_each_mark(m
->data
.sets
[k
], base
+ (k
<< m
->shift
), callback
, p
);
291 for (k
= 0; k
< 1024; k
++) {
292 if (m
->data
.marked
[k
])
293 callback(base
+ k
, m
->data
.marked
[k
], p
);
298 static void dump_marks_fn(uintmax_t mark
, void *object
, void *cbp
) {
299 struct object_entry
*e
= object
;
302 fprintf(f
, ":%" PRIuMAX
" %s\n", mark
, oid_to_hex(&e
->idx
.oid
));
305 static void write_branch_report(FILE *rpt
, struct branch
*b
)
307 fprintf(rpt
, "%s:\n", b
->name
);
309 fprintf(rpt
, " status :");
311 fputs(" active", rpt
);
312 if (b
->branch_tree
.tree
)
313 fputs(" loaded", rpt
);
314 if (is_null_oid(&b
->branch_tree
.versions
[1].oid
))
315 fputs(" dirty", rpt
);
318 fprintf(rpt
, " tip commit : %s\n", oid_to_hex(&b
->oid
));
319 fprintf(rpt
, " old tree : %s\n",
320 oid_to_hex(&b
->branch_tree
.versions
[0].oid
));
321 fprintf(rpt
, " cur tree : %s\n",
322 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
323 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
325 fputs(" last pack : ", rpt
);
326 if (b
->pack_id
< MAX_PACK_ID
)
327 fprintf(rpt
, "%u", b
->pack_id
);
333 static void write_crash_report(const char *err
)
335 char *loc
= repo_git_path(the_repository
, "fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
336 FILE *rpt
= fopen(loc
, "w");
339 struct recent_command
*rc
;
342 error_errno(_("can't write crash report %s"), loc
);
347 fprintf(stderr
, _("fast-import: dumping crash report to %s\n"), loc
);
349 fprintf(rpt
, "fast-import crash report:\n");
350 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
351 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
352 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_MODE(ISO8601
)));
355 fputs("fatal: ", rpt
);
360 fputs("Most Recent Commands Before Crash\n", rpt
);
361 fputs("---------------------------------\n", rpt
);
362 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
363 if (rc
->next
== &cmd_hist
)
372 fputs("Active Branch LRU\n", rpt
);
373 fputs("-----------------\n", rpt
);
374 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
376 max_active_branches
);
378 fputs(" pos clock name\n", rpt
);
379 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
380 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
381 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
382 ++lu
, b
->last_commit
, b
->name
);
385 fputs("Inactive Branches\n", rpt
);
386 fputs("-----------------\n", rpt
);
387 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
388 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
389 write_branch_report(rpt
, b
);
395 fputs("Annotated Tags\n", rpt
);
396 fputs("--------------\n", rpt
);
397 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
398 fputs(oid_to_hex(&tg
->oid
), rpt
);
400 fputs(tg
->name
, rpt
);
406 fputs("Marks\n", rpt
);
407 fputs("-----\n", rpt
);
408 if (export_marks_file
)
409 fprintf(rpt
, " exported to %s\n", export_marks_file
);
411 for_each_mark(marks
, 0, dump_marks_fn
, rpt
);
414 fputs("-------------------\n", rpt
);
415 fputs("END OF CRASH REPORT\n", rpt
);
420 static void end_packfile(void);
421 static void unkeep_all_packs(void);
422 static void dump_marks(void);
424 static NORETURN
void die_nicely(const char *err
, va_list params
)
428 report_fn die_message_fn
= get_die_message_routine();
431 die_message_fn(err
, params
);
434 char message
[2 * PATH_MAX
];
437 vsnprintf(message
, sizeof(message
), err
, cp
);
438 write_crash_report(message
);
446 #ifndef SIGUSR1 /* Windows, for example */
448 static void set_checkpoint_signal(void)
454 static void checkpoint_signal(int signo UNUSED
)
456 checkpoint_requested
= 1;
459 static void set_checkpoint_signal(void)
463 memset(&sa
, 0, sizeof(sa
));
464 sa
.sa_handler
= checkpoint_signal
;
465 sigemptyset(&sa
.sa_mask
);
466 sa
.sa_flags
= SA_RESTART
;
467 sigaction(SIGUSR1
, &sa
, NULL
);
472 static void alloc_objects(unsigned int cnt
)
474 struct object_entry_pool
*b
;
476 b
= xmalloc(sizeof(struct object_entry_pool
)
477 + cnt
* sizeof(struct object_entry
));
478 b
->next_pool
= blocks
;
479 b
->next_free
= b
->entries
;
480 b
->end
= b
->entries
+ cnt
;
485 static struct object_entry
*new_object(struct object_id
*oid
)
487 struct object_entry
*e
;
489 if (blocks
->next_free
== blocks
->end
)
490 alloc_objects(object_entry_alloc
);
492 e
= blocks
->next_free
++;
493 oidcpy(&e
->idx
.oid
, oid
);
497 static struct object_entry
*find_object(struct object_id
*oid
)
499 return hashmap_get_entry_from_hash(&object_table
, oidhash(oid
), oid
,
500 struct object_entry
, ent
);
503 static struct object_entry
*insert_object(struct object_id
*oid
)
505 struct object_entry
*e
;
506 unsigned int hash
= oidhash(oid
);
508 e
= hashmap_get_entry_from_hash(&object_table
, hash
, oid
,
509 struct object_entry
, ent
);
513 hashmap_entry_init(&e
->ent
, hash
);
514 hashmap_add(&object_table
, &e
->ent
);
520 static void invalidate_pack_id(unsigned int id
)
524 struct hashmap_iter iter
;
525 struct object_entry
*e
;
527 hashmap_for_each_entry(&object_table
, &iter
, e
, ent
) {
528 if (e
->pack_id
== id
)
529 e
->pack_id
= MAX_PACK_ID
;
532 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
535 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
536 if (b
->pack_id
== id
)
537 b
->pack_id
= MAX_PACK_ID
;
540 for (t
= first_tag
; t
; t
= t
->next_tag
)
541 if (t
->pack_id
== id
)
542 t
->pack_id
= MAX_PACK_ID
;
545 static unsigned int hc_str(const char *s
, size_t len
)
553 static void insert_mark(struct mark_set
**top
, uintmax_t idnum
, struct object_entry
*oe
)
555 struct mark_set
*s
= *top
;
557 while ((idnum
>> s
->shift
) >= 1024) {
558 s
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
559 s
->shift
= (*top
)->shift
+ 10;
560 s
->data
.sets
[0] = *top
;
564 uintmax_t i
= idnum
>> s
->shift
;
565 idnum
-= i
<< s
->shift
;
566 if (!s
->data
.sets
[i
]) {
567 s
->data
.sets
[i
] = mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
568 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
572 if (!s
->data
.marked
[idnum
])
574 s
->data
.marked
[idnum
] = oe
;
577 static void *find_mark(struct mark_set
*s
, uintmax_t idnum
)
579 uintmax_t orig_idnum
= idnum
;
580 struct object_entry
*oe
= NULL
;
581 if ((idnum
>> s
->shift
) < 1024) {
582 while (s
&& s
->shift
) {
583 uintmax_t i
= idnum
>> s
->shift
;
584 idnum
-= i
<< s
->shift
;
588 oe
= s
->data
.marked
[idnum
];
591 die(_("mark :%" PRIuMAX
" not declared"), orig_idnum
);
595 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
597 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
600 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
601 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
604 c
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct atom_str
) + len
+ 1);
606 memcpy(c
->str_dat
, s
, len
);
608 c
->next_atom
= atom_table
[hc
];
614 static struct branch
*lookup_branch(const char *name
)
616 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
619 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
620 if (!strcmp(name
, b
->name
))
625 static struct branch
*new_branch(const char *name
)
627 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
628 struct branch
*b
= lookup_branch(name
);
631 die(_("invalid attempt to create duplicate branch: %s"), name
);
632 if (check_refname_format(name
, REFNAME_ALLOW_ONELEVEL
))
633 die(_("branch name doesn't conform to Git standards: %s"), name
);
635 b
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct branch
));
636 b
->name
= mem_pool_strdup(&fi_mem_pool
, name
);
637 b
->table_next_branch
= branch_table
[hc
];
638 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
639 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
642 b
->pack_id
= MAX_PACK_ID
;
643 branch_table
[hc
] = b
;
648 static unsigned int hc_entries(unsigned int cnt
)
650 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
651 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
654 static struct tree_content
*new_tree_content(unsigned int cnt
)
656 struct avail_tree_content
*f
, *l
= NULL
;
657 struct tree_content
*t
;
658 unsigned int hc
= hc_entries(cnt
);
660 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
661 if (f
->entry_capacity
>= cnt
)
666 l
->next_avail
= f
->next_avail
;
668 avail_tree_table
[hc
] = f
->next_avail
;
670 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
671 f
= mem_pool_alloc(&fi_mem_pool
, sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
672 f
->entry_capacity
= cnt
;
675 t
= (struct tree_content
*)f
;
681 static void release_tree_entry(struct tree_entry
*e
);
682 static void release_tree_content(struct tree_content
*t
)
684 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
685 unsigned int hc
= hc_entries(f
->entry_capacity
);
686 f
->next_avail
= avail_tree_table
[hc
];
687 avail_tree_table
[hc
] = f
;
690 static void release_tree_content_recursive(struct tree_content
*t
)
693 for (i
= 0; i
< t
->entry_count
; i
++)
694 release_tree_entry(t
->entries
[i
]);
695 release_tree_content(t
);
698 static struct tree_content
*grow_tree_content(
699 struct tree_content
*t
,
702 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
703 r
->entry_count
= t
->entry_count
;
704 r
->delta_depth
= t
->delta_depth
;
705 COPY_ARRAY(r
->entries
, t
->entries
, t
->entry_count
);
706 release_tree_content(t
);
710 static struct tree_entry
*new_tree_entry(void)
712 struct tree_entry
*e
;
714 if (!avail_tree_entry
) {
715 unsigned int n
= tree_entry_alloc
;
716 tree_entry_allocd
+= n
* sizeof(struct tree_entry
);
718 avail_tree_entry
= e
;
720 *((void**)e
) = e
+ 1;
726 e
= avail_tree_entry
;
727 avail_tree_entry
= *((void**)e
);
731 static void release_tree_entry(struct tree_entry
*e
)
734 release_tree_content_recursive(e
->tree
);
735 *((void**)e
) = avail_tree_entry
;
736 avail_tree_entry
= e
;
739 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
741 struct tree_content
*d
;
742 struct tree_entry
*a
, *b
;
747 d
= new_tree_content(s
->entry_count
);
748 for (i
= 0; i
< s
->entry_count
; i
++) {
750 b
= new_tree_entry();
751 memcpy(b
, a
, sizeof(*a
));
752 if (a
->tree
&& is_null_oid(&b
->versions
[1].oid
))
753 b
->tree
= dup_tree_content(a
->tree
);
758 d
->entry_count
= s
->entry_count
;
759 d
->delta_depth
= s
->delta_depth
;
764 static void start_packfile(void)
766 struct strbuf tmp_file
= STRBUF_INIT
;
767 struct packed_git
*p
;
770 pack_fd
= odb_mkstemp(the_repository
->objects
, &tmp_file
,
771 "pack/tmp_pack_XXXXXX");
772 FLEX_ALLOC_STR(p
, pack_name
, tmp_file
.buf
);
773 strbuf_release(&tmp_file
);
775 p
->pack_fd
= pack_fd
;
777 p
->repo
= the_repository
;
778 pack_file
= hashfd(the_repository
->hash_algo
, pack_fd
, p
->pack_name
);
781 pack_size
= write_pack_header(pack_file
, 0);
784 REALLOC_ARRAY(all_packs
, pack_id
+ 1);
785 all_packs
[pack_id
] = p
;
788 static const char *create_index(void)
791 struct pack_idx_entry
**idx
, **c
, **last
;
792 struct object_entry
*e
;
793 struct object_entry_pool
*o
;
795 /* Build the table of object IDs. */
796 ALLOC_ARRAY(idx
, object_count
);
798 for (o
= blocks
; o
; o
= o
->next_pool
)
799 for (e
= o
->next_free
; e
-- != o
->entries
;)
800 if (pack_id
== e
->pack_id
)
802 last
= idx
+ object_count
;
804 die(_("internal consistency error creating the index"));
806 tmpfile
= write_idx_file(the_repository
, NULL
, idx
, object_count
,
807 &pack_idx_opts
, pack_data
->hash
);
812 static char *keep_pack(const char *curr_index_name
)
814 static const char *keep_msg
= "fast-import";
815 struct strbuf name
= STRBUF_INIT
;
818 odb_pack_name(pack_data
->repo
, &name
, pack_data
->hash
, "keep");
819 keep_fd
= safe_create_file_with_leading_directories(pack_data
->repo
,
822 die_errno(_("cannot create keep file"));
823 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
825 die_errno(_("failed to write keep file"));
827 odb_pack_name(pack_data
->repo
, &name
, pack_data
->hash
, "pack");
828 if (finalize_object_file(pack_data
->repo
, pack_data
->pack_name
, name
.buf
))
829 die(_("cannot store pack file"));
831 odb_pack_name(pack_data
->repo
, &name
, pack_data
->hash
, "idx");
832 if (finalize_object_file(pack_data
->repo
, curr_index_name
, name
.buf
))
833 die(_("cannot store index file"));
834 free((void *)curr_index_name
);
835 return strbuf_detach(&name
, NULL
);
838 static void unkeep_all_packs(void)
840 struct strbuf name
= STRBUF_INIT
;
843 for (k
= 0; k
< pack_id
; k
++) {
844 struct packed_git
*p
= all_packs
[k
];
845 odb_pack_name(p
->repo
, &name
, p
->hash
, "keep");
846 unlink_or_warn(name
.buf
);
848 strbuf_release(&name
);
851 static int loosen_small_pack(const struct packed_git
*p
)
853 struct child_process unpack
= CHILD_PROCESS_INIT
;
855 if (lseek(p
->pack_fd
, 0, SEEK_SET
) < 0)
856 die_errno(_("failed seeking to start of '%s'"), p
->pack_name
);
858 unpack
.in
= p
->pack_fd
;
860 unpack
.stdout_to_stderr
= 1;
861 strvec_push(&unpack
.args
, "unpack-objects");
863 strvec_push(&unpack
.args
, "-q");
865 return run_command(&unpack
);
868 static void end_packfile(void)
872 if (running
|| !pack_data
)
876 clear_delta_base_cache();
878 struct packed_git
*new_p
;
879 struct object_id cur_pack_oid
;
885 close_pack_windows(pack_data
);
886 finalize_hashfile(pack_file
, cur_pack_oid
.hash
, FSYNC_COMPONENT_PACK
, 0);
887 fixup_pack_header_footer(the_hash_algo
, pack_data
->pack_fd
,
888 pack_data
->hash
, pack_data
->pack_name
,
889 object_count
, cur_pack_oid
.hash
,
892 if (object_count
<= unpack_limit
) {
893 if (!loosen_small_pack(pack_data
)) {
894 invalidate_pack_id(pack_id
);
899 close(pack_data
->pack_fd
);
900 idx_name
= keep_pack(create_index());
902 /* Register the packfile with core git's machinery. */
903 new_p
= packfile_store_load_pack(pack_data
->repo
->objects
->packfiles
,
906 die(_("core Git rejected index %s"), idx_name
);
907 all_packs
[pack_id
] = new_p
;
910 /* Print the boundary */
912 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
913 for (i
= 0; i
< branch_table_sz
; i
++) {
914 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
915 if (b
->pack_id
== pack_id
)
916 fprintf(pack_edges
, " %s",
917 oid_to_hex(&b
->oid
));
920 for (t
= first_tag
; t
; t
= t
->next_tag
) {
921 if (t
->pack_id
== pack_id
)
922 fprintf(pack_edges
, " %s",
923 oid_to_hex(&t
->oid
));
925 fputc('\n', pack_edges
);
933 close(pack_data
->pack_fd
);
934 unlink_or_warn(pack_data
->pack_name
);
936 FREE_AND_NULL(pack_data
);
939 /* We can't carry a delta across packfiles. */
940 strbuf_release(&last_blob
.data
);
941 last_blob
.offset
= 0;
945 static void cycle_packfile(void)
951 static int store_object(
952 enum object_type type
,
954 struct last_object
*last
,
955 struct object_id
*oidout
,
958 struct packfile_store
*packs
= the_repository
->objects
->packfiles
;
960 struct object_entry
*e
;
961 unsigned char hdr
[96];
962 struct object_id oid
;
963 unsigned long hdrlen
, deltalen
;
964 struct git_hash_ctx c
;
967 hdrlen
= format_object_header((char *)hdr
, sizeof(hdr
), type
,
969 the_hash_algo
->init_fn(&c
);
970 git_hash_update(&c
, hdr
, hdrlen
);
971 git_hash_update(&c
, dat
->buf
, dat
->len
);
972 git_hash_final_oid(&oid
, &c
);
974 oidcpy(oidout
, &oid
);
976 e
= insert_object(&oid
);
978 insert_mark(&marks
, mark
, e
);
980 duplicate_count_by_type
[type
]++;
982 } else if (packfile_list_find_oid(packfile_store_get_packs(packs
), &oid
)) {
984 e
->pack_id
= MAX_PACK_ID
;
985 e
->idx
.offset
= 1; /* just not zero! */
986 duplicate_count_by_type
[type
]++;
990 if (last
&& last
->data
.len
&& last
->data
.buf
&& last
->depth
< max_depth
991 && dat
->len
> the_hash_algo
->rawsz
) {
993 delta_count_attempts_by_type
[type
]++;
994 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
996 &deltalen
, dat
->len
- the_hash_algo
->rawsz
);
1000 git_deflate_init(&s
, pack_compression_level
);
1003 s
.avail_in
= deltalen
;
1005 s
.next_in
= (void *)dat
->buf
;
1006 s
.avail_in
= dat
->len
;
1008 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
1009 s
.next_out
= out
= xmalloc(s
.avail_out
);
1010 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
1012 git_deflate_end(&s
);
1014 /* Determine if we should auto-checkpoint. */
1016 && (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) > max_packsize
)
1017 || (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) < pack_size
) {
1019 /* This new object needs to *not* have the current pack_id. */
1020 e
->pack_id
= pack_id
+ 1;
1023 /* We cannot carry a delta into the new pack. */
1025 FREE_AND_NULL(delta
);
1027 git_deflate_init(&s
, pack_compression_level
);
1028 s
.next_in
= (void *)dat
->buf
;
1029 s
.avail_in
= dat
->len
;
1030 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
1031 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
1032 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
1034 git_deflate_end(&s
);
1039 e
->pack_id
= pack_id
;
1040 e
->idx
.offset
= pack_size
;
1042 object_count_by_type
[type
]++;
1044 crc32_begin(pack_file
);
1047 off_t ofs
= e
->idx
.offset
- last
->offset
;
1048 unsigned pos
= sizeof(hdr
) - 1;
1050 delta_count_by_type
[type
]++;
1051 e
->depth
= last
->depth
+ 1;
1053 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1054 OBJ_OFS_DELTA
, deltalen
);
1055 hashwrite(pack_file
, hdr
, hdrlen
);
1056 pack_size
+= hdrlen
;
1058 hdr
[pos
] = ofs
& 127;
1060 hdr
[--pos
] = 128 | (--ofs
& 127);
1061 hashwrite(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
1062 pack_size
+= sizeof(hdr
) - pos
;
1065 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1067 hashwrite(pack_file
, hdr
, hdrlen
);
1068 pack_size
+= hdrlen
;
1071 hashwrite(pack_file
, out
, s
.total_out
);
1072 pack_size
+= s
.total_out
;
1074 e
->idx
.crc32
= crc32_end(pack_file
);
1079 if (last
->no_swap
) {
1082 strbuf_swap(&last
->data
, dat
);
1084 last
->offset
= e
->idx
.offset
;
1085 last
->depth
= e
->depth
;
1090 static void truncate_pack(struct hashfile_checkpoint
*checkpoint
)
1092 if (hashfile_truncate(pack_file
, checkpoint
))
1093 die_errno(_("cannot truncate pack to skip duplicate"));
1094 pack_size
= checkpoint
->offset
;
1097 static void stream_blob(uintmax_t len
, struct object_id
*oidout
, uintmax_t mark
)
1099 struct packfile_store
*packs
= the_repository
->objects
->packfiles
;
1100 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1101 unsigned char *in_buf
= xmalloc(in_sz
);
1102 unsigned char *out_buf
= xmalloc(out_sz
);
1103 struct object_entry
*e
;
1104 struct object_id oid
;
1105 unsigned long hdrlen
;
1107 struct git_hash_ctx c
;
1109 struct hashfile_checkpoint checkpoint
;
1112 /* Determine if we should auto-checkpoint. */
1114 && (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) > max_packsize
)
1115 || (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) < pack_size
)
1118 hashfile_checkpoint_init(pack_file
, &checkpoint
);
1119 hashfile_checkpoint(pack_file
, &checkpoint
);
1120 offset
= checkpoint
.offset
;
1122 hdrlen
= format_object_header((char *)out_buf
, out_sz
, OBJ_BLOB
, len
);
1124 the_hash_algo
->init_fn(&c
);
1125 git_hash_update(&c
, out_buf
, hdrlen
);
1127 crc32_begin(pack_file
);
1129 git_deflate_init(&s
, pack_compression_level
);
1131 hdrlen
= encode_in_pack_object_header(out_buf
, out_sz
, OBJ_BLOB
, len
);
1133 s
.next_out
= out_buf
+ hdrlen
;
1134 s
.avail_out
= out_sz
- hdrlen
;
1136 while (status
!= Z_STREAM_END
) {
1137 if (0 < len
&& !s
.avail_in
) {
1138 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1139 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1140 if (!n
&& feof(stdin
))
1141 die(_("EOF in data (%" PRIuMAX
" bytes remaining)"), len
);
1143 git_hash_update(&c
, in_buf
, n
);
1149 status
= git_deflate(&s
, len
? 0 : Z_FINISH
);
1151 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1152 size_t n
= s
.next_out
- out_buf
;
1153 hashwrite(pack_file
, out_buf
, n
);
1155 s
.next_out
= out_buf
;
1156 s
.avail_out
= out_sz
;
1165 die(_("unexpected deflate failure: %d"), status
);
1168 git_deflate_end(&s
);
1169 git_hash_final_oid(&oid
, &c
);
1172 oidcpy(oidout
, &oid
);
1174 e
= insert_object(&oid
);
1177 insert_mark(&marks
, mark
, e
);
1179 if (e
->idx
.offset
) {
1180 duplicate_count_by_type
[OBJ_BLOB
]++;
1181 truncate_pack(&checkpoint
);
1183 } else if (packfile_list_find_oid(packfile_store_get_packs(packs
), &oid
)) {
1185 e
->pack_id
= MAX_PACK_ID
;
1186 e
->idx
.offset
= 1; /* just not zero! */
1187 duplicate_count_by_type
[OBJ_BLOB
]++;
1188 truncate_pack(&checkpoint
);
1193 e
->pack_id
= pack_id
;
1194 e
->idx
.offset
= offset
;
1195 e
->idx
.crc32
= crc32_end(pack_file
);
1197 object_count_by_type
[OBJ_BLOB
]++;
1204 /* All calls must be guarded by find_object() or find_mark() to
1205 * ensure the 'struct object_entry' passed was written by this
1206 * process instance. We unpack the entry by the offset, avoiding
1207 * the need for the corresponding .idx file. This unpacking rule
1208 * works because we only use OBJ_REF_DELTA within the packfiles
1209 * created by fast-import.
1211 * oe must not be NULL. Such an oe usually comes from giving
1212 * an unknown SHA-1 to find_object() or an undefined mark to
1213 * find_mark(). Callers must test for this condition and use
1214 * the standard read_sha1_file() when it happens.
1216 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1217 * find_mark(), where the mark was reloaded from an existing marks
1218 * file and is referencing an object that this fast-import process
1219 * instance did not write out to a packfile. Callers must test for
1220 * this condition and use read_sha1_file() instead.
1222 static void *gfi_unpack_entry(
1223 struct object_entry
*oe
,
1224 unsigned long *sizep
)
1226 enum object_type type
;
1227 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1228 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ the_hash_algo
->rawsz
)) {
1229 /* The object is stored in the packfile we are writing to
1230 * and we have modified it since the last time we scanned
1231 * back to read a previously written object. If an old
1232 * window covered [p->pack_size, p->pack_size + rawsz) its
1233 * data is stale and is not valid. Closing all windows
1234 * and updating the packfile length ensures we can read
1235 * the newly written data.
1237 close_pack_windows(p
);
1238 hashflush(pack_file
);
1240 /* We have to offer rawsz bytes additional on the end of
1241 * the packfile as the core unpacker code assumes the
1242 * footer is present at the file end and must promise
1243 * at least rawsz bytes within any window it maps. But
1244 * we don't actually create the footer here.
1246 p
->pack_size
= pack_size
+ the_hash_algo
->rawsz
;
1248 return unpack_entry(the_repository
, p
, oe
->idx
.offset
, &type
, sizep
);
1251 static void load_tree(struct tree_entry
*root
)
1253 struct object_id
*oid
= &root
->versions
[1].oid
;
1254 struct object_entry
*myoe
;
1255 struct tree_content
*t
;
1260 root
->tree
= t
= new_tree_content(8);
1261 if (is_null_oid(oid
))
1264 myoe
= find_object(oid
);
1265 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1266 if (myoe
->type
!= OBJ_TREE
)
1267 die(_("not a tree: %s"), oid_to_hex(oid
));
1268 t
->delta_depth
= myoe
->depth
;
1269 buf
= gfi_unpack_entry(myoe
, &size
);
1271 die(_("can't load tree %s"), oid_to_hex(oid
));
1273 enum object_type type
;
1274 buf
= odb_read_object(the_repository
->objects
, oid
, &type
, &size
);
1275 if (!buf
|| type
!= OBJ_TREE
)
1276 die(_("can't load tree %s"), oid_to_hex(oid
));
1280 while (c
!= (buf
+ size
)) {
1281 struct tree_entry
*e
= new_tree_entry();
1283 if (t
->entry_count
== t
->entry_capacity
)
1284 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1285 t
->entries
[t
->entry_count
++] = e
;
1288 c
= parse_mode(c
, &e
->versions
[1].mode
);
1290 die(_("corrupt mode in %s"), oid_to_hex(oid
));
1291 e
->versions
[0].mode
= e
->versions
[1].mode
;
1292 e
->name
= to_atom(c
, strlen(c
));
1293 c
+= e
->name
->str_len
+ 1;
1294 oidread(&e
->versions
[0].oid
, (unsigned char *)c
,
1295 the_repository
->hash_algo
);
1296 oidread(&e
->versions
[1].oid
, (unsigned char *)c
,
1297 the_repository
->hash_algo
);
1298 c
+= the_hash_algo
->rawsz
;
1303 static int tecmp0 (const void *_a
, const void *_b
)
1305 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1306 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1307 return base_name_compare(
1308 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1309 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1312 static int tecmp1 (const void *_a
, const void *_b
)
1314 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1315 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1316 return base_name_compare(
1317 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1318 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1321 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1327 QSORT(t
->entries
, t
->entry_count
, tecmp0
);
1329 QSORT(t
->entries
, t
->entry_count
, tecmp1
);
1331 for (i
= 0; i
< t
->entry_count
; i
++) {
1332 if (t
->entries
[i
]->versions
[v
].mode
)
1333 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1337 strbuf_grow(b
, maxlen
);
1338 for (i
= 0; i
< t
->entry_count
; i
++) {
1339 struct tree_entry
*e
= t
->entries
[i
];
1340 if (!e
->versions
[v
].mode
)
1342 strbuf_addf(b
, "%o %s%c",
1343 (unsigned int)(e
->versions
[v
].mode
& ~NO_DELTA
),
1344 e
->name
->str_dat
, '\0');
1345 strbuf_add(b
, e
->versions
[v
].oid
.hash
, the_hash_algo
->rawsz
);
1349 static void store_tree(struct tree_entry
*root
)
1351 struct tree_content
*t
;
1352 unsigned int i
, j
, del
;
1353 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1354 struct object_entry
*le
= NULL
;
1356 if (!is_null_oid(&root
->versions
[1].oid
))
1363 for (i
= 0; i
< t
->entry_count
; i
++) {
1364 if (t
->entries
[i
]->tree
)
1365 store_tree(t
->entries
[i
]);
1368 if (!(root
->versions
[0].mode
& NO_DELTA
))
1369 le
= find_object(&root
->versions
[0].oid
);
1370 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1371 mktree(t
, 0, &old_tree
);
1373 lo
.offset
= le
->idx
.offset
;
1374 lo
.depth
= t
->delta_depth
;
1377 mktree(t
, 1, &new_tree
);
1378 store_object(OBJ_TREE
, &new_tree
, &lo
, &root
->versions
[1].oid
, 0);
1380 t
->delta_depth
= lo
.depth
;
1381 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1382 struct tree_entry
*e
= t
->entries
[i
];
1383 if (e
->versions
[1].mode
) {
1384 e
->versions
[0].mode
= e
->versions
[1].mode
;
1385 oidcpy(&e
->versions
[0].oid
, &e
->versions
[1].oid
);
1386 t
->entries
[j
++] = e
;
1388 release_tree_entry(e
);
1392 t
->entry_count
-= del
;
1395 static void tree_content_replace(
1396 struct tree_entry
*root
,
1397 const struct object_id
*oid
,
1398 const uint16_t mode
,
1399 struct tree_content
*newtree
)
1402 die(_("root cannot be a non-directory"));
1403 oidclr(&root
->versions
[0].oid
, the_repository
->hash_algo
);
1404 oidcpy(&root
->versions
[1].oid
, oid
);
1406 release_tree_content_recursive(root
->tree
);
1407 root
->tree
= newtree
;
1410 static int tree_content_set(
1411 struct tree_entry
*root
,
1413 const struct object_id
*oid
,
1414 const uint16_t mode
,
1415 struct tree_content
*subtree
)
1417 struct tree_content
*t
;
1420 struct tree_entry
*e
;
1422 slash1
= strchrnul(p
, '/');
1425 die(_("empty path component found in input"));
1426 if (!*slash1
&& !S_ISDIR(mode
) && subtree
)
1427 die(_("non-directories cannot have subtrees"));
1432 for (i
= 0; i
< t
->entry_count
; i
++) {
1434 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1437 && e
->versions
[1].mode
== mode
1438 && oideq(&e
->versions
[1].oid
, oid
))
1440 e
->versions
[1].mode
= mode
;
1441 oidcpy(&e
->versions
[1].oid
, oid
);
1443 release_tree_content_recursive(e
->tree
);
1447 * We need to leave e->versions[0].sha1 alone
1448 * to avoid modifying the preimage tree used
1449 * when writing out the parent directory.
1450 * But after replacing the subdir with a
1451 * completely different one, it's not a good
1452 * delta base any more, and besides, we've
1453 * thrown away the tree entries needed to
1454 * make a delta against it.
1456 * So let's just explicitly disable deltas
1459 if (S_ISDIR(e
->versions
[0].mode
))
1460 e
->versions
[0].mode
|= NO_DELTA
;
1462 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1465 if (!S_ISDIR(e
->versions
[1].mode
)) {
1466 e
->tree
= new_tree_content(8);
1467 e
->versions
[1].mode
= S_IFDIR
;
1471 if (tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
)) {
1472 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1479 if (t
->entry_count
== t
->entry_capacity
)
1480 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1481 e
= new_tree_entry();
1482 e
->name
= to_atom(p
, n
);
1483 e
->versions
[0].mode
= 0;
1484 oidclr(&e
->versions
[0].oid
, the_repository
->hash_algo
);
1485 t
->entries
[t
->entry_count
++] = e
;
1487 e
->tree
= new_tree_content(8);
1488 e
->versions
[1].mode
= S_IFDIR
;
1489 tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
);
1492 e
->versions
[1].mode
= mode
;
1493 oidcpy(&e
->versions
[1].oid
, oid
);
1495 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1499 static int tree_content_remove(
1500 struct tree_entry
*root
,
1502 struct tree_entry
*backup_leaf
,
1505 struct tree_content
*t
;
1508 struct tree_entry
*e
;
1510 slash1
= strchrnul(p
, '/');
1516 if (!*p
&& allow_root
) {
1522 for (i
= 0; i
< t
->entry_count
; i
++) {
1524 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1525 if (*slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1527 * If p names a file in some subdirectory, and a
1528 * file or symlink matching the name of the
1529 * parent directory of p exists, then p cannot
1530 * exist and need not be deleted.
1533 if (!*slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1537 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
, 0)) {
1538 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1539 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1540 oidclr(&root
->versions
[1].oid
,
1541 the_repository
->hash_algo
);
1555 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1557 release_tree_content_recursive(e
->tree
);
1559 e
->versions
[1].mode
= 0;
1560 oidclr(&e
->versions
[1].oid
, the_repository
->hash_algo
);
1561 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1565 static int tree_content_get(
1566 struct tree_entry
*root
,
1568 struct tree_entry
*leaf
,
1571 struct tree_content
*t
;
1574 struct tree_entry
*e
;
1576 slash1
= strchrnul(p
, '/');
1578 if (!n
&& !allow_root
)
1579 die(_("empty path component found in input"));
1590 for (i
= 0; i
< t
->entry_count
; i
++) {
1592 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1595 if (!S_ISDIR(e
->versions
[1].mode
))
1599 return tree_content_get(e
, slash1
+ 1, leaf
, 0);
1605 memcpy(leaf
, e
, sizeof(*leaf
));
1606 if (e
->tree
&& is_null_oid(&e
->versions
[1].oid
))
1607 leaf
->tree
= dup_tree_content(e
->tree
);
1613 static int update_branch(struct branch
*b
)
1615 static const char *msg
= "fast-import";
1616 struct ref_transaction
*transaction
;
1617 struct object_id old_oid
;
1618 struct strbuf err
= STRBUF_INIT
;
1619 static const char *replace_prefix
= "refs/replace/";
1621 if (starts_with(b
->name
, replace_prefix
) &&
1622 !strcmp(b
->name
+ strlen(replace_prefix
),
1623 oid_to_hex(&b
->oid
))) {
1625 warning(_("dropping %s since it would point to "
1626 "itself (i.e. to %s)"),
1627 b
->name
, oid_to_hex(&b
->oid
));
1628 refs_delete_ref(get_main_ref_store(the_repository
),
1629 NULL
, b
->name
, NULL
, 0);
1632 if (is_null_oid(&b
->oid
)) {
1634 refs_delete_ref(get_main_ref_store(the_repository
),
1635 NULL
, b
->name
, NULL
, 0);
1638 if (refs_read_ref(get_main_ref_store(the_repository
), b
->name
, &old_oid
))
1639 oidclr(&old_oid
, the_repository
->hash_algo
);
1640 if (!force_update
&& !is_null_oid(&old_oid
)) {
1641 struct commit
*old_cmit
, *new_cmit
;
1644 old_cmit
= lookup_commit_reference_gently(the_repository
,
1646 new_cmit
= lookup_commit_reference_gently(the_repository
,
1648 if (!old_cmit
|| !new_cmit
)
1649 return error(_("branch %s is missing commits."), b
->name
);
1651 ret
= repo_in_merge_bases(the_repository
, old_cmit
, new_cmit
);
1655 warning(_("not updating %s"
1656 " (new tip %s does not contain %s)"),
1657 b
->name
, oid_to_hex(&b
->oid
),
1658 oid_to_hex(&old_oid
));
1662 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
1665 ref_transaction_update(transaction
, b
->name
, &b
->oid
, &old_oid
,
1666 NULL
, NULL
, 0, msg
, &err
) ||
1667 ref_transaction_commit(transaction
, &err
)) {
1668 ref_transaction_free(transaction
);
1669 error("%s", err
.buf
);
1670 strbuf_release(&err
);
1673 ref_transaction_free(transaction
);
1674 strbuf_release(&err
);
1678 static void dump_branches(void)
1683 for (i
= 0; i
< branch_table_sz
; i
++) {
1684 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1685 failure
|= update_branch(b
);
1689 static void dump_tags(void)
1691 static const char *msg
= "fast-import";
1693 struct strbuf ref_name
= STRBUF_INIT
;
1694 struct strbuf err
= STRBUF_INIT
;
1695 struct ref_transaction
*transaction
;
1697 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
1700 failure
|= error("%s", err
.buf
);
1703 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1704 strbuf_reset(&ref_name
);
1705 strbuf_addf(&ref_name
, "refs/tags/%s", t
->name
);
1707 if (ref_transaction_update(transaction
, ref_name
.buf
,
1708 &t
->oid
, NULL
, NULL
, NULL
,
1710 failure
|= error("%s", err
.buf
);
1714 if (ref_transaction_commit(transaction
, &err
))
1715 failure
|= error("%s", err
.buf
);
1718 ref_transaction_free(transaction
);
1719 strbuf_release(&ref_name
);
1720 strbuf_release(&err
);
1723 static void dump_marks(void)
1725 struct lock_file mark_lock
= LOCK_INIT
;
1728 if (!export_marks_file
|| (import_marks_file
&& !import_marks_file_done
))
1731 if (safe_create_leading_directories_const(the_repository
, export_marks_file
)) {
1732 failure
|= error_errno(_("unable to create leading directories of %s"),
1737 if (hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0) < 0) {
1738 failure
|= error_errno(_("unable to write marks file %s"),
1743 f
= fdopen_lock_file(&mark_lock
, "w");
1745 int saved_errno
= errno
;
1746 rollback_lock_file(&mark_lock
);
1747 failure
|= error(_("unable to write marks file %s: %s"),
1748 export_marks_file
, strerror(saved_errno
));
1752 for_each_mark(marks
, 0, dump_marks_fn
, f
);
1753 if (commit_lock_file(&mark_lock
)) {
1754 failure
|= error_errno(_("unable to write file %s"),
1760 static void insert_object_entry(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
)
1762 struct object_entry
*e
;
1763 e
= find_object(oid
);
1765 enum object_type type
= odb_read_object_info(the_repository
->objects
,
1768 die(_("object not found: %s"), oid_to_hex(oid
));
1769 e
= insert_object(oid
);
1771 e
->pack_id
= MAX_PACK_ID
;
1772 e
->idx
.offset
= 1; /* just not zero! */
1774 insert_mark(s
, mark
, e
);
1777 static void insert_oid_entry(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
)
1779 insert_mark(s
, mark
, xmemdupz(oid
, sizeof(*oid
)));
1782 static void read_mark_file(struct mark_set
**s
, FILE *f
, mark_set_inserter_t inserter
)
1785 while (fgets(line
, sizeof(line
), f
)) {
1788 struct object_id oid
;
1790 /* Ensure SHA-1 objects are padded with zeros. */
1791 memset(oid
.hash
, 0, sizeof(oid
.hash
));
1793 end
= strchr(line
, '\n');
1794 if (line
[0] != ':' || !end
)
1795 die(_("corrupt mark line: %s"), line
);
1797 mark
= strtoumax(line
+ 1, &end
, 10);
1798 if (!mark
|| end
== line
+ 1
1800 || get_oid_hex_any(end
+ 1, &oid
) == GIT_HASH_UNKNOWN
)
1801 die(_("corrupt mark line: %s"), line
);
1802 inserter(s
, &oid
, mark
);
1806 static void read_marks(void)
1808 FILE *f
= fopen(import_marks_file
, "r");
1811 else if (import_marks_file_ignore_missing
&& errno
== ENOENT
)
1812 goto done
; /* Marks file does not exist */
1814 die_errno(_("cannot read '%s'"), import_marks_file
);
1815 read_mark_file(&marks
, f
, insert_object_entry
);
1818 import_marks_file_done
= 1;
1822 static int read_next_command(void)
1824 static int stdin_eof
= 0;
1827 unread_command_buf
= 0;
1832 if (unread_command_buf
) {
1833 unread_command_buf
= 0;
1835 struct recent_command
*rc
;
1837 stdin_eof
= strbuf_getline_lf(&command_buf
, stdin
);
1841 if (!seen_data_command
1842 && !starts_with(command_buf
.buf
, "feature ")
1843 && !starts_with(command_buf
.buf
, "option ")) {
1852 cmd_hist
.next
= rc
->next
;
1853 cmd_hist
.next
->prev
= &cmd_hist
;
1857 rc
->buf
= xstrdup(command_buf
.buf
);
1858 rc
->prev
= cmd_tail
;
1859 rc
->next
= cmd_hist
.prev
;
1860 rc
->prev
->next
= rc
;
1863 if (command_buf
.buf
[0] == '#')
1869 static void skip_optional_lf(void)
1871 int term_char
= fgetc(stdin
);
1872 if (term_char
!= '\n' && term_char
!= EOF
)
1873 ungetc(term_char
, stdin
);
1876 static void parse_mark(void)
1879 if (skip_prefix(command_buf
.buf
, "mark :", &v
)) {
1880 next_mark
= strtoumax(v
, NULL
, 10);
1881 read_next_command();
1887 static void parse_original_identifier(void)
1890 if (skip_prefix(command_buf
.buf
, "original-oid ", &v
))
1891 read_next_command();
1894 static int parse_data(struct strbuf
*sb
, uintmax_t limit
, uintmax_t *len_res
)
1899 if (!skip_prefix(command_buf
.buf
, "data ", &data
))
1900 die(_("expected 'data n' command, found: %s"), command_buf
.buf
);
1902 if (skip_prefix(data
, "<<", &data
)) {
1903 char *term
= xstrdup(data
);
1904 size_t term_len
= command_buf
.len
- (data
- command_buf
.buf
);
1907 if (strbuf_getline_lf(&command_buf
, stdin
) == EOF
)
1908 die(_("EOF in data (terminator '%s' not found)"), term
);
1909 if (term_len
== command_buf
.len
1910 && !strcmp(term
, command_buf
.buf
))
1912 strbuf_addbuf(sb
, &command_buf
);
1913 strbuf_addch(sb
, '\n');
1918 uintmax_t len
= strtoumax(data
, NULL
, 10);
1919 size_t n
= 0, length
= (size_t)len
;
1921 if (limit
&& limit
< len
) {
1926 die(_("data is too large to use in this context"));
1928 while (n
< length
) {
1929 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1930 if (!s
&& feof(stdin
))
1931 die(_("EOF in data (%lu bytes remaining)"),
1932 (unsigned long)(length
- n
));
1941 static int validate_raw_date(const char *src
, struct strbuf
*result
, int strict
)
1943 const char *orig_src
= src
;
1949 num
= strtoul(src
, &endp
, 10);
1951 * NEEDSWORK: perhaps check for reasonable values? For example, we
1952 * could error on values representing times more than a
1953 * day in the future.
1955 if (errno
|| endp
== src
|| *endp
!= ' ')
1959 if (*src
!= '-' && *src
!= '+')
1962 num
= strtoul(src
+ 1, &endp
, 10);
1964 * NEEDSWORK: check for brokenness other than num > 1400, such as
1965 * (num % 100) >= 60, or ((num % 100) % 15) != 0 ?
1967 if (errno
|| endp
== src
+ 1 || *endp
|| /* did not parse */
1968 (strict
&& (1400 < num
)) /* parsed a broken timezone */
1972 strbuf_addstr(result
, orig_src
);
1976 static char *parse_ident(const char *buf
)
1980 struct strbuf ident
= STRBUF_INIT
;
1982 /* ensure there is a space delimiter even if there is no name */
1986 ltgt
= buf
+ strcspn(buf
, "<>");
1988 die(_("missing < in ident string: %s"), buf
);
1989 if (ltgt
!= buf
&& ltgt
[-1] != ' ')
1990 die(_("missing space before < in ident string: %s"), buf
);
1991 ltgt
= ltgt
+ 1 + strcspn(ltgt
+ 1, "<>");
1993 die(_("missing > in ident string: %s"), buf
);
1996 die(_("missing space after > in ident string: %s"), buf
);
1998 name_len
= ltgt
- buf
;
1999 strbuf_add(&ident
, buf
, name_len
);
2003 if (validate_raw_date(ltgt
, &ident
, 1) < 0)
2004 die(_("invalid raw date \"%s\" in ident: %s"), ltgt
, buf
);
2006 case WHENSPEC_RAW_PERMISSIVE
:
2007 if (validate_raw_date(ltgt
, &ident
, 0) < 0)
2008 die(_("invalid raw date \"%s\" in ident: %s"), ltgt
, buf
);
2010 case WHENSPEC_RFC2822
:
2011 if (parse_date(ltgt
, &ident
) < 0)
2012 die(_("invalid rfc2822 date \"%s\" in ident: %s"), ltgt
, buf
);
2015 if (strcmp("now", ltgt
))
2016 die(_("date in ident must be 'now': %s"), buf
);
2021 return strbuf_detach(&ident
, NULL
);
2024 static void parse_and_store_blob(
2025 struct last_object
*last
,
2026 struct object_id
*oidout
,
2029 static struct strbuf buf
= STRBUF_INIT
;
2032 if (parse_data(&buf
, repo_settings_get_big_file_threshold(the_repository
), &len
))
2033 store_object(OBJ_BLOB
, &buf
, last
, oidout
, mark
);
2036 strbuf_release(&last
->data
);
2040 stream_blob(len
, oidout
, mark
);
2045 static void parse_new_blob(void)
2047 read_next_command();
2049 parse_original_identifier();
2050 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
2053 static void unload_one_branch(void)
2055 while (cur_active_branches
2056 && cur_active_branches
>= max_active_branches
) {
2057 uintmax_t min_commit
= ULONG_MAX
;
2058 struct branch
*e
, *l
= NULL
, *p
= NULL
;
2060 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
2061 if (e
->last_commit
< min_commit
) {
2063 min_commit
= e
->last_commit
;
2069 e
= p
->active_next_branch
;
2070 p
->active_next_branch
= e
->active_next_branch
;
2072 e
= active_branches
;
2073 active_branches
= e
->active_next_branch
;
2076 e
->active_next_branch
= NULL
;
2077 if (e
->branch_tree
.tree
) {
2078 release_tree_content_recursive(e
->branch_tree
.tree
);
2079 e
->branch_tree
.tree
= NULL
;
2081 cur_active_branches
--;
2085 static void load_branch(struct branch
*b
)
2087 load_tree(&b
->branch_tree
);
2090 b
->active_next_branch
= active_branches
;
2091 active_branches
= b
;
2092 cur_active_branches
++;
2093 branch_load_count
++;
2097 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2099 unsigned char fanout
= 0;
2100 while ((num_notes
>>= 8))
2105 static void construct_path_with_fanout(const char *hex_sha1
,
2106 unsigned char fanout
, char *path
)
2108 unsigned int i
= 0, j
= 0;
2109 if (fanout
>= the_hash_algo
->rawsz
)
2110 die(_("too large fanout (%u)"), fanout
);
2112 path
[i
++] = hex_sha1
[j
++];
2113 path
[i
++] = hex_sha1
[j
++];
2117 memcpy(path
+ i
, hex_sha1
+ j
, the_hash_algo
->hexsz
- j
);
2118 path
[i
+ the_hash_algo
->hexsz
- j
] = '\0';
2121 static uintmax_t do_change_note_fanout(
2122 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2123 char *hex_oid
, unsigned int hex_oid_len
,
2124 char *fullpath
, unsigned int fullpath_len
,
2125 unsigned char fanout
)
2127 struct tree_content
*t
;
2128 struct tree_entry
*e
, leaf
;
2129 unsigned int i
, tmp_hex_oid_len
, tmp_fullpath_len
;
2130 uintmax_t num_notes
= 0;
2131 struct object_id oid
;
2132 /* hex oid + '/' between each pair of hex digits + NUL */
2133 char realpath
[GIT_MAX_HEXSZ
+ ((GIT_MAX_HEXSZ
/ 2) - 1) + 1];
2134 const unsigned hexsz
= the_hash_algo
->hexsz
;
2140 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2142 tmp_hex_oid_len
= hex_oid_len
+ e
->name
->str_len
;
2143 tmp_fullpath_len
= fullpath_len
;
2146 * We're interested in EITHER existing note entries (entries
2147 * with exactly 40 hex chars in path, not including directory
2148 * separators), OR directory entries that may contain note
2149 * entries (with < 40 hex chars in path).
2150 * Also, each path component in a note entry must be a multiple
2153 if (!e
->versions
[1].mode
||
2154 tmp_hex_oid_len
> hexsz
||
2155 e
->name
->str_len
% 2)
2158 /* This _may_ be a note entry, or a subdir containing notes */
2159 memcpy(hex_oid
+ hex_oid_len
, e
->name
->str_dat
,
2161 if (tmp_fullpath_len
)
2162 fullpath
[tmp_fullpath_len
++] = '/';
2163 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2165 tmp_fullpath_len
+= e
->name
->str_len
;
2166 fullpath
[tmp_fullpath_len
] = '\0';
2168 if (tmp_hex_oid_len
== hexsz
&& !get_oid_hex(hex_oid
, &oid
)) {
2169 /* This is a note entry */
2170 if (fanout
== 0xff) {
2171 /* Counting mode, no rename */
2175 construct_path_with_fanout(hex_oid
, fanout
, realpath
);
2176 if (!strcmp(fullpath
, realpath
)) {
2177 /* Note entry is in correct location */
2182 /* Rename fullpath to realpath */
2183 if (!tree_content_remove(orig_root
, fullpath
, &leaf
, 0))
2184 die(_("failed to remove path %s"), fullpath
);
2185 tree_content_set(orig_root
, realpath
,
2186 &leaf
.versions
[1].oid
,
2187 leaf
.versions
[1].mode
,
2189 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2190 /* This is a subdir that may contain note entries */
2191 num_notes
+= do_change_note_fanout(orig_root
, e
,
2192 hex_oid
, tmp_hex_oid_len
,
2193 fullpath
, tmp_fullpath_len
, fanout
);
2196 /* The above may have reallocated the current tree_content */
2202 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2203 unsigned char fanout
)
2206 * The size of path is due to one slash between every two hex digits,
2207 * plus the terminating NUL. Note that there is no slash at the end, so
2208 * the number of slashes is one less than half the number of hex
2211 char hex_oid
[GIT_MAX_HEXSZ
], path
[GIT_MAX_HEXSZ
+ (GIT_MAX_HEXSZ
/ 2) - 1 + 1];
2212 return do_change_note_fanout(root
, root
, hex_oid
, 0, path
, 0, fanout
);
2215 static int parse_mapped_oid_hex(const char *hex
, struct object_id
*oid
, const char **end
)
2220 /* Make SHA-1 object IDs have all-zero padding. */
2221 memset(oid
->hash
, 0, sizeof(oid
->hash
));
2223 algo
= parse_oid_hex_any(hex
, oid
, end
);
2224 if (algo
== GIT_HASH_UNKNOWN
)
2227 it
= kh_get_oid_map(sub_oid_map
, *oid
);
2228 /* No such object? */
2229 if (it
== kh_end(sub_oid_map
)) {
2230 /* If we're using the same algorithm, pass it through. */
2231 if (hash_algos
[algo
].format_id
== the_hash_algo
->format_id
)
2235 oidcpy(oid
, kh_value(sub_oid_map
, it
));
2240 * Given a pointer into a string, parse a mark reference:
2242 * idnum ::= ':' bigint;
2244 * Update *endptr to point to the first character after the value.
2246 * Complain if the following character is not what is expected,
2247 * either a space or end of the string.
2249 static uintmax_t parse_mark_ref(const char *p
, char **endptr
)
2255 mark
= strtoumax(p
, endptr
, 10);
2257 die(_("no value after ':' in mark: %s"), command_buf
.buf
);
2262 * Parse the mark reference, and complain if this is not the end of
2265 static uintmax_t parse_mark_ref_eol(const char *p
)
2270 mark
= parse_mark_ref(p
, &end
);
2272 die(_("garbage after mark: %s"), command_buf
.buf
);
2277 * Parse the mark reference, demanding a trailing space. Update *p to
2278 * point to the first character after the space.
2280 static uintmax_t parse_mark_ref_space(const char **p
)
2285 mark
= parse_mark_ref(*p
, &end
);
2287 die(_("missing space after mark: %s"), command_buf
.buf
);
2293 * Parse the path string into the strbuf. The path can either be quoted with
2294 * escape sequences or unquoted without escape sequences. Unquoted strings may
2295 * contain spaces only if `is_last_field` is nonzero; otherwise, it stops
2296 * parsing at the first space.
2298 static void parse_path(struct strbuf
*sb
, const char *p
, const char **endp
,
2299 int is_last_field
, const char *field
)
2302 if (unquote_c_style(sb
, p
, endp
))
2303 die(_("invalid %s: %s"), field
, command_buf
.buf
);
2304 if (strlen(sb
->buf
) != sb
->len
)
2305 die(_("NUL in %s: %s"), field
, command_buf
.buf
);
2308 * Unless we are parsing the last field of a line,
2309 * SP is the end of this field.
2311 *endp
= is_last_field
2313 : strchrnul(p
, ' ');
2314 strbuf_add(sb
, p
, *endp
- p
);
2319 * Parse the path string into the strbuf, and complain if this is not the end of
2320 * the string. Unquoted strings may contain spaces.
2322 static void parse_path_eol(struct strbuf
*sb
, const char *p
, const char *field
)
2326 parse_path(sb
, p
, &end
, 1, field
);
2328 die(_("garbage after %s: %s"), field
, command_buf
.buf
);
2332 * Parse the path string into the strbuf, and ensure it is followed by a space.
2333 * Unquoted strings may not contain spaces. Update *endp to point to the first
2334 * character after the space.
2336 static void parse_path_space(struct strbuf
*sb
, const char *p
,
2337 const char **endp
, const char *field
)
2339 parse_path(sb
, p
, endp
, 0, field
);
2341 die(_("missing space after %s: %s"), field
, command_buf
.buf
);
2345 static void file_change_m(const char *p
, struct branch
*b
)
2347 static struct strbuf path
= STRBUF_INIT
;
2348 struct object_entry
*oe
;
2349 struct object_id oid
;
2350 uint16_t mode
, inline_data
= 0;
2352 p
= parse_mode(p
, &mode
);
2354 die(_("corrupt mode: %s"), command_buf
.buf
);
2359 case S_IFREG
| 0644:
2360 case S_IFREG
| 0755:
2367 die(_("corrupt mode: %s"), command_buf
.buf
);
2371 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2372 oidcpy(&oid
, &oe
->idx
.oid
);
2373 } else if (skip_prefix(p
, "inline ", &p
)) {
2375 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2377 if (parse_mapped_oid_hex(p
, &oid
, &p
))
2378 die(_("invalid dataref: %s"), command_buf
.buf
);
2379 oe
= find_object(&oid
);
2381 die(_("missing space after SHA1: %s"), command_buf
.buf
);
2384 strbuf_reset(&path
);
2385 parse_path_eol(&path
, p
, "path");
2387 /* Git does not track empty, non-toplevel directories. */
2388 if (S_ISDIR(mode
) &&
2389 is_empty_tree_oid(&oid
, the_repository
->hash_algo
) &&
2391 tree_content_remove(&b
->branch_tree
, path
.buf
, NULL
, 0);
2395 if (S_ISGITLINK(mode
)) {
2397 die(_("Git links cannot be specified 'inline': %s"),
2400 if (oe
->type
!= OBJ_COMMIT
)
2401 die(_("not a commit (actually a %s): %s"),
2402 type_name(oe
->type
), command_buf
.buf
);
2405 * Accept the sha1 without checking; it expected to be in
2406 * another repository.
2408 } else if (inline_data
) {
2410 die(_("directories cannot be specified 'inline': %s"),
2412 while (read_next_command() != EOF
) {
2414 if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2417 parse_and_store_blob(&last_blob
, &oid
, 0);
2422 enum object_type expected
= S_ISDIR(mode
) ?
2424 enum object_type type
= oe
? oe
->type
:
2425 odb_read_object_info(the_repository
->objects
,
2428 die(_("%s not found: %s"),
2429 S_ISDIR(mode
) ? _("tree") : _("blob"),
2431 if (type
!= expected
)
2432 die(_("not a %s (actually a %s): %s"),
2433 type_name(expected
), type_name(type
),
2438 tree_content_replace(&b
->branch_tree
, &oid
, mode
, NULL
);
2442 if (!verify_path(path
.buf
, mode
))
2443 die(_("invalid path '%s'"), path
.buf
);
2444 tree_content_set(&b
->branch_tree
, path
.buf
, &oid
, mode
, NULL
);
2447 static void file_change_d(const char *p
, struct branch
*b
)
2449 static struct strbuf path
= STRBUF_INIT
;
2451 strbuf_reset(&path
);
2452 parse_path_eol(&path
, p
, "path");
2453 tree_content_remove(&b
->branch_tree
, path
.buf
, NULL
, 1);
2456 static void file_change_cr(const char *p
, struct branch
*b
, int rename
)
2458 static struct strbuf source
= STRBUF_INIT
;
2459 static struct strbuf dest
= STRBUF_INIT
;
2460 struct tree_entry leaf
;
2462 strbuf_reset(&source
);
2463 parse_path_space(&source
, p
, &p
, "source");
2464 strbuf_reset(&dest
);
2465 parse_path_eol(&dest
, p
, "dest");
2467 memset(&leaf
, 0, sizeof(leaf
));
2469 tree_content_remove(&b
->branch_tree
, source
.buf
, &leaf
, 1);
2471 tree_content_get(&b
->branch_tree
, source
.buf
, &leaf
, 1);
2472 if (!leaf
.versions
[1].mode
)
2473 die(_("path %s not in branch"), source
.buf
);
2474 if (!*dest
.buf
) { /* C "path/to/subdir" "" */
2475 tree_content_replace(&b
->branch_tree
,
2476 &leaf
.versions
[1].oid
,
2477 leaf
.versions
[1].mode
,
2481 if (!verify_path(dest
.buf
, leaf
.versions
[1].mode
))
2482 die(_("invalid path '%s'"), dest
.buf
);
2483 tree_content_set(&b
->branch_tree
, dest
.buf
,
2484 &leaf
.versions
[1].oid
,
2485 leaf
.versions
[1].mode
,
2489 static void note_change_n(const char *p
, struct branch
*b
, unsigned char *old_fanout
)
2491 struct object_entry
*oe
;
2493 struct object_id oid
, commit_oid
;
2494 char path
[GIT_MAX_RAWSZ
* 3];
2495 uint16_t inline_data
= 0;
2496 unsigned char new_fanout
;
2499 * When loading a branch, we don't traverse its tree to count the real
2500 * number of notes (too expensive to do this for all non-note refs).
2501 * This means that recently loaded notes refs might incorrectly have
2502 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2504 * Fix this by traversing the tree and counting the number of notes
2505 * when b->num_notes == 0. If the notes tree is truly empty, the
2506 * calculation should not take long.
2508 if (b
->num_notes
== 0 && *old_fanout
== 0) {
2509 /* Invoke change_note_fanout() in "counting mode". */
2510 b
->num_notes
= change_note_fanout(&b
->branch_tree
, 0xff);
2511 *old_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2514 /* Now parse the notemodify command. */
2515 /* <dataref> or 'inline' */
2517 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2518 oidcpy(&oid
, &oe
->idx
.oid
);
2519 } else if (skip_prefix(p
, "inline ", &p
)) {
2521 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2523 if (parse_mapped_oid_hex(p
, &oid
, &p
))
2524 die(_("invalid dataref: %s"), command_buf
.buf
);
2525 oe
= find_object(&oid
);
2527 die(_("missing space after SHA1: %s"), command_buf
.buf
);
2531 s
= lookup_branch(p
);
2533 if (is_null_oid(&s
->oid
))
2534 die(_("can't add a note on empty branch."));
2535 oidcpy(&commit_oid
, &s
->oid
);
2536 } else if (*p
== ':') {
2537 uintmax_t commit_mark
= parse_mark_ref_eol(p
);
2538 struct object_entry
*commit_oe
= find_mark(marks
, commit_mark
);
2539 if (commit_oe
->type
!= OBJ_COMMIT
)
2540 die(_("mark :%" PRIuMAX
" not a commit"), commit_mark
);
2541 oidcpy(&commit_oid
, &commit_oe
->idx
.oid
);
2542 } else if (!repo_get_oid(the_repository
, p
, &commit_oid
)) {
2544 char *buf
= odb_read_object_peeled(the_repository
->objects
,
2545 &commit_oid
, OBJ_COMMIT
, &size
,
2547 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2548 die(_("not a valid commit: %s"), p
);
2551 die(_("invalid ref name or SHA1 expression: %s"), p
);
2554 read_next_command();
2555 parse_and_store_blob(&last_blob
, &oid
, 0);
2557 if (oe
->type
!= OBJ_BLOB
)
2558 die(_("not a blob (actually a %s): %s"),
2559 type_name(oe
->type
), command_buf
.buf
);
2560 } else if (!is_null_oid(&oid
)) {
2561 enum object_type type
= odb_read_object_info(the_repository
->objects
, &oid
,
2564 die(_("blob not found: %s"), command_buf
.buf
);
2565 if (type
!= OBJ_BLOB
)
2566 die(_("not a blob (actually a %s): %s"),
2567 type_name(type
), command_buf
.buf
);
2570 construct_path_with_fanout(oid_to_hex(&commit_oid
), *old_fanout
, path
);
2571 if (tree_content_remove(&b
->branch_tree
, path
, NULL
, 0))
2574 if (is_null_oid(&oid
))
2575 return; /* nothing to insert */
2578 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2579 construct_path_with_fanout(oid_to_hex(&commit_oid
), new_fanout
, path
);
2580 tree_content_set(&b
->branch_tree
, path
, &oid
, S_IFREG
| 0644, NULL
);
2583 static void file_change_deleteall(struct branch
*b
)
2585 release_tree_content_recursive(b
->branch_tree
.tree
);
2586 oidclr(&b
->branch_tree
.versions
[0].oid
, the_repository
->hash_algo
);
2587 oidclr(&b
->branch_tree
.versions
[1].oid
, the_repository
->hash_algo
);
2588 load_tree(&b
->branch_tree
);
2592 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2594 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2595 die(_("not a valid commit: %s"), oid_to_hex(&b
->oid
));
2596 if (memcmp("tree ", buf
, 5)
2597 || get_oid_hex(buf
+ 5, &b
->branch_tree
.versions
[1].oid
))
2598 die(_("the commit %s is corrupt"), oid_to_hex(&b
->oid
));
2599 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2600 &b
->branch_tree
.versions
[1].oid
);
2603 static void parse_from_existing(struct branch
*b
)
2605 if (is_null_oid(&b
->oid
)) {
2606 oidclr(&b
->branch_tree
.versions
[0].oid
, the_repository
->hash_algo
);
2607 oidclr(&b
->branch_tree
.versions
[1].oid
, the_repository
->hash_algo
);
2612 buf
= odb_read_object_peeled(the_repository
->objects
, &b
->oid
,
2613 OBJ_COMMIT
, &size
, &b
->oid
);
2614 parse_from_commit(b
, buf
, size
);
2619 static int parse_objectish(struct branch
*b
, const char *objectish
)
2622 struct object_id oid
;
2624 oidcpy(&oid
, &b
->branch_tree
.versions
[1].oid
);
2626 s
= lookup_branch(objectish
);
2628 die(_("can't create a branch from itself: %s"), b
->name
);
2630 struct object_id
*t
= &s
->branch_tree
.versions
[1].oid
;
2631 oidcpy(&b
->oid
, &s
->oid
);
2632 oidcpy(&b
->branch_tree
.versions
[0].oid
, t
);
2633 oidcpy(&b
->branch_tree
.versions
[1].oid
, t
);
2634 } else if (*objectish
== ':') {
2635 uintmax_t idnum
= parse_mark_ref_eol(objectish
);
2636 struct object_entry
*oe
= find_mark(marks
, idnum
);
2637 if (oe
->type
!= OBJ_COMMIT
)
2638 die(_("mark :%" PRIuMAX
" not a commit"), idnum
);
2639 if (!oideq(&b
->oid
, &oe
->idx
.oid
)) {
2640 oidcpy(&b
->oid
, &oe
->idx
.oid
);
2641 if (oe
->pack_id
!= MAX_PACK_ID
) {
2643 char *buf
= gfi_unpack_entry(oe
, &size
);
2644 parse_from_commit(b
, buf
, size
);
2647 parse_from_existing(b
);
2649 } else if (!repo_get_oid(the_repository
, objectish
, &b
->oid
)) {
2650 parse_from_existing(b
);
2651 if (is_null_oid(&b
->oid
))
2655 die(_("invalid ref name or SHA1 expression: %s"), objectish
);
2657 if (b
->branch_tree
.tree
&& !oideq(&oid
, &b
->branch_tree
.versions
[1].oid
)) {
2658 release_tree_content_recursive(b
->branch_tree
.tree
);
2659 b
->branch_tree
.tree
= NULL
;
2662 read_next_command();
2666 static int parse_from(struct branch
*b
)
2670 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2673 return parse_objectish(b
, from
);
2676 static int parse_objectish_with_prefix(struct branch
*b
, const char *prefix
)
2680 if (!skip_prefix(command_buf
.buf
, prefix
, &base
))
2683 return parse_objectish(b
, base
);
2686 static struct hash_list
*parse_merge(unsigned int *count
)
2688 struct hash_list
*list
= NULL
, **tail
= &list
, *n
;
2693 while (skip_prefix(command_buf
.buf
, "merge ", &from
)) {
2694 n
= xmalloc(sizeof(*n
));
2695 s
= lookup_branch(from
);
2697 oidcpy(&n
->oid
, &s
->oid
);
2698 else if (*from
== ':') {
2699 uintmax_t idnum
= parse_mark_ref_eol(from
);
2700 struct object_entry
*oe
= find_mark(marks
, idnum
);
2701 if (oe
->type
!= OBJ_COMMIT
)
2702 die(_("mark :%" PRIuMAX
" not a commit"), idnum
);
2703 oidcpy(&n
->oid
, &oe
->idx
.oid
);
2704 } else if (!repo_get_oid(the_repository
, from
, &n
->oid
)) {
2706 char *buf
= odb_read_object_peeled(the_repository
->objects
,
2707 &n
->oid
, OBJ_COMMIT
,
2709 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2710 die(_("not a valid commit: %s"), from
);
2713 die(_("invalid ref name or SHA1 expression: %s"), from
);
2720 read_next_command();
2725 struct signature_data
{
2726 char *hash_algo
; /* "sha1" or "sha256" */
2727 char *sig_format
; /* "openpgp", "x509", "ssh", or "unknown" */
2728 struct strbuf data
; /* The actual signature data */
2731 static void parse_one_signature(struct signature_data
*sig
, const char *v
)
2733 char *args
= xstrdup(v
); /* Will be freed when sig->hash_algo is freed */
2734 char *space
= strchr(args
, ' ');
2737 die(_("expected gpgsig format: 'gpgsig <hash-algo> <signature-format>', "
2738 "got 'gpgsig %s'"), args
);
2741 sig
->hash_algo
= args
;
2742 sig
->sig_format
= space
+ 1;
2744 /* Validate hash algorithm */
2745 if (strcmp(sig
->hash_algo
, "sha1") &&
2746 strcmp(sig
->hash_algo
, "sha256"))
2747 die(_("unknown git hash algorithm in gpgsig: '%s'"), sig
->hash_algo
);
2749 /* Validate signature format */
2750 if (!valid_signature_format(sig
->sig_format
))
2751 die(_("invalid signature format in gpgsig: '%s'"), sig
->sig_format
);
2752 if (!strcmp(sig
->sig_format
, "unknown"))
2753 warning(_("'unknown' signature format in gpgsig"));
2755 /* Read signature data */
2756 read_next_command();
2757 parse_data(&sig
->data
, 0, NULL
);
2760 static void discard_one_signature(void)
2762 struct strbuf data
= STRBUF_INIT
;
2764 read_next_command();
2765 parse_data(&data
, 0, NULL
);
2766 strbuf_release(&data
);
2769 static void add_gpgsig_to_commit(struct strbuf
*commit_data
,
2771 struct signature_data
*sig
)
2773 struct string_list siglines
= STRING_LIST_INIT_NODUP
;
2775 if (!sig
->hash_algo
)
2778 strbuf_addstr(commit_data
, header
);
2779 string_list_split_in_place(&siglines
, sig
->data
.buf
, "\n", -1);
2780 strbuf_add_separated_string_list(commit_data
, "\n ", &siglines
);
2781 strbuf_addch(commit_data
, '\n');
2782 string_list_clear(&siglines
, 1);
2783 strbuf_release(&sig
->data
);
2784 free(sig
->hash_algo
);
2787 static void store_signature(struct signature_data
*stored_sig
,
2788 struct signature_data
*new_sig
,
2789 const char *hash_type
)
2791 if (stored_sig
->hash_algo
) {
2792 warning(_("multiple %s signatures found, "
2793 "ignoring additional signature"),
2795 strbuf_release(&new_sig
->data
);
2796 free(new_sig
->hash_algo
);
2798 *stored_sig
= *new_sig
;
2802 static void import_one_signature(struct signature_data
*sig_sha1
,
2803 struct signature_data
*sig_sha256
,
2806 struct signature_data sig
= { NULL
, NULL
, STRBUF_INIT
};
2808 parse_one_signature(&sig
, v
);
2810 if (!strcmp(sig
.hash_algo
, "sha1"))
2811 store_signature(sig_sha1
, &sig
, "SHA-1");
2812 else if (!strcmp(sig
.hash_algo
, "sha256"))
2813 store_signature(sig_sha256
, &sig
, "SHA-256");
2815 die(_("parse_one_signature() returned unknown hash algo"));
2818 static void parse_new_commit(const char *arg
)
2820 static struct strbuf msg
= STRBUF_INIT
;
2821 struct signature_data sig_sha1
= { NULL
, NULL
, STRBUF_INIT
};
2822 struct signature_data sig_sha256
= { NULL
, NULL
, STRBUF_INIT
};
2824 char *author
= NULL
;
2825 char *committer
= NULL
;
2826 char *encoding
= NULL
;
2827 struct hash_list
*merge_list
= NULL
;
2828 unsigned int merge_count
;
2829 unsigned char prev_fanout
, new_fanout
;
2832 b
= lookup_branch(arg
);
2834 b
= new_branch(arg
);
2836 read_next_command();
2838 parse_original_identifier();
2839 if (skip_prefix(command_buf
.buf
, "author ", &v
)) {
2840 author
= parse_ident(v
);
2841 read_next_command();
2843 if (skip_prefix(command_buf
.buf
, "committer ", &v
)) {
2844 committer
= parse_ident(v
);
2845 read_next_command();
2848 die(_("expected committer but didn't get one"));
2850 while (skip_prefix(command_buf
.buf
, "gpgsig ", &v
)) {
2851 switch (signed_commit_mode
) {
2853 /* First, modes that don't need the signature to be parsed */
2855 die(_("encountered signed commit; use "
2856 "--signed-commits=<mode> to handle it"));
2857 case SIGN_WARN_STRIP
:
2858 warning(_("stripping a commit signature"));
2861 discard_one_signature();
2864 /* Second, modes that parse the signature */
2865 case SIGN_WARN_VERBATIM
:
2866 warning(_("importing a commit signature verbatim"));
2869 import_one_signature(&sig_sha1
, &sig_sha256
, v
);
2874 BUG("invalid signed_commit_mode value %d", signed_commit_mode
);
2876 read_next_command();
2879 if (skip_prefix(command_buf
.buf
, "encoding ", &v
)) {
2880 encoding
= xstrdup(v
);
2881 read_next_command();
2883 parse_data(&msg
, 0, NULL
);
2884 read_next_command();
2886 merge_list
= parse_merge(&merge_count
);
2888 /* ensure the branch is active/loaded */
2889 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2890 unload_one_branch();
2894 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2897 while (command_buf
.len
> 0) {
2898 if (skip_prefix(command_buf
.buf
, "M ", &v
))
2899 file_change_m(v
, b
);
2900 else if (skip_prefix(command_buf
.buf
, "D ", &v
))
2901 file_change_d(v
, b
);
2902 else if (skip_prefix(command_buf
.buf
, "R ", &v
))
2903 file_change_cr(v
, b
, 1);
2904 else if (skip_prefix(command_buf
.buf
, "C ", &v
))
2905 file_change_cr(v
, b
, 0);
2906 else if (skip_prefix(command_buf
.buf
, "N ", &v
))
2907 note_change_n(v
, b
, &prev_fanout
);
2908 else if (!strcmp("deleteall", command_buf
.buf
))
2909 file_change_deleteall(b
);
2910 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
2912 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2915 unread_command_buf
= 1;
2918 if (read_next_command() == EOF
)
2922 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2923 if (new_fanout
!= prev_fanout
)
2924 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2926 /* build the tree and the commit */
2927 store_tree(&b
->branch_tree
);
2928 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2929 &b
->branch_tree
.versions
[1].oid
);
2931 strbuf_reset(&new_data
);
2932 strbuf_addf(&new_data
, "tree %s\n",
2933 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
2934 if (!is_null_oid(&b
->oid
))
2935 strbuf_addf(&new_data
, "parent %s\n",
2936 oid_to_hex(&b
->oid
));
2937 while (merge_list
) {
2938 struct hash_list
*next
= merge_list
->next
;
2939 strbuf_addf(&new_data
, "parent %s\n",
2940 oid_to_hex(&merge_list
->oid
));
2944 strbuf_addf(&new_data
,
2947 author
? author
: committer
, committer
);
2949 strbuf_addf(&new_data
,
2953 add_gpgsig_to_commit(&new_data
, "gpgsig ", &sig_sha1
);
2954 add_gpgsig_to_commit(&new_data
, "gpgsig-sha256 ", &sig_sha256
);
2956 strbuf_addch(&new_data
, '\n');
2957 strbuf_addbuf(&new_data
, &msg
);
2962 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, &b
->oid
, next_mark
))
2963 b
->pack_id
= pack_id
;
2964 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2967 static void handle_tag_signature(struct strbuf
*msg
, const char *name
)
2969 size_t sig_offset
= parse_signed_buffer(msg
->buf
, msg
->len
);
2971 /* If there is no signature, there is nothing to do. */
2972 if (sig_offset
>= msg
->len
)
2975 switch (signed_tag_mode
) {
2977 /* First, modes that don't change anything */
2979 die(_("encountered signed tag; use "
2980 "--signed-tags=<mode> to handle it"));
2981 case SIGN_WARN_VERBATIM
:
2982 warning(_("importing a tag signature verbatim for tag '%s'"), name
);
2985 /* Nothing to do, the signature will be put into the imported tag. */
2988 /* Second, modes that remove the signature */
2989 case SIGN_WARN_STRIP
:
2990 warning(_("stripping a tag signature for tag '%s'"), name
);
2993 /* Truncate the buffer to remove the signature */
2994 strbuf_setlen(msg
, sig_offset
);
2999 BUG("invalid signed_tag_mode value %d from tag '%s'",
3000 signed_tag_mode
, name
);
3004 static void parse_new_tag(const char *arg
)
3006 static struct strbuf msg
= STRBUF_INIT
;
3011 uintmax_t from_mark
= 0;
3012 struct object_id oid
;
3013 enum object_type type
;
3016 t
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct tag
));
3017 t
->name
= mem_pool_strdup(&fi_mem_pool
, arg
);
3019 last_tag
->next_tag
= t
;
3023 read_next_command();
3027 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
3028 die(_("expected 'from' command, got '%s'"), command_buf
.buf
);
3029 s
= lookup_branch(from
);
3031 if (is_null_oid(&s
->oid
))
3032 die(_("can't tag an empty branch."));
3033 oidcpy(&oid
, &s
->oid
);
3035 } else if (*from
== ':') {
3036 struct object_entry
*oe
;
3037 from_mark
= parse_mark_ref_eol(from
);
3038 oe
= find_mark(marks
, from_mark
);
3040 oidcpy(&oid
, &oe
->idx
.oid
);
3041 } else if (!repo_get_oid(the_repository
, from
, &oid
)) {
3042 struct object_entry
*oe
= find_object(&oid
);
3044 type
= odb_read_object_info(the_repository
->objects
,
3047 die(_("not a valid object: %s"), from
);
3051 die(_("invalid ref name or SHA1 expression: %s"), from
);
3052 read_next_command();
3054 /* original-oid ... */
3055 parse_original_identifier();
3058 if (skip_prefix(command_buf
.buf
, "tagger ", &v
)) {
3059 tagger
= parse_ident(v
);
3060 read_next_command();
3064 /* tag payload/message */
3065 parse_data(&msg
, 0, NULL
);
3067 handle_tag_signature(&msg
, t
->name
);
3069 /* build the tag object */
3070 strbuf_reset(&new_data
);
3072 strbuf_addf(&new_data
,
3076 oid_to_hex(&oid
), type_name(type
), t
->name
);
3078 strbuf_addf(&new_data
,
3079 "tagger %s\n", tagger
);
3080 strbuf_addch(&new_data
, '\n');
3081 strbuf_addbuf(&new_data
, &msg
);
3084 if (store_object(OBJ_TAG
, &new_data
, NULL
, &t
->oid
, next_mark
))
3085 t
->pack_id
= MAX_PACK_ID
;
3087 t
->pack_id
= pack_id
;
3090 static void parse_reset_branch(const char *arg
)
3093 const char *tag_name
;
3095 b
= lookup_branch(arg
);
3097 oidclr(&b
->oid
, the_repository
->hash_algo
);
3098 oidclr(&b
->branch_tree
.versions
[0].oid
, the_repository
->hash_algo
);
3099 oidclr(&b
->branch_tree
.versions
[1].oid
, the_repository
->hash_algo
);
3100 if (b
->branch_tree
.tree
) {
3101 release_tree_content_recursive(b
->branch_tree
.tree
);
3102 b
->branch_tree
.tree
= NULL
;
3106 b
= new_branch(arg
);
3107 read_next_command();
3109 if (b
->delete && skip_prefix(b
->name
, "refs/tags/", &tag_name
)) {
3111 * Elsewhere, we call dump_branches() before dump_tags(),
3112 * and dump_branches() will handle ref deletions first, so
3113 * in order to make sure the deletion actually takes effect,
3114 * we need to remove the tag from our list of tags to update.
3116 * NEEDSWORK: replace list of tags with hashmap for faster
3119 struct tag
*t
, *prev
= NULL
;
3120 for (t
= first_tag
; t
; t
= t
->next_tag
) {
3121 if (!strcmp(t
->name
, tag_name
))
3127 prev
->next_tag
= t
->next_tag
;
3129 first_tag
= t
->next_tag
;
3132 /* There is no mem_pool_free(t) function to call. */
3135 if (command_buf
.len
> 0)
3136 unread_command_buf
= 1;
3139 static void cat_blob_write(const char *buf
, unsigned long size
)
3141 if (write_in_full(cat_blob_fd
, buf
, size
) < 0)
3142 die_errno(_("write to frontend failed"));
3145 static void cat_blob(struct object_entry
*oe
, struct object_id
*oid
)
3147 struct strbuf line
= STRBUF_INIT
;
3149 enum object_type type
= 0;
3152 if (!oe
|| oe
->pack_id
== MAX_PACK_ID
) {
3153 buf
= odb_read_object(the_repository
->objects
, oid
, &type
, &size
);
3156 buf
= gfi_unpack_entry(oe
, &size
);
3160 * Output based on batch_one_object() from cat-file.c.
3163 strbuf_reset(&line
);
3164 strbuf_addf(&line
, "%s missing\n", oid_to_hex(oid
));
3165 cat_blob_write(line
.buf
, line
.len
);
3166 strbuf_release(&line
);
3171 die(_("can't read object %s"), oid_to_hex(oid
));
3172 if (type
!= OBJ_BLOB
)
3173 die(_("object %s is a %s but a blob was expected."),
3174 oid_to_hex(oid
), type_name(type
));
3175 strbuf_reset(&line
);
3176 strbuf_addf(&line
, "%s %s %"PRIuMAX
"\n", oid_to_hex(oid
),
3177 type_name(type
), (uintmax_t)size
);
3178 cat_blob_write(line
.buf
, line
.len
);
3179 strbuf_release(&line
);
3180 cat_blob_write(buf
, size
);
3181 cat_blob_write("\n", 1);
3182 if (oe
&& oe
->pack_id
== pack_id
) {
3183 last_blob
.offset
= oe
->idx
.offset
;
3184 strbuf_attach(&last_blob
.data
, buf
, size
, size
);
3185 last_blob
.depth
= oe
->depth
;
3190 static void parse_get_mark(const char *p
)
3192 struct object_entry
*oe
;
3193 char output
[GIT_MAX_HEXSZ
+ 2];
3195 /* get-mark SP <object> LF */
3197 die(_("not a mark: %s"), p
);
3199 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
3201 die(_("unknown mark: %s"), command_buf
.buf
);
3203 xsnprintf(output
, sizeof(output
), "%s\n", oid_to_hex(&oe
->idx
.oid
));
3204 cat_blob_write(output
, the_hash_algo
->hexsz
+ 1);
3207 static void parse_cat_blob(const char *p
)
3209 struct object_entry
*oe
;
3210 struct object_id oid
;
3212 /* cat-blob SP <object> LF */
3214 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
3216 die(_("unknown mark: %s"), command_buf
.buf
);
3217 oidcpy(&oid
, &oe
->idx
.oid
);
3219 if (parse_mapped_oid_hex(p
, &oid
, &p
))
3220 die(_("invalid dataref: %s"), command_buf
.buf
);
3222 die(_("garbage after SHA1: %s"), command_buf
.buf
);
3223 oe
= find_object(&oid
);
3229 static struct object_entry
*dereference(struct object_entry
*oe
,
3230 struct object_id
*oid
)
3234 const unsigned hexsz
= the_hash_algo
->hexsz
;
3237 enum object_type type
= odb_read_object_info(the_repository
->objects
,
3240 die(_("object not found: %s"), oid_to_hex(oid
));
3242 oe
= insert_object(oid
);
3244 oe
->pack_id
= MAX_PACK_ID
;
3248 case OBJ_TREE
: /* easy case. */
3254 die(_("not a tree-ish: %s"), command_buf
.buf
);
3257 if (oe
->pack_id
!= MAX_PACK_ID
) { /* in a pack being written */
3258 buf
= gfi_unpack_entry(oe
, &size
);
3260 enum object_type unused
;
3261 buf
= odb_read_object(the_repository
->objects
, oid
,
3265 die(_("can't load object %s"), oid_to_hex(oid
));
3267 /* Peel one layer. */
3270 if (size
< hexsz
+ strlen("object ") ||
3271 get_oid_hex(buf
+ strlen("object "), oid
))
3272 die(_("invalid SHA1 in tag: %s"), command_buf
.buf
);
3275 if (size
< hexsz
+ strlen("tree ") ||
3276 get_oid_hex(buf
+ strlen("tree "), oid
))
3277 die(_("invalid SHA1 in commit: %s"), command_buf
.buf
);
3281 return find_object(oid
);
3284 static void insert_mapped_mark(uintmax_t mark
, void *object
, void *cbp
)
3286 struct object_id
*fromoid
= object
;
3287 struct object_id
*tooid
= find_mark(cbp
, mark
);
3291 it
= kh_put_oid_map(sub_oid_map
, *fromoid
, &ret
);
3292 /* We've already seen this object. */
3295 kh_value(sub_oid_map
, it
) = tooid
;
3298 static void build_mark_map_one(struct mark_set
*from
, struct mark_set
*to
)
3300 for_each_mark(from
, 0, insert_mapped_mark
, to
);
3303 static void build_mark_map(struct string_list
*from
, struct string_list
*to
)
3305 struct string_list_item
*fromp
, *top
;
3307 sub_oid_map
= kh_init_oid_map();
3309 for_each_string_list_item(fromp
, from
) {
3310 top
= string_list_lookup(to
, fromp
->string
);
3312 die(_("missing from marks for submodule '%s'"), fromp
->string
);
3313 } else if (!top
|| !top
->util
) {
3314 die(_("missing to marks for submodule '%s'"), fromp
->string
);
3316 build_mark_map_one(fromp
->util
, top
->util
);
3320 static struct object_entry
*parse_treeish_dataref(const char **p
)
3322 struct object_id oid
;
3323 struct object_entry
*e
;
3325 if (**p
== ':') { /* <mark> */
3326 e
= find_mark(marks
, parse_mark_ref_space(p
));
3328 die(_("unknown mark: %s"), command_buf
.buf
);
3329 oidcpy(&oid
, &e
->idx
.oid
);
3330 } else { /* <sha1> */
3331 if (parse_mapped_oid_hex(*p
, &oid
, p
))
3332 die(_("invalid dataref: %s"), command_buf
.buf
);
3333 e
= find_object(&oid
);
3335 die(_("missing space after tree-ish: %s"), command_buf
.buf
);
3338 while (!e
|| e
->type
!= OBJ_TREE
)
3339 e
= dereference(e
, &oid
);
3343 static void print_ls(int mode
, const unsigned char *hash
, const char *path
)
3345 static struct strbuf line
= STRBUF_INIT
;
3347 /* See show_tree(). */
3349 S_ISGITLINK(mode
) ? commit_type
:
3350 S_ISDIR(mode
) ? tree_type
:
3354 /* missing SP path LF */
3355 strbuf_reset(&line
);
3356 strbuf_addstr(&line
, "missing ");
3357 quote_c_style(path
, &line
, NULL
, 0);
3358 strbuf_addch(&line
, '\n');
3360 /* mode SP type SP object_name TAB path LF */
3361 strbuf_reset(&line
);
3362 strbuf_addf(&line
, "%06o %s %s\t",
3363 mode
& ~NO_DELTA
, type
, hash_to_hex(hash
));
3364 quote_c_style(path
, &line
, NULL
, 0);
3365 strbuf_addch(&line
, '\n');
3367 cat_blob_write(line
.buf
, line
.len
);
3370 static void parse_ls(const char *p
, struct branch
*b
)
3372 static struct strbuf path
= STRBUF_INIT
;
3373 struct tree_entry
*root
= NULL
;
3374 struct tree_entry leaf
= {NULL
};
3376 /* ls SP (<tree-ish> SP)? <path> */
3379 die(_("not in a commit: %s"), command_buf
.buf
);
3380 root
= &b
->branch_tree
;
3382 struct object_entry
*e
= parse_treeish_dataref(&p
);
3383 root
= new_tree_entry();
3384 oidcpy(&root
->versions
[1].oid
, &e
->idx
.oid
);
3385 if (!is_null_oid(&root
->versions
[1].oid
))
3386 root
->versions
[1].mode
= S_IFDIR
;
3389 strbuf_reset(&path
);
3390 parse_path_eol(&path
, p
, "path");
3391 tree_content_get(root
, path
.buf
, &leaf
, 1);
3393 * A directory in preparation would have a sha1 of zero
3394 * until it is saved. Save, for simplicity.
3396 if (S_ISDIR(leaf
.versions
[1].mode
))
3399 print_ls(leaf
.versions
[1].mode
, leaf
.versions
[1].oid
.hash
, path
.buf
);
3401 release_tree_content_recursive(leaf
.tree
);
3402 if (!b
|| root
!= &b
->branch_tree
)
3403 release_tree_entry(root
);
3406 static void checkpoint(void)
3408 checkpoint_requested
= 0;
3417 static void parse_checkpoint(void)
3419 checkpoint_requested
= 1;
3423 static void parse_progress(void)
3425 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
3426 fputc('\n', stdout
);
3431 static void parse_alias(void)
3433 struct object_entry
*e
;
3437 read_next_command();
3442 die(_("expected 'mark' command, got %s"), command_buf
.buf
);
3445 memset(&b
, 0, sizeof(b
));
3446 if (!parse_objectish_with_prefix(&b
, "to "))
3447 die(_("expected 'to' command, got %s"), command_buf
.buf
);
3448 e
= find_object(&b
.oid
);
3450 insert_mark(&marks
, next_mark
, e
);
3453 static char* make_fast_import_path(const char *path
)
3455 if (!relative_marks_paths
|| is_absolute_path(path
))
3456 return prefix_filename(global_prefix
, path
);
3457 return repo_git_path(the_repository
, "info/fast-import/%s", path
);
3460 static void option_import_marks(const char *marks
,
3461 int from_stream
, int ignore_missing
)
3463 if (import_marks_file
) {
3465 die(_("only one import-marks command allowed per stream"));
3467 /* read previous mark file */
3468 if(!import_marks_file_from_stream
)
3472 free(import_marks_file
);
3473 import_marks_file
= make_fast_import_path(marks
);
3474 import_marks_file_from_stream
= from_stream
;
3475 import_marks_file_ignore_missing
= ignore_missing
;
3478 static void option_date_format(const char *fmt
)
3480 if (!strcmp(fmt
, "raw"))
3481 whenspec
= WHENSPEC_RAW
;
3482 else if (!strcmp(fmt
, "raw-permissive"))
3483 whenspec
= WHENSPEC_RAW_PERMISSIVE
;
3484 else if (!strcmp(fmt
, "rfc2822"))
3485 whenspec
= WHENSPEC_RFC2822
;
3486 else if (!strcmp(fmt
, "now"))
3487 whenspec
= WHENSPEC_NOW
;
3489 die(_("unknown --date-format argument %s"), fmt
);
3492 static unsigned long ulong_arg(const char *option
, const char *arg
)
3495 unsigned long rv
= strtoul(arg
, &endptr
, 0);
3496 if (strchr(arg
, '-') || endptr
== arg
|| *endptr
)
3497 die(_("%s: argument must be a non-negative integer"), option
);
3501 static void option_depth(const char *depth
)
3503 max_depth
= ulong_arg("--depth", depth
);
3504 if (max_depth
> MAX_DEPTH
)
3505 die(_("--depth cannot exceed %u"), MAX_DEPTH
);
3508 static void option_active_branches(const char *branches
)
3510 max_active_branches
= ulong_arg("--active-branches", branches
);
3513 static void option_export_marks(const char *marks
)
3515 free(export_marks_file
);
3516 export_marks_file
= make_fast_import_path(marks
);
3519 static void option_cat_blob_fd(const char *fd
)
3521 unsigned long n
= ulong_arg("--cat-blob-fd", fd
);
3522 if (n
> (unsigned long) INT_MAX
)
3523 die(_("--cat-blob-fd cannot exceed %d"), INT_MAX
);
3524 cat_blob_fd
= (int) n
;
3527 static void option_export_pack_edges(const char *edges
)
3529 char *fn
= prefix_filename(global_prefix
, edges
);
3532 pack_edges
= xfopen(fn
, "a");
3536 static void option_rewrite_submodules(const char *arg
, struct string_list
*list
)
3538 struct mark_set
*ms
;
3540 char *s
= xstrdup(arg
);
3541 char *f
= strchr(s
, ':');
3543 die(_("expected format name:filename for submodule rewrite option"));
3546 CALLOC_ARRAY(ms
, 1);
3548 f
= prefix_filename(global_prefix
, f
);
3551 die_errno(_("cannot read '%s'"), f
);
3552 read_mark_file(&ms
, fp
, insert_oid_entry
);
3556 string_list_insert(list
, s
)->util
= ms
;
3561 static int parse_one_option(const char *option
)
3563 if (skip_prefix(option
, "max-pack-size=", &option
)) {
3565 if (!git_parse_ulong(option
, &v
))
3568 warning(_("max-pack-size is now in bytes, assuming --max-pack-size=%lum"), v
);
3570 } else if (v
< 1024 * 1024) {
3571 warning(_("minimum max-pack-size is 1 MiB"));
3575 } else if (skip_prefix(option
, "big-file-threshold=", &option
)) {
3577 if (!git_parse_ulong(option
, &v
))
3579 repo_settings_set_big_file_threshold(the_repository
, v
);
3580 } else if (skip_prefix(option
, "depth=", &option
)) {
3581 option_depth(option
);
3582 } else if (skip_prefix(option
, "active-branches=", &option
)) {
3583 option_active_branches(option
);
3584 } else if (skip_prefix(option
, "export-pack-edges=", &option
)) {
3585 option_export_pack_edges(option
);
3586 } else if (skip_prefix(option
, "signed-commits=", &option
)) {
3587 if (parse_sign_mode(option
, &signed_commit_mode
))
3588 usagef(_("unknown --signed-commits mode '%s'"), option
);
3589 } else if (skip_prefix(option
, "signed-tags=", &option
)) {
3590 if (parse_sign_mode(option
, &signed_tag_mode
))
3591 usagef(_("unknown --signed-tags mode '%s'"), option
);
3592 } else if (!strcmp(option
, "quiet")) {
3595 } else if (!strcmp(option
, "stats")) {
3597 } else if (!strcmp(option
, "allow-unsafe-features")) {
3598 ; /* already handled during early option parsing */
3606 static void check_unsafe_feature(const char *feature
, int from_stream
)
3608 if (from_stream
&& !allow_unsafe_features
)
3609 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3613 static int parse_one_feature(const char *feature
, int from_stream
)
3617 if (skip_prefix(feature
, "date-format=", &arg
)) {
3618 option_date_format(arg
);
3619 } else if (skip_prefix(feature
, "import-marks=", &arg
)) {
3620 check_unsafe_feature("import-marks", from_stream
);
3621 option_import_marks(arg
, from_stream
, 0);
3622 } else if (skip_prefix(feature
, "import-marks-if-exists=", &arg
)) {
3623 check_unsafe_feature("import-marks-if-exists", from_stream
);
3624 option_import_marks(arg
, from_stream
, 1);
3625 } else if (skip_prefix(feature
, "export-marks=", &arg
)) {
3626 check_unsafe_feature(feature
, from_stream
);
3627 option_export_marks(arg
);
3628 } else if (!strcmp(feature
, "alias")) {
3629 ; /* Don't die - this feature is supported */
3630 } else if (skip_prefix(feature
, "rewrite-submodules-to=", &arg
)) {
3631 option_rewrite_submodules(arg
, &sub_marks_to
);
3632 } else if (skip_prefix(feature
, "rewrite-submodules-from=", &arg
)) {
3633 option_rewrite_submodules(arg
, &sub_marks_from
);
3634 } else if (!strcmp(feature
, "get-mark")) {
3635 ; /* Don't die - this feature is supported */
3636 } else if (!strcmp(feature
, "cat-blob")) {
3637 ; /* Don't die - this feature is supported */
3638 } else if (!strcmp(feature
, "relative-marks")) {
3639 relative_marks_paths
= 1;
3640 } else if (!strcmp(feature
, "no-relative-marks")) {
3641 relative_marks_paths
= 0;
3642 } else if (!strcmp(feature
, "done")) {
3643 require_explicit_termination
= 1;
3644 } else if (!strcmp(feature
, "force")) {
3646 } else if (!strcmp(feature
, "notes") || !strcmp(feature
, "ls")) {
3647 ; /* do nothing; we have the feature */
3655 static void parse_feature(const char *feature
)
3657 if (seen_data_command
)
3658 die(_("got feature command '%s' after data command"), feature
);
3660 if (parse_one_feature(feature
, 1))
3663 die(_("this version of fast-import does not support feature %s."), feature
);
3666 static void parse_option(const char *option
)
3668 if (seen_data_command
)
3669 die(_("got option command '%s' after data command"), option
);
3671 if (parse_one_option(option
))
3674 die(_("this version of fast-import does not support option: %s"), option
);
3677 static void git_pack_config(void)
3679 int indexversion_value
;
3681 unsigned long packsizelimit_value
;
3683 if (!repo_config_get_ulong(the_repository
, "pack.depth", &max_depth
)) {
3684 if (max_depth
> MAX_DEPTH
)
3685 max_depth
= MAX_DEPTH
;
3687 if (!repo_config_get_int(the_repository
, "pack.indexversion", &indexversion_value
)) {
3688 pack_idx_opts
.version
= indexversion_value
;
3689 if (pack_idx_opts
.version
> 2)
3690 git_die_config(the_repository
, "pack.indexversion",
3691 "bad pack.indexVersion=%"PRIu32
, pack_idx_opts
.version
);
3693 if (!repo_config_get_ulong(the_repository
, "pack.packsizelimit", &packsizelimit_value
))
3694 max_packsize
= packsizelimit_value
;
3696 if (!repo_config_get_int(the_repository
, "fastimport.unpacklimit", &limit
))
3697 unpack_limit
= limit
;
3698 else if (!repo_config_get_int(the_repository
, "transfer.unpacklimit", &limit
))
3699 unpack_limit
= limit
;
3701 repo_config(the_repository
, git_default_config
, NULL
);
3704 static const char fast_import_usage
[] =
3705 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3707 static void parse_argv(void)
3711 for (i
= 1; i
< global_argc
; i
++) {
3712 const char *a
= global_argv
[i
];
3714 if (*a
!= '-' || !strcmp(a
, "--"))
3717 if (!skip_prefix(a
, "--", &a
))
3718 die(_("unknown option %s"), a
);
3720 if (parse_one_option(a
))
3723 if (parse_one_feature(a
, 0))
3726 if (skip_prefix(a
, "cat-blob-fd=", &a
)) {
3727 option_cat_blob_fd(a
);
3731 die(_("unknown option --%s"), a
);
3733 if (i
!= global_argc
)
3734 usage(fast_import_usage
);
3736 seen_data_command
= 1;
3737 if (import_marks_file
)
3739 build_mark_map(&sub_marks_from
, &sub_marks_to
);
3742 int cmd_fast_import(int argc
,
3745 struct repository
*repo
)
3749 show_usage_if_asked(argc
, argv
, fast_import_usage
);
3751 reset_pack_idx_option(&pack_idx_opts
);
3754 alloc_objects(object_entry_alloc
);
3755 strbuf_init(&command_buf
, 0);
3756 CALLOC_ARRAY(atom_table
, atom_table_sz
);
3757 CALLOC_ARRAY(branch_table
, branch_table_sz
);
3758 CALLOC_ARRAY(avail_tree_table
, avail_tree_table_sz
);
3759 marks
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
3761 hashmap_init(&object_table
, object_entry_hashcmp
, NULL
, 0);
3764 * We don't parse most options until after we've seen the set of
3765 * "feature" lines at the start of the stream (which allows the command
3766 * line to override stream data). But we must do an early parse of any
3767 * command-line options that impact how we interpret the feature lines.
3769 for (i
= 1; i
< argc
; i
++) {
3770 const char *arg
= argv
[i
];
3771 if (*arg
!= '-' || !strcmp(arg
, "--"))
3773 if (!strcmp(arg
, "--allow-unsafe-features"))
3774 allow_unsafe_features
= 1;
3779 global_prefix
= prefix
;
3781 rc_free
= mem_pool_alloc(&fi_mem_pool
, cmd_save
* sizeof(*rc_free
));
3782 for (i
= 0; i
< (cmd_save
- 1); i
++)
3783 rc_free
[i
].next
= &rc_free
[i
+ 1];
3784 rc_free
[cmd_save
- 1].next
= NULL
;
3787 set_die_routine(die_nicely
);
3788 set_checkpoint_signal();
3789 while (read_next_command() != EOF
) {
3791 if (!strcmp("blob", command_buf
.buf
))
3793 else if (skip_prefix(command_buf
.buf
, "commit ", &v
))
3794 parse_new_commit(v
);
3795 else if (skip_prefix(command_buf
.buf
, "tag ", &v
))
3797 else if (skip_prefix(command_buf
.buf
, "reset ", &v
))
3798 parse_reset_branch(v
);
3799 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
3801 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
3803 else if (skip_prefix(command_buf
.buf
, "get-mark ", &v
))
3805 else if (!strcmp("checkpoint", command_buf
.buf
))
3807 else if (!strcmp("done", command_buf
.buf
))
3809 else if (!strcmp("alias", command_buf
.buf
))
3811 else if (starts_with(command_buf
.buf
, "progress "))
3813 else if (skip_prefix(command_buf
.buf
, "feature ", &v
))
3815 else if (skip_prefix(command_buf
.buf
, "option git ", &v
))
3817 else if (starts_with(command_buf
.buf
, "option "))
3818 /* ignore non-git options*/;
3820 die(_("unsupported command: %s"), command_buf
.buf
);
3822 if (checkpoint_requested
)
3826 /* argv hasn't been parsed yet, do so */
3827 if (!seen_data_command
)
3830 if (require_explicit_termination
&& feof(stdin
))
3831 die(_("stream ends early"));
3844 uintmax_t total_count
= 0, duplicate_count
= 0;
3845 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3846 total_count
+= object_count_by_type
[i
];
3847 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3848 duplicate_count
+= duplicate_count_by_type
[i
];
3850 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3851 fprintf(stderr
, "---------------------------------------------------------------------\n");
3852 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3853 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3854 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
]);
3855 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
]);
3856 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
]);
3857 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
]);
3858 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3859 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3860 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3861 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (tree_entry_allocd
+ fi_mem_pool
.pool_alloc
+ alloc_count
*sizeof(struct object_entry
))/1024);
3862 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd
+ fi_mem_pool
.pool_alloc
) /1024));
3863 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3864 fprintf(stderr
, "---------------------------------------------------------------------\n");
3866 fprintf(stderr
, "---------------------------------------------------------------------\n");
3867 fprintf(stderr
, "\n");
3870 return failure
? 1 : 0;