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

[PATCH] builtin-fast-export: Add importing and exporting of revision marks

From
PBPieter de Bie <pdebie@ai.rug.nl>
Date
Jun 4, 2008, 20:55 UTC
Message-ID
<1212612947-34720-1-git-send-email-pdebie@ai.rug.nl>

This adds the --import-marks and --export-marks to fast-export. These import and export the marks used to for all revisions exported in a similar fashion to what fast-import does. The format is the same as fast-import, so you can create a bidirectional importer / exporter by using the same marks file on both sides. ---

I used this to create a bidirectional import/export script between Git and Bazaar. As both sides can now both import and export marks, keeping two repositories in sync is just a matter of keeping the marks files up to date.

This is my first c code that's more than one line, so please don't be too harsh ;)

 builtin-fast-export.c |   71 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/builtin-fast-export.c b/builtin-fast-export.c
index 8218199..1d5c83d 100755
--- a/builtin-fast-export.c
+++ b/builtin-fast-export.c
@@ -375,30 +375,98 @@ static void handle_tags_and_duplicates(struct path_list *extra_refs)
 	}
 }
 
+static void export_marks(char * file)
+{
+	unsigned int i;
+	uintmax_t mark;
+	struct object_decoration *deco = idnums.hash;
+	FILE *f;
+
+	f = fopen(file, "w");
+	if (!f)
+		error("Unable to open marks file %s for writing", file);
+
+	for (i = 0; i < idnums.size; ++i) {
+		deco++;
+		if (deco && deco->base && deco->base->type == 1) {
+			mark = (uint32_t *) deco-> decoration - (uint32_t *)NULL;
+			fprintf(f, ":%" PRIuMAX " %s\n", mark, sha1_to_hex(deco->base->sha1));
+		}
+	}
+	if (ferror(f) || fclose(f)) {
+		error("Unable to write marks file %s.", file);
+	}
+}
+
+static void import_marks(char * input_file)
+{
+	char line[512];
+	FILE *f = fopen(input_file, "r");
+	if (!f)
+		die("cannot read %s: %s", input_file, strerror(errno));
+
+	while (fgets(line, sizeof(line), f)) {
+		uintmax_t mark;
+		char *end;
+		unsigned char sha1[20];
+		struct object *object;
+
+		end = strchr(line, '\n');
+		if (line[0] != ':' || !end)
+			die("corrupt mark line: %s", line);
+		*end = 0;
+		mark = strtoumax(line + 1, &end, 10);
+		if (!mark || end == line + 1
+			|| *end != ' ' || get_sha1(end + 1, sha1))
+			die("corrupt mark line: %s", line);
+
+		object = parse_object(sha1);
+		if (!object)
+			die ("Could not read blob %s", sha1_to_hex(sha1));
+
+		if (object->flags & SHOWN)
+			error("Object %s was already has a mark", sha1);
+
+		add_decoration(&idnums, object, ((uint32_t *)NULL) + mark);
+		if (last_idnum < mark)
+			last_idnum = mark;
+
+		object->flags |= SHOWN;
+	}
+	fclose(f);
+}
+
 int cmd_fast_export(int argc, const char **argv, const char *prefix)
 {
 	struct rev_info revs;
 	struct object_array commits = { 0, 0, NULL };
 	struct path_list extra_refs = { NULL, 0, 0, 0 };
 	struct commit *commit;
+	char *export_filename, *import_filename;
 	struct option options[] = {
 		OPT_INTEGER(0, "progress", &progress,
 			    "show progress after <n> objects"),
 		OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, "mode",
 			     "select handling of signed tags",
 			     parse_opt_signed_tag_mode),
+		OPT_STRING(0, "export-marks", &export_filename, "FILE", "Dump marks to this file"),
+		OPT_STRING(0, "import-marks", &import_filename, "FILE", "Import marks from this file"),
 		OPT_END()
 	};
 
 	/* we handle encodings */
 	git_config(git_default_config, NULL);
 
+
 	init_revisions(&revs, prefix);
 	argc = setup_revisions(argc, argv, &revs, NULL);
 	argc = parse_options(argc, argv, options, fast_export_usage, 0);
 	if (argc > 1)
 		usage_with_options (fast_export_usage, options);
 
+	if (import_filename)
+		import_marks(import_filename);
+
 	get_tags_and_duplicates(&revs.pending, &extra_refs);
 
 	if (prepare_revision_walk(&revs))
@@ -421,5 +489,8 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
 
 	handle_tags_and_duplicates(&extra_refs);
 
+	if (export_filename)
+		export_marks(export_filename);
+
 	return 0;
 }
-- 
1.5.6.rc0.165.ge08d6b.dirty
Next: Johannes Schindelin
Message 1 of 18 in “builtin-fast-export: Add importing and exporting of revision marks”
  1. builtin-fast-export: Add importing and exporting of revision marksPieter de Bie, Jun 4, 2008
  2. Johannes SchindelinJun 5, 2008
  3. Pieter de BieJun 5, 2008
  4. builtin-fast-export: Add importing and exporting of revision marksPieter de Bie, Jun 5, 2008
  5. Johannes SchindelinJun 5, 2008
  6. Junio C HamanoJun 6, 2008
  7. Pieter de BieJun 7, 2008
  8. Johannes SchindelinJun 7, 2008
  9. Junio C HamanoJun 7, 2008
  10. Johannes SchindelinJun 11, 2008
  11. Documentation/fast-export: Document --import-marks and --export-marks optionsPieter de Bie, Jun 7, 2008
  12. Johannes SchindelinJun 7, 2008
  13. Junio C HamanoJun 10, 2008
  14. builtin-fast-export: Add importing and exporting of revision marksPieter de Bie, Jun 11, 2008
  15. Pieter de BieJun 11, 2008
  16. Johannes SchindelinJun 11, 2008
  17. Junio C HamanoJun 11, 2008
  18. Johannes SchindelinJun 5, 2008

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.