{"thread":{"id":"52765","subject":"Conversion of 'git submodule' to C: need some help","startedAt":"2020-02-09T11:14:00Z","lastAt":"2020-02-13T13:34:09Z","messageCount":2,"participants":["Shourya Shukla","Johannes Schindelin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"391389","messageId":"20200209111349.20472-1-shouryashukla.oo@gmail.com","threadId":"52765","inReplyTo":null,"subject":"Conversion of 'git submodule' to C: need some help","fromName":"Shourya Shukla","fromEmail":"shouryashukla.oo@gmail.com","sentAt":"2020-02-09T11:13:49Z","receivedAt":"2020-02-09T11:14:00Z","isPatch":false,"sender":{"key":"shouryashukla.oo@gmail.com","avatar":"https://avatars.githubusercontent.com/u/43680618?v=4"},"body":"Hello everyone,\n\nI was trying to understand the code of 'git submodule'[1]. This is\nalso in reference to this conversation I had before[2].\n\nI read the code and stumbled across a function with a 'TODO' tag[3].\nHere we want to change the aforementioned function into a 'repo_submodule_init'\nfunction I suppose.\n\nI am facing some problems and would love some insight on them:\n\t\n\t1. What exactly are we aiming in [3]? To replace the function completely\n\t   or to just add some 'repo_submodule_init' functionality?\n\n\t2. Something I inferred was that functions with names of the pattern 'strbuf_git_*'\n\t   are trying to 'create a path'(are they physically creating the path or just\n\t   instructing git about them?) while functions of the pattern 'git_*' are trying\n\t   to check some conditions denoted by their function names(for instance\n\t   'git_config_rename_section_in_file')? Is this inference correct to some extent?\n\n\t3. How does one check which all parts of a command have been completed? Is it checked\n\t   by looking at the file history or by comparing with the shell script of the command\n\t   or are there any other means?\n\t\n\t4. Is it fine if I am not able to understand the purpose of certain functions right now(such as\n\t   'add_submodule_odb')? I am able to get a rough idea of what the functions are doing but I am\n\t   not able to decode certain functions line-by-line.\n\nCurrently, I am studying in depth about 'git objects' and the submodule command on the git Documentation.\nWhat else do would you advise me to strengthen my understanding of the code and git in general?\n\nRegards,\nShourya Shukla\n\n[1]: https://github.com/periperidip/git/blob/v2.25.0/submodule.c\n[2]: https://lore.kernel.org/git/20200201173841.13760-1-shouryashukla.oo@gmail.com/\n[3]: https://github.com/periperidip/git/blob/v2.25.0/submodule.c#L168\n\n"},{"id":"391660","messageId":"nycvar.QRO.7.76.6.2002131412300.46@tvgsbejvaqbjf.bet","threadId":"52765","inReplyTo":"20200209111349.20472-1-shouryashukla.oo@gmail.com","subject":"Re: Conversion of 'git submodule' to C: need some help","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2020-02-13T13:33:58Z","receivedAt":"2020-02-13T13:34:09Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Shourya,\n\njust adding a little to what Abhishek said (which was pretty sound\nadvice!) below.\n\nOn Sun, 9 Feb 2020, Shourya Shukla wrote:\n\n> I am facing some problems and would love some insight on them:\n>\n> \t1. What exactly are we aiming in [3]? To replace the function completely\n> \t   or to just add some 'repo_submodule_init' functionality?\n\nIf you follow the \"Git blame\" link in the breadcrumb menu, you will get to\nthe commit that added the TODO:\nhttps://github.com/periperidip/git/commit/18cfc0886617e28fb6d29d579bec0ffcdb439196\n\nUnfortunately, it does not necessarily help me understand what that TODO\nis about. So let's analyze the code:\n\nint add_submodule_odb(const char *path)\n{\n\tstruct strbuf objects_directory = STRBUF_INIT;\n\tint ret = 0;\n\tret = strbuf_git_path_submodule(&objects_directory, path, \"objects/\");\n\tif (ret)\n\t\tgoto done;\n\tif (!is_directory(objects_directory.buf)) {\n\t\tret = -1;\n\t\tgoto done;\n\t}\n\tadd_to_alternates_memory(objects_directory.buf);\ndone:\n\tstrbuf_release(&objects_directory);\n\treturn ret;\n}\n\nOkay, so this just adds the object database of the submodule (if it\nexists, if it does not exist, the submodule is probably _already_ using\nthe superproject's database).\n\nTo understand what I am talking about, have a look at this document:\nhttps://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt-objects\n\nSo what does the function do that was suggested as a better alternative?\n\nint repo_submodule_init(struct repository *subrepo,\n\t\t\tstruct repository *superproject,\n\t\t\tconst struct submodule *sub)\n{\n\tstruct strbuf gitdir = STRBUF_INIT;\n\tstruct strbuf worktree = STRBUF_INIT;\n\tint ret = 0;\n\n\n\tif (!sub) {\n\t\tret = -1;\n\t\tgoto out;\n\t}\n\n\n\tstrbuf_repo_worktree_path(&gitdir, superproject, \"%s/.git\", sub->path);\n\tstrbuf_repo_worktree_path(&worktree, superproject, \"%s\", sub->path);\n\n\n\tif (repo_init(subrepo, gitdir.buf, worktree.buf)) {\n\t\t/*\n\t\t * If initialization fails then it may be due to the\n\t\t * submodule\n\t\t * not being populated in the superproject's worktree.\n\t\t * Instead\n\t\t * we can try to initialize the submodule by finding it's\n\t\t * gitdir\n\t\t * in the superproject's 'modules' directory.  In this\n\t\t * case the\n\t\t * submodule would not have a worktree.\n\t\t */\n\t\tstrbuf_reset(&gitdir);\n\t\tstrbuf_repo_git_path(&gitdir, superproject,\n\t\t\t\t     \"modules/%s\", sub->name);\n\n\n\t\tif (repo_init(subrepo, gitdir.buf, NULL)) {\n\t\t\tret = -1;\n\t\t\tgoto out;\n\t\t}\n\t}\n\n\n\tsubrepo->submodule_prefix = xstrfmt(\"%s%s/\",\n\t\t\t\t\t    superproject->submodule_prefix ?\n\t\t\t\t\t    superproject->submodule_prefix :\n\t\t\t\t\t    \"\", sub->path);\n\nout:\n\tstrbuf_release(&gitdir);\n\tstrbuf_release(&worktree);\n\treturn ret;\n}\n\nAh, that populates a complete `struct repository`! I fear, however, that\nour object lookup is currently not tied to such a `struct repository`\ninstance. So I think that this TODO can only be addressed once a ton more\npatch series like\nhttps://lore.kernel.org/git/f1e4da02-9411-8a93-ca62-6d7ae7bf4ae8@gmail.com/\nmade it not only to the Git mailing list, but into `master`.\n\n> \t2. Something I inferred was that functions with names of the pattern 'strbuf_git_*'\n> \t   are trying to 'create a path'(are they physically creating the path or just\n> \t   instructing git about them?) while functions of the pattern 'git_*' are trying\n> \t   to check some conditions denoted by their function names(for instance\n> \t   'git_config_rename_section_in_file')? Is this inference correct to some extent?\n\nAll `strbuf_*()` functions work on our \"string class\" (I forgot who said\nit, but it is true that any sufficiently advanced C project sooner or\nlater develops their own string data type).\n\nTo know whether the functions in question create a path or not, you will\nhave to find their documentation in the appropriate header file (usually\n`strbuf.h`), or absent that, find and understand their implementation\n(usually in `strbuf.c`).\n\n> \t3. How does one check which all parts of a command have been completed? Is it checked\n> \t   by looking at the file history or by comparing with the shell script of the command\n> \t   or are there any other means?\n\nYou mean whether a scripted command has been completely converted to C?\nThere is no universal way to do that.\n\nIn `git submodule`'s instance, I would say that a subcommand is converted\nsuccessfully when all parts except for the command-line option parsing\nhave been moved into the `submodule--helper`. Eventually,\n`git-submodule.sh` will only have functions that parse command-line\noptions and then pass the result on to the helper. At that point, the\ncommand-line option parsing can _also_ be moved into the helper. Or maybe\neven the entire script in one go, I am not sure how big of a patch that\nwould be.\n\n> \t4. Is it fine if I am not able to understand the purpose of certain functions right now(such as\n> \t   'add_submodule_odb')? I am able to get a rough idea of what the functions are doing but I am\n> \t   not able to decode certain functions line-by-line.\n\nIt is okay not to understand all the details, but if you want to work on\nthe code, you will need to understand at least the purpose, and if you\nwant to come up with a project plan (e.g. for GSoC), it will be _really_\nhelpful to form an understanding of the implementation details, too.\n\n> Currently, I am studying in depth about 'git objects' and the submodule command on the git Documentation.\n> What else do would you advise me to strengthen my understanding of the code and git in general?\n\nI don't know what in particular you want to strengthen. Typically, a good\nway to learn enough about the code base in preparation for Google Summer\nof Code or Outreachy is to read the code, and whenever anything is\nunclear, try to learn about the data structures and/or the underlying\ndesign by studying the files in `Documentation/` (in particular in the\n`technical/` subdirectory) whose names seem relevant.\n\nCiao,\nJohannes\n\n>\n> Regards,\n> Shourya Shukla\n>\n> [1]: https://github.com/periperidip/git/blob/v2.25.0/submodule.c\n> [2]: https://lore.kernel.org/git/20200201173841.13760-1-shouryashukla.oo@gmail.com/\n> [3]: https://github.com/periperidip/git/blob/v2.25.0/submodule.c#L168\n>\n>\n"}]}