From: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
To: git@vger.kernel.org
Subject: [RFC PATCH 5/6] sha1-name: Teach `get_oid_with_context[_1]()` to be gentle
Date: Tue, 17 Jul 2018 15:06:35 +0300 [thread overview]
Message-ID: <dc1abfca46cd536f060b9097b9f2d1a64988f932.1531778417.git.ungureanupaulsebastian@gmail.com> (raw)
In-Reply-To: <cover.1531778417.git.ungureanupaulsebastian@gmail.com>
This commit makes `get_oid_with_context()` and `get_oid_with_context_1()`
to recognize the `GET_OID_GENTLY` flag.
The `gentle` flag does not imply `quiet` and we might need to reconsider
whether we should display any message if `GET_OID_GENTLY` is given.
Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com>
---
sha1-name.c | 103 ++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 83 insertions(+), 20 deletions(-)
diff --git a/sha1-name.c b/sha1-name.c
index 74ecbd550..a5d4e0dc7 100644
--- a/sha1-name.c
+++ b/sha1-name.c
@@ -1521,11 +1521,12 @@ int get_oid_blob(const char *name, struct object_id *oid)
}
/* Must be called only when object_name:filename doesn't exist. */
-static void diagnose_invalid_oid_path(const char *prefix,
+static int diagnose_invalid_oid_path(const char *prefix,
const char *filename,
const struct object_id *tree_oid,
const char *object_name,
- int object_name_len)
+ int object_name_len,
+ int gentle)
{
struct object_id oid;
unsigned mode;
@@ -1533,12 +1534,19 @@ static void diagnose_invalid_oid_path(const char *prefix,
if (!prefix)
prefix = "";
- if (file_exists(filename))
+ if (file_exists(filename)) {
+ if (gentle)
+ return -1;
die("Path '%s' exists on disk, but not in '%.*s'.",
filename, object_name_len, object_name);
+ }
if (is_missing_file_error(errno)) {
char *fullname = xstrfmt("%s%s", prefix, filename);
-
+ if (gentle) {
+ warning(_("%s or %s does not exist."), fullname,
+ filename);
+ return -1;
+ }
if (!get_tree_entry(tree_oid, fullname, &oid, &mode)) {
die("Path '%s' exists, but not '%s'.\n"
"Did you mean '%.*s:%s' aka '%.*s:./%s'?",
@@ -1552,12 +1560,14 @@ static void diagnose_invalid_oid_path(const char *prefix,
die("Path '%s' does not exist in '%.*s'",
filename, object_name_len, object_name);
}
+ return 0;
}
/* Must be called only when :stage:filename doesn't exist. */
-static void diagnose_invalid_index_path(int stage,
+static int diagnose_invalid_index_path(int stage,
const char *prefix,
- const char *filename)
+ const char *filename,
+ int gentle)
{
const struct cache_entry *ce;
int pos;
@@ -1574,11 +1584,20 @@ static void diagnose_invalid_index_path(int stage,
if (pos < active_nr) {
ce = active_cache[pos];
if (ce_namelen(ce) == namelen &&
- !memcmp(ce->name, filename, namelen))
+ !memcmp(ce->name, filename, namelen)) {
+ if (gentle) {
+ warning("Path '%s' is in the index "
+ "but not at stage %d.\n"
+ "Did you mean ':%d:%s'?",
+ filename, stage,
+ ce_stage(ce), filename);
+ return -1;
+ }
die("Path '%s' is in the index, but not at stage %d.\n"
"Did you mean ':%d:%s'?",
filename, stage,
ce_stage(ce), filename);
+ }
}
/* Confusion between relative and absolute filenames? */
@@ -1590,31 +1609,58 @@ static void diagnose_invalid_index_path(int stage,
if (pos < active_nr) {
ce = active_cache[pos];
if (ce_namelen(ce) == fullname.len &&
- !memcmp(ce->name, fullname.buf, fullname.len))
+ !memcmp(ce->name, fullname.buf, fullname.len)) {
+ if (gentle)
+ return -1;
die("Path '%s' is in the index, but not '%s'.\n"
"Did you mean ':%d:%s' aka ':%d:./%s'?",
fullname.buf, filename,
ce_stage(ce), fullname.buf,
ce_stage(ce), filename);
+ }
}
- if (file_exists(filename))
+ if (file_exists(filename)) {
+ if (gentle)
+ return -1;
die("Path '%s' exists on disk, but not in the index.", filename);
- if (is_missing_file_error(errno))
+ }
+ if (is_missing_file_error(errno)) {
+ if (gentle)
+ return -1;
die("Path '%s' does not exist (neither on disk nor in the index).",
filename);
+ }
strbuf_release(&fullname);
+ return 0;
}
+static const char *resolve_error = "dummy";
-static char *resolve_relative_path(const char *rel)
+static char *resolve_relative_path_gently(const char *rel, int gentle)
{
if (!starts_with(rel, "./") && !starts_with(rel, "../"))
return NULL;
- if (!is_inside_work_tree())
+ if (!is_inside_work_tree()) {
+ /*
+ * `resolve_error` is a dummy variable and it is used to verify
+ * if there was any problem inside this function. This is
+ * returned only in the case we want to perform gently,
+ * otherwise, `exit()` or `die()` can be called.
+ */
+ if (gentle)
+ return (char*) resolve_error;
die("relative path syntax can't be used outside working tree.");
+ }
+
+ if (gentle) {
+ return prefix_path_gently(startup_info->prefix,
+ startup_info->prefix ?
+ strlen(startup_info->prefix) : 0,
+ NULL, rel);
+ }
/* die() inside prefix_path() if resolved path is outside worktree */
return prefix_path(startup_info->prefix,
@@ -1669,7 +1715,16 @@ static int get_oid_with_context_1(const char *name,
stage = name[1] - '0';
cp = name + 3;
}
- new_path = resolve_relative_path(cp);
+ /*
+ * Note that `resolve_relative_path_gently()` may die if
+ * the second parameter is zero. If it is a non-zero value,
+ * the function will return `resolve_error` on failure. This
+ * dummy variable is defined as a `static const char *`.
+ */
+ new_path = resolve_relative_path_gently(cp, flags & GET_OID_GENTLY);
+ if (new_path == resolve_error)
+ return -1;
+
if (!new_path) {
namelen = namelen - (cp - name);
} else {
@@ -1698,8 +1753,11 @@ static int get_oid_with_context_1(const char *name,
}
pos++;
}
- if (only_to_die && name[1] && name[1] != '/')
- diagnose_invalid_index_path(stage, prefix, cp);
+ if (only_to_die && name[1] && name[1] != '/' &&
+ diagnose_invalid_index_path(stage, prefix, cp,
+ flags & GET_OID_GENTLY))
+ return -1;
+
free(new_path);
return -1;
}
@@ -1723,7 +1781,10 @@ static int get_oid_with_context_1(const char *name,
const char *filename = cp+1;
char *new_filename = NULL;
- new_filename = resolve_relative_path(filename);
+ new_filename = resolve_relative_path_gently(filename,
+ flags & GET_OID_GENTLY);
+ if (new_filename == resolve_error)
+ return -1;
if (new_filename)
filename = new_filename;
if (flags & GET_OID_FOLLOW_SYMLINKS) {
@@ -1731,13 +1792,14 @@ static int get_oid_with_context_1(const char *name,
filename, oid, &oc->symlink_path,
&oc->mode, flags);
} else {
- ret = get_tree_entry(&tree_oid, filename, oid,
- &oc->mode);
+ ret = get_tree_entry_gently(&tree_oid, filename,
+ oid, &oc->mode,
+ flags & GET_OID_GENTLY);
if (ret && only_to_die) {
diagnose_invalid_oid_path(prefix,
filename,
&tree_oid,
- name, len);
+ name, len, 0);
}
}
if (flags & GET_OID_RECORD_PATH)
@@ -1769,7 +1831,8 @@ void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
int get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc)
{
- if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
+ if (flags & (GET_OID_FOLLOW_SYMLINKS | GET_OID_GENTLY) &&
+ flags & GET_OID_ONLY_TO_DIE)
BUG("incompatible flags for get_sha1_with_context");
return get_oid_with_context_1(str, flags, NULL, oid, oc);
}
--
2.18.0.rc2.184.ga79db55c2.dirty
next prev parent reply other threads:[~2018-07-17 12:06 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-17 12:06 [RFC PATCH 0/6] Add gentle alternative for `get_oid()` Paul-Sebastian Ungureanu
2018-07-17 12:06 ` [RFC PATCH 1/6] sha1-name: Add `GET_OID_GENTLY` flag Paul-Sebastian Ungureanu
2018-07-17 12:06 ` [RFC PATCH 2/6] tree-walk: Add three new gentle helpers Paul-Sebastian Ungureanu
2018-07-17 18:55 ` Junio C Hamano
2018-07-18 23:11 ` Paul-Sebastian Ungureanu
2018-07-17 12:06 ` [RFC PATCH 3/6] refs.c: Teach `read_ref_at()` to accept `GET_OID_GENTLY` flag Paul-Sebastian Ungureanu
2018-07-17 12:06 ` [RFC PATCH 4/6] sha1-name: Teach `get_oid_basic()` to be gentle Paul-Sebastian Ungureanu
2018-07-17 12:06 ` Paul-Sebastian Ungureanu [this message]
2018-07-17 19:13 ` [RFC PATCH 5/6] sha1-name: Teach `get_oid_with_context[_1]()` " Junio C Hamano
2018-07-18 23:14 ` Paul-Sebastian Ungureanu
2018-07-17 12:06 ` [RFC PATCH 6/6] sha1-name: Add gentle alternative for `get_oid()` Paul-Sebastian Ungureanu
2018-07-17 17:45 ` [RFC PATCH 0/6] " Duy Nguyen
2018-07-18 23:03 ` Paul-Sebastian Ungureanu
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=dc1abfca46cd536f060b9097b9f2d1a64988f932.1531778417.git.ungureanupaulsebastian@gmail.com \
--to=ungureanupaulsebastian@gmail.com \
--cc=git@vger.kernel.org \
/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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
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 read-only IMAP folder(s) and NNTP newsgroup(s).