Commit 0c6012293a78fc44970e555ed3c3544888fc811a

Carlos Martín Nieto 2015-01-05T20:10:43

Merge commit 'refs/pull/2632/head' of github.com:libgit2/libgit2

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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2c71d9b..5847a91 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -172,3 +172,7 @@ v0.21 + 1
 
 * The local transport now generates textual progress output like
   git-upload-pack does ("counting objects").
+
+* The git_submodule_update function was renamed to
+  git_submodule_update_strategy. git_submodule_update is now used to
+  provide functionalty similar to "git submodule update".
diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index 8efc26e..31c68e3 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -10,6 +10,8 @@
 #include "common.h"
 #include "types.h"
 #include "oid.h"
+#include "remote.h"
+#include "checkout.h"
 
 /**
  * @file git2/submodule.h
@@ -106,6 +108,68 @@ typedef enum {
 	GIT_SUBMODULE_STATUS_WD_UNTRACKED)) != 0)
 
 /**
+ * Submodule update options structure
+ *
+ * Use the GIT_SUBMODULE_UPDATE_OPTIONS_INIT to get the default settings, like this:
+ *
+ *		git_submodule_update_options opts = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+ */
+typedef struct git_submodule_update_options {
+	unsigned int version;
+
+	/**
+	 * These options are passed to the checkout step. To disable
+	 * checkout, set the `checkout_strategy` to
+	 * `GIT_CHECKOUT_NONE`. Generally you will want the use
+	 * GIT_CHECKOUT_SAFE to update files in the working
+	 * directory. Use the `clone_checkout_strategy` field
+	 * to set the checkout strategy that will be used in
+	 * the case where update needs to clone the repository.
+	 */
+	git_checkout_options checkout_opts;
+
+	/**
+	 * Callbacks to use for reporting fetch progress, and for acquiring
+	 * credentials in the event they are needed.
+	 */
+	git_remote_callbacks remote_callbacks;
+
+	/**
+	 * The checkout strategy to use when the sub repository needs to
+	 * be cloned. Use GIT_CHECKOUT_SAFE_CREATE to create all files
+	 * in the working directory for the newly cloned repository.
+	 */
+	unsigned int clone_checkout_strategy;
+
+	/**
+	 * The identity used when updating the reflog. NULL means to
+	 * use the default signature using the config.
+	 */
+	git_signature *signature;
+} git_submodule_update_options;
+
+#define GIT_SUBMODULE_UPDATE_OPTIONS_VERSION 1
+#define GIT_SUBMODULE_UPDATE_OPTIONS_INIT {GIT_CHECKOUT_OPTIONS_VERSION, {GIT_CHECKOUT_OPTIONS_VERSION, GIT_CHECKOUT_SAFE}, GIT_REMOTE_CALLBACKS_INIT, GIT_CHECKOUT_SAFE_CREATE}
+
+/**
+ * Update a submodule. This will clone a missing submodule and
+ * checkout the subrepository to the commit specified in the index of
+ * containing repository.
+ *
+ * @param submodule Submodule object
+ * @param init If the submodule is not initialized, setting this flag to true
+ *        will initialize the submodule before updating. Otherwise, this will
+ *        return an error if attempting to update an uninitialzed repository.
+ *        but setting this to true forces them to be updated.
+ * @param options configuration options for the update.  If NULL, the
+ *        function works as though GIT_SUBMODULE_UPDATE_OPTIONS_INIT was passed.
+ * @return 0 on success, any non-zero return value from a callback
+ *         function, or a negative value to indicate an error (use
+ *         `giterr_last` for a detailed error message).
+ */
+GIT_EXTERN(int) git_submodule_update(git_submodule *submodule, int init, git_submodule_update_options *options);
+
+/**
  * Lookup submodule information by name or path.
  *
  * Given either the submodule name or path (they are usually the same), this
@@ -403,7 +467,7 @@ GIT_EXTERN(git_submodule_ignore_t) git_submodule_set_ignore(
  * @return The current git_submodule_update_t value that will be used
  *         for this submodule.
  */
-GIT_EXTERN(git_submodule_update_t) git_submodule_update(
+GIT_EXTERN(git_submodule_update_t) git_submodule_update_strategy(
 	git_submodule *submodule);
 
 /**
diff --git a/src/submodule.c b/src/submodule.c
index 8dca0ae..d89dd04 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -381,10 +381,6 @@ int git_submodule_add_setup(
 		return GIT_EEXISTS;
 	}
 
-	/* resolve parameters */
-	if ((error = git_submodule_resolve_url(&real_url, repo, url)) < 0)
-		goto cleanup;
-
 	/* validate and normalize path */
 
 	if (git__prefixcmp(path, git_repository_workdir(repo)) == 0)
@@ -409,7 +405,7 @@ int git_submodule_add_setup(
 		goto cleanup;
 
 	if ((error = submodule_config_key_trunc_puts(&name, "url")) < 0 ||
-		(error = git_config_file_set_string(mods, name.ptr, real_url.ptr)) < 0)
+		(error = git_config_file_set_string(mods, name.ptr, url)) < 0)
 		goto cleanup;
 
 	git_buf_clear(&name);
@@ -425,7 +421,12 @@ int git_submodule_add_setup(
 	 */
 	if (!(git_path_exists(name.ptr) &&
 		git_path_contains(&name, DOT_GIT))) {
-		if ((error = submodule_repo_init(&subrepo, repo, path, real_url.ptr, use_gitlink)) < 0)
+
+		/* resolve the actual URL to use */
+		if ((error = git_submodule_resolve_url(&real_url, repo, url)) < 0)
+			goto cleanup;
+
+		 if ((error = submodule_repo_init(&subrepo, repo, path, real_url.ptr, use_gitlink)) < 0)
 			goto cleanup;
 	}
 
@@ -466,13 +467,24 @@ int git_submodule_repo_init(
 {
 	int error;
 	git_repository *sub_repo = NULL;
+	const char *configured_url;
+	git_config *cfg = NULL;
+	git_buf buf = GIT_BUF_INIT;
 
 	assert(out && sm);
 
-	error = submodule_repo_init(&sub_repo, sm->repo, sm->path, sm->url, use_gitlink);
+	/* get the configured remote url of the submodule */
+	if ((error = git_buf_printf(&buf, "submodule.%s.url", sm->name)) < 0 ||
+		(error = git_repository_config_snapshot(&cfg, sm->repo)) < 0 ||
+		(error = git_config_get_string(&configured_url, cfg, buf.ptr)) < 0 ||
+		(error = submodule_repo_init(&sub_repo, sm->repo, sm->path, configured_url, use_gitlink)) < 0)
+		goto done;
 
 	*out = sub_repo;
 
+done:
+	git_config_free(cfg);
+	git_buf_free(&buf);
 	return error;
 }
 
@@ -778,7 +790,7 @@ git_submodule_ignore_t git_submodule_set_ignore(
 	return old;
 }
 
-git_submodule_update_t git_submodule_update(git_submodule *submodule)
+git_submodule_update_t git_submodule_update_strategy(git_submodule *submodule)
 {
 	assert(submodule);
 	return (submodule->update < GIT_SUBMODULE_UPDATE_CHECKOUT) ?
@@ -823,11 +835,183 @@ git_submodule_recurse_t git_submodule_set_fetch_recurse_submodules(
 	return old;
 }
 
+static int submodule_repo_create(
+	git_repository **out,
+	git_repository *parent_repo,
+	const char *path)
+{
+	int error = 0;
+	git_buf workdir = GIT_BUF_INIT, repodir = GIT_BUF_INIT;
+	git_repository_init_options initopt = GIT_REPOSITORY_INIT_OPTIONS_INIT;
+	git_repository *subrepo = NULL;
+
+	initopt.flags =
+		GIT_REPOSITORY_INIT_MKPATH |
+		GIT_REPOSITORY_INIT_NO_REINIT |
+		GIT_REPOSITORY_INIT_NO_DOTGIT_DIR |
+		GIT_REPOSITORY_INIT_RELATIVE_GITLINK;
+
+	/* Workdir: path to sub-repo working directory */
+	error = git_buf_joinpath(&workdir, git_repository_workdir(parent_repo), path);
+	if (error < 0)
+		goto cleanup;
+
+	initopt.workdir_path = workdir.ptr;
+
+	/**
+	 * Repodir: path to the sub-repo. sub-repo goes in:
+	 * <repo-dir>/modules/<name>/ with a gitlink in the 
+	 * sub-repo workdir directory to that repository.
+	 */
+	error = git_buf_join3(
+		&repodir, '/', git_repository_path(parent_repo), "modules", path);
+	if (error < 0)
+		goto cleanup;
+
+	error = git_repository_init_ext(&subrepo, repodir.ptr, &initopt);
+
+cleanup:
+	git_buf_free(&workdir);
+	git_buf_free(&repodir);
+
+	*out = subrepo;
+
+	return error;
+}
+
+/**
+ * Callback to override sub-repository creation when
+ * cloning a sub-repository.
+ */
+static int git_submodule_update_repo_init_cb(
+	git_repository **out,
+	const char *path,
+	int bare,
+	void *payload)
+{
+	GIT_UNUSED(bare);
+	git_submodule *sm = payload;
+
+	return submodule_repo_create(out, sm->repo, path);
+}
+
+int git_submodule_update(git_submodule *sm, int init, git_submodule_update_options *_update_options)
+{
+	int error;
+	unsigned int submodule_status;
+	git_config *config = NULL;
+	const char *submodule_url;
+	git_repository *sub_repo = NULL;
+	git_remote *remote = NULL;
+	git_object *target_commit = NULL;
+	git_buf buf = GIT_BUF_INIT;
+	git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+	git_clone_options clone_options = GIT_CLONE_OPTIONS_INIT;
+
+	assert(sm);
+
+	if (_update_options)
+		memcpy(&update_options, _update_options, sizeof(git_submodule_update_options));
+
+	GITERR_CHECK_VERSION(&update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, "git_submodule_update_options");
+
+	/* Copy over the remote callbacks */
+	clone_options.remote_callbacks = update_options.remote_callbacks;
+	clone_options.signature = update_options.signature;
+
+	/* Get the status of the submodule to determine if it is already initialized  */
+	if ((error = git_submodule_status(&submodule_status, sm)) < 0)
+		goto done;
+
+	/*
+	 * If submodule work dir is not already initialized, check to see
+	 * what we need to do (initialize, clone, return error...)
+	 */
+	if (submodule_status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) {
+		/*
+		 * Work dir is not initialized, check to see if the submodule
+		 * info has been copied into .git/config
+		 */
+		if ((error = git_repository_config_snapshot(&config, sm->repo)) < 0 ||
+			(error = git_buf_printf(&buf, "submodule.%s.url", git_submodule_name(sm))) < 0)
+			goto done;
+
+		if ((error = git_config_get_string(&submodule_url, config, git_buf_cstr(&buf))) < 0) {
+			/*
+			 * If the error is not "not found" or if it is "not found" and we are not
+			 * initializing the submodule, then return error.
+			 */
+			if (error != GIT_ENOTFOUND)
+				goto done;
+
+			if (error == GIT_ENOTFOUND && !init) {
+				giterr_set(GITERR_SUBMODULE, "Submodule is not initialized.");
+				error = GIT_ERROR;
+				goto done;
+			}
+
+			/* The submodule has not been initialized yet - initialize it now.*/
+			if ((error = git_submodule_init(sm, 0)) < 0)
+				goto done;
+
+			git_config_free(config);
+			config = NULL;
+
+			if ((error = git_repository_config_snapshot(&config, sm->repo)) < 0 ||
+				(error = git_config_get_string(&submodule_url, config, git_buf_cstr(&buf))) < 0)
+				goto done;
+		}
+
+		/** submodule is initialized - now clone it **/
+		/* override repo creation */
+		clone_options.repository_cb = git_submodule_update_repo_init_cb;
+		clone_options.repository_cb_payload = sm;
+
+		/*
+		 * Do not perform checkout as part of clone, instead we 
+		 * will checkout the specific commit manually.
+		 */
+		clone_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE;
+		update_options.checkout_opts.checkout_strategy = update_options.clone_checkout_strategy;
+
+		if ((error = git_clone(&sub_repo, submodule_url, sm->path, &clone_options)) < 0 ||
+			(error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm), update_options.signature, NULL)) < 0 ||
+			(error = git_checkout_head(sub_repo, &update_options.checkout_opts)) != 0)
+			goto done;
+	} else {
+		/**
+		 * Work dir is initialized - look up the commit in the parent repository's index,
+		 * update the workdir contents of the subrepository, and set the subrepository's
+		 * head to the new commit.
+		 */
+		if ((error = git_submodule_open(&sub_repo, sm)) < 0 ||
+			(error = git_object_lookup(&target_commit, sub_repo, git_submodule_index_id(sm), GIT_OBJ_COMMIT)) < 0 ||
+			(error = git_checkout_tree(sub_repo, target_commit, &update_options.checkout_opts)) != 0 ||
+			(error = git_repository_set_head_detached(sub_repo, git_submodule_index_id(sm), update_options.signature, NULL)) < 0)
+			goto done;
+
+		/* Invalidate the wd flags as the workdir has been updated. */
+		sm->flags = sm->flags &
+			~(GIT_SUBMODULE_STATUS_IN_WD |
+		  	GIT_SUBMODULE_STATUS__WD_OID_VALID |
+		  	GIT_SUBMODULE_STATUS__WD_SCANNED);
+	}
+
+done:
+	git_buf_free(&buf);
+	git_config_free(config);
+	git_object_free(target_commit);
+	git_remote_free(remote);
+	git_repository_free(sub_repo);
+
+	return error;
+}
+
 int git_submodule_init(git_submodule *sm, int overwrite)
 {
 	int error;
 	const char *val;
-	git_buf key = GIT_BUF_INIT;
+	git_buf key = GIT_BUF_INIT, effective_submodule_url = GIT_BUF_INIT;
 	git_config *cfg = NULL;
 
 	if (!sm->url) {
@@ -841,9 +1025,10 @@ int git_submodule_init(git_submodule *sm, int overwrite)
 
 	/* write "submodule.NAME.url" */
 
-	if ((error = git_buf_printf(&key, "submodule.%s.url", sm->name)) < 0 ||
+	if ((error = git_submodule_resolve_url(&effective_submodule_url, sm->repo, sm->url)) < 0 ||
+		(error = git_buf_printf(&key, "submodule.%s.url", sm->name)) < 0 ||
 		(error = git_config__update_entry(
-			cfg, key.ptr, sm->url, overwrite != 0, false)) < 0)
+			cfg, key.ptr, effective_submodule_url.ptr, overwrite != 0, false)) < 0)
 		goto cleanup;
 
 	/* write "submodule.NAME.update" if not default */
@@ -861,6 +1046,7 @@ int git_submodule_init(git_submodule *sm, int overwrite)
 cleanup:
 	git_config_free(cfg);
 	git_buf_free(&key);
+	git_buf_free(&effective_submodule_url);
 
 	return error;
 }
@@ -1853,16 +2039,31 @@ static int lookup_head_remote_key(git_buf *remote_name, git_repository *repo)
 	if ((error = git_repository_head(&head, repo)) < 0)
 		return error;
 
+	/**
+	 * If head does not refer to a branch, then return
+	 * GIT_ENOTFOUND to indicate that we could not find
+	 * a remote key for the local tracking branch HEAD points to.
+	 **/
+	if (!git_reference_is_branch(head)) {
+		giterr_set(GITERR_INVALID,
+			"HEAD does not refer to a branch.");
+		error = GIT_ENOTFOUND;
+		goto done;
+	}
+
 	/* lookup remote tracking branch of HEAD */
-	if (!(error = git_branch_upstream_name(
-			&upstream_name, repo, git_reference_name(head))))
-	{
-		/* lookup remote of remote tracking branch */
-		error = git_branch_remote_name(remote_name, repo, upstream_name.ptr);
+	if ((error = git_branch_upstream_name(
+		&upstream_name,
+		repo,
+		git_reference_name(head))) < 0)
+		goto done;
 
-		git_buf_free(&upstream_name);
-	}
+	/* lookup remote of remote tracking branch */
+	if ((error = git_branch_remote_name(remote_name, repo, upstream_name.ptr)) < 0)
+		goto done;
 
+done:
+	git_buf_free(&upstream_name);
 	git_reference_free(head);
 
 	return error;
diff --git a/tests/resources/submodule_simple/.gitmodules b/tests/resources/submodule_simple/.gitmodules
new file mode 100644
index 0000000..03150b4
--- /dev/null
+++ b/tests/resources/submodule_simple/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "testrepo"]
+	path = testrepo
+	url = ../testrepo.git
diff --git a/tests/resources/submodule_simple/.gitted/HEAD b/tests/resources/submodule_simple/.gitted/HEAD
new file mode 100644
index 0000000..cb089cd
--- /dev/null
+++ b/tests/resources/submodule_simple/.gitted/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/tests/resources/submodule_simple/.gitted/config b/tests/resources/submodule_simple/.gitted/config
new file mode 100644
index 0000000..78387c5
--- /dev/null
+++ b/tests/resources/submodule_simple/.gitted/config
@@ -0,0 +1,8 @@
+[core]
+	repositoryformatversion = 0
+	filemode = false
+	bare = false
+	logallrefupdates = true
+	symlinks = false
+	ignorecase = true
+	hideDotFiles = dotGitOnly
diff --git a/tests/resources/submodule_simple/.gitted/description b/tests/resources/submodule_simple/.gitted/description
new file mode 100644
index 0000000..498b267
--- /dev/null
+++ b/tests/resources/submodule_simple/.gitted/description
@@ -0,0 +1 @@
+Unnamed repository; edit this file 'description' to name the repository.
diff --git a/tests/resources/submodule_simple/.gitted/index b/tests/resources/submodule_simple/.gitted/index
new file mode 100644
index 0000000..6e22d7f
Binary files /dev/null and b/tests/resources/submodule_simple/.gitted/index differ
diff --git a/tests/resources/submodule_simple/.gitted/objects/22/9cea838964f435d4fc2c11561ddb7447003609 b/tests/resources/submodule_simple/.gitted/objects/22/9cea838964f435d4fc2c11561ddb7447003609
new file mode 100644
index 0000000..9f0800d
Binary files /dev/null and b/tests/resources/submodule_simple/.gitted/objects/22/9cea838964f435d4fc2c11561ddb7447003609 differ
diff --git a/tests/resources/submodule_simple/.gitted/objects/5b/19f7523fbf55c96153ff5a94875583f1115a36 b/tests/resources/submodule_simple/.gitted/objects/5b/19f7523fbf55c96153ff5a94875583f1115a36
new file mode 100644
index 0000000..d0681ac
Binary files /dev/null and b/tests/resources/submodule_simple/.gitted/objects/5b/19f7523fbf55c96153ff5a94875583f1115a36 differ
diff --git a/tests/resources/submodule_simple/.gitted/objects/a8/575e6aaececba78823993e4f11abbc6172aabd b/tests/resources/submodule_simple/.gitted/objects/a8/575e6aaececba78823993e4f11abbc6172aabd
new file mode 100644
index 0000000..b8961b0
Binary files /dev/null and b/tests/resources/submodule_simple/.gitted/objects/a8/575e6aaececba78823993e4f11abbc6172aabd differ
diff --git a/tests/resources/submodule_simple/.gitted/objects/b4/f28943fad380f4ee3a9c6b95259b28204cc25a b/tests/resources/submodule_simple/.gitted/objects/b4/f28943fad380f4ee3a9c6b95259b28204cc25a
new file mode 100644
index 0000000..653238c
Binary files /dev/null and b/tests/resources/submodule_simple/.gitted/objects/b4/f28943fad380f4ee3a9c6b95259b28204cc25a differ
diff --git a/tests/resources/submodule_simple/.gitted/objects/d6/9ff504a3ba631f2fdb35bff93cc8cb8e85f4f8 b/tests/resources/submodule_simple/.gitted/objects/d6/9ff504a3ba631f2fdb35bff93cc8cb8e85f4f8
new file mode 100644
index 0000000..dabf65b
Binary files /dev/null and b/tests/resources/submodule_simple/.gitted/objects/d6/9ff504a3ba631f2fdb35bff93cc8cb8e85f4f8 differ
diff --git a/tests/resources/submodule_simple/.gitted/packed-refs b/tests/resources/submodule_simple/.gitted/packed-refs
new file mode 100644
index 0000000..0400126
--- /dev/null
+++ b/tests/resources/submodule_simple/.gitted/packed-refs
@@ -0,0 +1,3 @@
+# pack-refs with: peeled fully-peeled 
+229cea838964f435d4fc2c11561ddb7447003609 refs/remotes/origin/alternate_1
+a8575e6aaececba78823993e4f11abbc6172aabd refs/remotes/origin/master
diff --git a/tests/resources/submodule_simple/.gitted/refs/heads/alternate_1 b/tests/resources/submodule_simple/.gitted/refs/heads/alternate_1
new file mode 100644
index 0000000..6f51966
--- /dev/null
+++ b/tests/resources/submodule_simple/.gitted/refs/heads/alternate_1
@@ -0,0 +1 @@
+229cea838964f435d4fc2c11561ddb7447003609
diff --git a/tests/resources/submodule_simple/.gitted/refs/heads/master b/tests/resources/submodule_simple/.gitted/refs/heads/master
new file mode 100644
index 0000000..f01cfb2
--- /dev/null
+++ b/tests/resources/submodule_simple/.gitted/refs/heads/master
@@ -0,0 +1 @@
+a8575e6aaececba78823993e4f11abbc6172aabd
diff --git a/tests/submodule/init.c b/tests/submodule/init.c
new file mode 100644
index 0000000..d07bc9a
--- /dev/null
+++ b/tests/submodule/init.c
@@ -0,0 +1,111 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "path.h"
+#include "submodule_helpers.h"
+#include "fileops.h"
+
+static git_repository *g_repo = NULL;
+
+void test_submodule_init__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+void test_submodule_init__absolute_url(void)
+{
+	git_submodule *sm;
+	git_config *cfg;
+	git_buf absolute_url = GIT_BUF_INIT;
+	const char *config_url;
+
+	g_repo = setup_fixture_submodule_simple();
+
+	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
+	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	/* write the absolute url to the .gitmodules file*/
+	cl_git_pass(git_submodule_set_url(sm, absolute_url.ptr));
+
+	/* verify that the .gitmodules is set with an absolute path*/
+	cl_assert_equal_s(absolute_url.ptr, git_submodule_url(sm));
+
+	/* init and verify that absolute path is written to .git/config */
+	cl_git_pass(git_submodule_init(sm, false));
+
+	cl_git_pass(git_repository_config(&cfg, g_repo));
+
+	git_config_get_string(&config_url, cfg, "submodule.testrepo.url");
+	cl_assert_equal_s(absolute_url.ptr, config_url);
+
+	git_buf_free(&absolute_url);
+	git_config_free(cfg);
+}
+
+void test_submodule_init__relative_url(void)
+{
+	git_submodule *sm;
+	git_config *cfg;
+	git_buf absolute_url = GIT_BUF_INIT;
+	const char *config_url;
+
+	g_repo = setup_fixture_submodule_simple();
+
+	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
+	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	/* verify that the .gitmodules is set with an absolute path*/
+	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));
+
+	/* init and verify that absolute path is written to .git/config */
+	cl_git_pass(git_submodule_init(sm, false));
+
+	cl_git_pass(git_repository_config(&cfg, g_repo));
+
+	git_config_get_string(&config_url, cfg, "submodule.testrepo.url");
+	cl_assert_equal_s(absolute_url.ptr, config_url);
+
+	git_buf_free(&absolute_url);
+	git_config_free(cfg);
+}
+
+void test_submodule_init__relative_url_detached_head(void)
+{
+	git_submodule *sm;
+	git_config *cfg;
+	git_buf absolute_url = GIT_BUF_INIT;
+	const char *config_url;
+	git_reference *head_ref = NULL;
+	git_object *head_commit = NULL;
+
+	g_repo = setup_fixture_submodule_simple();
+
+	/* Put the parent repository into a detached head state. */
+	cl_git_pass(git_repository_head(&head_ref, g_repo));
+	cl_git_pass(git_reference_peel(&head_commit, head_ref, GIT_OBJ_COMMIT));
+
+	cl_git_pass(git_repository_set_head_detached(g_repo, git_commit_id((git_commit *)head_commit), NULL, NULL));
+
+	cl_assert(git_path_dirname_r(&absolute_url, git_repository_workdir(g_repo)) > 0);
+	cl_git_pass(git_buf_joinpath(&absolute_url, absolute_url.ptr, "testrepo.git"));
+
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	/* verify that the .gitmodules is set with an absolute path*/
+	cl_assert_equal_s("../testrepo.git", git_submodule_url(sm));
+
+	/* init and verify that absolute path is written to .git/config */
+	cl_git_pass(git_submodule_init(sm, false));
+
+	cl_git_pass(git_repository_config(&cfg, g_repo));
+
+	git_config_get_string(&config_url, cfg, "submodule.testrepo.url");
+	cl_assert_equal_s(absolute_url.ptr, config_url);
+
+	git_buf_free(&absolute_url);
+	git_config_free(cfg);
+
+}
diff --git a/tests/submodule/lookup.c b/tests/submodule/lookup.c
index 34de592..fa452fb 100644
--- a/tests/submodule/lookup.c
+++ b/tests/submodule/lookup.c
@@ -49,7 +49,7 @@ void test_submodule_lookup__accessors(void)
 	cl_assert(git_oid_streq(git_submodule_wd_id(sm), oid) == 0);
 
 	cl_assert(git_submodule_ignore(sm) == GIT_SUBMODULE_IGNORE_NONE);
-	cl_assert(git_submodule_update(sm) == GIT_SUBMODULE_UPDATE_CHECKOUT);
+	cl_assert(git_submodule_update_strategy(sm) == GIT_SUBMODULE_UPDATE_CHECKOUT);
 
 	git_submodule_free(sm);
 
diff --git a/tests/submodule/modify.c b/tests/submodule/modify.c
index 582d416..9bb48ba 100644
--- a/tests/submodule/modify.c
+++ b/tests/submodule/modify.c
@@ -160,7 +160,7 @@ void test_submodule_modify__edit_and_save(void)
 	cl_assert_equal_i(
 		GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm1));
 	cl_assert_equal_i(
-		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update(sm1));
+		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm1));
 	cl_assert_equal_i(
 		GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
 
@@ -179,7 +179,7 @@ void test_submodule_modify__edit_and_save(void)
 	/* check that revert was successful */
 	cl_assert_equal_s(old_url, git_submodule_url(sm1));
 	cl_assert_equal_i((int)old_ignore, (int)git_submodule_ignore(sm1));
-	cl_assert_equal_i((int)old_update, (int)git_submodule_update(sm1));
+	cl_assert_equal_i((int)old_update, (int)git_submodule_update_strategy(sm1));
 	cl_assert_equal_i(
 		old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1));
 
@@ -202,7 +202,7 @@ void test_submodule_modify__edit_and_save(void)
 	cl_assert_equal_i(
 		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
 	cl_assert_equal_i(
-		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
+		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
 	cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
 
 	/* call reload and check that the new values are loaded */
@@ -212,7 +212,7 @@ void test_submodule_modify__edit_and_save(void)
 	cl_assert_equal_i(
 		(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)git_submodule_ignore(sm1));
 	cl_assert_equal_i(
-		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update(sm1));
+		(int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(sm1));
 	cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
 
 	/* open a second copy of the repo and compare submodule */
@@ -223,7 +223,7 @@ void test_submodule_modify__edit_and_save(void)
 	cl_assert_equal_i(
 		GIT_SUBMODULE_IGNORE_UNTRACKED, git_submodule_ignore(sm2));
 	cl_assert_equal_i(
-		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update(sm2));
+		GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm2));
 	cl_assert_equal_i(
 		GIT_SUBMODULE_RECURSE_NO, git_submodule_fetch_recurse_submodules(sm2));
 
diff --git a/tests/submodule/repository_init.c b/tests/submodule/repository_init.c
index e9733a6..bf1968d 100644
--- a/tests/submodule/repository_init.c
+++ b/tests/submodule/repository_init.c
@@ -17,6 +17,7 @@ void test_submodule_repository_init__basic(void)
 	g_repo = setup_fixture_submod2();
 
 	cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_gitmodules_only"));
+	cl_git_pass(git_submodule_init(sm, 0));
 	cl_git_pass(git_submodule_repo_init(&repo, sm, 1));
 
 	/* Verify worktree */
diff --git a/tests/submodule/submodule_helpers.c b/tests/submodule/submodule_helpers.c
index c6d04b4..19bb04f 100644
--- a/tests/submodule/submodule_helpers.c
+++ b/tests/submodule/submodule_helpers.c
@@ -126,6 +126,20 @@ git_repository *setup_fixture_submod2(void)
 	return repo;
 }
 
+git_repository *setup_fixture_submodule_simple(void)
+{
+	git_repository *repo = cl_git_sandbox_init("submodule_simple");
+
+	cl_fixture_sandbox("testrepo.git");
+	p_mkdir("submodule_simple/testrepo", 0777);
+
+	cl_set_cleanup(cleanup_fixture_submodules, "testrepo.git");
+
+	cl_git_pass(git_repository_reinit_filesystem(repo, 1));
+
+	return repo;
+}
+
 void assert__submodule_exists(
 	git_repository *repo, const char *name,
 	const char *msg, const char *file, int line)
diff --git a/tests/submodule/submodule_helpers.h b/tests/submodule/submodule_helpers.h
index 4b2620b..1493f24 100644
--- a/tests/submodule/submodule_helpers.h
+++ b/tests/submodule/submodule_helpers.h
@@ -3,6 +3,7 @@ extern void rewrite_gitmodules(const char *workdir);
 /* these will automatically set a cleanup callback */
 extern git_repository *setup_fixture_submodules(void);
 extern git_repository *setup_fixture_submod2(void);
+extern git_repository *setup_fixture_submodule_simple(void);
 
 extern unsigned int get_submodule_status(git_repository *, const char *);
 
diff --git a/tests/submodule/update.c b/tests/submodule/update.c
new file mode 100644
index 0000000..ebf864d
--- /dev/null
+++ b/tests/submodule/update.c
@@ -0,0 +1,380 @@
+#include "clar_libgit2.h"
+#include "posix.h"
+#include "path.h"
+#include "submodule_helpers.h"
+#include "fileops.h"
+
+static git_repository *g_repo = NULL;
+
+void test_submodule_update__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+void test_submodule_update__unitialized_submodule_no_init(void)
+{
+	git_submodule *sm;
+	git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+
+	g_repo = setup_fixture_submodule_simple();
+
+	/* get the submodule */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	/* updating an unitialized repository throws */
+	cl_git_fail_with(
+		GIT_ERROR,
+		git_submodule_update(sm, 0, &update_options));
+
+	git_submodule_free(sm);
+}
+
+struct update_submodule_cb_payload {
+	int update_tips_called;
+	int checkout_progress_called;
+	int checkout_notify_called;
+};
+
+static void checkout_progress_cb(
+	const char *path,
+	size_t completed_steps,
+	size_t total_steps,
+	void *payload)
+{
+	struct update_submodule_cb_payload *update_payload = payload;
+
+	GIT_UNUSED(path);
+	GIT_UNUSED(completed_steps);
+	GIT_UNUSED(total_steps);
+
+	update_payload->checkout_progress_called = 1;
+}
+
+static int checkout_notify_cb(
+	git_checkout_notify_t why,
+	const char *path,
+	const git_diff_file *baseline,
+	const git_diff_file *target,
+	const git_diff_file *workdir,
+	void *payload)
+{
+	struct update_submodule_cb_payload *update_payload = payload;
+
+	GIT_UNUSED(why);
+	GIT_UNUSED(path);
+	GIT_UNUSED(baseline);
+	GIT_UNUSED(target);
+	GIT_UNUSED(workdir);
+
+	update_payload->checkout_notify_called = 1;
+
+	return 0;
+}
+
+static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
+{
+	struct update_submodule_cb_payload *update_payload = data;
+
+	GIT_UNUSED(refname);
+	GIT_UNUSED(a);
+	GIT_UNUSED(b);
+
+	update_payload->update_tips_called = 1;
+
+	return 1;
+}
+
+void test_submodule_update__update_submodule(void)
+{
+	git_submodule *sm;
+	git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+	unsigned int submodule_status = 0;
+	struct update_submodule_cb_payload update_payload = { 0 };
+
+	g_repo = setup_fixture_submodule_simple();
+
+	update_options.checkout_opts.progress_cb = checkout_progress_cb;
+	update_options.checkout_opts.progress_payload = &update_payload;
+
+	update_options.remote_callbacks.update_tips = update_tips;
+	update_options.remote_callbacks.payload = &update_payload;
+
+	/* get the submodule */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	/* verify the initial state of the submodule */
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_WD_UNINITIALIZED);
+
+	/* initialize and update the submodule */
+	cl_git_pass(git_submodule_init(sm, 0));
+	cl_git_pass(git_submodule_update(sm, 0, &update_options));
+
+	/* verify state */
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_IN_WD);
+
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+
+	/* verify that the expected callbacks have been called. */
+	cl_assert_equal_i(1, update_payload.checkout_progress_called);
+	cl_assert_equal_i(1, update_payload.update_tips_called);
+
+	git_submodule_free(sm);
+}
+
+void test_submodule_update__update_and_init_submodule(void)
+{
+	git_submodule *sm;
+	git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+	unsigned int submodule_status = 0;
+
+	g_repo = setup_fixture_submodule_simple();
+
+	/* get the submodule */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_WD_UNINITIALIZED);
+
+	/* update (with option to initialize sub repo) */
+	cl_git_pass(git_submodule_update(sm, 1, &update_options));
+
+	/* verify expected state */
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+
+	git_submodule_free(sm);
+}
+
+void test_submodule_update__update_already_checked_out_submodule(void)
+{
+	git_submodule *sm = NULL;
+	git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
+	git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+	unsigned int submodule_status = 0;
+	git_reference *branch_reference = NULL;
+	git_object *branch_commit = NULL;
+	struct update_submodule_cb_payload update_payload = { 0 };
+
+	g_repo = setup_fixture_submodule_simple();
+
+	update_options.checkout_opts.progress_cb = checkout_progress_cb;
+	update_options.checkout_opts.progress_payload = &update_payload;
+
+	/* Initialize and update the sub repository */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_WD_UNINITIALIZED);
+
+	cl_git_pass(git_submodule_update(sm, 1, &update_options));
+
+	/* verify expected state */
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+
+	/* checkout the alternate_1 branch */
+	checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
+
+	cl_git_pass(git_reference_lookup(&branch_reference, g_repo, "refs/heads/alternate_1"));
+	cl_git_pass(git_reference_peel(&branch_commit, branch_reference, GIT_OBJ_COMMIT));
+	cl_git_pass(git_checkout_tree(g_repo, branch_commit, &checkout_options));
+	cl_git_pass(git_repository_set_head(g_repo, git_reference_name(branch_reference), NULL, NULL));
+
+	/*
+	 * Verify state after checkout of parent repository. The submodule ID in the
+	 * HEAD commit and index should be updated, but not the workdir.
+	 */
+
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_IN_WD |
+		GIT_SUBMODULE_STATUS_WD_MODIFIED);
+
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+
+	/*
+	 * Update the submodule and verify the state.
+	 * Now, the HEAD, index, and Workdir commits should all be updated to
+	 * the new commit.
+	 */
+	cl_git_pass(git_submodule_update(sm, 0, &update_options));
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+
+	/* verify that the expected callbacks have been called. */
+	cl_assert_equal_i(1, update_payload.checkout_progress_called);
+	
+	git_submodule_free(sm);
+	git_object_free(branch_commit);
+	git_reference_free(branch_reference);
+}
+
+void test_submodule_update__update_blocks_on_dirty_wd(void)
+{
+	git_submodule *sm = NULL;
+	git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
+	git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+	unsigned int submodule_status = 0;
+	git_reference *branch_reference = NULL;
+	git_object *branch_commit = NULL;
+	struct update_submodule_cb_payload update_payload = { 0 };
+
+	g_repo = setup_fixture_submodule_simple();
+
+	update_options.checkout_opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
+	update_options.checkout_opts.notify_cb = checkout_notify_cb;
+	update_options.checkout_opts.notify_payload = &update_payload;
+
+	/* Initialize and update the sub repository */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_WD_UNINITIALIZED);
+
+	cl_git_pass(git_submodule_update(sm, 1, &update_options));
+
+	/* verify expected state */
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+
+	/* checkout the alternate_1 branch */
+	checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
+
+	cl_git_pass(git_reference_lookup(&branch_reference, g_repo, "refs/heads/alternate_1"));
+	cl_git_pass(git_reference_peel(&branch_commit, branch_reference, GIT_OBJ_COMMIT));
+	cl_git_pass(git_checkout_tree(g_repo, branch_commit, &checkout_options));
+	cl_git_pass(git_repository_set_head(g_repo, git_reference_name(branch_reference), NULL, NULL));
+
+	/*
+	 * Verify state after checkout of parent repository. The submodule ID in the
+	 * HEAD commit and index should be updated, but not the workdir.
+	 */
+
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_IN_WD |
+		GIT_SUBMODULE_STATUS_WD_MODIFIED);
+
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+
+	/*
+	 * Create a conflicting edit in the subrepository to verify that
+	 * the submodule update action is blocked.
+	 */
+	cl_git_write2file("submodule_simple/testrepo/branch_file.txt", "a conflicting edit", 0,
+		O_WRONLY | O_CREAT | O_TRUNC, 0755);
+
+	cl_git_fail(git_submodule_update(sm, 0, &update_options));
+
+	/* verify that the expected callbacks have been called. */
+	cl_assert_equal_i(1, update_payload.checkout_notify_called);
+
+	/* verify that the submodule state has not changed. */
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+
+	git_submodule_free(sm);
+	git_object_free(branch_commit);
+	git_reference_free(branch_reference);
+}
+
+void test_submodule_update__can_force_update(void)
+{
+	git_submodule *sm = NULL;
+	git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
+	git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+	unsigned int submodule_status = 0;
+	git_reference *branch_reference = NULL;
+	git_object *branch_commit = NULL;
+
+	g_repo = setup_fixture_submodule_simple();
+
+	/* Initialize and update the sub repository */
+	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
+
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_WD_UNINITIALIZED);
+
+	cl_git_pass(git_submodule_update(sm, 1, &update_options));
+
+	/* verify expected state */
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+
+	/* checkout the alternate_1 branch */
+	checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
+
+	cl_git_pass(git_reference_lookup(&branch_reference, g_repo, "refs/heads/alternate_1"));
+	cl_git_pass(git_reference_peel(&branch_commit, branch_reference, GIT_OBJ_COMMIT));
+	cl_git_pass(git_checkout_tree(g_repo, branch_commit, &checkout_options));
+	cl_git_pass(git_repository_set_head(g_repo, git_reference_name(branch_reference), NULL, NULL));
+
+	/*
+	 * Verify state after checkout of parent repository. The submodule ID in the
+	 * HEAD commit and index should be updated, but not the workdir.
+	 */
+	cl_git_pass(git_submodule_status(&submodule_status, sm));
+	cl_assert_equal_i(submodule_status, GIT_SUBMODULE_STATUS_IN_HEAD |
+		GIT_SUBMODULE_STATUS_IN_INDEX |
+		GIT_SUBMODULE_STATUS_IN_CONFIG |
+		GIT_SUBMODULE_STATUS_IN_WD |
+		GIT_SUBMODULE_STATUS_WD_MODIFIED);
+
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "be3563ae3f795b2b4353bcce3a527ad0a4f7f644") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+
+	/*
+	 * Create a conflicting edit in the subrepository to verify that
+	 * the submodule update action is blocked.
+	 */
+	cl_git_write2file("submodule_simple/testrepo/branch_file.txt", "a conflicting edit", 0,
+		O_WRONLY | O_CREAT | O_TRUNC, 0777);
+
+	/* forcefully checkout and verify the submodule state was updated. */
+	update_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+	cl_git_pass(git_submodule_update(sm, 0, &update_options));
+	cl_assert(git_oid_streq(git_submodule_head_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_wd_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+	cl_assert(git_oid_streq(git_submodule_index_id(sm), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750") == 0);
+
+	git_submodule_free(sm);
+	git_object_free(branch_commit);
+	git_reference_free(branch_reference);
+}