From: Jonathan Nieder <jrnieder@gmail.com>
To: Stefan Beller <sbeller@google.com>
Cc: Junio C Hamano <gitster@pobox.com>,
git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>,
Jeff King <peff@peff.net>
Subject: [PATCH 06/14] lockfile: introduce flag for locks outside .git
Date: Tue, 2 Dec 2014 21:19:12 -0800 [thread overview]
Message-ID: <20141203051911.GP6527@google.com> (raw)
In-Reply-To: <20141203050217.GJ6527@google.com>
When the lockfile API was introduced, it was only used for the index
file and error messages like
fatal: Unable to create '/path/to/foo.lock': File exists.
If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.
were appropriate advice for that case.
Nowadays, the lockfile API is used for other files that need similar
lock-then-update semantics, including files not associated to any
repository, such as /etc/gitconfig, ../my.bundle, and /tmp/temp.marks.
Add a flag that makes the message stop referring to "this repository"
for such cases.
This should make it possible to print nicer error messages from
such non-core users of the lockfile API.
This introduces the flag. Later patches will use it.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Documentation/technical/api-lockfile.txt | 12 ++++++++++++
builtin/update-index.c | 2 +-
lockfile.c | 26 +++++++++++++++++---------
lockfile.h | 5 +++--
refs.c | 4 ++--
5 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index 93b5f23..fa60cfd 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -124,6 +124,18 @@ LOCK_NO_DEREF::
for backwards-compatibility reasons can be a symbolic link
containing the name of the referred-to-reference.
+LOCK_OUTSIDE_REPOSITORY:
+
+ When the ".lock" file being created already exists, the error
+ message usually explains that another git process must have
+ crashed in the same Git repository. If `LOCK_OUTSIDE_REPOSITORY`
+ is set, then the message is replaced with something more
+ appropriate when updating files that are not inside a
+ repository.
++
+For example, this flag should be set when locking a configuration
+file in the user's home directory.
+
LOCK_DIE_ON_ERROR::
If a lock is already taken for the file, `die()` with an error
diff --git a/builtin/update-index.c b/builtin/update-index.c
index b0e3dc9..56abd18 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -943,7 +943,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
if (newfd < 0) {
if (refresh_args.flags & REFRESH_QUIET)
exit(128);
- unable_to_lock_die(get_index_file(), lock_error);
+ unable_to_lock_die(get_index_file(), 0, lock_error);
}
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
die("Unable to write new index file");
diff --git a/lockfile.c b/lockfile.c
index 8685c68..102584f 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -149,24 +149,32 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
return lk->fd;
}
-void unable_to_lock_message(const char *path, int err, struct strbuf *buf)
+void unable_to_lock_message(const char *path, int flags, int err,
+ struct strbuf *buf)
{
- if (err == EEXIST) {
+ if (err != EEXIST) {
+ strbuf_addf(buf, "Unable to create '%s.lock': %s",
+ absolute_path(path), strerror(err));
+ } else if (flags & LOCK_OUTSIDE_REPOSITORY) {
+ strbuf_addf(buf, "Unable to create '%s.lock': %s.\n\n"
+ "If no other git process is currently running, this probably means\n"
+ "another git process crashed earlier. Make sure no other git process\n"
+ "is running and remove the file manually to continue.",
+ absolute_path(path), strerror(err));
+ } else {
strbuf_addf(buf, "Unable to create '%s.lock': %s.\n\n"
"If no other git process is currently running, this probably means a\n"
"git process crashed in this repository earlier. Make sure no other git\n"
"process is running and remove the file manually to continue.",
absolute_path(path), strerror(err));
- } else
- strbuf_addf(buf, "Unable to create '%s.lock': %s",
- absolute_path(path), strerror(err));
+ }
}
-NORETURN void unable_to_lock_die(const char *path, int err)
+NORETURN void unable_to_lock_die(const char *path, int flags, int err)
{
struct strbuf buf = STRBUF_INIT;
- unable_to_lock_message(path, err, &buf);
+ unable_to_lock_message(path, flags, err, &buf);
die("%s", buf.buf);
}
@@ -175,7 +183,7 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
{
int fd = lock_file(lk, path, flags);
if (fd < 0 && (flags & LOCK_DIE_ON_ERROR))
- unable_to_lock_die(path, errno);
+ unable_to_lock_die(path, flags, errno);
return fd;
}
@@ -189,7 +197,7 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path,
fd = lock_file(lk, path, flags);
if (fd < 0) {
- unable_to_lock_message(path, errno, err);
+ unable_to_lock_message(path, flags, errno, err);
return fd;
}
diff --git a/lockfile.h b/lockfile.h
index ca36a1d..779a992 100644
--- a/lockfile.h
+++ b/lockfile.h
@@ -70,10 +70,11 @@ struct lock_file {
#define LOCK_DIE_ON_ERROR 1
#define LOCK_NO_DEREF 2
+#define LOCK_OUTSIDE_REPOSITORY 4
-extern void unable_to_lock_message(const char *path, int err,
+extern void unable_to_lock_message(const char *path, int, int err,
struct strbuf *buf);
-extern NORETURN void unable_to_lock_die(const char *path, int err);
+extern NORETURN void unable_to_lock_die(const char *path, int, int err);
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
extern int hold_lock_file_for_append(struct lock_file *, const char *path,
int, struct strbuf *err);
diff --git a/refs.c b/refs.c
index 917f8fc..39e43cf 100644
--- a/refs.c
+++ b/refs.c
@@ -2326,7 +2326,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
*/
goto retry;
else
- unable_to_lock_die(ref_file, errno);
+ unable_to_lock_die(ref_file, lflags, errno);
}
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
@@ -2376,7 +2376,7 @@ int lock_packed_refs(struct strbuf *err)
struct packed_ref_cache *packed_ref_cache;
if (hold_lock_file_for_update(&packlock, git_path("packed-refs"), 0) < 0) {
- unable_to_lock_message(git_path("packed-refs"), errno, err);
+ unable_to_lock_message(git_path("packed-refs"), 0, errno, err);
return -1;
}
next prev parent reply other threads:[~2014-12-03 5:19 UTC|newest]
Thread overview: 83+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-17 22:14 [PATCH] copy.c: make copy_fd preserve meaningful errno Stefan Beller
2014-11-17 23:08 ` Junio C Hamano
2014-11-17 23:13 ` Stefan Beller
2014-11-17 23:35 ` Jonathan Nieder
2014-11-18 0:18 ` Stefan Beller
2014-11-18 0:48 ` Jonathan Nieder
2014-11-18 1:01 ` Stefan Beller
2014-12-03 5:02 ` [PATCH 0/14] " Jonathan Nieder
2014-12-03 5:10 ` [PATCH 01/14] strbuf: introduce strbuf_prefixf() Jonathan Nieder
2014-12-03 20:10 ` Eric Sunshine
2014-12-03 20:14 ` Jonathan Nieder
2014-12-03 21:45 ` Junio C Hamano
2014-12-03 21:59 ` Jonathan Nieder
2014-12-03 22:09 ` Jonathan Nieder
2014-12-03 22:40 ` Junio C Hamano
2014-12-03 5:12 ` [PATCH 02/14] add_to_alternates_file: respect GIT_OBJECT_DIRECTORY Jonathan Nieder
2014-12-03 18:53 ` Junio C Hamano
2014-12-03 5:13 ` [PATCH 03/14] copy_fd: pass error message back through a strbuf Jonathan Nieder
2014-12-03 19:01 ` Junio C Hamano
2014-12-03 20:28 ` Jonathan Nieder
2014-12-03 21:00 ` Jeff King
2014-12-03 21:38 ` Jonathan Nieder
2014-12-04 7:59 ` Jeff King
2014-12-04 8:36 ` Stefan Beller
2014-12-04 9:04 ` Jeff King
2014-12-10 17:02 ` Michael Haggerty
2014-12-10 19:00 ` Junio C Hamano
2014-12-10 19:14 ` Jeff King
2014-12-04 3:01 ` [PATCH/RFC] doc: document error handling functions and conventions (Re: [PATCH 03/14] copy_fd: pass error message back through a strbuf) Jonathan Nieder
2014-12-04 23:27 ` Junio C Hamano
2014-12-04 23:37 ` Jonathan Nieder
2014-12-04 23:41 ` Jonathan Nieder
2014-12-04 23:44 ` Jeff King
2014-12-04 23:52 ` Junio C Hamano
2014-12-05 0:01 ` Jeff King
2014-12-05 18:00 ` Junio C Hamano
2014-12-07 10:07 ` Jeff King
2014-12-09 18:43 ` Junio C Hamano
2014-12-09 18:49 ` Jeff King
2015-02-12 23:08 ` Junio C Hamano
2015-02-17 15:50 ` Michael Haggerty
2015-02-17 16:03 ` Junio C Hamano
2015-02-17 16:05 ` Jeff King
2015-02-17 22:46 ` Junio C Hamano
2014-12-03 20:02 ` [PATCH 03/14] copy_fd: pass error message back through a strbuf Junio C Hamano
2014-12-03 20:18 ` Jonathan Nieder
2014-12-03 21:43 ` Junio C Hamano
2014-12-03 21:51 ` Jonathan Nieder
2014-12-03 20:20 ` Stefan Beller
2014-12-03 5:14 ` [PATCH 04/14] hold_lock_file_for_append: " Jonathan Nieder
2014-12-03 6:09 ` Torsten Bögershausen
2014-12-03 7:04 ` Jonathan Nieder
2014-12-03 5:16 ` [PATCH 05/14] lock_packed_refs: " Jonathan Nieder
2014-12-03 5:19 ` Jonathan Nieder [this message]
2014-12-03 23:13 ` [PATCH 06/14] lockfile: introduce flag for locks outside .git Junio C Hamano
2014-12-03 23:24 ` Jonathan Nieder
2014-12-03 23:25 ` Junio C Hamano
2014-12-03 23:29 ` Jonathan Nieder
2014-12-03 23:38 ` Junio C Hamano
2014-12-03 23:41 ` Jonathan Nieder
2014-12-03 23:43 ` Junio C Hamano
2014-12-03 23:57 ` Jeff King
2014-12-04 5:51 ` Junio C Hamano
2014-12-04 17:56 ` Jonathan Nieder
2014-12-03 5:19 ` [PATCH 07/14] fast-import: use message from lockfile API when writing marks fails Jonathan Nieder
2014-12-03 5:20 ` [PATCH 08/14] credentials: use message from lockfile API when locking ~/.git-credentials fails Jonathan Nieder
2014-12-03 5:21 ` [PATCH 09/14] config: use message from lockfile API when locking config file fails Jonathan Nieder
2014-12-03 19:59 ` Junio C Hamano
2014-12-03 20:16 ` Jonathan Nieder
2014-12-03 5:22 ` [PATCH 10/14] rerere: error out on autoupdate failure Jonathan Nieder
2014-12-03 5:25 ` [PATCH 11/14] hold_locked_index: pass error message back through a strbuf Jonathan Nieder
2014-12-03 5:26 ` [PATCH 12/14] hold_lock_file_for_update: " Jonathan Nieder
2014-12-03 18:53 ` Jonathan Nieder
2014-12-03 5:26 ` [PATCH 13/14] lockfile: remove unused function 'unable_to_lock_die' Jonathan Nieder
2014-12-03 5:27 ` [PATCH 14/14] lockfile: make 'unable_to_lock_message' private Jonathan Nieder
2014-12-03 20:42 ` Stefan Beller
2014-11-18 16:32 ` [PATCH] copy.c: make copy_fd preserve meaningful errno Junio C Hamano
2014-11-18 17:08 ` Junio C Hamano
2014-11-21 9:14 ` Michael Haggerty
2014-11-21 9:17 ` Michael Haggerty
2014-11-21 17:48 ` Junio C Hamano
2014-11-21 17:54 ` Jeff King
2014-11-21 18:31 ` 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=20141203051911.GP6527@google.com \
--to=jrnieder@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mhagger@alum.mit.edu \
--cc=peff@peff.net \
--cc=sbeller@google.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).