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

[PATCH v3 00/17] Lib'ify quite a few functions in sequencer.c

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Sep 9, 2016, 14:35 UTC
Message-ID
<cover.1473431645.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1472219214.git.johannes.schindelin@gmx.de>

This patch series is one of the half dozen patch series left to move the bulk of rebase -i into a builtin.

The purpose of this patch series is to switch the functions in sequencer.c from die()ing to returning errors instead, as proper library functions should do, to give callers a chance to clean up after an error.

Changes since v2:
- the commit message of the read_populate_opts() patch now clarifies why
  we do not take care of git_config_from_file() possibly die()ing.
- the save_head() and the save_opts() conditionals are now separate, for
  improved readability.
- the fast_forward_to() libification now happens in its own commit.
- checkout_fast_forward_to() is now libified, too.
- added a code comment to clarify why we don't care about
  git_parse_source() being able to die().
- added rollbacks in case of failure in read_and_refresh_cache() and
  save_head().
Johannes Schindelin (17):
  sequencer: lib'ify sequencer_pick_revisions()
  sequencer: do not die() in do_pick_commit()
  sequencer: lib'ify write_message()
  sequencer: lib'ify do_recursive_merge()
  sequencer: lib'ify do_pick_commit()
  sequencer: lib'ify walk_revs_populate_todo()
  sequencer: lib'ify prepare_revs()
  sequencer: lib'ify read_and_refresh_cache()
  sequencer: lib'ify read_populate_todo()
  sequencer: lib'ify read_populate_opts()
  sequencer: lib'ify create_seq_dir()
  sequencer: lib'ify save_head()
  sequencer: lib'ify save_todo()
  sequencer: lib'ify save_opts()
  sequencer: lib'ify fast_forward_to()
  lib'ify checkout_fast_forward_to()
  sequencer: ensure to release the lock when we could not read the index
 merge.c     |   9 ++-
 sequencer.c | 197 ++++++++++++++++++++++++++++++++++++++----------------------
 2 files changed, 131 insertions(+), 75 deletions(-)
Published-As: https://github.com/dscho/git/releases/tag/libify-sequencer-v3
Fetch-It-Via: git fetch https://github.com/dscho/git libify-sequencer-v3
Interdiff vs v2:
 diff --git a/merge.c b/merge.c
 index 5db7d56..23866c9 100644
 --- a/merge.c
 +++ b/merge.c
 @@ -57,7 +57,8 @@ int checkout_fast_forward(const unsigned char *head,
  
  	refresh_cache(REFRESH_QUIET);
  
 -	hold_locked_index(lock_file, 1);
 +	if (hold_locked_index(lock_file, 0) < 0)
 +		return -1;
  
  	memset(&trees, 0, sizeof(trees));
  	memset(&opts, 0, sizeof(opts));
 @@ -90,7 +91,9 @@ int checkout_fast_forward(const unsigned char *head,
  	}
  	if (unpack_trees(nr_trees, t, &opts))
  		return -1;
 -	if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
 -		die(_("unable to write new index file"));
 +	if (write_locked_index(&the_index, lock_file, COMMIT_LOCK)) {
 +		rollback_lock_file(lock_file);
 +		return error(_("unable to write new index file"));
 +	}
  	return 0;
  }
 diff --git a/sequencer.c b/sequencer.c
 index b6481bb..eec8a60 100644
 --- a/sequencer.c
 +++ b/sequencer.c
 @@ -644,14 +644,18 @@ 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)
 +	if (read_index_preload(&the_index, NULL) < 0) {
 +		rollback_lock_file(&index_lock);
  		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))
 +		if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) {
 +			rollback_lock_file(&index_lock);
  			return error(_("git %s: failed to refresh the index"),
  				action_name(opts));
 +		}
  	}
  	rollback_lock_file(&index_lock);
  	return 0;
 @@ -812,6 +816,12 @@ static int read_populate_opts(struct replay_opts **opts)
  {
  	if (!file_exists(git_path_opts_file()))
  		return 0;
 +	/*
 +	 * The function git_parse_source(), called from git_config_from_file(),
 +	 * may die() in case of a syntactically incorrect file. We do not care
 +	 * about this case, though, because we wrote that file ourselves, so we
 +	 * are pretty certain that it is syntactically correct.
 +	 */
  	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), *opts) < 0)
  		return error(_("Malformed options sheet: %s"),
  			git_path_opts_file());
 @@ -853,14 +863,20 @@ static int save_head(const char *head)
  	int fd;
  
  	fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);
 -	if (fd < 0)
 +	if (fd < 0) {
 +		rollback_lock_file(&head_lock);
  		return error_errno(_("Could not lock HEAD"));
 +	}
  	strbuf_addf(&buf, "%s\n", head);
 -	if (write_in_full(fd, buf.buf, buf.len) < 0)
 +	if (write_in_full(fd, buf.buf, buf.len) < 0) {
 +		rollback_lock_file(&head_lock);
  		return error_errno(_("Could not write to %s"),
  				   git_path_head_file());
 -	if (commit_lock_file(&head_lock) < 0)
 +	}
 +	if (commit_lock_file(&head_lock) < 0) {
 +		rollback_lock_file(&head_lock);
  		return error(_("Error wrapping up %s."), git_path_head_file());
 +	}
  	return 0;
  }
  
 @@ -1135,8 +1151,9 @@ int sequencer_pick_revisions(struct replay_opts *opts)
  		return -1;
  	if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT))
  		return error(_("Can't revert as initial commit"));
 -	if (save_head(sha1_to_hex(sha1)) ||
 -			save_opts(opts))
 +	if (save_head(sha1_to_hex(sha1)))
 +		return -1;
 +	if (save_opts(opts))
  		return -1;
  	return pick_commits(todo_list, opts);
  }
-- 
2.10.0.windows.1.10.g803177d

base-commit: 6ebdac1bab966b720d776aa43ca188fe378b1f4b
Previous: Junio C HamanoNext: Johannes Schindelin
Message 71 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.