3
3
*
4
4
* Copyright (C) 2006 Linus Torvalds
5
5
*/
6
-
7
6
#include "builtin.h"
8
7
#include "advice.h"
9
8
#include "config.h"
18
17
#include "preload-index.h"
19
18
#include "diff.h"
20
19
#include "read-cache.h"
21
- #include "repository.h"
22
20
#include "revision.h"
23
21
#include "bulk-checkin.h"
24
22
#include "strvec.h"
@@ -36,24 +34,27 @@ static int pathspec_file_nul;
36
34
static int include_sparse ;
37
35
static const char * pathspec_from_file ;
38
36
39
- static int chmod_pathspec (struct pathspec * pathspec , char flip , int show_only )
37
+ static int chmod_pathspec (struct repository * repo ,
38
+ struct pathspec * pathspec ,
39
+ char flip ,
40
+ int show_only )
40
41
{
41
42
int i , ret = 0 ;
42
43
43
- for (i = 0 ; i < the_repository -> index -> cache_nr ; i ++ ) {
44
- struct cache_entry * ce = the_repository -> index -> cache [i ];
44
+ for (i = 0 ; i < repo -> index -> cache_nr ; i ++ ) {
45
+ struct cache_entry * ce = repo -> index -> cache [i ];
45
46
int err ;
46
47
47
48
if (!include_sparse &&
48
49
(ce_skip_worktree (ce ) ||
49
- !path_in_sparse_checkout (ce -> name , the_repository -> index )))
50
+ !path_in_sparse_checkout (ce -> name , repo -> index )))
50
51
continue ;
51
52
52
- if (pathspec && !ce_path_match (the_repository -> index , ce , pathspec , NULL ))
53
+ if (pathspec && !ce_path_match (repo -> index , ce , pathspec , NULL ))
53
54
continue ;
54
55
55
56
if (!show_only )
56
- err = chmod_index_entry (the_repository -> index , ce , flip );
57
+ err = chmod_index_entry (repo -> index , ce , flip );
57
58
else
58
59
err = S_ISREG (ce -> ce_mode ) ? 0 : -1 ;
59
60
@@ -64,31 +65,36 @@ static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
64
65
return ret ;
65
66
}
66
67
67
- static int renormalize_tracked_files (const struct pathspec * pathspec , int flags )
68
+ static int renormalize_tracked_files (struct repository * repo ,
69
+ const struct pathspec * pathspec ,
70
+ int flags )
68
71
{
69
72
int i , retval = 0 ;
70
73
71
- for (i = 0 ; i < the_repository -> index -> cache_nr ; i ++ ) {
72
- struct cache_entry * ce = the_repository -> index -> cache [i ];
74
+ for (i = 0 ; i < repo -> index -> cache_nr ; i ++ ) {
75
+ struct cache_entry * ce = repo -> index -> cache [i ];
73
76
74
77
if (!include_sparse &&
75
78
(ce_skip_worktree (ce ) ||
76
- !path_in_sparse_checkout (ce -> name , the_repository -> index )))
79
+ !path_in_sparse_checkout (ce -> name , repo -> index )))
77
80
continue ;
78
81
if (ce_stage (ce ))
79
82
continue ; /* do not touch unmerged paths */
80
83
if (!S_ISREG (ce -> ce_mode ) && !S_ISLNK (ce -> ce_mode ))
81
84
continue ; /* do not touch non blobs */
82
- if (pathspec && !ce_path_match (the_repository -> index , ce , pathspec , NULL ))
85
+ if (pathspec && !ce_path_match (repo -> index , ce , pathspec , NULL ))
83
86
continue ;
84
- retval |= add_file_to_index (the_repository -> index , ce -> name ,
87
+ retval |= add_file_to_index (repo -> index , ce -> name ,
85
88
flags | ADD_CACHE_RENORMALIZE );
86
89
}
87
90
88
91
return retval ;
89
92
}
90
93
91
- static char * prune_directory (struct dir_struct * dir , struct pathspec * pathspec , int prefix )
94
+ static char * prune_directory (struct repository * repo ,
95
+ struct dir_struct * dir ,
96
+ struct pathspec * pathspec ,
97
+ int prefix )
92
98
{
93
99
char * seen ;
94
100
int i ;
@@ -100,16 +106,16 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
100
106
i = dir -> nr ;
101
107
while (-- i >= 0 ) {
102
108
struct dir_entry * entry = * src ++ ;
103
- if (dir_path_match (the_repository -> index , entry , pathspec , prefix , seen ))
109
+ if (dir_path_match (repo -> index , entry , pathspec , prefix , seen ))
104
110
* dst ++ = entry ;
105
111
}
106
112
dir -> nr = dst - dir -> entries ;
107
- add_pathspec_matches_against_index (pathspec , the_repository -> index , seen ,
113
+ add_pathspec_matches_against_index (pathspec , repo -> index , seen ,
108
114
PS_IGNORE_SKIP_WORKTREE );
109
115
return seen ;
110
116
}
111
117
112
- static int refresh (int verbose , const struct pathspec * pathspec )
118
+ static int refresh (struct repository * repo , int verbose , const struct pathspec * pathspec )
113
119
{
114
120
char * seen ;
115
121
int i , ret = 0 ;
@@ -119,14 +125,14 @@ static int refresh(int verbose, const struct pathspec *pathspec)
119
125
(verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET );
120
126
121
127
seen = xcalloc (pathspec -> nr , 1 );
122
- refresh_index (the_repository -> index , flags , pathspec , seen ,
128
+ refresh_index (repo -> index , flags , pathspec , seen ,
123
129
_ ("Unstaged changes after refreshing the index:" ));
124
130
for (i = 0 ; i < pathspec -> nr ; i ++ ) {
125
131
if (!seen [i ]) {
126
132
const char * path = pathspec -> items [i ].original ;
127
133
128
134
if (matches_skip_worktree (pathspec , i , & skip_worktree_seen ) ||
129
- !path_in_sparse_checkout (path , the_repository -> index )) {
135
+ !path_in_sparse_checkout (path , repo -> index )) {
130
136
string_list_append (& only_match_skip_worktree ,
131
137
pathspec -> items [i ].original );
132
138
} else {
@@ -147,7 +153,10 @@ static int refresh(int verbose, const struct pathspec *pathspec)
147
153
return ret ;
148
154
}
149
155
150
- int interactive_add (const char * * argv , const char * prefix , int patch )
156
+ int interactive_add (struct repository * repo ,
157
+ const char * * argv ,
158
+ const char * prefix ,
159
+ int patch )
151
160
{
152
161
struct pathspec pathspec ;
153
162
int ret ;
@@ -159,28 +168,31 @@ int interactive_add(const char **argv, const char *prefix, int patch)
159
168
prefix , argv );
160
169
161
170
if (patch )
162
- ret = !!run_add_p (the_repository , ADD_P_ADD , NULL , & pathspec );
171
+ ret = !!run_add_p (repo , ADD_P_ADD , NULL , & pathspec );
163
172
else
164
- ret = !!run_add_i (the_repository , & pathspec );
173
+ ret = !!run_add_i (repo , & pathspec );
165
174
166
175
clear_pathspec (& pathspec );
167
176
return ret ;
168
177
}
169
178
170
- static int edit_patch (int argc , const char * * argv , const char * prefix )
179
+ static int edit_patch (struct repository * repo ,
180
+ int argc ,
181
+ const char * * argv ,
182
+ const char * prefix )
171
183
{
172
- char * file = git_pathdup ( "ADD_EDIT.patch" );
184
+ char * file = repo_git_path ( repo , "ADD_EDIT.patch" );
173
185
struct child_process child = CHILD_PROCESS_INIT ;
174
186
struct rev_info rev ;
175
187
int out ;
176
188
struct stat st ;
177
189
178
- git_config ( git_diff_basic_config , NULL ); /* no "diff" UI options */
190
+ repo_config ( repo , git_diff_basic_config , NULL );
179
191
180
- if (repo_read_index (the_repository ) < 0 )
192
+ if (repo_read_index (repo ) < 0 )
181
193
die (_ ("could not read the index" ));
182
194
183
- repo_init_revisions (the_repository , & rev , prefix );
195
+ repo_init_revisions (repo , & rev , prefix );
184
196
rev .diffopt .context = 7 ;
185
197
186
198
argc = setup_revisions (argc , argv , & rev , NULL );
@@ -318,7 +330,7 @@ static void check_embedded_repo(const char *path)
318
330
strbuf_release (& name );
319
331
}
320
332
321
- static int add_files (struct dir_struct * dir , int flags )
333
+ static int add_files (struct repository * repo , struct dir_struct * dir , int flags )
322
334
{
323
335
int i , exit_status = 0 ;
324
336
struct string_list matched_sparse_paths = STRING_LIST_INIT_NODUP ;
@@ -334,12 +346,12 @@ static int add_files(struct dir_struct *dir, int flags)
334
346
335
347
for (i = 0 ; i < dir -> nr ; i ++ ) {
336
348
if (!include_sparse &&
337
- !path_in_sparse_checkout (dir -> entries [i ]-> name , the_repository -> index )) {
349
+ !path_in_sparse_checkout (dir -> entries [i ]-> name , repo -> index )) {
338
350
string_list_append (& matched_sparse_paths ,
339
351
dir -> entries [i ]-> name );
340
352
continue ;
341
353
}
342
- if (add_file_to_index (the_repository -> index , dir -> entries [i ]-> name , flags )) {
354
+ if (add_file_to_index (repo -> index , dir -> entries [i ]-> name , flags )) {
343
355
if (!ignore_add_errors )
344
356
die (_ ("adding files failed" ));
345
357
exit_status = 1 ;
@@ -358,7 +370,10 @@ static int add_files(struct dir_struct *dir, int flags)
358
370
return exit_status ;
359
371
}
360
372
361
- int cmd_add (int argc , const char * * argv , const char * prefix )
373
+ int cmd_add (int argc ,
374
+ const char * * argv ,
375
+ const char * prefix ,
376
+ struct repository * repo )
362
377
{
363
378
int exit_status = 0 ;
364
379
struct pathspec pathspec ;
@@ -370,7 +385,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
370
385
char * ps_matched = NULL ;
371
386
struct lock_file lock_file = LOCK_INIT ;
372
387
373
- git_config ( add_config , NULL );
388
+ repo_config ( repo , add_config , NULL );
374
389
375
390
argc = parse_options (argc , argv , prefix , builtin_add_options ,
376
391
builtin_add_usage , PARSE_OPT_KEEP_ARGV0 );
@@ -381,13 +396,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
381
396
die (_ ("options '%s' and '%s' cannot be used together" ), "--dry-run" , "--interactive/--patch" );
382
397
if (pathspec_from_file )
383
398
die (_ ("options '%s' and '%s' cannot be used together" ), "--pathspec-from-file" , "--interactive/--patch" );
384
- exit (interactive_add (argv + 1 , prefix , patch_interactive ));
399
+ exit (interactive_add (repo , argv + 1 , prefix , patch_interactive ));
385
400
}
386
401
387
402
if (edit_interactive ) {
388
403
if (pathspec_from_file )
389
404
die (_ ("options '%s' and '%s' cannot be used together" ), "--pathspec-from-file" , "--edit" );
390
- return (edit_patch (argc , argv , prefix ));
405
+ return (edit_patch (repo , argc , argv , prefix ));
391
406
}
392
407
argc -- ;
393
408
argv ++ ;
@@ -410,10 +425,10 @@ int cmd_add(int argc, const char **argv, const char *prefix)
410
425
add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize ;
411
426
require_pathspec = !(take_worktree_changes || (0 < addremove_explicit ));
412
427
413
- prepare_repo_settings (the_repository );
414
- the_repository -> settings .command_requires_full_index = 0 ;
428
+ prepare_repo_settings (repo );
429
+ repo -> settings .command_requires_full_index = 0 ;
415
430
416
- repo_hold_locked_index (the_repository , & lock_file , LOCK_DIE_ON_ERROR );
431
+ repo_hold_locked_index (repo , & lock_file , LOCK_DIE_ON_ERROR );
417
432
418
433
/*
419
434
* Check the "pathspec '%s' did not match any files" block
@@ -454,11 +469,11 @@ int cmd_add(int argc, const char **argv, const char *prefix)
454
469
(!(addremove || take_worktree_changes )
455
470
? ADD_CACHE_IGNORE_REMOVAL : 0 ));
456
471
457
- if (repo_read_index_preload (the_repository , & pathspec , 0 ) < 0 )
472
+ if (repo_read_index_preload (repo , & pathspec , 0 ) < 0 )
458
473
die (_ ("index file corrupt" ));
459
474
460
- die_in_unpopulated_submodule (the_repository -> index , prefix );
461
- die_path_inside_submodule (the_repository -> index , & pathspec );
475
+ die_in_unpopulated_submodule (repo -> index , prefix );
476
+ die_path_inside_submodule (repo -> index , & pathspec );
462
477
463
478
if (add_new_files ) {
464
479
int baselen ;
@@ -470,13 +485,13 @@ int cmd_add(int argc, const char **argv, const char *prefix)
470
485
}
471
486
472
487
/* This picks up the paths that are not tracked */
473
- baselen = fill_directory (& dir , the_repository -> index , & pathspec );
488
+ baselen = fill_directory (& dir , repo -> index , & pathspec );
474
489
if (pathspec .nr )
475
- seen = prune_directory (& dir , & pathspec , baselen );
490
+ seen = prune_directory (repo , & dir , & pathspec , baselen );
476
491
}
477
492
478
493
if (refresh_only ) {
479
- exit_status |= refresh (verbose , & pathspec );
494
+ exit_status |= refresh (repo , verbose , & pathspec );
480
495
goto finish ;
481
496
}
482
497
@@ -487,7 +502,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
487
502
488
503
if (!seen )
489
504
seen = find_pathspecs_matching_against_index (& pathspec ,
490
- the_repository -> index , PS_IGNORE_SKIP_WORKTREE );
505
+ repo -> index , PS_IGNORE_SKIP_WORKTREE );
491
506
492
507
/*
493
508
* file_exists() assumes exact match
@@ -523,8 +538,8 @@ int cmd_add(int argc, const char **argv, const char *prefix)
523
538
!file_exists (path )) {
524
539
if (ignore_missing ) {
525
540
int dtype = DT_UNKNOWN ;
526
- if (is_excluded (& dir , the_repository -> index , path , & dtype ))
527
- dir_add_ignored (& dir , the_repository -> index ,
541
+ if (is_excluded (& dir , repo -> index , path , & dtype ))
542
+ dir_add_ignored (& dir , repo -> index ,
528
543
path , pathspec .items [i ].len );
529
544
} else
530
545
die (_ ("pathspec '%s' did not match any files" ),
@@ -547,9 +562,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)
547
562
548
563
ps_matched = xcalloc (pathspec .nr , 1 );
549
564
if (add_renormalize )
550
- exit_status |= renormalize_tracked_files (& pathspec , flags );
565
+ exit_status |= renormalize_tracked_files (repo , & pathspec , flags );
551
566
else
552
- exit_status |= add_files_to_cache (the_repository , prefix ,
567
+ exit_status |= add_files_to_cache (repo , prefix ,
553
568
& pathspec , ps_matched ,
554
569
include_sparse , flags );
555
570
@@ -558,14 +573,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
558
573
exit (128 );
559
574
560
575
if (add_new_files )
561
- exit_status |= add_files (& dir , flags );
576
+ exit_status |= add_files (repo , & dir , flags );
562
577
563
578
if (chmod_arg && pathspec .nr )
564
- exit_status |= chmod_pathspec (& pathspec , chmod_arg [0 ], show_only );
579
+ exit_status |= chmod_pathspec (repo , & pathspec , chmod_arg [0 ], show_only );
565
580
end_odb_transaction ();
566
581
567
582
finish :
568
- if (write_locked_index (the_repository -> index , & lock_file ,
583
+ if (write_locked_index (repo -> index , & lock_file ,
569
584
COMMIT_LOCK | SKIP_IF_UNCHANGED ))
570
585
die (_ ("unable to write new index file" ));
571
586
0 commit comments