Changes 'git diff --no-index $directory $file' behaviour.
Now it is transformed to 'git diff --no-index $directory/&file $file'
instead of throwing an error.
Signed-off-by: Yurii Shevtsov <ungetch <at> gmail.com>
---
diff-no-index.c | 21 +++++++++++++++++++--
1 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/diff-no-index.c b/diff-no-index.c
index 265709b..4e71b36 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -97,8 +97,25 @@ static int queue_diff(struct diff_options *o,
if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
return -1;
- if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
- return error("file/directory conflict: %s, %s", name1, name2);
+ if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) {
+ struct strbuf dirnfile;
+ const char *dir, *file;
+ char *filename;
+ int ret = 0;
+
+ dir = S_ISDIR(mode1) ? name1 : name2;
+ file = (dir == name1) ? name2 : name1;
+ strbuf_init(&dirnfile, strlen(name1) + strlen(name2) + 2);
+ strbuf_addstr(&dirnfile, dir);
+ if (dirnfile.buf[dirnfile.len - 1] != '/')
+ strbuf_addch(&dirnfile, '/');
+ filename = strrchr(file, '/');
+ strbuf_addstr(&dirnfile, filename ? (filename + 1) : file);
+ ret = queue_diff(o, dirnfile.buf, file);
+ strbuf_release(&dirnfile);
+
+ return ret;
+ }
if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
struct strbuf buffer1 = STRBUF_INIT;
--
I hope I understood task correct. I think this patch requires writing
additional tests, so that's what I'm going to do now.
Hi,
On 03/15, Yurii Shevtsov wrote:
Changes 'git diff --no-index $directory $file' behaviour.
Now it is transformed to 'git diff --no-index $directory/&file $file'
instead of throwing an error.
The commit message should describe why the change is made, see
Documentation/SubmittingPatches, section (2)
Signed-off-by: Yurii Shevtsov <ungetch <at> gmail.com>
Please use your full email here, without replacing @ with <at>
---
diff-no-index.c | 21 +++++++++++++++++++--
You should probably add a test for the new behaviour in
t/t4053-diff-no-index.sh.
quoted hunk
1 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/diff-no-index.c b/diff-no-index.c
index 265709b..4e71b36 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -97,8 +97,25 @@ static int queue_diff(struct diff_options *o,
if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
return -1;
- if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
- return error("file/directory conflict: %s, %s", name1, name2);
+ if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) {
+ struct strbuf dirnfile;
+ const char *dir, *file;
+ char *filename;
+ int ret = 0;
+
+ dir = S_ISDIR(mode1) ? name1 : name2;
+ file = (dir == name1) ? name2 : name1;
This makes git diff --no-index $directory $file the same as
git diff --no-index $file $directory. Shouldn't these commands give
different results? (See the behaviour of diff in this case, and
compare it to the behaviour you introduced here)
+ strbuf_init(&dirnfile, strlen(name1) + strlen(name2) + 2);
+ strbuf_addstr(&dirnfile, dir);
+ if (dirnfile.buf[dirnfile.len - 1] != '/')
+ strbuf_addch(&dirnfile, '/');
+ filename = strrchr(file, '/');
+ strbuf_addstr(&dirnfile, filename ? (filename + 1) : file);
+ ret = queue_diff(o, dirnfile.buf, file);
+ strbuf_release(&dirnfile);
+
+ return ret;
+ }
Your MUA seems to have replaced tabs with spaces in this email. The
easiest way to get the formatting correct is the git send-email tool.
You should also try to sending the mail to yourself first and see if
it applies correctly with git am.
if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
struct strbuf buffer1 = STRBUF_INIT;
--
I hope I understood task correct. I think this patch requires writing
additional tests, so that's what I'm going to do now.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
In addition to the points raised by Matthieu and Thomas...
On Sun, Mar 15, 2015 at 11:35 AM, Yurii Shevtsov [off-list ref] wrote:
make "git diff --no-index $directory $file" DWIM better.
Specify the area affected by the change, followed by a colon, followed
by the change summary. Drop the period at the end. So, something like:
diff: improve '--no-index <directory> <file>' DWIMing
Changes 'git diff --no-index $directory $file' behaviour.
Now it is transformed to 'git diff --no-index $directory/&file $file'
instead of throwing an error.
Write in imperative mood, so "Change" rather than "Changes". By
itself, the first sentence isn't saying much; it would read better if
you combined the two sentences into one.
quoted hunk
Signed-off-by: Yurii Shevtsov <ungetch <at> gmail.com>
---
diff --git a/diff-no-index.c b/diff-no-index.c
index 265709b..4e71b36 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -97,8 +97,25 @@ static int queue_diff(struct diff_options *o,
if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
return -1;
- if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
- return error("file/directory conflict: %s, %s", name1, name2);
+ if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) {
+ struct strbuf dirnfile;
Is this name supposed to stand for "dir'n'file", a shorthand for
"dir-and-file"? Perhaps a simpler more idiomatic name such as "path"
would suffice. Also, you can initialize the strbuf here at point of
declaration:
struct strbuf path = STRBUF_INIT;
+ const char *dir, *file;
+ char *filename;
+ int ret = 0;
+
+ dir = S_ISDIR(mode1) ? name1 : name2;
+ file = (dir == name1) ? name2 : name1;
+ strbuf_init(&dirnfile, strlen(name1) + strlen(name2) + 2);
Unless this is a performance critical loop where you want to avoid
multiple re-allocations, it's customary to pass 0 for the second
argument. Doing so makes the code a bit easier to read and understand,
and avoids questions like this one: Why are you adding 2 to the
requested length? I presume that you're taking the "/" and NUL into
account, however, strbuf takes NUL into account on its own as part of
its contract, so you probably wanted to add only 1.
+ strbuf_addstr(&dirnfile, dir);
+ if (dirnfile.buf[dirnfile.len - 1] != '/')
I don't know how others feel about it, but it makes me a bit
uncomfortable to see potential access of array item -1. I suppose, in
this case, neither name will be zero-length, however, I'd still feel
more comfortable seeing that documented formally, either via assert():
assert(dirnfile.len > 0);
if (dirnfile.buf[dirnfile.len - 1] != '/')
or:
if (dirnfile.len > 0 && dirnfile.buf[dirnfile.len - 1] != '/')
+ strbuf_addch(&dirnfile, '/');
+ filename = strrchr(file, '/');
+ strbuf_addstr(&dirnfile, filename ? (filename + 1) : file);
+ ret = queue_diff(o, dirnfile.buf, file);
+ strbuf_release(&dirnfile);
+
+ return ret;
+ }
if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
struct strbuf buffer1 = STRBUF_INIT;
--
I hope I understood task correct. I think this patch requires writing
additional tests, so that's what I'm going to do now.
--