@@ -267,7 +267,8 @@ static void check_notes_merge_worktree(struct notes_merge_options *o)
267
267
* Must establish NOTES_MERGE_WORKTREE.
268
268
* Abort if NOTES_MERGE_WORKTREE already exists
269
269
*/
270
- if (file_exists (git_path (NOTES_MERGE_WORKTREE ))) {
270
+ if (file_exists (git_path (NOTES_MERGE_WORKTREE )) &&
271
+ !is_empty_dir (git_path (NOTES_MERGE_WORKTREE ))) {
271
272
if (advice_resolve_conflict )
272
273
die ("You have not concluded your previous "
273
274
"notes merge (%s exists).\nPlease, use "
@@ -687,51 +688,60 @@ int notes_merge_commit(struct notes_merge_options *o,
687
688
{
688
689
/*
689
690
* Iterate through files in .git/NOTES_MERGE_WORKTREE and add all
690
- * found notes to 'partial_tree'. Write the updates notes tree to
691
+ * found notes to 'partial_tree'. Write the updated notes tree to
691
692
* the DB, and commit the resulting tree object while reusing the
692
693
* commit message and parents from 'partial_commit'.
693
694
* Finally store the new commit object SHA1 into 'result_sha1'.
694
695
*/
695
- struct dir_struct dir ;
696
- char * path = xstrdup ( git_path ( NOTES_MERGE_WORKTREE "/" )) ;
697
- int path_len = strlen ( path ), i ;
696
+ DIR * dir ;
697
+ struct dirent * e ;
698
+ struct strbuf path = STRBUF_INIT ;
698
699
char * msg = strstr (partial_commit -> buffer , "\n\n" );
699
700
struct strbuf sb_msg = STRBUF_INIT ;
701
+ int baselen ;
700
702
703
+ strbuf_addstr (& path , git_path (NOTES_MERGE_WORKTREE ));
701
704
if (o -> verbosity >= 3 )
702
- printf ("Committing notes in notes merge worktree at %.* s\n" ,
703
- path_len - 1 , path );
705
+ printf ("Committing notes in notes merge worktree at %s\n" ,
706
+ path . buf );
704
707
705
708
if (!msg || msg [2 ] == '\0' )
706
709
die ("partial notes commit has empty message" );
707
710
msg += 2 ;
708
711
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 ];
712
+ dir = opendir (path .buf );
713
+ if (!dir )
714
+ die_errno ("could not open %s" , path .buf );
715
+
716
+ strbuf_addch (& path , '/' );
717
+ baselen = path .len ;
718
+ while ((e = readdir (dir )) != NULL ) {
713
719
struct stat st ;
714
- const char * relpath = ent -> name + path_len ;
715
720
unsigned char obj_sha1 [20 ], blob_sha1 [20 ];
716
721
717
- if (ent -> len - path_len != 40 || get_sha1_hex (relpath , obj_sha1 )) {
722
+ if (is_dot_or_dotdot (e -> d_name ))
723
+ continue ;
724
+
725
+ if (strlen (e -> d_name ) != 40 || get_sha1_hex (e -> d_name , obj_sha1 )) {
718
726
if (o -> verbosity >= 3 )
719
- printf ("Skipping non-SHA1 entry '%s'\n" ,
720
- ent -> name );
727
+ printf ("Skipping non-SHA1 entry '%s%s '\n" ,
728
+ path . buf , e -> d_name );
721
729
continue ;
722
730
}
723
731
732
+ strbuf_addstr (& path , e -> d_name );
724
733
/* 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 );
734
+ if (stat (path . buf , & st ))
735
+ die_errno ("Failed to stat '%s'" , path . buf );
736
+ if (index_path (blob_sha1 , path . buf , & st , HASH_WRITE_OBJECT ))
737
+ die ("Failed to write blob object from '%s'" , path . buf );
729
738
if (add_note (partial_tree , obj_sha1 , blob_sha1 , NULL ))
730
739
die ("Failed to add resolved note '%s' to notes tree" ,
731
- ent -> name );
740
+ path . buf );
732
741
if (o -> verbosity >= 4 )
733
742
printf ("Added resolved note for object %s: %s\n" ,
734
743
sha1_to_hex (obj_sha1 ), sha1_to_hex (blob_sha1 ));
744
+ strbuf_setlen (& path , baselen );
735
745
}
736
746
737
747
strbuf_attach (& sb_msg , msg , strlen (msg ), strlen (msg ) + 1 );
@@ -740,20 +750,25 @@ int notes_merge_commit(struct notes_merge_options *o,
740
750
if (o -> verbosity >= 4 )
741
751
printf ("Finalized notes merge commit: %s\n" ,
742
752
sha1_to_hex (result_sha1 ));
743
- free (path );
753
+ strbuf_release (& path );
754
+ closedir (dir );
744
755
return 0 ;
745
756
}
746
757
747
758
int notes_merge_abort (struct notes_merge_options * o )
748
759
{
749
- /* Remove .git/NOTES_MERGE_WORKTREE directory and all files within */
760
+ /*
761
+ * Remove all files within .git/NOTES_MERGE_WORKTREE. We do not remove
762
+ * the .git/NOTES_MERGE_WORKTREE directory itself, since it might be
763
+ * the current working directory of the user.
764
+ */
750
765
struct strbuf buf = STRBUF_INIT ;
751
766
int ret ;
752
767
753
768
strbuf_addstr (& buf , git_path (NOTES_MERGE_WORKTREE ));
754
769
if (o -> verbosity >= 3 )
755
- printf ("Removing notes merge worktree at %s\n" , buf .buf );
756
- ret = remove_dir_recursively (& buf , 0 );
770
+ printf ("Removing notes merge worktree at %s/* \n" , buf .buf );
771
+ ret = remove_dir_recursively (& buf , REMOVE_DIR_KEEP_TOPLEVEL );
757
772
strbuf_release (& buf );
758
773
return ret ;
759
774
}
0 commit comments