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.contre...@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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to