The error handling via passing around a strbuf is well exercised in the
refs code, so apply that pattern here, too.

Signed-off-by: Stefan Beller <sbel...@google.com>
---
 builtin/mv.c | 15 ++++++++++++---
 submodule.c  | 33 ++++++++++++++++++++++++---------
 submodule.h  |  6 ++++--
 3 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/builtin/mv.c b/builtin/mv.c
index aeae855..74516f4 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -247,6 +247,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
        }
 
        for (i = 0; i < argc; i++) {
+               struct strbuf err = STRBUF_INIT;
                const char *src = source[i], *dst = destination[i];
                enum update_mode mode = modes[i];
                int pos;
@@ -256,9 +257,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                        if (rename(src, dst) < 0 && !ignore_errors)
                                die_errno(_("renaming '%s' failed"), src);
                        if (submodule_gitfile[i]) {
-                               if (submodule_gitfile[i] != 
SUBMODULE_WITH_GITDIR)
-                                       connect_work_tree_and_git_dir(dst, 
submodule_gitfile[i]);
-                               if (!update_path_in_gitmodules(src, dst))
+                               if ((submodule_gitfile[i] != 
SUBMODULE_WITH_GITDIR &&
+                                   connect_work_tree_and_git_dir(dst, 
submodule_gitfile[i], &err)) ||
+                                   update_path_in_gitmodules(src, dst, &err)) {
+                                       if (err.len) {
+                                               if (ignore_errors) {
+                                                       warning("%s", err.buf);
+                                                       continue;
+                                               } else
+                                                       die("%s", err.buf);
+                                       }
+                               } else
                                        gitmodules_modified = 1;
                        }
                }
diff --git a/submodule.c b/submodule.c
index 90825e1..ed18d34 100644
--- a/submodule.c
+++ b/submodule.c
@@ -51,7 +51,8 @@ int is_staging_gitmodules_ok(void)
  * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
  * with the correct path=<oldpath> setting was found and we could update it.
  */
-int update_path_in_gitmodules(const char *oldpath, const char *newpath)
+int update_path_in_gitmodules(const char *oldpath, const char *newpath,
+                             struct strbuf *err)
 {
        struct strbuf entry = STRBUF_INIT;
        const struct submodule *submodule;
@@ -59,8 +60,10 @@ int update_path_in_gitmodules(const char *oldpath, const 
char *newpath)
        if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
                return -1;
 
-       if (gitmodules_is_unmerged)
-               die(_("Cannot change unmerged .gitmodules, resolve merge 
conflicts first"));
+       if (gitmodules_is_unmerged) {
+               strbuf_addf(err, _("Cannot change unmerged .gitmodules, resolve 
merge conflicts first"));
+               return -1;
+       }
 
        submodule = submodule_from_path(null_sha1, oldpath);
        if (!submodule || !submodule->name) {
@@ -1102,27 +1105,39 @@ int merge_submodule(unsigned char result[20], const 
char *path,
 }
 
 /* Update gitfile and core.worktree setting to connect work tree and git dir */
-void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
+int connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir,
+                                 struct strbuf *err)
 {
+       int ret = 0;
        struct strbuf file_name = STRBUF_INIT;
        struct strbuf rel_path = STRBUF_INIT;
        const char *real_work_tree = xstrdup(real_path(work_tree));
 
        /* Update gitfile */
        strbuf_addf(&file_name, "%s/.git", work_tree);
-       write_file(file_name.buf, "gitdir: %s",
-                  relative_path(git_dir, real_work_tree, &rel_path));
+       if (write_file_gently(file_name.buf, "gitdir: %s",
+           relative_path(git_dir, real_work_tree, &rel_path))) {
+               strbuf_addf(err, _("could not write .git file (%s): %s"),
+                               file_name.buf, strerror(errno));
+               ret = -1;
+               goto out;
+       }
 
        /* Update core.worktree setting */
        strbuf_reset(&file_name);
        strbuf_addf(&file_name, "%s/config", git_dir);
-       git_config_set_in_file(file_name.buf, "core.worktree",
-                              relative_path(real_work_tree, git_dir,
-                                            &rel_path));
+       if (git_config_set_in_file_gently(file_name.buf, "core.worktree",
+           relative_path(real_work_tree, git_dir, &rel_path))) {
+               strbuf_addf(err, _("could not set core.worktree in %s"), 
file_name.buf);
+               ret = -1;
+               goto out;
+       }
 
+out:
        strbuf_release(&file_name);
        strbuf_release(&rel_path);
        free((void *)real_work_tree);
+       return ret;
 }
 
 int parallel_submodules(void)
diff --git a/submodule.h b/submodule.h
index 7ef3775..bf816e5 100644
--- a/submodule.h
+++ b/submodule.h
@@ -30,7 +30,8 @@ struct submodule_update_strategy {
 #define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL}
 
 int is_staging_gitmodules_ok(void);
-int update_path_in_gitmodules(const char *oldpath, const char *newpath);
+int update_path_in_gitmodules(const char *oldpath, const char *newpath,
+                             struct strbuf *err);
 int remove_path_from_gitmodules(const char *path);
 void stage_updated_gitmodules(void);
 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
@@ -58,7 +59,8 @@ int merge_submodule(unsigned char result[20], const char 
*path, const unsigned c
 int find_unpushed_submodules(unsigned char new_sha1[20], const char 
*remotes_name,
                struct string_list *needs_pushing);
 int push_unpushed_submodules(unsigned char new_sha1[20], const char 
*remotes_name);
-void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
+int connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir,
+                                 struct strbuf *err);
 int parallel_submodules(void);
 
 #endif
-- 
2.8.1.211.g24879d1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to