git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [GSoC][PATCH] submodule: port submodule subcommand 'add' from shell to C

From
Shourya Shukla <shouryashukla.oo@gmail.com>
Date
Aug 26, 2020, 09:15 UTC
Message-ID
<20200826091502.GA29471@konoha>
In-Reply-To
<xmqq8se36gev.fsf@gitster.c.googlers.com>
On 24/08 11:35, Junio C Hamano wrote:
Show 38 quoted lines
> Shourya Shukla <shouryashukla.oo@gmail.com> writes:
> 
> > 	if test -z "$force"
> > 	then
> > 		git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
> > 		die "$(eval_gettext "'\$sm_path' already exists in the index")"
> > 	else
> > 		git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
> > 		die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
> > 	fi
> 
> Hmph.  So,
> 
>  - if we are not being 'force'd, we see if there is anything in the
>    index for the path and error out, whether it is a gitlink or not.
> 
>  - if there is 'force' option, we see what the given path is in the
>    index, and if it is already a gitlink, then die.  That sort of
>    makes sense, as long as the remainder of the code deals with the
>    path that is not a submodule in a sensible way.
> 
> > This is what I have done in C:
> >
> > 	if (!force) {
> > 		if (is_directory(path) && submodule_from_path(the_repository, &null_oid, path))
> > 			die(_("'%s' already exists in the index"), path);
> 
> 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:

    - 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?

Show 12 quoted lines
> > 	} else {
> > 		int err;
> > 		if (index_name_pos(&the_index, path, strlen(path)) >= 0 &&
> > 		    !is_submodule_populated_gently(path, &err))
> > 			die(_("'%s' already exists in the index and is not a "
> > 			      "submodule"), path);
>
> Likewise.  The above does much more than the original.
>
> The original was checking if the found cache entry has 160000 mode
> bit, so the second test would not be is_submodule_populated_gently()
> but more like !S_ISGITLINK(ce->ce_mode)

Using this results in failure of t7506.[33-40]. I implemented this in two ways:

    1. Use stat() to initialise the stat st corresponding to the 'path'.
       Then do a '!S_ISGITLINK(st.st_mode)'.
    2. Run a for loop:
		for (i = 0; i < active_nr; i++) {
		const struct cache_entry *ce = active_cache[i];
		if (index_name_pos(&the_index, path, strlen(path)) >= 0 &&
		    !S_ISGITLINK(ce->ce_mode))
			die(_("'%s' already exists in the index and is not a "
			      "submodule"), path);
        }
        Still the tests failed. What is meant by 'active_nr' BTW? I am
        not aware of this term.
Where am I going wrong for both the if-cases?
Previous: Kaartic SivaraamNext: Kaartic Sivaraam
Message 7 of 12 in “submodule: port submodule subcommand 'add' from shell to C”
  1. Shourya ShuklaAug 24, 2020
  2. Junio C HamanoAug 24, 2020
  3. Kaartic SivaraamAug 24, 2020
  4. Junio C HamanoAug 24, 2020
  5. Shourya ShuklaAug 26, 2020
  6. Kaartic SivaraamAug 26, 2020
  7. Shourya ShuklaAug 26, 2020
  8. Kaartic SivaraamAug 30, 2020
  9. Shourya ShuklaAug 31, 2020
  10. Kaartic SivaraamSep 1, 2020
  11. Shourya ShuklaSep 2, 2020
  12. Kaartic SivaraamSep 3, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.