So that we can load and store rewrites, as well as other operations on a
list of rewritten commits.
Signed-off-by: Felipe Contreras <redacted>
---
Makefile | 1 +
builtin/rewrite.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
builtin/rewrite.h | 18 ++++++++++++++
3 files changed, 93 insertions(+)
create mode 100644 builtin/rewrite.c
create mode 100644 builtin/rewrite.h
diff --git a/Makefile b/Makefile
index 4c7bb88..a167e68 100644
--- a/Makefile
+++ b/Makefile
@@ -991,6 +991,7 @@ BUILTIN_OBJS += builtin/verify-tag.o
BUILTIN_OBJS += builtin/write-tree.o
BUILTIN_LIB_OBJS += builtin/sequencer.o
+BUILTIN_LIB_OBJS += builtin/rewrite.o
BUILTIN_LIB_OBJS += $(BUILTIN_OBJS)
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
diff --git a/builtin/rewrite.c b/builtin/rewrite.c
new file mode 100644
index 0000000..2519352
--- /dev/null
+++ b/builtin/rewrite.c
@@ -0,0 +1,74 @@
+#include "cache.h"
+#include "rewrite.h"
+
+void add_rewritten(struct rewritten *list, unsigned char *from, unsigned char *to)
+{
+ struct rewritten_item *item;
+ if (list->nr + 1 >= list->alloc) {
+ list->alloc += 32;
+ list->items = xrealloc(list->items, list->alloc * sizeof(*list->items));
+ }
+ 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/builtin/rewrite.h b/builtin/rewrite.h
new file mode 100644
index 0000000..09e7222
--- /dev/null
+++ b/builtin/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.8.3.698.g079b096