Commit 3e3a69f1a29c6322b5464dca5023d8766661d7bf

Stefan Sperling 2019-07-25T14:25:02

open fileindex just once to make rebase and histedit faster

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
diff --git a/got/got.c b/got/got.c
index d826f0a..f267e72 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3598,18 +3598,19 @@ rebase_progress(void *arg, unsigned char status, const char *path)
 }
 
 static const struct got_error *
-rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
-    struct got_reference *new_base_branch, struct got_reference *tmp_branch,
-    struct got_repository *repo)
+rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
+    struct got_reference *branch, struct got_reference *new_base_branch,
+    struct got_reference *tmp_branch, struct got_repository *repo)
 {
 	printf("Switching work tree to %s\n", got_ref_get_name(branch));
-	return got_worktree_rebase_complete(worktree,
+	return got_worktree_rebase_complete(worktree, fileindex,
 	    new_base_branch, tmp_branch, branch, repo);
 }
 
 static const struct got_error *
 rebase_commit(struct got_pathlist_head *merged_paths,
-    struct got_worktree *worktree, struct got_reference *tmp_branch,
+    struct got_worktree *worktree, struct got_fileindex *fileindex,
+    struct got_reference *tmp_branch,
     struct got_object_id *commit_id, struct got_repository *repo)
 {
 	const struct got_error *error;
@@ -3621,7 +3622,7 @@ rebase_commit(struct got_pathlist_head *merged_paths,
 		return error;
 
 	error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
-	    worktree, tmp_branch, commit, commit_id, repo);
+	    worktree, fileindex, tmp_branch, commit, commit_id, repo);
 	if (error) {
 		if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
 			goto done;
@@ -3764,6 +3765,7 @@ cmd_rebase(int argc, char *argv[])
 	const struct got_error *error = NULL;
 	struct got_worktree *worktree = NULL;
 	struct got_repository *repo = NULL;
+	struct got_fileindex *fileindex = NULL;
 	char *cwd = NULL;
 	struct got_reference *branch = NULL;
 	struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
@@ -3840,12 +3842,13 @@ cmd_rebase(int argc, char *argv[])
 			goto done;
 		}
 		error = got_worktree_rebase_continue(&resume_commit_id,
-		    &new_base_branch, &tmp_branch, &branch, worktree, repo);
+		    &new_base_branch, &tmp_branch, &branch, &fileindex,
+		    worktree, repo);
 		if (error)
 			goto done;
 		printf("Switching work tree to %s\n",
 		    got_ref_get_symref_target(new_base_branch));
-		error = got_worktree_rebase_abort(worktree, repo,
+		error = got_worktree_rebase_abort(worktree, fileindex, repo,
 		    new_base_branch, update_progress, &did_something);
 		if (error)
 			goto done;
@@ -3859,11 +3862,12 @@ cmd_rebase(int argc, char *argv[])
 			goto done;
 		}
 		error = got_worktree_rebase_continue(&resume_commit_id,
-		    &new_base_branch, &tmp_branch, &branch, worktree, repo);
+		    &new_base_branch, &tmp_branch, &branch, &fileindex,
+		    worktree, repo);
 		if (error)
 			goto done;
 
-		error = rebase_commit(NULL, worktree, tmp_branch,
+		error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
 		    resume_commit_id, repo);
 		if (error)
 			goto done;
@@ -3910,7 +3914,7 @@ cmd_rebase(int argc, char *argv[])
 		}
 
 		error = got_worktree_rebase_prepare(&new_base_branch,
-		    &tmp_branch, worktree, branch, repo);
+		    &tmp_branch, &fileindex, worktree, branch, repo);
 		if (error)
 			goto done;
 	}
@@ -3932,8 +3936,8 @@ cmd_rebase(int argc, char *argv[])
 
 	if (SIMPLEQ_EMPTY(&commits)) {
 		if (continue_rebase)
-			error = rebase_complete(worktree, branch,
-			    new_base_branch, tmp_branch, repo);
+			error = rebase_complete(worktree, fileindex,
+			    branch, new_base_branch, tmp_branch, repo);
 		else
 			error = got_error(GOT_ERR_EMPTY_REBASE);
 		goto done;
@@ -3946,8 +3950,8 @@ cmd_rebase(int argc, char *argv[])
 		pid = qid;
 
 		error = got_worktree_rebase_merge_files(&merged_paths,
-		    worktree, parent_id, commit_id, repo, rebase_progress,
-		    &rebase_status, check_cancelled, NULL);
+		    worktree, fileindex, parent_id, commit_id, repo,
+		    rebase_progress, &rebase_status, check_cancelled, NULL);
 		if (error)
 			goto done;
 
@@ -3956,22 +3960,22 @@ cmd_rebase(int argc, char *argv[])
 			break;
 		}
 
-		error = rebase_commit(&merged_paths, worktree, tmp_branch,
-		    commit_id, repo);
+		error = rebase_commit(&merged_paths, worktree, fileindex,
+		    tmp_branch, commit_id, repo);
 		got_worktree_rebase_pathlist_free(&merged_paths);
 		if (error)
 			goto done;
 	}
 
 	if (rebase_status == GOT_STATUS_CONFLICT) {
-		error = got_worktree_rebase_postpone(worktree);
+		error = got_worktree_rebase_postpone(worktree, fileindex);
 		if (error)
 			goto done;
 		error = got_error_msg(GOT_ERR_CONFLICTS,
 		    "conflicts must be resolved before rebasing can continue");
 	} else
-		error = rebase_complete(worktree, branch, new_base_branch,
-		    tmp_branch, repo);
+		error = rebase_complete(worktree, fileindex, branch,
+		    new_base_branch, tmp_branch, repo);
 done:
 	got_object_id_queue_free(&commits);
 	free(branch_head_commit_id);
@@ -4520,13 +4524,13 @@ histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
 
 static const struct got_error *
 histedit_complete(struct got_worktree *worktree,
-    struct got_reference *tmp_branch, struct got_reference *branch,
-    struct got_repository *repo)
+    struct got_fileindex *fileindex, struct got_reference *tmp_branch,
+    struct got_reference *branch, struct got_repository *repo)
 {
 	printf("Switching work tree to %s\n",
 	    got_ref_get_symref_target(branch));
-	return got_worktree_histedit_complete(worktree, tmp_branch, branch,
-	    repo);
+	return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
+	    branch, repo);
 }
 
 static const struct got_error *
@@ -4586,8 +4590,9 @@ done:
 
 static const struct got_error *
 histedit_commit(struct got_pathlist_head *merged_paths,
-    struct got_worktree *worktree, struct got_reference *tmp_branch,
-    struct got_histedit_list_entry *hle, struct got_repository *repo)
+    struct got_worktree *worktree, struct got_fileindex *fileindex,
+    struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
+    struct got_repository *repo)
 {
 	const struct got_error *err;
 	struct got_commit_object *commit;
@@ -4605,7 +4610,8 @@ histedit_commit(struct got_pathlist_head *merged_paths,
 		return err;
 
 	err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
-	    worktree, tmp_branch, commit, hle->commit_id, hle->logmsg, repo);
+	    worktree, fileindex, tmp_branch, commit, hle->commit_id,
+	    hle->logmsg, repo);
 	if (err) {
 		if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
 			goto done;
@@ -4681,6 +4687,7 @@ cmd_histedit(int argc, char *argv[])
 {
 	const struct got_error *error = NULL;
 	struct got_worktree *worktree = NULL;
+	struct got_fileindex *fileindex = NULL;
 	struct got_repository *repo = NULL;
 	char *cwd = NULL;
 	struct got_reference *branch = NULL;
@@ -4771,12 +4778,13 @@ cmd_histedit(int argc, char *argv[])
 	if (edit_in_progress && abort_edit) {
 		int did_something;
 		error = got_worktree_histedit_continue(&resume_commit_id,
-		    &tmp_branch, &branch, &base_commit_id, worktree, repo);
+		    &tmp_branch, &branch, &base_commit_id, &fileindex,
+		    worktree, repo);
 		if (error)
 			goto done;
 		printf("Switching work tree to %s\n",
 		    got_ref_get_symref_target(branch));
-		error = got_worktree_histedit_abort(worktree, repo,
+		error = got_worktree_histedit_abort(worktree, fileindex, repo,
 		    branch, base_commit_id, update_progress, &did_something);
 		if (error)
 			goto done;
@@ -4806,7 +4814,8 @@ cmd_histedit(int argc, char *argv[])
 			goto done;
 
 		error = got_worktree_histedit_continue(&resume_commit_id,
-		    &tmp_branch, &branch, &base_commit_id, worktree, repo);
+		    &tmp_branch, &branch, &base_commit_id, &fileindex,
+		    worktree, repo);
 		if (error)
 			goto done;
 
@@ -4884,7 +4893,7 @@ cmd_histedit(int argc, char *argv[])
 			goto done;
 
 		error = got_worktree_histedit_prepare(&tmp_branch, &branch,
-		    &base_commit_id, worktree, repo);
+		    &base_commit_id, &fileindex, worktree, repo);
 		if (error)
 			goto done;
 
@@ -4907,7 +4916,7 @@ cmd_histedit(int argc, char *argv[])
 				   repo);
 			} else {
 				error = histedit_commit(NULL, worktree,
-				    tmp_branch, hle, repo);
+				    fileindex, tmp_branch, hle, repo);
 			}
 			if (error)
 				goto done;
@@ -4929,8 +4938,8 @@ cmd_histedit(int argc, char *argv[])
 		pid = SIMPLEQ_FIRST(parent_ids);
 
 		error = got_worktree_histedit_merge_files(&merged_paths,
-		    worktree, pid->id, hle->commit_id, repo, rebase_progress,
-		    &rebase_status, check_cancelled, NULL);
+		    worktree, fileindex, pid->id, hle->commit_id, repo,
+		    rebase_progress, &rebase_status, check_cancelled, NULL);
 		if (error)
 			goto done;
 		got_object_commit_close(commit);
@@ -4950,7 +4959,8 @@ cmd_histedit(int argc, char *argv[])
 			    id_str);
 			free(id_str);
 			got_worktree_rebase_pathlist_free(&merged_paths);
-			error = got_worktree_histedit_postpone(worktree);
+			error = got_worktree_histedit_postpone(worktree,
+			    fileindex);
 			goto done;
 		}
 
@@ -4961,21 +4971,22 @@ cmd_histedit(int argc, char *argv[])
 			continue;
 		}
 
-		error = histedit_commit(&merged_paths, worktree, tmp_branch,
-		    hle, repo);
+		error = histedit_commit(&merged_paths, worktree, fileindex,
+		    tmp_branch, hle, repo);
 		got_worktree_rebase_pathlist_free(&merged_paths);
 		if (error)
 			goto done;
 	}
 
 	if (rebase_status == GOT_STATUS_CONFLICT) {
-		error = got_worktree_histedit_postpone(worktree);
+		error = got_worktree_histedit_postpone(worktree, fileindex);
 		if (error)
 			goto done;
 		error = got_error_msg(GOT_ERR_CONFLICTS,
 		    "conflicts must be resolved before rebasing can continue");
 	} else
-		error = histedit_complete(worktree, tmp_branch, branch, repo);
+		error = histedit_complete(worktree, fileindex, tmp_branch,
+		    branch, repo);
 done:
 	got_object_id_queue_free(&commits);
 	free(head_commit_id);
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 6e6d2f5..e9f5a6c 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -17,6 +17,7 @@
 struct got_worktree;
 struct got_commitable;
 struct got_commit_object;
+struct got_fileindex;
 
 /* status codes */
 #define GOT_STATUS_NO_CHANGE	' '
@@ -218,20 +219,24 @@ unsigned int got_commitable_get_status(struct got_commitable *);
  * "got/worktree/rebase/" namespace. These references are used to
  * keep track of rebase operation state and are used as input and/or
  * output arguments with other rebase-related functions.
+ * The function also returns a pointer to a fileindex which must be
+ * passed back to other rebase-related functions.
  */
 const struct got_error *got_worktree_rebase_prepare(struct got_reference **,
-    struct got_reference **, struct got_worktree *, struct got_reference *,
-    struct got_repository *);
+    struct got_reference **, struct got_fileindex **, struct got_worktree *,
+    struct got_reference *, struct got_repository *);
 
 /*
  * Continue an interrupted rebase operation.
  * This function returns existing references created when rebase was prepared,
  * and the ID of the commit currently being rebased. This should be called
  * before either resuming or aborting a rebase operation.
+ * The function also returns a pointer to a fileindex which must be
+ * passed back to other rebase-related functions.
  */
 const struct got_error *got_worktree_rebase_continue(struct got_object_id **,
     struct got_reference **, struct got_reference **, struct got_reference **,
-    struct got_worktree *, struct got_repository *);
+    struct got_fileindex **, struct got_worktree *, struct got_repository *);
 
 /* Check whether a, potentially interrupted, rebase operation is in progress. */
 const struct got_error *got_worktree_rebase_in_progress(int *,
@@ -246,7 +251,7 @@ const struct got_error *got_worktree_rebase_in_progress(int *,
  * got_worktree_rebase_pathlist_free().
  */
 const struct got_error *got_worktree_rebase_merge_files(
-    struct got_pathlist_head *, struct got_worktree *,
+    struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
     struct got_object_id *, struct got_object_id *, struct got_repository *,
     got_worktree_checkout_cb, void *, got_worktree_cancel_cb, void *);
 
@@ -257,7 +262,7 @@ const struct got_error *got_worktree_rebase_merge_files(
  * crawl across the entire work tree to find paths to commit.
  */
 const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
-    struct got_pathlist_head *, struct got_worktree *,
+    struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
     struct got_reference *, struct got_commit_object *,
     struct got_object_id *, struct got_repository *);
 
@@ -265,22 +270,23 @@ const struct got_error *got_worktree_rebase_commit(struct got_object_id **,
 void got_worktree_rebase_pathlist_free(struct got_pathlist_head *);
 
 /* Postpone the rebase operation. Should be called after a merge conflict. */
-const struct got_error *got_worktree_rebase_postpone(struct got_worktree *);
+const struct got_error *got_worktree_rebase_postpone(struct got_worktree *,
+    struct got_fileindex *);
 
 /*
  * Complete the current rebase operation. This should be called once all
  * commits have been rebased successfully.
  */
 const struct got_error *got_worktree_rebase_complete(struct got_worktree *,
-    struct got_reference *, struct got_reference *, struct got_reference *,
-    struct got_repository *);
+    struct got_fileindex *, struct got_reference *, struct got_reference *,
+    struct got_reference *, struct got_repository *);
 
 /*
  * Abort the current rebase operation.
  * Report reverted files via the specified progress callback.
  */
 const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
-    struct got_repository *, struct got_reference *,
+    struct got_fileindex *, struct got_repository *, struct got_reference *,
     got_worktree_checkout_cb, void *);
 
 /*
@@ -292,8 +298,8 @@ const struct got_error *got_worktree_rebase_abort(struct got_worktree *,
  * functions.
  */
 const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
-    struct got_reference **, struct got_object_id **, struct got_worktree *,
-    struct got_repository *);
+    struct got_reference **, struct got_object_id **, struct got_fileindex **,
+    struct got_worktree *, struct got_repository *);
 
 /*
  * Continue an interrupted histedit operation.
@@ -303,7 +309,7 @@ const struct got_error *got_worktree_histedit_prepare(struct got_reference **,
  */
 const struct got_error *got_worktree_histedit_continue(struct got_object_id **,
     struct got_reference **, struct got_reference **, struct got_object_id **,
-    struct got_worktree *, struct got_repository *);
+    struct got_fileindex **, struct got_worktree *, struct got_repository *);
 
 /* Check whether a histedit operation is in progress. */
 const struct got_error *got_worktree_histedit_in_progress(int *,
@@ -318,7 +324,7 @@ const struct got_error *got_worktree_histedit_in_progress(int *,
  * got_worktree_rebase_pathlist_free().
  */
 const struct got_error *got_worktree_histedit_merge_files(
-    struct got_pathlist_head *, struct got_worktree *,
+    struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
     struct got_object_id *, struct got_object_id *, struct got_repository *,
     got_worktree_checkout_cb, void *, got_worktree_cancel_cb, void *);
 
@@ -331,7 +337,7 @@ const struct got_error *got_worktree_histedit_merge_files(
  * commit's original message.
  */
 const struct got_error *got_worktree_histedit_commit(struct got_object_id **,
-    struct got_pathlist_head *, struct got_worktree *,
+    struct got_pathlist_head *, struct got_worktree *, struct got_fileindex *,
     struct got_reference *, struct got_commit_object *,
     struct got_object_id *, const char *, struct got_repository *);
 
@@ -344,22 +350,24 @@ const struct got_error *got_worktree_histedit_skip_commit(struct got_worktree *,
     struct got_object_id *, struct got_repository *);
 
 /* Postpone the histedit operation. */
-const struct got_error *got_worktree_histedit_postpone(struct got_worktree *);
+const struct got_error *got_worktree_histedit_postpone(struct got_worktree *,
+    struct got_fileindex *);
 
 /*
  * Complete the current histedit operation. This should be called once all
  * commits have been edited successfully.
  */
 const struct got_error *got_worktree_histedit_complete(struct got_worktree *,
-    struct got_reference *, struct got_reference *, struct got_repository *);
+    struct got_fileindex *, struct got_reference *, struct got_reference *,
+    struct got_repository *);
 
 /*
  * Abort the current histedit operation.
  * Report reverted files via the specified progress callback.
  */
 const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
-    struct got_repository *, struct got_reference *, struct got_object_id *,
-    got_worktree_checkout_cb, void *);
+    struct got_fileindex *, struct got_repository *, struct got_reference *,
+    struct got_object_id *, got_worktree_checkout_cb, void *);
 
 /* Get the path to this work tree's histedit command list file. */
 const struct got_error *got_worktree_get_histedit_list_path(char **,
diff --git a/lib/worktree.c b/lib/worktree.c
index e97e4a4..a4aa679 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1507,6 +1507,20 @@ done:
 }
 
 static const struct got_error *
+get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
+{
+	const struct got_error *err = NULL;
+
+	if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
+	    GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
+		err = got_error_from_errno("asprintf");
+		*fileindex_path = NULL;
+	}
+	return err;
+}
+
+
+static const struct got_error *
 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
    struct got_worktree *worktree)
 {
@@ -1518,12 +1532,9 @@ open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
 	if (*fileindex == NULL)
 		return got_error_from_errno("got_fileindex_alloc");
 
-	if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
-	    GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
-		err = got_error_from_errno("asprintf");
-		*fileindex_path = NULL;
+	err = get_fileindex_path(fileindex_path, worktree);
+	if (err)
 		goto done;
-	}
 
 	index = fopen(*fileindex_path, "rb");
 	if (index == NULL) {
@@ -3599,32 +3610,33 @@ check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
 
 const struct got_error *
 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
-    struct got_reference **tmp_branch, struct got_worktree *worktree,
-    struct got_reference *branch, struct got_repository *repo)
+    struct got_reference **tmp_branch, struct got_fileindex **fileindex,
+    struct got_worktree *worktree, struct got_reference *branch,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
 	char *branch_ref_name = NULL;
-	struct got_fileindex *fileindex = NULL;
 	char *fileindex_path = NULL;
 	struct check_rebase_ok_arg ok_arg;
 	struct got_reference *wt_branch = NULL, *branch_ref = NULL;
 
 	*new_base_branch_ref = NULL;
 	*tmp_branch = NULL;
+	*fileindex = NULL;
 
 	err = lock_worktree(worktree, LOCK_EX);
 	if (err)
 		return err;
 
-	err = open_fileindex(&fileindex, &fileindex_path, worktree);
+	err = open_fileindex(fileindex, &fileindex_path, worktree);
 	if (err)
 		goto done;
 
 	ok_arg.worktree = worktree;
 	ok_arg.repo = repo;
 	ok_arg.rebase_in_progress = 0;
-	err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
+	err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
 	    &ok_arg);
 	if (err)
 		goto done;
@@ -3677,8 +3689,6 @@ got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
 		goto done;
 done:
 	free(fileindex_path);
-	if (fileindex)
-		got_fileindex_free(fileindex);
 	free(tmp_branch_name);
 	free(new_base_branch_ref_name);
 	free(branch_ref_name);
@@ -3695,6 +3705,10 @@ done:
 			got_ref_close(*tmp_branch);
 			*tmp_branch = NULL;
 		}
+		if (*fileindex) {
+			got_fileindex_free(*fileindex);
+			*fileindex = NULL;
+		}
 		lock_worktree(worktree, LOCK_SH);
 	}
 	return err;
@@ -3703,15 +3717,28 @@ done:
 const struct got_error *
 got_worktree_rebase_continue(struct got_object_id **commit_id,
     struct got_reference **new_base_branch, struct got_reference **tmp_branch,
-    struct got_reference **branch, struct got_worktree *worktree,
-    struct got_repository *repo)
+    struct got_reference **branch, struct got_fileindex **fileindex,
+    struct got_worktree *worktree, struct got_repository *repo)
 {
 	const struct got_error *err;
 	char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
 	char *tmp_branch_name = NULL, *branch_ref_name = NULL;
 	struct got_reference *commit_ref = NULL, *branch_ref = NULL;
+	char *fileindex_path = NULL;
 
 	*commit_id = NULL;
+	*new_base_branch = NULL;
+	*tmp_branch = NULL;
+	*branch = NULL;
+	*fileindex = NULL;
+
+	err = lock_worktree(worktree, LOCK_EX);
+	if (err)
+		return err;
+
+	err = open_fileindex(fileindex, &fileindex_path, worktree);
+	if (err)
+		goto done;
 
 	err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
 	if (err)
@@ -3757,6 +3784,7 @@ got_worktree_rebase_continue(struct got_object_id **commit_id,
 done:
 	free(commit_ref_name);
 	free(branch_ref_name);
+	free(fileindex_path);
 	if (commit_ref)
 		got_ref_close(commit_ref);
 	if (branch_ref)
@@ -3776,6 +3804,11 @@ done:
 			got_ref_close(*branch);
 			*branch = NULL;
 		}
+		if (*fileindex) {
+			got_fileindex_free(*fileindex);
+			*fileindex = NULL;
+		}
+		lock_worktree(worktree, LOCK_SH);
 	}
 	return err;
 }
@@ -3895,19 +3928,19 @@ done:
 static const struct got_error *
 rebase_merge_files(struct got_pathlist_head *merged_paths,
     const char *commit_ref_name, struct got_worktree *worktree,
-    struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
-    struct got_repository *repo, got_worktree_checkout_cb progress_cb,
-    void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+    struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
+    struct got_object_id *commit_id, struct got_repository *repo,
+    got_worktree_checkout_cb progress_cb, void *progress_arg,
+    got_worktree_cancel_cb cancel_cb, void *cancel_arg)
 {
 	const struct got_error *err;
-	struct got_fileindex *fileindex;
-	char *fileindex_path;
 	struct got_reference *commit_ref = NULL;
 	struct collect_merged_paths_arg cmp_arg;
+	char *fileindex_path;
 
 	/* Work tree is locked/unlocked during rebase preparation/teardown. */
 
-	err = open_fileindex(&fileindex, &fileindex_path, worktree);
+	err = get_fileindex_path(&fileindex_path, worktree);
 	if (err)
 		return err;
 
@@ -3917,8 +3950,6 @@ rebase_merge_files(struct got_pathlist_head *merged_paths,
 	err = merge_files(worktree, fileindex, fileindex_path,
 	    parent_commit_id, commit_id, repo, collect_merged_paths,
 	    &cmp_arg, cancel_cb, cancel_arg);
-	got_fileindex_free(fileindex);
-	free(fileindex_path);
 	if (commit_ref)
 		got_ref_close(commit_ref);
 	return err;
@@ -3926,8 +3957,9 @@ rebase_merge_files(struct got_pathlist_head *merged_paths,
 
 const struct got_error *
 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
-    struct got_worktree *worktree, struct got_object_id *parent_commit_id,
-    struct got_object_id *commit_id, struct got_repository *repo,
+    struct got_worktree *worktree, struct got_fileindex *fileindex,
+    struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
+    struct got_repository *repo,
     got_worktree_checkout_cb progress_cb, void *progress_arg,
     got_worktree_cancel_cb cancel_cb, void *cancel_arg)
 {
@@ -3943,8 +3975,8 @@ got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
 		goto done;
 
 	err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
-	    parent_commit_id, commit_id, repo, progress_cb, progress_arg,
-	    cancel_cb, cancel_arg);
+	    fileindex, parent_commit_id, commit_id, repo, progress_cb,
+	    progress_arg, cancel_cb, cancel_arg);
 done:
 	free(commit_ref_name);
 	return err;
@@ -3952,8 +3984,9 @@ done:
 
 const struct got_error *
 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
-    struct got_worktree *worktree, struct got_object_id *parent_commit_id,
-    struct got_object_id *commit_id, struct got_repository *repo,
+    struct got_worktree *worktree, struct got_fileindex *fileindex,
+    struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
+    struct got_repository *repo,
     got_worktree_checkout_cb progress_cb, void *progress_arg,
     got_worktree_cancel_cb cancel_cb, void *cancel_arg)
 {
@@ -3969,8 +4002,8 @@ got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
 		goto done;
 
 	err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
-	    parent_commit_id, commit_id, repo, progress_cb, progress_arg,
-	    cancel_cb, cancel_arg);
+	    fileindex, parent_commit_id, commit_id, repo, progress_cb,
+	    progress_arg, cancel_cb, cancel_arg);
 done:
 	free(commit_ref_name);
 	return err;
@@ -3979,14 +4012,13 @@ done:
 static const struct got_error *
 rebase_commit(struct got_object_id **new_commit_id,
     struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
-    struct got_worktree *worktree, struct got_reference *tmp_branch,
-    struct got_commit_object *orig_commit, const char *new_logmsg,
-    struct got_repository *repo)
+    struct got_worktree *worktree, struct got_fileindex *fileindex,
+    struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
+    const char *new_logmsg, struct got_repository *repo)
 {
 	const struct got_error *err, *sync_err;
 	struct got_pathlist_head commitable_paths;
 	struct collect_commitables_arg cc_arg;
-	struct got_fileindex *fileindex = NULL;
 	char *fileindex_path = NULL;
 	struct got_reference *head_ref = NULL;
 	struct got_object_id *head_commit_id = NULL;
@@ -3997,9 +4029,9 @@ rebase_commit(struct got_object_id **new_commit_id,
 
 	/* Work tree is locked/unlocked during rebase preparation/teardown. */
 
-	err = open_fileindex(&fileindex, &fileindex_path, worktree);
+	err = get_fileindex_path(&fileindex_path, worktree);
 	if (err)
-		goto done;
+		return err;
 
 	cc_arg.commitable_paths = &commitable_paths;
 	cc_arg.worktree = worktree;
@@ -4074,8 +4106,6 @@ rebase_commit(struct got_object_id **new_commit_id,
 	if (sync_err && err == NULL)
 		err = sync_err;
 done:
-	if (fileindex)
-		got_fileindex_free(fileindex);
 	free(fileindex_path);
 	free(head_commit_id);
 	if (head_ref)
@@ -4090,7 +4120,8 @@ done:
 const struct got_error *
 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
     struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
-    struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
+    struct got_fileindex *fileindex, struct got_reference *tmp_branch,
+    struct got_commit_object *orig_commit,
     struct got_object_id *orig_commit_id, struct got_repository *repo)
 {
 	const struct got_error *err;
@@ -4114,7 +4145,7 @@ got_worktree_rebase_commit(struct got_object_id **new_commit_id,
 	}
 
 	err = rebase_commit(new_commit_id, merged_paths, commit_ref,
-	    worktree, tmp_branch, orig_commit, NULL, repo);
+	    worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
 done:
 	if (commit_ref)
 		got_ref_close(commit_ref);
@@ -4126,7 +4157,8 @@ done:
 const struct got_error *
 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
     struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
-    struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
+    struct got_fileindex *fileindex, struct got_reference *tmp_branch,
+    struct got_commit_object *orig_commit,
     struct got_object_id *orig_commit_id, const char *new_logmsg,
     struct got_repository *repo)
 {
@@ -4151,7 +4183,7 @@ got_worktree_histedit_commit(struct got_object_id **new_commit_id,
 	}
 
 	err = rebase_commit(new_commit_id, merged_paths, commit_ref,
-	    worktree, tmp_branch, orig_commit, new_logmsg, repo);
+	    worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
 done:
 	if (commit_ref)
 		got_ref_close(commit_ref);
@@ -4161,8 +4193,11 @@ done:
 }
 
 const struct got_error *
-got_worktree_rebase_postpone(struct got_worktree *worktree)
+got_worktree_rebase_postpone(struct got_worktree *worktree,
+    struct got_fileindex *fileindex)
 {
+	if (fileindex)
+		got_fileindex_free(fileindex);
 	return lock_worktree(worktree, LOCK_SH);
 }
 
@@ -4229,8 +4264,8 @@ done:
 
 const struct got_error *
 got_worktree_rebase_complete(struct got_worktree *worktree,
-    struct got_reference *new_base_branch, struct got_reference *tmp_branch,
-    struct got_reference *rebased_branch,
+    struct got_fileindex *fileindex, struct got_reference *new_base_branch,
+    struct got_reference *tmp_branch, struct got_reference *rebased_branch,
     struct got_repository *repo)
 {
 	const struct got_error *err, *unlockerr;
@@ -4254,6 +4289,8 @@ got_worktree_rebase_complete(struct got_worktree *worktree,
 
 	err = delete_rebase_refs(worktree, repo);
 done:
+	if (fileindex)
+		got_fileindex_free(fileindex);
 	free(new_head_commit_id);
 	unlockerr = lock_worktree(worktree, LOCK_SH);
 	if (unlockerr && err == NULL)
@@ -4293,13 +4330,13 @@ collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
 
 const struct got_error *
 got_worktree_rebase_abort(struct got_worktree *worktree,
-    struct got_repository *repo, struct got_reference *new_base_branch,
+    struct got_fileindex *fileindex, struct got_repository *repo,
+    struct got_reference *new_base_branch,
      got_worktree_checkout_cb progress_cb, void *progress_arg)
 {
 	const struct got_error *err, *unlockerr, *sync_err;
 	struct got_reference *resolved = NULL;
 	struct got_object_id *commit_id = NULL;
-	struct got_fileindex *fileindex = NULL;
 	char *fileindex_path = NULL;
 	struct got_pathlist_head revertible_paths;
 	struct got_pathlist_entry *pe;
@@ -4343,7 +4380,7 @@ got_worktree_rebase_abort(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
-	err = open_fileindex(&fileindex, &fileindex_path, worktree);
+	err = get_fileindex_path(&fileindex_path, worktree);
 	if (err)
 		goto done;
 
@@ -4387,13 +4424,13 @@ done:
 const struct got_error *
 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
     struct got_reference **branch_ref, struct got_object_id **base_commit_id,
-    struct got_worktree *worktree, struct got_repository *repo)
+    struct got_fileindex **fileindex, struct got_worktree *worktree,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *tmp_branch_name = NULL;
 	char *branch_ref_name = NULL;
 	char *base_commit_ref_name = NULL;
-	struct got_fileindex *fileindex = NULL;
 	char *fileindex_path = NULL;
 	struct check_rebase_ok_arg ok_arg;
 	struct got_reference *wt_branch = NULL;
@@ -4402,19 +4439,20 @@ got_worktree_histedit_prepare(struct got_reference **tmp_branch,
 	*tmp_branch = NULL;
 	*branch_ref = NULL;
 	*base_commit_id = NULL;
+	*fileindex = NULL;
 
 	err = lock_worktree(worktree, LOCK_EX);
 	if (err)
 		return err;
 
-	err = open_fileindex(&fileindex, &fileindex_path, worktree);
+	err = open_fileindex(fileindex, &fileindex_path, worktree);
 	if (err)
 		goto done;
 
 	ok_arg.worktree = worktree;
 	ok_arg.repo = repo;
 	ok_arg.rebase_in_progress = 0;
-	err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
+	err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
 	    &ok_arg);
 	if (err)
 		goto done;
@@ -4471,8 +4509,6 @@ got_worktree_histedit_prepare(struct got_reference **tmp_branch,
 		goto done;
 done:
 	free(fileindex_path);
-	if (fileindex)
-		got_fileindex_free(fileindex);
 	free(tmp_branch_name);
 	free(branch_ref_name);
 	free(base_commit_ref_name);
@@ -4488,14 +4524,21 @@ done:
 			*tmp_branch = NULL;
 		}
 		free(*base_commit_id);
+		if (*fileindex) {
+			got_fileindex_free(*fileindex);
+			*fileindex = NULL;
+		}
 		lock_worktree(worktree, LOCK_SH);
 	}
 	return err;
 }
 
 const struct got_error *
-got_worktree_histedit_postpone(struct got_worktree *worktree)
+got_worktree_histedit_postpone(struct got_worktree *worktree,
+    struct got_fileindex *fileindex)
 {
+	if (fileindex)
+		got_fileindex_free(fileindex);
 	return lock_worktree(worktree, LOCK_SH);
 }
 
@@ -4518,7 +4561,7 @@ got_worktree_histedit_in_progress(int *in_progress,
 const struct got_error *
 got_worktree_histedit_continue(struct got_object_id **commit_id,
     struct got_reference **tmp_branch, struct got_reference **branch_ref,
-    struct got_object_id **base_commit_id,
+    struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
     struct got_worktree *worktree, struct got_repository *repo)
 {
 	const struct got_error *err;
@@ -4526,10 +4569,20 @@ got_worktree_histedit_continue(struct got_object_id **commit_id,
 	char *tmp_branch_name = NULL, *branch_ref_name = NULL;
 	struct got_reference *commit_ref = NULL;
 	struct got_reference *base_commit_ref = NULL;
+	char *fileindex_path = NULL;
 
 	*commit_id = NULL;
 	*tmp_branch = NULL;
 	*base_commit_id = NULL;
+	*fileindex = NULL;
+
+	err = lock_worktree(worktree, LOCK_EX);
+	if (err)
+		return err;
+
+	err = open_fileindex(fileindex, &fileindex_path, worktree);
+	if (err)
+		goto done;
 
 	err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
 	if (err)
@@ -4572,6 +4625,7 @@ got_worktree_histedit_continue(struct got_object_id **commit_id,
 done:
 	free(commit_ref_name);
 	free(branch_ref_name);
+	free(fileindex_path);
 	if (commit_ref)
 		got_ref_close(commit_ref);
 	if (base_commit_ref)
@@ -4585,6 +4639,11 @@ done:
 			got_ref_close(*tmp_branch);
 			*tmp_branch = NULL;
 		}
+		if (*fileindex) {
+			got_fileindex_free(*fileindex);
+			*fileindex = NULL;
+		}
+		lock_worktree(worktree, LOCK_EX);
 	}
 	return err;
 }
@@ -4634,13 +4693,12 @@ done:
 
 const struct got_error *
 got_worktree_histedit_abort(struct got_worktree *worktree,
-    struct got_repository *repo, struct got_reference *branch,
-    struct got_object_id *base_commit_id,
+    struct got_fileindex *fileindex, struct got_repository *repo,
+    struct got_reference *branch, struct got_object_id *base_commit_id,
     got_worktree_checkout_cb progress_cb, void *progress_arg)
 {
 	const struct got_error *err, *unlockerr, *sync_err;
 	struct got_reference *resolved = NULL;
-	struct got_fileindex *fileindex = NULL;
 	char *fileindex_path = NULL;
 	struct got_pathlist_head revertible_paths;
 	struct got_pathlist_entry *pe;
@@ -4675,7 +4733,7 @@ got_worktree_histedit_abort(struct got_worktree *worktree,
 	if (err)
 		goto done;
 
-	err = open_fileindex(&fileindex, &fileindex_path, worktree);
+	err = get_fileindex_path(&fileindex_path, worktree);
 	if (err)
 		goto done;
 
@@ -4702,8 +4760,6 @@ sync:
 done:
 	got_ref_close(resolved);
 	free(tree_id);
-	if (fileindex)
-		got_fileindex_free(fileindex);
 	free(fileindex_path);
 	TAILQ_FOREACH(pe, &revertible_paths, entry)
 		free((char *)pe->path);
@@ -4717,8 +4773,8 @@ done:
 
 const struct got_error *
 got_worktree_histedit_complete(struct got_worktree *worktree,
-    struct got_reference *tmp_branch, struct got_reference *edited_branch,
-    struct got_repository *repo)
+    struct got_fileindex *fileindex, struct got_reference *tmp_branch,
+    struct got_reference *edited_branch, struct got_repository *repo)
 {
 	const struct got_error *err, *unlockerr;
 	struct got_object_id *new_head_commit_id = NULL;
@@ -4747,6 +4803,8 @@ got_worktree_histedit_complete(struct got_worktree *worktree,
 
 	err = delete_histedit_refs(worktree, repo);
 done:
+	if (fileindex)
+		got_fileindex_free(fileindex);
 	free(new_head_commit_id);
 	unlockerr = lock_worktree(worktree, LOCK_SH);
 	if (unlockerr && err == NULL)