@@ -20,6 +20,8 @@ static int no_done;
2020static int fetch_fsck_objects = -1 ;
2121static int transfer_fsck_objects = -1 ;
2222static int agent_supported ;
23+ static struct lock_file shallow_lock ;
24+ static const char * alternate_shallow_file ;
2325
2426#define COMPLETE (1U << 0)
2527#define COMMON (1U << 1)
@@ -683,7 +685,7 @@ static int get_pack(struct fetch_pack_args *args,
683685 int xd [2 ], char * * pack_lockfile )
684686{
685687 struct async demux ;
686- const char * argv [20 ];
688+ const char * argv [22 ];
687689 char keep_arg [256 ];
688690 char hdr_arg [256 ];
689691 const char * * av ;
@@ -724,6 +726,11 @@ static int get_pack(struct fetch_pack_args *args,
724726 do_keep = 1 ;
725727 }
726728
729+ if (alternate_shallow_file ) {
730+ * av ++ = "--shallow-file" ;
731+ * av ++ = alternate_shallow_file ;
732+ }
733+
727734 if (do_keep ) {
728735 if (pack_lockfile )
729736 cmd .out = -1 ;
@@ -779,6 +786,27 @@ static int cmp_ref_by_name(const void *a_, const void *b_)
779786 return strcmp (a -> name , b -> name );
780787}
781788
789+ static void setup_alternate_shallow (void )
790+ {
791+ struct strbuf sb = STRBUF_INIT ;
792+ int fd ;
793+
794+ check_shallow_file_for_update ();
795+ fd = hold_lock_file_for_update (& shallow_lock , git_path ("shallow" ),
796+ LOCK_DIE_ON_ERROR );
797+ if (write_shallow_commits (& sb , 0 )) {
798+ if (write_in_full (fd , sb .buf , sb .len ) != sb .len )
799+ die_errno ("failed to write to %s" , shallow_lock .filename );
800+ alternate_shallow_file = shallow_lock .filename ;
801+ } else
802+ /*
803+ * is_repository_shallow() sees empty string as "no
804+ * shallow file".
805+ */
806+ alternate_shallow_file = "" ;
807+ strbuf_release (& sb );
808+ }
809+
782810static struct ref * do_fetch_pack (struct fetch_pack_args * args ,
783811 int fd [2 ],
784812 const struct ref * orig_ref ,
@@ -858,6 +886,8 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
858886
859887 if (args -> stateless_rpc )
860888 packet_flush (fd [1 ]);
889+ if (args -> depth > 0 )
890+ setup_alternate_shallow ();
861891 if (get_pack (args , fd , pack_lockfile ))
862892 die ("git fetch-pack: fetch failed." );
863893
@@ -936,15 +966,9 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
936966 struct ref * * sought , int nr_sought ,
937967 char * * pack_lockfile )
938968{
939- struct stat st ;
940969 struct ref * ref_cpy ;
941970
942971 fetch_pack_setup ();
943- if (args -> depth > 0 ) {
944- if (stat (git_path ("shallow" ), & st ))
945- st .st_mtime = 0 ;
946- }
947-
948972 if (nr_sought )
949973 nr_sought = remove_duplicates_in_refs (sought , nr_sought );
950974
@@ -954,35 +978,12 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
954978 }
955979 ref_cpy = do_fetch_pack (args , fd , ref , sought , nr_sought , pack_lockfile );
956980
957- if (args -> depth > 0 ) {
958- static struct lock_file lock ;
959- struct cache_time mtime ;
960- struct strbuf sb = STRBUF_INIT ;
961- char * shallow = git_path ("shallow" );
962- int fd ;
963-
964- mtime .sec = st .st_mtime ;
965- mtime .nsec = ST_MTIME_NSEC (st );
966- if (stat (shallow , & st )) {
967- if (mtime .sec )
968- die ("shallow file was removed during fetch" );
969- } else if (st .st_mtime != mtime .sec
970- #ifdef USE_NSEC
971- || ST_MTIME_NSEC (st ) != mtime .nsec
972- #endif
973- )
974- die ("shallow file was changed during fetch" );
975-
976- fd = hold_lock_file_for_update (& lock , shallow ,
977- LOCK_DIE_ON_ERROR );
978- if (!write_shallow_commits (& sb , 0 )
979- || write_in_full (fd , sb .buf , sb .len ) != sb .len ) {
980- unlink_or_warn (shallow );
981- rollback_lock_file (& lock );
982- } else {
983- commit_lock_file (& lock );
984- }
985- strbuf_release (& sb );
981+ if (alternate_shallow_file ) {
982+ if (* alternate_shallow_file == '\0' ) { /* --unshallow */
983+ unlink_or_warn (git_path ("shallow" ));
984+ rollback_lock_file (& shallow_lock );
985+ } else
986+ commit_lock_file (& shallow_lock );
986987 }
987988
988989 reprepare_packed_git ();
0 commit comments