772
static int is_index_unchanged(struct repository *r)
773
{
774
struct object_id head_oid, *cache_tree_oid;
775
+ const struct object_id *head_tree_oid;
776
struct commit *head_commit;
777
struct index_state *istate = r->index;
778
+ const char *head_name;
779
+
780
+ if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) {
781
+ /* Check to see if this is an unborn branch */
782
+ head_name = resolve_ref_unsafe("HEAD",
783
+ RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
784
+ &head_oid, NULL);
785
+ if (!head_name ||
786
+ !starts_with(head_name, "refs/heads/") ||
787
+ !is_null_oid(&head_oid))
788
+ return error(_("could not resolve HEAD commit"));
789
+ head_tree_oid = the_hash_algo->empty_tree;
790
+ } else {
791
+ head_commit = lookup_commit(r, &head_oid);
792
778
- if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
779
- return error(_("could not resolve HEAD commit"));
780
-
781
- head_commit = lookup_commit(r, &head_oid);
793
+ /*
794
+ * If head_commit is NULL, check_commit, called from
795
+ * lookup_commit, would have indicated that head_commit is not
796
+ * a commit object already. repo_parse_commit() will return failure
797
+ * without further complaints in such a case. Otherwise, if
798
+ * the commit is invalid, repo_parse_commit() will complain. So
799
+ * there is nothing for us to say here. Just return failure.
800
+ */
801
+ if (repo_parse_commit(r, head_commit))
802
+ return -1;
803
783
- /*
784
- * If head_commit is NULL, check_commit, called from
785
- * lookup_commit, would have indicated that head_commit is not
786
- * a commit object already. repo_parse_commit() will return failure
787
- * without further complaints in such a case. Otherwise, if
788
- * the commit is invalid, repo_parse_commit() will complain. So
789
- * there is nothing for us to say here. Just return failure.
790
- */
791
- if (repo_parse_commit(r, head_commit))
792
- return -1;
804
+ head_tree_oid = get_commit_tree_oid(head_commit);
805
+ }
806
807
if (!(cache_tree_oid = get_cache_tree_oid(istate)))
808
return -1;
809
797
- return oideq(cache_tree_oid, get_commit_tree_oid(head_commit));
810
+ return oideq(cache_tree_oid, head_tree_oid);
811
}
812
813
static int write_author_script(const char *message)