@@ -1371,16 +1371,33 @@ static void dump_marks_helper(FILE *f,
13711371
13721372static void dump_marks (void )
13731373{
1374- if (mark_file )
1375- {
1376- FILE * f = fopen (mark_file , "w" );
1377- if (f ) {
1378- dump_marks_helper (f , 0 , marks );
1379- fclose (f );
1380- } else
1381- failure |= error ("Unable to write marks file %s: %s" ,
1382- mark_file , strerror (errno ));
1374+ static struct lock_file mark_lock ;
1375+ int mark_fd ;
1376+ FILE * f ;
1377+
1378+ if (!mark_file )
1379+ return ;
1380+
1381+ mark_fd = hold_lock_file_for_update (& mark_lock , mark_file , 0 );
1382+ if (mark_fd < 0 ) {
1383+ failure |= error ("Unable to write marks file %s: %s" ,
1384+ mark_file , strerror (errno ));
1385+ return ;
1386+ }
1387+
1388+ f = fdopen (mark_fd , "w" );
1389+ if (!f ) {
1390+ rollback_lock_file (& mark_lock );
1391+ failure |= error ("Unable to write marks file %s: %s" ,
1392+ mark_file , strerror (errno ));
1393+ return ;
13831394 }
1395+
1396+ dump_marks_helper (f , 0 , marks );
1397+ fclose (f );
1398+ if (commit_lock_file (& mark_lock ))
1399+ failure |= error ("Unable to write marks file %s: %s" ,
1400+ mark_file , strerror (errno ));
13841401}
13851402
13861403static void read_next_command (void )
@@ -1976,6 +1993,40 @@ static void cmd_checkpoint(void)
19761993 read_next_command ();
19771994}
19781995
1996+ static void import_marks (const char * input_file )
1997+ {
1998+ char line [512 ];
1999+ FILE * f = fopen (input_file , "r" );
2000+ if (!f )
2001+ die ("cannot read %s: %s" , input_file , strerror (errno ));
2002+ while (fgets (line , sizeof (line ), f )) {
2003+ uintmax_t mark ;
2004+ char * end ;
2005+ unsigned char sha1 [20 ];
2006+ struct object_entry * e ;
2007+
2008+ end = strchr (line , '\n' );
2009+ if (line [0 ] != ':' || !end )
2010+ die ("corrupt mark line: %s" , line );
2011+ * end = 0 ;
2012+ mark = strtoumax (line + 1 , & end , 10 );
2013+ if (!mark || end == line + 1
2014+ || * end != ' ' || get_sha1 (end + 1 , sha1 ))
2015+ die ("corrupt mark line: %s" , line );
2016+ e = find_object (sha1 );
2017+ if (!e ) {
2018+ enum object_type type = sha1_object_info (sha1 , NULL );
2019+ if (type < 0 )
2020+ die ("object not found: %s" , sha1_to_hex (sha1 ));
2021+ e = insert_object (sha1 );
2022+ e -> type = type ;
2023+ e -> pack_id = MAX_PACK_ID ;
2024+ }
2025+ insert_mark (mark , e );
2026+ }
2027+ fclose (f );
2028+ }
2029+
19792030static const char fast_import_usage [] =
19802031"git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]" ;
19812032
@@ -1984,6 +2035,12 @@ int main(int argc, const char **argv)
19842035 int i , show_stats = 1 ;
19852036
19862037 git_config (git_default_config );
2038+ alloc_objects (object_entry_alloc );
2039+ strbuf_init (& command_buf );
2040+ atom_table = xcalloc (atom_table_sz , sizeof (struct atom_str * ));
2041+ branch_table = xcalloc (branch_table_sz , sizeof (struct branch * ));
2042+ avail_tree_table = xcalloc (avail_tree_table_sz , sizeof (struct avail_tree_content * ));
2043+ marks = pool_calloc (1 , sizeof (struct mark_set ));
19872044
19882045 for (i = 1 ; i < argc ; i ++ ) {
19892046 const char * a = argv [i ];
@@ -2007,6 +2064,8 @@ int main(int argc, const char **argv)
20072064 max_depth = strtoul (a + 8 , NULL , 0 );
20082065 else if (!prefixcmp (a , "--active-branches=" ))
20092066 max_active_branches = strtoul (a + 18 , NULL , 0 );
2067+ else if (!prefixcmp (a , "--import-marks=" ))
2068+ import_marks (a + 15 );
20102069 else if (!prefixcmp (a , "--export-marks=" ))
20112070 mark_file = a + 15 ;
20122071 else if (!prefixcmp (a , "--export-pack-edges=" )) {
@@ -2027,14 +2086,6 @@ int main(int argc, const char **argv)
20272086 if (i != argc )
20282087 usage (fast_import_usage );
20292088
2030- alloc_objects (object_entry_alloc );
2031- strbuf_init (& command_buf );
2032-
2033- atom_table = xcalloc (atom_table_sz , sizeof (struct atom_str * ));
2034- branch_table = xcalloc (branch_table_sz , sizeof (struct branch * ));
2035- avail_tree_table = xcalloc (avail_tree_table_sz , sizeof (struct avail_tree_content * ));
2036- marks = pool_calloc (1 , sizeof (struct mark_set ));
2037-
20382089 start_packfile ();
20392090 for (;;) {
20402091 read_next_command ();
0 commit comments