687
{
688
/*
689
* Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
690
- * found notes to 'partial_tree'. Write the updates notes tree to
690
+ * found notes to 'partial_tree'. Write the updated notes tree to
691
* the DB, and commit the resulting tree object while reusing the
692
* commit message and parents from 'partial_commit'.
693
* Finally store the new commit object SHA1 into 'result_sha1'.
694
*/
695
- struct dir_struct dir;
696
- char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
697
- int path_len = strlen(path), i;
695
+ DIR *dir;
696
+ struct dirent *e;
697
+ struct strbuf path = STRBUF_INIT;
698
char *msg = strstr(partial_commit->buffer, "\n\n");
699
struct strbuf sb_msg = STRBUF_INIT;
700
+ int baselen;
701
702
+ strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
703
if (o->verbosity >= 3)
702
- printf("Committing notes in notes merge worktree at %.*s\n",
703
- path_len - 1, path);
704
+ printf("Committing notes in notes merge worktree at %s\n",
705
+ path.buf);
706
707
if (!msg || msg[2] == '\0')
708
die("partial notes commit has empty message");
709
msg += 2;
710
709
- memset(&dir, 0, sizeof(dir));
710
- read_directory(&dir, path, path_len, NULL);
711
- for (i = 0; i < dir.nr; i++) {
712
- struct dir_entry *ent = dir.entries[i];
711
+ dir = opendir(path.buf);
712
+ if (!dir)
713
+ die_errno("could not open %s", path.buf);
714
+
715
+ strbuf_addch(&path, '/');
716
+ baselen = path.len;
717
+ while ((e = readdir(dir)) != NULL) {
718
struct stat st;
714
- const char *relpath = ent->name + path_len;
719
unsigned char obj_sha1[20], blob_sha1[20];
720
717
- if (ent->len - path_len != 40 || get_sha1_hex(relpath, obj_sha1)) {
721
+ if (is_dot_or_dotdot(e->d_name))
722
+ continue;
723
+
724
+ if (strlen(e->d_name) != 40 || get_sha1_hex(e->d_name, obj_sha1)) {
725
if (o->verbosity >= 3)
719
- printf("Skipping non-SHA1 entry '%s'\n",
720
- ent->name);
726
+ printf("Skipping non-SHA1 entry '%s%s'\n",
727
+ path.buf, e->d_name);
728
continue;
729
}
730
731
+ strbuf_addstr(&path, e->d_name);
732
/* write file as blob, and add to partial_tree */
725
- if (stat(ent->name, &st))
726
- die_errno("Failed to stat '%s'", ent->name);
727
- if (index_path(blob_sha1, ent->name, &st, HASH_WRITE_OBJECT))
728
- die("Failed to write blob object from '%s'", ent->name);
733
+ if (stat(path.buf, &st))
734
+ die_errno("Failed to stat '%s'", path.buf);
735
+ if (index_path(blob_sha1, path.buf, &st, HASH_WRITE_OBJECT))
736
+ die("Failed to write blob object from '%s'", path.buf);
737
if (add_note(partial_tree, obj_sha1, blob_sha1, NULL))
738
die("Failed to add resolved note '%s' to notes tree",
731
- ent->name);
739
+ path.buf);
740
if (o->verbosity >= 4)
741
printf("Added resolved note for object %s: %s\n",
742
sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
743
+ strbuf_setlen(&path, baselen);
744
}
745
746
strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
749
if (o->verbosity >= 4)
750
printf("Finalized notes merge commit: %s\n",
751
sha1_to_hex(result_sha1));
743
- free(path);
752
+ strbuf_release(&path);
753
+ closedir(dir);
754
return 0;
755
}
756