@matheusbsbs / matheus-bagliano / commits / 420acb31ac
Fork 0

get_relative_cwd(): clarify why it handles dir == NULL

The comment did not make a good case why it makes sense. Clarify, and remove stale comment about the caller being lazy. The behaviour on NULL input is pretty much intentional. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Aug 1, 2007 at 19:26 UTC 420acb31acbfbd78e0c35ac0c614de8717daed0a
1 file changed +10 -5
dir.c
+10 -5
@@ -646,16 +646,21 @@ file_exists(const char *f)
646 /*
647 * get_relative_cwd() gets the prefix of the current working directory
648 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
649 - * As a convenience, it also returns NULL if 'dir' is already NULL.
649 + *
650 + * As a convenience, it also returns NULL if 'dir' is already NULL. The
651 + * reason for this behaviour is that it is natural for functions returning
652 + * directory names to return NULL to say "this directory does not exist"
653 + * or "this directory is invalid". These cases are usually handled the
654 + * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
655 + * returns NULL for both of them.
656 + *
657 + * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
658 + * unifies the handling of "outside work tree" with "no work tree at all".
659 */
660 char *get_relative_cwd(char *buffer, int size, const char *dir)
661 {
662 char *cwd = buffer;
663
655 - /*
656 - * a lazy caller can pass a NULL returned from get_git_work_tree()
657 - * and rely on this function to return NULL.
658 - */
664 if (!dir)
665 return NULL;
666 if (!getcwd(buffer, size))