builtin: replace the_repository parameter in is_bare_repository()
PATCH 1/1 builtin: replace the_repository parameter in is_bare_repository()
Many functions receive a struct repository *repo parameter but do not use it (marked UNUSED) and instead pass the global the_repository parameter to the is_bare_repository() function.
Replace the argument passed to is_bare_repository() from the global the_repository parameter to take the unused repository parameter so that we no longer depend on the_repository. Patch covers the sites that were not updated previously.
The patch leaves some instances of this case that would require additional changes rather than simply replacing the arguments passed to the function call.
- Only update sites that do not introduce any functional changes.
- UNUSED is dropped from *repo parameter to match the changes.
--- builtin/blame.c | 4 ++-- builtin/gc.c | 4 ++-- builtin/repo.c | 4 ++-- builtin/reset.c | 4 ++-- transport.c | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-)
builtin/blame.c +2 -2
@@ -957,7 +957,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,int cmd_blame (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct rev_info revs;char *path =NULL ;
@@ -1187,7 +1187,7 @@ int cmd_blame(int argc, revs.disable_stdin =1 ;setup_revisions (argc, argv, &revs,NULL ); -if (!revs.pending.nr &&is_bare_repository (the_repository)) { +if (!revs.pending.nr &&is_bare_repository (repo)) {struct commit *head_commit;struct object_id head_oid;
builtin/gc.c +2 -2
@@ -582,7 +582,7 @@ static int maintenance_task_odb(struct maintenance_run_opts *opts,int cmd_gc (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int aggressive =0 ;int force =0 ;
@@ -637,7 +637,7 @@ int cmd_gc(int argc,die (_ ("failed to parse gc.logExpiry value %s" ), cfg.gc_log_expire);if (cfg.pack_refs <0 ) - cfg.pack_refs = !is_bare_repository (the_repository); + cfg.pack_refs = !is_bare_repository (repo); argc =parse_options (argc, argv, prefix, builtin_gc_options, builtin_gc_usage,0 );
builtin/repo.c +2 -2
@@ -58,9 +58,9 @@ struct repo_info_field {get_value_fn *get_value; }; -static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf) +static int get_layout_bare (struct repository *repo,struct strbuf *buf) { -strbuf_addstr (buf,is_bare_repository (the_repository) ?"true" :"false" ); +strbuf_addstr (buf,is_bare_repository (repo) ?"true" :"false" );return 0 ; }
builtin/reset.c +2 -2
@@ -336,7 +336,7 @@ static int git_reset_config(const char *var, const char *value,int cmd_reset (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int reset_type =NONE , update_ref_status =0 , quiet =0 ;int no_refresh =0 ;
@@ -470,7 +470,7 @@ int cmd_reset(int argc,if (reset_type !=SOFT && (reset_type !=MIXED ||repo_get_work_tree (the_repository)))setup_work_tree (the_repository); -if (reset_type ==MIXED &&is_bare_repository (the_repository)) +if (reset_type ==MIXED &&is_bare_repository (repo))die (_ ("%s reset is not allowed in a bare repository" ),_ (reset_type_names[reset_type]));
transport.c +2 -2
@@ -1528,7 +1528,7 @@ int transport_push(struct repository *r,if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |TRANSPORT_RECURSE_SUBMODULES_ONLY )) && - !is_bare_repository (the_repository)) { + !is_bare_repository (r)) {struct ref *ref = remote_refs;struct oid_array commits =OID_ARRAY_INIT ;
@@ -1555,7 +1555,7 @@ int transport_push(struct repository *r,if (((flags &TRANSPORT_RECURSE_SUBMODULES_CHECK ) || ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |TRANSPORT_RECURSE_SUBMODULES_ONLY )) && - !pretend)) && !is_bare_repository (the_repository)) { + !pretend)) && !is_bare_repository (r)) {struct ref *ref = remote_refs;struct string_list needs_pushing =STRING_LIST_INIT_DUP ;struct oid_array commits =OID_ARRAY_INIT ;
---
base-commit: f78ce2f7b6df702f93d40b85d6bda92a3f65da79
change-id: 20260827-env-is_bare_repo-564917c2d3ab
Quoted 5 lines · Hardik Kumar
The patch leaves some instances of this case that would require additional changes rather than simply replacing the arguments passed to the function call.
- Only update sites that do not introduce any functional changes.
How did you make sure that the places touched by this patch do not?
How do you deal with repo==NULL case in these code paths?
Quoted 119 lines
-
UNUSEDis dropped from*repoparameter to match the changes.Signed-off-by Hardik Kumar <[email protected]>--- builtin/blame.c | 4 ++-- builtin/gc.c | 4 ++-- builtin/repo.c | 4 ++-- builtin/reset.c | 4 ++-- transport.c | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-)builtin/blame.c +2
-2@@ -957,7 +957,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,int cmd_blame (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct rev_info revs;char *path =NULL ;@@ -1187,7 +1187,7 @@ int cmd_blame(int argc, revs.disable_stdin =1 ;setup_revisions (argc, argv, &revs,NULL ); -if (!revs.pending.nr &&is_bare_repository (the_repository)) { +if (!revs.pending.nr &&is_bare_repository (repo)) {struct commit *head_commit;struct object_id head_oid;builtin/gc.c +2
-2@@ -582,7 +582,7 @@ static int maintenance_task_odb(struct maintenance_run_opts *opts,int cmd_gc (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int aggressive =0 ;int force =0 ;@@ -637,7 +637,7 @@ int cmd_gc(int argc,die (_ ("failed to parse gc.logExpiry value %s" ), cfg.gc_log_expire);if (cfg.pack_refs <0 ) - cfg.pack_refs = !is_bare_repository (the_repository); + cfg.pack_refs = !is_bare_repository (repo); argc =parse_options (argc, argv, prefix, builtin_gc_options, builtin_gc_usage,0 );builtin/repo.c +2
-2@@ -58,9 +58,9 @@ struct repo_info_field {get_value_fn *get_value; }; -static int get_layout_bare(struct repository *repo UNUSED, struct strbuf *buf) +static int get_layout_bare (struct repository *repo,struct strbuf *buf) { -strbuf_addstr (buf,is_bare_repository (the_repository) ?"true" :"false" ); +strbuf_addstr (buf,is_bare_repository (repo) ?"true" :"false" );return 0 ; }builtin/reset.c +2
-2@@ -336,7 +336,7 @@ static int git_reset_config(const char *var, const char *value,int cmd_reset (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {int reset_type =NONE , update_ref_status =0 , quiet =0 ;int no_refresh =0 ;@@ -470,7 +470,7 @@ int cmd_reset(int argc,if (reset_type !=SOFT && (reset_type !=MIXED ||repo_get_work_tree (the_repository)))setup_work_tree (the_repository); -if (reset_type ==MIXED &&is_bare_repository (the_repository)) +if (reset_type ==MIXED &&is_bare_repository (repo))die (_ ("%s reset is not allowed in a bare repository" ),_ (reset_type_names[reset_type]));transport.c +2
-3@@ -1528,7 +1528,7 @@ int transport_push(struct repository *r,if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |TRANSPORT_RECURSE_SUBMODULES_ONLY )) && - !is_bare_repository (the_repository)) { + !is_bare_repository (r)) {struct ref *ref = remote_refs;struct oid_array commits =OID_ARRAY_INIT ;@@ -1555,7 +1555,7 @@ int transport_push(struct repository *r,if (((flags &TRANSPORT_RECURSE_SUBMODULES_CHECK ) || ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |TRANSPORT_RECURSE_SUBMODULES_ONLY )) && - !pretend)) && !is_bare_repository (the_repository)) { + !pretend)) && !is_bare_repository (r)) {struct ref *ref = remote_refs;struct string_list needs_pushing =STRING_LIST_INIT_DUP ;struct oid_array commits =OID_ARRAY_INIT ; ---base-commit:
f78ce2f7b6df702f93d40b85d6bda92a3f65da79
change-id: 20260827-env-is_bare_repo-564917c2d3ab
Quoted 5 lines · Junio C Hamano
Quoted 1 line
- Only update sites that do not introduce any functional changes.
How did you make sure that the places touched by this patch do not?
How do you deal with repo==NULL case in these code paths?
I guess this was a bit too short, so let me explain in a bit more detail.
Quoted 15 lines
Quoted 15 lines
builtin/blame.c +1
-1@@ -957,7 +957,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,int cmd_blame (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct rev_info revs;char *path =NULL ;@@ -1187,7 +1187,7 @@ int cmd_blame(int argc,
Quoted 6 lines
builtin/blame.c int cmd_blame(int argc, · line 1192
Quoted 6 lines
builtin/blame.c +1
-1revs.disable_stdin =1 ;setup_revisions (argc, argv, &revs,NULL ); -if (!revs.pending.nr &&is_bare_repository (the_repository)) { +if (!revs.pending.nr &&is_bare_repository (repo)) {struct commit *head_commit;struct object_id head_oid;
There are a handful of uses of the_repository before the execution reaches here. But you left them unmodified.
The original code used to consistently used the_repository. Here you changed it to use "repo". In practice, they are most likely the same when "repo" is not NULL, so in that sense, this may not be breaking anything, but you must ask yourself what the point is, unless you convert all uses of the_repository with "repo". It does not help libification effort at all.
In general, builtin/foo.c::cmd_foo() are concrete programs that work on specific repository (i.e., the_repository), and there is not much reason to rewrite the use of the_repository to use "repo" given by the caller which is git potty. You'd also need to deal with the case where "repo" is NULL (hint: "cd / && git foo -h").
They are quite different from other parts of the system, things outside builtin/, many of which are general utility/helper routines, many of which should be designed to work with given repository.
Quoted 28 lines · Junio C Hamano
I guess this was a bit too short, so let me explain in a bit more detail.
Quoted 15 lines
Quoted 15 lines
builtin/blame.c +1
-1@@ -957,7 +957,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,int cmd_blame (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) +struct repository *repo) {struct rev_info revs;char *path =NULL ;@@ -1187,7 +1187,7 @@ int cmd_blame(int argc,Quoted 6 lines
builtin/blame.c int cmd_blame(int argc, · line 1192
Quoted 6 lines
builtin/blame.c +1
-1revs.disable_stdin =1 ;setup_revisions (argc, argv, &revs,NULL ); -if (!revs.pending.nr &&is_bare_repository (the_repository)) { +if (!revs.pending.nr &&is_bare_repository (repo)) {struct commit *head_commit;struct object_id head_oid;There are a handful of uses of
the_repositorybefore the execution reaches here. But you left them unmodified.
Quoted 6 lines
The original code used to consistently used
the_repository. Here you changed it to use "repo". In practice, they are most likely the same when "repo" is not NULL, so in that sense, this may not be breaking anything, but you must ask yourself what the point is, unless you convert all uses ofthe_repositorywith "repo". It does not help libification effort at all.
My main goal here was to start small with by removing the dependence from the_repository. The other instances are obivous left out which makes the patch seem inconsistent but I suppose those functions are the ones that require execution before the flow ever checks for the value of repo and so they rely on the global one instead.
Quoted 5 lines
In general, builtin/foo.c::cmd_foo() are concrete programs that work on specific repository (i.e.,
the_repository), and there is not much reason to rewrite the use ofthe_repositoryto use "repo" given by the caller which is git potty. You'd also need to deal with the case where "repo" is NULL (hint: "cd / && git foo -h").
Right, but would safety check be required for single instance or better to find and work on only the specific ones which could lead to an exception.
Quoted 4 lines
They are quite different from other parts of the system, things outside builtin/, many of which are general utility/helper routines, many of which should be designed to work with given repository.
Quoted 9 lines · Hardik Kumar
Quoted 5 lines
In general, builtin/foo.c::cmd_foo() are concrete programs that work on specific repository (i.e.,
the_repository), and there is not much reason to rewrite the use ofthe_repositoryto use "repo" given by the caller which is git potty. You'd also need to deal with the case where "repo" is NULL (hint: "cd / && git foo -h").Right, but would safety check be required for single instance or better to find and work on only the specific ones which could lead to an exception.
I do not quite get what you mean. In a single function you use the_repository and repo interchangeably, relying on repo, when it is not NULL), being the same as the_repository for correctness. If they ever refer to different things, then your updated code is less consistent than the original, which would not be an improvement.
I actually think a good medium size project is to fix the last parameter given to cmd_foo() built-in command implementations that is a pointer to "struct repository". It was a misguided design.
This pointer is either NULL or the_repository; it never takes any other value. It is misleading and invites confusion that these functions can take arbitrary repository instance. It also invites people to replace all references in cmd_foo() to "the_repository" with "repo", which may lead to an error. Depending on where parse_options() call appears in the cmd_foo(), "cd / && git foo -h" will cause segfaults with such a change.
If we change the function signature of cmd_foo() to receive a "bool" that says "true" if the command was run inside a repository and "false" otherwise, cmd_foo() can still tell if the command was run outside a repository, and programmers will not be misled to use anything other than "the_repository" as the repository to work on.
The utility functions builtin/foo.c borrows from outside builtin/ directory are being "libified" to reduce the hardcoded dependence on the_repository, and cmd_foo() can call these functions with the_repository as a parameter. But we have no reason to waste our time updating (and also reviewing patches that make such updates) the built-in implementations themselves to take a pointer to an arbitrary repository.
Quoted 10 lines · Junio C Hamano
Quoted 5 lines · "Hardik Kumar
Quoted 5 lines
In general, builtin/foo.c::cmd_foo() are concrete programs that work on specific repository (i.e.,
the_repository), and there is not much reason to rewrite the use ofthe_repositoryto use "repo" given by the caller which is git potty. You'd also need to deal with the case where "repo" is NULL (hint: "cd / && git foo -h").
[snip]
Quoted 7 lines
The utility functions
builtin/foo.cborrows from outside builtin/ directory are being "libified" to reduce the hardcoded dependence onthe_repository, andcmd_foo()can call these functions withthe_repositoryas a parameter. But we have no reason to waste our time updating (and also reviewing patches that make such updates) the built-in implementations themselves to take a pointer to an arbitrary repository.
Hm. What if a program wants to do « exactly what ‘git switch’ does » sans shelling out? It’d be a maintenance burden to answer « open code it using helpers in libgit and the builtin sources », at least as presented. Many builtin sources seem (at least to me) quite complex! And it’s not immediately obvious how I could recreate the intended effects of, say, « switch branches » with just one or 2 libgit calls. (Maybe that’s just me.)
In other words, the stance « it’s not worth it to libify builtins » only makes sense if we also say « we don’t explicitly support precisely replicating builtin behavior via libgit ».
Which is fine! I’m probably just not aware if there was discussion/consensus on that somewhere. Pointers welcome :)
(I for one think it would be nice to have the ability to call a builtin directly through libgit, or at least think through what organization of code would be necessary to make it feasible to say « to do builtin X, you need only call these 5 lib functions in sequence with your choice of arguments »—since perhaps parseopt’ing isn’t so relevant at that level—but I lack a concrete use case at the moment.)
Quoted 12 lines · Ben Knoble
Quoted 9 lines · Junio C Hamano
Quoted 5 lines · "Hardik Kumar
Quoted 5 lines
In general, builtin/foo.c::cmd_foo() are concrete programs that work on specific repository (i.e.,
the_repository), and there is not much reason to rewrite the use ofthe_repositoryto use "repo" given by the caller which is git potty. You'd also need to deal with the case where "repo" is NULL (hint: "cd / && git foo -h").[snip]
Quoted 7 lines
Quoted 7 lines
The utility functions
builtin/foo.cborrows from outside builtin/ directory are being "libified" to reduce the hardcoded dependence onthe_repository, andcmd_foo()can call these functions withthe_repositoryas a parameter. But we have no reason to waste our time updating (and also reviewing patches that make such updates) the built-in implementations themselves to take a pointer to an arbitrary repository.
Line wrap to reasonable length.
Quoted 2 lines
Hm. What if a program wants to do « exactly what ‘git switch’ does » sans shelling out?
Instead of cheating, properly factor out reusable part from cmd_checkout() into a set of libified routines, and make both cmd_checkout() and cmd_switch() to call them
An approach like that would help "libify" things. libifying is not just reducing dependence of globals.
Calling main() from something else is not a libification.
Quoted 23 lines · Junio C Hamano
Quoted 12 lines · Ben Knoble
Quoted 9 lines · Junio C Hamano
Quoted 5 lines · "Hardik Kumar
Quoted 5 lines
In general, builtin/foo.c::cmd_foo() are concrete programs that work on specific repository (i.e.,
the_repository), and there is not much reason to rewrite the use ofthe_repositoryto use "repo" given by the caller which is git potty. You'd also need to deal with the case where "repo" is NULL (hint: "cd / && git foo -h").[snip]
Quoted 7 lines
Quoted 7 lines
The utility functions
builtin/foo.cborrows from outside builtin/ directory are being "libified" to reduce the hardcoded dependence onthe_repository, andcmd_foo()can call these functions withthe_repositoryas a parameter. But we have no reason to waste our time updating (and also reviewing patches that make such updates) the built-in implementations themselves to take a pointer to an arbitrary repository.
Quoted 1 line
Line wrap to reasonable length.
Yeah… my iPhone (from which I sometimes reply) supports plaintext if I do things just right, but it doesn't wrap lines. It's difficult to judge where to break, but maybe I'll try hardbreaks every so often and see if that helps.
Quoted 11 lines
Quoted 2 lines
Hm. What if a program wants to do « exactly what ‘git switch’ does » sans shelling out?
Instead of cheating, properly factor out reusable part from
cmd_checkout()into a set of libified routines, and make bothcmd_checkout()andcmd_switch()to call themAn approach like that would help "libify" things. libifying is not just reducing dependence of globals.
Calling
main()from something else is not a libification.
Sensible. Thanks!
Signature · 1 line
D. Ben Knoble
Quoted 7 lines · D. Ben Knoble
Quoted 7 lines
Quoted 2 lines
Hm. What if a program wants to do « exactly what ‘git switch’ does » sans shelling out?
Instead of cheating, properly factor out reusable part from
cmd_checkout()into a set of libified routines, and make bothcmd_checkout()andcmd_switch()to call them
Quoted 6 lines
Quoted 4 lines
An approach like that would help "libify" things. libifying is not just reducing dependence of globals.
Calling
main()from something else is not a libification.Sensible. Thanks!
I actually answered a wrong question though ;-)
The way cmd_switch() and cmd_restore() were introduced by sharing what used to serve cmd_checkout() was serviceable, but ugly. Had we started from separate implementations for 'switch' and 'restore' that were later merged into 'checkout', we would not have ended up with a design centered on a single monolithic choke point like checkout_main().
That is what I meant by "cheating instead of refactoring reusable parts". However, that is not directly relevant to your example.
It is an anti-pattern to call the top-level implementation of 'git foo' in cmd_foo() directly from cmd_bar(), since these cmd_foo() functions are like main() in ordinary programs, performing one-time initialization (such as git_config() calls) and finalization that cannot be repeated. To help our codebase, as well as the use case you imagined in your message, it would help to trim down these non-reusable cmd_foo() implementations by turning them into mere orchestrators that call refactored helper functions. Such a change would put cmd_foo() and a client that wants to reuse 'git switch' functionality on the same footing, allowing more of our code to be used in different contexts. I think that is what people mean by the "libification" effort.
cover More sensible checkout/switch/restore code refactoring
In the beginning, there was only 'git checkout'. We then introduced 'git switch' and 'git restore', borrowing as much original code as possible from the implementation of 'git checkout'.
Unfortunately, this has left the code in a strange shape. Had we started from separate implementations for 'switch' and 'restore' then later merged them into 'checkout', we would not have arrived at a design where three front-end routines, cmd_checkout(), cmd_switch(), and cmd_restore(), funnel into a single monolithic choke point in checkout_main(), which then determines whether we are checking out a branch or paths, and dispatches to checkout_branch() or checkout_paths().
This series is an attempt to repartition the code into a better shape. Because checkout_branch() and checkout_paths() already exist as two reusable helper functions, with sufficient refactoring of checkout_main(), the two newer entry points, cmd_switch() and cmd_restore(), can parse command-line arguments and configuration settings and jump straight into checkout_branch() or checkout_paths() respectively, without worrying about the other path.
Of course, cmd_checkout() still needs to decide whether to check out a branch or a set of paths and dispatch between these two backends.
The series begins with three fairly isolated cleanups that are worth making on their own, even if we do not follow through with the rest of the series. Patches 4 and 5 then refactor various utility functions out of existing code paths, and patch 6 uses them to rewrite the three top-level command entry points to call these functions without going through checkout_main(). Patch 7 is a style-only fix done after the dust settles, which was deliberately kept separate to simplify review.
The final step moves one helper utility out of builtin/checkout.c to top-level checkout.c as an illustration of the libification discussed in our recent threads.
Note that I consider this a fairly early and rough draft for illustration only. There may be a few topics that touch 'git checkout', and a refactoring of this scale might adversely impact them, so I will not even merge these patches to 'seen'.
1/8: checkout: pass cb_option explicitly to branch name parsers 2/8: checkout: validate new branch name in checkout_branch() 3/8: checkout: validate stage and merge option compatibility in
checkout_paths()
4/8: checkout: extract option validation and pathspec helpers 5/8: checkout: extract branch setup and tracking helpers 6/8: checkout: restructure switch, restore, and checkout
entrypoints
7/8: checkout: wrap overly long lines
8/8: checkout: move post_checkout_hook() to checkout.c
builtin/checkout.c | 545 +++++++++++++++++++++++---------------------- checkout.c | 22 ++ checkout.h | 8 + 3 files changed, 311 insertions(+), 264 deletions(-)
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 1/8 checkout: pass cb_option explicitly to branch name parsers
The file-scope static variable 'cb_option' is used to record whether a new branch is being created via '-b' (in 'git checkout') or '-c' (in 'git switch'), primarily for error reporting and advice messages in parse_remote_branch().
Global mutable state makes the code harder to reason about and refactor.
Pass 'cb_option' explicitly as a parameter to parse_remote_branch() and parse_branchname_arg(), removing the file-scope static variable.
--- builtin/checkout.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
builtin/checkout.c +9 -14
@@ -1346,7 +1346,7 @@ enum checkout_command {static char *parse_remote_branch (const char *arg,struct object_id *rev,int could_be_checkout_paths, -enum checkout_command which_command) +char cb_option) {int num_matches =0 ;char *remote =unique_tracking_name (arg, rev, &num_matches);
@@ -1361,16 +1361,15 @@ static char *parse_remote_branch(const char *arg,if (advice_enabled (ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME )) {const char *cmdname; -switch (which_command) { -case CHECKOUT_CHECKOUT : +switch (cb_option) { +case 'b' : cmdname ="checkout" ;break ; -case CHECKOUT_SWITCH : +case 'c' : cmdname ="switch" ;break ;default : -BUG ("command <%d> should not reach parse_remote_branch" , - which_command); +BUG ("unexpected cb_option '%c'" , cb_option);break ; }
@@ -1394,7 +1393,7 @@ static char *parse_remote_branch(const char *arg,static int parse_branchname_arg (int argc,const char **argv,int dwim_new_local_branch_ok, -enum checkout_command which_command, +char cb_option,struct branch_info *new_branch_info,struct checkout_opts *opts,struct object_id *rev)
@@ -1505,7 +1504,7 @@ static int parse_branchname_arg(int argc, const char **argv,if (recover_with_dwim) { remote =parse_remote_branch (arg, rev, could_be_checkout_paths, - which_command); + cb_option);if (remote) { *new_branch = arg; arg = remote;
@@ -1832,9 +1831,6 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,return newopts; } -/* create-branch option (either b or c) */ -static char cb_option ='b' ; -static int checkout_main (int argc,const char **argv,const char *prefix,struct checkout_opts *opts,struct option *options,enum checkout_command which_command)
@@ -1842,6 +1838,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,int parseopt_flags =0 ;struct branch_info new_branch_info = {0 };int ret; +char cb_option = (which_command ==CHECKOUT_SWITCH ) ?'c' :'b' ;static const char *const checkout_usage[] = {N_ ("git checkout [<options>] <branch>" ),
@@ -1997,7 +1994,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track ==BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; -int n =parse_branchname_arg (argc, argv, dwim_ok, which_command, +int n =parse_branchname_arg (argc, argv, dwim_ok, cb_option, &new_branch_info, opts, &rev); argv += n; argc -= n;
@@ -2174,8 +2171,6 @@ int cmd_switch(int argc, options =add_common_options (&opts, options); options =add_common_switch_branch_options (&opts, options); - cb_option ='c' ; -return checkout_main (argc, argv, prefix, &opts, options,CHECKOUT_SWITCH ); }
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 2/8 checkout: validate new branch name in checkout_branch()
In reply to Junio C HamanoIn checkout_main(), new branch name validation is performed before dispatching to checkout_branch() or checkout_paths(). Checking out paths does not create new branches, so this validation only belongs in checkout_branch().
Move the validate_branchname() and validate_new_branchname() calls from checkout_main() into checkout_branch(). checkout_paths() checks and fails if '.new_branch' is set before doing anything, which indicates that this change is safe and makes good sense.
--- builtin/checkout.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
builtin/checkout.c +11 -11
@@ -1734,6 +1734,17 @@ static int checkout_branch(struct checkout_opts *opts,free (full_ref); } +if (opts->new_branch) { +struct strbuf buf =STRBUF_INIT ; + +if (opts->new_branch_force) + opts->branch_exists =validate_branchname (opts->new_branch, &buf); +else + opts->branch_exists = +validate_new_branchname (opts->new_branch, &buf,0 ); +strbuf_release (&buf); + } +if (!new_branch_info->commit && opts->new_branch) {struct object_id rev;int flag;
@@ -2062,17 +2073,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,die (_ ("you must specify path(s) to restore" )); } -if (opts->new_branch) { -struct strbuf buf =STRBUF_INIT ; - -if (opts->new_branch_force) - opts->branch_exists =validate_branchname (opts->new_branch, &buf); -else - opts->branch_exists = -validate_new_branchname (opts->new_branch, &buf,0 ); -strbuf_release (&buf); - } -if (opts->patch_mode || opts->pathspec.nr) ret =checkout_paths (opts, &new_branch_info);else
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 3/8 checkout: validate stage and merge option compatibility in checkout_paths()
In reply to Junio C HamanoIn checkout_main(), checking that no more than one of --ours/--theirs, --force, and --merge is specified is performed when pathspecs are present, before dispatching to checkout_paths(). Checking out a branch does not use index stages, so this validation belongs in checkout_paths().
Move the incompatibility check from checkout_main() into checkout_paths().
--- builtin/checkout.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
builtin/checkout.c +5 -5
@@ -591,6 +591,10 @@ static int checkout_paths(const struct checkout_opts *opts,die (_ ("'%s', '%s', or '%s' cannot be used when checking out of a tree" ),"--merge" ,"--ours" ,"--theirs" ); +if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) +die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" +"checking out of the index." )); +if (opts->patch_mode) {enum add_p_mode patch_mode;struct interactive_options interactive_opts = {
@@ -2063,11 +2067,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->pathspec.recursive =1 ; -if (opts->pathspec.nr) { -if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) -die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" -"checking out of the index." )); - }else { +if (!opts->pathspec.nr) {if (opts->accept_pathspec && !opts->empty_pathspec_ok && !opts->patch_mode)/* patch mode is special */ die (_ ("you must specify path(s) to restore" ));
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 4/8 checkout: extract option validation and pathspec helpers
In reply to Junio C HamanoThe checkout_main() function handles initialization, option parsing, option validation, and pathspec resolution.
Extract option validation and pathspec handling logic into static helper functions:
- init_checkout_opts() initializes default checkout options and repo settings.
- validate_path_options() validates patch context and index/worktree flags.
- prepare_common_options() prepares progress, merge, and force options.
- parse_pathspec_from_file_options() parses and validates pathspecs read from a file.
Call these helpers from checkout_main().
--- builtin/checkout.c | 171 +++++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 75 deletions(-)
builtin/checkout.c +96 -75
@@ -1846,6 +1846,98 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,return newopts; } +static void init_checkout_opts (struct checkout_opts *opts,const char *prefix) +{ + opts->overwrite_ignore =1 ; + opts->prefix = prefix; + opts->show_progress =-1 ; + +repo_config (the_repository, git_checkout_config, opts); +if (the_repository->gitdir) { +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; + } + + opts->track =BRANCH_TRACK_UNSPECIFIED ; +} + +static void validate_path_options (struct checkout_opts *opts) +{ +if (opts->patch_context <-1 ) +die (_ ("'%s' cannot be negative" ),"--unified" ); +if (opts->patch_interhunk_context <-1 ) +die (_ ("'%s' cannot be negative" ),"--inter-hunk-context" ); + +if (!opts->patch_mode) { +if (opts->patch_context !=-1 ) +die (_ ("the option '%s' requires '%s'" ),"--unified" ,"--patch" ); +if (opts->patch_interhunk_context !=-1 ) +die (_ ("the option '%s' requires '%s'" ),"--inter-hunk-context" ,"--patch" ); +if (!opts->auto_advance) +die (_ ("the option '%s' requires '%s'" ),"--no-auto-advance" ,"--patch" ); + } + +if (opts->overlay_mode ==1 && opts->patch_mode) +die (_ ("options '%s' and '%s' cannot be used together" ),"-p" ,"--overlay" ); + +if (opts->checkout_index >=0 || opts->checkout_worktree >=0 ) { +if (opts->checkout_index <0 ) + opts->checkout_index =0 ; +if (opts->checkout_worktree <0 ) + opts->checkout_worktree =0 ; + }else { +if (opts->checkout_index <0 ) + opts->checkout_index = -opts->checkout_index -1 ; +if (opts->checkout_worktree <0 ) + opts->checkout_worktree = -opts->checkout_worktree -1 ; + } +if (opts->checkout_index <0 || opts->checkout_worktree <0 ) +BUG ("these flags should be non-negative by now" ); +} + +static void prepare_common_options (struct checkout_opts *opts) +{ +if (opts->show_progress <0 ) { +if (opts->quiet) + opts->show_progress =0 ; +else + opts->show_progress =isatty (2 ); + } + +/* --conflicts implies --merge */ +if (opts->merge ==-1 ) + opts->merge = opts->conflict_style >=0 ; + +if (opts->force) { + opts->discard_changes =1 ; + opts->ignore_unmerged_opt ="--force" ; + opts->ignore_unmerged =1 ; + } +} + +static void parse_pathspec_from_file_options (struct checkout_opts *opts, +const char *prefix) +{ +if (opts->pathspec_from_file) { +if (opts->pathspec.nr) +die (_ ("'%s' and pathspec arguments cannot be used together" ),"--pathspec-from-file" ); + +if (opts->force_detach) +die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--detach" ); + +if (opts->patch_mode) +die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--patch" ); + +parse_pathspec_file (&opts->pathspec,0 , +0 , + prefix, opts->pathspec_from_file, opts->pathspec_file_nul); + }else if (opts->pathspec_file_nul) { +die (_ ("the option '%s' requires '%s'" ),"--pathspec-file-nul" ,"--pathspec-from-file" ); + } + + opts->pathspec.recursive =1 ; +} +static int checkout_main (int argc,const char **argv,const char *prefix,struct checkout_opts *opts,struct option *options,enum checkout_command which_command)
@@ -1887,17 +1979,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,BUG ("no such checkout variant %d" , which_command); } - opts->overwrite_ignore =1 ; - opts->prefix = prefix; - opts->show_progress =-1 ; - -repo_config (the_repository, git_checkout_config, opts); -if (the_repository->gitdir) { -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; - } - - opts->track =BRANCH_TRACK_UNSPECIFIED ; +init_checkout_opts (opts, prefix);if (!opts->accept_pathspec && !opts->accept_ref)BUG ("make up your mind, you need to take _something_" );
@@ -1907,57 +1989,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, argc =parse_options (argc, argv, prefix, options, usagestr, parseopt_flags); -if (opts->patch_context <-1 ) -die (_ ("'%s' cannot be negative" ),"--unified" ); -if (opts->patch_interhunk_context <-1 ) -die (_ ("'%s' cannot be negative" ),"--inter-hunk-context" ); - -if (!opts->patch_mode) { -if (opts->patch_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--unified" ,"--patch" ); -if (opts->patch_interhunk_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--inter-hunk-context" ,"--patch" ); -if (!opts->auto_advance) -die (_ ("the option '%s' requires '%s'" ),"--no-auto-advance" ,"--patch" ); - } - -if (opts->show_progress <0 ) { -if (opts->quiet) - opts->show_progress =0 ; -else - opts->show_progress =isatty (2 ); - } - -/* --conflicts implies --merge */ -if (opts->merge ==-1 ) - opts->merge = opts->conflict_style >=0 ; - -if (opts->force) { - opts->discard_changes =1 ; - opts->ignore_unmerged_opt ="--force" ; - opts->ignore_unmerged =1 ; - } +validate_path_options (opts); +prepare_common_options (opts);if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 )die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), cb_option,toupper (cb_option),"--orphan" ); -if (opts->overlay_mode ==1 && opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"-p" ,"--overlay" ); - -if (opts->checkout_index >=0 || opts->checkout_worktree >=0 ) { -if (opts->checkout_index <0 ) - opts->checkout_index =0 ; -if (opts->checkout_worktree <0 ) - opts->checkout_worktree =0 ; - }else { -if (opts->checkout_index <0 ) - opts->checkout_index = -opts->checkout_index -1 ; -if (opts->checkout_worktree <0 ) - opts->checkout_worktree = -opts->checkout_worktree -1 ; - } -if (opts->checkout_index <0 || opts->checkout_worktree <0 ) -BUG ("these flags should be non-negative by now" );/* * convenient shortcut: "git restore --staged [--worktree]" equals * "git restore --staged [--worktree] --source HEAD"
@@ -2048,24 +2086,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, argv[0]); } -if (opts->pathspec_from_file) { -if (opts->pathspec.nr) -die (_ ("'%s' and pathspec arguments cannot be used together" ),"--pathspec-from-file" ); - -if (opts->force_detach) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--detach" ); - -if (opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--patch" ); - -parse_pathspec_file (&opts->pathspec,0 , -0 , - prefix, opts->pathspec_from_file, opts->pathspec_file_nul); - }else if (opts->pathspec_file_nul) { -die (_ ("the option '%s' requires '%s'" ),"--pathspec-file-nul" ,"--pathspec-from-file" ); - } - - opts->pathspec.recursive =1 ; +parse_pathspec_from_file_options (opts, prefix);if (!opts->pathspec.nr) { if (opts->accept_pathspec && !opts->empty_pathspec_ok &&
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 5/8 checkout: extract branch setup and tracking helpers
In reply to Junio C HamanoThe checkout_main() function validates branch-creation options, DWIMs tracking branch options, and sets up branch information directly in its body.
Extract these branch setup operations into static helper functions:
- validate_branch_options() validates compatibility of '-b', '-B', and '--orphan' options.
- dwim_branch_track_option() infers the branch name when '--track' is given without an explicit branch name.
- setup_branch_name_and_info() drives branch validation and parses the branch name argument.
Call the new setup helper from checkout_main().
--- builtin/checkout.c | 104 +++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 50 deletions(-)
builtin/checkout.c +54 -50
@@ -1938,6 +1938,57 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts, opts->pathspec.recursive =1 ; } +static void validate_branch_options (struct checkout_opts *opts,char cb_option) +{ +if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 ) +die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), + cb_option,toupper (cb_option),"--orphan" ); + +if (opts->new_branch_force) + opts->new_branch = opts->new_branch_force; + +if (opts->new_orphan_branch) + opts->new_branch = opts->new_orphan_branch; +} + +static void dwim_branch_track_option (int argc,const char **argv, +struct checkout_opts *opts,char cb_option) +{ +/* --track without -c/-C/-b/-B/--orphan should DWIM */ +if (opts->track !=BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) { +const char *argv0 = argv[0 ]; +if (!argc || !strcmp (argv0,"--" )) +die (_ ("--track needs a branch name" )); +skip_prefix (argv0,"refs/" , &argv0); +skip_prefix (argv0,"remotes/" , &argv0); + argv0 =strchr (argv0,'/' ); +if (!argv0 || !argv0[1 ]) +die (_ ("missing branch name; try -%c" ), cb_option); + opts->new_branch = argv0 +1 ; + } +} + +static int setup_branch_name_and_info (int argc,const char **argv, +struct checkout_opts *opts, +struct branch_info *new_branch_info, +char cb_option) +{ +validate_branch_options (opts, cb_option); +dwim_branch_track_option (argc, argv, opts, cb_option); + +if (argc) { +struct object_id rev; +int dwim_ok = + !opts->patch_mode && + opts->dwim_new_local_branch && + opts->track ==BRANCH_TRACK_UNSPECIFIED && + !opts->new_branch; +return parse_branchname_arg (argc, argv, dwim_ok, cb_option, + new_branch_info, opts, &rev); + } +return 0 ; +} +static int checkout_main (int argc,const char **argv,const char *prefix,struct checkout_opts *opts,struct option *options,enum checkout_command which_command)
@@ -1992,10 +2043,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,validate_path_options (opts);prepare_common_options (opts); -if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 ) -die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), - cb_option,toupper (cb_option),"--orphan" ); -/* * convenient shortcut: "git restore --staged [--worktree]" equals * "git restore --staged [--worktree] --source HEAD"
@@ -2003,52 +2050,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,if (!opts->from_treeish && opts->checkout_index) opts->from_treeish ="HEAD" ; -/* -* From here on, new_branch will contain the branch to be checked out, -* and new_branch_force and new_orphan_branch will tell us which one of -* -b/-B/-c/-C/--orphan is being used. -*/ -if (opts->new_branch_force) - opts->new_branch = opts->new_branch_force; - -if (opts->new_orphan_branch) - opts->new_branch = opts->new_orphan_branch; - -/* --track without -c/-C/-b/-B/--orphan should DWIM */ -if (opts->track !=BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) { -const char *argv0 = argv[0 ]; -if (!argc || !strcmp (argv0,"--" )) -die (_ ("--track needs a branch name" )); -skip_prefix (argv0,"refs/" , &argv0); -skip_prefix (argv0,"remotes/" , &argv0); - argv0 =strchr (argv0,'/' ); -if (!argv0 || !argv0[1 ]) -die (_ ("missing branch name; try -%c" ), cb_option); - opts->new_branch = argv0 +1 ; - } - -/* -* Extract branch name from command line arguments, so -* all that is left is pathspecs. -* -* Handle -* -* 1) git checkout <tree> -- [<paths>] -* 2) git checkout -- [<paths>] -* 3) git checkout <something> [<paths>] -* -* including "last branch" syntax and DWIM-ery for names of -* remote branches, erroring out for invalid or ambiguous cases. -*/ -if (argc && opts->accept_ref) { -struct object_id rev; -int dwim_ok = - !opts->patch_mode && - opts->dwim_new_local_branch && - opts->track ==BRANCH_TRACK_UNSPECIFIED && - !opts->new_branch; -int n =parse_branchname_arg (argc, argv, dwim_ok, cb_option, - &new_branch_info, opts, &rev); +if (opts->accept_ref) { +int n =setup_branch_name_and_info (argc, argv, opts, + &new_branch_info, cb_option); argv += n; argc -= n; }else if (!opts->accept_ref && opts->from_treeish) {
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 6/8 checkout: restructure switch, restore, and checkout entrypoints
In reply to Junio C Hamanocmd_switch(), cmd_restore(), and cmd_checkout() pass their options to checkout_main(), which parses options and configuration, validates and dispatches to checkout_branch() or checkout_paths().
Now that option initialization, validation, and branch setup have been split into dedicated helper functions, restructure cmd_switch(), cmd_restore(), and cmd_checkout() to invoke these helpers directly and dispatch to checkout_branch() or checkout_paths().
In cmd_restore(), handle the --staged default from_treeish = "HEAD" and resolve opts.from_treeish into new_branch_info and opts.source_tree.
This allows us to remove checkout_main() and enum checkout_command as they are no longer needed.
--- builtin/checkout.c | 297 +++++++++++++++++++++++---------------------- 1 file changed, 149 insertions(+), 148 deletions(-)
builtin/checkout.c +149 -148
@@ -1341,12 +1341,6 @@ static void setup_new_branch_info_and_source_tree( } -enum checkout_command { -CHECKOUT_CHECKOUT =1 , -CHECKOUT_SWITCH =2 , -CHECKOUT_RESTORE =3 , -}; - static char *parse_remote_branch(const char *arg, struct object_id *rev, int could_be_checkout_paths,
@@ -1989,19 +1983,25 @@ static int setup_branch_name_and_info(int argc, const char **argv,return 0 ; } -static int checkout_main (int argc,const char **argv,const char *prefix, -struct checkout_opts *opts,struct option *options, -enum checkout_command which_command) +int cmd_switch (int argc, +const char **argv, +const char *prefix, + struct repository *repo UNUSED) { -int parseopt_flags =0 ; +struct checkout_opts opts =CHECKOUT_OPTS_INIT ; +struct option *options =NULL ;struct branch_info new_branch_info = {0 };int ret; -char cb_option = (which_command ==CHECKOUT_SWITCH ) ?'c' :'b' ; - -static const char *const checkout_usage[] = { -N_ ("git checkout [<options>] <branch>" ), -N_ ("git checkout [<options>] [<branch>] -- <file>..." ), -NULL , +struct option switch_options[] = { +OPT_STRING ('c' ,"create" , &opts.new_branch,N_ ("branch" ), +N_ ("create and switch to a new branch" )), +OPT_STRING ('C' ,"force-create" , &opts.new_branch_force,N_ ("branch" ), +N_ ("create/reset and switch to a branch" )), +OPT_BOOL (0 ,"guess" , &opts.dwim_new_local_branch, +N_ ("second guess 'git switch <no-such-branch>'" )), +OPT_BOOL (0 ,"discard-changes" , &opts.discard_changes, +N_ ("throw away local modifications" )), +OPT_END () };static const char *const switch_branch_usage[] = {
@@ -2009,103 +2009,125 @@ static int checkout_main(int argc, const char **argv, const char *prefix,NULL , }; + opts.dwim_new_local_branch =1 ; + opts.accept_ref =1 ; + opts.accept_pathspec =0 ; + opts.switch_branch_doing_nothing_is_ok =0 ; + opts.only_merge_on_switching_branches =1 ; + opts.implicit_detach =0 ; + opts.can_switch_when_in_progress =0 ; + opts.orphan_from_empty_tree =1 ; + opts.overlay_mode =-1 ; + +init_checkout_opts (&opts, prefix); + + options =parse_options_dup (switch_options); + options =add_common_options (&opts, options); + options =add_common_switch_branch_options (&opts, options); + + argc =parse_options (argc, argv, prefix, options, + switch_branch_usage,0 ); + +prepare_common_options (&opts); +setup_branch_name_and_info (argc, argv, &opts, &new_branch_info,'c' ); + + ret =checkout_branch (&opts, &new_branch_info); + +branch_info_release (&new_branch_info); +clear_pathspec (&opts.pathspec); +free (opts.pathspec_from_file); +free (options); + +return ret; +} + +int cmd_restore (int argc, +const char **argv, +const char *prefix, + struct repository *repo UNUSED) +{ +struct checkout_opts opts =CHECKOUT_OPTS_INIT ; +struct option *options; +struct branch_info new_branch_info = {0 }; +int ret; +struct option restore_options[] = { +OPT_STRING ('s' ,"source" , &opts.from_treeish,"<tree-ish>" , +N_ ("which tree-ish to checkout from" )), +OPT_BOOL ('S' ,"staged" , &opts.checkout_index, +N_ ("restore the index" )), +OPT_BOOL ('W' ,"worktree" , &opts.checkout_worktree, +N_ ("restore the working tree (default)" )), +OPT_BOOL (0 ,"ignore-unmerged" , &opts.ignore_unmerged, +N_ ("ignore unmerged entries" )), +OPT_BOOL (0 ,"overlay" , &opts.overlay_mode,N_ ("use overlay mode" )), +OPT_END () + }; +static const char *const restore_usage[] = {N_ ("git restore [<options>] [--source=<branch>] <file>..." ),NULL , }; -const char *const *usagestr; - -switch (which_command) { -case CHECKOUT_CHECKOUT : - usagestr = checkout_usage; -break ; -case CHECKOUT_SWITCH : - usagestr = switch_branch_usage; -break ; -case CHECKOUT_RESTORE : - usagestr = restore_usage; -break ; -default : -BUG ("no such checkout variant %d" , which_command); - } + opts.accept_ref =0 ; + opts.accept_pathspec =1 ; + opts.empty_pathspec_ok =0 ; + opts.overlay_mode =0 ; + opts.checkout_index =-1 ;/* default off */ + opts.checkout_worktree =-2 ;/* default on */ + opts.ignore_unmerged_opt ="--ignore-unmerged" ; -init_checkout_opts (opts, prefix); +init_checkout_opts (&opts, prefix); -if (!opts->accept_pathspec && !opts->accept_ref) -BUG ("make up your mind, you need to take _something_" ); -if (opts->accept_pathspec && opts->accept_ref) - parseopt_flags =PARSE_OPT_KEEP_DASHDASH ; + options =parse_options_dup (restore_options); + options =add_common_options (&opts, options); + options =add_checkout_path_options (&opts, options); argc =parse_options (argc, argv, prefix, options, - usagestr, parseopt_flags); + restore_usage,0 ); -validate_path_options (opts); -prepare_common_options (opts); +validate_path_options (&opts); +prepare_common_options (&opts);/* * convenient shortcut: "git restore --staged [--worktree]" equals * "git restore --staged [--worktree] --source HEAD" */ -if (!opts->from_treeish && opts->checkout_index) - opts->from_treeish ="HEAD" ; - -if (opts->accept_ref) { -int n =setup_branch_name_and_info (argc, argv, opts, - &new_branch_info, cb_option); - argv += n; - argc -= n; - }else if (!opts->accept_ref && opts->from_treeish) { +if (!opts.from_treeish && opts.checkout_index) + opts.from_treeish ="HEAD" ; + +if (opts.from_treeish) {struct object_id rev; -if (repo_get_oid_mb (the_repository, opts->from_treeish, &rev)) -die (_ ("could not resolve '%s'" ), opts->from_treeish); +if (repo_get_oid_mb (the_repository, opts.from_treeish, &rev)) +die (_ ("could not resolve '%s'" ), opts.from_treeish);setup_new_branch_info_and_source_tree (&new_branch_info, - opts, &rev, - opts->from_treeish); + &opts, &rev, + opts.from_treeish); -if (!opts->source_tree) -die (_ ("reference is not a tree: %s" ), opts->from_treeish); +if (!opts.source_tree) +die (_ ("reference is not a tree: %s" ), opts.from_treeish); }if (argc) { -parse_pathspec (&opts->pathspec,0 , - opts->patch_mode ?PATHSPEC_PREFIX_ORIGIN :0 , +parse_pathspec (&opts.pathspec,0 , + opts.patch_mode ?PATHSPEC_PREFIX_ORIGIN :0 , prefix, argv); -if (!opts->pathspec.nr) +if (!opts.pathspec.nr)die (_ ("invalid path specification" )); - -/* -* Try to give more helpful suggestion. -* new_branch && argc > 1 will be caught later. -*/ -if (opts->new_branch && argc ==1 && !new_branch_info.commit) -die (_ ("'%s' is not a commit and a branch '%s' cannot be created from it" ), - argv[0 ], opts->new_branch); - -if (opts->force_detach) -die (_ ("git checkout: --detach does not take a path argument '%s'" ), - argv[0 ]); } -parse_pathspec_from_file_options (opts, prefix); +parse_pathspec_from_file_options (&opts, prefix); -if (!opts->pathspec.nr) { -if (opts->accept_pathspec && !opts->empty_pathspec_ok && - !opts->patch_mode)/* patch mode is special */ -die (_ ("you must specify path(s) to restore" )); - } +if (!opts.pathspec.nr && !opts.patch_mode) +die (_ ("you must specify path(s) to restore" )); -if (opts->patch_mode || opts->pathspec.nr) - ret =checkout_paths (opts, &new_branch_info); -else - ret =checkout_branch (opts, &new_branch_info); + ret =checkout_paths (&opts, &new_branch_info);branch_info_release (&new_branch_info); -clear_pathspec (&opts->pathspec); -free (opts->pathspec_from_file); +clear_pathspec (&opts.pathspec); +free (opts.pathspec_from_file);free (options);return ret;
@@ -2118,6 +2140,8 @@ int cmd_checkout(int argc, {struct checkout_opts opts =CHECKOUT_OPTS_INIT ;struct option *options; +struct branch_info new_branch_info = {0 }; +int ret, n;struct option checkout_options[] = {OPT_STRING ('b' ,NULL , &opts.new_branch,N_ ("branch" ),N_ ("create and checkout a new branch" )),
@@ -2132,6 +2156,12 @@ int cmd_checkout(int argc, OPT_END() }; +static const char *const checkout_usage[] = { +N_ ("git checkout [<options>] <branch>" ), +N_ ("git checkout [<options>] [<branch>] -- <file>..." ), +NULL , + }; + opts.dwim_new_local_branch =1 ; opts.switch_branch_doing_nothing_is_ok =1 ; opts.only_merge_on_switching_branches =0 ;
@@ -2154,84 +2184,55 @@ int cmd_checkout(int argc, opts.only_merge_on_switching_branches =1 ; } +init_checkout_opts (&opts, prefix); + options =parse_options_dup (checkout_options); options =add_common_options (&opts, options); options =add_common_switch_branch_options (&opts, options); options =add_checkout_path_options (&opts, options); -return checkout_main (argc, argv, prefix, &opts, options, -CHECKOUT_CHECKOUT ); -} + argc =parse_options (argc, argv, prefix, options, + checkout_usage,PARSE_OPT_KEEP_DASHDASH ); -int cmd_switch (int argc, -const char **argv, -const char *prefix, - struct repository *repo UNUSED) -{ -struct checkout_opts opts =CHECKOUT_OPTS_INIT ; -struct option *options =NULL ; -struct option switch_options[] = { -OPT_STRING ('c' ,"create" , &opts.new_branch,N_ ("branch" ), -N_ ("create and switch to a new branch" )), -OPT_STRING ('C' ,"force-create" , &opts.new_branch_force,N_ ("branch" ), -N_ ("create/reset and switch to a branch" )), -OPT_BOOL (0 ,"guess" , &opts.dwim_new_local_branch, -N_ ("second guess 'git switch <no-such-branch>'" )), -OPT_BOOL (0 ,"discard-changes" , &opts.discard_changes, -N_ ("throw away local modifications" )), -OPT_END () - }; +validate_path_options (&opts); +prepare_common_options (&opts); - opts.dwim_new_local_branch =1 ; - opts.accept_ref =1 ; - opts.accept_pathspec =0 ; - opts.switch_branch_doing_nothing_is_ok =0 ; - opts.only_merge_on_switching_branches =1 ; - opts.implicit_detach =0 ; - opts.can_switch_when_in_progress =0 ; - opts.orphan_from_empty_tree =1 ; - opts.overlay_mode =-1 ; + n =setup_branch_name_and_info (argc, argv, &opts, &new_branch_info,'b' ); + argv += n; + argc -= n; - options =parse_options_dup (switch_options); - options =add_common_options (&opts, options); - options =add_common_switch_branch_options (&opts, options); +if (argc) { +parse_pathspec (&opts.pathspec,0 , + opts.patch_mode ?PATHSPEC_PREFIX_ORIGIN :0 , + prefix, argv); -return checkout_main (argc, argv, prefix, &opts, options, -CHECKOUT_SWITCH ); -} +if (!opts.pathspec.nr) +die (_ ("invalid path specification" )); -int cmd_restore (int argc, -const char **argv, -const char *prefix, - struct repository *repo UNUSED) -{ -struct checkout_opts opts =CHECKOUT_OPTS_INIT ; -struct option *options; -struct option restore_options[] = { -OPT_STRING ('s' ,"source" , &opts.from_treeish,"<tree-ish>" , -N_ ("which tree-ish to checkout from" )), -OPT_BOOL ('S' ,"staged" , &opts.checkout_index, -N_ ("restore the index" )), -OPT_BOOL ('W' ,"worktree" , &opts.checkout_worktree, -N_ ("restore the working tree (default)" )), -OPT_BOOL (0 ,"ignore-unmerged" , &opts.ignore_unmerged, -N_ ("ignore unmerged entries" )), -OPT_BOOL (0 ,"overlay" , &opts.overlay_mode,N_ ("use overlay mode" )), -OPT_END () - }; +/* +* Try to give more helpful suggestion. +* new_branch && argc > 1 will be caught later. +*/ +if (opts.new_branch && argc ==1 && !new_branch_info.commit) +die (_ ("'%s' is not a commit and a branch '%s' cannot be created from it" ), + argv[0 ], opts.new_branch); - opts.accept_ref =0 ; - opts.accept_pathspec =1 ; - opts.empty_pathspec_ok =0 ; - opts.overlay_mode =0 ; - opts.checkout_index =-1 ;/* default off */ - opts.checkout_worktree =-2 ;/* default on */ - opts.ignore_unmerged_opt ="--ignore-unmerged" ; +if (opts.force_detach) +die (_ ("git checkout: --detach does not take a path argument '%s'" ), + argv[0 ]); + } - options =parse_options_dup (restore_options); - options =add_common_options (&opts, options); - options =add_checkout_path_options (&opts, options); +parse_pathspec_from_file_options (&opts, prefix); -return checkout_main (argc, argv, prefix, &opts, options, -CHECKOUT_RESTORE ); +if (opts.patch_mode || opts.pathspec.nr) + ret =checkout_paths (&opts, &new_branch_info); +else + ret =checkout_branch (&opts, &new_branch_info); + +branch_info_release (&new_branch_info); +clear_pathspec (&opts.pathspec); +free (opts.pathspec_from_file); +free (options); + +return ret; }
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 7/8 checkout: wrap overly long lines
In reply to Junio C HamanoSo far, the patches in this series have tried to leave the original code intact as much as possible when moving it, to make the refactoring easier to review.
However, there are quite a few overly long lines that are hard to read. There are also several manual checks for mutually incompatible options where die_for_incompatible_optN() could be used instead.
Now that most of the refactoring is complete, tidy up these warts to finish off the series.
--- builtin/checkout.c | 54 +++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 20 deletions(-)
builtin/checkout.c +34 -20
@@ -1864,15 +1864,18 @@ static void validate_path_options(struct checkout_opts *opts)if (!opts->patch_mode) {if (opts->patch_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--unified" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--unified" ,"--patch" );if (opts->patch_interhunk_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--inter-hunk-context" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--inter-hunk-context" ,"--patch" );if (!opts->auto_advance) -die (_ ("the option '%s' requires '%s'" ),"--no-auto-advance" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--no-auto-advance" ,"--patch" ); } -if (opts->overlay_mode ==1 && opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"-p" ,"--overlay" ); +die_for_incompatible_opt2 (opts->overlay_mode ==1 ,"--overlay" , + opts->patch_mode,"-p" );if (opts->checkout_index >=0 || opts->checkout_worktree >=0 ) {if (opts->checkout_index <0 )
@@ -1914,19 +1917,20 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts, {if (opts->pathspec_from_file) {if (opts->pathspec.nr) -die (_ ("'%s' and pathspec arguments cannot be used together" ),"--pathspec-from-file" ); +die (_ ("'%s' and pathspec arguments cannot be used together" ), +"--pathspec-from-file" ); -if (opts->force_detach) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--detach" ); +die_for_incompatible_opt2 (opts->force_detach,"--detach" , +1 ,"--pathspec-from-file" ); -if (opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--patch" ); - -parse_pathspec_file (&opts->pathspec,0 , -0 , - prefix, opts->pathspec_from_file, opts->pathspec_file_nul); +die_for_incompatible_opt2 (opts->patch_mode,"--patch" , +1 ,"--pathspec-from-file" ); +parse_pathspec_file (&opts->pathspec,0 ,0 , + prefix, opts->pathspec_from_file, + opts->pathspec_file_nul); }else if (opts->pathspec_file_nul) { -die (_ ("the option '%s' requires '%s'" ),"--pathspec-file-nul" ,"--pathspec-from-file" ); +die (_ ("the option '%s' requires '%s'" ), +"--pathspec-file-nul" ,"--pathspec-from-file" ); } opts->pathspec.recursive =1 ;
@@ -1934,9 +1938,17 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts,static void validate_branch_options (struct checkout_opts *opts,char cb_option) { -if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 ) -die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), - cb_option,toupper (cb_option),"--orphan" ); +char new_branch_opt[] ="-c" ; +char new_branch_force_opt[] ="-C" ; + + new_branch_opt[1 ] = cb_option; + new_branch_force_opt[1 ] =toupper (cb_option); + +die_for_incompatible_opt3 (opts->new_branch, + new_branch_opt, + opts->new_new_branch_force, + new_branch_force_opt, + opts->new_orphan_branch,"--orphan" );if (opts->new_branch_force) opts->new_branch = opts->new_branch_force;
@@ -2147,10 +2159,12 @@ int cmd_checkout(int argc, N_("create and checkout a new branch")),OPT_STRING ('B' ,NULL , &opts.new_branch_force,N_ ("branch" ),N_ ("create/reset and checkout a branch" )), -OPT_BOOL ('l' ,NULL , &opts.new_branch_log,N_ ("create reflog for new branch" )), +OPT_BOOL ('l' ,NULL , &opts.new_branch_log, +N_ ("create reflog for new branch" )),OPT_BOOL (0 ,"guess" , &opts.dwim_new_local_branch,N_ ("second guess 'git checkout <no-such-branch>' (default)" )), -OPT_BOOL (0 ,"overlay" , &opts.overlay_mode,N_ ("use overlay mode (default)" )), +OPT_BOOL (0 ,"overlay" , &opts.overlay_mode, +N_ ("use overlay mode (default)" )),OPT_BOOL (0 ,"auto-advance" , &opts.auto_advance,N_ ("auto advance to the next file when selecting hunks interactively" )),OPT_END ()
Signature · 1 line
2.55.0-884-g76cf8659c2
Re: [PATCH 7/8] checkout: wrap overly long lines
Quoted 17 lines · Junio C Hamano
So far, the patches in this series have tried to leave the original code intact as much as possible when moving it, to make the refactoring easier to review.
However, there are quite a few overly long lines that are hard to read. There are also several manual checks for mutually incompatible options where
die_for_incompatible_optN()could be used instead.Now that most of the refactoring is complete, tidy up these warts to finish off the series.
Signed-off-by Junio C Hamano <[email protected]>--- builtin/checkout.c | 54 +++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 20 deletions(-)...
builtin/checkout.c static void parse_pathspec_from_file_options(struct checkout_opts *opts, · line 1949
Quoted 3 lines
builtin/checkout.c +3
-0+ die_for_incompatible_opt3(opts->new_branch, + new_branch_opt, + opts->new_new_branch_force,
Editor slip. This should of course be "opts->new_branch_force".
Quoted 2 lines
builtin/checkout.c +2
-0+ new_branch_force_opt, + opts->new_orphan_branch, "--orphan");
Re: [PATCH 7/8] checkout: wrap overly long lines
Quoted 5 lines · Junio C Hamano
Review
Quoted 3 lines
(quoted diff) +3
-0+ die_for_incompatible_opt3(opts->new_branch, + new_branch_opt, + opts->new_new_branch_force,Editor slip. This should of course be "
opts->new_branch_force".
Not just that, but some uses of die_for_incompatible_optN() changed the order of the options in the error message. Here is a fixed version.
builtin/checkout.c +41 -22
@@ -1864,15 +1864,18 @@ static void validate_path_options(struct checkout_opts *opts)if (!opts->patch_mode) {if (opts->patch_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--unified" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--unified" ,"--patch" );if (opts->patch_interhunk_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--inter-hunk-context" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--inter-hunk-context" ,"--patch" );if (!opts->auto_advance) -die (_ ("the option '%s' requires '%s'" ),"--no-auto-advance" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--no-auto-advance" ,"--patch" ); } -if (opts->overlay_mode ==1 && opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"-p" ,"--overlay" ); +die_for_incompatible_opt2 (opts->patch_mode,"-p" , + opts->overlay_mode ==1 ,"--overlay" );if (opts->checkout_index >=0 || opts->checkout_worktree >=0 ) {if (opts->checkout_index <0 )
@@ -1914,19 +1917,25 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts, {if (opts->pathspec_from_file) {if (opts->pathspec.nr) -die (_ ("'%s' and pathspec arguments cannot be used together" ),"--pathspec-from-file" ); - -if (opts->force_detach) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--detach" ); - -if (opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--patch" ); - -parse_pathspec_file (&opts->pathspec,0 , -0 , - prefix, opts->pathspec_from_file, opts->pathspec_file_nul); +die (_ ("'%s' and pathspec arguments cannot be used together" ), +"--pathspec-from-file" ); + +die_for_incompatible_opt2 (!!opts->pathspec_from_file, +"--pathspec-from-file" , + opts->force_detach, +"--detach" ); + +die_for_incompatible_opt2 (!!opts->pathspec_from_file, +"--pathspec-from-file" , + opts->patch_mode, +"--patch" ); + +parse_pathspec_file (&opts->pathspec,0 ,0 , + prefix, opts->pathspec_from_file, + opts->pathspec_file_nul); }else if (opts->pathspec_file_nul) { -die (_ ("the option '%s' requires '%s'" ),"--pathspec-file-nul" ,"--pathspec-from-file" ); +die (_ ("the option '%s' requires '%s'" ), +"--pathspec-file-nul" ,"--pathspec-from-file" ); } opts->pathspec.recursive =1 ;
@@ -1934,9 +1943,17 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts,static void validate_branch_options (struct checkout_opts *opts,char cb_option) { -if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 ) -die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), - cb_option,toupper (cb_option),"--orphan" ); +char new_branch_opt[] ="-c" ; +char new_branch_force_opt[] ="-C" ; + + new_branch_opt[1 ] = cb_option; + new_branch_force_opt[1 ] =toupper (cb_option); + +die_for_incompatible_opt3 (!!opts->new_branch, + new_branch_opt, + !!opts->new_branch_force, + new_branch_force_opt, + !!opts->new_orphan_branch,"--orphan" );if (opts->new_branch_force) opts->new_branch = opts->new_branch_force;
@@ -2147,10 +2164,12 @@ int cmd_checkout(int argc, N_("create and checkout a new branch")),OPT_STRING ('B' ,NULL , &opts.new_branch_force,N_ ("branch" ),N_ ("create/reset and checkout a branch" )), -OPT_BOOL ('l' ,NULL , &opts.new_branch_log,N_ ("create reflog for new branch" )), +OPT_BOOL ('l' ,NULL , &opts.new_branch_log, +N_ ("create reflog for new branch" )),OPT_BOOL (0 ,"guess" , &opts.dwim_new_local_branch,N_ ("second guess 'git checkout <no-such-branch>' (default)" )), -OPT_BOOL (0 ,"overlay" , &opts.overlay_mode,N_ ("use overlay mode (default)" )), +OPT_BOOL (0 ,"overlay" , &opts.overlay_mode, +N_ ("use overlay mode (default)" )),OPT_BOOL (0 ,"auto-advance" , &opts.auto_advance,N_ ("auto advance to the next file when selecting hunks interactively" )),OPT_END ()
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH 8/8 checkout: move post_checkout_hook() to checkout.c
In reply to Junio C Hamanopost_checkout_hook() in builtin/checkout.c runs the 'post-checkout' hook after switching branches or checking out paths.
Move post_checkout_hook() to checkout.c and declare it in checkout.h so that other subsystems can invoke the post-checkout hook without depending on builtin/checkout.c.
This step in the series is entirely optional and is here primarily for illustration. We may later want to teach 'git worktree' to trigger the 'post-checkout' hook, for example, in which case such libification may turn out to be useful.
--- builtin/checkout.c | 18 ------------------ checkout.c | 22 ++++++++++++++++++++++ checkout.h | 8 ++++++++ 3 files changed, 30 insertions(+), 18 deletions(-)
builtin/checkout.c +0 -18
@@ -124,24 +124,6 @@ static void branch_info_release(struct branch_info *info)free (info->checkout); } -static int post_checkout_hook (struct commit *old_commit,struct commit *new_commit, -int changed) -{ -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_FORCE_SERIAL ; - -/* -* "new_commit" can be NULL when checking out from the index before -* a commit exists. -*/ -strvec_pushl (&opt.args, -oid_to_hex (old_commit ? &old_commit->object.oid :null_oid (the_hash_algo)), -oid_to_hex (new_commit ? &new_commit->object.oid :null_oid (the_hash_algo)), - changed ?"1" :"0" , -NULL ); - -return run_hooks_opt (the_repository,"post-checkout" , &opt); -} -/* * Handle a tree object and determine if we need to recurse into the * tree (READ_TREE_RECURSIVE) or skip it (0).
checkout.c +22 -0
@@ -1,6 +1,9 @@#define USE_THE_REPOSITORY_VARIABLE #include "git-compat-util.h" +#include "commit.h" +#include "hex.h" +#include "hook.h" #include "object-name.h" #include "remote.h" #include "refspec.h"
@@ -8,6 +11,7 @@#include "checkout.h" #include "config.h" #include "strbuf.h" +#include "strvec.h" struct tracking_name_data {/* const */ char *src_ref;
@@ -73,3 +77,21 @@ char *unique_tracking_name(const char *name, struct object_id *oid, }return NULL ; } + +int post_checkout_hook (struct commit *old_commit,struct commit *new_commit, +int changed) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_FORCE_SERIAL ; + +/* +* "new_commit" can be NULL when checking out from the index before +* a commit exists. +*/ +strvec_pushl (&opt.args, +oid_to_hex (old_commit ? &old_commit->object.oid :null_oid (the_hash_algo)), +oid_to_hex (new_commit ? &new_commit->object.oid :null_oid (the_hash_algo)), + changed ?"1" :"0" , +NULL ); + +return run_hooks_opt (the_repository,"post-checkout" , &opt); +}
checkout.h +8 -0
@@ -3,6 +3,8 @@#include "hash.h" +struct commit ; +/* * Check if the branch name uniquely matches a branch name on a remote * tracking branch. Return the name of the remote if such a branch
@@ -12,4 +14,10 @@ char *unique_tracking_name(const char *name,struct object_id *oid, int *dwim_remotes_matched); +/* +* Run the post-checkout hook. +*/ +int post_checkout_hook (struct commit *old_commit,struct commit *new_commit, +int changed); +#endif /* CHECKOUT_H */
Signature · 1 line
2.55.0-884-g76cf8659c2
Re: [PATCH 8/8] checkout: move post_checkout_hook() to checkout.c
Quoted 7 lines · Junio C Hamano
checkout.c +7
-0+int post_checkout_hook (struct commit *old_commit,struct commit *new_commit, +int changed) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_FORCE_SERIAL ; + +/* +* "new_commit" can be NULL when checking out from the index before
checkout.c char *unique_tracking_name(const char *name, struct object_id *oid, · line 97
Quoted 10 lines
checkout.c +10
-0+* a commit exists. +*/ +strvec_pushl (&opt.args, +oid_to_hex (old_commit ? &old_commit->object.oid :null_oid (the_hash_algo)), +oid_to_hex (new_commit ? &new_commit->object.oid :null_oid (the_hash_algo)), + changed ?"1" :"0" , +NULL ); + +return run_hooks_opt (the_repository,"post-checkout" , &opt); +}
Not a very good illustration. We should teach the caller to pass "struct repository *repo" to this function, and then teach this function to learn what hash algorithm to use from the given repository instance.
Quoted 7 lines
checkout.h +6
-0+/* +* Run the post-checkout hook. +*/ +int post_checkout_hook (struct commit *old_commit,struct commit *new_commit, +int changed); +#endif /* CHECKOUT_H */
Re: [PATCH 8/8] checkout: move post_checkout_hook() to checkout.c
Quoted 4 lines · Junio C Hamano
Not a very good illustration. We should teach the caller to pass "struct repository *repo" to this function, and then teach this function to learn what hash algorithm to use from the given repository instance.
I've locally amended this step like so.
The dependency on 'the_repository' has been removed. While OK when the helper was in builtin/checkout.c as an integral part of 'git checkout' (and 'git restore'), this is no longer true for a common utility function. So it now takes a pointer to 'struct repository' and uses its associated hash algorithm.
builtin/checkout.c +3 -20
@@ -124,24 +124,6 @@ static void branch_info_release(struct branch_info *info)free (info->checkout); } -static int post_checkout_hook (struct commit *old_commit,struct commit *new_commit, -int changed) -{ -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_FORCE_SERIAL ; - -/* -* "new_commit" can be NULL when checking out from the index before -* a commit exists. -*/ -strvec_pushl (&opt.args, -oid_to_hex (old_commit ? &old_commit->object.oid :null_oid (the_hash_algo)), -oid_to_hex (new_commit ? &new_commit->object.oid :null_oid (the_hash_algo)), - changed ?"1" :"0" , -NULL ); - -return run_hooks_opt (the_repository,"post-checkout" , &opt); -} -/* * Handle a tree object and determine if we need to recurse into the * tree (READ_TREE_RECURSIVE) or skip it (0).
@@ -718,7 +700,7 @@ static int checkout_paths(const struct checkout_opts *opts, &rev, NULL); head =lookup_commit_reference_gently (the_repository, &rev,1 ); - errs |=post_checkout_hook (head, head,0 ); + errs |=post_checkout_hook (the_repository, head, head,0 );return errs; }
@@ -1273,7 +1255,8 @@ static int switch_branches(const struct checkout_opts *opts, } } - ret =post_checkout_hook (old_branch_info.commit, new_branch_info->commit,1 ); + ret =post_checkout_hook (the_repository, + old_branch_info.commit, new_branch_info->commit,1 );branch_info_release (&old_branch_info);strbuf_release (&old_commit_shortname);strbuf_release (&autostash_msg);
checkout.c +28 -0
@@ -1,6 +1,9 @@#define USE_THE_REPOSITORY_VARIABLE #include "git-compat-util.h" +#include "commit.h" +#include "hex.h" +#include "hook.h" #include "object-name.h" #include "remote.h" #include "refspec.h"
@@ -8,6 +11,7 @@#include "checkout.h" #include "config.h" #include "strbuf.h" +#include "strvec.h" struct tracking_name_data {/* const */ char *src_ref;
@@ -73,3 +77,27 @@ char *unique_tracking_name(const char *name, struct object_id *oid, }return NULL ; } + +int post_checkout_hook (struct repository *repo, +struct commit *old_commit,struct commit *new_commit, +int changed) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_FORCE_SERIAL ; +const struct git_hash_algo *hash_algo = repo->hash_algo; + +/* +* "new_commit" can be NULL when checking out from the index before +* a commit exists. +*/ +strvec_pushl (&opt.args, +oid_to_hex (old_commit + ? &old_commit->object.oid + :null_oid (hash_algo)), +oid_to_hex (new_commit ? + &new_commit->object.oid + :null_oid (hash_algo)), + changed ?"1" :"0" , +NULL ); + +return run_hooks_opt (repo,"post-checkout" , &opt); +}
checkout.h +10 -0
@@ -3,6 +3,9 @@#include "hash.h" +struct commit ; +struct repository ; +/* * Check if the branch name uniquely matches a branch name on a remote * tracking branch. Return the name of the remote if such a branch
@@ -12,4 +15,11 @@ char *unique_tracking_name(const char *name,struct object_id *oid, int *dwim_remotes_matched); +/* +* Run the post-checkout hook. +*/ +int post_checkout_hook (struct repository *, +struct commit *old_commit,struct commit *new_commit, +int changed); +#endif /* CHECKOUT_H */
Signature · 1 line
2.55.0-884-g76cf8659c2
v2 cover More sensible checkout/switch/restore code refactoring
In reply to Junio C HamanoIn the beginning, there was only 'git checkout'. We then introduced 'git switch' and 'git restore', borrowing as much original code as possible from the implementation of 'git checkout'.
Unfortunately, this has left the code in a strange shape. Had we started from separate implementations for 'switch' and 'restore' then later merged them into 'checkout', we would not have arrived at a design where three front-end routines, cmd_checkout(), cmd_switch(), and cmd_restore(), funnel into a single monolithic choke point in checkout_main(), which then determines whether we are checking out a branch or paths, and dispatches to checkout_branch() or checkout_paths().
This series is an attempt to repartition the code into a better shape. Because checkout_branch() and checkout_paths() already exist as two reusable helper functions, with sufficient refactoring of checkout_main(), the two newer entry points, cmd_switch() and cmd_restore(), can parse command-line arguments and configuration settings and jump straight into checkout_branch() or checkout_paths() respectively, without worrying about the other path.
Of course, cmd_checkout() still needs to decide whether to check out a branch or a set of paths and dispatch between these two backends.
The series begins with three fairly isolated cleanups that are worth making on their own, even if we do not follow through with the rest of the series. Patches 4 and 5 then refactor various utility functions out of existing code paths, and patch 6 uses them to rewrite the three top-level command entry points to call these functions without going through checkout_main(). Patch 7 is a style-only fix done after the dust settles, which was deliberately kept separate to simplify review.
The final step moves one helper utility out of builtin/checkout.c to top-level checkout.c as an illustration of the libification discussed in our recent threads.
Note that I consider this a fairly early and rough draft for illustration only. There may be a few topics that touch 'git checkout', and a refactoring of this scale might adversely impact them, so I will not even merge these patches to 'seen'.
1/8: checkout: pass cb_option explicitly to branch name parsers 2/8: checkout: validate new branch name in checkout_branch() 3/8: checkout: validate stage and merge option compatibility in
checkout_paths()
4/8: checkout: extract option validation and pathspec helpers 5/8: checkout: extract branch setup and tracking helpers 6/8: checkout: restructure switch, restore, and checkout
entrypoints
7/8: checkout: wrap overly long lines
8/8: checkout: move post_checkout_hook() to checkout.c
Changes since the initial version are:
* [7/8] corrects the arguments to die_for_incompatible_optN() to preserve similarity to the original as well as the output message.
* [8/8] corrects the function signature of post_checkout_hook() to make it independent of 'the_repository'.
builtin/checkout.c | 557 +++++++++++++++++++++++---------------------- checkout.c | 28 +++ checkout.h | 10 + 3 files changed, 328 insertions(+), 267 deletions(-)
Range-diff against v1:
1: 346f98225a = 1: 346f98225a checkout: pass cb_option explicitly to branch name parsers
2: 51320f57c9 = 2: 51320f57c9 checkout: validate new branch name in checkout_branch()
3: 6c321a88d6 = 3: 6c321a88d6 checkout: validate stage and merge option compatibility in checkout_paths()
4: 4eefe85ca7 = 4: 4eefe85ca7 checkout: extract option validation and pathspec helpers
5: 0154ed2547 = 5: 0154ed2547 checkout: extract branch setup and tracking helpers
6: c431b36468 = 6: c431b36468 checkout: restructure switch, restore, and checkout entrypoints
7: 9fdd854d1b ! 7: c8869d0b6a checkout: wrap overly long lines
@@ builtin/checkout.c: static void validate_path_options(struct checkout_opts *opts
- if (opts->overlay_mode == 1 && opts->patch_mode)
- die(_("options '%s' and '%s' cannot be used together"), "-p", "--overlay");
-+ die_for_incompatible_opt2(opts->overlay_mode == 1, "--overlay",
-+ opts->patch_mode, "-p");
++ die_for_incompatible_opt2(opts->patch_mode, "-p",
++ opts->overlay_mode == 1, "--overlay");
if (opts->checkout_index >= 0 || opts->checkout_worktree >= 0) {
if (opts->checkout_index < 0)
@@ builtin/checkout.c: static void parse_pathspec_from_file_options(struct checkout
if (opts->pathspec_from_file) {
if (opts->pathspec.nr)
- die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
-+ die(_("'%s' and pathspec arguments cannot be used together"),
-+ "--pathspec-from-file");
-
+-
- if (opts->force_detach)
- die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--detach");
-+ die_for_incompatible_opt2(opts->force_detach, "--detach",
-+ 1, "--pathspec-from-file");
-
+-
- if (opts->patch_mode)
- die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--patch");
-
- parse_pathspec_file(&opts->pathspec, 0,
- 0,
- prefix, opts->pathspec_from_file, opts->pathspec_file_nul);
-+ die_for_incompatible_opt2(opts->patch_mode, "--patch",
-+ 1, "--pathspec-from-file");
++ die(_("'%s' and pathspec arguments cannot be used together"),
++ "--pathspec-from-file");
++
++ die_for_incompatible_opt2(!!opts->pathspec_from_file,
++ "--pathspec-from-file",
++ opts->force_detach,
++ "--detach");
++
++ die_for_incompatible_opt2(!!opts->pathspec_from_file,
++ "--pathspec-from-file",
++ opts->patch_mode,
++ "--patch");
++
+ parse_pathspec_file(&opts->pathspec, 0, 0,
+ prefix, opts->pathspec_from_file,
+ opts->pathspec_file_nul);
@@ builtin/checkout.c: static void parse_pathspec_from_file_options(struct checkout
+ new_branch_opt[1] = cb_option;
+ new_branch_force_opt[1] = toupper(cb_option);
+
-+ die_for_incompatible_opt3(opts->new_branch,
++ die_for_incompatible_opt3(!!opts->new_branch,
+ new_branch_opt,
-+ opts->new_new_branch_force,
++ !!opts->new_branch_force,
+ new_branch_force_opt,
-+ opts->new_orphan_branch, "--orphan");
++ !!opts->new_orphan_branch, "--orphan");
if (opts->new_branch_force)
opts->new_branch = opts->new_branch_force;
8: 295188d521 ! 8: b5b31c33ae checkout: move post_checkout_hook() to checkout.c
@@ Commit message
so that other subsystems can invoke the post-checkout hook without
depending on builtin/checkout.c.
+ Remove the dependency on 'the_repository'. While OK when the helper
+ was in builtin/checkout.c as an integral part of 'git checkout' (and
+ 'git restore'), this is no longer true for a common utility
+ function. Have it take a pointer to 'struct repository' and use its
+ associated hash algorithm.
+
This step in the series is entirely optional and is here primarily
for illustration. We may later want to teach 'git worktree' to
trigger the 'post-checkout' hook, for example, in which case such
@@ builtin/checkout.c: static void branch_info_release(struct branch_info *info)
/*
* Handle a tree object and determine if we need to recurse into the
* tree (READ_TREE_RECURSIVE) or skip it (0).
+@@ builtin/checkout.c: static int checkout_paths(const struct checkout_opts *opts,
+ &rev, NULL);
+ head = lookup_commit_reference_gently(the_repository, &rev, 1);
+
+- errs |= post_checkout_hook(head, head, 0);
++ errs |= post_checkout_hook(the_repository, head, head, 0);
+ return errs;
+ }
+
+@@ builtin/checkout.c: static int switch_branches(const struct checkout_opts *opts,
+ }
+ }
+
+- ret = post_checkout_hook(old_branch_info.commit, new_branch_info->commit, 1);
++ ret = post_checkout_hook(the_repository,
++ old_branch_info.commit, new_branch_info->commit, 1);
+ branch_info_release(&old_branch_info);
+ strbuf_release(&old_commit_shortname);
+ strbuf_release(&autostash_msg);
## checkout.c ##
@@
@@ checkout.c: char *unique_tracking_name(const char *name, struct object_id *oid,
return NULL;
}
+
-+int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
++int post_checkout_hook(struct repository *repo,
++ struct commit *old_commit, struct commit *new_commit,
+ int changed)
+{
+ struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT_FORCE_SERIAL;
++ const struct git_hash_algo *hash_algo = repo->hash_algo;
+
+ /*
+ * "new_commit" can be NULL when checking out from the index before
+ * a commit exists.
+ */
+ strvec_pushl(&opt.args,
-+ oid_to_hex(old_commit ? &old_commit->object.oid : null_oid(the_hash_algo)),
-+ oid_to_hex(new_commit ? &new_commit->object.oid : null_oid(the_hash_algo)),
++ oid_to_hex(old_commit
++ ? &old_commit->object.oid
++ : null_oid(hash_algo)),
++ oid_to_hex(new_commit ?
++ &new_commit->object.oid
++ : null_oid(hash_algo)),
+ changed ? "1" : "0",
+ NULL);
+
-+ return run_hooks_opt(the_repository, "post-checkout", &opt);
++ return run_hooks_opt(repo, "post-checkout", &opt);
+}
## checkout.h ##
@@ checkout.h
#include "hash.h"
+struct commit;
++struct repository;
+
/*
* Check if the branch name uniquely matches a branch name on a remote
@@ checkout.h: char *unique_tracking_name(const char *name,
+/*
+ * Run the post-checkout hook.
+ */
-+int post_checkout_hook(struct commit *old_commit, struct commit *new_commit,
++int post_checkout_hook(struct repository *,
++ struct commit *old_commit, struct commit *new_commit,
+ int changed);
+
#endif /* CHECKOUT_H */Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH v2 1/8 checkout: pass cb_option explicitly to branch name parsers
The file-scope static variable 'cb_option' is used to record whether a new branch is being created via '-b' (in 'git checkout') or '-c' (in 'git switch'), primarily for error reporting and advice messages in parse_remote_branch().
Global mutable state makes the code harder to reason about and refactor.
Pass 'cb_option' explicitly as a parameter to parse_remote_branch() and parse_branchname_arg(), removing the file-scope static variable.
--- builtin/checkout.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
builtin/checkout.c +9 -14
@@ -1346,7 +1346,7 @@ enum checkout_command {static char *parse_remote_branch (const char *arg,struct object_id *rev,int could_be_checkout_paths, -enum checkout_command which_command) +char cb_option) {int num_matches =0 ;char *remote =unique_tracking_name (arg, rev, &num_matches);
@@ -1361,16 +1361,15 @@ static char *parse_remote_branch(const char *arg,if (advice_enabled (ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME )) {const char *cmdname; -switch (which_command) { -case CHECKOUT_CHECKOUT : +switch (cb_option) { +case 'b' : cmdname ="checkout" ;break ; -case CHECKOUT_SWITCH : +case 'c' : cmdname ="switch" ;break ;default : -BUG ("command <%d> should not reach parse_remote_branch" , - which_command); +BUG ("unexpected cb_option '%c'" , cb_option);break ; }
@@ -1394,7 +1393,7 @@ static char *parse_remote_branch(const char *arg,static int parse_branchname_arg (int argc,const char **argv,int dwim_new_local_branch_ok, -enum checkout_command which_command, +char cb_option,struct branch_info *new_branch_info,struct checkout_opts *opts,struct object_id *rev)
@@ -1505,7 +1504,7 @@ static int parse_branchname_arg(int argc, const char **argv,if (recover_with_dwim) { remote =parse_remote_branch (arg, rev, could_be_checkout_paths, - which_command); + cb_option);if (remote) { *new_branch = arg; arg = remote;
@@ -1832,9 +1831,6 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,return newopts; } -/* create-branch option (either b or c) */ -static char cb_option ='b' ; -static int checkout_main (int argc,const char **argv,const char *prefix,struct checkout_opts *opts,struct option *options,enum checkout_command which_command)
@@ -1842,6 +1838,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,int parseopt_flags =0 ;struct branch_info new_branch_info = {0 };int ret; +char cb_option = (which_command ==CHECKOUT_SWITCH ) ?'c' :'b' ;static const char *const checkout_usage[] = {N_ ("git checkout [<options>] <branch>" ),
@@ -1997,7 +1994,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->dwim_new_local_branch && opts->track ==BRANCH_TRACK_UNSPECIFIED && !opts->new_branch; -int n =parse_branchname_arg (argc, argv, dwim_ok, which_command, +int n =parse_branchname_arg (argc, argv, dwim_ok, cb_option, &new_branch_info, opts, &rev); argv += n; argc -= n;
@@ -2174,8 +2171,6 @@ int cmd_switch(int argc, options =add_common_options (&opts, options); options =add_common_switch_branch_options (&opts, options); - cb_option ='c' ; -return checkout_main (argc, argv, prefix, &opts, options,CHECKOUT_SWITCH ); }
Signature · 1 line
2.55.0-884-g76cf8659c2
Re: [PATCH v2 1/8] checkout: pass cb_option explicitly to branch name parsers
Quoted 10 lines · Junio C Hamano
The file-scope static variable '
cb_option' is used to record whether a new branch is being created via '-b' (in 'git checkout') or '-c' (in 'git switch'), primarily for error reporting and advice messages inparse_remote_branch().Global mutable state makes the code harder to reason about and refactor.
Pass '
cb_option' explicitly as a parameter toparse_remote_branch()andparse_branchname_arg(), removing the file-scope static variable.
I was wondering why we couldn't simply get rid of 'cb_option' entirely? The only usecase for it is for printing user facing messages in checkout_main(), where we already have access to enum checkout_command. Wouldn't it be better to simply only keep the enum and drop this?
[snip]
PATCH v2 2/8 checkout: validate new branch name in checkout_branch()
In reply to Junio C HamanoIn checkout_main(), new branch name validation is performed before dispatching to checkout_branch() or checkout_paths(). Checking out paths does not create new branches, so this validation only belongs in checkout_branch().
Move the validate_branchname() and validate_new_branchname() calls from checkout_main() into checkout_branch(). checkout_paths() checks and fails if '.new_branch' is set before doing anything, which indicates that this change is safe and makes good sense.
--- builtin/checkout.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
builtin/checkout.c +11 -11
@@ -1734,6 +1734,17 @@ static int checkout_branch(struct checkout_opts *opts,free (full_ref); } +if (opts->new_branch) { +struct strbuf buf =STRBUF_INIT ; + +if (opts->new_branch_force) + opts->branch_exists =validate_branchname (opts->new_branch, &buf); +else + opts->branch_exists = +validate_new_branchname (opts->new_branch, &buf,0 ); +strbuf_release (&buf); + } +if (!new_branch_info->commit && opts->new_branch) {struct object_id rev;int flag;
@@ -2062,17 +2073,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,die (_ ("you must specify path(s) to restore" )); } -if (opts->new_branch) { -struct strbuf buf =STRBUF_INIT ; - -if (opts->new_branch_force) - opts->branch_exists =validate_branchname (opts->new_branch, &buf); -else - opts->branch_exists = -validate_new_branchname (opts->new_branch, &buf,0 ); -strbuf_release (&buf); - } -if (opts->patch_mode || opts->pathspec.nr) ret =checkout_paths (opts, &new_branch_info);else
Signature · 1 line
2.55.0-884-g76cf8659c2
Re: [PATCH v2 2/8] checkout: validate new branch name in checkout_branch()
Quoted 10 lines · Junio C Hamano
In
checkout_main(), new branch name validation is performed before dispatching tocheckout_branch()orcheckout_paths(). Checking out paths does not create new branches, so this validation only belongs incheckout_branch().Move the
validate_branchname()andvalidate_new_branchname()calls fromcheckout_main()intocheckout_branch().checkout_paths()checks and fails if '.new_branch' is set before doing anything, which indicates that this change is safe and makes good sense.
This is straightforward and makes sense.
Quoted 46 lines
Signed-off-by Junio C Hamano <[email protected]>--- builtin/checkout.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)builtin/checkout.c +11
-12@@ -1734,6 +1734,17 @@ static int checkout_branch(struct checkout_opts *opts,free (full_ref); } +if (opts->new_branch) { +struct strbuf buf =STRBUF_INIT ; + +if (opts->new_branch_force) + opts->branch_exists =validate_branchname (opts->new_branch, &buf); +else + opts->branch_exists = +validate_new_branchname (opts->new_branch, &buf,0 ); +strbuf_release (&buf); + } +if (!new_branch_info->commit && opts->new_branch) {struct object_id rev;int flag;@@ -2062,17 +2073,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,die (_ ("you must specify path(s) to restore" )); } -if (opts->new_branch) { -struct strbuf buf =STRBUF_INIT ; - -if (opts->new_branch_force) - opts->branch_exists =validate_branchname (opts->new_branch, &buf); -else - opts->branch_exists = -validate_new_branchname (opts->new_branch, &buf,0 ); -strbuf_release (&buf); - } -if (opts->patch_mode || opts->pathspec.nr) ret =checkout_paths (opts, &new_branch_info);else --
Quoted 1 line
2.55.0-884-g76cf8659c2
The changes looks good too.
PATCH v2 3/8 checkout: validate stage and merge option compatibility in checkout_paths()
In reply to Junio C HamanoIn checkout_main(), checking that no more than one of --ours/--theirs, --force, and --merge is specified is performed when pathspecs are present, before dispatching to checkout_paths(). Checking out a branch does not use index stages, so this validation belongs in checkout_paths().
Move the incompatibility check from checkout_main() into checkout_paths().
--- builtin/checkout.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
builtin/checkout.c +5 -5
@@ -591,6 +591,10 @@ static int checkout_paths(const struct checkout_opts *opts,die (_ ("'%s', '%s', or '%s' cannot be used when checking out of a tree" ),"--merge" ,"--ours" ,"--theirs" ); +if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) +die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" +"checking out of the index." )); +if (opts->patch_mode) {enum add_p_mode patch_mode;struct interactive_options interactive_opts = {
@@ -2063,11 +2067,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->pathspec.recursive =1 ; -if (opts->pathspec.nr) { -if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) -die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" -"checking out of the index." )); - }else { +if (!opts->pathspec.nr) {if (opts->accept_pathspec && !opts->empty_pathspec_ok && !opts->patch_mode)/* patch mode is special */ die (_ ("you must specify path(s) to restore" ));
Signature · 1 line
2.55.0-884-g76cf8659c2
Re: [PATCH v2 3/8] checkout: validate stage and merge option compatibility in checkout_paths()
Quoted 19 lines · Junio C Hamano
In
checkout_main(), checking that no more than one of --ours/--theirs, --force, and --merge is specified is performed when pathspecs are present, before dispatching tocheckout_paths(). Checking out a branch does not use index stages, so this validation belongs incheckout_paths().Move the incompatibility check from
checkout_main()intocheckout_paths().Signed-off-by Junio C Hamano <[email protected]>--- builtin/checkout.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)builtin/checkout.c +0
-0
builtin/checkout.c static int checkout_paths(const struct checkout_opts *opts, · line 597
Quoted 7 lines
builtin/checkout.c +4
-0@@ -591,6 +591,10 @@ static int checkout_paths(const struct checkout_opts *opts,die (_ ("'%s', '%s', or '%s' cannot be used when checking out of a tree" ),"--merge" ,"--ours" ,"--theirs" ); +if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) +die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" +"checking out of the index." )); +
Doesn't checkout_paths() also get triggered when using '--patch', shouldn't this go below...
builtin/checkout.c static int checkout_paths(const struct checkout_opts *opts, · line 600
Quoted 4 lines
builtin/checkout.c +0
-0if (opts->patch_mode) {enum add_p_mode patch_mode;struct interactive_options interactive_opts = {
...this block? As the original checked for opts->patchspec.nr
Quoted 15 lines
builtin/checkout.c +1
-6@@ -2063,11 +2067,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->pathspec.recursive =1 ; -if (opts->pathspec.nr) { -if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) -die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" -"checking out of the index." )); - }else { +if (!opts->pathspec.nr) {if (opts->accept_pathspec && !opts->empty_pathspec_ok && !opts->patch_mode)/* patch mode is special */ die (_ ("you must specify path(s) to restore" )); --2.55.0-884-g76cf8659c2
Re: [PATCH v2 3/8] checkout: validate stage and merge option compatibility in checkout_paths()
Quoted 12 lines · Karthik Nayak
builtin/checkout.c static int checkout_paths(const struct checkout_opts *opts, · line 597
Quoted 8 lines
builtin/checkout.c +4
-0@@ -591,6 +591,10 @@ static int checkout_paths(const struct checkout_opts *opts,die (_ ("'%s', '%s', or '%s' cannot be used when checking out of a tree" ),"--merge" ,"--ours" ,"--theirs" ); +if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) +die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" +"checking out of the index." )); +Doesn't
checkout_paths()also get triggered when using '--patch', shouldn't this go below...
Quoted 6 lines
builtin/checkout.c static int checkout_paths(const struct checkout_opts *opts, · line 600
Quoted 4 lines
builtin/checkout.c +0
-0if (opts->patch_mode) {enum add_p_mode patch_mode;struct interactive_options interactive_opts = {...this block? As the original checked for
opts->patchspec.nr
Sorry, I do not quite understand your question.
The "if (opts->patch_mode)" block indeed appears in checkout_paths() and when "--patch" is given the control comes here. Are you worried about
git restore --patch --merge --theirs
triggering the option compatibility check, instead of ignoring it and directly jumping into the patch mode?
Quoted 15 lines
Quoted 15 lines
builtin/checkout.c +1
-6@@ -2063,11 +2067,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->pathspec.recursive =1 ; -if (opts->pathspec.nr) { -if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) -die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" -"checking out of the index." )); - }else { +if (!opts->pathspec.nr) {if (opts->accept_pathspec && !opts->empty_pathspec_ok && !opts->patch_mode)/* patch mode is special */ die (_ ("you must specify path(s) to restore" )); --2.55.0-884-g76cf8659c2
Re: [PATCH v2 3/8] checkout: validate stage and merge option compatibility in checkout_paths()
Quoted 23 lines · Junio C Hamano
Quoted 12 lines · Karthik Nayak
builtin/checkout.c static int checkout_paths(const struct checkout_opts *opts, · line 597
Quoted 8 lines
builtin/checkout.c +4
-0@@ -591,6 +591,10 @@ static int checkout_paths(const struct checkout_opts *opts,die (_ ("'%s', '%s', or '%s' cannot be used when checking out of a tree" ),"--merge" ,"--ours" ,"--theirs" ); +if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) +die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" +"checking out of the index." )); +Doesn't
checkout_paths()also get triggered when using '--patch', shouldn't this go below...Quoted 6 lines
builtin/checkout.c static int checkout_paths(const struct checkout_opts *opts, · line 600
Quoted 4 lines
builtin/checkout.c +0
-0if (opts->patch_mode) {enum add_p_mode patch_mode;struct interactive_options interactive_opts = {...this block? As the original checked for
opts->patchspec.nrSorry, I do not quite understand your question.
Quoted 9 lines
The "if (
opts->patch_mode)" block indeed appears incheckout_paths()and when "--patch" is given the control comes here. Are you worried aboutgit restore --patch --merge --theirstriggering the option compatibility check, instead of ignoring it and directly jumping into the patch mode?
Sorry if I was unclear. I was stating that the condition udner which the check runs is now difference. Previously we checked opts->pathspec.nr, but now checkout_paths() is also entered for '--patch' without any path spec.
Having a closer look, it seems to be okay. Because in patch mode, each of the combinations is already rejected before in checkout_paths().
It still might be good to have a sentence in a the commit message, since the guard did change and the reasoning why that is safe is not so obvious.
This does however change the precedence of the error messages:
$ git.master restore --staged --theirs --merge foo fatal: git checkout: --ours/--theirs, --force and --merge are incompatible when checking out of the index.
$ git.patch restore --staged --theirs --merge foo fatal: '--ours' or '--theirs' cannot be used with --staged
This seems okay to me, not sure if we have tests to validate this behavior.
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
builtin/checkout.c +1
-6@@ -2063,11 +2067,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, opts->pathspec.recursive =1 ; -if (opts->pathspec.nr) { -if (1 < !!opts->writeout_stage + !!opts->force + !!opts->merge) -die (_ ("git checkout: --ours/--theirs, --force and --merge are incompatible when\n" -"checking out of the index." )); - }else { +if (!opts->pathspec.nr) {if (opts->accept_pathspec && !opts->empty_pathspec_ok && !opts->patch_mode)/* patch mode is special */ die (_ ("you must specify path(s) to restore" )); --2.55.0-884-g76cf8659c2
Re: [PATCH v2 3/8] checkout: validate stage and merge option compatibility in checkout_paths()
Quoted 7 lines · Karthik Nayak
Sorry if I was unclear. I was stating that the condition udner which the check runs is now difference. Previously we checked
opts->pathspec.nr, but nowcheckout_paths()is also entered for '--patch' without any path spec.Having a closer look, it seems to be okay. Because in patch mode, each of the combinations is already rejected before in
checkout_paths().
Yes, that was why I did not see what difference between the code before and after the change you were concerned about.
Quoted 3 lines
It still might be good to have a sentence in a the commit message, since the guard did change and the reasoning why that is safe is not so obvious.
Perhaps.
Having done this, I do not particularly think these changes and refactorings are all that useful. As unit of reusable code, an entire command (like "git restore") may still be too coarse and callers would want a finer grained control out of "Git restore callable from C programmatically, without having to go through run_command() interface", which means the caller has to still formulate argv[] array in order to call them. These 8 patches may give us a good starting point, but it merely scratches the surface.
Re: [PATCH v2 3/8] checkout: validate stage and merge option compatibility in checkout_paths()
Quoted 19 lines · Junio C Hamano
Quoted 7 lines · Karthik Nayak
Sorry if I was unclear. I was stating that the condition udner which the check runs is now difference. Previously we checked
opts->pathspec.nr, but nowcheckout_paths()is also entered for '--patch' without any path spec.Having a closer look, it seems to be okay. Because in patch mode, each of the combinations is already rejected before in
checkout_paths().Yes, that was why I did not see what difference between the code before and after the change you were concerned about.
Quoted 3 lines
It still might be good to have a sentence in a the commit message, since the guard did change and the reasoning why that is safe is not so obvious.
Perhaps.
Quoted 8 lines
Having done this, I do not particularly think these changes and refactorings are all that useful. As unit of reusable code, an entire command (like "git restore") may still be too coarse and callers would want a finer grained control out of "Git restore callable from C programmatically, without having to go through
run_command()interface", which means the caller has to still formulate argv[] array in order to call them. These 8 patches may give us a good starting point, but it merely scratches the surface.
I do think there is merit in merging this down since it leaves us in a better place than before.
PATCH v2 4/8 checkout: extract option validation and pathspec helpers
In reply to Junio C HamanoThe checkout_main() function handles initialization, option parsing, option validation, and pathspec resolution.
Extract option validation and pathspec handling logic into static helper functions:
- init_checkout_opts() initializes default checkout options and repo settings.
- validate_path_options() validates patch context and index/worktree flags.
- prepare_common_options() prepares progress, merge, and force options.
- parse_pathspec_from_file_options() parses and validates pathspecs read from a file.
Call these helpers from checkout_main().
--- builtin/checkout.c | 171 +++++++++++++++++++++++++-------------------- 1 file changed, 96 insertions(+), 75 deletions(-)
builtin/checkout.c +96 -75
@@ -1846,6 +1846,98 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,return newopts; } +static void init_checkout_opts (struct checkout_opts *opts,const char *prefix) +{ + opts->overwrite_ignore =1 ; + opts->prefix = prefix; + opts->show_progress =-1 ; + +repo_config (the_repository, git_checkout_config, opts); +if (the_repository->gitdir) { +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; + } + + opts->track =BRANCH_TRACK_UNSPECIFIED ; +} + +static void validate_path_options (struct checkout_opts *opts) +{ +if (opts->patch_context <-1 ) +die (_ ("'%s' cannot be negative" ),"--unified" ); +if (opts->patch_interhunk_context <-1 ) +die (_ ("'%s' cannot be negative" ),"--inter-hunk-context" ); + +if (!opts->patch_mode) { +if (opts->patch_context !=-1 ) +die (_ ("the option '%s' requires '%s'" ),"--unified" ,"--patch" ); +if (opts->patch_interhunk_context !=-1 ) +die (_ ("the option '%s' requires '%s'" ),"--inter-hunk-context" ,"--patch" ); +if (!opts->auto_advance) +die (_ ("the option '%s' requires '%s'" ),"--no-auto-advance" ,"--patch" ); + } + +if (opts->overlay_mode ==1 && opts->patch_mode) +die (_ ("options '%s' and '%s' cannot be used together" ),"-p" ,"--overlay" ); + +if (opts->checkout_index >=0 || opts->checkout_worktree >=0 ) { +if (opts->checkout_index <0 ) + opts->checkout_index =0 ; +if (opts->checkout_worktree <0 ) + opts->checkout_worktree =0 ; + }else { +if (opts->checkout_index <0 ) + opts->checkout_index = -opts->checkout_index -1 ; +if (opts->checkout_worktree <0 ) + opts->checkout_worktree = -opts->checkout_worktree -1 ; + } +if (opts->checkout_index <0 || opts->checkout_worktree <0 ) +BUG ("these flags should be non-negative by now" ); +} + +static void prepare_common_options (struct checkout_opts *opts) +{ +if (opts->show_progress <0 ) { +if (opts->quiet) + opts->show_progress =0 ; +else + opts->show_progress =isatty (2 ); + } + +/* --conflicts implies --merge */ +if (opts->merge ==-1 ) + opts->merge = opts->conflict_style >=0 ; + +if (opts->force) { + opts->discard_changes =1 ; + opts->ignore_unmerged_opt ="--force" ; + opts->ignore_unmerged =1 ; + } +} + +static void parse_pathspec_from_file_options (struct checkout_opts *opts, +const char *prefix) +{ +if (opts->pathspec_from_file) { +if (opts->pathspec.nr) +die (_ ("'%s' and pathspec arguments cannot be used together" ),"--pathspec-from-file" ); + +if (opts->force_detach) +die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--detach" ); + +if (opts->patch_mode) +die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--patch" ); + +parse_pathspec_file (&opts->pathspec,0 , +0 , + prefix, opts->pathspec_from_file, opts->pathspec_file_nul); + }else if (opts->pathspec_file_nul) { +die (_ ("the option '%s' requires '%s'" ),"--pathspec-file-nul" ,"--pathspec-from-file" ); + } + + opts->pathspec.recursive =1 ; +} +static int checkout_main (int argc,const char **argv,const char *prefix,struct checkout_opts *opts,struct option *options,enum checkout_command which_command)
@@ -1887,17 +1979,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,BUG ("no such checkout variant %d" , which_command); } - opts->overwrite_ignore =1 ; - opts->prefix = prefix; - opts->show_progress =-1 ; - -repo_config (the_repository, git_checkout_config, opts); -if (the_repository->gitdir) { -prepare_repo_settings (the_repository); - the_repository->settings.command_requires_full_index =0 ; - } - - opts->track =BRANCH_TRACK_UNSPECIFIED ; +init_checkout_opts (opts, prefix);if (!opts->accept_pathspec && !opts->accept_ref)BUG ("make up your mind, you need to take _something_" );
@@ -1907,57 +1989,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix, argc =parse_options (argc, argv, prefix, options, usagestr, parseopt_flags); -if (opts->patch_context <-1 ) -die (_ ("'%s' cannot be negative" ),"--unified" ); -if (opts->patch_interhunk_context <-1 ) -die (_ ("'%s' cannot be negative" ),"--inter-hunk-context" ); - -if (!opts->patch_mode) { -if (opts->patch_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--unified" ,"--patch" ); -if (opts->patch_interhunk_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--inter-hunk-context" ,"--patch" ); -if (!opts->auto_advance) -die (_ ("the option '%s' requires '%s'" ),"--no-auto-advance" ,"--patch" ); - } - -if (opts->show_progress <0 ) { -if (opts->quiet) - opts->show_progress =0 ; -else - opts->show_progress =isatty (2 ); - } - -/* --conflicts implies --merge */ -if (opts->merge ==-1 ) - opts->merge = opts->conflict_style >=0 ; - -if (opts->force) { - opts->discard_changes =1 ; - opts->ignore_unmerged_opt ="--force" ; - opts->ignore_unmerged =1 ; - } +validate_path_options (opts); +prepare_common_options (opts);if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 )die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), cb_option,toupper (cb_option),"--orphan" ); -if (opts->overlay_mode ==1 && opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"-p" ,"--overlay" ); - -if (opts->checkout_index >=0 || opts->checkout_worktree >=0 ) { -if (opts->checkout_index <0 ) - opts->checkout_index =0 ; -if (opts->checkout_worktree <0 ) - opts->checkout_worktree =0 ; - }else { -if (opts->checkout_index <0 ) - opts->checkout_index = -opts->checkout_index -1 ; -if (opts->checkout_worktree <0 ) - opts->checkout_worktree = -opts->checkout_worktree -1 ; - } -if (opts->checkout_index <0 || opts->checkout_worktree <0 ) -BUG ("these flags should be non-negative by now" );/* * convenient shortcut: "git restore --staged [--worktree]" equals * "git restore --staged [--worktree] --source HEAD"
@@ -2048,24 +2086,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix, argv[0]); } -if (opts->pathspec_from_file) { -if (opts->pathspec.nr) -die (_ ("'%s' and pathspec arguments cannot be used together" ),"--pathspec-from-file" ); - -if (opts->force_detach) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--detach" ); - -if (opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--patch" ); - -parse_pathspec_file (&opts->pathspec,0 , -0 , - prefix, opts->pathspec_from_file, opts->pathspec_file_nul); - }else if (opts->pathspec_file_nul) { -die (_ ("the option '%s' requires '%s'" ),"--pathspec-file-nul" ,"--pathspec-from-file" ); - } - - opts->pathspec.recursive =1 ; +parse_pathspec_from_file_options (opts, prefix);if (!opts->pathspec.nr) { if (opts->accept_pathspec && !opts->empty_pathspec_ok &&
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH v2 5/8 checkout: extract branch setup and tracking helpers
In reply to Junio C HamanoThe checkout_main() function validates branch-creation options, DWIMs tracking branch options, and sets up branch information directly in its body.
Extract these branch setup operations into static helper functions:
- validate_branch_options() validates compatibility of '-b', '-B', and '--orphan' options.
- dwim_branch_track_option() infers the branch name when '--track' is given without an explicit branch name.
- setup_branch_name_and_info() drives branch validation and parses the branch name argument.
Call the new setup helper from checkout_main().
--- builtin/checkout.c | 104 +++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 50 deletions(-)
builtin/checkout.c +54 -50
@@ -1938,6 +1938,57 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts, opts->pathspec.recursive =1 ; } +static void validate_branch_options (struct checkout_opts *opts,char cb_option) +{ +if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 ) +die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), + cb_option,toupper (cb_option),"--orphan" ); + +if (opts->new_branch_force) + opts->new_branch = opts->new_branch_force; + +if (opts->new_orphan_branch) + opts->new_branch = opts->new_orphan_branch; +} + +static void dwim_branch_track_option (int argc,const char **argv, +struct checkout_opts *opts,char cb_option) +{ +/* --track without -c/-C/-b/-B/--orphan should DWIM */ +if (opts->track !=BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) { +const char *argv0 = argv[0 ]; +if (!argc || !strcmp (argv0,"--" )) +die (_ ("--track needs a branch name" )); +skip_prefix (argv0,"refs/" , &argv0); +skip_prefix (argv0,"remotes/" , &argv0); + argv0 =strchr (argv0,'/' ); +if (!argv0 || !argv0[1 ]) +die (_ ("missing branch name; try -%c" ), cb_option); + opts->new_branch = argv0 +1 ; + } +} + +static int setup_branch_name_and_info (int argc,const char **argv, +struct checkout_opts *opts, +struct branch_info *new_branch_info, +char cb_option) +{ +validate_branch_options (opts, cb_option); +dwim_branch_track_option (argc, argv, opts, cb_option); + +if (argc) { +struct object_id rev; +int dwim_ok = + !opts->patch_mode && + opts->dwim_new_local_branch && + opts->track ==BRANCH_TRACK_UNSPECIFIED && + !opts->new_branch; +return parse_branchname_arg (argc, argv, dwim_ok, cb_option, + new_branch_info, opts, &rev); + } +return 0 ; +} +static int checkout_main (int argc,const char **argv,const char *prefix,struct checkout_opts *opts,struct option *options,enum checkout_command which_command)
@@ -1992,10 +2043,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,validate_path_options (opts);prepare_common_options (opts); -if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 ) -die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), - cb_option,toupper (cb_option),"--orphan" ); -/* * convenient shortcut: "git restore --staged [--worktree]" equals * "git restore --staged [--worktree] --source HEAD"
@@ -2003,52 +2050,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,if (!opts->from_treeish && opts->checkout_index) opts->from_treeish ="HEAD" ; -/* -* From here on, new_branch will contain the branch to be checked out, -* and new_branch_force and new_orphan_branch will tell us which one of -* -b/-B/-c/-C/--orphan is being used. -*/ -if (opts->new_branch_force) - opts->new_branch = opts->new_branch_force; - -if (opts->new_orphan_branch) - opts->new_branch = opts->new_orphan_branch; - -/* --track without -c/-C/-b/-B/--orphan should DWIM */ -if (opts->track !=BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) { -const char *argv0 = argv[0 ]; -if (!argc || !strcmp (argv0,"--" )) -die (_ ("--track needs a branch name" )); -skip_prefix (argv0,"refs/" , &argv0); -skip_prefix (argv0,"remotes/" , &argv0); - argv0 =strchr (argv0,'/' ); -if (!argv0 || !argv0[1 ]) -die (_ ("missing branch name; try -%c" ), cb_option); - opts->new_branch = argv0 +1 ; - } - -/* -* Extract branch name from command line arguments, so -* all that is left is pathspecs. -* -* Handle -* -* 1) git checkout <tree> -- [<paths>] -* 2) git checkout -- [<paths>] -* 3) git checkout <something> [<paths>] -* -* including "last branch" syntax and DWIM-ery for names of -* remote branches, erroring out for invalid or ambiguous cases. -*/ -if (argc && opts->accept_ref) { -struct object_id rev; -int dwim_ok = - !opts->patch_mode && - opts->dwim_new_local_branch && - opts->track ==BRANCH_TRACK_UNSPECIFIED && - !opts->new_branch; -int n =parse_branchname_arg (argc, argv, dwim_ok, cb_option, - &new_branch_info, opts, &rev); +if (opts->accept_ref) { +int n =setup_branch_name_and_info (argc, argv, opts, + &new_branch_info, cb_option); argv += n; argc -= n; }else if (!opts->accept_ref && opts->from_treeish) {
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH v2 6/8 checkout: restructure switch, restore, and checkout entrypoints
In reply to Junio C Hamanocmd_switch(), cmd_restore(), and cmd_checkout() pass their options to checkout_main(), which parses options and configuration, validates and dispatches to checkout_branch() or checkout_paths().
Now that option initialization, validation, and branch setup have been split into dedicated helper functions, restructure cmd_switch(), cmd_restore(), and cmd_checkout() to invoke these helpers directly and dispatch to checkout_branch() or checkout_paths().
In cmd_restore(), handle the --staged default from_treeish = "HEAD" and resolve opts.from_treeish into new_branch_info and opts.source_tree.
This allows us to remove checkout_main() and enum checkout_command as they are no longer needed.
--- builtin/checkout.c | 297 +++++++++++++++++++++++---------------------- 1 file changed, 149 insertions(+), 148 deletions(-)
builtin/checkout.c +149 -148
@@ -1341,12 +1341,6 @@ static void setup_new_branch_info_and_source_tree( } -enum checkout_command { -CHECKOUT_CHECKOUT =1 , -CHECKOUT_SWITCH =2 , -CHECKOUT_RESTORE =3 , -}; - static char *parse_remote_branch(const char *arg, struct object_id *rev, int could_be_checkout_paths,
@@ -1989,19 +1983,25 @@ static int setup_branch_name_and_info(int argc, const char **argv,return 0 ; } -static int checkout_main (int argc,const char **argv,const char *prefix, -struct checkout_opts *opts,struct option *options, -enum checkout_command which_command) +int cmd_switch (int argc, +const char **argv, +const char *prefix, + struct repository *repo UNUSED) { -int parseopt_flags =0 ; +struct checkout_opts opts =CHECKOUT_OPTS_INIT ; +struct option *options =NULL ;struct branch_info new_branch_info = {0 };int ret; -char cb_option = (which_command ==CHECKOUT_SWITCH ) ?'c' :'b' ; - -static const char *const checkout_usage[] = { -N_ ("git checkout [<options>] <branch>" ), -N_ ("git checkout [<options>] [<branch>] -- <file>..." ), -NULL , +struct option switch_options[] = { +OPT_STRING ('c' ,"create" , &opts.new_branch,N_ ("branch" ), +N_ ("create and switch to a new branch" )), +OPT_STRING ('C' ,"force-create" , &opts.new_branch_force,N_ ("branch" ), +N_ ("create/reset and switch to a branch" )), +OPT_BOOL (0 ,"guess" , &opts.dwim_new_local_branch, +N_ ("second guess 'git switch <no-such-branch>'" )), +OPT_BOOL (0 ,"discard-changes" , &opts.discard_changes, +N_ ("throw away local modifications" )), +OPT_END () };static const char *const switch_branch_usage[] = {
@@ -2009,103 +2009,125 @@ static int checkout_main(int argc, const char **argv, const char *prefix,NULL , }; + opts.dwim_new_local_branch =1 ; + opts.accept_ref =1 ; + opts.accept_pathspec =0 ; + opts.switch_branch_doing_nothing_is_ok =0 ; + opts.only_merge_on_switching_branches =1 ; + opts.implicit_detach =0 ; + opts.can_switch_when_in_progress =0 ; + opts.orphan_from_empty_tree =1 ; + opts.overlay_mode =-1 ; + +init_checkout_opts (&opts, prefix); + + options =parse_options_dup (switch_options); + options =add_common_options (&opts, options); + options =add_common_switch_branch_options (&opts, options); + + argc =parse_options (argc, argv, prefix, options, + switch_branch_usage,0 ); + +prepare_common_options (&opts); +setup_branch_name_and_info (argc, argv, &opts, &new_branch_info,'c' ); + + ret =checkout_branch (&opts, &new_branch_info); + +branch_info_release (&new_branch_info); +clear_pathspec (&opts.pathspec); +free (opts.pathspec_from_file); +free (options); + +return ret; +} + +int cmd_restore (int argc, +const char **argv, +const char *prefix, + struct repository *repo UNUSED) +{ +struct checkout_opts opts =CHECKOUT_OPTS_INIT ; +struct option *options; +struct branch_info new_branch_info = {0 }; +int ret; +struct option restore_options[] = { +OPT_STRING ('s' ,"source" , &opts.from_treeish,"<tree-ish>" , +N_ ("which tree-ish to checkout from" )), +OPT_BOOL ('S' ,"staged" , &opts.checkout_index, +N_ ("restore the index" )), +OPT_BOOL ('W' ,"worktree" , &opts.checkout_worktree, +N_ ("restore the working tree (default)" )), +OPT_BOOL (0 ,"ignore-unmerged" , &opts.ignore_unmerged, +N_ ("ignore unmerged entries" )), +OPT_BOOL (0 ,"overlay" , &opts.overlay_mode,N_ ("use overlay mode" )), +OPT_END () + }; +static const char *const restore_usage[] = {N_ ("git restore [<options>] [--source=<branch>] <file>..." ),NULL , }; -const char *const *usagestr; - -switch (which_command) { -case CHECKOUT_CHECKOUT : - usagestr = checkout_usage; -break ; -case CHECKOUT_SWITCH : - usagestr = switch_branch_usage; -break ; -case CHECKOUT_RESTORE : - usagestr = restore_usage; -break ; -default : -BUG ("no such checkout variant %d" , which_command); - } + opts.accept_ref =0 ; + opts.accept_pathspec =1 ; + opts.empty_pathspec_ok =0 ; + opts.overlay_mode =0 ; + opts.checkout_index =-1 ;/* default off */ + opts.checkout_worktree =-2 ;/* default on */ + opts.ignore_unmerged_opt ="--ignore-unmerged" ; -init_checkout_opts (opts, prefix); +init_checkout_opts (&opts, prefix); -if (!opts->accept_pathspec && !opts->accept_ref) -BUG ("make up your mind, you need to take _something_" ); -if (opts->accept_pathspec && opts->accept_ref) - parseopt_flags =PARSE_OPT_KEEP_DASHDASH ; + options =parse_options_dup (restore_options); + options =add_common_options (&opts, options); + options =add_checkout_path_options (&opts, options); argc =parse_options (argc, argv, prefix, options, - usagestr, parseopt_flags); + restore_usage,0 ); -validate_path_options (opts); -prepare_common_options (opts); +validate_path_options (&opts); +prepare_common_options (&opts);/* * convenient shortcut: "git restore --staged [--worktree]" equals * "git restore --staged [--worktree] --source HEAD" */ -if (!opts->from_treeish && opts->checkout_index) - opts->from_treeish ="HEAD" ; - -if (opts->accept_ref) { -int n =setup_branch_name_and_info (argc, argv, opts, - &new_branch_info, cb_option); - argv += n; - argc -= n; - }else if (!opts->accept_ref && opts->from_treeish) { +if (!opts.from_treeish && opts.checkout_index) + opts.from_treeish ="HEAD" ; + +if (opts.from_treeish) {struct object_id rev; -if (repo_get_oid_mb (the_repository, opts->from_treeish, &rev)) -die (_ ("could not resolve '%s'" ), opts->from_treeish); +if (repo_get_oid_mb (the_repository, opts.from_treeish, &rev)) +die (_ ("could not resolve '%s'" ), opts.from_treeish);setup_new_branch_info_and_source_tree (&new_branch_info, - opts, &rev, - opts->from_treeish); + &opts, &rev, + opts.from_treeish); -if (!opts->source_tree) -die (_ ("reference is not a tree: %s" ), opts->from_treeish); +if (!opts.source_tree) +die (_ ("reference is not a tree: %s" ), opts.from_treeish); }if (argc) { -parse_pathspec (&opts->pathspec,0 , - opts->patch_mode ?PATHSPEC_PREFIX_ORIGIN :0 , +parse_pathspec (&opts.pathspec,0 , + opts.patch_mode ?PATHSPEC_PREFIX_ORIGIN :0 , prefix, argv); -if (!opts->pathspec.nr) +if (!opts.pathspec.nr)die (_ ("invalid path specification" )); - -/* -* Try to give more helpful suggestion. -* new_branch && argc > 1 will be caught later. -*/ -if (opts->new_branch && argc ==1 && !new_branch_info.commit) -die (_ ("'%s' is not a commit and a branch '%s' cannot be created from it" ), - argv[0 ], opts->new_branch); - -if (opts->force_detach) -die (_ ("git checkout: --detach does not take a path argument '%s'" ), - argv[0 ]); } -parse_pathspec_from_file_options (opts, prefix); +parse_pathspec_from_file_options (&opts, prefix); -if (!opts->pathspec.nr) { -if (opts->accept_pathspec && !opts->empty_pathspec_ok && - !opts->patch_mode)/* patch mode is special */ -die (_ ("you must specify path(s) to restore" )); - } +if (!opts.pathspec.nr && !opts.patch_mode) +die (_ ("you must specify path(s) to restore" )); -if (opts->patch_mode || opts->pathspec.nr) - ret =checkout_paths (opts, &new_branch_info); -else - ret =checkout_branch (opts, &new_branch_info); + ret =checkout_paths (&opts, &new_branch_info);branch_info_release (&new_branch_info); -clear_pathspec (&opts->pathspec); -free (opts->pathspec_from_file); +clear_pathspec (&opts.pathspec); +free (opts.pathspec_from_file);free (options);return ret;
@@ -2118,6 +2140,8 @@ int cmd_checkout(int argc, {struct checkout_opts opts =CHECKOUT_OPTS_INIT ;struct option *options; +struct branch_info new_branch_info = {0 }; +int ret, n;struct option checkout_options[] = {OPT_STRING ('b' ,NULL , &opts.new_branch,N_ ("branch" ),N_ ("create and checkout a new branch" )),
@@ -2132,6 +2156,12 @@ int cmd_checkout(int argc, OPT_END() }; +static const char *const checkout_usage[] = { +N_ ("git checkout [<options>] <branch>" ), +N_ ("git checkout [<options>] [<branch>] -- <file>..." ), +NULL , + }; + opts.dwim_new_local_branch =1 ; opts.switch_branch_doing_nothing_is_ok =1 ; opts.only_merge_on_switching_branches =0 ;
@@ -2154,84 +2184,55 @@ int cmd_checkout(int argc, opts.only_merge_on_switching_branches =1 ; } +init_checkout_opts (&opts, prefix); + options =parse_options_dup (checkout_options); options =add_common_options (&opts, options); options =add_common_switch_branch_options (&opts, options); options =add_checkout_path_options (&opts, options); -return checkout_main (argc, argv, prefix, &opts, options, -CHECKOUT_CHECKOUT ); -} + argc =parse_options (argc, argv, prefix, options, + checkout_usage,PARSE_OPT_KEEP_DASHDASH ); -int cmd_switch (int argc, -const char **argv, -const char *prefix, - struct repository *repo UNUSED) -{ -struct checkout_opts opts =CHECKOUT_OPTS_INIT ; -struct option *options =NULL ; -struct option switch_options[] = { -OPT_STRING ('c' ,"create" , &opts.new_branch,N_ ("branch" ), -N_ ("create and switch to a new branch" )), -OPT_STRING ('C' ,"force-create" , &opts.new_branch_force,N_ ("branch" ), -N_ ("create/reset and switch to a branch" )), -OPT_BOOL (0 ,"guess" , &opts.dwim_new_local_branch, -N_ ("second guess 'git switch <no-such-branch>'" )), -OPT_BOOL (0 ,"discard-changes" , &opts.discard_changes, -N_ ("throw away local modifications" )), -OPT_END () - }; +validate_path_options (&opts); +prepare_common_options (&opts); - opts.dwim_new_local_branch =1 ; - opts.accept_ref =1 ; - opts.accept_pathspec =0 ; - opts.switch_branch_doing_nothing_is_ok =0 ; - opts.only_merge_on_switching_branches =1 ; - opts.implicit_detach =0 ; - opts.can_switch_when_in_progress =0 ; - opts.orphan_from_empty_tree =1 ; - opts.overlay_mode =-1 ; + n =setup_branch_name_and_info (argc, argv, &opts, &new_branch_info,'b' ); + argv += n; + argc -= n; - options =parse_options_dup (switch_options); - options =add_common_options (&opts, options); - options =add_common_switch_branch_options (&opts, options); +if (argc) { +parse_pathspec (&opts.pathspec,0 , + opts.patch_mode ?PATHSPEC_PREFIX_ORIGIN :0 , + prefix, argv); -return checkout_main (argc, argv, prefix, &opts, options, -CHECKOUT_SWITCH ); -} +if (!opts.pathspec.nr) +die (_ ("invalid path specification" )); -int cmd_restore (int argc, -const char **argv, -const char *prefix, - struct repository *repo UNUSED) -{ -struct checkout_opts opts =CHECKOUT_OPTS_INIT ; -struct option *options; -struct option restore_options[] = { -OPT_STRING ('s' ,"source" , &opts.from_treeish,"<tree-ish>" , -N_ ("which tree-ish to checkout from" )), -OPT_BOOL ('S' ,"staged" , &opts.checkout_index, -N_ ("restore the index" )), -OPT_BOOL ('W' ,"worktree" , &opts.checkout_worktree, -N_ ("restore the working tree (default)" )), -OPT_BOOL (0 ,"ignore-unmerged" , &opts.ignore_unmerged, -N_ ("ignore unmerged entries" )), -OPT_BOOL (0 ,"overlay" , &opts.overlay_mode,N_ ("use overlay mode" )), -OPT_END () - }; +/* +* Try to give more helpful suggestion. +* new_branch && argc > 1 will be caught later. +*/ +if (opts.new_branch && argc ==1 && !new_branch_info.commit) +die (_ ("'%s' is not a commit and a branch '%s' cannot be created from it" ), + argv[0 ], opts.new_branch); - opts.accept_ref =0 ; - opts.accept_pathspec =1 ; - opts.empty_pathspec_ok =0 ; - opts.overlay_mode =0 ; - opts.checkout_index =-1 ;/* default off */ - opts.checkout_worktree =-2 ;/* default on */ - opts.ignore_unmerged_opt ="--ignore-unmerged" ; +if (opts.force_detach) +die (_ ("git checkout: --detach does not take a path argument '%s'" ), + argv[0 ]); + } - options =parse_options_dup (restore_options); - options =add_common_options (&opts, options); - options =add_checkout_path_options (&opts, options); +parse_pathspec_from_file_options (&opts, prefix); -return checkout_main (argc, argv, prefix, &opts, options, -CHECKOUT_RESTORE ); +if (opts.patch_mode || opts.pathspec.nr) + ret =checkout_paths (&opts, &new_branch_info); +else + ret =checkout_branch (&opts, &new_branch_info); + +branch_info_release (&new_branch_info); +clear_pathspec (&opts.pathspec); +free (opts.pathspec_from_file); +free (options); + +return ret; }
Signature · 1 line
2.55.0-884-g76cf8659c2
Re: [PATCH v2 6/8] checkout: restructure switch, restore, and checkout entrypoints
Quoted 25 lines · Junio C Hamano
cmd_switch(),cmd_restore(), andcmd_checkout()pass their options tocheckout_main(), which parses options and configuration, validates and dispatches tocheckout_branch()orcheckout_paths().Now that option initialization, validation, and branch setup have been split into dedicated helper functions, restructure
cmd_switch(),cmd_restore(), andcmd_checkout()to invoke these helpers directly and dispatch tocheckout_branch()orcheckout_paths().In
cmd_restore(), handle the --staged defaultfrom_treeish= "HEAD" and resolve opts.from_treeish intonew_branch_infoand opts.source_tree.This allows us to remove
checkout_main()and enumcheckout_commandas they are no longer needed.Signed-off-by Junio C Hamano <[email protected]>--- builtin/checkout.c | 297 +++++++++++++++++++++++---------------------- 1 file changed, 149 insertions(+), 148 deletions(-)builtin/checkout.c +0
-0
builtin/checkout.c static void setup_new_branch_info_and_source_tree( · line 1349
Quoted 9 lines
builtin/checkout.c +0
-6@@ -1341,12 +1341,6 @@ static void setup_new_branch_info_and_source_tree( } -enum checkout_command { -CHECKOUT_CHECKOUT =1 , -CHECKOUT_SWITCH =2 , -CHECKOUT_RESTORE =3 , -}; -
Okay so this is how the first commit fits in. Now it makes sense.
[snip]
Re: [PATCH v2 6/8] checkout: restructure switch, restore, and checkout entrypoints
Quoted 30 lines · Karthik Nayak
Quoted 28 lines · Junio C Hamano
cmd_switch(),cmd_restore(), andcmd_checkout()pass their options tocheckout_main(), which parses options and configuration, validates and dispatches tocheckout_branch()orcheckout_paths().Now that option initialization, validation, and branch setup have been split into dedicated helper functions, restructure
cmd_switch(),cmd_restore(), andcmd_checkout()to invoke these helpers directly and dispatch tocheckout_branch()orcheckout_paths().In
cmd_restore(), handle the --staged defaultfrom_treeish= "HEAD" and resolve opts.from_treeish intonew_branch_infoand opts.source_tree.This allows us to remove
checkout_main()and enumcheckout_commandas they are no longer needed.Signed-off-by Junio C Hamano <[email protected]>--- builtin/checkout.c | 297 +++++++++++++++++++++++---------------------- 1 file changed, 149 insertions(+), 148 deletions(-)builtin/checkout.c +0
-0@@ -1341,12 +1341,6 @@ static void setup_new_branch_info_and_source_tree( }
Quoted 8 lines
builtin/checkout.c static void setup_new_branch_info_and_source_tree( · line 1349
Quoted 6 lines
builtin/checkout.c +0
-6-enum checkout_command { -CHECKOUT_CHECKOUT =1 , -CHECKOUT_SWITCH =2 , -CHECKOUT_RESTORE =3 , -}; -Okay so this is how the first commit fits in. Now it makes sense.
Yes. The point of the series was to get rid of the monolithic checkout_main() that everybody goes through. And the enum is primarily for that monolith to decide which code path the original request is about.
In the step that gets rid of the monolith, the enum no longer becomes needed, even though an error message with advice to tell which options are mutually incompatible may need to know the exact name of the option. That makes [1/8] more reasonable than passing enums around.
Re: [PATCH v2 6/8] checkout: restructure switch, restore, and checkout entrypoints
Quoted 46 lines · Junio C Hamano
Quoted 30 lines · Karthik Nayak
Quoted 28 lines · Junio C Hamano
cmd_switch(),cmd_restore(), andcmd_checkout()pass their options tocheckout_main(), which parses options and configuration, validates and dispatches tocheckout_branch()orcheckout_paths().Now that option initialization, validation, and branch setup have been split into dedicated helper functions, restructure
cmd_switch(),cmd_restore(), andcmd_checkout()to invoke these helpers directly and dispatch tocheckout_branch()orcheckout_paths().In
cmd_restore(), handle the --staged defaultfrom_treeish= "HEAD" and resolve opts.from_treeish intonew_branch_infoand opts.source_tree.This allows us to remove
checkout_main()and enumcheckout_commandas they are no longer needed.Signed-off-by Junio C Hamano <[email protected]>--- builtin/checkout.c | 297 +++++++++++++++++++++++---------------------- 1 file changed, 149 insertions(+), 148 deletions(-)builtin/checkout.c +0
-0@@ -1341,12 +1341,6 @@ static void setup_new_branch_info_and_source_tree( }Quoted 8 lines
builtin/checkout.c static void setup_new_branch_info_and_source_tree( · line 1349
Quoted 6 lines
builtin/checkout.c +0
-6-enum checkout_command { -CHECKOUT_CHECKOUT =1 , -CHECKOUT_SWITCH =2 , -CHECKOUT_RESTORE =3 , -}; -Okay so this is how the first commit fits in. Now it makes sense.
Yes. The point of the series was to get rid of the monolithic
checkout_main()that everybody goes through. And the enum is primarily for that monolith to decide which code path the original request is about.
Quoted 5 lines
In the step that gets rid of the monolith, the enum no longer becomes needed, even though an error message with advice to tell which options are mutually incompatible may need to know the exact name of the option. That makes [1/8] more reasonable than passing enums around.
Yeah. It makes sense, in isolation [1/8] looked odd, but together it fits.
PATCH v2 7/8 checkout: wrap overly long lines
In reply to Junio C HamanoSo far, the patches in this series have tried to leave the original code intact as much as possible when moving it, to make the refactoring easier to review.
However, there are quite a few overly long lines that are hard to read. There are also several manual checks for mutually incompatible options where die_for_incompatible_optN() could be used instead.
Now that most of the refactoring is complete, tidy up these warts to finish off the series.
--- builtin/checkout.c | 63 ++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 22 deletions(-)
builtin/checkout.c +41 -22
@@ -1864,15 +1864,18 @@ static void validate_path_options(struct checkout_opts *opts)if (!opts->patch_mode) {if (opts->patch_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--unified" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--unified" ,"--patch" );if (opts->patch_interhunk_context !=-1 ) -die (_ ("the option '%s' requires '%s'" ),"--inter-hunk-context" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--inter-hunk-context" ,"--patch" );if (!opts->auto_advance) -die (_ ("the option '%s' requires '%s'" ),"--no-auto-advance" ,"--patch" ); +die (_ ("the option '%s' requires '%s'" ), +"--no-auto-advance" ,"--patch" ); } -if (opts->overlay_mode ==1 && opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"-p" ,"--overlay" ); +die_for_incompatible_opt2 (opts->patch_mode,"-p" , + opts->overlay_mode ==1 ,"--overlay" );if (opts->checkout_index >=0 || opts->checkout_worktree >=0 ) {if (opts->checkout_index <0 )
@@ -1914,19 +1917,25 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts, {if (opts->pathspec_from_file) {if (opts->pathspec.nr) -die (_ ("'%s' and pathspec arguments cannot be used together" ),"--pathspec-from-file" ); - -if (opts->force_detach) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--detach" ); - -if (opts->patch_mode) -die (_ ("options '%s' and '%s' cannot be used together" ),"--pathspec-from-file" ,"--patch" ); - -parse_pathspec_file (&opts->pathspec,0 , -0 , - prefix, opts->pathspec_from_file, opts->pathspec_file_nul); +die (_ ("'%s' and pathspec arguments cannot be used together" ), +"--pathspec-from-file" ); + +die_for_incompatible_opt2 (!!opts->pathspec_from_file, +"--pathspec-from-file" , + opts->force_detach, +"--detach" ); + +die_for_incompatible_opt2 (!!opts->pathspec_from_file, +"--pathspec-from-file" , + opts->patch_mode, +"--patch" ); + +parse_pathspec_file (&opts->pathspec,0 ,0 , + prefix, opts->pathspec_from_file, + opts->pathspec_file_nul); }else if (opts->pathspec_file_nul) { -die (_ ("the option '%s' requires '%s'" ),"--pathspec-file-nul" ,"--pathspec-from-file" ); +die (_ ("the option '%s' requires '%s'" ), +"--pathspec-file-nul" ,"--pathspec-from-file" ); } opts->pathspec.recursive =1 ;
@@ -1934,9 +1943,17 @@ static void parse_pathspec_from_file_options(struct checkout_opts *opts,static void validate_branch_options (struct checkout_opts *opts,char cb_option) { -if ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) >1 ) -die (_ ("options '-%c', '-%c', and '%s' cannot be used together" ), - cb_option,toupper (cb_option),"--orphan" ); +char new_branch_opt[] ="-c" ; +char new_branch_force_opt[] ="-C" ; + + new_branch_opt[1 ] = cb_option; + new_branch_force_opt[1 ] =toupper (cb_option); + +die_for_incompatible_opt3 (!!opts->new_branch, + new_branch_opt, + !!opts->new_branch_force, + new_branch_force_opt, + !!opts->new_orphan_branch,"--orphan" );if (opts->new_branch_force) opts->new_branch = opts->new_branch_force;
@@ -2147,10 +2164,12 @@ int cmd_checkout(int argc, N_("create and checkout a new branch")),OPT_STRING ('B' ,NULL , &opts.new_branch_force,N_ ("branch" ),N_ ("create/reset and checkout a branch" )), -OPT_BOOL ('l' ,NULL , &opts.new_branch_log,N_ ("create reflog for new branch" )), +OPT_BOOL ('l' ,NULL , &opts.new_branch_log, +N_ ("create reflog for new branch" )),OPT_BOOL (0 ,"guess" , &opts.dwim_new_local_branch,N_ ("second guess 'git checkout <no-such-branch>' (default)" )), -OPT_BOOL (0 ,"overlay" , &opts.overlay_mode,N_ ("use overlay mode (default)" )), +OPT_BOOL (0 ,"overlay" , &opts.overlay_mode, +N_ ("use overlay mode (default)" )),OPT_BOOL (0 ,"auto-advance" , &opts.auto_advance,N_ ("auto advance to the next file when selecting hunks interactively" )),OPT_END ()
Signature · 1 line
2.55.0-884-g76cf8659c2
PATCH v2 8/8 checkout: move post_checkout_hook() to checkout.c
In reply to Junio C Hamanopost_checkout_hook() in builtin/checkout.c runs the 'post-checkout' hook after switching branches or checking out paths.
Move post_checkout_hook() to checkout.c and declare it in checkout.h so that other subsystems can invoke the post-checkout hook without depending on builtin/checkout.c.
Remove the dependency on 'the_repository'. While OK when the helper was in builtin/checkout.c as an integral part of 'git checkout' (and 'git restore'), this is no longer true for a common utility function. Have it take a pointer to 'struct repository' and use its associated hash algorithm.
This step in the series is entirely optional and is here primarily for illustration. We may later want to teach 'git worktree' to trigger the 'post-checkout' hook, for example, in which case such libification may turn out to be useful.
--- builtin/checkout.c | 23 +++-------------------- checkout.c | 28 ++++++++++++++++++++++++++++ checkout.h | 10 ++++++++++ 3 files changed, 41 insertions(+), 20 deletions(-)
builtin/checkout.c +3 -20
@@ -124,24 +124,6 @@ static void branch_info_release(struct branch_info *info)free (info->checkout); } -static int post_checkout_hook (struct commit *old_commit,struct commit *new_commit, -int changed) -{ -struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_FORCE_SERIAL ; - -/* -* "new_commit" can be NULL when checking out from the index before -* a commit exists. -*/ -strvec_pushl (&opt.args, -oid_to_hex (old_commit ? &old_commit->object.oid :null_oid (the_hash_algo)), -oid_to_hex (new_commit ? &new_commit->object.oid :null_oid (the_hash_algo)), - changed ?"1" :"0" , -NULL ); - -return run_hooks_opt (the_repository,"post-checkout" , &opt); -} -/* * Handle a tree object and determine if we need to recurse into the * tree (READ_TREE_RECURSIVE) or skip it (0).
@@ -718,7 +700,7 @@ static int checkout_paths(const struct checkout_opts *opts, &rev, NULL); head =lookup_commit_reference_gently (the_repository, &rev,1 ); - errs |=post_checkout_hook (head, head,0 ); + errs |=post_checkout_hook (the_repository, head, head,0 );return errs; }
@@ -1273,7 +1255,8 @@ static int switch_branches(const struct checkout_opts *opts, } } - ret =post_checkout_hook (old_branch_info.commit, new_branch_info->commit,1 ); + ret =post_checkout_hook (the_repository, + old_branch_info.commit, new_branch_info->commit,1 );branch_info_release (&old_branch_info);strbuf_release (&old_commit_shortname);strbuf_release (&autostash_msg);
checkout.c +28 -0
@@ -1,6 +1,9 @@#define USE_THE_REPOSITORY_VARIABLE #include "git-compat-util.h" +#include "commit.h" +#include "hex.h" +#include "hook.h" #include "object-name.h" #include "remote.h" #include "refspec.h"
@@ -8,6 +11,7 @@#include "checkout.h" #include "config.h" #include "strbuf.h" +#include "strvec.h" struct tracking_name_data {/* const */ char *src_ref;
@@ -73,3 +77,27 @@ char *unique_tracking_name(const char *name, struct object_id *oid, }return NULL ; } + +int post_checkout_hook (struct repository *repo, +struct commit *old_commit,struct commit *new_commit, +int changed) +{ +struct run_hooks_opt opt =RUN_HOOKS_OPT_INIT_FORCE_SERIAL ; +const struct git_hash_algo *hash_algo = repo->hash_algo; + +/* +* "new_commit" can be NULL when checking out from the index before +* a commit exists. +*/ +strvec_pushl (&opt.args, +oid_to_hex (old_commit + ? &old_commit->object.oid + :null_oid (hash_algo)), +oid_to_hex (new_commit ? + &new_commit->object.oid + :null_oid (hash_algo)), + changed ?"1" :"0" , +NULL ); + +return run_hooks_opt (repo,"post-checkout" , &opt); +}
checkout.h +10 -0
@@ -3,6 +3,9 @@#include "hash.h" +struct commit ; +struct repository ; +/* * Check if the branch name uniquely matches a branch name on a remote * tracking branch. Return the name of the remote if such a branch
@@ -12,4 +15,11 @@ char *unique_tracking_name(const char *name,struct object_id *oid, int *dwim_remotes_matched); +/* +* Run the post-checkout hook. +*/ +int post_checkout_hook (struct repository *, +struct commit *old_commit,struct commit *new_commit, +int changed); +#endif /* CHECKOUT_H */
Signature · 1 line
2.55.0-884-g76cf8659c2
Quoted 29 lines · Junio C Hamano
Quoted 7 lines · D. Ben Knoble
Quoted 7 lines
Quoted 2 lines
Hm. What if a program wants to do « exactly what ‘git switch’ does » sans shelling out?
Instead of cheating, properly factor out reusable part from
cmd_checkout()into a set of libified routines, and make bothcmd_checkout()andcmd_switch()to call themQuoted 6 lines
Quoted 4 lines
An approach like that would help "libify" things. libifying is not just reducing dependence of globals.
Calling
main()from something else is not a libification.Sensible. Thanks!
I actually answered a wrong question though ;-)
The way
cmd_switch()andcmd_restore()were introduced by sharing what used to servecmd_checkout()was serviceable, but ugly. Had we started from separate implementations for 'switch' and 'restore' that were later merged into 'checkout', we would not have ended up with a design centered on a single monolithic choke point likecheckout_main().That is what I meant by "cheating instead of refactoring reusable parts". However, that is not directly relevant to your example.
Quoted 12 lines
It is an anti-pattern to call the top-level implementation of 'git foo' in
cmd_foo()directly fromcmd_bar(), since thesecmd_foo()functions are likemain()in ordinary programs, performing one-time initialization (such asgit_config()calls) and finalization that cannot be repeated. To help our codebase, as well as the use case you imagined in your message, it would help to trim down these non-reusablecmd_foo()implementations by turning them into mere orchestrators that call refactored helper functions. Such a change would putcmd_foo()and a client that wants to reuse 'git switch' functionality on the same footing, allowing more of our code to be used in different contexts. I think that is what people mean by the "libification" effort.
Yes, I think such an organization would end up being quite lovely.
Signature · 1 line
D. Ben Knoble
PATCH 1/1 do not pass "repo" to builtin commmand implementations
In reply to Junio C HamanoQuoted 3 lines · Junio C Hamano
I actually think a good medium size project is to fix the last parameter given to
cmd_foo()built-in command implementations that is a pointer to "struct repository". It was a misguided design.
Such a patch would look like this. I am sending it out mostly for illustration, but it applies cleanly to today's 'master' and the result passes the test suite.
As it affects everything inside builtin/, it may be tricky to find the time when it is appropriate to apply a patch like this one, but in not so longer term it would be worth doing to avoid wasted work.
Opinions?
----- >8 -----
Earlier, commit 9b1cb5070f (builtin: add a repository parameter for builtin functions, 2024-09-13) added a 'repo' parameter to built-in command implementations, but this was largely a misguided design.
We claimed that this would help reduce direct use of 'the_repository' and aid the libification effort. However, it does not. These built-in implementations are not intended to be reusable library routines to begin with.
Furthermore, when the 'repo' parameter given to them is non-NULL, it is always 'the_repository'. To safely use the passed-in 'repo' instead of 'the_repository', an implementation must handle the case where 'repo' is NULL (which happens when running outside a repository) and rely on fallback logic such as defaulting back to 'the_repository'.
Quite honestly, it is wasteful makework.
Replace the 'repo' parameter given to cmd_foo() in builtin/ with a boolean 'has_repo' flag. This ensures that these builtin implementations can still tell when they are invoked outside a repository, and also ensures that developers do not waste time modifying '.c' files under builtin/ to reduce uses of 'the_repository'. That effort is better spent libifying code outside the builtin/ directory where it truly matters.
--- Documentation/MyFirstContribution.adoc | 8 +- Documentation/MyFirstObjectWalk.adoc | 16 +- builtin.h | 292 ++++++++++++++++----------------- builtin/add.c | 47 +++--- builtin/am.c | 2 +- builtin/annotate.c | 4 +- builtin/apply.c | 8 +- builtin/archive.c | 5 +- builtin/backfill.c | 8 +- builtin/bisect.c | 4 +- builtin/blame.c | 2 +- builtin/branch.c | 2 +- builtin/bugreport.c | 2 +- builtin/bundle.c | 4 +- builtin/cat-file.c | 2 +- builtin/check-attr.c | 2 +- builtin/check-ignore.c | 2 +- builtin/check-mailmap.c | 2 +- builtin/check-ref-format.c | 2 +- builtin/checkout--worker.c | 2 +- builtin/checkout-index.c | 29 ++-- builtin/checkout.c | 6 +- builtin/clean.c | 2 +- builtin/clone.c | 2 +- builtin/column.c | 2 +- builtin/commit-graph.c | 4 +- builtin/commit-tree.c | 2 +- builtin/commit.c | 4 +- builtin/config.c | 4 +- builtin/count-objects.c | 2 +- builtin/credential-cache--daemon.c | 4 +- builtin/credential-cache.c | 4 +- builtin/credential-store.c | 2 +- builtin/credential.c | 2 +- builtin/describe.c | 2 +- builtin/diagnose.c | 2 +- builtin/diff-files.c | 2 +- builtin/diff-index.c | 2 +- builtin/diff-pairs.c | 11 +- builtin/diff-tree.c | 2 +- builtin/diff.c | 2 +- builtin/difftool.c | 13 +- builtin/fast-export.c | 2 +- builtin/fast-import.c | 4 +- builtin/fetch-pack.c | 2 +- builtin/fetch.c | 2 +- builtin/fmt-merge-msg.c | 5 +- builtin/for-each-ref.c | 5 +- builtin/for-each-repo.c | 2 +- builtin/fsck.c | 61 +++---- builtin/fsmonitor--daemon.c | 4 +- builtin/gc.c | 6 +- builtin/get-tar-commit-id.c | 2 +- builtin/grep.c | 2 +- builtin/hash-object.c | 2 +- builtin/help.c | 2 +- builtin/history.c | 4 +- builtin/hook.c | 4 +- builtin/index-pack.c | 2 +- builtin/init-db.c | 2 +- builtin/interpret-trailers.c | 2 +- builtin/last-modified.c | 7 +- builtin/log.c | 12 +- builtin/ls-files.c | 23 +-- builtin/ls-remote.c | 2 +- builtin/ls-tree.c | 2 +- builtin/mailinfo.c | 2 +- builtin/mailsplit.c | 2 +- builtin/merge-base.c | 2 +- builtin/merge-file.c | 6 +- builtin/merge-index.c | 2 +- builtin/merge-ours.c | 13 +- builtin/merge-recursive.c | 2 +- builtin/merge-tree.c | 2 +- builtin/merge.c | 2 +- builtin/mktag.c | 4 +- builtin/mktree.c | 7 +- builtin/multi-pack-index.c | 4 +- builtin/mv.c | 2 +- builtin/name-rev.c | 4 +- builtin/notes.c | 4 +- builtin/pack-objects.c | 2 +- builtin/pack-redundant.c | 2 +- builtin/pack-refs.c | 5 +- builtin/patch-id.c | 2 +- builtin/prune-packed.c | 2 +- builtin/prune.c | 19 ++- builtin/pull.c | 2 +- builtin/push.c | 2 +- builtin/range-diff.c | 2 +- builtin/read-tree.c | 2 +- builtin/rebase.c | 2 +- builtin/receive-pack.c | 2 +- builtin/reflog.c | 6 +- builtin/refs.c | 5 +- builtin/remote-ext.c | 2 +- builtin/remote-fd.c | 2 +- builtin/remote.c | 4 +- builtin/repack.c | 49 +++--- builtin/replace.c | 2 +- builtin/replay.c | 11 +- builtin/repo.c | 4 +- builtin/rerere.c | 2 +- builtin/reset.c | 2 +- builtin/rev-list.c | 2 +- builtin/rev-parse.c | 2 +- builtin/revert.c | 8 +- builtin/rm.c | 2 +- builtin/send-pack.c | 9 +- builtin/shortlog.c | 2 +- builtin/show-branch.c | 2 +- builtin/show-index.c | 2 +- builtin/show-ref.c | 6 +- builtin/sparse-checkout.c | 8 +- builtin/stash.c | 6 +- builtin/stripspace.c | 2 +- builtin/submodule--helper.c | 4 +- builtin/symbolic-ref.c | 2 +- builtin/tag.c | 2 +- builtin/unpack-file.c | 2 +- builtin/unpack-objects.c | 4 +- builtin/update-index.c | 2 +- builtin/update-ref.c | 2 +- builtin/update-server-info.c | 7 +- builtin/upload-archive.c | 8 +- builtin/upload-pack.c | 2 +- builtin/url-parse.c | 2 +- builtin/var.c | 2 +- builtin/verify-commit.c | 7 +- builtin/verify-pack.c | 2 +- builtin/verify-tag.c | 9 +- builtin/worktree.c | 4 +- builtin/write-tree.c | 2 +- git.c | 4 +- help.c | 2 +- 135 files changed, 507 insertions(+), 486 deletions(-)
Documentation/MyFirstContribution.adoc +4 -4
@@ -152,7 +152,7 @@ point for your command in a function matching the style and signature: ---- int cmd_psuh(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, struct repository *repo UNUSED) + const char *prefix UNUSED, bool has_repo UNUSED) ---- A few things to note:
@@ -160,7 +160,7 @@ A few things to note: * A subcommand implementation takes its command line arguments in `int argc` + `const char **argv`, like `main()` would. -* It also takes two extra parameters, `prefix` and `repo`. What +* It also takes two extra parameters, `prefix` and `has_repo`. What they mean will not be discussed until much later. * Because this first example will not use any of the parameters,
@@ -175,7 +175,7 @@ declaration for `cmd_pull`, and add a new line for `psuh` immediately before it, in order to keep the declarations alphabetically sorted: ---- -int cmd_psuh(int argc, const char **argv, const char *prefix, struct repository *repo); +int cmd_psuh(int argc, const char **argv, const char *prefix, bool has_repo); ---- Be sure to `#include "builtin.h"` in your `psuh.c`. You'll also need to
@@ -192,7 +192,7 @@ should also do so when writing your user-facing commands in the future. ---- int cmd_psuh(int argc UNUSED, const char **argv UNUSED, - const char *prefix UNUSED, struct repository *repo UNUSED) + const char *prefix UNUSED, bool has_repo UNUSED) { printf(_("Pony saying hello goes here.\n")); return 0;
Documentation/MyFirstObjectWalk.adoc +8 -8
@@ -43,7 +43,7 @@ Open up a new file `builtin/walken.c` and set up the command handler: #include "builtin.h" #include "trace.h" -int cmd_walken(int argc, const char **argv, const char *prefix, struct repository *repo) +int cmd_walken(int argc, const char **argv, const char *prefix, bool has_repo UNUSED) { trace_printf(_("cmd_walken incoming...\n")); return 0;
@@ -67,7 +67,7 @@ We'll need to include the `parse-options.h` header. ... -int cmd_walken(int argc, const char **argv, const char *prefix) +int cmd_walken(int argc, const char **argv, const char *prefix, bool has_repo UNUSED) { const char * const walken_usage[] = { N_("git walken"),
@@ -86,7 +86,7 @@ int cmd_walken(int argc, const char **argv, const char *prefix) Also add the relevant line in `builtin.h` near `cmd_version()`: ---- -int cmd_walken(int argc, const char **argv, const char *prefix, struct repository *repo); +int cmd_walken(int argc, const char **argv, const char *prefix, bool has_repo); ---- Include the command in `git.c` in `commands[]` near the entry for `version`,
@@ -237,11 +237,11 @@ static int git_walken_config(const char *var, const char *value, Make sure to invoke `repo_config()` with it in your `cmd_walken()`: ---- -int cmd_walken(int argc, const char **argv, const char *prefix, struct repository *repo) +int cmd_walken(int argc, const char **argv, const char *prefix, bool has_repo UNUSED) { ... - repo_config(repo, git_walken_config, NULL); + repo_config(the_repository, git_walken_config, NULL); ... }
@@ -263,14 +263,14 @@ We'll also need to include the `revision.h` header: ... -int cmd_walken(int argc, const char **argv, const char *prefix, struct repository *repo) +int cmd_walken(int argc, const char **argv, const char *prefix, bool has_repo UNUSED) { /* This can go wherever you like in your declarations.*/ struct rev_info rev; ... /* This should go after the repo_config() call. */ - repo_init_revisions(repo, &rev, prefix); + repo_init_revisions(the_repository, &rev, prefix); ... }
@@ -318,7 +318,7 @@ Then let's invoke `final_rev_info_setup()` after the call to `repo_init_revisions()`: ---- -int cmd_walken(int argc, const char **argv, const char *prefix, struct repository *repo) +int cmd_walken(int argc, const char **argv, const char *prefix, bool has_repo UNUSED) { ...
builtin.h +146 -146
303 lines not on this page · raw message
builtin/add.c +24 -23
139 lines not on this page · raw message
builtin/am.c +1 -1
@@ -2312,7 +2312,7 @@ static int parse_opt_show_current_patch(const struct option *opt, const char *arint cmd_am (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct am_state state;int binary =-1 ;
builtin/annotate.c +2 -2
@@ -11,7 +11,7 @@int cmd_annotate (int argc,const char **argv,const char *prefix, -struct repository *repo) +bool has_repo) {struct strvec args =STRVEC_INIT ;const char **args_copy;
@@ -28,7 +28,7 @@ int cmd_annotate(int argc,CALLOC_ARRAY (args_copy, args.nr +1 );COPY_ARRAY (args_copy, args.v, args.nr); - ret =cmd_blame (args.nr, args_copy, prefix, repo); + ret =cmd_blame (args.nr, args_copy, prefix, has_repo);strvec_clear (&args);free (args_copy);
builtin/apply.c +4 -4
@@ -12,7 +12,7 @@ static const char * const apply_usage[] = {int cmd_apply (int argc,const char **argv,const char *prefix, -struct repository *repo) +bool has_repo) {int force_apply =0 ;int options =0 ;
@@ -35,9 +35,9 @@ int cmd_apply(int argc, &state, &force_apply, &options, apply_usage); -if (repo) { -prepare_repo_settings (repo); - repo->settings.command_requires_full_index =0 ; +if (has_repo) { +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; }if (check_apply_state (&state, force_apply))
builtin/archive.c +3 -2
@@ -2,6 +2,7 @@* Copyright (c) 2006 Franck Bui-Huu * Copyright (c) 2006 Rene Scharfe */ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "archive.h" #include "gettext.h"
@@ -78,7 +79,7 @@ static int run_remote_archiver(int argc, const char **argv,int cmd_archive (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {const char *exec ="git-upload-archive" ;char *output =NULL ;
@@ -109,7 +110,7 @@ int cmd_archive(int argc,setvbuf (stderr,NULL , _IOLBF,BUFSIZ ); - ret =write_archive (argc, argv, prefix, repo, output,0 ); + ret =write_archive (argc, argv, prefix, the_repository, output,0 ); out:free (output);
builtin/backfill.c +4 -4
@@ -139,11 +139,11 @@ static int do_backfill(struct backfill_context *ctx)return ret; } -int cmd_backfill (int argc,const char **argv,const char *prefix,struct repository *repo) +int cmd_backfill(int argc, const char **argv, const char *prefix, bool has_repo UNUSED) {int result;struct backfill_context ctx = { - .repo = repo, + .repo = the_repository, .current_batch =OID_ARRAY_INIT , .min_batch_size =50000 , .sparse =-1 ,
@@ -169,14 +169,14 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct repositPARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_DASHDASH); -repo_init_revisions (repo, &ctx.revs, prefix); +repo_init_revisions (the_repository, &ctx.revs, prefix); argc =setup_revisions (argc, argv, &ctx.revs,NULL );if (argc >1 )die (_ ("unrecognized argument: %s" ), argv[1 ]);reject_unsupported_rev_list_options (&ctx.revs); -repo_config (repo, git_default_config,NULL ); +repo_config (the_repository, git_default_config,NULL );if (ctx.sparse <0 ) ctx.sparse = cfg->apply_sparse_checkout;
builtin/bisect.c +2 -2
@@ -1598,7 +1598,7 @@ static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSEint cmd_bisect (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int res =0 ;parse_opt_subcommand_fn *fn =NULL ;
@@ -1640,7 +1640,7 @@ int cmd_bisect(int argc, }else { argc--; argv++; - res =fn (argc, argv, prefix, repo); + res =fn (argc, argv, prefix, the_repository); }if (res ==BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND ) {
builtin/blame.c +1 -1
@@ -957,7 +957,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,int cmd_blame (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct rev_info revs;char *path =NULL ;
builtin/branch.c +1 -1
@@ -969,7 +969,7 @@ static void die_if_upstream_looks_like_remote(const char *new_upstream, const chint cmd_branch (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {/* possible actions */ int delete = 0, rename = 0, copy = 0, list = 0,
builtin/bugreport.c +1 -1
@@ -93,7 +93,7 @@ static void get_header(struct strbuf *buf, const char *title)int cmd_bugreport (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct strbuf buffer =STRBUF_INIT ;struct strbuf report_path =STRBUF_INIT ;
builtin/bundle.c +2 -2
@@ -235,7 +235,7 @@ static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix,int cmd_bundle (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option options[] = {
@@ -251,5 +251,5 @@ int cmd_bundle(int argc,packet_trace_identity ("bundle" ); -return !!fn (argc, argv, prefix, repo); +return !!fn (argc, argv, prefix, the_repository); }
builtin/cat-file.c +1 -1
@@ -1182,7 +1182,7 @@ static int batch_option_callback(const struct option *opt,int cmd_cat_file (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int opt =0 ;int opt_cw =0 ;
builtin/check-attr.c +1 -1
@@ -110,7 +110,7 @@ static NORETURN void error_with_usage(const char *msg)int cmd_check_attr (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct attr_check *check;struct object_id initialized_oid;
builtin/check-ignore.c +1 -1
@@ -155,7 +155,7 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)int cmd_check_ignore (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int num_ignored;struct dir_struct dir =DIR_INIT ;
builtin/check-mailmap.c +1 -1
@@ -52,7 +52,7 @@ static void check_mailmap(struct string_list *mailmap, const char *contact)int cmd_check_mailmap (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i;struct string_list mailmap =STRING_LIST_INIT_NODUP ;
builtin/check-ref-format.c +1 -1
@@ -56,7 +56,7 @@ static int check_ref_format_branch(const char *arg)int cmd_check_ref_format (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i;int normalize =0 ;
builtin/checkout--worker.c +1 -1
@@ -120,7 +120,7 @@ static const char * const checkout_worker_usage[] = {int cmd_checkout__worker (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct checkout state =CHECKOUT_INIT ;struct option checkout_worker_options[] = {
builtin/checkout-index.c +15 -14
@@ -5,6 +5,7 @@* */ +#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -213,7 +214,7 @@ static int option_parse_stage(const struct option *opt,int cmd_checkout_index (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int i;struct lock_file lock_file =LOCK_INIT ;
@@ -253,19 +254,19 @@ int cmd_checkout_index(int argc,show_usage_with_options_if_asked (argc, argv, builtin_checkout_index_usage, builtin_checkout_index_options); -repo_config (repo, git_default_config,NULL ); +repo_config (the_repository, git_default_config,NULL ); prefix_length = prefix ?strlen (prefix) :0 ; -prepare_repo_settings (repo); - repo->settings.command_requires_full_index =0 ; +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; -if (repo_read_index (repo) <0 ) { +if (repo_read_index (the_repository) <0 ) {die ("invalid cache" ); } argc =parse_options (argc, argv, prefix, builtin_checkout_index_options, builtin_checkout_index_usage,0 ); - state.istate = repo->index; + state.istate = the_repository->index; state.force = force; state.quiet = quiet; state.not_new = not_new;
@@ -285,8 +286,8 @@ int cmd_checkout_index(int argc,*/ if (index_opt && !state.base_dir_len && !to_tempfile) { state.refresh_cache =1 ; - state.istate = repo->index; -repo_hold_locked_index (repo, &lock_file, + state.istate = the_repository->index; +repo_hold_locked_index (the_repository, &lock_file,LOCK_DIE_ON_ERROR ); }
@@ -303,8 +304,8 @@ int cmd_checkout_index(int argc,die ("git checkout-index: don't mix '--all' and explicit filenames" );if (read_from_stdin)die ("git checkout-index: don't mix '--stdin' and explicit filenames" ); - p =prefix_path (repo, prefix, prefix_length, arg); - err |=checkout_file (repo->index, p, prefix); + p =prefix_path (the_repository, prefix, prefix_length, arg); + err |=checkout_file (the_repository->index, p, prefix);free (p); }
@@ -325,8 +326,8 @@ int cmd_checkout_index(int argc,die ("line is badly quoted" );strbuf_swap (&buf, &unquoted); } - p =prefix_path (repo, prefix, prefix_length, buf.buf); - err |=checkout_file (repo->index, p, prefix); + p =prefix_path (the_repository, prefix, prefix_length, buf.buf); + err |=checkout_file (the_repository->index, p, prefix);free (p); }strbuf_release (&unquoted);
@@ -334,7 +335,7 @@ int cmd_checkout_index(int argc, }if (all) - err |=checkout_all (repo->index, prefix, prefix_length); + err |=checkout_all (the_repository->index, prefix, prefix_length);if (pc_workers >1 ) err |= run_parallel_checkout(&state, pc_workers, pc_threshold,
@@ -344,7 +345,7 @@ int cmd_checkout_index(int argc,return 1 ;if (is_lock_file_locked (&lock_file) && -write_locked_index (repo->index, &lock_file,COMMIT_LOCK )) +write_locked_index (the_repository->index, &lock_file,COMMIT_LOCK ))die ("Unable to write new index file" );return 0 ; }
builtin/checkout.c +3 -3
@@ -2092,7 +2092,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,int cmd_checkout (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct checkout_opts opts =CHECKOUT_OPTS_INIT ;struct option *options;
@@ -2144,7 +2144,7 @@ int cmd_checkout(int argc,int cmd_switch (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct checkout_opts opts =CHECKOUT_OPTS_INIT ;struct option *options =NULL ;
@@ -2183,7 +2183,7 @@ int cmd_switch(int argc,int cmd_restore (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct checkout_opts opts =CHECKOUT_OPTS_INIT ;struct option *options;
builtin/clean.c +1 -1
@@ -919,7 +919,7 @@ static void correct_untracked_entries(struct dir_struct *dir)int cmd_clean (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i, res;int dry_run =0 , remove_directories =0 , quiet =0 , ignored =0 ;
builtin/clone.c +1 -1
@@ -869,7 +869,7 @@ static int path_exists(const char *path)int cmd_clone (int argc,const char **argv,const char *prefix, - struct repository *repository UNUSED) + bool has_repo UNUSED) {int is_bundle =0 , is_local;int reject_shallow =0 ;
builtin/column.c +1 -1
@@ -22,7 +22,7 @@ static int column_config(const char *var, const char *value,int cmd_column (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct string_list list =STRING_LIST_INIT_DUP ;struct strbuf sb =STRBUF_INIT ;
builtin/commit-graph.c +2 -2
@@ -341,7 +341,7 @@ static int graph_write(int argc, const char **argv, const char *prefix,int cmd_commit_graph (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option builtin_commit_graph_options[] = {
@@ -360,5 +360,5 @@ int cmd_commit_graph(int argc, builtin_commit_graph_usage, 0);FREE_AND_NULL (options); -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/commit-tree.c +1 -1
@@ -95,7 +95,7 @@ static int parse_file_arg_callback(const struct option *opt,int cmd_commit_tree (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {static struct strbuf buffer =STRBUF_INIT ;struct commit_list *parents =NULL ;
builtin/commit.c +2 -2
@@ -1537,7 +1537,7 @@ static int git_status_config(const char *k, const char *v,int cmd_status (int argc,const char **argv,const char *prefix, -struct repository *repo UNUSED) +bool has_repo UNUSED) {static int no_renames =-1 ;static const char *rename_score_arg = (const char *)-1 ;
@@ -1698,7 +1698,7 @@ static int git_commit_config(const char *k, const char *v,int cmd_commit (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {static struct wt_status s;static const char *cleanup_arg =NULL ;
builtin/config.c +2 -2
@@ -1627,7 +1627,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)int cmd_config (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *subcommand =NULL ;struct option subcommand_opts[] = {
@@ -1654,7 +1654,7 @@ int cmd_config(int argc,if (subcommand) { argc =parse_options (argc, argv, prefix, subcommand_opts, builtin_config_usage,PARSE_OPT_SUBCOMMAND_OPTIONAL |PARSE_OPT_KEEP_UNKNOWN_OPT ); -return subcommand (argc, argv, prefix, repo); +return subcommand (argc, argv, prefix, the_repository); }return cmd_config_actions (argc, argv, prefix);
builtin/count-objects.c +1 -1
@@ -97,7 +97,7 @@ static char const * const count_objects_usage[] = {int cmd_count_objects (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int human_readable =0 ;struct option opts[] = {
builtin/credential-cache--daemon.c +2 -2
@@ -291,7 +291,7 @@ static void init_socket_directory(const char *path)int cmd_credential_cache_daemon (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct tempfile *socket_file;const char *socket_path;
@@ -337,7 +337,7 @@ int cmd_credential_cache_daemon(int argc,int cmd_credential_cache_daemon (int argc,const char **argv,const char *prefix, -struct repository *repo UNUSED) +bool has_repo UNUSED) {const char *const usage[] = {"git credential-cache--daemon [--debug] <socket-path>" ,
builtin/credential-cache.c +2 -2
@@ -140,7 +140,7 @@ static void announce_capabilities(void)int cmd_credential_cache (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *socket_path_arg =NULL ;char *socket_path;
@@ -190,7 +190,7 @@ int cmd_credential_cache(int argc, #else int cmd_credential_cache(int argc, const char **argv, const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) { const char * const usage[] = { "git credential-cache [options] <action>",
builtin/credential-store.c +1 -1
@@ -169,7 +169,7 @@ static void lookup_credential(const struct string_list *fns, struct credential *int cmd_credential_store (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *const usage[] = {"git credential-store [<options>] <action>" ,
builtin/credential.c +1 -1
@@ -12,7 +12,7 @@ static const char usage_msg[] =int cmd_credential (int argc,const char **argv, const char *prefix UNUSED, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *op;struct credential c =CREDENTIAL_INIT ;
builtin/describe.c +1 -1
@@ -601,7 +601,7 @@ static int option_parse_exact_match(const struct option *opt, const char *arg,int cmd_describe (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED ) + bool has_repo UNUSED) {struct refs_for_each_ref_options for_each_ref_opts = { .flags =REFS_FOR_EACH_INCLUDE_BROKEN ,
builtin/diagnose.c +1 -1
@@ -16,7 +16,7 @@ static const char * const diagnose_usage[] = {int cmd_diagnose (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct strbuf zip_path =STRBUF_INIT ;time_t now =time (NULL );
builtin/diff-files.c +1 -1
@@ -23,7 +23,7 @@ COMMON_DIFF_OPTIONS_HELP;int cmd_diff_files (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct rev_info rev;int result;
builtin/diff-index.c +1 -1
@@ -19,7 +19,7 @@ COMMON_DIFF_OPTIONS_HELP;int cmd_diff_index (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct rev_info rev;unsigned int option =0 ;
builtin/diff-pairs.c +6 -5
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "diff.h"
@@ -28,7 +29,7 @@ static void parse_oid_or_die(const char *hex, struct object_id *oid, }int cmd_diff_pairs (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {struct strbuf path_dst =STRBUF_INIT ;struct strbuf path =STRBUF_INIT ;
@@ -46,7 +47,7 @@ int cmd_diff_pairs(int argc, const char **argv, const char *prefix, OPT_END() }; -repo_init_revisions (repo, &revs, prefix); +repo_init_revisions (the_repository, &revs, prefix);/* * Diff options are usually parsed implicitly as part of
@@ -56,7 +57,7 @@ int cmd_diff_pairs(int argc, const char **argv, const char *prefix, parseopts =add_diff_options (builtin_diff_pairs_options, &revs.diffopt);show_usage_with_options_if_asked (argc, argv, builtin_diff_pairs_usage, parseopts); -repo_config (repo, git_diff_basic_config,NULL ); +repo_config (the_repository, git_diff_basic_config,NULL ); revs.diffopt.no_free =1 ; revs.disable_stdin =1 ; revs.abbrev =0 ;
@@ -129,8 +130,8 @@ int cmd_diff_pairs(int argc, const char **argv, const char *prefix,if (S_ISDIR (mode_a) ||S_ISDIR (mode_b))die (_ ("tree objects not supported" )); -parse_oid_or_die (p, &oid_a, &p, repo->hash_algo); -parse_oid_or_die (p, &oid_b, &p, repo->hash_algo); +parse_oid_or_die (p, &oid_a, &p, the_repository->hash_algo); +parse_oid_or_die (p, &oid_b, &p, the_repository->hash_algo); status = *p++;
builtin/diff-tree.c +1 -1
@@ -112,7 +112,7 @@ static void diff_tree_tweak_rev(struct rev_info *rev)int cmd_diff_tree (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {char line[1000 ];struct object *tree1, *tree2;
builtin/diff.c +1 -1
@@ -399,7 +399,7 @@ static void symdiff_release(struct symdiff *sdiff)int cmd_diff (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i;struct rev_info rev;
builtin/difftool.c +7 -6
@@ -12,6 +12,7 @@* Copyright (C) 2016 Johannes Schindelin */ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "abspath.h"
@@ -717,7 +718,7 @@ static int run_file_diff(int prompt, const char *prefix,int cmd_difftool (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int use_gui_tool =-1 , dir_diff =0 , prompt =-1 , tool_help =0 , no_index =0 ;static char *difftool_cmd =NULL , *extcmd =NULL ;
@@ -753,7 +754,7 @@ int cmd_difftool(int argc, };struct child_process child =CHILD_PROCESS_INIT ; -repo_config (repo, difftool_config, &dt_options); +repo_config (the_repository, difftool_config, &dt_options); dt_options.symlinks = dt_options.has_symlinks; argc = parse_options(argc, argv, prefix, builtin_difftool_options,
@@ -767,9 +768,9 @@ int cmd_difftool(int argc,die (_ ("difftool requires worktree or --no-index" ));if (!no_index){ -setup_work_tree (repo); -setenv (GIT_DIR_ENVIRONMENT ,absolute_path (repo_get_git_dir (repo)),1 ); -setenv (GIT_WORK_TREE_ENVIRONMENT ,absolute_path (repo_get_work_tree (repo)),1 ); +setup_work_tree (the_repository); +setenv (GIT_DIR_ENVIRONMENT ,absolute_path (repo_get_git_dir (the_repository)),1 ); +setenv (GIT_WORK_TREE_ENVIRONMENT ,absolute_path (repo_get_work_tree (the_repository)),1 ); }else if (dir_diff)die (_ ("options '%s' and '%s' cannot be used together" ),"--dir-diff" ,"--no-index" );
@@ -818,6 +819,6 @@ int cmd_difftool(int argc,strvec_pushv (&child.args, argv);if (dir_diff) -return run_dir_diff (repo, &dt_options, extcmd, prefix, &child); +return run_dir_diff (the_repository, &dt_options, extcmd, prefix, &child);return run_file_diff (prompt, prefix, &child); }
builtin/fast-export.c +1 -1
@@ -1310,7 +1310,7 @@ static int parse_opt_anonymize_map(const struct option *opt,int cmd_fast_export (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct rev_info revs;struct commit *commit;
builtin/fast-import.c +2 -2
@@ -4123,7 +4123,7 @@ static int option_parse_quiet(const struct option *opt UNUSED,int cmd_fast_import (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {struct fast_import_state state;
@@ -4320,7 +4320,7 @@ int cmd_fast_import(int argc,fprintf (stderr," pools: %10lu KiB\n" , (unsigned long )((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024 ));fprintf (stderr," objects: %10" PRIuMAX" KiB\n" , (alloc_count*sizeof (struct object_entry ))/1024 );fprintf (stderr,"---------------------------------------------------------------------\n" ); -pack_report (repo); +pack_report (the_repository);fprintf (stderr,"---------------------------------------------------------------------\n" );fprintf (stderr,"\n" ); }
builtin/fetch-pack.c +1 -1
@@ -49,7 +49,7 @@ static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,int cmd_fetch_pack (int argc,const char **argv, const char *prefix UNUSED, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i, ret;struct ref *fetched_refs =NULL , *remote_refs =NULL ;
builtin/fetch.c +1 -1
@@ -2505,7 +2505,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv,int cmd_fetch (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct fetch_config config = { .display_format =DISPLAY_FORMAT_FULL ,
builtin/fmt-merge-msg.c +3 -2
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "fmt-merge-msg.h"
@@ -12,7 +13,7 @@ static const char * const fmt_merge_msg_usage[] = {int cmd_fmt_merge_msg (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {char *inpath =NULL ;const char *message =NULL ;
@@ -53,7 +54,7 @@ int cmd_fmt_merge_msg(int argc,int ret;struct fmt_merge_msg_opts opts; -repo_config (repo, fmt_merge_msg_config, &merge_log_config); +repo_config (the_repository, fmt_merge_msg_config, &merge_log_config); argc =parse_options (argc, argv, prefix, options, fmt_merge_msg_usage,0 );if (argc >0 )
builtin/for-each-ref.c +3 -2
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "commit.h" #include "config.h"
@@ -113,12 +114,12 @@ int for_each_ref_core(int argc, const char **argv, const char *prefix, struct reint cmd_for_each_ref (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {static char const *const for_each_ref_usage[] = {N_ ("git for-each-ref " COMMON_USAGE_FOR_EACH_REF ),NULL }; -return for_each_ref_core (argc, argv, prefix, repo, for_each_ref_usage); +return for_each_ref_core (argc, argv, prefix, the_repository, for_each_ref_usage); }
builtin/for-each-repo.c +1 -1
@@ -33,7 +33,7 @@ static int run_command_on_repo(const char *path, const char **argv)int cmd_for_each_repo (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {static const char *config_key =NULL ;int keep_going =0 ;
builtin/fsck.c +31 -30
146 lines not on this page · raw message
builtin/fsmonitor--daemon.c +2 -2
@@ -1568,7 +1568,7 @@ static int try_to_start_background_daemon(void)int cmd_fsmonitor__daemon (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *subcmd;enum fsmonitor_reason reason;
@@ -1631,7 +1631,7 @@ int cmd_fsmonitor__daemon(int argc, }#else -int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) +int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix UNUSED, bool has_repo UNUSED) {struct option options[] = { OPT_END()
builtin/gc.c +3 -3
@@ -582,7 +582,7 @@ static int maintenance_task_odb(struct maintenance_run_opts *opts,int cmd_gc (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int aggressive =0 ;int force =0 ;
@@ -3141,7 +3141,7 @@ static const char *const builtin_maintenance_usage[] = {int cmd_maintenance (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option builtin_maintenance_options[] = {
@@ -3156,5 +3156,5 @@ int cmd_maintenance(int argc, argc =parse_options (argc, argv, prefix, builtin_maintenance_options, builtin_maintenance_usage,0 ); -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/get-tar-commit-id.c +1 -1
@@ -15,7 +15,7 @@ static const char builtin_get_tar_commit_id_usage[] =int cmd_get_tar_commit_id (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {char buffer[HEADERSIZE ];struct ustar_header *header = (struct ustar_header *)buffer;
builtin/grep.c +1 -1
@@ -1035,7 +1035,7 @@ static int pattern_callback(const struct option *opt, const char *arg,int cmd_grep (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int hit =0 ;int cached =0 , untracked =0 , opt_exclude =-1 ;
builtin/hash-object.c +1 -1
@@ -64,7 +64,7 @@ static void hash_stdin_paths(const char *type, int no_filters, unsigned flags) int cmd_hash_object(int argc, const char **argv, const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) { static const char * const hash_object_usage[] = { N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]\n"
builtin/help.c +1 -1
@@ -662,7 +662,7 @@ static void opt_mode_usage(int argc, const char *opt_mode,int cmd_help (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int nongit;enum help_format parsed_help_format;
builtin/history.c +2 -2
@@ -1187,7 +1187,7 @@ static int cmd_history_drop(int argc,int cmd_history (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {const char *const usage[] = {GIT_HISTORY_DROP_USAGE ,
@@ -1206,5 +1206,5 @@ int cmd_history(int argc, }; argc =parse_options (argc, argv, prefix, options, usage,0 ); -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/hook.c +2 -2
@@ -192,7 +192,7 @@ static int run(int argc, const char **argv, const char *prefix,int cmd_hook (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option builtin_hook_options[] = {
@@ -204,5 +204,5 @@ int cmd_hook(int argc, argc =parse_options (argc, argv,NULL , builtin_hook_options, builtin_hook_usage,0 ); -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/index-pack.c +1 -1
@@ -1886,7 +1886,7 @@ static void repack_local_links(void)int cmd_index_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i, fix_thin_pack =0 , verify =0 , stat_only =0 , rev_index;const char *curr_index;
builtin/init-db.c +1 -1
@@ -72,7 +72,7 @@ static const char *const init_db_usage[] = {int cmd_init_db (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {char *git_dir;const char *real_git_dir =NULL ;
builtin/interpret-trailers.c +1 -1
@@ -139,7 +139,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,int cmd_interpret_trailers (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct process_trailer_options opts =PROCESS_TRAILER_OPTIONS_INIT ;LIST_HEAD (trailers);
builtin/last-modified.c +4 -3
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "git-compat-util.h" #include "bloom.h" #include "builtin.h"
@@ -518,7 +519,7 @@ static int last_modified_init(struct last_modified *lm, struct repository *r, }int cmd_last_modified (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int ret;struct last_modified lm = {0 };
@@ -546,9 +547,9 @@ int cmd_last_modified(int argc, const char **argv, const char *prefix,PARSE_OPT_KEEP_ARGV0 |PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_DASHDASH); -repo_config (repo, git_default_config,NULL ); +repo_config (the_repository, git_default_config,NULL ); - ret =last_modified_init (&lm, repo, prefix, argc, argv); + ret =last_modified_init (&lm, the_repository, prefix, argc, argv);if (ret >0 )usage_with_options (last_modified_usage, last_modified_options);
builtin/log.c +6 -6
@@ -526,7 +526,7 @@ static int git_log_config(const char *var, const char *value,int cmd_whatchanged (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct log_config cfg;struct rev_info rev;
@@ -661,7 +661,7 @@ static void show_setup_revisions_tweak(struct rev_info *rev)int cmd_show (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct log_config cfg;struct rev_info rev;
@@ -780,7 +780,7 @@ int cmd_show(int argc,int cmd_log_reflog (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct log_config cfg;struct rev_info rev;
@@ -825,7 +825,7 @@ static void log_setup_revisions_tweak(struct rev_info *rev)int cmd_log (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct log_config cfg;struct rev_info rev;
@@ -1955,7 +1955,7 @@ static void infer_range_diff_ranges(struct strbuf *r1,int cmd_format_patch (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct format_config cfg;struct commit *commit;
@@ -2735,7 +2735,7 @@ static void prefetch_cherry_blobs(struct repository *repo,int cmd_cherry (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct rev_info revs;struct patch_ids ids;
builtin/ls-files.c +12 -11
@@ -6,6 +6,7 @@* Copyright (C) Linus Torvalds, 2005 */ +#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -590,7 +591,7 @@ static int option_parse_exclude_standard(const struct option *opt,int cmd_ls_files (int argc,const char **argv,const char *cmd_prefix, -struct repository *repo) + bool has_repo UNUSED) {int require_work_tree =0 , show_tag =0 , i;char *max_prefix;
@@ -670,15 +671,15 @@ int cmd_ls_files(int argc,show_usage_with_options_if_asked (argc, argv, ls_files_usage, builtin_ls_files_options); -prepare_repo_settings (repo); - repo->settings.command_requires_full_index =0 ; +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; prefix = cmd_prefix;if (prefix) prefix_len =strlen (prefix); -repo_config (repo, git_default_config,NULL ); +repo_config (the_repository, git_default_config,NULL ); -if (repo_read_index (repo) <0 ) +if (repo_read_index (the_repository) <0 )die ("index file corrupt" ); argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -717,8 +718,8 @@ int cmd_ls_files(int argc,if (dir.exclude_per_dir) exc_given =1 ; -if (require_work_tree && !is_inside_work_tree (repo)) -setup_work_tree (repo); +if (require_work_tree && !is_inside_work_tree (the_repository)) +setup_work_tree (the_repository); if (recurse_submodules && (show_deleted || show_others || show_unmerged ||
@@ -747,7 +748,7 @@ int cmd_ls_files(int argc, max_prefix =common_prefix (&pathspec); max_prefix_len =get_common_prefix_len (max_prefix); -prune_index (repo->index, max_prefix, max_prefix_len); +prune_index (the_repository->index, max_prefix, max_prefix_len);/* Treat unmatching pathspec elements as errors */ if (pathspec.nr && error_unmatch)
@@ -771,13 +772,13 @@ int cmd_ls_files(int argc,*/ if (show_stage || show_unmerged)die (_ ("options '%s' and '%s' cannot be used together" ),"ls-files --with-tree" ,"-s/-u" ); -overlay_tree_on_index (repo->index, with_tree, max_prefix); +overlay_tree_on_index (the_repository->index, with_tree, max_prefix); } -show_files (repo, &dir); +show_files (the_repository, &dir);if (show_resolve_undo) -show_ru_info (repo, repo->index); +show_ru_info (the_repository, the_repository->index);if (ps_matched &&report_path_error (ps_matched, &pathspec)) {fprintf (stderr,"Did you forget to 'git add'?\n" );
builtin/ls-remote.c +1 -1
@@ -41,7 +41,7 @@ static int tail_match(const struct strvec *pattern, const char *path)int cmd_ls_remote (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *dest =NULL ;unsigned flags =0 ;
builtin/ls-tree.c +1 -1
@@ -338,7 +338,7 @@ static struct ls_tree_cmdmode_to_fmt ls_tree_cmdmode_format[] = {int cmd_ls_tree (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct object_id oid;struct tree *tree;
builtin/mailinfo.c +1 -1
@@ -52,7 +52,7 @@ static int parse_opt_quoted_cr(const struct option *opt, const char *arg, int unint cmd_mailinfo (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct metainfo_charset meta_charset;struct mailinfo mi;
builtin/mailsplit.c +1 -1
@@ -274,7 +274,7 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,int cmd_mailsplit (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int nr =0 , nr_prec =4 , num =0 ;int allow_bare =0 ;
builtin/merge-base.c +1 -1
@@ -149,7 +149,7 @@ static int handle_fork_point(int argc, const char **argv)int cmd_merge_base (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct commit **rev;size_t rev_nr =0 ;
builtin/merge-file.c +3 -3
@@ -60,7 +60,7 @@ static int diff_algorithm_cb(const struct option *opt,int cmd_merge_file (int argc,const char **argv,const char *prefix, -struct repository *repo) +bool has_repo) {const char *names[3 ] = {0 };mmfile_t mmfs[3 ] = {0 };
@@ -96,7 +96,7 @@ int cmd_merge_file(int argc, xmp.favor =0 ;/* Read the configuration file */ -repo_config (repo, git_xmerge_config,NULL ); +repo_config (the_repository, git_xmerge_config,NULL );if (0 <= git_xmerge_style) xmp.style = git_xmerge_style;
@@ -108,7 +108,7 @@ int cmd_merge_file(int argc,return error_errno ("failed to redirect stderr to /dev/null" ); } -if (!repo && object_id) +if (!has_repo && object_id)/* emit the correct "not a git repo" error in this case */ setup_git_directory (the_repository);
builtin/merge-index.c +1 -1
@@ -81,7 +81,7 @@ static const char usage_string[] =int cmd_merge_index (int argc,const char **argv, const char *prefix UNUSED, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i, force_file =0 ;
builtin/merge-ours.c +7 -6
@@ -8,6 +8,7 @@* Pretend we resolved the heads, but declare our tree trumps everybody else. */ +#define USE_THE_REPOSITORY_VARIABLE #include "git-compat-util.h" #include "builtin.h" #include "config.h"
@@ -20,22 +21,22 @@ static const char builtin_merge_ours_usage[] =int cmd_merge_ours (int argc,const char **argv, const char *prefix UNUSED, -struct repository *repo) + bool has_repo UNUSED) {show_usage_if_asked (argc, argv, builtin_merge_ours_usage); -repo_config (repo, git_default_config,NULL ); -prepare_repo_settings (repo); - repo->settings.command_requires_full_index =0 ; +repo_config (the_repository, git_default_config,NULL ); +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ;/* * The contents of the current index becomes the tree we * commit. The index must match HEAD, or this merge cannot go * through. */ -if (repo_read_index (repo) <0 ) +if (repo_read_index (the_repository) <0 )die_errno ("read_cache failed" ); -if (index_differs_from (repo,"HEAD" ,NULL ,0 )) +if (index_differs_from (the_repository,"HEAD" ,NULL ,0 ))return 2 ;return 0 ; }
builtin/merge-recursive.c +1 -1
@@ -24,7 +24,7 @@ static char *better_branch_name(const char *branch)int cmd_merge_recursive (int argc,const char **argv, const char *prefix UNUSED, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct object_id bases[21 ];unsigned bases_count =0 ;
builtin/merge-tree.c +1 -1
@@ -539,7 +539,7 @@ static int real_merge(struct merge_tree_options *o,int cmd_merge_tree (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct merge_tree_options o = { .show_messages =-1 };struct strvec xopts =STRVEC_INIT ;
builtin/merge.c +1 -1
@@ -1363,7 +1363,7 @@ static int merging_a_throwaway_tag(struct commit *commit)int cmd_merge (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct object_id result_tree, stash, head_oid;struct commit *head_commit;
builtin/mktag.c +2 -2
@@ -75,7 +75,7 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) int cmd_mktag(int argc, const char **argv, const char *prefix, - struct repository *repo) + bool has_repo UNUSED) { static struct option builtin_mktag_options[] = { OPT_BOOL(0, "strict", &option_strict,
@@ -94,7 +94,7 @@ int cmd_mktag(int argc,if (strbuf_read (&buf,0 ,0 ) <0 )die_errno (_ ("could not read from stdin" )); -fsck_options_init (&fsck_options, repo,FSCK_OPTIONS_STRICT ); +fsck_options_init (&fsck_options, the_repository,FSCK_OPTIONS_STRICT ); fsck_options.error_func = mktag_fsck_error_func;fsck_set_msg_type_from_ids (&fsck_options,FSCK_MSG_EXTRA_HEADER_ENTRY ,FSCK_WARN );
builtin/mktree.c +4 -3
@@ -3,6 +3,7 @@* * Copyright (c) Junio C Hamano, 2006, 2009 */ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "gettext.h" #include "hex.h"
@@ -154,7 +155,7 @@ static void mktree_line(struct repository *repo, char *buf, int nul_term_line, iint cmd_mktree (int ac,const char **av,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {struct strbuf sb =STRBUF_INIT ;struct object_id oid;
@@ -186,7 +187,7 @@ int cmd_mktree(int ac,break ;die ("input format error: (blank line only valid in batch mode)" ); } -mktree_line (repo, sb.buf, nul_term_line, allow_missing); +mktree_line (the_repository, sb.buf, nul_term_line, allow_missing); }if (is_batch_mode && got_eof && used <1 ) {/*
@@ -196,7 +197,7 @@ int cmd_mktree(int ac,*/ ;/* skip creating an empty tree */ }else { -write_tree (repo, &oid); +write_tree (the_repository, &oid);puts (oid_to_hex (&oid));fflush (stdout); }
builtin/multi-pack-index.c +2 -2
@@ -405,7 +405,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,int cmd_multi_pack_index (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int res;parse_opt_subcommand_fn *fn =NULL ;
@@ -432,7 +432,7 @@ int cmd_multi_pack_index(int argc, builtin_multi_pack_index_usage, 0);FREE_AND_NULL (options); - res =fn (argc, argv, prefix, repo); + res =fn (argc, argv, prefix, the_repository);free (opts.object_dir);return res;
builtin/mv.c +1 -1
@@ -215,7 +215,7 @@ static int pathmap_cmp(const void *cmp_data UNUSED,int cmd_mv (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i, flags, gitmodules_modified =0 ;int verbose =0 , show_only =0 , force =0 , ignore_errors =0 , ignore_sparse =0 ;
builtin/name-rev.c +2 -2
@@ -636,7 +636,7 @@ static void name_rev_line(char *p, struct command *cmd)int cmd_name_rev (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct mem_pool string_pool;struct object_array revs =OBJECT_ARRAY_INIT ;
@@ -811,7 +811,7 @@ static char const *const format_rev_usage[] = {int cmd_format_rev (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *format =NULL ;enum stdin_mode stdin_mode;
builtin/notes.c +2 -2
@@ -1127,7 +1127,7 @@ static int get_ref(int argc, const char **argv, const char *prefix,int cmd_notes (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {const char *override_notes_ref =NULL ;parse_opt_subcommand_fn *fn =NULL ;
@@ -1166,5 +1166,5 @@ int cmd_notes(int argc,strbuf_release (&sb); } -return !!fn (argc, argv, prefix, repo); +return !!fn (argc, argv, prefix, the_repository); }
builtin/pack-objects.c +1 -1
@@ -5118,7 +5118,7 @@ static int parse_stdin_packs_mode(const struct option *opt, const char *arg,int cmd_pack_objects (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int use_internal_rev_list =0 ;int all_progress_implied =0 ;
builtin/pack-redundant.c +1 -1
@@ -587,7 +587,7 @@ static void load_all(void)add_pack (p); } -int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, struct repository *repo UNUSED) { +int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, bool has_repo UNUSED) {int i;int i_still_use_this =0 ;struct pack_list *min =NULL , *red, *pl;struct llist *ignore;struct strbuf idx_name =STRBUF_INIT ;
builtin/pack-refs.c +3 -2
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "gettext.h" #include "pack-refs.h"
@@ -5,12 +6,12 @@int cmd_pack_refs (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {static char const *const pack_refs_usage[] = {N_ ("git pack-refs " PACK_REFS_OPTS ),NULL }; -return pack_refs_core (argc, argv, prefix, repo, pack_refs_usage); +return pack_refs_core (argc, argv, prefix, the_repository, pack_refs_usage); }
builtin/patch-id.c +1 -1
@@ -222,7 +222,7 @@ static int git_patch_id_config(const char *var, const char *value,int cmd_patch_id (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {/* if nothing is set, default to unstable */ struct patch_id_opts config = {0 ,0 };
builtin/prune-packed.c +1 -1
@@ -11,7 +11,7 @@ static const char * const prune_packed_usage[] = {int cmd_prune_packed (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int opts =isatty (2 ) ?PRUNE_PACKED_VERBOSE :0 ;const struct option prune_packed_options[] = {
builtin/prune.c +10 -9
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #define DISABLE_SIGN_COMPARE_WARNINGS #include "builtin.h"
@@ -153,7 +154,7 @@ static void remove_temporary_files(const char *path)int cmd_prune (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {struct rev_info revs;int exclude_promisor_objects =0 ;
@@ -175,16 +176,16 @@ int cmd_prune(int argc, argc =parse_options (argc, argv, prefix, options, prune_usage,0 ); -repo_init_revisions (repo, &revs, prefix); -if (repo->repository_format_precious_objects) +repo_init_revisions (the_repository, &revs, prefix); +if (the_repository->repository_format_precious_objects)die (_ ("cannot prune in a precious-objects repo" ));while (argc--) {struct object_id oid;const char *name = *argv++; -if (!repo_get_oid (repo, name, &oid)) { -struct object *object =parse_object_or_die (repo, &oid, name); +if (!repo_get_oid (the_repository, name, &oid)) { +struct object *object =parse_object_or_die (the_repository, &oid, name);add_pending_object (&revs, object,"" ); }else
@@ -198,16 +199,16 @@ int cmd_prune(int argc, revs.exclude_promisor_objects =1 ; } -for_each_loose_file_in_source (repo->objects->sources, +for_each_loose_file_in_source (the_repository->objects->sources, prune_object, prune_cruft, prune_subdir, &revs);prune_packed_objects (show_only ?PRUNE_PACKED_DRY_RUN :0 ); -remove_temporary_files (repo_get_object_directory (repo)); - s =mkpathdup ("%s/pack" ,repo_get_object_directory (repo)); +remove_temporary_files (repo_get_object_directory (the_repository)); + s =mkpathdup ("%s/pack" ,repo_get_object_directory (the_repository));remove_temporary_files (s);free (s); -if (is_repository_shallow (repo)) { +if (is_repository_shallow (the_repository)) {perform_reachability_traversal (&revs);prune_shallow (show_only ?PRUNE_SHOW_ONLY :0 ); }
builtin/pull.c +1 -1
@@ -856,7 +856,7 @@ static void show_advice_pull_non_ff(void)int cmd_pull (int argc,const char **argv,const char *prefix, - struct repository *repository UNUSED) + bool has_repo UNUSED) {const char *repo, **refspecs;struct oid_array merge_heads =OID_ARRAY_INIT ;
builtin/push.c +1 -1
@@ -691,7 +691,7 @@ static void die_if_repo_looks_like_ref(const char *repo)int cmd_push (int argc,const char **argv,const char *prefix, - struct repository *repository UNUSED) + bool has_repo UNUSED) {int flags =0 ;int tags =0 ;
builtin/range-diff.c +1 -1
@@ -35,7 +35,7 @@ static int parse_max_memory(const struct option *opt, const char *arg, int unsetint cmd_range_diff (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct diff_options diffopt = {NULL };struct strvec log_arg =STRVEC_INIT ;
builtin/read-tree.c +1 -1
@@ -111,7 +111,7 @@ static int git_read_tree_config(const char *var, const char *value,int cmd_read_tree (int argc,const char **argv,const char *cmd_prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i, stage =0 ;struct object_id oid;
builtin/rebase.c +1 -1
@@ -1099,7 +1099,7 @@ static int check_exec_cmd(const char *cmd)int cmd_rebase (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct rebase_options options =REBASE_OPTIONS_INIT ;const char *branch_name;
builtin/receive-pack.c +1 -1
@@ -2612,7 +2612,7 @@ static int delete_only(struct command *commands)int cmd_receive_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int advertise_refs =0 ;struct command *commands;
builtin/reflog.c +3 -3
@@ -467,7 +467,7 @@ static int cmd_reflog_write(int argc, const char **argv, const char *prefix,int cmd_reflog (int argc,const char **argv,const char *prefix, -struct repository *repository) +bool has_repo) {parse_opt_subcommand_fn *fn =NULL ;struct option options[] = {
@@ -486,7 +486,7 @@ int cmd_reflog(int argc,PARSE_OPT_KEEP_DASHDASH |PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);if (fn) -return fn (argc -1 , argv +1 , prefix, repository); +return fn (argc -1 , argv +1 , prefix, the_repository);else -return cmd_log_reflog (argc, argv, prefix, repository); +return cmd_log_reflog (argc, argv, prefix, has_repo); }
builtin/refs.c +3 -2
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "fsck.h"
@@ -377,7 +378,7 @@ static int cmd_refs_rename(int argc, const char **argv, const char *prefix,int cmd_refs (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {const char *const refs_usage[] = {REFS_MIGRATE_USAGE ,
@@ -406,5 +407,5 @@ int cmd_refs(int argc, }; argc =parse_options (argc, argv, prefix, opts, refs_usage,0 ); -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/remote-ext.c +1 -1
@@ -198,7 +198,7 @@ static int command_loop(const char *child)int cmd_remote_ext (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {BUG_ON_NON_EMPTY_PREFIX (prefix);
builtin/remote-fd.c +1 -1
@@ -56,7 +56,7 @@ static void command_loop(int input_fd, int output_fd)int cmd_remote_fd (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int input_fd =-1 ;int output_fd =-1 ;
builtin/remote.c +2 -2
@@ -1933,7 +1933,7 @@ static int set_url(int argc, const char **argv, const char *prefix,int cmd_remote (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option options[] = {
@@ -1956,7 +1956,7 @@ int cmd_remote(int argc, PARSE_OPT_SUBCOMMAND_OPTIONAL);if (fn) { -return !!fn (argc, argv, prefix, repo); +return !!fn (argc, argv, prefix, the_repository); }else {if (argc) {error (_ ("unknown subcommand: `%s'" ), argv[0 ]);
builtin/repack.c +25 -24
172 lines not on this page · raw message
builtin/replace.c +1 -1
@@ -545,7 +545,7 @@ static int convert_graft_file(int force)int cmd_replace (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int force =0 ;int raw =0 ;
builtin/replay.c +6 -5
@@ -2,6 +2,7 @@* "git replay" builtin command */ +#define USE_THE_REPOSITORY_VARIABLE #include "git-compat-util.h" #include "builtin.h"
@@ -69,7 +70,7 @@ static int handle_ref_update(enum ref_action_mode mode,int cmd_replay (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {struct replay_revisions_options opts = {0 };struct replay_result result = {0 };
@@ -134,7 +135,7 @@ int cmd_replay(int argc, !!opts.contained, "--contained");/* Parse ref action mode from command line or config */ - ref_mode =get_ref_action_mode (repo, ref_action); + ref_mode =get_ref_action_mode (the_repository, ref_action);/* * Cherry-pick/rebase need oldest-first ordering so that each
@@ -144,7 +145,7 @@ int cmd_replay(int argc,*/ desired_reverse = !opts.revert; -repo_init_revisions (repo, &revs, prefix); +repo_init_revisions (the_repository, &revs, prefix);/* * Set desired values for rev walking options here. If they
@@ -206,14 +207,14 @@ int cmd_replay(int argc,strbuf_addf (&reflog_msg,"replay --advance %s" , opts.advance); }else {struct object_id oid; -if (repo_get_oid_committish (repo, opts.onto, &oid)) +if (repo_get_oid_committish (the_repository, opts.onto, &oid))BUG ("--onto commit should have been resolved beforehand already" );strbuf_addf (&reflog_msg,"replay --onto %s" ,oid_to_hex (&oid)); }/* Initialize ref transaction if using update mode */ if (ref_mode ==REF_ACTION_UPDATE ) { - transaction =ref_store_transaction_begin (get_main_ref_store (repo), + transaction =ref_store_transaction_begin (get_main_ref_store (the_repository),0 , &transaction_err);if (!transaction) { ret = error(_("failed to begin ref transaction: %s"),
builtin/repo.c +2 -2
@@ -981,7 +981,7 @@ static int cmd_repo_structure(int argc, const char **argv, const char *prefix, }int cmd_repo (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option options[] = {
@@ -992,5 +992,5 @@ int cmd_repo(int argc, const char **argv, const char *prefix, argc =parse_options (argc, argv, prefix, options, repo_usage,0 ); -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/rerere.c +1 -1
@@ -53,7 +53,7 @@ static int diff_two(const char *file1, const char *label1,int cmd_rerere (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct string_list merge_rr =STRING_LIST_INIT_DUP ;int autoupdate =-1 , flags =0 ;
builtin/reset.c +1 -1
@@ -336,7 +336,7 @@ static int git_reset_config(const char *var, const char *value,int cmd_reset (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int reset_type =NONE , update_ref_status =0 , quiet =0 ;int no_refresh =0 ;
builtin/rev-list.c +1 -1
@@ -692,7 +692,7 @@ static void prepare_maximal_independent(struct rev_info *revs)int cmd_rev_list (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct rev_info revs;struct rev_list_info info;
builtin/rev-parse.c +1 -1
@@ -698,7 +698,7 @@ static void print_path(const char *path, const char *prefix,int cmd_rev_parse (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i, as_is =0 , verify =0 , quiet =0 , revs_count =0 , type =0 ;const struct git_hash_algo *output_algo =NULL ;
builtin/revert.c +4 -4
@@ -281,7 +281,7 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,int cmd_revert (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct replay_opts opts =REPLAY_OPTS_INIT ;int res;
@@ -299,9 +299,9 @@ int cmd_revert(int argc, }int cmd_cherry_pick (int argc, -const char **argv, -const char *prefix, -struct repository *repo UNUSED) +const char **argv, +const char *prefix, + bool has_repo UNUSED) {struct replay_opts opts =REPLAY_OPTS_INIT ;int res;
builtin/rm.c +1 -1
@@ -265,7 +265,7 @@ static struct option builtin_rm_options[] = {int cmd_rm (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct lock_file lock_file =LOCK_INIT ;int i, ret =0 ;
builtin/send-pack.c +5 -4
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "environment.h"
@@ -151,7 +152,7 @@ static int send_pack_config(const char *k, const char *v,int cmd_send_pack (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {struct refspec rs;const char *remote_name =NULL ;
@@ -212,10 +213,10 @@ int cmd_send_pack(int argc, OPT_END() }; -repo_config (repo, send_pack_config,NULL ); +repo_config (the_repository, send_pack_config,NULL ); argc =parse_options (argc, argv, prefix, options, send_pack_usage,0 ); -refspec_init_push (&rs, repo->hash_algo); +refspec_init_push (&rs, the_repository->hash_algo);if (argc >0 ) { dest = argv[0 ];
@@ -322,7 +323,7 @@ int cmd_send_pack(int argc,set_ref_status_for_push (remote_refs, args.send_mirror, args.force_update); - ret =send_pack (repo, &args, fd, conn, remote_refs, &extra_have); + ret =send_pack (the_repository, &args, fd, conn, remote_refs, &extra_have);if (helper_status)print_helper_status (remote_refs);
builtin/shortlog.c +1 -1
@@ -383,7 +383,7 @@ void shortlog_finish_setup(struct shortlog *log)int cmd_shortlog (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct shortlog log = {STRING_LIST_INIT_NODUP };struct rev_info rev;
builtin/show-branch.c +1 -1
@@ -636,7 +636,7 @@ static int parse_reflog_param(const struct option *opt, const char *arg,int cmd_show_branch (int ac,const char **av,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct commit *rev[MAX_REVS ], *commit;char *reflog_msg[MAX_REVS ] = {0 };
builtin/show-index.c +1 -1
@@ -16,7 +16,7 @@ static const char *const show_index_usage[] = {int cmd_show_index (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int i;unsigned nr;
builtin/show-ref.c +3 -3
@@ -298,9 +298,9 @@ static int exclude_existing_callback(const struct option *opt, const char *arg, }int cmd_show_ref (int argc, -const char **argv, -const char *prefix, -struct repository *repo UNUSED) +const char **argv, +const char *prefix, + bool has_repo UNUSED) {struct exclude_existing_options exclude_existing_opts = {0 };struct patterns_options patterns_opts = {0 };
builtin/sparse-checkout.c +4 -4
@@ -1183,7 +1183,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *int cmd_sparse_checkout (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option builtin_sparse_checkout_options[] = {
@@ -1204,8 +1204,8 @@ int cmd_sparse_checkout(int argc,repo_config (the_repository, git_default_config,NULL ); -prepare_repo_settings (repo); - repo->settings.command_requires_full_index =0 ; +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/stash.c +3 -3
@@ -2453,7 +2453,7 @@ static int export_stash(int argc,int cmd_stash (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {pid_t pid =getpid ();const char *index_file;
@@ -2493,9 +2493,9 @@ int cmd_stash(int argc, (uintmax_t)pid);if (fn) -return !!fn (argc, argv, prefix, repo); +return !!fn (argc, argv, prefix, the_repository);else if (!argc) -return !!push_stash_unassumed (0 ,NULL , prefix, repo); +return !!push_stash_unassumed (0 ,NULL , prefix, the_repository);/* Assume 'stash push' */ strvec_push (&args,"push" );
builtin/stripspace.c +1 -1
@@ -33,7 +33,7 @@ enum stripspace_mode {int cmd_stripspace (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct strbuf buf =STRBUF_INIT ;enum stripspace_mode mode =STRIP_DEFAULT ;
builtin/submodule--helper.c +2 -2
@@ -3804,7 +3804,7 @@ static int module_add(int argc, const char **argv, const char *prefix,int cmd_submodule__helper (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;const char *const usage[] = {
@@ -3833,5 +3833,5 @@ int cmd_submodule__helper(int argc, }; argc =parse_options (argc, argv, prefix, options, usage,0 ); -return fn (argc, argv, prefix, repo); +return fn (argc, argv, prefix, the_repository); }
builtin/symbolic-ref.c +1 -1
@@ -46,7 +46,7 @@ static int check_symref(const char *HEAD, int quiet, int shorten, int recurse, iint cmd_symbolic_ref (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int quiet =0 , delete =0 , shorten =0 , recurse =1 , ret =0 ;const char *msg =NULL ;
builtin/tag.c +1 -1
@@ -454,7 +454,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)int cmd_tag (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct strbuf buf =STRBUF_INIT ;struct strbuf ref =STRBUF_INIT ;
builtin/unpack-file.c +1 -1
@@ -34,7 +34,7 @@ static const char usage_msg[] =int cmd_unpack_file (int argc,const char **argv, const char *prefix UNUSED, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct object_id oid;
builtin/unpack-objects.c +2 -2
@@ -617,7 +617,7 @@ static void unpack_all(void)int cmd_unpack_objects (int argc,const char **argv, const char *prefix UNUSED, -struct repository *repo) + bool has_repo UNUSED) {int i;struct object_id oid;
@@ -631,7 +631,7 @@ int cmd_unpack_objects(int argc,show_usage_if_asked (argc, argv, unpack_usage); -fsck_options_init (&fsck_options, repo,FSCK_OPTIONS_STRICT ); +fsck_options_init (&fsck_options, the_repository,FSCK_OPTIONS_STRICT );for (i =1 ; i < argc; i++) {const char *arg = argv[i];
builtin/update-index.c +1 -1
@@ -915,7 +915,7 @@ static enum parse_opt_result reupdate_callback(int cmd_update_index (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int newfd, entries, has_errors =0 , nul_term_line =0 ;enum uc_mode untracked_cache =UC_UNSPECIFIED ;
builtin/update-ref.c +1 -1
@@ -810,7 +810,7 @@ static void update_refs_stdin(unsigned int flags)int cmd_update_ref (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *refname, *oldval;struct object_id oid, oldoid;
builtin/update-server-info.c +4 -3
@@ -1,3 +1,4 @@ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "environment.h"
@@ -13,7 +14,7 @@ static const char * const update_server_info_usage[] = {int cmd_update_server_info (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int force =0 ;struct option options[] = {
@@ -21,12 +22,12 @@ int cmd_update_server_info(int argc, OPT_END() }; -repo_config (repo, git_default_config,NULL ); +repo_config (the_repository, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, options, update_server_info_usage,0 );if (argc >0 )usage_with_options (update_server_info_usage, options); -return !!update_server_info (repo, force); +return !!update_server_info (the_repository, force); }
builtin/upload-archive.c +4 -4
@@ -21,7 +21,7 @@ static const char deadchild[] =int cmd_upload_archive_writer (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct strvec sent_argv =STRVEC_INIT ;const char *arg_cmd ="argument " ;
@@ -85,9 +85,9 @@ static ssize_t process_input(int child_fd, int band) }int cmd_upload_archive (int argc, -const char **argv, -const char *prefix, -struct repository *repo UNUSED) +const char **argv, +const char *prefix, + bool has_repo UNUSED) {struct child_process writer =CHILD_PROCESS_INIT ;
builtin/upload-pack.c +1 -1
@@ -22,7 +22,7 @@ static const char * const upload_pack_usage[] = {int cmd_upload_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const char *dir;int strict =0 ;
builtin/url-parse.c +1 -1
@@ -100,7 +100,7 @@ static char *extract_component(enum url_component component,int cmd_url_parse (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {struct url_info info;enum url_component selected =URL_NONE ;
builtin/var.c +1 -1
@@ -217,7 +217,7 @@ static int show_config(const char *var, const char *value,int cmd_var (int argc,const char **argv, const char *prefix UNUSED, - struct repository *repo UNUSED) + bool has_repo UNUSED) {const struct git_var *git_var;char *val;
builtin/verify-commit.c +4 -3
@@ -5,6 +5,7 @@* * Based on git-verify-tag */ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "environment.h"
@@ -54,7 +55,7 @@ static int verify_commit(struct repository *repo, const char *name, unsigned flaint cmd_verify_commit (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -64,7 +65,7 @@ int cmd_verify_commit(int argc, OPT_END() }; -repo_config (repo, git_default_config,NULL ); +repo_config (the_repository, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_commit_options, verify_commit_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -78,7 +79,7 @@ int cmd_verify_commit(int argc,* was received in the process of writing the gpg input: */ signal (SIGPIPE ,SIG_IGN );while (i < argc) -if (verify_commit (repo, argv[i++], flags)) +if (verify_commit (the_repository, argv[i++], flags)) had_error =1 ;return had_error; }
builtin/verify-pack.c +1 -1
@@ -66,7 +66,7 @@ static const char * const verify_pack_usage[] = {int cmd_verify_pack (int argc,const char **argv,const char *prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int err =0 ;unsigned int flags =0 ;
builtin/verify-tag.c +5 -4
@@ -5,6 +5,7 @@* * Based on git-verify-tag.sh */ +#define USE_THE_REPOSITORY_VARIABLE #include "builtin.h" #include "config.h" #include "environment.h"
@@ -23,7 +24,7 @@ static const char * const verify_tag_usage[] = {int cmd_verify_tag (int argc,const char **argv,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {int i =1 , verbose =0 , had_error =0 ;unsigned flags =0 ;
@@ -35,7 +36,7 @@ int cmd_verify_tag(int argc, OPT_END() }; -repo_config (repo, git_default_config,NULL ); +repo_config (the_repository, git_default_config,NULL ); argc =parse_options (argc, argv, prefix, verify_tag_options, verify_tag_usage,PARSE_OPT_KEEP_ARGV0 );
@@ -56,12 +57,12 @@ int cmd_verify_tag(int argc,struct object_id oid;const char *name = argv[i++]; -if (repo_get_oid (repo, name, &oid)) { +if (repo_get_oid (the_repository, name, &oid)) { had_error = !!error ("tag '%s' not found." , name);continue ; } -if (gpg_verify_tag (repo, &oid, name, flags)) { +if (gpg_verify_tag (the_repository, &oid, name, flags)) { had_error =1 ;continue ; }
builtin/worktree.c +2 -2
@@ -1469,7 +1469,7 @@ static int repair(int ac, const char **av, const char *prefix,int cmd_worktree (int ac,const char **av,const char *prefix, -struct repository *repo) + bool has_repo UNUSED) {parse_opt_subcommand_fn *fn =NULL ;struct option options[] = {
@@ -1494,5 +1494,5 @@ int cmd_worktree(int ac,prepare_repo_settings (the_repository); the_repository->settings.command_requires_full_index =0 ; -return fn (ac, av, prefix, repo); +return fn (ac, av, prefix, the_repository); }
builtin/write-tree.c +1 -1
@@ -21,7 +21,7 @@ static const char * const write_tree_usage[] = {int cmd_write_tree (int argc,const char **argv,const char *cmd_prefix, - struct repository *repo UNUSED) + bool has_repo UNUSED) {int flags =0 , ret;const char *tree_prefix =NULL ;
git.c +2 -2
@@ -32,7 +32,7 @@struct cmd_struct {const char *cmd; -int (*fn)(int ,const char **,const char *,struct repository *); +int (*fn)(int ,const char **,const char *,bool );unsigned int option; };
@@ -507,7 +507,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv, structtrace2_cmd_name (p->cmd);validate_cache_entries (repo->index); - status = p->fn (argc, argv, prefix, no_repo ?NULL : repo); + status = p->fn (argc, argv, prefix, !no_repo);validate_cache_entries (repo->index);if (status)
help.c +1 -1
@@ -830,7 +830,7 @@ void get_version_info(struct strbuf *buf, int show_build_options) } } -int cmd_version(int argc, const char **argv, const char *prefix, struct repository *repository UNUSED) +int cmd_version(int argc, const char **argv, const char *prefix, bool has_repo UNUSED) {struct strbuf buf =STRBUF_INIT ;int build_options =0 ;
Re: [PATCH] do not pass "repo" to builtin commmand implementations
This would certainly help make it more obvious as not use the pointer parameter. But would you not consider to eventually move towards something more efiicient in the future?
Re: [PATCH] do not pass "repo" to builtin commmand implementations
Quoted 3 lines · Hardik Kumar
This would certainly help make it more obvious as not use the pointer parameter. But would you not consider to eventually move towards something more efiicient in the future?
It is unclear what kind of more efficient alternative you have in mind.
The primary motivation for this change is to make the implementation of built-in commands less error-prone and harder to abuse. The implementation of 'git foo' in cmd_foo() in builtin/foo.c performs one-time initialization (such as calling git_config()) and finalization that cannot be repeated, making it an anti-pattern to call cmd_foo() from within cmd_bar(). Refraining from pretending these functions can operate on an arbitrary caller-supplied repository instance is one step toward preventing such misuse.
However, another motivation for this change is to encourage developers who want to aid the libification effort to refactor code out of cmd_foo() into reusable helper functions. Instead of keeping those helpers within builtin/foo.c, they can be moved to library files outside the builtin/ directory and take a pointer to 'struct repository'.
The top-level cmd_foo() can still access 'the_repository' directly and orchestrate the execution of 'git foo' by passing a pointer to these helper functions. That way, more code becomes reusable.
Quoted 36 lines · Junio C Hamano
Quoted 9 lines · Hardik Kumar
Quoted 5 lines
In general, builtin/foo.c::cmd_foo() are concrete programs that work on specific repository (i.e.,
the_repository), and there is not much reason to rewrite the use ofthe_repositoryto use "repo" given by the caller which is git potty. You'd also need to deal with the case where "repo" is NULL (hint: "cd / && git foo -h").Right, but would safety check be required for single instance or better to find and work on only the specific ones which could lead to an exception.
I do not quite get what you mean. In a single function you use
the_repositoryand repo interchangeably, relying on repo, when it is not NULL), being the same asthe_repositoryfor correctness. If they ever refer to different things, then your updated code is less consistent than the original, which would not be an improvement.I actually think a good medium size project is to fix the last parameter given to
cmd_foo()built-in command implementations that is a pointer to "struct repository". It was a misguided design.This pointer is either NULL or
the_repository; it never takes any other value. It is misleading and invites confusion that these functions can take arbitrary repository instance. It also invites people to replace all references incmd_foo()to "the_repository" with "repo", which may lead to an error. Depending on whereparse_options()call appears in thecmd_foo(), "cd / && git foo -h" will cause segfaults with such a change.If we change the function signature of
cmd_foo()to receive a "bool" that says "true" if the command was run inside a repository and "false" otherwise,cmd_foo()can still tell if the command was run outside a repository, and programmers will not be misled to use anything other than "the_repository" as the repository to work on.
Quoted 7 lines
The utility functions
builtin/foo.cborrows from outside builtin/ directory are being "libified" to reduce the hardcoded dependence onthe_repository, andcmd_foo()can call these functions withthe_repositoryas a parameter. But we have no reason to waste our time updating (and also reviewing patches that make such updates) the built-in implementations themselves to take a pointer to an arbitrary repository.
Understood. Thanks for the clarification.
Quoted 9 lines · Junio C Hamano
Quoted 5 lines
The patch leaves some instances of this case that would require additional changes rather than simply replacing the arguments passed to the function call.
- Only update sites that do not introduce any functional changes.
How did you make sure that the places touched by this patch do not?
How do you deal with repo==NULL case in these code paths?
I verified by running the entire test suite which passed fine. In addition to that the methods in repository.c should already handle such a case if the repo were to ever be NULL or not initialized as the same pointer is what is passed to all callers.
Also the repo should always be NOT NULL and then git just throws an error about the inexistence of it.