]> git.ipfire.org Git - thirdparty/git.git/blobdiff - submodule.c
Merge branch 'cb/notes-freeing-always-null-fix'
[thirdparty/git.git] / submodule.c
index a2701ede4a1135fae736705e92882885d4bdce47..6415cc55807c7ed9ee4cbcaa57f41a5804b6e854 100644 (file)
@@ -51,6 +51,24 @@ int is_gitmodules_unmerged(const struct index_state *istate)
        return 0;
 }
 
+/*
+ * Check if the .gitmodules file is safe to write.
+ *
+ * Writing to the .gitmodules file requires that the file exists in the
+ * working tree or, if it doesn't, that a brand new .gitmodules file is going
+ * to be created (i.e. it's neither in the index nor in the current branch).
+ *
+ * It is not safe to write to .gitmodules if it's not in the working tree but
+ * it is in the index or in the current branch, because writing new values
+ * (and staging them) would blindly overwrite ALL the old content.
+ */
+int is_writing_gitmodules_ok(void)
+{
+       struct object_id oid;
+       return file_exists(GITMODULES_FILE) ||
+               (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
+}
+
 /*
  * Check if the .gitmodules file has unstaged modifications.  This must be
  * checked before allowing modifications to the .gitmodules file with the
@@ -89,6 +107,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
 {
        struct strbuf entry = STRBUF_INIT;
        const struct submodule *submodule;
+       int ret;
 
        if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
                return -1;
@@ -104,14 +123,9 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
        strbuf_addstr(&entry, "submodule.");
        strbuf_addstr(&entry, submodule->name);
        strbuf_addstr(&entry, ".path");
-       if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
-               /* Maybe the user already did that, don't error out here */
-               warning(_("Could not update .gitmodules entry %s"), entry.buf);
-               strbuf_release(&entry);
-               return -1;
-       }
+       ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
        strbuf_release(&entry);
-       return 0;
+       return ret;
 }
 
 /*