2 (See Documentation/git-fast-import.txt for maintained documentation.)
3 Format of STDIN stream:
15 new_blob ::= 'blob' lf
18 file_content ::= data;
20 new_commit ::= 'commit' sp ref_str lf
22 ('author' (sp name)? sp '<' email '>' sp when lf)?
23 'committer' (sp name)? sp '<' email '>' sp when lf
25 ('from' sp committish lf)?
26 ('merge' sp committish lf)*
31 file_change ::= file_clr
37 file_clr ::= 'deleteall' lf;
38 file_del ::= 'D' sp path_str lf;
39 file_rnm ::= 'R' sp path_str sp path_str lf;
40 file_cpy ::= 'C' sp path_str sp path_str lf;
41 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
42 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
44 note_obm ::= 'N' sp (hexsha1 | idnum) sp committish lf;
45 note_inm ::= 'N' sp 'inline' sp committish lf
48 new_tag ::= 'tag' sp tag_str lf
49 'from' sp committish lf
50 ('tagger' (sp name)? sp '<' email '>' sp when lf)?
54 reset_branch ::= 'reset' sp ref_str lf
55 ('from' sp committish lf)?
58 checkpoint ::= 'checkpoint' lf
61 progress ::= 'progress' sp not_lf* lf
64 # note: the first idnum in a stream should be 1 and subsequent
65 # idnums should not have gaps between values as this will cause
66 # the stream parser to reserve space for the gapped values. An
67 # idnum can be updated in the future to a new object by issuing
68 # a new mark directive with the old idnum.
70 mark ::= 'mark' sp idnum lf;
71 data ::= (delimited_data | exact_data)
74 # note: delim may be any string but must not contain lf.
75 # data_line may contain any data but must not be exactly
77 delimited_data ::= 'data' sp '<<' delim lf
81 # note: declen indicates the length of binary_data in bytes.
82 # declen does not include the lf preceding the binary data.
84 exact_data ::= 'data' sp declen lf
87 # note: quoted strings are C-style quoting supporting \c for
88 # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
89 # is the signed byte value in octal. Note that the only
90 # characters which must actually be escaped to protect the
91 # stream formatting is: \, " and LF. Otherwise these values
94 committish ::= (ref_str | hexsha1 | sha1exp_str | idnum);
96 sha1exp_str ::= sha1exp;
98 path_str ::= path | '"' quoted(path) '"' ;
99 mode ::= '100644' | '644'
104 declen ::= # unsigned 32 bit value, ascii base10 notation;
105 bigint ::= # unsigned integer value, ascii base10 notation;
106 binary_data ::= # file content, not interpreted;
108 when ::= raw_when | rfc2822_when;
109 raw_when ::= ts sp tz;
110 rfc2822_when ::= # Valid RFC 2822 date and time;
112 sp ::= # ASCII space character;
113 lf ::= # ASCII newline (LF) character;
115 # note: a colon (':') must precede the numerical value assigned to
116 # an idnum. This is to distinguish it from a ref or tag name as
117 # GIT does not permit ':' in ref or tag strings.
119 idnum ::= ':' bigint;
120 path ::= # GIT style file path, e.g. "a/b/c";
121 ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
122 tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
123 sha1exp ::= # Any valid GIT SHA1 expression;
124 hexsha1 ::= # SHA1 in hexadecimal format;
126 # note: name and email are UTF8 strings, however name must not
127 # contain '<' or lf and email must not contain any of the
128 # following: '<', '>', lf.
130 name ::= # valid GIT author/committer name;
131 email ::= # valid GIT author/committer email;
132 ts ::= # time since the epoch in seconds, ascii base10 notation;
133 tz ::= # GIT style timezone;
135 # note: comments and cat requests may appear anywhere
136 # in the input, except within a data command. Any form
137 # of the data command always escapes the related input
138 # from comment processing.
140 # In case it is not clear, the '#' that starts the comment
141 # must be the first character on that line (an lf
144 cat_request ::= 'cat' sp (hexsha1 | idnum) lf
145 | 'cat' sp hexsha1 sp path_str lf;
147 comment ::= '#' not_lf* lf;
148 not_lf ::= # Any byte that is not ASCII newline (LF);
160 #include "csum-file.h"
162 #include "exec_cmd.h"
164 #define PACK_ID_BITS 16
165 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
166 #define DEPTH_BITS 13
167 #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
171 struct pack_idx_entry idx
;
172 struct object_entry
*next
;
173 uint32_t type
: TYPE_BITS
,
174 pack_id
: PACK_ID_BITS
,
178 struct object_entry_pool
180 struct object_entry_pool
*next_pool
;
181 struct object_entry
*next_free
;
182 struct object_entry
*end
;
183 struct object_entry entries
[FLEX_ARRAY
]; /* more */
189 struct object_entry
*marked
[1024];
190 struct mark_set
*sets
[1024];
200 unsigned no_swap
: 1;
205 struct mem_pool
*next_pool
;
208 uintmax_t space
[FLEX_ARRAY
]; /* more */
213 struct atom_str
*next_atom
;
214 unsigned short str_len
;
215 char str_dat
[FLEX_ARRAY
]; /* more */
221 struct tree_content
*tree
;
222 struct atom_str
*name
;
226 unsigned char sha1
[20];
232 unsigned int entry_capacity
; /* must match avail_tree_content */
233 unsigned int entry_count
;
234 unsigned int delta_depth
;
235 struct tree_entry
*entries
[FLEX_ARRAY
]; /* more */
238 struct avail_tree_content
240 unsigned int entry_capacity
; /* must match tree_content */
241 struct avail_tree_content
*next_avail
;
246 struct branch
*table_next_branch
;
247 struct branch
*active_next_branch
;
249 struct tree_entry branch_tree
;
250 uintmax_t last_commit
;
253 unsigned pack_id
: PACK_ID_BITS
;
254 unsigned char sha1
[20];
259 struct tag
*next_tag
;
261 unsigned int pack_id
;
262 unsigned char sha1
[20];
267 struct hash_list
*next
;
268 unsigned char sha1
[20];
277 struct recent_command
279 struct recent_command
*prev
;
280 struct recent_command
*next
;
284 /* Configured limits on output */
285 static unsigned long max_depth
= 10;
286 static off_t max_packsize
;
287 static uintmax_t big_file_threshold
= 512 * 1024 * 1024;
288 static int force_update
;
289 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
290 static int pack_compression_seen
;
292 /* Stats and misc. counters */
293 static uintmax_t alloc_count
;
294 static uintmax_t marks_set_count
;
295 static uintmax_t object_count_by_type
[1 << TYPE_BITS
];
296 static uintmax_t duplicate_count_by_type
[1 << TYPE_BITS
];
297 static uintmax_t delta_count_by_type
[1 << TYPE_BITS
];
298 static unsigned long object_count
;
299 static unsigned long branch_count
;
300 static unsigned long branch_load_count
;
302 static FILE *pack_edges
;
303 static unsigned int show_stats
= 1;
304 static int global_argc
;
305 static const char **global_argv
;
308 static size_t mem_pool_alloc
= 2*1024*1024 - sizeof(struct mem_pool
);
309 static size_t total_allocd
;
310 static struct mem_pool
*mem_pool
;
312 /* Atom management */
313 static unsigned int atom_table_sz
= 4451;
314 static unsigned int atom_cnt
;
315 static struct atom_str
**atom_table
;
317 /* The .pack file being generated */
318 static unsigned int pack_id
;
319 static struct sha1file
*pack_file
;
320 static struct packed_git
*pack_data
;
321 static struct packed_git
**all_packs
;
322 static off_t pack_size
;
324 /* Table of objects we've written. */
325 static unsigned int object_entry_alloc
= 5000;
326 static struct object_entry_pool
*blocks
;
327 static struct object_entry
*object_table
[1 << 16];
328 static struct mark_set
*marks
;
329 static const char *export_marks_file
;
330 static const char *import_marks_file
;
331 static int import_marks_file_from_stream
;
332 static int relative_marks_paths
;
335 static struct last_object last_blob
= { STRBUF_INIT
, 0, 0, 0 };
337 /* Tree management */
338 static unsigned int tree_entry_alloc
= 1000;
339 static void *avail_tree_entry
;
340 static unsigned int avail_tree_table_sz
= 100;
341 static struct avail_tree_content
**avail_tree_table
;
342 static struct strbuf old_tree
= STRBUF_INIT
;
343 static struct strbuf new_tree
= STRBUF_INIT
;
346 static unsigned long max_active_branches
= 5;
347 static unsigned long cur_active_branches
;
348 static unsigned long branch_table_sz
= 1039;
349 static struct branch
**branch_table
;
350 static struct branch
*active_branches
;
353 static struct tag
*first_tag
;
354 static struct tag
*last_tag
;
356 /* Input stream parsing */
357 static whenspec_type whenspec
= WHENSPEC_RAW
;
358 static struct strbuf command_buf
= STRBUF_INIT
;
359 static int unread_command_buf
;
360 static struct recent_command cmd_hist
= {&cmd_hist
, &cmd_hist
, NULL
};
361 static struct recent_command
*cmd_tail
= &cmd_hist
;
362 static struct recent_command
*rc_free
;
363 static unsigned int cmd_save
= 100;
364 static uintmax_t next_mark
;
365 static struct strbuf new_data
= STRBUF_INIT
;
366 static int seen_data_command
;
368 /* Where to report commits */
369 static int report_fd
= -1;
371 static void parse_argv(void);
372 static void parse_cat_request(void);
374 static void write_branch_report(FILE *rpt
, struct branch
*b
)
376 fprintf(rpt
, "%s:\n", b
->name
);
378 fprintf(rpt
, " status :");
380 fputs(" active", rpt
);
381 if (b
->branch_tree
.tree
)
382 fputs(" loaded", rpt
);
383 if (is_null_sha1(b
->branch_tree
.versions
[1].sha1
))
384 fputs(" dirty", rpt
);
387 fprintf(rpt
, " tip commit : %s\n", sha1_to_hex(b
->sha1
));
388 fprintf(rpt
, " old tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[0].sha1
));
389 fprintf(rpt
, " cur tree : %s\n", sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
390 fprintf(rpt
, " commit clock: %" PRIuMAX
"\n", b
->last_commit
);
392 fputs(" last pack : ", rpt
);
393 if (b
->pack_id
< MAX_PACK_ID
)
394 fprintf(rpt
, "%u", b
->pack_id
);
400 static void dump_marks_helper(FILE *, uintmax_t, struct mark_set
*);
402 static void write_crash_report(const char *err
)
404 char *loc
= git_path("fast_import_crash_%"PRIuMAX
, (uintmax_t) getpid());
405 FILE *rpt
= fopen(loc
, "w");
408 struct recent_command
*rc
;
411 error("can't write crash report %s: %s", loc
, strerror(errno
));
415 fprintf(stderr
, "fast-import: dumping crash report to %s\n", loc
);
417 fprintf(rpt
, "fast-import crash report:\n");
418 fprintf(rpt
, " fast-import process: %"PRIuMAX
"\n", (uintmax_t) getpid());
419 fprintf(rpt
, " parent process : %"PRIuMAX
"\n", (uintmax_t) getppid());
420 fprintf(rpt
, " at %s\n", show_date(time(NULL
), 0, DATE_LOCAL
));
423 fputs("fatal: ", rpt
);
428 fputs("Most Recent Commands Before Crash\n", rpt
);
429 fputs("---------------------------------\n", rpt
);
430 for (rc
= cmd_hist
.next
; rc
!= &cmd_hist
; rc
= rc
->next
) {
431 if (rc
->next
== &cmd_hist
)
440 fputs("Active Branch LRU\n", rpt
);
441 fputs("-----------------\n", rpt
);
442 fprintf(rpt
, " active_branches = %lu cur, %lu max\n",
444 max_active_branches
);
446 fputs(" pos clock name\n", rpt
);
447 fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt
);
448 for (b
= active_branches
, lu
= 0; b
; b
= b
->active_next_branch
)
449 fprintf(rpt
, " %2lu) %6" PRIuMAX
" %s\n",
450 ++lu
, b
->last_commit
, b
->name
);
453 fputs("Inactive Branches\n", rpt
);
454 fputs("-----------------\n", rpt
);
455 for (lu
= 0; lu
< branch_table_sz
; lu
++) {
456 for (b
= branch_table
[lu
]; b
; b
= b
->table_next_branch
)
457 write_branch_report(rpt
, b
);
463 fputs("Annotated Tags\n", rpt
);
464 fputs("--------------\n", rpt
);
465 for (tg
= first_tag
; tg
; tg
= tg
->next_tag
) {
466 fputs(sha1_to_hex(tg
->sha1
), rpt
);
468 fputs(tg
->name
, rpt
);
474 fputs("Marks\n", rpt
);
475 fputs("-----\n", rpt
);
476 if (export_marks_file
)
477 fprintf(rpt
, " exported to %s\n", export_marks_file
);
479 dump_marks_helper(rpt
, 0, marks
);
482 fputs("-------------------\n", rpt
);
483 fputs("END OF CRASH REPORT\n", rpt
);
487 static void end_packfile(void);
488 static void unkeep_all_packs(void);
489 static void dump_marks(void);
491 static NORETURN
void die_nicely(const char *err
, va_list params
)
494 char message
[2 * PATH_MAX
];
496 vsnprintf(message
, sizeof(message
), err
, params
);
497 fputs("fatal: ", stderr
);
498 fputs(message
, stderr
);
503 write_crash_report(message
);
511 static void alloc_objects(unsigned int cnt
)
513 struct object_entry_pool
*b
;
515 b
= xmalloc(sizeof(struct object_entry_pool
)
516 + cnt
* sizeof(struct object_entry
));
517 b
->next_pool
= blocks
;
518 b
->next_free
= b
->entries
;
519 b
->end
= b
->entries
+ cnt
;
524 static struct object_entry
*new_object(unsigned char *sha1
)
526 struct object_entry
*e
;
528 if (blocks
->next_free
== blocks
->end
)
529 alloc_objects(object_entry_alloc
);
531 e
= blocks
->next_free
++;
532 hashcpy(e
->idx
.sha1
, sha1
);
536 static struct object_entry
*find_object(unsigned char *sha1
)
538 unsigned int h
= sha1
[0] << 8 | sha1
[1];
539 struct object_entry
*e
;
540 for (e
= object_table
[h
]; e
; e
= e
->next
)
541 if (!hashcmp(sha1
, e
->idx
.sha1
))
546 static struct object_entry
*insert_object(unsigned char *sha1
)
548 unsigned int h
= sha1
[0] << 8 | sha1
[1];
549 struct object_entry
*e
= object_table
[h
];
550 struct object_entry
*p
= NULL
;
553 if (!hashcmp(sha1
, e
->idx
.sha1
))
559 e
= new_object(sha1
);
569 static unsigned int hc_str(const char *s
, size_t len
)
577 static void *pool_alloc(size_t len
)
582 /* round up to a 'uintmax_t' alignment */
583 if (len
& (sizeof(uintmax_t) - 1))
584 len
+= sizeof(uintmax_t) - (len
& (sizeof(uintmax_t) - 1));
586 for (p
= mem_pool
; p
; p
= p
->next_pool
)
587 if ((p
->end
- p
->next_free
>= len
))
591 if (len
>= (mem_pool_alloc
/2)) {
595 total_allocd
+= sizeof(struct mem_pool
) + mem_pool_alloc
;
596 p
= xmalloc(sizeof(struct mem_pool
) + mem_pool_alloc
);
597 p
->next_pool
= mem_pool
;
598 p
->next_free
= (char *) p
->space
;
599 p
->end
= p
->next_free
+ mem_pool_alloc
;
608 static void *pool_calloc(size_t count
, size_t size
)
610 size_t len
= count
* size
;
611 void *r
= pool_alloc(len
);
616 static char *pool_strdup(const char *s
)
618 char *r
= pool_alloc(strlen(s
) + 1);
623 static void insert_mark(uintmax_t idnum
, struct object_entry
*oe
)
625 struct mark_set
*s
= marks
;
626 while ((idnum
>> s
->shift
) >= 1024) {
627 s
= pool_calloc(1, sizeof(struct mark_set
));
628 s
->shift
= marks
->shift
+ 10;
629 s
->data
.sets
[0] = marks
;
633 uintmax_t i
= idnum
>> s
->shift
;
634 idnum
-= i
<< s
->shift
;
635 if (!s
->data
.sets
[i
]) {
636 s
->data
.sets
[i
] = pool_calloc(1, sizeof(struct mark_set
));
637 s
->data
.sets
[i
]->shift
= s
->shift
- 10;
641 if (!s
->data
.marked
[idnum
])
643 s
->data
.marked
[idnum
] = oe
;
646 static struct object_entry
*find_mark(uintmax_t idnum
)
648 uintmax_t orig_idnum
= idnum
;
649 struct mark_set
*s
= marks
;
650 struct object_entry
*oe
= NULL
;
651 if ((idnum
>> s
->shift
) < 1024) {
652 while (s
&& s
->shift
) {
653 uintmax_t i
= idnum
>> s
->shift
;
654 idnum
-= i
<< s
->shift
;
658 oe
= s
->data
.marked
[idnum
];
661 die("mark :%" PRIuMAX
" not declared", orig_idnum
);
665 static struct atom_str
*to_atom(const char *s
, unsigned short len
)
667 unsigned int hc
= hc_str(s
, len
) % atom_table_sz
;
670 for (c
= atom_table
[hc
]; c
; c
= c
->next_atom
)
671 if (c
->str_len
== len
&& !strncmp(s
, c
->str_dat
, len
))
674 c
= pool_alloc(sizeof(struct atom_str
) + len
+ 1);
676 strncpy(c
->str_dat
, s
, len
);
678 c
->next_atom
= atom_table
[hc
];
684 static struct branch
*lookup_branch(const char *name
)
686 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
689 for (b
= branch_table
[hc
]; b
; b
= b
->table_next_branch
)
690 if (!strcmp(name
, b
->name
))
695 static struct branch
*new_branch(const char *name
)
697 unsigned int hc
= hc_str(name
, strlen(name
)) % branch_table_sz
;
698 struct branch
*b
= lookup_branch(name
);
701 die("Invalid attempt to create duplicate branch: %s", name
);
702 switch (check_ref_format(name
)) {
703 case 0: break; /* its valid */
704 case CHECK_REF_FORMAT_ONELEVEL
:
705 break; /* valid, but too few '/', allow anyway */
707 die("Branch name doesn't conform to GIT standards: %s", name
);
710 b
= pool_calloc(1, sizeof(struct branch
));
711 b
->name
= pool_strdup(name
);
712 b
->table_next_branch
= branch_table
[hc
];
713 b
->branch_tree
.versions
[0].mode
= S_IFDIR
;
714 b
->branch_tree
.versions
[1].mode
= S_IFDIR
;
717 b
->pack_id
= MAX_PACK_ID
;
718 branch_table
[hc
] = b
;
723 static unsigned int hc_entries(unsigned int cnt
)
725 cnt
= cnt
& 7 ? (cnt
/ 8) + 1 : cnt
/ 8;
726 return cnt
< avail_tree_table_sz
? cnt
: avail_tree_table_sz
- 1;
729 static struct tree_content
*new_tree_content(unsigned int cnt
)
731 struct avail_tree_content
*f
, *l
= NULL
;
732 struct tree_content
*t
;
733 unsigned int hc
= hc_entries(cnt
);
735 for (f
= avail_tree_table
[hc
]; f
; l
= f
, f
= f
->next_avail
)
736 if (f
->entry_capacity
>= cnt
)
741 l
->next_avail
= f
->next_avail
;
743 avail_tree_table
[hc
] = f
->next_avail
;
745 cnt
= cnt
& 7 ? ((cnt
/ 8) + 1) * 8 : cnt
;
746 f
= pool_alloc(sizeof(*t
) + sizeof(t
->entries
[0]) * cnt
);
747 f
->entry_capacity
= cnt
;
750 t
= (struct tree_content
*)f
;
756 static void release_tree_entry(struct tree_entry
*e
);
757 static void release_tree_content(struct tree_content
*t
)
759 struct avail_tree_content
*f
= (struct avail_tree_content
*)t
;
760 unsigned int hc
= hc_entries(f
->entry_capacity
);
761 f
->next_avail
= avail_tree_table
[hc
];
762 avail_tree_table
[hc
] = f
;
765 static void release_tree_content_recursive(struct tree_content
*t
)
768 for (i
= 0; i
< t
->entry_count
; i
++)
769 release_tree_entry(t
->entries
[i
]);
770 release_tree_content(t
);
773 static struct tree_content
*grow_tree_content(
774 struct tree_content
*t
,
777 struct tree_content
*r
= new_tree_content(t
->entry_count
+ amt
);
778 r
->entry_count
= t
->entry_count
;
779 r
->delta_depth
= t
->delta_depth
;
780 memcpy(r
->entries
,t
->entries
,t
->entry_count
*sizeof(t
->entries
[0]));
781 release_tree_content(t
);
785 static struct tree_entry
*new_tree_entry(void)
787 struct tree_entry
*e
;
789 if (!avail_tree_entry
) {
790 unsigned int n
= tree_entry_alloc
;
791 total_allocd
+= n
* sizeof(struct tree_entry
);
792 avail_tree_entry
= e
= xmalloc(n
* sizeof(struct tree_entry
));
794 *((void**)e
) = e
+ 1;
800 e
= avail_tree_entry
;
801 avail_tree_entry
= *((void**)e
);
805 static void release_tree_entry(struct tree_entry
*e
)
808 release_tree_content_recursive(e
->tree
);
809 *((void**)e
) = avail_tree_entry
;
810 avail_tree_entry
= e
;
813 static struct tree_content
*dup_tree_content(struct tree_content
*s
)
815 struct tree_content
*d
;
816 struct tree_entry
*a
, *b
;
821 d
= new_tree_content(s
->entry_count
);
822 for (i
= 0; i
< s
->entry_count
; i
++) {
824 b
= new_tree_entry();
825 memcpy(b
, a
, sizeof(*a
));
826 if (a
->tree
&& is_null_sha1(b
->versions
[1].sha1
))
827 b
->tree
= dup_tree_content(a
->tree
);
832 d
->entry_count
= s
->entry_count
;
833 d
->delta_depth
= s
->delta_depth
;
838 static void start_packfile(void)
840 static char tmpfile
[PATH_MAX
];
841 struct packed_git
*p
;
842 struct pack_header hdr
;
845 pack_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
846 "pack/tmp_pack_XXXXXX");
847 p
= xcalloc(1, sizeof(*p
) + strlen(tmpfile
) + 2);
848 strcpy(p
->pack_name
, tmpfile
);
849 p
->pack_fd
= pack_fd
;
850 pack_file
= sha1fd(pack_fd
, p
->pack_name
);
852 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
853 hdr
.hdr_version
= htonl(2);
855 sha1write(pack_file
, &hdr
, sizeof(hdr
));
858 pack_size
= sizeof(hdr
);
861 all_packs
= xrealloc(all_packs
, sizeof(*all_packs
) * (pack_id
+ 1));
862 all_packs
[pack_id
] = p
;
865 static const char *create_index(void)
868 struct pack_idx_entry
**idx
, **c
, **last
;
869 struct object_entry
*e
;
870 struct object_entry_pool
*o
;
872 /* Build the table of object IDs. */
873 idx
= xmalloc(object_count
* sizeof(*idx
));
875 for (o
= blocks
; o
; o
= o
->next_pool
)
876 for (e
= o
->next_free
; e
-- != o
->entries
;)
877 if (pack_id
== e
->pack_id
)
879 last
= idx
+ object_count
;
881 die("internal consistency error creating the index");
883 tmpfile
= write_idx_file(NULL
, idx
, object_count
, pack_data
->sha1
);
888 static char *keep_pack(const char *curr_index_name
)
890 static char name
[PATH_MAX
];
891 static const char *keep_msg
= "fast-import";
894 keep_fd
= odb_pack_keep(name
, sizeof(name
), pack_data
->sha1
);
896 die_errno("cannot create keep file");
897 write_or_die(keep_fd
, keep_msg
, strlen(keep_msg
));
899 die_errno("failed to write keep file");
901 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
902 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
903 if (move_temp_to_file(pack_data
->pack_name
, name
))
904 die("cannot store pack file");
906 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
907 get_object_directory(), sha1_to_hex(pack_data
->sha1
));
908 if (move_temp_to_file(curr_index_name
, name
))
909 die("cannot store index file");
910 free((void *)curr_index_name
);
914 static void unkeep_all_packs(void)
916 static char name
[PATH_MAX
];
919 for (k
= 0; k
< pack_id
; k
++) {
920 struct packed_git
*p
= all_packs
[k
];
921 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
922 get_object_directory(), sha1_to_hex(p
->sha1
));
923 unlink_or_warn(name
);
927 static void end_packfile(void)
929 struct packed_git
*old_p
= pack_data
, *new_p
;
931 clear_delta_base_cache();
933 unsigned char cur_pack_sha1
[20];
939 close_pack_windows(pack_data
);
940 sha1close(pack_file
, cur_pack_sha1
, 0);
941 fixup_pack_header_footer(pack_data
->pack_fd
, pack_data
->sha1
,
942 pack_data
->pack_name
, object_count
,
943 cur_pack_sha1
, pack_size
);
944 close(pack_data
->pack_fd
);
945 idx_name
= keep_pack(create_index());
947 /* Register the packfile with core git's machinery. */
948 new_p
= add_packed_git(idx_name
, strlen(idx_name
), 1);
950 die("core git rejected index %s", idx_name
);
951 all_packs
[pack_id
] = new_p
;
952 install_packed_git(new_p
);
954 /* Print the boundary */
956 fprintf(pack_edges
, "%s:", new_p
->pack_name
);
957 for (i
= 0; i
< branch_table_sz
; i
++) {
958 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
) {
959 if (b
->pack_id
== pack_id
)
960 fprintf(pack_edges
, " %s", sha1_to_hex(b
->sha1
));
963 for (t
= first_tag
; t
; t
= t
->next_tag
) {
964 if (t
->pack_id
== pack_id
)
965 fprintf(pack_edges
, " %s", sha1_to_hex(t
->sha1
));
967 fputc('\n', pack_edges
);
974 close(old_p
->pack_fd
);
975 unlink_or_warn(old_p
->pack_name
);
979 /* We can't carry a delta across packfiles. */
980 strbuf_release(&last_blob
.data
);
981 last_blob
.offset
= 0;
985 static void cycle_packfile(void)
991 static int store_object(
992 enum object_type type
,
994 struct last_object
*last
,
995 unsigned char *sha1out
,
999 struct object_entry
*e
;
1000 unsigned char hdr
[96];
1001 unsigned char sha1
[20];
1002 unsigned long hdrlen
, deltalen
;
1006 hdrlen
= sprintf((char *)hdr
,"%s %lu", typename(type
),
1007 (unsigned long)dat
->len
) + 1;
1009 git_SHA1_Update(&c
, hdr
, hdrlen
);
1010 git_SHA1_Update(&c
, dat
->buf
, dat
->len
);
1011 git_SHA1_Final(sha1
, &c
);
1013 hashcpy(sha1out
, sha1
);
1015 e
= insert_object(sha1
);
1017 insert_mark(mark
, e
);
1018 if (e
->idx
.offset
) {
1019 duplicate_count_by_type
[type
]++;
1021 } else if (find_sha1_pack(sha1
, packed_git
)) {
1023 e
->pack_id
= MAX_PACK_ID
;
1024 e
->idx
.offset
= 1; /* just not zero! */
1025 duplicate_count_by_type
[type
]++;
1029 if (last
&& last
->data
.buf
&& last
->depth
< max_depth
&& dat
->len
> 20) {
1030 delta
= diff_delta(last
->data
.buf
, last
->data
.len
,
1032 &deltalen
, dat
->len
- 20);
1036 memset(&s
, 0, sizeof(s
));
1037 deflateInit(&s
, pack_compression_level
);
1040 s
.avail_in
= deltalen
;
1042 s
.next_in
= (void *)dat
->buf
;
1043 s
.avail_in
= dat
->len
;
1045 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1046 s
.next_out
= out
= xmalloc(s
.avail_out
);
1047 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1051 /* Determine if we should auto-checkpoint. */
1052 if ((max_packsize
&& (pack_size
+ 60 + s
.total_out
) > max_packsize
)
1053 || (pack_size
+ 60 + s
.total_out
) < pack_size
) {
1055 /* This new object needs to *not* have the current pack_id. */
1056 e
->pack_id
= pack_id
+ 1;
1059 /* We cannot carry a delta into the new pack. */
1064 memset(&s
, 0, sizeof(s
));
1065 deflateInit(&s
, pack_compression_level
);
1066 s
.next_in
= (void *)dat
->buf
;
1067 s
.avail_in
= dat
->len
;
1068 s
.avail_out
= deflateBound(&s
, s
.avail_in
);
1069 s
.next_out
= out
= xrealloc(out
, s
.avail_out
);
1070 while (deflate(&s
, Z_FINISH
) == Z_OK
)
1077 e
->pack_id
= pack_id
;
1078 e
->idx
.offset
= pack_size
;
1080 object_count_by_type
[type
]++;
1082 crc32_begin(pack_file
);
1085 off_t ofs
= e
->idx
.offset
- last
->offset
;
1086 unsigned pos
= sizeof(hdr
) - 1;
1088 delta_count_by_type
[type
]++;
1089 e
->depth
= last
->depth
+ 1;
1091 hdrlen
= encode_in_pack_object_header(OBJ_OFS_DELTA
, deltalen
, hdr
);
1092 sha1write(pack_file
, hdr
, hdrlen
);
1093 pack_size
+= hdrlen
;
1095 hdr
[pos
] = ofs
& 127;
1097 hdr
[--pos
] = 128 | (--ofs
& 127);
1098 sha1write(pack_file
, hdr
+ pos
, sizeof(hdr
) - pos
);
1099 pack_size
+= sizeof(hdr
) - pos
;
1102 hdrlen
= encode_in_pack_object_header(type
, dat
->len
, hdr
);
1103 sha1write(pack_file
, hdr
, hdrlen
);
1104 pack_size
+= hdrlen
;
1107 sha1write(pack_file
, out
, s
.total_out
);
1108 pack_size
+= s
.total_out
;
1110 e
->idx
.crc32
= crc32_end(pack_file
);
1115 if (last
->no_swap
) {
1118 strbuf_swap(&last
->data
, dat
);
1120 last
->offset
= e
->idx
.offset
;
1121 last
->depth
= e
->depth
;
1126 static void truncate_pack(off_t to
, git_SHA_CTX
*ctx
)
1128 if (ftruncate(pack_data
->pack_fd
, to
)
1129 || lseek(pack_data
->pack_fd
, to
, SEEK_SET
) != to
)
1130 die_errno("cannot truncate pack to skip duplicate");
1133 /* yes this is a layering violation */
1134 pack_file
->total
= to
;
1135 pack_file
->offset
= 0;
1136 pack_file
->ctx
= *ctx
;
1139 static void stream_blob(uintmax_t len
, unsigned char *sha1out
, uintmax_t mark
)
1141 size_t in_sz
= 64 * 1024, out_sz
= 64 * 1024;
1142 unsigned char *in_buf
= xmalloc(in_sz
);
1143 unsigned char *out_buf
= xmalloc(out_sz
);
1144 struct object_entry
*e
;
1145 unsigned char sha1
[20];
1146 unsigned long hdrlen
;
1149 git_SHA_CTX pack_file_ctx
;
1153 /* Determine if we should auto-checkpoint. */
1154 if ((max_packsize
&& (pack_size
+ 60 + len
) > max_packsize
)
1155 || (pack_size
+ 60 + len
) < pack_size
)
1160 /* preserve the pack_file SHA1 ctx in case we have to truncate later */
1161 sha1flush(pack_file
);
1162 pack_file_ctx
= pack_file
->ctx
;
1164 hdrlen
= snprintf((char *)out_buf
, out_sz
, "blob %" PRIuMAX
, len
) + 1;
1165 if (out_sz
<= hdrlen
)
1166 die("impossibly large object header");
1169 git_SHA1_Update(&c
, out_buf
, hdrlen
);
1171 crc32_begin(pack_file
);
1173 memset(&s
, 0, sizeof(s
));
1174 deflateInit(&s
, pack_compression_level
);
1176 hdrlen
= encode_in_pack_object_header(OBJ_BLOB
, len
, out_buf
);
1177 if (out_sz
<= hdrlen
)
1178 die("impossibly large object header");
1180 s
.next_out
= out_buf
+ hdrlen
;
1181 s
.avail_out
= out_sz
- hdrlen
;
1183 while (status
!= Z_STREAM_END
) {
1184 if (0 < len
&& !s
.avail_in
) {
1185 size_t cnt
= in_sz
< len
? in_sz
: (size_t)len
;
1186 size_t n
= fread(in_buf
, 1, cnt
, stdin
);
1187 if (!n
&& feof(stdin
))
1188 die("EOF in data (%" PRIuMAX
" bytes remaining)", len
);
1190 git_SHA1_Update(&c
, in_buf
, n
);
1196 status
= deflate(&s
, len
? 0 : Z_FINISH
);
1198 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
1199 size_t n
= s
.next_out
- out_buf
;
1200 sha1write(pack_file
, out_buf
, n
);
1202 s
.next_out
= out_buf
;
1203 s
.avail_out
= out_sz
;
1212 die("unexpected deflate failure: %d", status
);
1216 git_SHA1_Final(sha1
, &c
);
1219 hashcpy(sha1out
, sha1
);
1221 e
= insert_object(sha1
);
1224 insert_mark(mark
, e
);
1226 if (e
->idx
.offset
) {
1227 duplicate_count_by_type
[OBJ_BLOB
]++;
1228 truncate_pack(offset
, &pack_file_ctx
);
1230 } else if (find_sha1_pack(sha1
, packed_git
)) {
1232 e
->pack_id
= MAX_PACK_ID
;
1233 e
->idx
.offset
= 1; /* just not zero! */
1234 duplicate_count_by_type
[OBJ_BLOB
]++;
1235 truncate_pack(offset
, &pack_file_ctx
);
1240 e
->pack_id
= pack_id
;
1241 e
->idx
.offset
= offset
;
1242 e
->idx
.crc32
= crc32_end(pack_file
);
1244 object_count_by_type
[OBJ_BLOB
]++;
1251 /* All calls must be guarded by find_object() or find_mark() to
1252 * ensure the 'struct object_entry' passed was written by this
1253 * process instance. We unpack the entry by the offset, avoiding
1254 * the need for the corresponding .idx file. This unpacking rule
1255 * works because we only use OBJ_REF_DELTA within the packfiles
1256 * created by fast-import.
1258 * oe must not be NULL. Such an oe usually comes from giving
1259 * an unknown SHA-1 to find_object() or an undefined mark to
1260 * find_mark(). Callers must test for this condition and use
1261 * the standard read_sha1_file() when it happens.
1263 * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
1264 * find_mark(), where the mark was reloaded from an existing marks
1265 * file and is referencing an object that this fast-import process
1266 * instance did not write out to a packfile. Callers must test for
1267 * this condition and use read_sha1_file() instead.
1269 static void *gfi_unpack_entry(
1270 struct object_entry
*oe
,
1271 unsigned long *sizep
)
1273 enum object_type type
;
1274 struct packed_git
*p
= all_packs
[oe
->pack_id
];
1275 if (p
== pack_data
&& p
->pack_size
< (pack_size
+ 20)) {
1276 /* The object is stored in the packfile we are writing to
1277 * and we have modified it since the last time we scanned
1278 * back to read a previously written object. If an old
1279 * window covered [p->pack_size, p->pack_size + 20) its
1280 * data is stale and is not valid. Closing all windows
1281 * and updating the packfile length ensures we can read
1282 * the newly written data.
1284 close_pack_windows(p
);
1285 sha1flush(pack_file
);
1287 /* We have to offer 20 bytes additional on the end of
1288 * the packfile as the core unpacker code assumes the
1289 * footer is present at the file end and must promise
1290 * at least 20 bytes within any window it maps. But
1291 * we don't actually create the footer here.
1293 p
->pack_size
= pack_size
+ 20;
1295 return unpack_entry(p
, oe
->idx
.offset
, &type
, sizep
);
1298 static const char *get_mode(const char *str
, uint16_t *modep
)
1303 while ((c
= *str
++) != ' ') {
1304 if (c
< '0' || c
> '7')
1306 mode
= (mode
<< 3) + (c
- '0');
1312 static void load_tree(struct tree_entry
*root
)
1314 unsigned char *sha1
= root
->versions
[1].sha1
;
1315 struct object_entry
*myoe
;
1316 struct tree_content
*t
;
1321 root
->tree
= t
= new_tree_content(8);
1322 if (is_null_sha1(sha1
))
1325 myoe
= find_object(sha1
);
1326 if (myoe
&& myoe
->pack_id
!= MAX_PACK_ID
) {
1327 if (myoe
->type
!= OBJ_TREE
)
1328 die("Not a tree: %s", sha1_to_hex(sha1
));
1329 t
->delta_depth
= myoe
->depth
;
1330 buf
= gfi_unpack_entry(myoe
, &size
);
1332 die("Can't load tree %s", sha1_to_hex(sha1
));
1334 enum object_type type
;
1335 buf
= read_sha1_file(sha1
, &type
, &size
);
1336 if (!buf
|| type
!= OBJ_TREE
)
1337 die("Can't load tree %s", sha1_to_hex(sha1
));
1341 while (c
!= (buf
+ size
)) {
1342 struct tree_entry
*e
= new_tree_entry();
1344 if (t
->entry_count
== t
->entry_capacity
)
1345 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1346 t
->entries
[t
->entry_count
++] = e
;
1349 c
= get_mode(c
, &e
->versions
[1].mode
);
1351 die("Corrupt mode in %s", sha1_to_hex(sha1
));
1352 e
->versions
[0].mode
= e
->versions
[1].mode
;
1353 e
->name
= to_atom(c
, strlen(c
));
1354 c
+= e
->name
->str_len
+ 1;
1355 hashcpy(e
->versions
[0].sha1
, (unsigned char *)c
);
1356 hashcpy(e
->versions
[1].sha1
, (unsigned char *)c
);
1362 static int tecmp0 (const void *_a
, const void *_b
)
1364 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1365 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1366 return base_name_compare(
1367 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[0].mode
,
1368 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[0].mode
);
1371 static int tecmp1 (const void *_a
, const void *_b
)
1373 struct tree_entry
*a
= *((struct tree_entry
**)_a
);
1374 struct tree_entry
*b
= *((struct tree_entry
**)_b
);
1375 return base_name_compare(
1376 a
->name
->str_dat
, a
->name
->str_len
, a
->versions
[1].mode
,
1377 b
->name
->str_dat
, b
->name
->str_len
, b
->versions
[1].mode
);
1380 static void mktree(struct tree_content
*t
, int v
, struct strbuf
*b
)
1386 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp0
);
1388 qsort(t
->entries
,t
->entry_count
,sizeof(t
->entries
[0]),tecmp1
);
1390 for (i
= 0; i
< t
->entry_count
; i
++) {
1391 if (t
->entries
[i
]->versions
[v
].mode
)
1392 maxlen
+= t
->entries
[i
]->name
->str_len
+ 34;
1396 strbuf_grow(b
, maxlen
);
1397 for (i
= 0; i
< t
->entry_count
; i
++) {
1398 struct tree_entry
*e
= t
->entries
[i
];
1399 if (!e
->versions
[v
].mode
)
1401 strbuf_addf(b
, "%o %s%c", (unsigned int)e
->versions
[v
].mode
,
1402 e
->name
->str_dat
, '\0');
1403 strbuf_add(b
, e
->versions
[v
].sha1
, 20);
1407 static void store_tree(struct tree_entry
*root
)
1409 struct tree_content
*t
= root
->tree
;
1410 unsigned int i
, j
, del
;
1411 struct last_object lo
= { STRBUF_INIT
, 0, 0, /* no_swap */ 1 };
1412 struct object_entry
*le
;
1414 if (!is_null_sha1(root
->versions
[1].sha1
))
1417 for (i
= 0; i
< t
->entry_count
; i
++) {
1418 if (t
->entries
[i
]->tree
)
1419 store_tree(t
->entries
[i
]);
1422 le
= find_object(root
->versions
[0].sha1
);
1423 if (S_ISDIR(root
->versions
[0].mode
) && le
&& le
->pack_id
== pack_id
) {
1424 mktree(t
, 0, &old_tree
);
1426 lo
.offset
= le
->idx
.offset
;
1427 lo
.depth
= t
->delta_depth
;
1430 mktree(t
, 1, &new_tree
);
1431 store_object(OBJ_TREE
, &new_tree
, &lo
, root
->versions
[1].sha1
, 0);
1433 t
->delta_depth
= lo
.depth
;
1434 for (i
= 0, j
= 0, del
= 0; i
< t
->entry_count
; i
++) {
1435 struct tree_entry
*e
= t
->entries
[i
];
1436 if (e
->versions
[1].mode
) {
1437 e
->versions
[0].mode
= e
->versions
[1].mode
;
1438 hashcpy(e
->versions
[0].sha1
, e
->versions
[1].sha1
);
1439 t
->entries
[j
++] = e
;
1441 release_tree_entry(e
);
1445 t
->entry_count
-= del
;
1448 static int tree_content_set(
1449 struct tree_entry
*root
,
1451 const unsigned char *sha1
,
1452 const uint16_t mode
,
1453 struct tree_content
*subtree
)
1455 struct tree_content
*t
= root
->tree
;
1458 struct tree_entry
*e
;
1460 slash1
= strchr(p
, '/');
1466 die("Empty path component found in input");
1467 if (!slash1
&& !S_ISDIR(mode
) && subtree
)
1468 die("Non-directories cannot have subtrees");
1470 for (i
= 0; i
< t
->entry_count
; i
++) {
1472 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1475 && e
->versions
[1].mode
== mode
1476 && !hashcmp(e
->versions
[1].sha1
, sha1
))
1478 e
->versions
[1].mode
= mode
;
1479 hashcpy(e
->versions
[1].sha1
, sha1
);
1481 release_tree_content_recursive(e
->tree
);
1483 hashclr(root
->versions
[1].sha1
);
1486 if (!S_ISDIR(e
->versions
[1].mode
)) {
1487 e
->tree
= new_tree_content(8);
1488 e
->versions
[1].mode
= S_IFDIR
;
1492 if (tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
)) {
1493 hashclr(root
->versions
[1].sha1
);
1500 if (t
->entry_count
== t
->entry_capacity
)
1501 root
->tree
= t
= grow_tree_content(t
, t
->entry_count
);
1502 e
= new_tree_entry();
1503 e
->name
= to_atom(p
, n
);
1504 e
->versions
[0].mode
= 0;
1505 hashclr(e
->versions
[0].sha1
);
1506 t
->entries
[t
->entry_count
++] = e
;
1508 e
->tree
= new_tree_content(8);
1509 e
->versions
[1].mode
= S_IFDIR
;
1510 tree_content_set(e
, slash1
+ 1, sha1
, mode
, subtree
);
1513 e
->versions
[1].mode
= mode
;
1514 hashcpy(e
->versions
[1].sha1
, sha1
);
1516 hashclr(root
->versions
[1].sha1
);
1520 static int tree_content_remove(
1521 struct tree_entry
*root
,
1523 struct tree_entry
*backup_leaf
)
1525 struct tree_content
*t
= root
->tree
;
1528 struct tree_entry
*e
;
1530 slash1
= strchr(p
, '/');
1536 for (i
= 0; i
< t
->entry_count
; i
++) {
1538 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1539 if (slash1
&& !S_ISDIR(e
->versions
[1].mode
))
1541 * If p names a file in some subdirectory, and a
1542 * file or symlink matching the name of the
1543 * parent directory of p exists, then p cannot
1544 * exist and need not be deleted.
1547 if (!slash1
|| !S_ISDIR(e
->versions
[1].mode
))
1551 if (tree_content_remove(e
, slash1
+ 1, backup_leaf
)) {
1552 for (n
= 0; n
< e
->tree
->entry_count
; n
++) {
1553 if (e
->tree
->entries
[n
]->versions
[1].mode
) {
1554 hashclr(root
->versions
[1].sha1
);
1568 memcpy(backup_leaf
, e
, sizeof(*backup_leaf
));
1570 release_tree_content_recursive(e
->tree
);
1572 e
->versions
[1].mode
= 0;
1573 hashclr(e
->versions
[1].sha1
);
1574 hashclr(root
->versions
[1].sha1
);
1578 static int tree_content_get(
1579 struct tree_entry
*root
,
1581 struct tree_entry
*leaf
)
1583 struct tree_content
*t
= root
->tree
;
1586 struct tree_entry
*e
;
1588 slash1
= strchr(p
, '/');
1594 for (i
= 0; i
< t
->entry_count
; i
++) {
1596 if (e
->name
->str_len
== n
&& !strncmp(p
, e
->name
->str_dat
, n
)) {
1598 memcpy(leaf
, e
, sizeof(*leaf
));
1599 if (e
->tree
&& is_null_sha1(e
->versions
[1].sha1
))
1600 leaf
->tree
= dup_tree_content(e
->tree
);
1605 if (!S_ISDIR(e
->versions
[1].mode
))
1609 return tree_content_get(e
, slash1
+ 1, leaf
);
1615 static int update_branch(struct branch
*b
)
1617 static const char *msg
= "fast-import";
1618 struct ref_lock
*lock
;
1619 unsigned char old_sha1
[20];
1621 if (is_null_sha1(b
->sha1
))
1623 if (read_ref(b
->name
, old_sha1
))
1625 lock
= lock_any_ref_for_update(b
->name
, old_sha1
, 0);
1627 return error("Unable to lock %s", b
->name
);
1628 if (!force_update
&& !is_null_sha1(old_sha1
)) {
1629 struct commit
*old_cmit
, *new_cmit
;
1631 old_cmit
= lookup_commit_reference_gently(old_sha1
, 0);
1632 new_cmit
= lookup_commit_reference_gently(b
->sha1
, 0);
1633 if (!old_cmit
|| !new_cmit
) {
1635 return error("Branch %s is missing commits.", b
->name
);
1638 if (!in_merge_bases(old_cmit
, &new_cmit
, 1)) {
1640 warning("Not updating %s"
1641 " (new tip %s does not contain %s)",
1642 b
->name
, sha1_to_hex(b
->sha1
), sha1_to_hex(old_sha1
));
1646 if (write_ref_sha1(lock
, b
->sha1
, msg
) < 0)
1647 return error("Unable to update %s", b
->name
);
1651 static void dump_branches(void)
1656 for (i
= 0; i
< branch_table_sz
; i
++) {
1657 for (b
= branch_table
[i
]; b
; b
= b
->table_next_branch
)
1658 failure
|= update_branch(b
);
1662 static void dump_tags(void)
1664 static const char *msg
= "fast-import";
1666 struct ref_lock
*lock
;
1667 char ref_name
[PATH_MAX
];
1669 for (t
= first_tag
; t
; t
= t
->next_tag
) {
1670 sprintf(ref_name
, "tags/%s", t
->name
);
1671 lock
= lock_ref_sha1(ref_name
, NULL
);
1672 if (!lock
|| write_ref_sha1(lock
, t
->sha1
, msg
) < 0)
1673 failure
|= error("Unable to update %s", ref_name
);
1677 static void dump_marks_helper(FILE *f
,
1683 for (k
= 0; k
< 1024; k
++) {
1684 if (m
->data
.sets
[k
])
1685 dump_marks_helper(f
, base
+ (k
<< m
->shift
),
1689 for (k
= 0; k
< 1024; k
++) {
1690 if (m
->data
.marked
[k
])
1691 fprintf(f
, ":%" PRIuMAX
" %s\n", base
+ k
,
1692 sha1_to_hex(m
->data
.marked
[k
]->idx
.sha1
));
1697 static void dump_marks(void)
1699 static struct lock_file mark_lock
;
1703 if (!export_marks_file
)
1706 mark_fd
= hold_lock_file_for_update(&mark_lock
, export_marks_file
, 0);
1708 failure
|= error("Unable to write marks file %s: %s",
1709 export_marks_file
, strerror(errno
));
1713 f
= fdopen(mark_fd
, "w");
1715 int saved_errno
= errno
;
1716 rollback_lock_file(&mark_lock
);
1717 failure
|= error("Unable to write marks file %s: %s",
1718 export_marks_file
, strerror(saved_errno
));
1723 * Since the lock file was fdopen()'ed, it should not be close()'ed.
1724 * Assign -1 to the lock file descriptor so that commit_lock_file()
1725 * won't try to close() it.
1729 dump_marks_helper(f
, 0, marks
);
1730 if (ferror(f
) || fclose(f
)) {
1731 int saved_errno
= errno
;
1732 rollback_lock_file(&mark_lock
);
1733 failure
|= error("Unable to write marks file %s: %s",
1734 export_marks_file
, strerror(saved_errno
));
1738 if (commit_lock_file(&mark_lock
)) {
1739 int saved_errno
= errno
;
1740 rollback_lock_file(&mark_lock
);
1741 failure
|= error("Unable to commit marks file %s: %s",
1742 export_marks_file
, strerror(saved_errno
));
1747 static void read_marks(void)
1750 FILE *f
= fopen(import_marks_file
, "r");
1752 die_errno("cannot read '%s'", import_marks_file
);
1753 while (fgets(line
, sizeof(line
), f
)) {
1756 unsigned char sha1
[20];
1757 struct object_entry
*e
;
1759 end
= strchr(line
, '\n');
1760 if (line
[0] != ':' || !end
)
1761 die("corrupt mark line: %s", line
);
1763 mark
= strtoumax(line
+ 1, &end
, 10);
1764 if (!mark
|| end
== line
+ 1
1765 || *end
!= ' ' || get_sha1(end
+ 1, sha1
))
1766 die("corrupt mark line: %s", line
);
1767 e
= find_object(sha1
);
1769 enum object_type type
= sha1_object_info(sha1
, NULL
);
1771 die("object not found: %s", sha1_to_hex(sha1
));
1772 e
= insert_object(sha1
);
1774 e
->pack_id
= MAX_PACK_ID
;
1775 e
->idx
.offset
= 1; /* just not zero! */
1777 insert_mark(mark
, e
);
1782 static int read_next_command(void)
1784 static int stdin_eof
= 0;
1787 unread_command_buf
= 0;
1792 if (unread_command_buf
) {
1793 unread_command_buf
= 0;
1795 struct recent_command
*rc
;
1797 strbuf_detach(&command_buf
, NULL
);
1798 stdin_eof
= strbuf_getline(&command_buf
, stdin
, '\n');
1802 if (!seen_data_command
1803 && prefixcmp(command_buf
.buf
, "feature ")
1804 && prefixcmp(command_buf
.buf
, "option ")) {
1813 cmd_hist
.next
= rc
->next
;
1814 cmd_hist
.next
->prev
= &cmd_hist
;
1818 rc
->buf
= command_buf
.buf
;
1819 rc
->prev
= cmd_tail
;
1820 rc
->next
= cmd_hist
.prev
;
1821 rc
->prev
->next
= rc
;
1824 if (!prefixcmp(command_buf
.buf
, "cat ")) {
1825 parse_cat_request();
1828 if (command_buf
.buf
[0] != '#')
1835 static void skip_optional_lf(void)
1837 int term_char
= fgetc(stdin
);
1838 if (term_char
!= '\n' && term_char
!= EOF
)
1839 ungetc(term_char
, stdin
);
1842 static void parse_mark(void)
1844 if (!prefixcmp(command_buf
.buf
, "mark :")) {
1845 next_mark
= strtoumax(command_buf
.buf
+ 6, NULL
, 10);
1846 read_next_command();
1852 static int parse_data(struct strbuf
*sb
, uintmax_t limit
, uintmax_t *len_res
)
1856 if (prefixcmp(command_buf
.buf
, "data "))
1857 die("Expected 'data n' command, found: %s", command_buf
.buf
);
1859 if (!prefixcmp(command_buf
.buf
+ 5, "<<")) {
1860 char *term
= xstrdup(command_buf
.buf
+ 5 + 2);
1861 size_t term_len
= command_buf
.len
- 5 - 2;
1863 strbuf_detach(&command_buf
, NULL
);
1865 if (strbuf_getline(&command_buf
, stdin
, '\n') == EOF
)
1866 die("EOF in data (terminator '%s' not found)", term
);
1867 if (term_len
== command_buf
.len
1868 && !strcmp(term
, command_buf
.buf
))
1870 strbuf_addbuf(sb
, &command_buf
);
1871 strbuf_addch(sb
, '\n');
1876 uintmax_t len
= strtoumax(command_buf
.buf
+ 5, NULL
, 10);
1877 size_t n
= 0, length
= (size_t)len
;
1879 if (limit
&& limit
< len
) {
1884 die("data is too large to use in this context");
1886 while (n
< length
) {
1887 size_t s
= strbuf_fread(sb
, length
- n
, stdin
);
1888 if (!s
&& feof(stdin
))
1889 die("EOF in data (%lu bytes remaining)",
1890 (unsigned long)(length
- n
));
1899 static int validate_raw_date(const char *src
, char *result
, int maxlen
)
1901 const char *orig_src
= src
;
1907 num
= strtoul(src
, &endp
, 10);
1908 /* NEEDSWORK: perhaps check for reasonable values? */
1909 if (errno
|| endp
== src
|| *endp
!= ' ')
1913 if (*src
!= '-' && *src
!= '+')
1916 num
= strtoul(src
+ 1, &endp
, 10);
1917 if (errno
|| endp
== src
+ 1 || *endp
|| (endp
- orig_src
) >= maxlen
||
1921 strcpy(result
, orig_src
);
1925 static char *parse_ident(const char *buf
)
1931 gt
= strrchr(buf
, '>');
1933 die("Missing > in ident string: %s", buf
);
1936 die("Missing space after > in ident string: %s", buf
);
1938 name_len
= gt
- buf
;
1939 ident
= xmalloc(name_len
+ 24);
1940 strncpy(ident
, buf
, name_len
);
1944 if (validate_raw_date(gt
, ident
+ name_len
, 24) < 0)
1945 die("Invalid raw date \"%s\" in ident: %s", gt
, buf
);
1947 case WHENSPEC_RFC2822
:
1948 if (parse_date(gt
, ident
+ name_len
, 24) < 0)
1949 die("Invalid rfc2822 date \"%s\" in ident: %s", gt
, buf
);
1952 if (strcmp("now", gt
))
1953 die("Date in ident must be 'now': %s", buf
);
1954 datestamp(ident
+ name_len
, 24);
1961 static void parse_and_store_blob(
1962 struct last_object
*last
,
1963 unsigned char *sha1out
,
1966 static struct strbuf buf
= STRBUF_INIT
;
1969 if (parse_data(&buf
, big_file_threshold
, &len
))
1970 store_object(OBJ_BLOB
, &buf
, last
, sha1out
, mark
);
1973 strbuf_release(&last
->data
);
1977 stream_blob(len
, sha1out
, mark
);
1982 static void parse_new_blob(void)
1984 read_next_command();
1986 parse_and_store_blob(&last_blob
, NULL
, next_mark
);
1989 static void unload_one_branch(void)
1991 while (cur_active_branches
1992 && cur_active_branches
>= max_active_branches
) {
1993 uintmax_t min_commit
= ULONG_MAX
;
1994 struct branch
*e
, *l
= NULL
, *p
= NULL
;
1996 for (e
= active_branches
; e
; e
= e
->active_next_branch
) {
1997 if (e
->last_commit
< min_commit
) {
1999 min_commit
= e
->last_commit
;
2005 e
= p
->active_next_branch
;
2006 p
->active_next_branch
= e
->active_next_branch
;
2008 e
= active_branches
;
2009 active_branches
= e
->active_next_branch
;
2012 e
->active_next_branch
= NULL
;
2013 if (e
->branch_tree
.tree
) {
2014 release_tree_content_recursive(e
->branch_tree
.tree
);
2015 e
->branch_tree
.tree
= NULL
;
2017 cur_active_branches
--;
2021 static void load_branch(struct branch
*b
)
2023 load_tree(&b
->branch_tree
);
2026 b
->active_next_branch
= active_branches
;
2027 active_branches
= b
;
2028 cur_active_branches
++;
2029 branch_load_count
++;
2033 static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes
)
2035 unsigned char fanout
= 0;
2036 while ((num_notes
>>= 8))
2041 static void construct_path_with_fanout(const char *hex_sha1
,
2042 unsigned char fanout
, char *path
)
2044 unsigned int i
= 0, j
= 0;
2046 die("Too large fanout (%u)", fanout
);
2048 path
[i
++] = hex_sha1
[j
++];
2049 path
[i
++] = hex_sha1
[j
++];
2053 memcpy(path
+ i
, hex_sha1
+ j
, 40 - j
);
2054 path
[i
+ 40 - j
] = '\0';
2057 static uintmax_t do_change_note_fanout(
2058 struct tree_entry
*orig_root
, struct tree_entry
*root
,
2059 char *hex_sha1
, unsigned int hex_sha1_len
,
2060 char *fullpath
, unsigned int fullpath_len
,
2061 unsigned char fanout
)
2063 struct tree_content
*t
= root
->tree
;
2064 struct tree_entry
*e
, leaf
;
2065 unsigned int i
, tmp_hex_sha1_len
, tmp_fullpath_len
;
2066 uintmax_t num_notes
= 0;
2067 unsigned char sha1
[20];
2070 for (i
= 0; t
&& i
< t
->entry_count
; i
++) {
2072 tmp_hex_sha1_len
= hex_sha1_len
+ e
->name
->str_len
;
2073 tmp_fullpath_len
= fullpath_len
;
2076 * We're interested in EITHER existing note entries (entries
2077 * with exactly 40 hex chars in path, not including directory
2078 * separators), OR directory entries that may contain note
2079 * entries (with < 40 hex chars in path).
2080 * Also, each path component in a note entry must be a multiple
2083 if (!e
->versions
[1].mode
||
2084 tmp_hex_sha1_len
> 40 ||
2085 e
->name
->str_len
% 2)
2088 /* This _may_ be a note entry, or a subdir containing notes */
2089 memcpy(hex_sha1
+ hex_sha1_len
, e
->name
->str_dat
,
2091 if (tmp_fullpath_len
)
2092 fullpath
[tmp_fullpath_len
++] = '/';
2093 memcpy(fullpath
+ tmp_fullpath_len
, e
->name
->str_dat
,
2095 tmp_fullpath_len
+= e
->name
->str_len
;
2096 fullpath
[tmp_fullpath_len
] = '\0';
2098 if (tmp_hex_sha1_len
== 40 && !get_sha1_hex(hex_sha1
, sha1
)) {
2099 /* This is a note entry */
2100 construct_path_with_fanout(hex_sha1
, fanout
, realpath
);
2101 if (!strcmp(fullpath
, realpath
)) {
2102 /* Note entry is in correct location */
2107 /* Rename fullpath to realpath */
2108 if (!tree_content_remove(orig_root
, fullpath
, &leaf
))
2109 die("Failed to remove path %s", fullpath
);
2110 tree_content_set(orig_root
, realpath
,
2111 leaf
.versions
[1].sha1
,
2112 leaf
.versions
[1].mode
,
2114 } else if (S_ISDIR(e
->versions
[1].mode
)) {
2115 /* This is a subdir that may contain note entries */
2118 num_notes
+= do_change_note_fanout(orig_root
, e
,
2119 hex_sha1
, tmp_hex_sha1_len
,
2120 fullpath
, tmp_fullpath_len
, fanout
);
2123 /* The above may have reallocated the current tree_content */
2129 static uintmax_t change_note_fanout(struct tree_entry
*root
,
2130 unsigned char fanout
)
2132 char hex_sha1
[40], path
[60];
2133 return do_change_note_fanout(root
, root
, hex_sha1
, 0, path
, 0, fanout
);
2136 static void file_change_m(struct branch
*b
)
2138 const char *p
= command_buf
.buf
+ 2;
2139 static struct strbuf uq
= STRBUF_INIT
;
2141 struct object_entry
*oe
= oe
;
2142 unsigned char sha1
[20];
2143 uint16_t mode
, inline_data
= 0;
2145 p
= get_mode(p
, &mode
);
2147 die("Corrupt mode: %s", command_buf
.buf
);
2152 case S_IFREG
| 0644:
2153 case S_IFREG
| 0755:
2160 die("Corrupt mode: %s", command_buf
.buf
);
2165 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
2166 hashcpy(sha1
, oe
->idx
.sha1
);
2168 } else if (!prefixcmp(p
, "inline")) {
2172 if (get_sha1_hex(p
, sha1
))
2173 die("Invalid SHA1: %s", command_buf
.buf
);
2174 oe
= find_object(sha1
);
2178 die("Missing space after SHA1: %s", command_buf
.buf
);
2181 if (!unquote_c_style(&uq
, p
, &endp
)) {
2183 die("Garbage after path in: %s", command_buf
.buf
);
2187 if (S_ISGITLINK(mode
)) {
2189 die("Git links cannot be specified 'inline': %s",
2192 if (oe
->type
!= OBJ_COMMIT
)
2193 die("Not a commit (actually a %s): %s",
2194 typename(oe
->type
), command_buf
.buf
);
2197 * Accept the sha1 without checking; it expected to be in
2198 * another repository.
2200 } else if (inline_data
) {
2202 die("Directories cannot be specified 'inline': %s",
2205 strbuf_addstr(&uq
, p
);
2208 read_next_command();
2209 parse_and_store_blob(&last_blob
, sha1
, 0);
2211 enum object_type expected
= S_ISDIR(mode
) ?
2213 enum object_type type
= oe
? oe
->type
:
2214 sha1_object_info(sha1
, NULL
);
2216 die("%s not found: %s",
2217 S_ISDIR(mode
) ? "Tree" : "Blob",
2219 if (type
!= expected
)
2220 die("Not a %s (actually a %s): %s",
2221 typename(expected
), typename(type
),
2225 tree_content_set(&b
->branch_tree
, p
, sha1
, mode
, NULL
);
2228 static void file_change_d(struct branch
*b
)
2230 const char *p
= command_buf
.buf
+ 2;
2231 static struct strbuf uq
= STRBUF_INIT
;
2235 if (!unquote_c_style(&uq
, p
, &endp
)) {
2237 die("Garbage after path in: %s", command_buf
.buf
);
2240 tree_content_remove(&b
->branch_tree
, p
, NULL
);
2243 static void file_change_cr(struct branch
*b
, int rename
)
2246 static struct strbuf s_uq
= STRBUF_INIT
;
2247 static struct strbuf d_uq
= STRBUF_INIT
;
2249 struct tree_entry leaf
;
2251 s
= command_buf
.buf
+ 2;
2252 strbuf_reset(&s_uq
);
2253 if (!unquote_c_style(&s_uq
, s
, &endp
)) {
2255 die("Missing space after source: %s", command_buf
.buf
);
2257 endp
= strchr(s
, ' ');
2259 die("Missing space after source: %s", command_buf
.buf
);
2260 strbuf_add(&s_uq
, s
, endp
- s
);
2266 die("Missing dest: %s", command_buf
.buf
);
2269 strbuf_reset(&d_uq
);
2270 if (!unquote_c_style(&d_uq
, d
, &endp
)) {
2272 die("Garbage after dest in: %s", command_buf
.buf
);
2276 memset(&leaf
, 0, sizeof(leaf
));
2278 tree_content_remove(&b
->branch_tree
, s
, &leaf
);
2280 tree_content_get(&b
->branch_tree
, s
, &leaf
);
2281 if (!leaf
.versions
[1].mode
)
2282 die("Path %s not in branch", s
);
2283 tree_content_set(&b
->branch_tree
, d
,
2284 leaf
.versions
[1].sha1
,
2285 leaf
.versions
[1].mode
,
2289 static void note_change_n(struct branch
*b
, unsigned char old_fanout
)
2291 const char *p
= command_buf
.buf
+ 2;
2292 static struct strbuf uq
= STRBUF_INIT
;
2293 struct object_entry
*oe
= oe
;
2295 unsigned char sha1
[20], commit_sha1
[20];
2297 uint16_t inline_data
= 0;
2298 unsigned char new_fanout
;
2300 /* <dataref> or 'inline' */
2303 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
2304 hashcpy(sha1
, oe
->idx
.sha1
);
2306 } else if (!prefixcmp(p
, "inline")) {
2310 if (get_sha1_hex(p
, sha1
))
2311 die("Invalid SHA1: %s", command_buf
.buf
);
2312 oe
= find_object(sha1
);
2316 die("Missing space after SHA1: %s", command_buf
.buf
);
2319 s
= lookup_branch(p
);
2321 hashcpy(commit_sha1
, s
->sha1
);
2322 } else if (*p
== ':') {
2323 uintmax_t commit_mark
= strtoumax(p
+ 1, NULL
, 10);
2324 struct object_entry
*commit_oe
= find_mark(commit_mark
);
2325 if (commit_oe
->type
!= OBJ_COMMIT
)
2326 die("Mark :%" PRIuMAX
" not a commit", commit_mark
);
2327 hashcpy(commit_sha1
, commit_oe
->idx
.sha1
);
2328 } else if (!get_sha1(p
, commit_sha1
)) {
2330 char *buf
= read_object_with_reference(commit_sha1
,
2331 commit_type
, &size
, commit_sha1
);
2332 if (!buf
|| size
< 46)
2333 die("Not a valid commit: %s", p
);
2336 die("Invalid ref name or SHA1 expression: %s", p
);
2340 strbuf_addstr(&uq
, p
);
2343 read_next_command();
2344 parse_and_store_blob(&last_blob
, sha1
, 0);
2346 if (oe
->type
!= OBJ_BLOB
)
2347 die("Not a blob (actually a %s): %s",
2348 typename(oe
->type
), command_buf
.buf
);
2349 } else if (!is_null_sha1(sha1
)) {
2350 enum object_type type
= sha1_object_info(sha1
, NULL
);
2352 die("Blob not found: %s", command_buf
.buf
);
2353 if (type
!= OBJ_BLOB
)
2354 die("Not a blob (actually a %s): %s",
2355 typename(type
), command_buf
.buf
);
2358 construct_path_with_fanout(sha1_to_hex(commit_sha1
), old_fanout
, path
);
2359 if (tree_content_remove(&b
->branch_tree
, path
, NULL
))
2362 if (is_null_sha1(sha1
))
2363 return; /* nothing to insert */
2366 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2367 construct_path_with_fanout(sha1_to_hex(commit_sha1
), new_fanout
, path
);
2368 tree_content_set(&b
->branch_tree
, path
, sha1
, S_IFREG
| 0644, NULL
);
2371 static void file_change_deleteall(struct branch
*b
)
2373 release_tree_content_recursive(b
->branch_tree
.tree
);
2374 hashclr(b
->branch_tree
.versions
[0].sha1
);
2375 hashclr(b
->branch_tree
.versions
[1].sha1
);
2376 load_tree(&b
->branch_tree
);
2380 static void parse_from_commit(struct branch
*b
, char *buf
, unsigned long size
)
2382 if (!buf
|| size
< 46)
2383 die("Not a valid commit: %s", sha1_to_hex(b
->sha1
));
2384 if (memcmp("tree ", buf
, 5)
2385 || get_sha1_hex(buf
+ 5, b
->branch_tree
.versions
[1].sha1
))
2386 die("The commit %s is corrupt", sha1_to_hex(b
->sha1
));
2387 hashcpy(b
->branch_tree
.versions
[0].sha1
,
2388 b
->branch_tree
.versions
[1].sha1
);
2391 static void parse_from_existing(struct branch
*b
)
2393 if (is_null_sha1(b
->sha1
)) {
2394 hashclr(b
->branch_tree
.versions
[0].sha1
);
2395 hashclr(b
->branch_tree
.versions
[1].sha1
);
2400 buf
= read_object_with_reference(b
->sha1
,
2401 commit_type
, &size
, b
->sha1
);
2402 parse_from_commit(b
, buf
, size
);
2407 static int parse_from(struct branch
*b
)
2412 if (prefixcmp(command_buf
.buf
, "from "))
2415 if (b
->branch_tree
.tree
) {
2416 release_tree_content_recursive(b
->branch_tree
.tree
);
2417 b
->branch_tree
.tree
= NULL
;
2420 from
= strchr(command_buf
.buf
, ' ') + 1;
2421 s
= lookup_branch(from
);
2423 die("Can't create a branch from itself: %s", b
->name
);
2425 unsigned char *t
= s
->branch_tree
.versions
[1].sha1
;
2426 hashcpy(b
->sha1
, s
->sha1
);
2427 hashcpy(b
->branch_tree
.versions
[0].sha1
, t
);
2428 hashcpy(b
->branch_tree
.versions
[1].sha1
, t
);
2429 } else if (*from
== ':') {
2430 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2431 struct object_entry
*oe
= find_mark(idnum
);
2432 if (oe
->type
!= OBJ_COMMIT
)
2433 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2434 hashcpy(b
->sha1
, oe
->idx
.sha1
);
2435 if (oe
->pack_id
!= MAX_PACK_ID
) {
2437 char *buf
= gfi_unpack_entry(oe
, &size
);
2438 parse_from_commit(b
, buf
, size
);
2441 parse_from_existing(b
);
2442 } else if (!get_sha1(from
, b
->sha1
))
2443 parse_from_existing(b
);
2445 die("Invalid ref name or SHA1 expression: %s", from
);
2447 read_next_command();
2451 static struct hash_list
*parse_merge(unsigned int *count
)
2453 struct hash_list
*list
= NULL
, *n
, *e
= e
;
2458 while (!prefixcmp(command_buf
.buf
, "merge ")) {
2459 from
= strchr(command_buf
.buf
, ' ') + 1;
2460 n
= xmalloc(sizeof(*n
));
2461 s
= lookup_branch(from
);
2463 hashcpy(n
->sha1
, s
->sha1
);
2464 else if (*from
== ':') {
2465 uintmax_t idnum
= strtoumax(from
+ 1, NULL
, 10);
2466 struct object_entry
*oe
= find_mark(idnum
);
2467 if (oe
->type
!= OBJ_COMMIT
)
2468 die("Mark :%" PRIuMAX
" not a commit", idnum
);
2469 hashcpy(n
->sha1
, oe
->idx
.sha1
);
2470 } else if (!get_sha1(from
, n
->sha1
)) {
2472 char *buf
= read_object_with_reference(n
->sha1
,
2473 commit_type
, &size
, n
->sha1
);
2474 if (!buf
|| size
< 46)
2475 die("Not a valid commit: %s", from
);
2478 die("Invalid ref name or SHA1 expression: %s", from
);
2487 read_next_command();
2492 static void parse_new_commit(void)
2494 static struct strbuf msg
= STRBUF_INIT
;
2497 char *author
= NULL
;
2498 char *committer
= NULL
;
2499 struct hash_list
*merge_list
= NULL
;
2500 unsigned int merge_count
;
2501 unsigned char prev_fanout
, new_fanout
;
2503 /* Obtain the branch name from the rest of our command */
2504 sp
= strchr(command_buf
.buf
, ' ') + 1;
2505 b
= lookup_branch(sp
);
2509 read_next_command();
2511 if (!prefixcmp(command_buf
.buf
, "author ")) {
2512 author
= parse_ident(command_buf
.buf
+ 7);
2513 read_next_command();
2515 if (!prefixcmp(command_buf
.buf
, "committer ")) {
2516 committer
= parse_ident(command_buf
.buf
+ 10);
2517 read_next_command();
2520 die("Expected committer but didn't get one");
2521 parse_data(&msg
, 0, NULL
);
2522 read_next_command();
2524 merge_list
= parse_merge(&merge_count
);
2526 /* ensure the branch is active/loaded */
2527 if (!b
->branch_tree
.tree
|| !max_active_branches
) {
2528 unload_one_branch();
2532 prev_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2535 while (command_buf
.len
> 0) {
2536 if (!prefixcmp(command_buf
.buf
, "M "))
2538 else if (!prefixcmp(command_buf
.buf
, "D "))
2540 else if (!prefixcmp(command_buf
.buf
, "R "))
2541 file_change_cr(b
, 1);
2542 else if (!prefixcmp(command_buf
.buf
, "C "))
2543 file_change_cr(b
, 0);
2544 else if (!prefixcmp(command_buf
.buf
, "N "))
2545 note_change_n(b
, prev_fanout
);
2546 else if (!strcmp("deleteall", command_buf
.buf
))
2547 file_change_deleteall(b
);
2549 unread_command_buf
= 1;
2552 if (read_next_command() == EOF
)
2556 new_fanout
= convert_num_notes_to_fanout(b
->num_notes
);
2557 if (new_fanout
!= prev_fanout
)
2558 b
->num_notes
= change_note_fanout(&b
->branch_tree
, new_fanout
);
2560 /* build the tree and the commit */
2561 store_tree(&b
->branch_tree
);
2562 hashcpy(b
->branch_tree
.versions
[0].sha1
,
2563 b
->branch_tree
.versions
[1].sha1
);
2565 strbuf_reset(&new_data
);
2566 strbuf_addf(&new_data
, "tree %s\n",
2567 sha1_to_hex(b
->branch_tree
.versions
[1].sha1
));
2568 if (!is_null_sha1(b
->sha1
))
2569 strbuf_addf(&new_data
, "parent %s\n", sha1_to_hex(b
->sha1
));
2570 while (merge_list
) {
2571 struct hash_list
*next
= merge_list
->next
;
2572 strbuf_addf(&new_data
, "parent %s\n", sha1_to_hex(merge_list
->sha1
));
2576 strbuf_addf(&new_data
,
2580 author
? author
: committer
, committer
);
2581 strbuf_addbuf(&new_data
, &msg
);
2585 if (!store_object(OBJ_COMMIT
, &new_data
, NULL
, b
->sha1
, next_mark
))
2586 b
->pack_id
= pack_id
;
2587 if (report_fd
!= -1) {
2588 char *buf
= sha1_to_hex(b
->sha1
);
2590 write_or_die(report_fd
, buf
, 41);
2592 b
->last_commit
= object_count_by_type
[OBJ_COMMIT
];
2595 static void parse_new_tag(void)
2597 static struct strbuf msg
= STRBUF_INIT
;
2603 uintmax_t from_mark
= 0;
2604 unsigned char sha1
[20];
2605 enum object_type type
;
2607 /* Obtain the new tag name from the rest of our command */
2608 sp
= strchr(command_buf
.buf
, ' ') + 1;
2609 t
= pool_alloc(sizeof(struct tag
));
2611 t
->name
= pool_strdup(sp
);
2613 last_tag
->next_tag
= t
;
2617 read_next_command();
2620 if (prefixcmp(command_buf
.buf
, "from "))
2621 die("Expected from command, got %s", command_buf
.buf
);
2622 from
= strchr(command_buf
.buf
, ' ') + 1;
2623 s
= lookup_branch(from
);
2625 hashcpy(sha1
, s
->sha1
);
2627 } else if (*from
== ':') {
2628 struct object_entry
*oe
;
2629 from_mark
= strtoumax(from
+ 1, NULL
, 10);
2630 oe
= find_mark(from_mark
);
2632 hashcpy(sha1
, oe
->idx
.sha1
);
2633 } else if (!get_sha1(from
, sha1
)) {
2637 buf
= read_sha1_file(sha1
, &type
, &size
);
2638 if (!buf
|| size
< 46)
2639 die("Not a valid commit: %s", from
);
2642 die("Invalid ref name or SHA1 expression: %s", from
);
2643 read_next_command();
2646 if (!prefixcmp(command_buf
.buf
, "tagger ")) {
2647 tagger
= parse_ident(command_buf
.buf
+ 7);
2648 read_next_command();
2652 /* tag payload/message */
2653 parse_data(&msg
, 0, NULL
);
2655 /* build the tag object */
2656 strbuf_reset(&new_data
);
2658 strbuf_addf(&new_data
,
2662 sha1_to_hex(sha1
), typename(type
), t
->name
);
2664 strbuf_addf(&new_data
,
2665 "tagger %s\n", tagger
);
2666 strbuf_addch(&new_data
, '\n');
2667 strbuf_addbuf(&new_data
, &msg
);
2670 if (store_object(OBJ_TAG
, &new_data
, NULL
, t
->sha1
, 0))
2671 t
->pack_id
= MAX_PACK_ID
;
2673 t
->pack_id
= pack_id
;
2676 static void parse_reset_branch(void)
2681 /* Obtain the branch name from the rest of our command */
2682 sp
= strchr(command_buf
.buf
, ' ') + 1;
2683 b
= lookup_branch(sp
);
2686 hashclr(b
->branch_tree
.versions
[0].sha1
);
2687 hashclr(b
->branch_tree
.versions
[1].sha1
);
2688 if (b
->branch_tree
.tree
) {
2689 release_tree_content_recursive(b
->branch_tree
.tree
);
2690 b
->branch_tree
.tree
= NULL
;
2695 read_next_command();
2697 if (command_buf
.len
> 0)
2698 unread_command_buf
= 1;
2701 static void quoted_path_sha1(unsigned char sha1
[20], struct tree_entry
*root
,
2702 const char *path
, const char *line
)
2704 struct strbuf uq
= STRBUF_INIT
;
2705 struct tree_entry leaf
= {0};
2708 if (unquote_c_style(&uq
, path
, &x
))
2709 die("Invalid path: %s", line
);
2711 die("Garbage after path: %s", line
);
2713 hashcpy(sha1
, root
->versions
[1].sha1
);
2716 tree_content_get(root
, uq
.buf
, &leaf
);
2717 if (!leaf
.versions
[1].mode
)
2718 die("Path %s not in tree", uq
.buf
);
2719 hashcpy(sha1
, leaf
.versions
[1].sha1
);
2722 static void sendreport(const char *buf
, unsigned long size
)
2724 if (write_in_full(report_fd
, buf
, size
) != size
)
2725 die_errno("Write to frontend failed");
2728 static void cat_object(struct object_entry
*oe
, unsigned char sha1
[20])
2730 struct strbuf line
= STRBUF_INIT
;
2732 enum object_type type
= 0;
2736 die("Internal error: bad report_fd %d", report_fd
);
2737 if (oe
&& oe
->pack_id
!= MAX_PACK_ID
) {
2739 buf
= gfi_unpack_entry(oe
, &size
);
2741 buf
= read_sha1_file(sha1
, &type
, &size
);
2744 die("Can't read object %s", sha1_to_hex(sha1
));
2747 * Output based on batch_one_object() from cat-file.c.
2750 strbuf_reset(&line
);
2751 strbuf_addf(&line
, "%s missing\n", sha1_to_hex(sha1
));
2752 if (write_in_full(report_fd
, line
.buf
, line
.len
) != line
.len
)
2753 die_errno("Write to frontend failed 1");
2755 strbuf_reset(&line
);
2756 strbuf_addf(&line
, "%s %s %lu\n", sha1_to_hex(sha1
),
2757 typename(type
), size
);
2758 sendreport(line
.buf
, line
.len
);
2759 sendreport(buf
, size
);
2760 sendreport("\n", 1);
2765 static void parse_cat_request(void)
2768 struct object_entry
*oe
= oe
;
2769 unsigned char sha1
[20];
2770 struct tree_entry root
= {0};
2772 /* cat SP <object> */
2773 p
= command_buf
.buf
+ strlen("cat ");
2775 die("The cat command requires the report-fd feature.");
2778 oe
= find_mark(strtoumax(p
+ 1, &x
, 10));
2780 die("Invalid mark: %s", command_buf
.buf
);
2782 die("Unknown mark: %s", command_buf
.buf
);
2784 hashcpy(sha1
, oe
->idx
.sha1
);
2786 if (get_sha1_hex(p
, sha1
))
2787 die("Invalid SHA1: %s", command_buf
.buf
);
2790 oe
= find_object(sha1
);
2793 /* [ SP "<path>" ] */
2796 die("Missing space after SHA1: %s", command_buf
.buf
);
2798 /* cat <tree> "<path>" form. */
2799 hashcpy(root
.versions
[1].sha1
, sha1
);
2801 quoted_path_sha1(sha1
, &root
, p
, command_buf
.buf
);
2802 oe
= find_object(sha1
);
2805 cat_object(oe
, sha1
);
2808 static void parse_checkpoint(void)
2819 static void parse_progress(void)
2821 fwrite(command_buf
.buf
, 1, command_buf
.len
, stdout
);
2822 fputc('\n', stdout
);
2827 static char* make_fast_import_path(const char *path
)
2829 struct strbuf abs_path
= STRBUF_INIT
;
2831 if (!relative_marks_paths
|| is_absolute_path(path
))
2832 return xstrdup(path
);
2833 strbuf_addf(&abs_path
, "%s/info/fast-import/%s", get_git_dir(), path
);
2834 return strbuf_detach(&abs_path
, NULL
);
2837 static void option_import_marks(const char *marks
, int from_stream
)
2839 if (import_marks_file
) {
2841 die("Only one import-marks command allowed per stream");
2843 /* read previous mark file */
2844 if(!import_marks_file_from_stream
)
2848 import_marks_file
= make_fast_import_path(marks
);
2849 safe_create_leading_directories_const(import_marks_file
);
2850 import_marks_file_from_stream
= from_stream
;
2853 static void option_date_format(const char *fmt
)
2855 if (!strcmp(fmt
, "raw"))
2856 whenspec
= WHENSPEC_RAW
;
2857 else if (!strcmp(fmt
, "rfc2822"))
2858 whenspec
= WHENSPEC_RFC2822
;
2859 else if (!strcmp(fmt
, "now"))
2860 whenspec
= WHENSPEC_NOW
;
2862 die("unknown --date-format argument %s", fmt
);
2865 static void option_depth(const char *depth
)
2867 max_depth
= strtoul(depth
, NULL
, 0);
2868 if (max_depth
> MAX_DEPTH
)
2869 die("--depth cannot exceed %u", MAX_DEPTH
);
2872 static void option_active_branches(const char *branches
)
2874 max_active_branches
= strtoul(branches
, NULL
, 0);
2877 static void option_export_marks(const char *marks
)
2879 export_marks_file
= make_fast_import_path(marks
);
2880 safe_create_leading_directories_const(export_marks_file
);
2883 static void option_report_fd(const char *fd
)
2885 unsigned long n
= strtoul(fd
, NULL
, 0);
2886 if (n
> (unsigned long) INT_MAX
)
2887 die("--report-fd cannot exceed %d", INT_MAX
);
2888 report_fd
= (int) n
;
2891 static void option_export_pack_edges(const char *edges
)
2895 pack_edges
= fopen(edges
, "a");
2897 die_errno("Cannot open '%s'", edges
);
2900 static int parse_one_option(const char *option
)
2902 if (!prefixcmp(option
, "max-pack-size=")) {
2904 if (!git_parse_ulong(option
+ 14, &v
))
2907 warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v
);
2909 } else if (v
< 1024 * 1024) {
2910 warning("minimum max-pack-size is 1 MiB");
2914 } else if (!prefixcmp(option
, "big-file-threshold=")) {
2916 if (!git_parse_ulong(option
+ 19, &v
))
2918 big_file_threshold
= v
;
2919 } else if (!prefixcmp(option
, "depth=")) {
2920 option_depth(option
+ 6);
2921 } else if (!prefixcmp(option
, "active-branches=")) {
2922 option_active_branches(option
+ 16);
2923 } else if (!prefixcmp(option
, "export-pack-edges=")) {
2924 option_export_pack_edges(option
+ 18);
2925 } else if (!prefixcmp(option
, "quiet")) {
2927 } else if (!prefixcmp(option
, "stats")) {
2936 static int parse_one_feature(const char *feature
, int from_stream
)
2938 if (!prefixcmp(feature
, "date-format=")) {
2939 option_date_format(feature
+ 12);
2940 } else if (!prefixcmp(feature
, "import-marks=")) {
2941 option_import_marks(feature
+ 13, from_stream
);
2942 } else if (!prefixcmp(feature
, "export-marks=")) {
2943 option_export_marks(feature
+ 13);
2944 } else if (!prefixcmp(feature
, "report-fd=")) {
2945 option_report_fd(feature
+ strlen("report-fd="));
2946 } else if (!prefixcmp(feature
, "relative-marks")) {
2947 relative_marks_paths
= 1;
2948 } else if (!prefixcmp(feature
, "no-relative-marks")) {
2949 relative_marks_paths
= 0;
2950 } else if (!prefixcmp(feature
, "force")) {
2959 static void parse_feature(void)
2961 char *feature
= command_buf
.buf
+ 8;
2963 if (seen_data_command
)
2964 die("Got feature command '%s' after data command", feature
);
2966 if (parse_one_feature(feature
, 1))
2969 die("This version of fast-import does not support feature %s.", feature
);
2972 static void parse_option(void)
2974 char *option
= command_buf
.buf
+ 11;
2976 if (seen_data_command
)
2977 die("Got option command '%s' after data command", option
);
2979 if (parse_one_option(option
))
2982 die("This version of fast-import does not support option: %s", option
);
2985 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2987 if (!strcmp(k
, "pack.depth")) {
2988 max_depth
= git_config_int(k
, v
);
2989 if (max_depth
> MAX_DEPTH
)
2990 max_depth
= MAX_DEPTH
;
2993 if (!strcmp(k
, "pack.compression")) {
2994 int level
= git_config_int(k
, v
);
2996 level
= Z_DEFAULT_COMPRESSION
;
2997 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
2998 die("bad pack compression level %d", level
);
2999 pack_compression_level
= level
;
3000 pack_compression_seen
= 1;
3003 if (!strcmp(k
, "pack.indexversion")) {
3004 pack_idx_default_version
= git_config_int(k
, v
);
3005 if (pack_idx_default_version
> 2)
3006 die("bad pack.indexversion=%"PRIu32
,
3007 pack_idx_default_version
);
3010 if (!strcmp(k
, "pack.packsizelimit")) {
3011 max_packsize
= git_config_ulong(k
, v
);
3014 if (!strcmp(k
, "core.bigfilethreshold")) {
3015 long n
= git_config_int(k
, v
);
3016 big_file_threshold
= 0 < n
? n
: 0;
3018 return git_default_config(k
, v
, cb
);
3021 static const char fast_import_usage
[] =
3022 "git fast-import [--date-format=f] [--max-pack-size=n] [--big-file-threshold=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
3024 static void parse_argv(void)
3028 for (i
= 1; i
< global_argc
; i
++) {
3029 const char *a
= global_argv
[i
];
3031 if (*a
!= '-' || !strcmp(a
, "--"))
3034 if (parse_one_option(a
+ 2))
3037 if (parse_one_feature(a
+ 2, 0))
3040 die("unknown option %s", a
);
3042 if (i
!= global_argc
)
3043 usage(fast_import_usage
);
3045 seen_data_command
= 1;
3046 if (import_marks_file
)
3050 int main(int argc
, const char **argv
)
3054 git_extract_argv0_path(argv
[0]);
3056 if (argc
== 2 && !strcmp(argv
[1], "-h"))
3057 usage(fast_import_usage
);
3059 setup_git_directory();
3060 git_config(git_pack_config
, NULL
);
3061 if (!pack_compression_seen
&& core_compression_seen
)
3062 pack_compression_level
= core_compression_level
;
3064 alloc_objects(object_entry_alloc
);
3065 strbuf_init(&command_buf
, 0);
3066 atom_table
= xcalloc(atom_table_sz
, sizeof(struct atom_str
*));
3067 branch_table
= xcalloc(branch_table_sz
, sizeof(struct branch
*));
3068 avail_tree_table
= xcalloc(avail_tree_table_sz
, sizeof(struct avail_tree_content
*));
3069 marks
= pool_calloc(1, sizeof(struct mark_set
));
3074 rc_free
= pool_alloc(cmd_save
* sizeof(*rc_free
));
3075 for (i
= 0; i
< (cmd_save
- 1); i
++)
3076 rc_free
[i
].next
= &rc_free
[i
+ 1];
3077 rc_free
[cmd_save
- 1].next
= NULL
;
3079 prepare_packed_git();
3081 set_die_routine(die_nicely
);
3082 while (read_next_command() != EOF
) {
3083 if (!strcmp("blob", command_buf
.buf
))
3085 else if (!prefixcmp(command_buf
.buf
, "commit "))
3087 else if (!prefixcmp(command_buf
.buf
, "tag "))
3089 else if (!prefixcmp(command_buf
.buf
, "reset "))
3090 parse_reset_branch();
3091 else if (!strcmp("checkpoint", command_buf
.buf
))
3093 else if (!prefixcmp(command_buf
.buf
, "progress "))
3095 else if (!prefixcmp(command_buf
.buf
, "feature "))
3097 else if (!prefixcmp(command_buf
.buf
, "option git "))
3099 else if (!prefixcmp(command_buf
.buf
, "option "))
3100 /* ignore non-git options*/;
3102 die("Unsupported command: %s", command_buf
.buf
);
3105 /* argv hasn't been parsed yet, do so */
3106 if (!seen_data_command
)
3120 uintmax_t total_count
= 0, duplicate_count
= 0;
3121 for (i
= 0; i
< ARRAY_SIZE(object_count_by_type
); i
++)
3122 total_count
+= object_count_by_type
[i
];
3123 for (i
= 0; i
< ARRAY_SIZE(duplicate_count_by_type
); i
++)
3124 duplicate_count
+= duplicate_count_by_type
[i
];
3126 fprintf(stderr
, "%s statistics:\n", argv
[0]);
3127 fprintf(stderr
, "---------------------------------------------------------------------\n");
3128 fprintf(stderr
, "Alloc'd objects: %10" PRIuMAX
"\n", alloc_count
);
3129 fprintf(stderr
, "Total objects: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates )\n", total_count
, duplicate_count
);
3130 fprintf(stderr
, " blobs : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_BLOB
], duplicate_count_by_type
[OBJ_BLOB
], delta_count_by_type
[OBJ_BLOB
]);
3131 fprintf(stderr
, " trees : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_TREE
], duplicate_count_by_type
[OBJ_TREE
], delta_count_by_type
[OBJ_TREE
]);
3132 fprintf(stderr
, " commits: %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_COMMIT
], duplicate_count_by_type
[OBJ_COMMIT
], delta_count_by_type
[OBJ_COMMIT
]);
3133 fprintf(stderr
, " tags : %10" PRIuMAX
" (%10" PRIuMAX
" duplicates %10" PRIuMAX
" deltas)\n", object_count_by_type
[OBJ_TAG
], duplicate_count_by_type
[OBJ_TAG
], delta_count_by_type
[OBJ_TAG
]);
3134 fprintf(stderr
, "Total branches: %10lu (%10lu loads )\n", branch_count
, branch_load_count
);
3135 fprintf(stderr
, " marks: %10" PRIuMAX
" (%10" PRIuMAX
" unique )\n", (((uintmax_t)1) << marks
->shift
) * 1024, marks_set_count
);
3136 fprintf(stderr
, " atoms: %10u\n", atom_cnt
);
3137 fprintf(stderr
, "Memory total: %10" PRIuMAX
" KiB\n", (total_allocd
+ alloc_count
*sizeof(struct object_entry
))/1024);
3138 fprintf(stderr
, " pools: %10lu KiB\n", (unsigned long)(total_allocd
/1024));
3139 fprintf(stderr
, " objects: %10" PRIuMAX
" KiB\n", (alloc_count
*sizeof(struct object_entry
))/1024);
3140 fprintf(stderr
, "---------------------------------------------------------------------\n");
3142 fprintf(stderr
, "---------------------------------------------------------------------\n");
3143 fprintf(stderr
, "\n");
3146 return failure
? 1 : 0;