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

[PATCH 08/33] grep: convert to struct object_id

From
BWBrandon Williams <bmwill@google.com>
Date
May 30, 2017, 17:30 UTC
Message-ID
<20170530173109.54904-9-bmwill@google.com>
In-Reply-To
<20170530173109.54904-1-bmwill@google.com>
Convert the remaining parts of grep to use struct object_id.
Signed-off-by: Brandon Williams <bmwill@google.com>
---
 builtin/grep.c | 22 +++++++++++-----------
 cache.h        |  7 +++++++
 grep.c         | 17 ++++++++---------
 grep.h         |  2 +-
 4 files changed, 27 insertions(+), 21 deletions(-)
diff --git a/builtin/grep.c b/builtin/grep.c
index c6c26e9b9..623c13a93 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -327,7 +327,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
 
 #ifndef NO_PTHREADS
 	if (num_threads) {
-		add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
+		add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
 		strbuf_release(&pathbuf);
 		return 0;
 	} else
@@ -336,7 +336,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
 		struct grep_source gs;
 		int hit;
 
-		grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
+		grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
 		strbuf_release(&pathbuf);
 		hit = grep_source(opt, &gs);
 
@@ -570,7 +570,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
 	 * with the object's name: 'tree-name:filename'.  In order to
 	 * provide uniformity of output we want to pass the name of the
 	 * parent project's object name to the submodule so the submodule can
-	 * prefix its output with the parent's name and not its own SHA1.
+	 * prefix its output with the parent's name and not its own OID.
 	 */
 	if (gs->identifier && end_of_base)
 		argv_array_pushf(&cp.args, "--parent-basename=%.*s",
@@ -583,12 +583,12 @@ static int grep_submodule_launch(struct grep_opt *opt,
 		 * If there is a tree identifier for the submodule, add the
 		 * rev after adding the submodule options but before the
 		 * pathspecs.  To do this we listen for the '--' and insert the
-		 * sha1 before pushing the '--' onto the child process argv
+		 * oid before pushing the '--' onto the child process argv
 		 * array.
 		 */
 		if (gs->identifier &&
 		    !strcmp("--", submodule_options.argv[i])) {
-			argv_array_push(&cp.args, sha1_to_hex(gs->identifier));
+			argv_array_push(&cp.args, oid_to_hex(gs->identifier));
 		}
 
 		argv_array_push(&cp.args, submodule_options.argv[i]);
@@ -618,11 +618,11 @@ static int grep_submodule_launch(struct grep_opt *opt,
 
 /*
  * Prep grep structures for a submodule grep
- * sha1: the sha1 of the submodule or NULL if using the working tree
+ * oid: the oid of the submodule or NULL if using the working tree
  * filename: name of the submodule including tree name of parent
  * path: location of the submodule
  */
-static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
+static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
 			  const char *filename, const char *path)
 {
 	if (!is_submodule_initialized(path))
@@ -632,7 +632,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
 		 * If searching history, check for the presense of the
 		 * submodule's gitdir before skipping the submodule.
 		 */
-		if (sha1) {
+		if (oid) {
 			const struct submodule *sub =
 					submodule_from_path(null_sha1, path);
 			if (sub)
@@ -647,7 +647,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
 
 #ifndef NO_PTHREADS
 	if (num_threads) {
-		add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, sha1);
+		add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, oid);
 		return 0;
 	} else
 #endif
@@ -656,7 +656,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
 		int hit;
 
 		grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
-				 filename, path, sha1);
+				 filename, path, oid);
 		hit = grep_submodule_launch(opt, &gs);
 
 		grep_source_clear(&gs);
@@ -775,7 +775,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 					 check_attr);
 			free(data);
 		} else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
-			hit |= grep_submodule(opt, entry.oid->hash, base->buf,
+			hit |= grep_submodule(opt, entry.oid, base->buf,
 					      base->buf + tn_len);
 		}
 
diff --git a/cache.h b/cache.h
index ae4c45d37..50fd2b3cc 100644
--- a/cache.h
+++ b/cache.h
@@ -1026,6 +1026,13 @@ static inline void oidcpy(struct object_id *dst, const struct object_id *src)
 	hashcpy(dst->hash, src->hash);
 }
 
+static inline struct object_id *oiddup(const struct object_id *src)
+{
+	struct object_id *dst = xmalloc(sizeof(struct object_id));
+	oidcpy(dst, src);
+	return dst;
+}
+
 static inline void hashclr(unsigned char *hash)
 {
 	memset(hash, 0, GIT_SHA1_RAWSZ);
diff --git a/grep.c b/grep.c
index 47cee4506..a240b4cdb 100644
--- a/grep.c
+++ b/grep.c
@@ -1403,7 +1403,7 @@ static int fill_textconv_grep(struct userdiff_driver *driver,
 	 */
 	df = alloc_filespec(gs->path);
 	switch (gs->type) {
-	case GREP_SOURCE_SHA1:
+	case GREP_SOURCE_OID:
 		fill_filespec(df, gs->identifier, 1, 0100644);
 		break;
 	case GREP_SOURCE_FILE:
@@ -1747,9 +1747,8 @@ void grep_source_init(struct grep_source *gs, enum grep_source_type type,
 		 * If the identifier is non-NULL (in the submodule case) it
 		 * will be a SHA1 that needs to be copied.
 		 */
-	case GREP_SOURCE_SHA1:
-		gs->identifier = xmalloc(20);
-		hashcpy(gs->identifier, identifier);
+	case GREP_SOURCE_OID:
+		gs->identifier = oiddup(identifier);
 		break;
 	case GREP_SOURCE_BUF:
 		gs->identifier = NULL;
@@ -1772,7 +1771,7 @@ void grep_source_clear_data(struct grep_source *gs)
 {
 	switch (gs->type) {
 	case GREP_SOURCE_FILE:
-	case GREP_SOURCE_SHA1:
+	case GREP_SOURCE_OID:
 	case GREP_SOURCE_SUBMODULE:
 		free(gs->buf);
 		gs->buf = NULL;
@@ -1784,7 +1783,7 @@ void grep_source_clear_data(struct grep_source *gs)
 	}
 }
 
-static int grep_source_load_sha1(struct grep_source *gs)
+static int grep_source_load_oid(struct grep_source *gs)
 {
 	enum object_type type;
 
@@ -1795,7 +1794,7 @@ static int grep_source_load_sha1(struct grep_source *gs)
 	if (!gs->buf)
 		return error(_("'%s': unable to read %s"),
 			     gs->name,
-			     sha1_to_hex(gs->identifier));
+			     oid_to_hex(gs->identifier));
 	return 0;
 }
 
@@ -1841,8 +1840,8 @@ static int grep_source_load(struct grep_source *gs)
 	switch (gs->type) {
 	case GREP_SOURCE_FILE:
 		return grep_source_load_file(gs);
-	case GREP_SOURCE_SHA1:
-		return grep_source_load_sha1(gs);
+	case GREP_SOURCE_OID:
+		return grep_source_load_oid(gs);
 	case GREP_SOURCE_BUF:
 		return gs->buf ? 0 : -1;
 	case GREP_SOURCE_SUBMODULE:
diff --git a/grep.h b/grep.h
index 267534ca2..c88b40bdc 100644
--- a/grep.h
+++ b/grep.h
@@ -158,7 +158,7 @@ struct grep_source {
 	char *name;
 
 	enum grep_source_type {
-		GREP_SOURCE_SHA1,
+		GREP_SOURCE_OID,
 		GREP_SOURCE_FILE,
 		GREP_SOURCE_BUF,
 		GREP_SOURCE_SUBMODULE,
-- 
2.13.0.219.gdb65acc882-goog
Previous: Brandon WilliamsNext: Junio C Hamano
Message 12 of 66 in “object id conversion (grep and diff)”
  1. 00/33 object id conversion (grep and diff)Brandon Williams, May 30, 2017
  2. 01/33 notes: convert internal structures to struct object_idBrandon Williams, May 30, 2017
  3. 03/33 notes: convert for_each_note to struct object_idBrandon Williams, May 30, 2017
  4. 05/33 notes: convert format_display_notes to struct object_idBrandon Williams, May 30, 2017
  5. 06/33 builtin/notes: convert to struct object_idBrandon Williams, May 30, 2017
  6. 02/33 notes: convert internal parts to struct object_idBrandon Williams, May 30, 2017
  7. 04/33 notes: make get_note return pointer to struct object_idBrandon Williams, May 30, 2017
  8. René ScharfeJul 15, 2017
  9. Brandon WilliamsJul 17, 2017
  10. 09/33 diff: convert get_stat_data to struct object_idBrandon Williams, May 30, 2017
  11. 07/33 notes: convert some accessor functions to struct object_idBrandon Williams, May 30, 2017
  12. 08/33 grep: convert to struct object_idBrandon Williams, May 30, 2017
  13. Junio C HamanoJun 2, 2017
  14. 15/33 diff: convert reuse_worktree_file to struct object_idBrandon Williams, May 30, 2017
  15. 10/33 diff: convert diff_index_show_file to struct object_idBrandon Williams, May 30, 2017
  16. 19/33 combine-diff: convert diff_tree_combined to struct object_idBrandon Williams, May 30, 2017
  17. 14/33 diff: convert fill_filespec to struct object_idBrandon Williams, May 30, 2017
  18. 21/33 tree-diff: convert diff_root_tree_sha1 to struct object_idBrandon Williams, May 30, 2017
  19. 24/33 notes-merge: convert find_notes_merge_pair_ps to struct object_idBrandon Williams, May 30, 2017
  20. 18/33 diff: convert diff_flush_patch_id to struct object_idBrandon Williams, May 30, 2017
  21. 25/33 notes-merge: convert verify_notes_filepair to struct object_idBrandon Williams, May 30, 2017
  22. Stefan BellerMay 31, 2017
  23. Junio C HamanoJun 2, 2017
  24. Brandon WilliamsJun 2, 2017
  25. Junio C HamanoJun 2, 2017
  26. 30/33 tree-diff: convert diff_tree_paths to struct object_idBrandon Williams, May 30, 2017
  27. Stefan BellerMay 31, 2017
  28. Jeff KingMay 31, 2017
  29. Jeff KingMay 31, 2017
  30. Junio C HamanoJun 2, 2017
  31. René ScharfeJul 15, 2017
  32. brian m. carlsonJul 15, 2017
  33. 26/33 notes-merge: convert write_note_to_worktree to struct object_idBrandon Williams, May 30, 2017
  34. 29/33 tree-diff: convert try_to_follow_renames to struct object_idBrandon Williams, May 30, 2017
  35. 20/33 combine-diff: convert find_paths_* to struct object_idBrandon Williams, May 30, 2017
  36. Stefan BellerMay 31, 2017
  37. Junio C HamanoJun 2, 2017
  38. 23/33 notes-merge: convert merge_from_diffs to struct object_idBrandon Williams, May 30, 2017
  39. Stefan BellerMay 31, 2017
  40. Junio C HamanoJun 2, 2017
  41. 22/33 notes-merge: convert notes_merge* to struct object_idBrandon Williams, May 30, 2017
  42. Stefan BellerMay 31, 2017
  43. brian m. carlsonMay 31, 2017
  44. Junio C HamanoJun 2, 2017
  45. Brandon WilliamsJun 2, 2017
  46. 33/33 diff: rename diff_fill_sha1_info to diff_fill_oid_infoBrandon Williams, May 30, 2017
  47. 31/33 tree-diff: convert path_appendnew to object_idBrandon Williams, May 30, 2017
  48. Junio C HamanoJun 2, 2017
  49. 27/33 diff-tree: convert diff_tree_sha1 to struct object_idBrandon Williams, May 30, 2017
  50. 32/33 diffcore-rename: use is_empty_blob_oidBrandon Williams, May 30, 2017
  51. 28/33 builtin/diff-tree: cleanup references to sha1Brandon Williams, May 30, 2017
  52. Junio C HamanoJun 2, 2017
  53. 17/33 patch-ids: convert to struct object_idBrandon Williams, May 30, 2017
  54. 16/33 diff: finish conversion for prepare_temp_file to struct object_idBrandon Williams, May 30, 2017
  55. Stefan BellerMay 31, 2017
  56. 11/33 diff: convert diff_addremove to struct object_idBrandon Williams, May 30, 2017
  57. 12/33 diff: convert run_diff_files to struct object_idBrandon Williams, May 30, 2017
  58. 13/33 diff: convert diff_change to struct object_idBrandon Williams, May 30, 2017
  59. brian m. carlsonMay 31, 2017
  60. Brandon WilliamsJun 2, 2017
  61. Junio C HamanoJun 2, 2017
  62. Junio C HamanoJun 2, 2017
  63. Junio C HamanoJun 2, 2017
  64. Brandon WilliamsJun 2, 2017
  65. Junio C HamanoJun 2, 2017
  66. Brandon WilliamsJun 5, 2017

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.