7
7
#include "refs.h"
8
8
#include "string-list.h"
9
9
#include "lockfile.h"
10
+ #include "dir.h"
10
11
11
12
struct add_i_state {
12
13
struct repository * r ;
@@ -559,6 +560,7 @@ static int is_valid_prefix(const char *prefix, size_t prefix_len)
559
560
struct print_file_item_data {
560
561
const char * modified_fmt , * color , * reset ;
561
562
struct strbuf buf , name , index , worktree ;
563
+ unsigned only_names :1 ;
562
564
};
563
565
564
566
static void print_file_item (int i , int selected , struct string_list_item * item ,
@@ -582,6 +584,12 @@ static void print_file_item(int i, int selected, struct string_list_item *item,
582
584
highlighted = d -> name .buf ;
583
585
}
584
586
587
+ if (d -> only_names ) {
588
+ printf ("%c%2d: %s" , selected ? '*' : ' ' , i + 1 ,
589
+ highlighted ? highlighted : item -> string );
590
+ return ;
591
+ }
592
+
585
593
render_adddel (& d -> worktree , & c -> worktree , _ ("nothing" ));
586
594
render_adddel (& d -> index , & c -> index , _ ("unchanged" ));
587
595
@@ -761,6 +769,88 @@ static int run_revert(struct add_i_state *s, const struct pathspec *ps,
761
769
return res ;
762
770
}
763
771
772
+ static int get_untracked_files (struct repository * r ,
773
+ struct prefix_item_list * files ,
774
+ const struct pathspec * ps )
775
+ {
776
+ struct dir_struct dir = { 0 };
777
+ size_t i ;
778
+ struct strbuf buf = STRBUF_INIT ;
779
+
780
+ if (repo_read_index (r ) < 0 )
781
+ return error (_ ("could not read index" ));
782
+
783
+ prefix_item_list_clear (files );
784
+ setup_standard_excludes (& dir );
785
+ add_pattern_list (& dir , EXC_CMDL , "--exclude option" );
786
+ fill_directory (& dir , r -> index , ps );
787
+
788
+ for (i = 0 ; i < dir .nr ; i ++ ) {
789
+ struct dir_entry * ent = dir .entries [i ];
790
+
791
+ if (index_name_is_other (r -> index , ent -> name , ent -> len )) {
792
+ strbuf_reset (& buf );
793
+ strbuf_add (& buf , ent -> name , ent -> len );
794
+ add_file_item (& files -> items , buf .buf );
795
+ }
796
+ }
797
+
798
+ strbuf_release (& buf );
799
+ return 0 ;
800
+ }
801
+
802
+ static int run_add_untracked (struct add_i_state * s , const struct pathspec * ps ,
803
+ struct prefix_item_list * files ,
804
+ struct list_and_choose_options * opts )
805
+ {
806
+ struct print_file_item_data * d = opts -> list_opts .print_item_data ;
807
+ int res = 0 , fd ;
808
+ size_t count , i ;
809
+ struct lock_file index_lock ;
810
+
811
+ if (get_untracked_files (s -> r , files , ps ) < 0 )
812
+ return -1 ;
813
+
814
+ if (!files -> items .nr ) {
815
+ printf (_ ("No untracked files.\n" ));
816
+ goto finish_add_untracked ;
817
+ }
818
+
819
+ opts -> prompt = N_ ("Add untracked" );
820
+ d -> only_names = 1 ;
821
+ count = list_and_choose (s , files , opts );
822
+ d -> only_names = 0 ;
823
+ if (count <= 0 )
824
+ goto finish_add_untracked ;
825
+
826
+ fd = repo_hold_locked_index (s -> r , & index_lock , LOCK_REPORT_ON_ERROR );
827
+ if (fd < 0 ) {
828
+ res = -1 ;
829
+ goto finish_add_untracked ;
830
+ }
831
+
832
+ for (i = 0 ; i < files -> items .nr ; i ++ ) {
833
+ const char * name = files -> items .items [i ].string ;
834
+ if (files -> selected [i ] &&
835
+ add_file_to_index (s -> r -> index , name , 0 ) < 0 ) {
836
+ res = error (_ ("could not stage '%s'" ), name );
837
+ break ;
838
+ }
839
+ }
840
+
841
+ if (!res &&
842
+ write_locked_index (s -> r -> index , & index_lock , COMMIT_LOCK ) < 0 )
843
+ res = error (_ ("could not write index" ));
844
+
845
+ if (!res )
846
+ printf (Q_ ("added %d path\n" ,
847
+ "added %d paths\n" , count ), (int )count );
848
+
849
+ finish_add_untracked :
850
+ putchar ('\n' );
851
+ return res ;
852
+ }
853
+
764
854
static int run_help (struct add_i_state * s , const struct pathspec * unused_ps ,
765
855
struct prefix_item_list * unused_files ,
766
856
struct list_and_choose_options * unused_opts )
@@ -857,6 +947,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
857
947
{ "status" , run_status },
858
948
{ "update" , run_update },
859
949
{ "revert" , run_revert },
950
+ { "add untracked" , run_add_untracked },
860
951
{ "help" , run_help },
861
952
};
862
953
struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT ;
0 commit comments