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
;
192 static struct mem_pool fi_mem_pool
= {
193 .block_alloc
= 2*1024*1024 - sizeof(struct mp_block
),
196 /* Atom management */
197 static unsigned int atom_table_sz
= 4451;
198 static unsigned int atom_cnt
;
199 static struct atom_str
**atom_table
;
201 /* The .pack file being generated */
202 static struct pack_idx_option pack_idx_opts
;
203 static unsigned int pack_id
;
204 static struct hashfile
*pack_file
;
205 static struct packed_git
*pack_data
;
206 static struct packed_git
**all_packs
;
207 static off_t pack_size
;
209 /* Table of objects we've written. */
210 static unsigned int object_entry_alloc
= 5000;
211 static struct object_entry_pool
*blocks
;
212 static struct hashmap object_table
;
213 static struct mark_set
*marks
;
214 static char *export_marks_file
;
215 static char *import_marks_file
;
216 static int import_marks_file_from_stream
;
217 static int import_marks_file_ignore_missing
;
218 static int import_marks_file_done
;
219 static int relative_marks_paths
;
222 static struct last_object last_blob
= {
226 /* Tree management */
227 static unsigned int tree_entry_alloc
= 1000;
228 static void *avail_tree_entry
;
229 static unsigned int avail_tree_table_sz
= 100;
230 static struct avail_tree_content
**avail_tree_table
;
231 static size_t tree_entry_allocd
;
232 static struct strbuf old_tree
= STRBUF_INIT
;
233 static struct strbuf new_tree
= STRBUF_INIT
;
236 static unsigned long max_active_branches
= 5;
237 static unsigned long cur_active_branches
;
238 static unsigned long branch_table_sz
= 1039;
239 static struct branch
**branch_table
;
240 static struct branch
*active_branches
;
243 static struct tag
*first_tag
;
244 static struct tag
*last_tag
;
246 /* Input stream parsing */
247 static whenspec_type whenspec
= WHENSPEC_RAW
;
248 static struct strbuf command_buf
= STRBUF_INIT
;
249 static int unread_command_buf
;
250 static struct recent_command cmd_hist
= {
254 static struct recent_command
*cmd_tail
= &cmd_hist
;
255 static struct recent_command
*rc_free
;
256 static unsigned int cmd_save
= 100;
257 static uintmax_t next_mark
;
258 static struct strbuf new_data
= STRBUF_INIT
;
259 static int seen_data_command
;
260 static int require_explicit_termination
;
261 static int allow_unsafe_features
;
263 /* Signal handling */
264 static volatile sig_atomic_t checkpoint_requested
;
266 /* Submodule marks */
267 static struct string_list sub_marks_from
= STRING_LIST_INIT_DUP
;
268 static struct string_list sub_marks_to
= STRING_LIST_INIT_DUP
;
269 static kh_oid_map_t
*sub_oid_map
;
271 /* Where to write output of cat-blob commands */
272 static int cat_blob_fd
= STDOUT_FILENO
;
274 static void parse_argv(void);
275 static void parse_get_mark(const char *p
);
276 static void parse_cat_blob(const char *p
);
277 static void parse_ls(const char *p
, struct branch
*b
);
279 static void for_each_mark(struct mark_set
*m
, uintmax_t base
, each_mark_fn_t callback
, void *p
)
283 for (k
= 0; k
< 1024; k
++) {
285 for_each_mark(m
->data
.sets
[k
], base
+ (k
<< m
->shift
), callback
, p
);
288 for (k
= 0; k
< 1024; k
++) {
289 if (m
->data
.marked
[k
])
290 callback(base
+ k
, m
->data
.marked
[k
], p
);
295 static void dump_marks_fn(uintmax_t mark
, void *object
, void *cbp
) {
296 struct object_entry
*e
= object
;
299 fprintf(f
, ":%" PRIuMAX
" %s\n", mark
, oid_to_hex(&e
->idx
.oid
));
302 static void write_branch_report(FILE *rpt
, struct branch
*b
)
304 fprintf(rpt
, "%s:\n", b
->name
);
306 fprintf(rpt
, " status :");
308 fputs(" active", rpt
);
309 if (b
->branch_tree
.tree
)
310 fputs(" loaded", rpt
);
311 if (is_null_oid(&b
->branch_tree
.versions
[1].oid
))
312 fputs(" dirty", rpt
);
315 fprintf(rpt
, " tip commit : %s\n", oid_to_hex(&b
->oid
));
316 fprintf(rpt
, " old tree : %s\n",
317 oid_to_hex(&b
->branch_tree
.versions
[0].oid
));
318 fprintf(rpt
, " cur tree : %s\n",
319 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
320 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
322 fputs(" last pack : ", rpt
);
323 if (b
->pack_id
< MAX_PACK_ID
)
324 fprintf(rpt
, "%u", b
->pack_id
);
330 static void write_crash_report(const char *err
)
332 char *loc
= repo_git_path(the_repository
, "fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
333 FILE *rpt
= fopen(loc
, "w");
336 struct recent_command
*rc
;
339 error_errno("can't write crash report %s", loc
);
344 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
346 fprintf(rpt
, "fast-import crash report:\n");
347 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
348 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
349 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_MODE(ISO8601
)));
352 fputs("fatal: ", rpt
);
357 fputs("Most Recent Commands Before Crash\n", rpt
);
358 fputs("---------------------------------\n", rpt
);
359 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
360 if (rc
->next
== &cmd_hist
)
369 fputs("Active Branch LRU\n", rpt
);
370 fputs("-----------------\n", rpt
);
371 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
373 max_active_branches
);
375 fputs(" pos clock name\n", rpt
);
376 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
377 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
378 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
379 ++lu
, b
->last_commit
, b
->name
);
382 fputs("Inactive Branches\n", rpt
);
383 fputs("-----------------\n", rpt
);
384 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
385 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
386 write_branch_report(rpt
, b
);
392 fputs("Annotated Tags\n", rpt
);
393 fputs("--------------\n", rpt
);
394 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
395 fputs(oid_to_hex(&tg
->oid
), rpt
);
397 fputs(tg
->name
, rpt
);
403 fputs("Marks\n", rpt
);
404 fputs("-----\n", rpt
);
405 if (export_marks_file
)
406 fprintf(rpt
, " exported to %s\n", export_marks_file
);
408 for_each_mark(marks
, 0, dump_marks_fn
, rpt
);
411 fputs("-------------------\n", rpt
);
412 fputs("END OF CRASH REPORT\n", rpt
);
417 static void end_packfile(void);
418 static void unkeep_all_packs(void);
419 static void dump_marks(void);
421 static NORETURN
void die_nicely(const char *err
, va_list params
)
425 report_fn die_message_fn
= get_die_message_routine();
428 die_message_fn(err
, params
);
431 char message
[2 * PATH_MAX
];
434 vsnprintf(message
, sizeof(message
), err
, cp
);
435 write_crash_report(message
);
443 #ifndef SIGUSR1 /* Windows, for example */
445 static void set_checkpoint_signal(void)
451 static void checkpoint_signal(int signo UNUSED
)
453 checkpoint_requested
= 1;
456 static void set_checkpoint_signal(void)
460 memset(&sa
, 0, sizeof(sa
));
461 sa
.sa_handler
= checkpoint_signal
;
462 sigemptyset(&sa
.sa_mask
);
463 sa
.sa_flags
= SA_RESTART
;
464 sigaction(SIGUSR1
, &sa
, NULL
);
469 static void alloc_objects(unsigned int cnt
)
471 struct object_entry_pool
*b
;
473 b
= xmalloc(sizeof(struct object_entry_pool
)
474 + cnt
* sizeof(struct object_entry
));
475 b
->next_pool
= blocks
;
476 b
->next_free
= b
->entries
;
477 b
->end
= b
->entries
+ cnt
;
482 static struct object_entry
*new_object(struct object_id
*oid
)
484 struct object_entry
*e
;
486 if (blocks
->next_free
== blocks
->end
)
487 alloc_objects(object_entry_alloc
);
489 e
= blocks
->next_free
++;
490 oidcpy(&e
->idx
.oid
, oid
);
494 static struct object_entry
*find_object(struct object_id
*oid
)
496 return hashmap_get_entry_from_hash(&object_table
, oidhash(oid
), oid
,
497 struct object_entry
, ent
);
500 static struct object_entry
*insert_object(struct object_id
*oid
)
502 struct object_entry
*e
;
503 unsigned int hash
= oidhash(oid
);
505 e
= hashmap_get_entry_from_hash(&object_table
, hash
, oid
,
506 struct object_entry
, ent
);
510 hashmap_entry_init(&e
->ent
, hash
);
511 hashmap_add(&object_table
, &e
->ent
);
517 static void invalidate_pack_id(unsigned int id
)
521 struct hashmap_iter iter
;
522 struct object_entry
*e
;
524 hashmap_for_each_entry(&object_table
, &iter
, e
, ent
) {
525 if (e
->pack_id
== id
)
526 e
->pack_id
= MAX_PACK_ID
;
529 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
532 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
533 if (b
->pack_id
== id
)
534 b
->pack_id
= MAX_PACK_ID
;
537 for (t
= first_tag
; t
; t
= t
->next_tag
)
538 if (t
->pack_id
== id
)
539 t
->pack_id
= MAX_PACK_ID
;
542 static unsigned int hc_str(const char *s
, size_t len
)
550 static void insert_mark(struct mark_set
**top
, uintmax_t idnum
, struct object_entry
*oe
)
552 struct mark_set
*s
= *top
;
554 while ((idnum
>> s
->shift
) >= 1024) {
555 s
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
556 s
->shift
= (*top
)->shift
+ 10;
557 s
->data
.sets
[0] = *top
;
561 uintmax_t i
= idnum
>> s
->shift
;
562 idnum
-= i
<< s
->shift
;
563 if (!s
->data
.sets
[i
]) {
564 s
->data
.sets
[i
] = mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
565 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
569 if (!s
->data
.marked
[idnum
])
571 s
->data
.marked
[idnum
] = oe
;
574 static void *find_mark(struct mark_set
*s
, uintmax_t idnum
)
576 uintmax_t orig_idnum
= idnum
;
577 struct object_entry
*oe
= NULL
;
578 if ((idnum
>> s
->shift
) < 1024) {
579 while (s
&& s
->shift
) {
580 uintmax_t i
= idnum
>> s
->shift
;
581 idnum
-= i
<< s
->shift
;
585 oe
= s
->data
.marked
[idnum
];
588 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
592 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
594 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
597 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
598 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
601 c
= mem_pool_alloc(&fi_mem_pool
, sizeof(struct atom_str
) + len
+ 1);
603 memcpy(c
->str_dat
, s
, len
);
605 c
->next_atom
= atom_table
[hc
];
611 static struct branch
*lookup_branch(const char *name
)
613 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
616 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
617 if (!strcmp(name
, b
->name
))
622 static struct branch
*new_branch(const char *name
)
624 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
625 struct branch
*b
= lookup_branch(name
);
628 die("Invalid attempt to create duplicate branch: %s", name
);
629 if (check_refname_format(name
, REFNAME_ALLOW_ONELEVEL
))
630 die("Branch name doesn't conform to GIT standards: %s", name
);
632 b
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct branch
));
633 b
->name
= mem_pool_strdup(&fi_mem_pool
, name
);
634 b
->table_next_branch
= branch_table
[hc
];
635 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
636 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
639 b
->pack_id
= MAX_PACK_ID
;
640 branch_table
[hc
] = b
;
645 static unsigned int hc_entries(unsigned int cnt
)
647 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
648 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
651 static struct tree_content
*new_tree_content(unsigned int cnt
)
653 struct avail_tree_content
*f
, *l
= NULL
;
654 struct tree_content
*t
;
655 unsigned int hc
= hc_entries(cnt
);
657 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
658 if (f
->entry_capacity
>= cnt
)
663 l
->next_avail
= f
->next_avail
;
665 avail_tree_table
[hc
] = f
->next_avail
;
667 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
668 f
= mem_pool_alloc(&fi_mem_pool
, sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
669 f
->entry_capacity
= cnt
;
672 t
= (struct tree_content
*)f
;
678 static void release_tree_entry(struct tree_entry
*e
);
679 static void release_tree_content(struct tree_content
*t
)
681 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
682 unsigned int hc
= hc_entries(f
->entry_capacity
);
683 f
->next_avail
= avail_tree_table
[hc
];
684 avail_tree_table
[hc
] = f
;
687 static void release_tree_content_recursive(struct tree_content
*t
)
690 for (i
= 0; i
< t
->entry_count
; i
++)
691 release_tree_entry(t
->entries
[i
]);
692 release_tree_content(t
);
695 static struct tree_content
*grow_tree_content(
696 struct tree_content
*t
,
699 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
700 r
->entry_count
= t
->entry_count
;
701 r
->delta_depth
= t
->delta_depth
;
702 COPY_ARRAY(r
->entries
, t
->entries
, t
->entry_count
);
703 release_tree_content(t
);
707 static struct tree_entry
*new_tree_entry(void)
709 struct tree_entry
*e
;
711 if (!avail_tree_entry
) {
712 unsigned int n
= tree_entry_alloc
;
713 tree_entry_allocd
+= n
* sizeof(struct tree_entry
);
715 avail_tree_entry
= e
;
717 *((void**)e
) = e
+ 1;
723 e
= avail_tree_entry
;
724 avail_tree_entry
= *((void**)e
);
728 static void release_tree_entry(struct tree_entry
*e
)
731 release_tree_content_recursive(e
->tree
);
732 *((void**)e
) = avail_tree_entry
;
733 avail_tree_entry
= e
;
736 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
738 struct tree_content
*d
;
739 struct tree_entry
*a
, *b
;
744 d
= new_tree_content(s
->entry_count
);
745 for (i
= 0; i
< s
->entry_count
; i
++) {
747 b
= new_tree_entry();
748 memcpy(b
, a
, sizeof(*a
));
749 if (a
->tree
&& is_null_oid(&b
->versions
[1].oid
))
750 b
->tree
= dup_tree_content(a
->tree
);
755 d
->entry_count
= s
->entry_count
;
756 d
->delta_depth
= s
->delta_depth
;
761 static void start_packfile(void)
763 struct strbuf tmp_file
= STRBUF_INIT
;
764 struct packed_git
*p
;
767 pack_fd
= odb_mkstemp(the_repository
->objects
, &tmp_file
,
768 "pack/tmp_pack_XXXXXX");
769 FLEX_ALLOC_STR(p
, pack_name
, tmp_file
.buf
);
770 strbuf_release(&tmp_file
);
772 p
->pack_fd
= pack_fd
;
774 p
->repo
= the_repository
;
775 pack_file
= hashfd(the_repository
->hash_algo
, pack_fd
, p
->pack_name
);
778 pack_size
= write_pack_header(pack_file
, 0);
781 REALLOC_ARRAY(all_packs
, pack_id
+ 1);
782 all_packs
[pack_id
] = p
;
785 static const char *create_index(void)
788 struct pack_idx_entry
**idx
, **c
, **last
;
789 struct object_entry
*e
;
790 struct object_entry_pool
*o
;
792 /* Build the table of object IDs. */
793 ALLOC_ARRAY(idx
, object_count
);
795 for (o
= blocks
; o
; o
= o
->next_pool
)
796 for (e
= o
->next_free
; e
-- != o
->entries
;)
797 if (pack_id
== e
->pack_id
)
799 last
= idx
+ object_count
;
801 die("internal consistency error creating the index");
803 tmpfile
= write_idx_file(the_repository
, NULL
, idx
, object_count
,
804 &pack_idx_opts
, pack_data
->hash
);
809 static char *keep_pack(const char *curr_index_name
)
811 static const char *keep_msg
= "fast-import";
812 struct strbuf name
= STRBUF_INIT
;
815 odb_pack_name(pack_data
->repo
, &name
, pack_data
->hash
, "keep");
816 keep_fd
= safe_create_file_with_leading_directories(pack_data
->repo
,
819 die_errno("cannot create keep file");
820 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
822 die_errno("failed to write keep file");
824 odb_pack_name(pack_data
->repo
, &name
, pack_data
->hash
, "pack");
825 if (finalize_object_file(pack_data
->repo
, pack_data
->pack_name
, name
.buf
))
826 die("cannot store pack file");
828 odb_pack_name(pack_data
->repo
, &name
, pack_data
->hash
, "idx");
829 if (finalize_object_file(pack_data
->repo
, curr_index_name
, name
.buf
))
830 die("cannot store index file");
831 free((void *)curr_index_name
);
832 return strbuf_detach(&name
, NULL
);
835 static void unkeep_all_packs(void)
837 struct strbuf name
= STRBUF_INIT
;
840 for (k
= 0; k
< pack_id
; k
++) {
841 struct packed_git
*p
= all_packs
[k
];
842 odb_pack_name(p
->repo
, &name
, p
->hash
, "keep");
843 unlink_or_warn(name
.buf
);
845 strbuf_release(&name
);
848 static int loosen_small_pack(const struct packed_git
*p
)
850 struct child_process unpack
= CHILD_PROCESS_INIT
;
852 if (lseek(p
->pack_fd
, 0, SEEK_SET
) < 0)
853 die_errno("Failed seeking to start of '%s'", p
->pack_name
);
855 unpack
.in
= p
->pack_fd
;
857 unpack
.stdout_to_stderr
= 1;
858 strvec_push(&unpack
.args
, "unpack-objects");
860 strvec_push(&unpack
.args
, "-q");
862 return run_command(&unpack
);
865 static void end_packfile(void)
869 if (running
|| !pack_data
)
873 clear_delta_base_cache();
875 struct packed_git
*new_p
;
876 struct object_id cur_pack_oid
;
882 close_pack_windows(pack_data
);
883 finalize_hashfile(pack_file
, cur_pack_oid
.hash
, FSYNC_COMPONENT_PACK
, 0);
884 fixup_pack_header_footer(the_hash_algo
, pack_data
->pack_fd
,
885 pack_data
->hash
, pack_data
->pack_name
,
886 object_count
, cur_pack_oid
.hash
,
889 if (object_count
<= unpack_limit
) {
890 if (!loosen_small_pack(pack_data
)) {
891 invalidate_pack_id(pack_id
);
896 close(pack_data
->pack_fd
);
897 idx_name
= keep_pack(create_index());
899 /* Register the packfile with core git's machinery. */
900 new_p
= add_packed_git(pack_data
->repo
, idx_name
, strlen(idx_name
), 1);
902 die("core git rejected index %s", idx_name
);
903 all_packs
[pack_id
] = new_p
;
904 install_packed_git(the_repository
, new_p
);
907 /* Print the boundary */
909 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
910 for (i
= 0; i
< branch_table_sz
; i
++) {
911 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
912 if (b
->pack_id
== pack_id
)
913 fprintf(pack_edges
, " %s",
914 oid_to_hex(&b
->oid
));
917 for (t
= first_tag
; t
; t
= t
->next_tag
) {
918 if (t
->pack_id
== pack_id
)
919 fprintf(pack_edges
, " %s",
920 oid_to_hex(&t
->oid
));
922 fputc('\n', pack_edges
);
930 close(pack_data
->pack_fd
);
931 unlink_or_warn(pack_data
->pack_name
);
933 FREE_AND_NULL(pack_data
);
936 /* We can't carry a delta across packfiles. */
937 strbuf_release(&last_blob
.data
);
938 last_blob
.offset
= 0;
942 static void cycle_packfile(void)
948 static int store_object(
949 enum object_type type
,
951 struct last_object
*last
,
952 struct object_id
*oidout
,
956 struct object_entry
*e
;
957 unsigned char hdr
[96];
958 struct object_id oid
;
959 unsigned long hdrlen
, deltalen
;
960 struct git_hash_ctx c
;
963 hdrlen
= format_object_header((char *)hdr
, sizeof(hdr
), type
,
965 the_hash_algo
->init_fn(&c
);
966 git_hash_update(&c
, hdr
, hdrlen
);
967 git_hash_update(&c
, dat
->buf
, dat
->len
);
968 git_hash_final_oid(&oid
, &c
);
970 oidcpy(oidout
, &oid
);
972 e
= insert_object(&oid
);
974 insert_mark(&marks
, mark
, e
);
976 duplicate_count_by_type
[type
]++;
978 } else if (find_oid_pack(&oid
, get_all_packs(the_repository
))) {
980 e
->pack_id
= MAX_PACK_ID
;
981 e
->idx
.offset
= 1; /* just not zero! */
982 duplicate_count_by_type
[type
]++;
986 if (last
&& last
->data
.len
&& last
->data
.buf
&& last
->depth
< max_depth
987 && dat
->len
> the_hash_algo
->rawsz
) {
989 delta_count_attempts_by_type
[type
]++;
990 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
992 &deltalen
, dat
->len
- the_hash_algo
->rawsz
);
996 git_deflate_init(&s
, pack_compression_level
);
999 s
.avail_in
= deltalen
;
1001 s
.next_in
= (void *)dat
->buf
;
1002 s
.avail_in
= dat
->len
;
1004 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
1005 s
.next_out
= out
= xmalloc(s
.avail_out
);
1006 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
1008 git_deflate_end(&s
);
1010 /* Determine if we should auto-checkpoint. */
1012 && (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) > max_packsize
)
1013 || (pack_size
+ PACK_SIZE_THRESHOLD
+ s
.total_out
) < pack_size
) {
1015 /* This new object needs to *not* have the current pack_id. */
1016 e
->pack_id
= pack_id
+ 1;
1019 /* We cannot carry a delta into the new pack. */
1021 FREE_AND_NULL(delta
);
1023 git_deflate_init(&s
, pack_compression_level
);
1024 s
.next_in
= (void *)dat
->buf
;
1025 s
.avail_in
= dat
->len
;
1026 s
.avail_out
= git_deflate_bound(&s
, s
.avail_in
);
1027 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
1028 while (git_deflate(&s
, Z_FINISH
) == Z_OK
)
1030 git_deflate_end(&s
);
1035 e
->pack_id
= pack_id
;
1036 e
->idx
.offset
= pack_size
;
1038 object_count_by_type
[type
]++;
1040 crc32_begin(pack_file
);
1043 off_t ofs
= e
->idx
.offset
- last
->offset
;
1044 unsigned pos
= sizeof(hdr
) - 1;
1046 delta_count_by_type
[type
]++;
1047 e
->depth
= last
->depth
+ 1;
1049 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1050 OBJ_OFS_DELTA
, deltalen
);
1051 hashwrite(pack_file
, hdr
, hdrlen
);
1052 pack_size
+= hdrlen
;
1054 hdr
[pos
] = ofs
& 127;
1056 hdr
[--pos
] = 128 | (--ofs
& 127);
1057 hashwrite(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
1058 pack_size
+= sizeof(hdr
) - pos
;
1061 hdrlen
= encode_in_pack_object_header(hdr
, sizeof(hdr
),
1063 hashwrite(pack_file
, hdr
, hdrlen
);
1064 pack_size
+= hdrlen
;
1067 hashwrite(pack_file
, out
, s
.total_out
);
1068 pack_size
+= s
.total_out
;
1070 e
->idx
.crc32
= crc32_end(pack_file
);
1075 if (last
->no_swap
) {
1078 strbuf_swap(&last
->data
, dat
);
1080 last
->offset
= e
->idx
.offset
;
1081 last
->depth
= e
->depth
;
1086 static void truncate_pack(struct hashfile_checkpoint
*checkpoint
)
1088 if (hashfile_truncate(pack_file
, checkpoint
))
1089 die_errno("cannot truncate pack to skip duplicate");
1090 pack_size
= checkpoint
->offset
;
1093 static void stream_blob(uintmax_t len
, struct object_id
*oidout
, uintmax_t mark
)
1095 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1096 unsigned char *in_buf
= xmalloc(in_sz
);
1097 unsigned char *out_buf
= xmalloc(out_sz
);
1098 struct object_entry
*e
;
1099 struct object_id oid
;
1100 unsigned long hdrlen
;
1102 struct git_hash_ctx c
;
1104 struct hashfile_checkpoint checkpoint
;
1107 /* Determine if we should auto-checkpoint. */
1109 && (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) > max_packsize
)
1110 || (pack_size
+ PACK_SIZE_THRESHOLD
+ len
) < pack_size
)
1113 hashfile_checkpoint_init(pack_file
, &checkpoint
);
1114 hashfile_checkpoint(pack_file
, &checkpoint
);
1115 offset
= checkpoint
.offset
;
1117 hdrlen
= format_object_header((char *)out_buf
, out_sz
, OBJ_BLOB
, len
);
1119 the_hash_algo
->init_fn(&c
);
1120 git_hash_update(&c
, out_buf
, hdrlen
);
1122 crc32_begin(pack_file
);
1124 git_deflate_init(&s
, pack_compression_level
);
1126 hdrlen
= encode_in_pack_object_header(out_buf
, out_sz
, OBJ_BLOB
, len
);
1128 s
.next_out
= out_buf
+ hdrlen
;
1129 s
.avail_out
= out_sz
- hdrlen
;
1131 while (status
!= Z_STREAM_END
) {
1132 if (0 < len
&& !s
.avail_in
) {
1133 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1134 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1135 if (!n
&& feof(stdin
))
1136 die("EOF in data (%" PRIuMAX
" bytes remaining)", len
);
1138 git_hash_update(&c
, in_buf
, n
);
1144 status
= git_deflate(&s
, len
? 0 : Z_FINISH
);
1146 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1147 size_t n
= s
.next_out
- out_buf
;
1148 hashwrite(pack_file
, out_buf
, n
);
1150 s
.next_out
= out_buf
;
1151 s
.avail_out
= out_sz
;
1160 die("unexpected deflate failure: %d", status
);
1163 git_deflate_end(&s
);
1164 git_hash_final_oid(&oid
, &c
);
1167 oidcpy(oidout
, &oid
);
1169 e
= insert_object(&oid
);
1172 insert_mark(&marks
, mark
, e
);
1174 if (e
->idx
.offset
) {
1175 duplicate_count_by_type
[OBJ_BLOB
]++;
1176 truncate_pack(&checkpoint
);
1178 } else if (find_oid_pack(&oid
, get_all_packs(the_repository
))) {
1180 e
->pack_id
= MAX_PACK_ID
;
1181 e
->idx
.offset
= 1; /* just not zero! */
1182 duplicate_count_by_type
[OBJ_BLOB
]++;
1183 truncate_pack(&checkpoint
);
1188 e
->pack_id
= pack_id
;
1189 e
->idx
.offset
= offset
;
1190 e
->idx
.crc32
= crc32_end(pack_file
);
1192 object_count_by_type
[OBJ_BLOB
]++;
1199 /* All calls must be guarded by find_object() or find_mark() to
1200 * ensure the 'struct object_entry' passed was written by this
1201 * process instance. We unpack the entry by the offset, avoiding
1202 * the need for the corresponding .idx file. This unpacking rule
1203 * works because we only use OBJ_REF_DELTA within the packfiles
1204 * created by fast-import.
1206 * oe must not be NULL. Such an oe usually comes from giving
1207 * an unknown SHA-1 to find_object() or an undefined mark to
1208 * find_mark(). Callers must test for this condition and use
1209 * the standard read_sha1_file() when it happens.
1211 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1212 * find_mark(), where the mark was reloaded from an existing marks
1213 * file and is referencing an object that this fast-import process
1214 * instance did not write out to a packfile. Callers must test for
1215 * this condition and use read_sha1_file() instead.
1217 static void *gfi_unpack_entry(
1218 struct object_entry
*oe
,
1219 unsigned long *sizep
)
1221 enum object_type type
;
1222 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1223 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ the_hash_algo
->rawsz
)) {
1224 /* The object is stored in the packfile we are writing to
1225 * and we have modified it since the last time we scanned
1226 * back to read a previously written object. If an old
1227 * window covered [p->pack_size, p->pack_size + rawsz) its
1228 * data is stale and is not valid. Closing all windows
1229 * and updating the packfile length ensures we can read
1230 * the newly written data.
1232 close_pack_windows(p
);
1233 hashflush(pack_file
);
1235 /* We have to offer rawsz bytes additional on the end of
1236 * the packfile as the core unpacker code assumes the
1237 * footer is present at the file end and must promise
1238 * at least rawsz bytes within any window it maps. But
1239 * we don't actually create the footer here.
1241 p
->pack_size
= pack_size
+ the_hash_algo
->rawsz
;
1243 return unpack_entry(the_repository
, p
, oe
->idx
.offset
, &type
, sizep
);
1246 static void load_tree(struct tree_entry
*root
)
1248 struct object_id
*oid
= &root
->versions
[1].oid
;
1249 struct object_entry
*myoe
;
1250 struct tree_content
*t
;
1255 root
->tree
= t
= new_tree_content(8);
1256 if (is_null_oid(oid
))
1259 myoe
= find_object(oid
);
1260 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1261 if (myoe
->type
!= OBJ_TREE
)
1262 die("Not a tree: %s", oid_to_hex(oid
));
1263 t
->delta_depth
= myoe
->depth
;
1264 buf
= gfi_unpack_entry(myoe
, &size
);
1266 die("Can't load tree %s", oid_to_hex(oid
));
1268 enum object_type type
;
1269 buf
= odb_read_object(the_repository
->objects
, oid
, &type
, &size
);
1270 if (!buf
|| type
!= OBJ_TREE
)
1271 die("Can't load tree %s", oid_to_hex(oid
));
1275 while (c
!= (buf
+ size
)) {
1276 struct tree_entry
*e
= new_tree_entry();
1278 if (t
->entry_count
== t
->entry_capacity
)
1279 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1280 t
->entries
[t
->entry_count
++] = e
;
1283 c
= parse_mode(c
, &e
->versions
[1].mode
);
1285 die("Corrupt mode in %s", oid_to_hex(oid
));
1286 e
->versions
[0].mode
= e
->versions
[1].mode
;
1287 e
->name
= to_atom(c
, strlen(c
));
1288 c
+= e
->name
->str_len
+ 1;
1289 oidread(&e
->versions
[0].oid
, (unsigned char *)c
,
1290 the_repository
->hash_algo
);
1291 oidread(&e
->versions
[1].oid
, (unsigned char *)c
,
1292 the_repository
->hash_algo
);
1293 c
+= the_hash_algo
->rawsz
;
1298 static int tecmp0 (const void *_a
, const void *_b
)
1300 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1301 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1302 return base_name_compare(
1303 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1304 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1307 static int tecmp1 (const void *_a
, const void *_b
)
1309 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1310 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1311 return base_name_compare(
1312 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1313 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1316 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1322 QSORT(t
->entries
, t
->entry_count
, tecmp0
);
1324 QSORT(t
->entries
, t
->entry_count
, tecmp1
);
1326 for (i
= 0; i
< t
->entry_count
; i
++) {
1327 if (t
->entries
[i
]->versions
[v
].mode
)
1328 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1332 strbuf_grow(b
, maxlen
);
1333 for (i
= 0; i
< t
->entry_count
; i
++) {
1334 struct tree_entry
*e
= t
->entries
[i
];
1335 if (!e
->versions
[v
].mode
)
1337 strbuf_addf(b
, "%o %s%c",
1338 (unsigned int)(e
->versions
[v
].mode
& ~NO_DELTA
),
1339 e
->name
->str_dat
, '\0');
1340 strbuf_add(b
, e
->versions
[v
].oid
.hash
, the_hash_algo
->rawsz
);
1344 static void store_tree(struct tree_entry
*root
)
1346 struct tree_content
*t
;
1347 unsigned int i
, j
, del
;
1348 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1349 struct object_entry
*le
= NULL
;
1351 if (!is_null_oid(&root
->versions
[1].oid
))
1358 for (i
= 0; i
< t
->entry_count
; i
++) {
1359 if (t
->entries
[i
]->tree
)
1360 store_tree(t
->entries
[i
]);
1363 if (!(root
->versions
[0].mode
& NO_DELTA
))
1364 le
= find_object(&root
->versions
[0].oid
);
1365 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1366 mktree(t
, 0, &old_tree
);
1368 lo
.offset
= le
->idx
.offset
;
1369 lo
.depth
= t
->delta_depth
;
1372 mktree(t
, 1, &new_tree
);
1373 store_object(OBJ_TREE
, &new_tree
, &lo
, &root
->versions
[1].oid
, 0);
1375 t
->delta_depth
= lo
.depth
;
1376 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1377 struct tree_entry
*e
= t
->entries
[i
];
1378 if (e
->versions
[1].mode
) {
1379 e
->versions
[0].mode
= e
->versions
[1].mode
;
1380 oidcpy(&e
->versions
[0].oid
, &e
->versions
[1].oid
);
1381 t
->entries
[j
++] = e
;
1383 release_tree_entry(e
);
1387 t
->entry_count
-= del
;
1390 static void tree_content_replace(
1391 struct tree_entry
*root
,
1392 const struct object_id
*oid
,
1393 const uint16_t mode
,
1394 struct tree_content
*newtree
)
1397 die("Root cannot be a non-directory");
1398 oidclr(&root
->versions
[0].oid
, the_repository
->hash_algo
);
1399 oidcpy(&root
->versions
[1].oid
, oid
);
1401 release_tree_content_recursive(root
->tree
);
1402 root
->tree
= newtree
;
1405 static int tree_content_set(
1406 struct tree_entry
*root
,
1408 const struct object_id
*oid
,
1409 const uint16_t mode
,
1410 struct tree_content
*subtree
)
1412 struct tree_content
*t
;
1415 struct tree_entry
*e
;
1417 slash1
= strchrnul(p
, '/');
1420 die("Empty path component found in input");
1421 if (!*slash1
&& !S_ISDIR(mode
) && subtree
)
1422 die("Non-directories cannot have subtrees");
1427 for (i
= 0; i
< t
->entry_count
; i
++) {
1429 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1432 && e
->versions
[1].mode
== mode
1433 && oideq(&e
->versions
[1].oid
, oid
))
1435 e
->versions
[1].mode
= mode
;
1436 oidcpy(&e
->versions
[1].oid
, oid
);
1438 release_tree_content_recursive(e
->tree
);
1442 * We need to leave e->versions[0].sha1 alone
1443 * to avoid modifying the preimage tree used
1444 * when writing out the parent directory.
1445 * But after replacing the subdir with a
1446 * completely different one, it's not a good
1447 * delta base any more, and besides, we've
1448 * thrown away the tree entries needed to
1449 * make a delta against it.
1451 * So let's just explicitly disable deltas
1454 if (S_ISDIR(e
->versions
[0].mode
))
1455 e
->versions
[0].mode
|= NO_DELTA
;
1457 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1460 if (!S_ISDIR(e
->versions
[1].mode
)) {
1461 e
->tree
= new_tree_content(8);
1462 e
->versions
[1].mode
= S_IFDIR
;
1466 if (tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
)) {
1467 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1474 if (t
->entry_count
== t
->entry_capacity
)
1475 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1476 e
= new_tree_entry();
1477 e
->name
= to_atom(p
, n
);
1478 e
->versions
[0].mode
= 0;
1479 oidclr(&e
->versions
[0].oid
, the_repository
->hash_algo
);
1480 t
->entries
[t
->entry_count
++] = e
;
1482 e
->tree
= new_tree_content(8);
1483 e
->versions
[1].mode
= S_IFDIR
;
1484 tree_content_set(e
, slash1
+ 1, oid
, mode
, subtree
);
1487 e
->versions
[1].mode
= mode
;
1488 oidcpy(&e
->versions
[1].oid
, oid
);
1490 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1494 static int tree_content_remove(
1495 struct tree_entry
*root
,
1497 struct tree_entry
*backup_leaf
,
1500 struct tree_content
*t
;
1503 struct tree_entry
*e
;
1505 slash1
= strchrnul(p
, '/');
1511 if (!*p
&& allow_root
) {
1517 for (i
= 0; i
< t
->entry_count
; i
++) {
1519 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1520 if (*slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1522 * If p names a file in some subdirectory, and a
1523 * file or symlink matching the name of the
1524 * parent directory of p exists, then p cannot
1525 * exist and need not be deleted.
1528 if (!*slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1532 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
, 0)) {
1533 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1534 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1535 oidclr(&root
->versions
[1].oid
,
1536 the_repository
->hash_algo
);
1550 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1552 release_tree_content_recursive(e
->tree
);
1554 e
->versions
[1].mode
= 0;
1555 oidclr(&e
->versions
[1].oid
, the_repository
->hash_algo
);
1556 oidclr(&root
->versions
[1].oid
, the_repository
->hash_algo
);
1560 static int tree_content_get(
1561 struct tree_entry
*root
,
1563 struct tree_entry
*leaf
,
1566 struct tree_content
*t
;
1569 struct tree_entry
*e
;
1571 slash1
= strchrnul(p
, '/');
1573 if (!n
&& !allow_root
)
1574 die("Empty path component found in input");
1585 for (i
= 0; i
< t
->entry_count
; i
++) {
1587 if (e
->name
->str_len
== n
&& !fspathncmp(p
, e
->name
->str_dat
, n
)) {
1590 if (!S_ISDIR(e
->versions
[1].mode
))
1594 return tree_content_get(e
, slash1
+ 1, leaf
, 0);
1600 memcpy(leaf
, e
, sizeof(*leaf
));
1601 if (e
->tree
&& is_null_oid(&e
->versions
[1].oid
))
1602 leaf
->tree
= dup_tree_content(e
->tree
);
1608 static int update_branch(struct branch
*b
)
1610 static const char *msg
= "fast-import";
1611 struct ref_transaction
*transaction
;
1612 struct object_id old_oid
;
1613 struct strbuf err
= STRBUF_INIT
;
1614 static const char *replace_prefix
= "refs/replace/";
1616 if (starts_with(b
->name
, replace_prefix
) &&
1617 !strcmp(b
->name
+ strlen(replace_prefix
),
1618 oid_to_hex(&b
->oid
))) {
1620 warning("Dropping %s since it would point to "
1621 "itself (i.e. to %s)",
1622 b
->name
, oid_to_hex(&b
->oid
));
1623 refs_delete_ref(get_main_ref_store(the_repository
),
1624 NULL
, b
->name
, NULL
, 0);
1627 if (is_null_oid(&b
->oid
)) {
1629 refs_delete_ref(get_main_ref_store(the_repository
),
1630 NULL
, b
->name
, NULL
, 0);
1633 if (refs_read_ref(get_main_ref_store(the_repository
), b
->name
, &old_oid
))
1634 oidclr(&old_oid
, the_repository
->hash_algo
);
1635 if (!force_update
&& !is_null_oid(&old_oid
)) {
1636 struct commit
*old_cmit
, *new_cmit
;
1639 old_cmit
= lookup_commit_reference_gently(the_repository
,
1641 new_cmit
= lookup_commit_reference_gently(the_repository
,
1643 if (!old_cmit
|| !new_cmit
)
1644 return error("Branch %s is missing commits.", b
->name
);
1646 ret
= repo_in_merge_bases(the_repository
, old_cmit
, new_cmit
);
1650 warning("Not updating %s"
1651 " (new tip %s does not contain %s)",
1652 b
->name
, oid_to_hex(&b
->oid
),
1653 oid_to_hex(&old_oid
));
1657 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
1660 ref_transaction_update(transaction
, b
->name
, &b
->oid
, &old_oid
,
1661 NULL
, NULL
, 0, msg
, &err
) ||
1662 ref_transaction_commit(transaction
, &err
)) {
1663 ref_transaction_free(transaction
);
1664 error("%s", err
.buf
);
1665 strbuf_release(&err
);
1668 ref_transaction_free(transaction
);
1669 strbuf_release(&err
);
1673 static void dump_branches(void)
1678 for (i
= 0; i
< branch_table_sz
; i
++) {
1679 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1680 failure
|= update_branch(b
);
1684 static void dump_tags(void)
1686 static const char *msg
= "fast-import";
1688 struct strbuf ref_name
= STRBUF_INIT
;
1689 struct strbuf err
= STRBUF_INIT
;
1690 struct ref_transaction
*transaction
;
1692 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
1695 failure
|= error("%s", err
.buf
);
1698 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1699 strbuf_reset(&ref_name
);
1700 strbuf_addf(&ref_name
, "refs/tags/%s", t
->name
);
1702 if (ref_transaction_update(transaction
, ref_name
.buf
,
1703 &t
->oid
, NULL
, NULL
, NULL
,
1705 failure
|= error("%s", err
.buf
);
1709 if (ref_transaction_commit(transaction
, &err
))
1710 failure
|= error("%s", err
.buf
);
1713 ref_transaction_free(transaction
);
1714 strbuf_release(&ref_name
);
1715 strbuf_release(&err
);
1718 static void dump_marks(void)
1720 struct lock_file mark_lock
= LOCK_INIT
;
1723 if (!export_marks_file
|| (import_marks_file
&& !import_marks_file_done
))
1726 if (safe_create_leading_directories_const(the_repository
, export_marks_file
)) {
1727 failure
|= error_errno("unable to create leading directories of %s",
1732 if (hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0) < 0) {
1733 failure
|= error_errno("Unable to write marks file %s",
1738 f
= fdopen_lock_file(&mark_lock
, "w");
1740 int saved_errno
= errno
;
1741 rollback_lock_file(&mark_lock
);
1742 failure
|= error("Unable to write marks file %s: %s",
1743 export_marks_file
, strerror(saved_errno
));
1747 for_each_mark(marks
, 0, dump_marks_fn
, f
);
1748 if (commit_lock_file(&mark_lock
)) {
1749 failure
|= error_errno("Unable to write file %s",
1755 static void insert_object_entry(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
)
1757 struct object_entry
*e
;
1758 e
= find_object(oid
);
1760 enum object_type type
= odb_read_object_info(the_repository
->objects
,
1763 die("object not found: %s", oid_to_hex(oid
));
1764 e
= insert_object(oid
);
1766 e
->pack_id
= MAX_PACK_ID
;
1767 e
->idx
.offset
= 1; /* just not zero! */
1769 insert_mark(s
, mark
, e
);
1772 static void insert_oid_entry(struct mark_set
**s
, struct object_id
*oid
, uintmax_t mark
)
1774 insert_mark(s
, mark
, xmemdupz(oid
, sizeof(*oid
)));
1777 static void read_mark_file(struct mark_set
**s
, FILE *f
, mark_set_inserter_t inserter
)
1780 while (fgets(line
, sizeof(line
), f
)) {
1783 struct object_id oid
;
1785 /* Ensure SHA-1 objects are padded with zeros. */
1786 memset(oid
.hash
, 0, sizeof(oid
.hash
));
1788 end
= strchr(line
, '\n');
1789 if (line
[0] != ':' || !end
)
1790 die("corrupt mark line: %s", line
);
1792 mark
= strtoumax(line
+ 1, &end
, 10);
1793 if (!mark
|| end
== line
+ 1
1795 || get_oid_hex_any(end
+ 1, &oid
) == GIT_HASH_UNKNOWN
)
1796 die("corrupt mark line: %s", line
);
1797 inserter(s
, &oid
, mark
);
1801 static void read_marks(void)
1803 FILE *f
= fopen(import_marks_file
, "r");
1806 else if (import_marks_file_ignore_missing
&& errno
== ENOENT
)
1807 goto done
; /* Marks file does not exist */
1809 die_errno("cannot read '%s'", import_marks_file
);
1810 read_mark_file(&marks
, f
, insert_object_entry
);
1813 import_marks_file_done
= 1;
1817 static int read_next_command(void)
1819 static int stdin_eof
= 0;
1822 unread_command_buf
= 0;
1827 if (unread_command_buf
) {
1828 unread_command_buf
= 0;
1830 struct recent_command
*rc
;
1832 stdin_eof
= strbuf_getline_lf(&command_buf
, stdin
);
1836 if (!seen_data_command
1837 && !starts_with(command_buf
.buf
, "feature ")
1838 && !starts_with(command_buf
.buf
, "option ")) {
1847 cmd_hist
.next
= rc
->next
;
1848 cmd_hist
.next
->prev
= &cmd_hist
;
1852 rc
->buf
= xstrdup(command_buf
.buf
);
1853 rc
->prev
= cmd_tail
;
1854 rc
->next
= cmd_hist
.prev
;
1855 rc
->prev
->next
= rc
;
1858 if (command_buf
.buf
[0] == '#')
1864 static void skip_optional_lf(void)
1866 int term_char
= fgetc(stdin
);
1867 if (term_char
!= '\n' && term_char
!= EOF
)
1868 ungetc(term_char
, stdin
);
1871 static void parse_mark(void)
1874 if (skip_prefix(command_buf
.buf
, "mark :", &v
)) {
1875 next_mark
= strtoumax(v
, NULL
, 10);
1876 read_next_command();
1882 static void parse_original_identifier(void)
1885 if (skip_prefix(command_buf
.buf
, "original-oid ", &v
))
1886 read_next_command();
1889 static int parse_data(struct strbuf
*sb
, uintmax_t limit
, uintmax_t *len_res
)
1894 if (!skip_prefix(command_buf
.buf
, "data ", &data
))
1895 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1897 if (skip_prefix(data
, "<<", &data
)) {
1898 char *term
= xstrdup(data
);
1899 size_t term_len
= command_buf
.len
- (data
- command_buf
.buf
);
1902 if (strbuf_getline_lf(&command_buf
, stdin
) == EOF
)
1903 die("EOF in data (terminator '%s' not found)", term
);
1904 if (term_len
== command_buf
.len
1905 && !strcmp(term
, command_buf
.buf
))
1907 strbuf_addbuf(sb
, &command_buf
);
1908 strbuf_addch(sb
, '\n');
1913 uintmax_t len
= strtoumax(data
, NULL
, 10);
1914 size_t n
= 0, length
= (size_t)len
;
1916 if (limit
&& limit
< len
) {
1921 die("data is too large to use in this context");
1923 while (n
< length
) {
1924 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1925 if (!s
&& feof(stdin
))
1926 die("EOF in data (%lu bytes remaining)",
1927 (unsigned long)(length
- n
));
1936 static int validate_raw_date(const char *src
, struct strbuf
*result
, int strict
)
1938 const char *orig_src
= src
;
1944 num
= strtoul(src
, &endp
, 10);
1946 * NEEDSWORK: perhaps check for reasonable values? For example, we
1947 * could error on values representing times more than a
1948 * day in the future.
1950 if (errno
|| endp
== src
|| *endp
!= ' ')
1954 if (*src
!= '-' && *src
!= '+')
1957 num
= strtoul(src
+ 1, &endp
, 10);
1959 * NEEDSWORK: check for brokenness other than num > 1400, such as
1960 * (num % 100) >= 60, or ((num % 100) % 15) != 0 ?
1962 if (errno
|| endp
== src
+ 1 || *endp
|| /* did not parse */
1963 (strict
&& (1400 < num
)) /* parsed a broken timezone */
1967 strbuf_addstr(result
, orig_src
);
1971 static char *parse_ident(const char *buf
)
1975 struct strbuf ident
= STRBUF_INIT
;
1977 /* ensure there is a space delimiter even if there is no name */
1981 ltgt
= buf
+ strcspn(buf
, "<>");
1983 die("Missing < in ident string: %s", buf
);
1984 if (ltgt
!= buf
&& ltgt
[-1] != ' ')
1985 die("Missing space before < in ident string: %s", buf
);
1986 ltgt
= ltgt
+ 1 + strcspn(ltgt
+ 1, "<>");
1988 die("Missing > in ident string: %s", buf
);
1991 die("Missing space after > in ident string: %s", buf
);
1993 name_len
= ltgt
- buf
;
1994 strbuf_add(&ident
, buf
, name_len
);
1998 if (validate_raw_date(ltgt
, &ident
, 1) < 0)
1999 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
2001 case WHENSPEC_RAW_PERMISSIVE
:
2002 if (validate_raw_date(ltgt
, &ident
, 0) < 0)
2003 die("Invalid raw date \"%s\" in ident: %s", ltgt
, buf
);
2005 case WHENSPEC_RFC2822
:
2006 if (parse_date(ltgt
, &ident
) < 0)
2007 die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt
, buf
);
2010 if (strcmp("now", ltgt
))
2011 die("Date in ident must be 'now': %s", buf
);
2016 return strbuf_detach(&ident
, NULL
);
2019 static void parse_and_store_blob(
2020 struct last_object
*last
,
2021 struct object_id
*oidout
,
2024 static struct strbuf buf
= STRBUF_INIT
;
2027 if (parse_data(&buf
, repo_settings_get_big_file_threshold(the_repository
), &len
))
2028 store_object(OBJ_BLOB
, &buf
, last
, oidout
, mark
);
2031 strbuf_release(&last
->data
);
2035 stream_blob(len
, oidout
, mark
);
2040 static void parse_new_blob(void)
2042 read_next_command();
2044 parse_original_identifier();
2045 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
2048 static void unload_one_branch(void)
2050 while (cur_active_branches
2051 && cur_active_branches
>= max_active_branches
) {
2052 uintmax_t min_commit
= ULONG_MAX
;
2053 struct branch
*e
, *l
= NULL
, *p
= NULL
;
2055 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
2056 if (e
->last_commit
< min_commit
) {
2058 min_commit
= e
->last_commit
;
2064 e
= p
->active_next_branch
;
2065 p
->active_next_branch
= e
->active_next_branch
;
2067 e
= active_branches
;
2068 active_branches
= e
->active_next_branch
;
2071 e
->active_next_branch
= NULL
;
2072 if (e
->branch_tree
.tree
) {
2073 release_tree_content_recursive(e
->branch_tree
.tree
);
2074 e
->branch_tree
.tree
= NULL
;
2076 cur_active_branches
--;
2080 static void load_branch(struct branch
*b
)
2082 load_tree(&b
->branch_tree
);
2085 b
->active_next_branch
= active_branches
;
2086 active_branches
= b
;
2087 cur_active_branches
++;
2088 branch_load_count
++;
2092 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2094 unsigned char fanout
= 0;
2095 while ((num_notes
>>= 8))
2100 static void construct_path_with_fanout(const char *hex_sha1
,
2101 unsigned char fanout
, char *path
)
2103 unsigned int i
= 0, j
= 0;
2104 if (fanout
>= the_hash_algo
->rawsz
)
2105 die("Too large fanout (%u)", fanout
);
2107 path
[i
++] = hex_sha1
[j
++];
2108 path
[i
++] = hex_sha1
[j
++];
2112 memcpy(path
+ i
, hex_sha1
+ j
, the_hash_algo
->hexsz
- j
);
2113 path
[i
+ the_hash_algo
->hexsz
- j
] = '\0';
2116 static uintmax_t do_change_note_fanout(
2117 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2118 char *hex_oid
, unsigned int hex_oid_len
,
2119 char *fullpath
, unsigned int fullpath_len
,
2120 unsigned char fanout
)
2122 struct tree_content
*t
;
2123 struct tree_entry
*e
, leaf
;
2124 unsigned int i
, tmp_hex_oid_len
, tmp_fullpath_len
;
2125 uintmax_t num_notes
= 0;
2126 struct object_id oid
;
2127 /* hex oid + '/' between each pair of hex digits + NUL */
2128 char realpath
[GIT_MAX_HEXSZ
+ ((GIT_MAX_HEXSZ
/ 2) - 1) + 1];
2129 const unsigned hexsz
= the_hash_algo
->hexsz
;
2135 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2137 tmp_hex_oid_len
= hex_oid_len
+ e
->name
->str_len
;
2138 tmp_fullpath_len
= fullpath_len
;
2141 * We're interested in EITHER existing note entries (entries
2142 * with exactly 40 hex chars in path, not including directory
2143 * separators), OR directory entries that may contain note
2144 * entries (with < 40 hex chars in path).
2145 * Also, each path component in a note entry must be a multiple
2148 if (!e
->versions
[1].mode
||
2149 tmp_hex_oid_len
> hexsz
||
2150 e
->name
->str_len
% 2)
2153 /* This _may_ be a note entry, or a subdir containing notes */
2154 memcpy(hex_oid
+ hex_oid_len
, e
->name
->str_dat
,
2156 if (tmp_fullpath_len
)
2157 fullpath
[tmp_fullpath_len
++] = '/';
2158 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2160 tmp_fullpath_len
+= e
->name
->str_len
;
2161 fullpath
[tmp_fullpath_len
] = '\0';
2163 if (tmp_hex_oid_len
== hexsz
&& !get_oid_hex(hex_oid
, &oid
)) {
2164 /* This is a note entry */
2165 if (fanout
== 0xff) {
2166 /* Counting mode, no rename */
2170 construct_path_with_fanout(hex_oid
, fanout
, realpath
);
2171 if (!strcmp(fullpath
, realpath
)) {
2172 /* Note entry is in correct location */
2177 /* Rename fullpath to realpath */
2178 if (!tree_content_remove(orig_root
, fullpath
, &leaf
, 0))
2179 die("Failed to remove path %s", fullpath
);
2180 tree_content_set(orig_root
, realpath
,
2181 &leaf
.versions
[1].oid
,
2182 leaf
.versions
[1].mode
,
2184 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2185 /* This is a subdir that may contain note entries */
2186 num_notes
+= do_change_note_fanout(orig_root
, e
,
2187 hex_oid
, tmp_hex_oid_len
,
2188 fullpath
, tmp_fullpath_len
, fanout
);
2191 /* The above may have reallocated the current tree_content */
2197 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2198 unsigned char fanout
)
2201 * The size of path is due to one slash between every two hex digits,
2202 * plus the terminating NUL. Note that there is no slash at the end, so
2203 * the number of slashes is one less than half the number of hex
2206 char hex_oid
[GIT_MAX_HEXSZ
], path
[GIT_MAX_HEXSZ
+ (GIT_MAX_HEXSZ
/ 2) - 1 + 1];
2207 return do_change_note_fanout(root
, root
, hex_oid
, 0, path
, 0, fanout
);
2210 static int parse_mapped_oid_hex(const char *hex
, struct object_id
*oid
, const char **end
)
2215 /* Make SHA-1 object IDs have all-zero padding. */
2216 memset(oid
->hash
, 0, sizeof(oid
->hash
));
2218 algo
= parse_oid_hex_any(hex
, oid
, end
);
2219 if (algo
== GIT_HASH_UNKNOWN
)
2222 it
= kh_get_oid_map(sub_oid_map
, *oid
);
2223 /* No such object? */
2224 if (it
== kh_end(sub_oid_map
)) {
2225 /* If we're using the same algorithm, pass it through. */
2226 if (hash_algos
[algo
].format_id
== the_hash_algo
->format_id
)
2230 oidcpy(oid
, kh_value(sub_oid_map
, it
));
2235 * Given a pointer into a string, parse a mark reference:
2237 * idnum ::= ':' bigint;
2239 * Update *endptr to point to the first character after the value.
2241 * Complain if the following character is not what is expected,
2242 * either a space or end of the string.
2244 static uintmax_t parse_mark_ref(const char *p
, char **endptr
)
2250 mark
= strtoumax(p
, endptr
, 10);
2252 die("No value after ':' in mark: %s", command_buf
.buf
);
2257 * Parse the mark reference, and complain if this is not the end of
2260 static uintmax_t parse_mark_ref_eol(const char *p
)
2265 mark
= parse_mark_ref(p
, &end
);
2267 die("Garbage after mark: %s", command_buf
.buf
);
2272 * Parse the mark reference, demanding a trailing space. Update *p to
2273 * point to the first character after the space.
2275 static uintmax_t parse_mark_ref_space(const char **p
)
2280 mark
= parse_mark_ref(*p
, &end
);
2282 die("Missing space after mark: %s", command_buf
.buf
);
2288 * Parse the path string into the strbuf. The path can either be quoted with
2289 * escape sequences or unquoted without escape sequences. Unquoted strings may
2290 * contain spaces only if `is_last_field` is nonzero; otherwise, it stops
2291 * parsing at the first space.
2293 static void parse_path(struct strbuf
*sb
, const char *p
, const char **endp
,
2294 int is_last_field
, const char *field
)
2297 if (unquote_c_style(sb
, p
, endp
))
2298 die("Invalid %s: %s", field
, command_buf
.buf
);
2299 if (strlen(sb
->buf
) != sb
->len
)
2300 die("NUL in %s: %s", field
, command_buf
.buf
);
2303 * Unless we are parsing the last field of a line,
2304 * SP is the end of this field.
2306 *endp
= is_last_field
2308 : strchrnul(p
, ' ');
2309 strbuf_add(sb
, p
, *endp
- p
);
2314 * Parse the path string into the strbuf, and complain if this is not the end of
2315 * the string. Unquoted strings may contain spaces.
2317 static void parse_path_eol(struct strbuf
*sb
, const char *p
, const char *field
)
2321 parse_path(sb
, p
, &end
, 1, field
);
2323 die("Garbage after %s: %s", field
, command_buf
.buf
);
2327 * Parse the path string into the strbuf, and ensure it is followed by a space.
2328 * Unquoted strings may not contain spaces. Update *endp to point to the first
2329 * character after the space.
2331 static void parse_path_space(struct strbuf
*sb
, const char *p
,
2332 const char **endp
, const char *field
)
2334 parse_path(sb
, p
, endp
, 0, field
);
2336 die("Missing space after %s: %s", field
, command_buf
.buf
);
2340 static void file_change_m(const char *p
, struct branch
*b
)
2342 static struct strbuf path
= STRBUF_INIT
;
2343 struct object_entry
*oe
;
2344 struct object_id oid
;
2345 uint16_t mode
, inline_data
= 0;
2347 p
= parse_mode(p
, &mode
);
2349 die("Corrupt mode: %s", command_buf
.buf
);
2354 case S_IFREG
| 0644:
2355 case S_IFREG
| 0755:
2362 die("Corrupt mode: %s", command_buf
.buf
);
2366 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2367 oidcpy(&oid
, &oe
->idx
.oid
);
2368 } else if (skip_prefix(p
, "inline ", &p
)) {
2370 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2372 if (parse_mapped_oid_hex(p
, &oid
, &p
))
2373 die("Invalid dataref: %s", command_buf
.buf
);
2374 oe
= find_object(&oid
);
2376 die("Missing space after SHA1: %s", command_buf
.buf
);
2379 strbuf_reset(&path
);
2380 parse_path_eol(&path
, p
, "path");
2382 /* Git does not track empty, non-toplevel directories. */
2383 if (S_ISDIR(mode
) &&
2384 is_empty_tree_oid(&oid
, the_repository
->hash_algo
) &&
2386 tree_content_remove(&b
->branch_tree
, path
.buf
, NULL
, 0);
2390 if (S_ISGITLINK(mode
)) {
2392 die("Git links cannot be specified 'inline': %s",
2395 if (oe
->type
!= OBJ_COMMIT
)
2396 die("Not a commit (actually a %s): %s",
2397 type_name(oe
->type
), command_buf
.buf
);
2400 * Accept the sha1 without checking; it expected to be in
2401 * another repository.
2403 } else if (inline_data
) {
2405 die("Directories cannot be specified 'inline': %s",
2407 while (read_next_command() != EOF
) {
2409 if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2412 parse_and_store_blob(&last_blob
, &oid
, 0);
2417 enum object_type expected
= S_ISDIR(mode
) ?
2419 enum object_type type
= oe
? oe
->type
:
2420 odb_read_object_info(the_repository
->objects
,
2423 die("%s not found: %s",
2424 S_ISDIR(mode
) ? "Tree" : "Blob",
2426 if (type
!= expected
)
2427 die("Not a %s (actually a %s): %s",
2428 type_name(expected
), type_name(type
),
2433 tree_content_replace(&b
->branch_tree
, &oid
, mode
, NULL
);
2437 if (!verify_path(path
.buf
, mode
))
2438 die("invalid path '%s'", path
.buf
);
2439 tree_content_set(&b
->branch_tree
, path
.buf
, &oid
, mode
, NULL
);
2442 static void file_change_d(const char *p
, struct branch
*b
)
2444 static struct strbuf path
= STRBUF_INIT
;
2446 strbuf_reset(&path
);
2447 parse_path_eol(&path
, p
, "path");
2448 tree_content_remove(&b
->branch_tree
, path
.buf
, NULL
, 1);
2451 static void file_change_cr(const char *p
, struct branch
*b
, int rename
)
2453 static struct strbuf source
= STRBUF_INIT
;
2454 static struct strbuf dest
= STRBUF_INIT
;
2455 struct tree_entry leaf
;
2457 strbuf_reset(&source
);
2458 parse_path_space(&source
, p
, &p
, "source");
2459 strbuf_reset(&dest
);
2460 parse_path_eol(&dest
, p
, "dest");
2462 memset(&leaf
, 0, sizeof(leaf
));
2464 tree_content_remove(&b
->branch_tree
, source
.buf
, &leaf
, 1);
2466 tree_content_get(&b
->branch_tree
, source
.buf
, &leaf
, 1);
2467 if (!leaf
.versions
[1].mode
)
2468 die("Path %s not in branch", source
.buf
);
2469 if (!*dest
.buf
) { /* C "path/to/subdir" "" */
2470 tree_content_replace(&b
->branch_tree
,
2471 &leaf
.versions
[1].oid
,
2472 leaf
.versions
[1].mode
,
2476 if (!verify_path(dest
.buf
, leaf
.versions
[1].mode
))
2477 die("invalid path '%s'", dest
.buf
);
2478 tree_content_set(&b
->branch_tree
, dest
.buf
,
2479 &leaf
.versions
[1].oid
,
2480 leaf
.versions
[1].mode
,
2484 static void note_change_n(const char *p
, struct branch
*b
, unsigned char *old_fanout
)
2486 struct object_entry
*oe
;
2488 struct object_id oid
, commit_oid
;
2489 char path
[GIT_MAX_RAWSZ
* 3];
2490 uint16_t inline_data
= 0;
2491 unsigned char new_fanout
;
2494 * When loading a branch, we don't traverse its tree to count the real
2495 * number of notes (too expensive to do this for all non-note refs).
2496 * This means that recently loaded notes refs might incorrectly have
2497 * b->num_notes == 0, and consequently, old_fanout might be wrong.
2499 * Fix this by traversing the tree and counting the number of notes
2500 * when b->num_notes == 0. If the notes tree is truly empty, the
2501 * calculation should not take long.
2503 if (b
->num_notes
== 0 && *old_fanout
== 0) {
2504 /* Invoke change_note_fanout() in "counting mode". */
2505 b
->num_notes
= change_note_fanout(&b
->branch_tree
, 0xff);
2506 *old_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2509 /* Now parse the notemodify command. */
2510 /* <dataref> or 'inline' */
2512 oe
= find_mark(marks
, parse_mark_ref_space(&p
));
2513 oidcpy(&oid
, &oe
->idx
.oid
);
2514 } else if (skip_prefix(p
, "inline ", &p
)) {
2516 oe
= NULL
; /* not used with inline_data, but makes gcc happy */
2518 if (parse_mapped_oid_hex(p
, &oid
, &p
))
2519 die("Invalid dataref: %s", command_buf
.buf
);
2520 oe
= find_object(&oid
);
2522 die("Missing space after SHA1: %s", command_buf
.buf
);
2526 s
= lookup_branch(p
);
2528 if (is_null_oid(&s
->oid
))
2529 die("Can't add a note on empty branch.");
2530 oidcpy(&commit_oid
, &s
->oid
);
2531 } else if (*p
== ':') {
2532 uintmax_t commit_mark
= parse_mark_ref_eol(p
);
2533 struct object_entry
*commit_oe
= find_mark(marks
, commit_mark
);
2534 if (commit_oe
->type
!= OBJ_COMMIT
)
2535 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2536 oidcpy(&commit_oid
, &commit_oe
->idx
.oid
);
2537 } else if (!repo_get_oid(the_repository
, p
, &commit_oid
)) {
2539 char *buf
= odb_read_object_peeled(the_repository
->objects
,
2540 &commit_oid
, OBJ_COMMIT
, &size
,
2542 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2543 die("Not a valid commit: %s", p
);
2546 die("Invalid ref name or SHA1 expression: %s", p
);
2549 read_next_command();
2550 parse_and_store_blob(&last_blob
, &oid
, 0);
2552 if (oe
->type
!= OBJ_BLOB
)
2553 die("Not a blob (actually a %s): %s",
2554 type_name(oe
->type
), command_buf
.buf
);
2555 } else if (!is_null_oid(&oid
)) {
2556 enum object_type type
= odb_read_object_info(the_repository
->objects
, &oid
,
2559 die("Blob not found: %s", command_buf
.buf
);
2560 if (type
!= OBJ_BLOB
)
2561 die("Not a blob (actually a %s): %s",
2562 type_name(type
), command_buf
.buf
);
2565 construct_path_with_fanout(oid_to_hex(&commit_oid
), *old_fanout
, path
);
2566 if (tree_content_remove(&b
->branch_tree
, path
, NULL
, 0))
2569 if (is_null_oid(&oid
))
2570 return; /* nothing to insert */
2573 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2574 construct_path_with_fanout(oid_to_hex(&commit_oid
), new_fanout
, path
);
2575 tree_content_set(&b
->branch_tree
, path
, &oid
, S_IFREG
| 0644, NULL
);
2578 static void file_change_deleteall(struct branch
*b
)
2580 release_tree_content_recursive(b
->branch_tree
.tree
);
2581 oidclr(&b
->branch_tree
.versions
[0].oid
, the_repository
->hash_algo
);
2582 oidclr(&b
->branch_tree
.versions
[1].oid
, the_repository
->hash_algo
);
2583 load_tree(&b
->branch_tree
);
2587 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2589 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2590 die("Not a valid commit: %s", oid_to_hex(&b
->oid
));
2591 if (memcmp("tree ", buf
, 5)
2592 || get_oid_hex(buf
+ 5, &b
->branch_tree
.versions
[1].oid
))
2593 die("The commit %s is corrupt", oid_to_hex(&b
->oid
));
2594 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2595 &b
->branch_tree
.versions
[1].oid
);
2598 static void parse_from_existing(struct branch
*b
)
2600 if (is_null_oid(&b
->oid
)) {
2601 oidclr(&b
->branch_tree
.versions
[0].oid
, the_repository
->hash_algo
);
2602 oidclr(&b
->branch_tree
.versions
[1].oid
, the_repository
->hash_algo
);
2607 buf
= odb_read_object_peeled(the_repository
->objects
, &b
->oid
,
2608 OBJ_COMMIT
, &size
, &b
->oid
);
2609 parse_from_commit(b
, buf
, size
);
2614 static int parse_objectish(struct branch
*b
, const char *objectish
)
2617 struct object_id oid
;
2619 oidcpy(&oid
, &b
->branch_tree
.versions
[1].oid
);
2621 s
= lookup_branch(objectish
);
2623 die("Can't create a branch from itself: %s", b
->name
);
2625 struct object_id
*t
= &s
->branch_tree
.versions
[1].oid
;
2626 oidcpy(&b
->oid
, &s
->oid
);
2627 oidcpy(&b
->branch_tree
.versions
[0].oid
, t
);
2628 oidcpy(&b
->branch_tree
.versions
[1].oid
, t
);
2629 } else if (*objectish
== ':') {
2630 uintmax_t idnum
= parse_mark_ref_eol(objectish
);
2631 struct object_entry
*oe
= find_mark(marks
, idnum
);
2632 if (oe
->type
!= OBJ_COMMIT
)
2633 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2634 if (!oideq(&b
->oid
, &oe
->idx
.oid
)) {
2635 oidcpy(&b
->oid
, &oe
->idx
.oid
);
2636 if (oe
->pack_id
!= MAX_PACK_ID
) {
2638 char *buf
= gfi_unpack_entry(oe
, &size
);
2639 parse_from_commit(b
, buf
, size
);
2642 parse_from_existing(b
);
2644 } else if (!repo_get_oid(the_repository
, objectish
, &b
->oid
)) {
2645 parse_from_existing(b
);
2646 if (is_null_oid(&b
->oid
))
2650 die("Invalid ref name or SHA1 expression: %s", objectish
);
2652 if (b
->branch_tree
.tree
&& !oideq(&oid
, &b
->branch_tree
.versions
[1].oid
)) {
2653 release_tree_content_recursive(b
->branch_tree
.tree
);
2654 b
->branch_tree
.tree
= NULL
;
2657 read_next_command();
2661 static int parse_from(struct branch
*b
)
2665 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2668 return parse_objectish(b
, from
);
2671 static int parse_objectish_with_prefix(struct branch
*b
, const char *prefix
)
2675 if (!skip_prefix(command_buf
.buf
, prefix
, &base
))
2678 return parse_objectish(b
, base
);
2681 static struct hash_list
*parse_merge(unsigned int *count
)
2683 struct hash_list
*list
= NULL
, **tail
= &list
, *n
;
2688 while (skip_prefix(command_buf
.buf
, "merge ", &from
)) {
2689 n
= xmalloc(sizeof(*n
));
2690 s
= lookup_branch(from
);
2692 oidcpy(&n
->oid
, &s
->oid
);
2693 else if (*from
== ':') {
2694 uintmax_t idnum
= parse_mark_ref_eol(from
);
2695 struct object_entry
*oe
= find_mark(marks
, idnum
);
2696 if (oe
->type
!= OBJ_COMMIT
)
2697 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2698 oidcpy(&n
->oid
, &oe
->idx
.oid
);
2699 } else if (!repo_get_oid(the_repository
, from
, &n
->oid
)) {
2701 char *buf
= odb_read_object_peeled(the_repository
->objects
,
2702 &n
->oid
, OBJ_COMMIT
,
2704 if (!buf
|| size
< the_hash_algo
->hexsz
+ 6)
2705 die("Not a valid commit: %s", from
);
2708 die("Invalid ref name or SHA1 expression: %s", from
);
2715 read_next_command();
2720 struct signature_data
{
2721 char *hash_algo
; /* "sha1" or "sha256" */
2722 char *sig_format
; /* "openpgp", "x509", "ssh", or "unknown" */
2723 struct strbuf data
; /* The actual signature data */
2726 static void parse_one_signature(struct signature_data
*sig
, const char *v
)
2728 char *args
= xstrdup(v
); /* Will be freed when sig->hash_algo is freed */
2729 char *space
= strchr(args
, ' ');
2732 die("Expected gpgsig format: 'gpgsig <hash-algo> <signature-format>', "
2733 "got 'gpgsig %s'", args
);
2736 sig
->hash_algo
= args
;
2737 sig
->sig_format
= space
+ 1;
2739 /* Validate hash algorithm */
2740 if (strcmp(sig
->hash_algo
, "sha1") &&
2741 strcmp(sig
->hash_algo
, "sha256"))
2742 die("Unknown git hash algorithm in gpgsig: '%s'", sig
->hash_algo
);
2744 /* Validate signature format */
2745 if (!valid_signature_format(sig
->sig_format
))
2746 die("Invalid signature format in gpgsig: '%s'", sig
->sig_format
);
2747 if (!strcmp(sig
->sig_format
, "unknown"))
2748 warning("'unknown' signature format in gpgsig");
2750 /* Read signature data */
2751 read_next_command();
2752 parse_data(&sig
->data
, 0, NULL
);
2755 static void add_gpgsig_to_commit(struct strbuf
*commit_data
,
2757 struct signature_data
*sig
)
2759 struct string_list siglines
= STRING_LIST_INIT_NODUP
;
2761 if (!sig
->hash_algo
)
2764 strbuf_addstr(commit_data
, header
);
2765 string_list_split_in_place(&siglines
, sig
->data
.buf
, "\n", -1);
2766 strbuf_add_separated_string_list(commit_data
, "\n ", &siglines
);
2767 strbuf_addch(commit_data
, '\n');
2768 string_list_clear(&siglines
, 1);
2769 strbuf_release(&sig
->data
);
2770 free(sig
->hash_algo
);
2773 static void store_signature(struct signature_data
*stored_sig
,
2774 struct signature_data
*new_sig
,
2775 const char *hash_type
)
2777 if (stored_sig
->hash_algo
) {
2778 warning("multiple %s signatures found, "
2779 "ignoring additional signature",
2781 strbuf_release(&new_sig
->data
);
2782 free(new_sig
->hash_algo
);
2784 *stored_sig
= *new_sig
;
2788 static void parse_new_commit(const char *arg
)
2790 static struct strbuf msg
= STRBUF_INIT
;
2791 struct signature_data sig_sha1
= { NULL
, NULL
, STRBUF_INIT
};
2792 struct signature_data sig_sha256
= { NULL
, NULL
, STRBUF_INIT
};
2794 char *author
= NULL
;
2795 char *committer
= NULL
;
2796 char *encoding
= NULL
;
2797 struct hash_list
*merge_list
= NULL
;
2798 unsigned int merge_count
;
2799 unsigned char prev_fanout
, new_fanout
;
2802 b
= lookup_branch(arg
);
2804 b
= new_branch(arg
);
2806 read_next_command();
2808 parse_original_identifier();
2809 if (skip_prefix(command_buf
.buf
, "author ", &v
)) {
2810 author
= parse_ident(v
);
2811 read_next_command();
2813 if (skip_prefix(command_buf
.buf
, "committer ", &v
)) {
2814 committer
= parse_ident(v
);
2815 read_next_command();
2818 die("Expected committer but didn't get one");
2820 /* Process signatures (up to 2: one "sha1" and one "sha256") */
2821 while (skip_prefix(command_buf
.buf
, "gpgsig ", &v
)) {
2822 struct signature_data sig
= { NULL
, NULL
, STRBUF_INIT
};
2824 parse_one_signature(&sig
, v
);
2826 if (!strcmp(sig
.hash_algo
, "sha1"))
2827 store_signature(&sig_sha1
, &sig
, "SHA-1");
2828 else if (!strcmp(sig
.hash_algo
, "sha256"))
2829 store_signature(&sig_sha256
, &sig
, "SHA-256");
2831 BUG("parse_one_signature() returned unknown hash algo");
2833 read_next_command();
2836 if (skip_prefix(command_buf
.buf
, "encoding ", &v
)) {
2837 encoding
= xstrdup(v
);
2838 read_next_command();
2840 parse_data(&msg
, 0, NULL
);
2841 read_next_command();
2843 merge_list
= parse_merge(&merge_count
);
2845 /* ensure the branch is active/loaded */
2846 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2847 unload_one_branch();
2851 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2854 while (command_buf
.len
> 0) {
2855 if (skip_prefix(command_buf
.buf
, "M ", &v
))
2856 file_change_m(v
, b
);
2857 else if (skip_prefix(command_buf
.buf
, "D ", &v
))
2858 file_change_d(v
, b
);
2859 else if (skip_prefix(command_buf
.buf
, "R ", &v
))
2860 file_change_cr(v
, b
, 1);
2861 else if (skip_prefix(command_buf
.buf
, "C ", &v
))
2862 file_change_cr(v
, b
, 0);
2863 else if (skip_prefix(command_buf
.buf
, "N ", &v
))
2864 note_change_n(v
, b
, &prev_fanout
);
2865 else if (!strcmp("deleteall", command_buf
.buf
))
2866 file_change_deleteall(b
);
2867 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
2869 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
2872 unread_command_buf
= 1;
2875 if (read_next_command() == EOF
)
2879 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2880 if (new_fanout
!= prev_fanout
)
2881 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2883 /* build the tree and the commit */
2884 store_tree(&b
->branch_tree
);
2885 oidcpy(&b
->branch_tree
.versions
[0].oid
,
2886 &b
->branch_tree
.versions
[1].oid
);
2888 strbuf_reset(&new_data
);
2889 strbuf_addf(&new_data
, "tree %s\n",
2890 oid_to_hex(&b
->branch_tree
.versions
[1].oid
));
2891 if (!is_null_oid(&b
->oid
))
2892 strbuf_addf(&new_data
, "parent %s\n",
2893 oid_to_hex(&b
->oid
));
2894 while (merge_list
) {
2895 struct hash_list
*next
= merge_list
->next
;
2896 strbuf_addf(&new_data
, "parent %s\n",
2897 oid_to_hex(&merge_list
->oid
));
2901 strbuf_addf(&new_data
,
2904 author
? author
: committer
, committer
);
2906 strbuf_addf(&new_data
,
2910 add_gpgsig_to_commit(&new_data
, "gpgsig ", &sig_sha1
);
2911 add_gpgsig_to_commit(&new_data
, "gpgsig-sha256 ", &sig_sha256
);
2913 strbuf_addch(&new_data
, '\n');
2914 strbuf_addbuf(&new_data
, &msg
);
2919 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, &b
->oid
, next_mark
))
2920 b
->pack_id
= pack_id
;
2921 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2924 static void parse_new_tag(const char *arg
)
2926 static struct strbuf msg
= STRBUF_INIT
;
2931 uintmax_t from_mark
= 0;
2932 struct object_id oid
;
2933 enum object_type type
;
2936 t
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct tag
));
2937 t
->name
= mem_pool_strdup(&fi_mem_pool
, arg
);
2939 last_tag
->next_tag
= t
;
2943 read_next_command();
2947 if (!skip_prefix(command_buf
.buf
, "from ", &from
))
2948 die("Expected from command, got %s", command_buf
.buf
);
2949 s
= lookup_branch(from
);
2951 if (is_null_oid(&s
->oid
))
2952 die("Can't tag an empty branch.");
2953 oidcpy(&oid
, &s
->oid
);
2955 } else if (*from
== ':') {
2956 struct object_entry
*oe
;
2957 from_mark
= parse_mark_ref_eol(from
);
2958 oe
= find_mark(marks
, from_mark
);
2960 oidcpy(&oid
, &oe
->idx
.oid
);
2961 } else if (!repo_get_oid(the_repository
, from
, &oid
)) {
2962 struct object_entry
*oe
= find_object(&oid
);
2964 type
= odb_read_object_info(the_repository
->objects
,
2967 die("Not a valid object: %s", from
);
2971 die("Invalid ref name or SHA1 expression: %s", from
);
2972 read_next_command();
2974 /* original-oid ... */
2975 parse_original_identifier();
2978 if (skip_prefix(command_buf
.buf
, "tagger ", &v
)) {
2979 tagger
= parse_ident(v
);
2980 read_next_command();
2984 /* tag payload/message */
2985 parse_data(&msg
, 0, NULL
);
2987 /* build the tag object */
2988 strbuf_reset(&new_data
);
2990 strbuf_addf(&new_data
,
2994 oid_to_hex(&oid
), type_name(type
), t
->name
);
2996 strbuf_addf(&new_data
,
2997 "tagger %s\n", tagger
);
2998 strbuf_addch(&new_data
, '\n');
2999 strbuf_addbuf(&new_data
, &msg
);
3002 if (store_object(OBJ_TAG
, &new_data
, NULL
, &t
->oid
, next_mark
))
3003 t
->pack_id
= MAX_PACK_ID
;
3005 t
->pack_id
= pack_id
;
3008 static void parse_reset_branch(const char *arg
)
3011 const char *tag_name
;
3013 b
= lookup_branch(arg
);
3015 oidclr(&b
->oid
, the_repository
->hash_algo
);
3016 oidclr(&b
->branch_tree
.versions
[0].oid
, the_repository
->hash_algo
);
3017 oidclr(&b
->branch_tree
.versions
[1].oid
, the_repository
->hash_algo
);
3018 if (b
->branch_tree
.tree
) {
3019 release_tree_content_recursive(b
->branch_tree
.tree
);
3020 b
->branch_tree
.tree
= NULL
;
3024 b
= new_branch(arg
);
3025 read_next_command();
3027 if (b
->delete && skip_prefix(b
->name
, "refs/tags/", &tag_name
)) {
3029 * Elsewhere, we call dump_branches() before dump_tags(),
3030 * and dump_branches() will handle ref deletions first, so
3031 * in order to make sure the deletion actually takes effect,
3032 * we need to remove the tag from our list of tags to update.
3034 * NEEDSWORK: replace list of tags with hashmap for faster
3037 struct tag
*t
, *prev
= NULL
;
3038 for (t
= first_tag
; t
; t
= t
->next_tag
) {
3039 if (!strcmp(t
->name
, tag_name
))
3045 prev
->next_tag
= t
->next_tag
;
3047 first_tag
= t
->next_tag
;
3050 /* There is no mem_pool_free(t) function to call. */
3053 if (command_buf
.len
> 0)
3054 unread_command_buf
= 1;
3057 static void cat_blob_write(const char *buf
, unsigned long size
)
3059 if (write_in_full(cat_blob_fd
, buf
, size
) < 0)
3060 die_errno("Write to frontend failed");
3063 static void cat_blob(struct object_entry
*oe
, struct object_id
*oid
)
3065 struct strbuf line
= STRBUF_INIT
;
3067 enum object_type type
= 0;
3070 if (!oe
|| oe
->pack_id
== MAX_PACK_ID
) {
3071 buf
= odb_read_object(the_repository
->objects
, oid
, &type
, &size
);
3074 buf
= gfi_unpack_entry(oe
, &size
);
3078 * Output based on batch_one_object() from cat-file.c.
3081 strbuf_reset(&line
);
3082 strbuf_addf(&line
, "%s missing\n", oid_to_hex(oid
));
3083 cat_blob_write(line
.buf
, line
.len
);
3084 strbuf_release(&line
);
3089 die("Can't read object %s", oid_to_hex(oid
));
3090 if (type
!= OBJ_BLOB
)
3091 die("Object %s is a %s but a blob was expected.",
3092 oid_to_hex(oid
), type_name(type
));
3093 strbuf_reset(&line
);
3094 strbuf_addf(&line
, "%s %s %"PRIuMAX
"\n", oid_to_hex(oid
),
3095 type_name(type
), (uintmax_t)size
);
3096 cat_blob_write(line
.buf
, line
.len
);
3097 strbuf_release(&line
);
3098 cat_blob_write(buf
, size
);
3099 cat_blob_write("\n", 1);
3100 if (oe
&& oe
->pack_id
== pack_id
) {
3101 last_blob
.offset
= oe
->idx
.offset
;
3102 strbuf_attach(&last_blob
.data
, buf
, size
, size
);
3103 last_blob
.depth
= oe
->depth
;
3108 static void parse_get_mark(const char *p
)
3110 struct object_entry
*oe
;
3111 char output
[GIT_MAX_HEXSZ
+ 2];
3113 /* get-mark SP <object> LF */
3115 die("Not a mark: %s", p
);
3117 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
3119 die("Unknown mark: %s", command_buf
.buf
);
3121 xsnprintf(output
, sizeof(output
), "%s\n", oid_to_hex(&oe
->idx
.oid
));
3122 cat_blob_write(output
, the_hash_algo
->hexsz
+ 1);
3125 static void parse_cat_blob(const char *p
)
3127 struct object_entry
*oe
;
3128 struct object_id oid
;
3130 /* cat-blob SP <object> LF */
3132 oe
= find_mark(marks
, parse_mark_ref_eol(p
));
3134 die("Unknown mark: %s", command_buf
.buf
);
3135 oidcpy(&oid
, &oe
->idx
.oid
);
3137 if (parse_mapped_oid_hex(p
, &oid
, &p
))
3138 die("Invalid dataref: %s", command_buf
.buf
);
3140 die("Garbage after SHA1: %s", command_buf
.buf
);
3141 oe
= find_object(&oid
);
3147 static struct object_entry
*dereference(struct object_entry
*oe
,
3148 struct object_id
*oid
)
3152 const unsigned hexsz
= the_hash_algo
->hexsz
;
3155 enum object_type type
= odb_read_object_info(the_repository
->objects
,
3158 die("object not found: %s", oid_to_hex(oid
));
3160 oe
= insert_object(oid
);
3162 oe
->pack_id
= MAX_PACK_ID
;
3166 case OBJ_TREE
: /* easy case. */
3172 die("Not a tree-ish: %s", command_buf
.buf
);
3175 if (oe
->pack_id
!= MAX_PACK_ID
) { /* in a pack being written */
3176 buf
= gfi_unpack_entry(oe
, &size
);
3178 enum object_type unused
;
3179 buf
= odb_read_object(the_repository
->objects
, oid
,
3183 die("Can't load object %s", oid_to_hex(oid
));
3185 /* Peel one layer. */
3188 if (size
< hexsz
+ strlen("object ") ||
3189 get_oid_hex(buf
+ strlen("object "), oid
))
3190 die("Invalid SHA1 in tag: %s", command_buf
.buf
);
3193 if (size
< hexsz
+ strlen("tree ") ||
3194 get_oid_hex(buf
+ strlen("tree "), oid
))
3195 die("Invalid SHA1 in commit: %s", command_buf
.buf
);
3199 return find_object(oid
);
3202 static void insert_mapped_mark(uintmax_t mark
, void *object
, void *cbp
)
3204 struct object_id
*fromoid
= object
;
3205 struct object_id
*tooid
= find_mark(cbp
, mark
);
3209 it
= kh_put_oid_map(sub_oid_map
, *fromoid
, &ret
);
3210 /* We've already seen this object. */
3213 kh_value(sub_oid_map
, it
) = tooid
;
3216 static void build_mark_map_one(struct mark_set
*from
, struct mark_set
*to
)
3218 for_each_mark(from
, 0, insert_mapped_mark
, to
);
3221 static void build_mark_map(struct string_list
*from
, struct string_list
*to
)
3223 struct string_list_item
*fromp
, *top
;
3225 sub_oid_map
= kh_init_oid_map();
3227 for_each_string_list_item(fromp
, from
) {
3228 top
= string_list_lookup(to
, fromp
->string
);
3230 die(_("Missing from marks for submodule '%s'"), fromp
->string
);
3231 } else if (!top
|| !top
->util
) {
3232 die(_("Missing to marks for submodule '%s'"), fromp
->string
);
3234 build_mark_map_one(fromp
->util
, top
->util
);
3238 static struct object_entry
*parse_treeish_dataref(const char **p
)
3240 struct object_id oid
;
3241 struct object_entry
*e
;
3243 if (**p
== ':') { /* <mark> */
3244 e
= find_mark(marks
, parse_mark_ref_space(p
));
3246 die("Unknown mark: %s", command_buf
.buf
);
3247 oidcpy(&oid
, &e
->idx
.oid
);
3248 } else { /* <sha1> */
3249 if (parse_mapped_oid_hex(*p
, &oid
, p
))
3250 die("Invalid dataref: %s", command_buf
.buf
);
3251 e
= find_object(&oid
);
3253 die("Missing space after tree-ish: %s", command_buf
.buf
);
3256 while (!e
|| e
->type
!= OBJ_TREE
)
3257 e
= dereference(e
, &oid
);
3261 static void print_ls(int mode
, const unsigned char *hash
, const char *path
)
3263 static struct strbuf line
= STRBUF_INIT
;
3265 /* See show_tree(). */
3267 S_ISGITLINK(mode
) ? commit_type
:
3268 S_ISDIR(mode
) ? tree_type
:
3272 /* missing SP path LF */
3273 strbuf_reset(&line
);
3274 strbuf_addstr(&line
, "missing ");
3275 quote_c_style(path
, &line
, NULL
, 0);
3276 strbuf_addch(&line
, '\n');
3278 /* mode SP type SP object_name TAB path LF */
3279 strbuf_reset(&line
);
3280 strbuf_addf(&line
, "%06o %s %s\t",
3281 mode
& ~NO_DELTA
, type
, hash_to_hex(hash
));
3282 quote_c_style(path
, &line
, NULL
, 0);
3283 strbuf_addch(&line
, '\n');
3285 cat_blob_write(line
.buf
, line
.len
);
3288 static void parse_ls(const char *p
, struct branch
*b
)
3290 static struct strbuf path
= STRBUF_INIT
;
3291 struct tree_entry
*root
= NULL
;
3292 struct tree_entry leaf
= {NULL
};
3294 /* ls SP (<tree-ish> SP)? <path> */
3297 die("Not in a commit: %s", command_buf
.buf
);
3298 root
= &b
->branch_tree
;
3300 struct object_entry
*e
= parse_treeish_dataref(&p
);
3301 root
= new_tree_entry();
3302 oidcpy(&root
->versions
[1].oid
, &e
->idx
.oid
);
3303 if (!is_null_oid(&root
->versions
[1].oid
))
3304 root
->versions
[1].mode
= S_IFDIR
;
3307 strbuf_reset(&path
);
3308 parse_path_eol(&path
, p
, "path");
3309 tree_content_get(root
, path
.buf
, &leaf
, 1);
3311 * A directory in preparation would have a sha1 of zero
3312 * until it is saved. Save, for simplicity.
3314 if (S_ISDIR(leaf
.versions
[1].mode
))
3317 print_ls(leaf
.versions
[1].mode
, leaf
.versions
[1].oid
.hash
, path
.buf
);
3319 release_tree_content_recursive(leaf
.tree
);
3320 if (!b
|| root
!= &b
->branch_tree
)
3321 release_tree_entry(root
);
3324 static void checkpoint(void)
3326 checkpoint_requested
= 0;
3335 static void parse_checkpoint(void)
3337 checkpoint_requested
= 1;
3341 static void parse_progress(void)
3343 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
3344 fputc('\n', stdout
);
3349 static void parse_alias(void)
3351 struct object_entry
*e
;
3355 read_next_command();
3360 die(_("Expected 'mark' command, got %s"), command_buf
.buf
);
3363 memset(&b
, 0, sizeof(b
));
3364 if (!parse_objectish_with_prefix(&b
, "to "))
3365 die(_("Expected 'to' command, got %s"), command_buf
.buf
);
3366 e
= find_object(&b
.oid
);
3368 insert_mark(&marks
, next_mark
, e
);
3371 static char* make_fast_import_path(const char *path
)
3373 if (!relative_marks_paths
|| is_absolute_path(path
))
3374 return prefix_filename(global_prefix
, path
);
3375 return repo_git_path(the_repository
, "info/fast-import/%s", path
);
3378 static void option_import_marks(const char *marks
,
3379 int from_stream
, int ignore_missing
)
3381 if (import_marks_file
) {
3383 die("Only one import-marks command allowed per stream");
3385 /* read previous mark file */
3386 if(!import_marks_file_from_stream
)
3390 free(import_marks_file
);
3391 import_marks_file
= make_fast_import_path(marks
);
3392 import_marks_file_from_stream
= from_stream
;
3393 import_marks_file_ignore_missing
= ignore_missing
;
3396 static void option_date_format(const char *fmt
)
3398 if (!strcmp(fmt
, "raw"))
3399 whenspec
= WHENSPEC_RAW
;
3400 else if (!strcmp(fmt
, "raw-permissive"))
3401 whenspec
= WHENSPEC_RAW_PERMISSIVE
;
3402 else if (!strcmp(fmt
, "rfc2822"))
3403 whenspec
= WHENSPEC_RFC2822
;
3404 else if (!strcmp(fmt
, "now"))
3405 whenspec
= WHENSPEC_NOW
;
3407 die("unknown --date-format argument %s", fmt
);
3410 static unsigned long ulong_arg(const char *option
, const char *arg
)
3413 unsigned long rv
= strtoul(arg
, &endptr
, 0);
3414 if (strchr(arg
, '-') || endptr
== arg
|| *endptr
)
3415 die("%s: argument must be a non-negative integer", option
);
3419 static void option_depth(const char *depth
)
3421 max_depth
= ulong_arg("--depth", depth
);
3422 if (max_depth
> MAX_DEPTH
)
3423 die("--depth cannot exceed %u", MAX_DEPTH
);
3426 static void option_active_branches(const char *branches
)
3428 max_active_branches
= ulong_arg("--active-branches", branches
);
3431 static void option_export_marks(const char *marks
)
3433 free(export_marks_file
);
3434 export_marks_file
= make_fast_import_path(marks
);
3437 static void option_cat_blob_fd(const char *fd
)
3439 unsigned long n
= ulong_arg("--cat-blob-fd", fd
);
3440 if (n
> (unsigned long) INT_MAX
)
3441 die("--cat-blob-fd cannot exceed %d", INT_MAX
);
3442 cat_blob_fd
= (int) n
;
3445 static void option_export_pack_edges(const char *edges
)
3447 char *fn
= prefix_filename(global_prefix
, edges
);
3450 pack_edges
= xfopen(fn
, "a");
3454 static void option_rewrite_submodules(const char *arg
, struct string_list
*list
)
3456 struct mark_set
*ms
;
3458 char *s
= xstrdup(arg
);
3459 char *f
= strchr(s
, ':');
3461 die(_("Expected format name:filename for submodule rewrite option"));
3464 CALLOC_ARRAY(ms
, 1);
3466 f
= prefix_filename(global_prefix
, f
);
3469 die_errno("cannot read '%s'", f
);
3470 read_mark_file(&ms
, fp
, insert_oid_entry
);
3474 string_list_insert(list
, s
)->util
= ms
;
3479 static int parse_one_option(const char *option
)
3481 if (skip_prefix(option
, "max-pack-size=", &option
)) {
3483 if (!git_parse_ulong(option
, &v
))
3486 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v
);
3488 } else if (v
< 1024 * 1024) {
3489 warning("minimum max-pack-size is 1 MiB");
3493 } else if (skip_prefix(option
, "big-file-threshold=", &option
)) {
3495 if (!git_parse_ulong(option
, &v
))
3497 repo_settings_set_big_file_threshold(the_repository
, v
);
3498 } else if (skip_prefix(option
, "depth=", &option
)) {
3499 option_depth(option
);
3500 } else if (skip_prefix(option
, "active-branches=", &option
)) {
3501 option_active_branches(option
);
3502 } else if (skip_prefix(option
, "export-pack-edges=", &option
)) {
3503 option_export_pack_edges(option
);
3504 } else if (!strcmp(option
, "quiet")) {
3507 } else if (!strcmp(option
, "stats")) {
3509 } else if (!strcmp(option
, "allow-unsafe-features")) {
3510 ; /* already handled during early option parsing */
3518 static void check_unsafe_feature(const char *feature
, int from_stream
)
3520 if (from_stream
&& !allow_unsafe_features
)
3521 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3525 static int parse_one_feature(const char *feature
, int from_stream
)
3529 if (skip_prefix(feature
, "date-format=", &arg
)) {
3530 option_date_format(arg
);
3531 } else if (skip_prefix(feature
, "import-marks=", &arg
)) {
3532 check_unsafe_feature("import-marks", from_stream
);
3533 option_import_marks(arg
, from_stream
, 0);
3534 } else if (skip_prefix(feature
, "import-marks-if-exists=", &arg
)) {
3535 check_unsafe_feature("import-marks-if-exists", from_stream
);
3536 option_import_marks(arg
, from_stream
, 1);
3537 } else if (skip_prefix(feature
, "export-marks=", &arg
)) {
3538 check_unsafe_feature(feature
, from_stream
);
3539 option_export_marks(arg
);
3540 } else if (!strcmp(feature
, "alias")) {
3541 ; /* Don't die - this feature is supported */
3542 } else if (skip_prefix(feature
, "rewrite-submodules-to=", &arg
)) {
3543 option_rewrite_submodules(arg
, &sub_marks_to
);
3544 } else if (skip_prefix(feature
, "rewrite-submodules-from=", &arg
)) {
3545 option_rewrite_submodules(arg
, &sub_marks_from
);
3546 } else if (!strcmp(feature
, "get-mark")) {
3547 ; /* Don't die - this feature is supported */
3548 } else if (!strcmp(feature
, "cat-blob")) {
3549 ; /* Don't die - this feature is supported */
3550 } else if (!strcmp(feature
, "relative-marks")) {
3551 relative_marks_paths
= 1;
3552 } else if (!strcmp(feature
, "no-relative-marks")) {
3553 relative_marks_paths
= 0;
3554 } else if (!strcmp(feature
, "done")) {
3555 require_explicit_termination
= 1;
3556 } else if (!strcmp(feature
, "force")) {
3558 } else if (!strcmp(feature
, "notes") || !strcmp(feature
, "ls")) {
3559 ; /* do nothing; we have the feature */
3567 static void parse_feature(const char *feature
)
3569 if (seen_data_command
)
3570 die("Got feature command '%s' after data command", feature
);
3572 if (parse_one_feature(feature
, 1))
3575 die("This version of fast-import does not support feature %s.", feature
);
3578 static void parse_option(const char *option
)
3580 if (seen_data_command
)
3581 die("Got option command '%s' after data command", option
);
3583 if (parse_one_option(option
))
3586 die("This version of fast-import does not support option: %s", option
);
3589 static void git_pack_config(void)
3591 int indexversion_value
;
3593 unsigned long packsizelimit_value
;
3595 if (!repo_config_get_ulong(the_repository
, "pack.depth", &max_depth
)) {
3596 if (max_depth
> MAX_DEPTH
)
3597 max_depth
= MAX_DEPTH
;
3599 if (!repo_config_get_int(the_repository
, "pack.indexversion", &indexversion_value
)) {
3600 pack_idx_opts
.version
= indexversion_value
;
3601 if (pack_idx_opts
.version
> 2)
3602 git_die_config(the_repository
, "pack.indexversion",
3603 "bad pack.indexVersion=%"PRIu32
, pack_idx_opts
.version
);
3605 if (!repo_config_get_ulong(the_repository
, "pack.packsizelimit", &packsizelimit_value
))
3606 max_packsize
= packsizelimit_value
;
3608 if (!repo_config_get_int(the_repository
, "fastimport.unpacklimit", &limit
))
3609 unpack_limit
= limit
;
3610 else if (!repo_config_get_int(the_repository
, "transfer.unpacklimit", &limit
))
3611 unpack_limit
= limit
;
3613 repo_config(the_repository
, git_default_config
, NULL
);
3616 static const char fast_import_usage
[] =
3617 "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3619 static void parse_argv(void)
3623 for (i
= 1; i
< global_argc
; i
++) {
3624 const char *a
= global_argv
[i
];
3626 if (*a
!= '-' || !strcmp(a
, "--"))
3629 if (!skip_prefix(a
, "--", &a
))
3630 die("unknown option %s", a
);
3632 if (parse_one_option(a
))
3635 if (parse_one_feature(a
, 0))
3638 if (skip_prefix(a
, "cat-blob-fd=", &a
)) {
3639 option_cat_blob_fd(a
);
3643 die("unknown option --%s", a
);
3645 if (i
!= global_argc
)
3646 usage(fast_import_usage
);
3648 seen_data_command
= 1;
3649 if (import_marks_file
)
3651 build_mark_map(&sub_marks_from
, &sub_marks_to
);
3654 int cmd_fast_import(int argc
,
3657 struct repository
*repo
)
3661 show_usage_if_asked(argc
, argv
, fast_import_usage
);
3663 reset_pack_idx_option(&pack_idx_opts
);
3666 alloc_objects(object_entry_alloc
);
3667 strbuf_init(&command_buf
, 0);
3668 CALLOC_ARRAY(atom_table
, atom_table_sz
);
3669 CALLOC_ARRAY(branch_table
, branch_table_sz
);
3670 CALLOC_ARRAY(avail_tree_table
, avail_tree_table_sz
);
3671 marks
= mem_pool_calloc(&fi_mem_pool
, 1, sizeof(struct mark_set
));
3673 hashmap_init(&object_table
, object_entry_hashcmp
, NULL
, 0);
3676 * We don't parse most options until after we've seen the set of
3677 * "feature" lines at the start of the stream (which allows the command
3678 * line to override stream data). But we must do an early parse of any
3679 * command-line options that impact how we interpret the feature lines.
3681 for (i
= 1; i
< argc
; i
++) {
3682 const char *arg
= argv
[i
];
3683 if (*arg
!= '-' || !strcmp(arg
, "--"))
3685 if (!strcmp(arg
, "--allow-unsafe-features"))
3686 allow_unsafe_features
= 1;
3691 global_prefix
= prefix
;
3693 rc_free
= mem_pool_alloc(&fi_mem_pool
, cmd_save
* sizeof(*rc_free
));
3694 for (i
= 0; i
< (cmd_save
- 1); i
++)
3695 rc_free
[i
].next
= &rc_free
[i
+ 1];
3696 rc_free
[cmd_save
- 1].next
= NULL
;
3699 set_die_routine(die_nicely
);
3700 set_checkpoint_signal();
3701 while (read_next_command() != EOF
) {
3703 if (!strcmp("blob", command_buf
.buf
))
3705 else if (skip_prefix(command_buf
.buf
, "commit ", &v
))
3706 parse_new_commit(v
);
3707 else if (skip_prefix(command_buf
.buf
, "tag ", &v
))
3709 else if (skip_prefix(command_buf
.buf
, "reset ", &v
))
3710 parse_reset_branch(v
);
3711 else if (skip_prefix(command_buf
.buf
, "ls ", &v
))
3713 else if (skip_prefix(command_buf
.buf
, "cat-blob ", &v
))
3715 else if (skip_prefix(command_buf
.buf
, "get-mark ", &v
))
3717 else if (!strcmp("checkpoint", command_buf
.buf
))
3719 else if (!strcmp("done", command_buf
.buf
))
3721 else if (!strcmp("alias", command_buf
.buf
))
3723 else if (starts_with(command_buf
.buf
, "progress "))
3725 else if (skip_prefix(command_buf
.buf
, "feature ", &v
))
3727 else if (skip_prefix(command_buf
.buf
, "option git ", &v
))
3729 else if (starts_with(command_buf
.buf
, "option "))
3730 /* ignore non-git options*/;
3732 die("Unsupported command: %s", command_buf
.buf
);
3734 if (checkpoint_requested
)
3738 /* argv hasn't been parsed yet, do so */
3739 if (!seen_data_command
)
3742 if (require_explicit_termination
&& feof(stdin
))
3743 die("stream ends early");
3756 uintmax_t total_count
= 0, duplicate_count
= 0;
3757 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3758 total_count
+= object_count_by_type
[i
];
3759 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3760 duplicate_count
+= duplicate_count_by_type
[i
];
3762 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3763 fprintf(stderr
, "---------------------------------------------------------------------\n");
3764 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3765 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3766 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
]);
3767 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
]);
3768 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
]);
3769 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
]);
3770 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3771 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3772 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3773 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (tree_entry_allocd
+ fi_mem_pool
.pool_alloc
+ alloc_count
*sizeof(struct object_entry
))/1024);
3774 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd
+ fi_mem_pool
.pool_alloc
) /1024));
3775 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3776 fprintf(stderr
, "---------------------------------------------------------------------\n");
3778 fprintf(stderr
, "---------------------------------------------------------------------\n");
3779 fprintf(stderr
, "\n");
3782 return failure
? 1 : 0;