git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shourya Shukla <shouryashukla.oo@gmail.com>
To: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
Cc: gitster@pobox.com, git@vger.kernel.org,
	christian.couder@gmail.com, johannes.schindelin@gmx.de,
	liu.denton@gmail.com
Subject: Re: [GSoC][PATCH] submodule: port submodule subcommand 'add' from shell to C
Date: Mon, 31 Aug 2020 18:34:48 +0530	[thread overview]
Message-ID: <20200831130448.GA119147@konoha> (raw)
In-Reply-To: <ce151a1408291bb0991ce89459e36ee13ccdfa52.camel@gmail.com>

On 31/08 01:28, Kaartic Sivaraam wrote:
> On Wed, 2020-08-26 at 14:45 +0530, Shourya Shukla wrote:
> > On 24/08 11:35, Junio C Hamano wrote:
> > > Shourya Shukla <shouryashukla.oo@gmail.com> writes:
> > > 
> > > The shell version would error out with anything in the index, so I'd
> > > expect that a faithful conversion would not call is_directory() nor
> > > submodule_from_path() at all---it would just look path up in the_index
> > > and complains if anything is found.  For example, the quoted part in
> > > the original above is what gives the error message when I do
> > > 
> > > 	$ git submodule add ./Makefile
> > > 	'Makefile' already exists in the index.
> > > 
> > > I think.  And the above code won't trigger the "already exists" at
> > > all because 'path' is not a directory.
> > 
> > Alright. That is correct. I tried to use a multitude of functions but
> > did not find luck with any of them. The functions I tried:
> > 
> 
> It would've been nice to see the actual code you tried so that it's
> easier for others to more easily identify if you're using the wrong
> function or using the correct function in the wrong way.

Yeah, that is my fault. I will tag along below.

> >     - index_path() to check if the path is in the index. For some
> >       reason, it switched to the 'default' case and return the
> >       'unsupported file type' error.
> > 
> >     - A combination of doing an OR with index_file_exists() and
> >       index_dir_exists(). Still no luck. t7406.43 fails.
> > 
> >     - Using index_name_pos() along with the above two functions. Again a
> >       failure in the same test.
> > 
> > I feel that index_name_pos() should suffice this task but it fails in
> > t7406.43. The SM is in index since 'git ls-files --error-unmatch s1'
> > does return 's1' (s1 is the submodule). What am I missing here?
> > 
> 
> You're likely missing the fact that you should call `read_cache` before
> using `index_name_pos` or the likes of it.

Alright, called it.

> For instance, the following works without issues for most cases (more
> on that below):
> 
>         if (read_cache() < 0)
>                 die(_("index file corrupt"));
> 
>         cache_pos = cache_name_pos(path, strlen(path));
>         if (cache_pos >= 0) {
>                 if (!force) {
>                         die(_("'%s' already exists in the index"),
> path);
>                 }
>                 else {
>                         struct cache_entry *ce = the_index.cache[cache_pos];
> 
>                         if (!S_ISGITLINK(ce->ce_mode))
>                                 die(_("'%s' already exists in the index and is not a "
>                                       "submodule"), path);
>                 }
>         }

I actually did this only using 'index_*()' functions. But made a very
very very silly mistake:
I did a sizeof() instead of strlen() and I did not notice this until
I saw what you did. IDK how I made this mistake.

This is what I have done finally:
---
	if (read_cache() < 0)
		die(_("index file corrupt"));

	if (!force) {
		if (cache_file_exists(path, strlen(path), ignore_case) ||
		    cache_dir_exists(path, strlen(path)))
			die(_("'%s' already exists in the index"), path);
	} else {
		int cache_pos = cache_name_pos(path, strlen(path));
		struct cache_entry *ce = the_index.cache[cache_pos];
		if (cache_pos >= 0 && !S_ISGITLINK(ce->ce_mode))
			die(_("'%s' already exists in the index and is not a "
			      "submodule"), path);
	}
---

I did not put the 'cache_pos >= 0' at the start since I thought that it
will unnecessarily increase an indentation level. Since we are using
'cache_{file,dir}_exists' in the first check and 'cache_name_pos()' in
the second, the placement of check at another indentation level would be
unnecessary. What do you think about this?

> This is more close to what the shell version did but misses one case
> which might or might not be covered by the test suite[1]. The case when
> path is a directory that has tracked contents. In the shell version we
> would get:
> 
>    $ git submodule add ../git-crypt/ builtin
>    'builtin' already exists in the index
>    $ git submodule add --force ../git-crypt/ builtin
>    'builtin' already exists in the index and is not a submodule
> 
>    In the C version with the above snippet we get:
> 
>    $ git submodule add --force ../git-crypt/ builtin
>    fatal: 'builtin' does not have a commit checked out
>    $ git submodule add ../git-crypt/ builtin
>    fatal: 'builtin' does not have a commit checked out
> 
>    That's not appropriate and should be fixed. I believe we could do
>    something with `cache_dir_exists` to fix this.
> 
> 
>    Footnote
>    ===
> 
>    [1]: If it's not covered already, it might be a good idea to add a test
>    for the above case.

Like Junio said, we do not care if it is a file or a directory of any
sorts, we will give the error if it already exists. Therefore, even if
it is an untracked or a tracked one, it should not matter to us. Hence
testing for it may not be necessary is what I feel. Why should we test
it?


  reply	other threads:[~2020-08-31 13:12 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-24  9:03 Shourya Shukla
2020-08-24 18:35 ` Junio C Hamano
2020-08-24 20:30   ` Kaartic Sivaraam
2020-08-24 20:46     ` Junio C Hamano
2020-08-26  9:27     ` Shourya Shukla
2020-08-26 10:54       ` Kaartic Sivaraam
2020-08-26  9:15   ` Shourya Shukla
2020-08-30 19:58     ` Kaartic Sivaraam
2020-08-31 13:04       ` Shourya Shukla [this message]
2020-09-01 20:35         ` Kaartic Sivaraam
2020-09-02 12:04           ` Shourya Shukla
2020-09-03  8:46             ` Kaartic Sivaraam

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=20200831130448.GA119147@konoha \
    --to=shouryashukla.oo@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=kaartic.sivaraam@gmail.com \
    --cc=liu.denton@gmail.com \
    --subject='Re: [GSoC][PATCH] submodule: port submodule subcommand '\''add'\'' from shell to C' \
    /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

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).