All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Beller <sbeller@google.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, Jens.Lehmann@web.de, gmane@otterhall.com,
	Stefan Beller <sbeller@google.com>
Subject: [PATCH 1/2] mv submodule: respect ignore_errors for errors in submodule code
Date: Mon, 18 Apr 2016 16:41:23 -0700	[thread overview]
Message-ID: <1461022884-30819-2-git-send-email-sbeller@google.com> (raw)
In-Reply-To: <1461022884-30819-1-git-send-email-sbeller@google.com>

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 <sbeller@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

  reply	other threads:[~2016-04-18 23:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-18 23:41 [PATCH 0/2] WAS: [PATCH] mv: allow moving nested submodules Stefan Beller
2016-04-18 23:41 ` Stefan Beller [this message]
2016-04-18 23:41 ` [PATCH 2/2] " Stefan Beller
2016-04-19  7:13   ` Jacob Keller
2016-04-19  0:01 ` [PATCH 0/2] WAS: [PATCH] " Junio C Hamano
2016-04-19 16:56   ` Stefan Beller
2016-04-19 18:15     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1461022884-30819-2-git-send-email-sbeller@google.com \
    --to=sbeller@google.com \
    --cc=Jens.Lehmann@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=gmane@otterhall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.