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

[PATCH v2 13/19] reset.c: move lock, write and commit out of update_index_refresh()

From
Martin von Zweigbergk <martinvonz@gmail.com>
Date
Jan 15, 2013, 05:47 UTC
Message-ID
<1358228871-7142-14-git-send-email-martinvonz@gmail.com>
In-Reply-To
<1358228871-7142-1-git-send-email-martinvonz@gmail.com>

In preparation for the/a following patch, move the locking, writing and committing of the index file out of update_index_refresh(). The code duplication caused will soon be taken care of. What remains of update_index_refresh() is just one line, but it is still called from two places, so let's leave it for now.

In the process, we expose and fix the minor UI bug that makes us print "Could not refresh index" when we fail to write the index file when invoked with a pathspec. Copy the error message from the pathspec-less codepath ("Could not write new index file.").

Signed-off-by: Martin von Zweigbergk <martinvonz@gmail.com>
---
 builtin/reset.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
diff --git a/builtin/reset.c b/builtin/reset.c
index 70733c2..c1d6ef2 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -109,19 +109,10 @@ static void print_new_head_line(struct commit *commit)
 		printf("\n");
 }
 
-static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
+static void update_index_refresh(int flags)
 {
-	if (!index_lock) {
-		index_lock = xcalloc(1, sizeof(struct lock_file));
-		fd = hold_locked_index(index_lock, 1);
-	}
-
 	refresh_index(&the_index, (flags), NULL, NULL,
 		      _("Unstaged changes after reset:"));
-	if (write_cache(fd, active_cache, active_nr) ||
-			commit_locked_index(index_lock))
-		return error ("Could not refresh index");
-	return 0;
 }
 
 static void update_index_from_diff(struct diff_queue_struct *q,
@@ -321,9 +312,14 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 	if (pathspec) {
 		struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 		int index_fd = hold_locked_index(lock, 1);
-		return read_from_tree(pathspec, sha1) ||
-			update_index_refresh(index_fd, lock,
-					quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
+		if (read_from_tree(pathspec, sha1))
+			return 1;
+		update_index_refresh(
+			quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
+		if (write_cache(index_fd, active_cache, active_nr) ||
+		    commit_locked_index(lock))
+			return error("Could not write new index file.");
+		return 0;
 	}
 
 	/* Soft reset does not touch the index file nor the working tree
@@ -351,9 +347,15 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 
 	if (reset_type == HARD && !update_ref_status && !quiet)
 		print_new_head_line(commit);
-	else if (reset_type == MIXED) /* Report what has not been updated. */
-		update_index_refresh(0, NULL,
-				quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
+	else if (reset_type == MIXED) { /* Report what has not been updated. */
+		struct lock_file *index_lock = xcalloc(1, sizeof(struct lock_file));
+		int fd = hold_locked_index(index_lock, 1);
+		update_index_refresh(
+			quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN);
+		if (write_cache(fd, active_cache, active_nr) ||
+		    commit_locked_index(index_lock))
+			error("Could not refresh index");
+	}
 
 	remove_branch_state();
 
-- 
1.8.1.1.454.gce43f05
Previous: Martin von ZweigbergkNext: Martin von Zweigbergk
Message 59 of 68 in “reset improvements”
  1. 00/19 reset improvementsMartin von Zweigbergk, Jan 9, 2013
  2. 01/19 reset $pathspec: no need to discard indexMartin von Zweigbergk, Jan 9, 2013
  3. 02/19 reset $pathspec: exit with code 0 if successfulMartin von Zweigbergk, Jan 9, 2013
  4. 03/19 reset.c: pass pathspec around instead of (prefix, argv) pairMartin von Zweigbergk, Jan 9, 2013
  5. Matt KraaiJan 9, 2013
  6. Junio C HamanoJan 9, 2013
  7. Duy NguyenJan 10, 2013
  8. Junio C HamanoJan 10, 2013
  9. Duy NguyenJan 11, 2013
  10. 04/19 reset: don't allow "git reset -- $pathspec" in bare repoMartin von Zweigbergk, Jan 9, 2013
  11. Junio C HamanoJan 9, 2013
  12. Martin von ZweigbergkJan 10, 2013
  13. Junio C HamanoJan 10, 2013
  14. 05/19 reset.c: extract function for parsing argumentsMartin von Zweigbergk, Jan 9, 2013
  15. 06/19 reset.c: remove unnecessary variable 'i'Martin von Zweigbergk, Jan 9, 2013
  16. Junio C HamanoJan 9, 2013
  17. Martin von ZweigbergkJan 10, 2013
  18. 07/19 reset.c: extract function for updating {ORIG,}HEADMartin von Zweigbergk, Jan 9, 2013
  19. Matt KraaiJan 9, 2013
  20. 08/19 reset.c: share call to die_if_unmerged_cache()Martin von Zweigbergk, Jan 9, 2013
  21. Junio C HamanoJan 9, 2013
  22. Martin von ZweigbergkJan 10, 2013
  23. 09/19 reset.c: replace switch by if-elseMartin von Zweigbergk, Jan 9, 2013
  24. Junio C HamanoJan 9, 2013
  25. Martin von ZweigbergkJan 11, 2013
  26. Junio C HamanoJan 11, 2013
  27. 10/19 reset --keep: only write index file onceMartin von Zweigbergk, Jan 9, 2013
  28. Junio C HamanoJan 9, 2013
  29. 11/19 reset: avoid redundant error messageMartin von Zweigbergk, Jan 9, 2013
  30. 12/19 reset.c: move update_index_refresh() call out of read_from_tree()Martin von Zweigbergk, Jan 9, 2013
  31. 13/19 reset.c: move lock, write and commit out of update_index_refresh()Martin von Zweigbergk, Jan 9, 2013
  32. 14/19 reset [--mixed]: don't write index file twiceMartin von Zweigbergk, Jan 9, 2013
  33. 15/19 reset.c: finish entire cmd_reset() whether or not pathspec is givenMartin von Zweigbergk, Jan 9, 2013
  34. Junio C HamanoJan 9, 2013
  35. 16/19 reset [--mixed] --quiet: don't refresh indexMartin von Zweigbergk, Jan 9, 2013
  36. Jeff KingJan 9, 2013
  37. Martin von ZweigbergkJan 9, 2013
  38. Junio C HamanoJan 9, 2013
  39. Martin von ZweigbergkJan 9, 2013
  40. Junio C HamanoJan 9, 2013
  41. 17/19 reset $sha1 $pathspec: require $sha1 only to be treeishMartin von Zweigbergk, Jan 9, 2013
  42. Junio C HamanoJan 9, 2013
  43. 18/19 reset: allow reset on unborn branchMartin von Zweigbergk, Jan 9, 2013
  44. 19/19 reset [--mixed]: use diff-based reset whether or not pathspec was givenMartin von Zweigbergk, Jan 9, 2013
  45. Junio C HamanoJan 9, 2013
  46. 00/19 reset improvementsMartin von Zweigbergk, Jan 15, 2013
  47. 01/19 reset $pathspec: no need to discard indexMartin von Zweigbergk, Jan 15, 2013
  48. 02/19 reset $pathspec: exit with code 0 if successfulMartin von Zweigbergk, Jan 15, 2013
  49. 03/19 reset.c: pass pathspec around instead of (prefix, argv) pairMartin von Zweigbergk, Jan 15, 2013
  50. 04/19 reset: don't allow "git reset -- $pathspec" in bare repoMartin von Zweigbergk, Jan 15, 2013
  51. 05/19 reset.c: extract function for parsing argumentsMartin von Zweigbergk, Jan 15, 2013
  52. 06/19 reset.c: remove unnecessary variable 'i'Martin von Zweigbergk, Jan 15, 2013
  53. 07/19 reset.c: extract function for updating {ORIG_,}HEADMartin von Zweigbergk, Jan 15, 2013
  54. 08/19 reset.c: share call to die_if_unmerged_cache()Martin von Zweigbergk, Jan 15, 2013
  55. 09/19 reset --keep: only write index file onceMartin von Zweigbergk, Jan 15, 2013
  56. 10/19 reset: avoid redundant error messageMartin von Zweigbergk, Jan 15, 2013
  57. 11/19 reset.c: replace switch by if-elseMartin von Zweigbergk, Jan 15, 2013
  58. 12/19 reset.c: move update_index_refresh() call out of read_from_tree()Martin von Zweigbergk, Jan 15, 2013
  59. 13/19 reset.c: move lock, write and commit out of update_index_refresh()Martin von Zweigbergk, Jan 15, 2013
  60. 14/19 reset [--mixed]: only write index file onceMartin von Zweigbergk, Jan 15, 2013
  61. 15/19 reset.c: finish entire cmd_reset() whether or not pathspec is givenMartin von Zweigbergk, Jan 15, 2013
  62. 16/19 reset.c: inline update_index_refresh()Martin von Zweigbergk, Jan 15, 2013
  63. 17/19 reset $sha1 $pathspec: require $sha1 only to be treeishMartin von Zweigbergk, Jan 15, 2013
  64. 17/19 fixup! reset $sha1 $pathspec: require $sha1 only to be treeishMartin von Zweigbergk, Jan 16, 2013
  65. Martin von ZweigbergkJan 16, 2013
  66. 18/19 reset: allow reset on unborn branchMartin von Zweigbergk, Jan 15, 2013
  67. 19/19 reset [--mixed]: use diff-based reset whether or not pathspec was givenMartin von Zweigbergk, Jan 15, 2013
  68. Martin von ZweigbergkJan 15, 2013

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.