1108
static int push_check(int argc, const char **argv, const char *prefix)
1109
{
1110
struct remote *remote;
1111
+ const char *superproject_head;
1112
+ char *head;
1113
+ int detached_head = 0;
1114
+ struct object_id head_oid;
1115
1112
- if (argc < 2)
1113
- die("submodule--helper push-check requires at least 1 argument");
1116
+ if (argc < 3)
1117
+ die("submodule--helper push-check requires at least 2 arguments");
1118
+
1119
+ /*
1120
+ * superproject's resolved head ref.
1121
+ * if HEAD then the superproject is in a detached head state, otherwise
1122
+ * it will be the resolved head ref.
1123
+ */
1124
+ superproject_head = argv[1];
1125
+ argv++;
1126
+ argc--;
1127
+ /* Get the submodule's head ref and determine if it is detached */
1128
+ head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
1129
+ if (!head)
1130
+ die(_("Failed to resolve HEAD as a valid ref."));
1131
+ if (!strcmp(head, "HEAD"))
1132
+ detached_head = 1;
1133
1134
/*
1135
* The remote must be configured.
1152
if (rs->pattern || rs->matching)
1153
continue;
1154
1136
- /*
1137
- * LHS must match a single ref
1138
- * NEEDSWORK: add logic to special case 'HEAD' once
1139
- * working with submodules in a detached head state
1140
- * ceases to be the norm.
1141
- */
1142
- if (count_refspec_match(rs->src, local_refs, NULL) != 1)
1155
+ /* LHS must match a single ref */
1156
+ switch (count_refspec_match(rs->src, local_refs, NULL)) {
1157
+ case 1:
1158
+ break;
1159
+ case 0:
1160
+ /*
1161
+ * If LHS matches 'HEAD' then we need to ensure
1162
+ * that it matches the same named branch
1163
+ * checked out in the superproject.
1164
+ */
1165
+ if (!strcmp(rs->src, "HEAD")) {
1166
+ if (!detached_head &&
1167
+ !strcmp(head, superproject_head))
1168
+ break;
1169
+ die("HEAD does not match the named branch in the superproject");
1170
+ }
1171
+ default:
1172
die("src refspec '%s' must name a ref",
1173
rs->src);
1174
+ }
1175
}
1176
free_refspec(refspec_nr, refspec);
1177
}
1178
+ free(head);
1179
1180
return 0;
1181
}