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

[PATCH v7 06/12] builtin: add rewrite helper

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Apr 24, 2014, 02:44 UTC
Message-ID
<1398307491-21314-7-git-send-email-felipe.contreras@gmail.com>
In-Reply-To
<1398307491-21314-1-git-send-email-felipe.contreras@gmail.com>

So that we can load and store rewrites, as well as other operations on a list of rewritten commits.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 Makefile  |  2 ++
 rewrite.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 rewrite.h | 18 ++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 rewrite.c
 create mode 100644 rewrite.h
diff --git a/Makefile b/Makefile
index 33aa15b..d315b71 100644
--- a/Makefile
+++ b/Makefile
@@ -710,6 +710,7 @@ LIB_H += remote.h
 LIB_H += rerere.h
 LIB_H += resolve-undo.h
 LIB_H += revision.h
+LIB_H += rewrite.h
 LIB_H += run-command.h
 LIB_H += send-pack.h
 LIB_H += sequencer.h
@@ -855,6 +856,7 @@ LIB_OBJS += replace_object.o
 LIB_OBJS += rerere.o
 LIB_OBJS += resolve-undo.o
 LIB_OBJS += revision.o
+LIB_OBJS += rewrite.o
 LIB_OBJS += run-command.o
 LIB_OBJS += send-pack.o
 LIB_OBJS += sequencer.o
diff --git a/rewrite.c b/rewrite.c
new file mode 100644
index 0000000..436d54a
--- /dev/null
+++ b/rewrite.c
@@ -0,0 +1,71 @@
+#include "cache.h"
+#include "rewrite.h"
+
+void add_rewritten(struct rewritten *list, unsigned char *from, unsigned char *to)
+{
+	struct rewritten_item *item;
+	ALLOC_GROW(list->items, list->nr + 1, list->alloc);
+	item = &list->items[list->nr];
+	hashcpy(item->from, from);
+	hashcpy(item->to, to);
+	list->nr++;
+}
+
+int store_rewritten(struct rewritten *list, const char *file)
+{
+	static struct lock_file lock;
+	struct strbuf buf = STRBUF_INIT;
+	int fd, i, ret = 0;
+
+	fd = hold_lock_file_for_update(&lock, file, LOCK_DIE_ON_ERROR);
+	for (i = 0; i < list->nr; i++) {
+		struct rewritten_item *item = &list->items[i];
+		strbuf_addf(&buf, "%s %s\n", sha1_to_hex(item->from), sha1_to_hex(item->to));
+	}
+	if (write_in_full(fd, buf.buf, buf.len) < 0) {
+		error(_("Could not write to %s"), file);
+		ret = 1;
+		goto leave;
+	}
+	if (commit_lock_file(&lock) < 0) {
+		error(_("Error wrapping up %s."), file);
+		ret = 1;
+		goto leave;
+	}
+leave:
+	strbuf_release(&buf);
+	return ret;
+}
+
+void load_rewritten(struct rewritten *list, const char *file)
+{
+	struct strbuf buf = STRBUF_INIT;
+	char *p;
+	int fd;
+
+	fd = open(file, O_RDONLY);
+	if (fd < 0)
+		return;
+	if (strbuf_read(&buf, fd, 0) < 0) {
+		close(fd);
+		strbuf_release(&buf);
+		return;
+	}
+	close(fd);
+
+	for (p = buf.buf; *p;) {
+		unsigned char from[20];
+		unsigned char to[20];
+		char *eol = strchrnul(p, '\n');
+		if (eol - p != 81)
+			/* wrong size */
+			break;
+		if (get_sha1_hex(p, from))
+			break;
+		if (get_sha1_hex(p + 41, to))
+			break;
+		add_rewritten(list, from, to);
+		p = *eol ? eol + 1 : eol;
+	}
+	strbuf_release(&buf);
+}
diff --git a/rewrite.h b/rewrite.h
new file mode 100644
index 0000000..09e7222
--- /dev/null
+++ b/rewrite.h
@@ -0,0 +1,18 @@
+#ifndef REWRITE_H
+#define REWRITE_H
+
+struct rewritten_item {
+	unsigned char from[20];
+	unsigned char to[20];
+};
+
+struct rewritten {
+	struct rewritten_item *items;
+	unsigned int nr, alloc;
+};
+
+void add_rewritten(struct rewritten *list, unsigned char *from, unsigned char *to);
+int store_rewritten(struct rewritten *list, const char *file);
+void load_rewritten(struct rewritten *list, const char *file);
+
+#endif
-- 
1.9.2+fc1.2.gfbaae8c
Previous: Eric SunshineNext: Ramkumar Ramachandra
Message 11 of 23 in “Improvements to cherry-pick”
  1. 00/12 Improvements to cherry-pickFelipe Contreras, Apr 24, 2014
  2. 01/12 cherry-pick: don't barf when there's nothing to doFelipe Contreras, Apr 24, 2014
  3. 02/12 cherry-pick: add --skip-empty optionFelipe Contreras, Apr 24, 2014
  4. 03/12 revert/cherry-pick: add --quiet optionFelipe Contreras, Apr 24, 2014
  5. Ramkumar RamachandraApr 24, 2014
  6. Felipe ContrerasApr 24, 2014
  7. 04/12 revert/cherry-pick: add --skip optionFelipe Contreras, Apr 24, 2014
  8. Ramkumar RamachandraApr 24, 2014
  9. 05/12 cherry-pick: remember rerere-autoupdateFelipe Contreras, Apr 24, 2014
  10. Eric SunshineApr 24, 2014
  11. 06/12 builtin: add rewrite helperFelipe Contreras, Apr 24, 2014
  12. Ramkumar RamachandraApr 24, 2014
  13. Felipe ContrerasApr 24, 2014
  14. 07/12 cherry-pick: store rewritten commitsFelipe Contreras, Apr 24, 2014
  15. 08/12 cherry-pick: don't store skipped commitFelipe Contreras, Apr 24, 2014
  16. 09/12 builtin: move run_rewrite_hook() to rewrite.cFelipe Contreras, Apr 24, 2014
  17. 10/12 builtin: rewrite: add copy_rewrite_notes()Felipe Contreras, Apr 24, 2014
  18. 11/12 cherry-pick: add --action-name optionFelipe Contreras, Apr 24, 2014
  19. 12/12 cherry-pick: copy notes and run hooksFelipe Contreras, Apr 24, 2014
  20. Ramkumar RamachandraApr 24, 2014
  21. Felipe ContrerasApr 24, 2014
  22. Ramkumar RamachandraApr 24, 2014
  23. Felipe ContrerasApr 24, 2014

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.