13
13
#include "diff.h"
14
14
#include "diffcore.h"
15
15
#include "revision.h"
16
+ #include "blob.h"
16
17
17
18
/* Index extensions.
18
19
*
@@ -511,6 +512,14 @@ static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_
511
512
return new ;
512
513
}
513
514
515
+ static void record_intent_to_add (struct cache_entry * ce )
516
+ {
517
+ unsigned char sha1 [20 ];
518
+ if (write_sha1_file ("" , 0 , blob_type , sha1 ))
519
+ die ("cannot create an empty blob in the object database" );
520
+ hashcpy (ce -> sha1 , sha1 );
521
+ }
522
+
514
523
int add_to_index (struct index_state * istate , const char * path , struct stat * st , int flags )
515
524
{
516
525
int size , namelen , was_same ;
@@ -519,6 +528,9 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
519
528
unsigned ce_option = CE_MATCH_IGNORE_VALID |CE_MATCH_RACY_IS_DIRTY ;
520
529
int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND );
521
530
int pretend = flags & ADD_CACHE_PRETEND ;
531
+ int intent_only = flags & ADD_CACHE_INTENT ;
532
+ int add_option = (ADD_CACHE_OK_TO_ADD |ADD_CACHE_OK_TO_REPLACE |
533
+ (intent_only ? ADD_CACHE_NEW_ONLY : 0 ));
522
534
523
535
if (!S_ISREG (st_mode ) && !S_ISLNK (st_mode ) && !S_ISDIR (st_mode ))
524
536
return error ("%s: can only add regular files, symbolic links or git-directories" , path );
@@ -532,7 +544,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
532
544
ce = xcalloc (1 , size );
533
545
memcpy (ce -> name , path , namelen );
534
546
ce -> ce_flags = namelen ;
535
- fill_stat_cache_info (ce , st );
547
+ if (!intent_only )
548
+ fill_stat_cache_info (ce , st );
536
549
537
550
if (trust_executable_bit && has_symlinks )
538
551
ce -> ce_mode = create_ce_mode (st_mode );
@@ -555,8 +568,12 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
555
568
alias -> ce_flags |= CE_ADDED ;
556
569
return 0 ;
557
570
}
558
- if (index_path (ce -> sha1 , path , st , 1 ))
559
- return error ("unable to index file %s" , path );
571
+ if (!intent_only ) {
572
+ if (index_path (ce -> sha1 , path , st , 1 ))
573
+ return error ("unable to index file %s" , path );
574
+ } else
575
+ record_intent_to_add (ce );
576
+
560
577
if (ignore_case && alias && different_name (ce , alias ))
561
578
ce = create_alias_ce (ce , alias );
562
579
ce -> ce_flags |= CE_ADDED ;
@@ -569,7 +586,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
569
586
570
587
if (pretend )
571
588
;
572
- else if (add_index_entry (istate , ce , ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE ))
589
+ else if (add_index_entry (istate , ce , add_option ))
573
590
return error ("unable to add %s to index" ,path );
574
591
if (verbose && !was_same )
575
592
printf ("add '%s'\n" , path );
@@ -848,13 +865,15 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
848
865
int ok_to_add = option & ADD_CACHE_OK_TO_ADD ;
849
866
int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE ;
850
867
int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK ;
868
+ int new_only = option & ADD_CACHE_NEW_ONLY ;
851
869
852
870
cache_tree_invalidate_path (istate -> cache_tree , ce -> name );
853
871
pos = index_name_pos (istate , ce -> name , ce -> ce_flags );
854
872
855
873
/* existing match? Just replace it. */
856
874
if (pos >= 0 ) {
857
- replace_index_entry (istate , pos , ce );
875
+ if (!new_only )
876
+ replace_index_entry (istate , pos , ce );
858
877
return 0 ;
859
878
}
860
879
pos = - pos - 1 ;
0 commit comments