git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 08/14] sequencer: lib'ify read_and_refresh_cache()

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Aug 30, 2016, 09:09 UTC
Message-ID
<alpine.DEB.2.20.1608301106220.129229@virtualbox>
In-Reply-To
<xmqq7faz9ufi.fsf@gitster.mtv.corp.google.com>
Hi Junio,
On Mon, 29 Aug 2016, Junio C Hamano wrote:
Show 59 quoted lines
> Johannes Schindelin <johannes.schindelin@gmx.de> writes:
> 
> > Instead of dying there, let the caller high up in the callchain
> > notice the error and handle it (by dying, still).
> >
> > There are two call sites of read_and_refresh_cache(), one of which is
> > pick_commits(), whose callers were already prepared to do the right
> > thing given an "error" return from it by an earlier patch, so the
> > conversion is safe.
> >
> > The other one, sequencer_pick_revisions() was also prepared to relay
> > an error return back to its caller in all remaining cases in an
> > earlier patch.
> >
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> >  sequencer.c | 15 ++++++++++-----
> >  1 file changed, 10 insertions(+), 5 deletions(-)
> >
> > diff --git a/sequencer.c b/sequencer.c
> > index c006cae..e30aa82 100644
> > --- a/sequencer.c
> > +++ b/sequencer.c
> > @@ -640,18 +640,21 @@ static int prepare_revs(struct replay_opts *opts)
> >  	return 0;
> >  }
> >  
> > -static void read_and_refresh_cache(struct replay_opts *opts)
> > +static int read_and_refresh_cache(struct replay_opts *opts)
> >  {
> >  	static struct lock_file index_lock;
> >  	int index_fd = hold_locked_index(&index_lock, 0);
> >  	if (read_index_preload(&the_index, NULL) < 0)
> > -		die(_("git %s: failed to read the index"), action_name(opts));
> > +		return error(_("git %s: failed to read the index"),
> > +			action_name(opts));
> >  	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
> >  	if (the_index.cache_changed && index_fd >= 0) {
> >  		if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
> > -			die(_("git %s: failed to refresh the index"), action_name(opts));
> > +			return error(_("git %s: failed to refresh the index"),
> > +				action_name(opts));
> >  	}
> >  	rollback_lock_file(&index_lock);
> > +	return 0;
> >  }
> 
> With the current set of callers, a caller that notices an error from
> this function will immediately exit without doing any further
> damage.
> 
> So in that sense, this is a "safe" conversion.
> 
> But is it a sensible conversion?  When the caller wants to do
> anything else (e.g. clean-up and try something else, perhaps read
> the index again), the caller can't, as the index is still locked,
> because even though the code knows that the lock will not be
> released until the process exit, it chose to return error without
> releasing the lock.

It depends what the caller wants to do. The case about which I care most is when some helpful advice should be printed (see e.g. 3be18b4 (t5520: verify that `pull --rebase` shows the helpful advice when failing, 2016-07-26)). Those callers do not need to care, as the atexit() handler will clean up the lock file.

However, I am sympathetic to your angle, even if I do not expect any such caller to arise anytime soon.

Show 26 quoted lines
> For a file-scope static helper, that probably is sufficient.  But if
> this can be reached from a public entry point in the API, the caller
> of that entry point will find this not-so-useful, I would think.
> 
> I suspect doing the "right thing" to future-proof it may not be too
> much more work.
> 
> 	static int read_and_refresh_cache(struct replay_opts *opts)
>         {
> +        	int retval = 0; /* assume success */
> 		...
>                 if (read_idnex_preload(...) < 0) {
> 			retval = error(...);
>                         goto finish;
> 		}
>                 refresh_index(...);
>                 if (...changed...) {
>                 	if (write_locked_index(...))
>                         	retval = error(...);
> 		}
> +	finish:
>                 rollback_lock_file(&index_lock);
>                 return retval;
> 	}
> 
> or something like that on top?
I settled for rolling back in the if() clauses.

Ciao, Dscho

Previous: Junio C HamanoNext: Junio C Hamano
Message 66 of 91 in “Lib'ify quite a few functions in sequencer.c”
  1. 00/15 Lib'ify quite a few functions in sequencer.cJohannes Schindelin, Aug 23, 2016
  2. 07/15 sequencer: lib'ify read_populate_opts()Johannes Schindelin, Aug 23, 2016
  3. Junio C HamanoAug 26, 2016
  4. Johannes SchindelinAug 29, 2016
  5. 01/15 sequencer: lib'ify write_message()Johannes Schindelin, Aug 23, 2016
  6. Eric SunshineAug 24, 2016
  7. Johannes SchindelinAug 24, 2016
  8. 06/15 sequencer: lib'ify read_populate_todo()Johannes Schindelin, Aug 23, 2016
  9. Eric SunshineAug 24, 2016
  10. Johannes SchindelinAug 24, 2016
  11. Junio C HamanoAug 25, 2016
  12. Johannes SchindelinAug 26, 2016
  13. Junio C HamanoAug 26, 2016
  14. 04/15 sequencer: lib'ify prepare_revs()Johannes Schindelin, Aug 23, 2016
  15. Junio C HamanoAug 25, 2016
  16. Johannes SchindelinAug 26, 2016
  17. 02/15 sequencer: lib'ify do_recursive_merge()Johannes Schindelin, Aug 23, 2016
  18. Eric SunshineAug 24, 2016
  19. Johannes SchindelinAug 24, 2016
  20. 03/15 sequencer: lib'ify do_pick_commit()Johannes Schindelin, Aug 23, 2016
  21. Junio C HamanoAug 25, 2016
  22. Johannes SchindelinAug 26, 2016
  23. 13/15 sequencer: lib'ify sequencer_pick_revisions()Johannes Schindelin, Aug 23, 2016
  24. 15/15 sequencer: do not die() in fast_forward_to()Johannes Schindelin, Aug 23, 2016
  25. 08/15 sequencer: lib'ify walk_revs_populate_todo()Johannes Schindelin, Aug 23, 2016
  26. 05/15 sequencer: lib'ify read_and_refresh_cache()Johannes Schindelin, Aug 23, 2016
  27. Eric SunshineAug 24, 2016
  28. Johannes SchindelinAug 24, 2016
  29. Junio C HamanoAug 25, 2016
  30. 09/15 sequencer: lib'ify create_seq_dir()Johannes Schindelin, Aug 23, 2016
  31. Eric SunshineAug 24, 2016
  32. Johannes SchindelinAug 24, 2016
  33. 11/15 sequencer: lib'ify save_todo()Johannes Schindelin, Aug 23, 2016
  34. Eric SunshineAug 24, 2016
  35. Johannes SchindelinAug 24, 2016
  36. 10/15 sequencer: lib'ify save_head()Johannes Schindelin, Aug 23, 2016
  37. Eric SunshineAug 24, 2016
  38. Johannes SchindelinAug 24, 2016
  39. 12/15 sequencer: lib'ify save_opts()Johannes Schindelin, Aug 23, 2016
  40. Junio C HamanoAug 26, 2016
  41. Johannes SchindelinAug 29, 2016
  42. 14/15 sequencer: do not die() in do_pick_commit()Johannes Schindelin, Aug 23, 2016
  43. 00/14 Lib'ify quite a few functions in sequencer.cJohannes Schindelin, Aug 26, 2016
  44. 05/14 sequencer: lib'ify do_pick_commit()Johannes Schindelin, Aug 26, 2016
  45. Junio C HamanoAug 29, 2016
  46. 09/14 sequencer: lib'ify read_populate_todo()Johannes Schindelin, Aug 26, 2016
  47. 12/14 sequencer: lib'ify save_head()Johannes Schindelin, Aug 26, 2016
  48. Junio C HamanoAug 29, 2016
  49. Johannes SchindelinAug 30, 2016
  50. 11/14 sequencer: lib'ify create_seq_dir()Johannes Schindelin, Aug 26, 2016
  51. 10/14 sequencer: lib'ify read_populate_opts()Johannes Schindelin, Aug 26, 2016
  52. Junio C HamanoAug 29, 2016
  53. Junio C HamanoAug 29, 2016
  54. Johannes SchindelinAug 30, 2016
  55. Junio C HamanoAug 30, 2016
  56. 14/14 sequencer: lib'ify save_opts()Johannes Schindelin, Aug 26, 2016
  57. 04/14 sequencer: lib'ify do_recursive_merge()Johannes Schindelin, Aug 26, 2016
  58. 03/14 sequencer: lib'ify write_message()Johannes Schindelin, Aug 26, 2016
  59. Junio C HamanoAug 29, 2016
  60. Johannes SchindelinAug 30, 2016
  61. 13/14 sequencer: lib'ify save_todo()Johannes Schindelin, Aug 26, 2016
  62. 06/14 sequencer: lib'ify walk_revs_populate_todo()Johannes Schindelin, Aug 26, 2016
  63. 07/14 sequencer: lib'ify prepare_revs()Johannes Schindelin, Aug 26, 2016
  64. 08/14 sequencer: lib'ify read_and_refresh_cache()Johannes Schindelin, Aug 26, 2016
  65. Junio C HamanoAug 29, 2016
  66. Johannes SchindelinAug 30, 2016
  67. Junio C HamanoAug 30, 2016
  68. 01/14 sequencer: lib'ify sequencer_pick_revisions()Johannes Schindelin, Aug 26, 2016
  69. 02/14 sequencer: do not die() in do_pick_commit()Johannes Schindelin, Aug 26, 2016
  70. Junio C HamanoAug 29, 2016
  71. 00/17 Lib'ify quite a few functions in sequencer.cJohannes Schindelin, Sep 9, 2016
  72. 01/17 sequencer: lib'ify sequencer_pick_revisions()Johannes Schindelin, Sep 9, 2016
  73. 02/17 sequencer: do not die() in do_pick_commit()Johannes Schindelin, Sep 9, 2016
  74. 04/17 sequencer: lib'ify do_recursive_merge()Johannes Schindelin, Sep 9, 2016
  75. 05/17 sequencer: lib'ify do_pick_commit()Johannes Schindelin, Sep 9, 2016
  76. 06/17 sequencer: lib'ify walk_revs_populate_todo()Johannes Schindelin, Sep 9, 2016
  77. 09/17 sequencer: lib'ify read_populate_todo()Johannes Schindelin, Sep 9, 2016
  78. 08/17 sequencer: lib'ify read_and_refresh_cache()Johannes Schindelin, Sep 9, 2016
  79. 03/17 sequencer: lib'ify write_message()Johannes Schindelin, Sep 9, 2016
  80. 12/17 sequencer: lib'ify save_head()Johannes Schindelin, Sep 9, 2016
  81. 10/17 sequencer: lib'ify read_populate_opts()Johannes Schindelin, Sep 9, 2016
  82. 11/17 sequencer: lib'ify create_seq_dir()Johannes Schindelin, Sep 9, 2016
  83. 14/17 sequencer: lib'ify save_opts()Johannes Schindelin, Sep 9, 2016
  84. 16/17 lib'ify checkout_fast_forward_to()Johannes Schindelin, Sep 9, 2016
  85. Junio C HamanoSep 9, 2016
  86. Johannes SchindelinSep 11, 2016
  87. 13/17 sequencer: lib'ify save_todo()Johannes Schindelin, Sep 9, 2016
  88. 15/17 sequencer: lib'ify fast_forward_to()Johannes Schindelin, Sep 9, 2016
  89. 07/17 sequencer: lib'ify prepare_revs()Johannes Schindelin, Sep 9, 2016
  90. 17/17 sequencer: ensure to release the lock when we could not read the indexJohannes Schindelin, Sep 9, 2016
  91. Junio C HamanoSep 9, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.