Commit 74283ab8e59cdc12bc4adb3d2037f37df510bf1f

Stefan Sperling 2020-02-07T18:02:57

switch 'tog tree' repository path argument to a new -r option

diff --git a/tog/tog.1 b/tog/tog.1
index 36e3560..1cca129 100644
--- a/tog/tog.1
+++ b/tog/tog.1
@@ -250,11 +250,8 @@ Use the repository at the specified path.
 If not specified, assume the repository is located at or above the current
 working directory.
 .El
-.It Cm tree Oo Fl c Ar commit Oc Op Ar repository-path
+.It Cm tree Oo Fl c Ar commit Oc Oo Fl r Ar repository-path Oc
 Display the repository tree.
-If the
-.Ar repository path
-is omitted, assume the repository is located in the current working directory.
 .Pp
 Displayed tree entries may carry one of the following trailing annotations:
 .Bl -column YXZ description
@@ -308,6 +305,13 @@ Start traversing history at the specified
 The expected argument is the name of a branch or a commit ID SHA1 hash.
 An abbreviated hash argument will be expanded to a full SHA1 hash
 automatically, provided the abbreviation is unique.
+.It Fl r Ar repository-path
+Use the repository at the specified path.
+If not specified, assume the repository is located at or above the current
+working directory.
+If this directory is a
+.Xr got 1
+work tree, use the repository path associated with this work tree.
 .El
 .El
 .Sh ENVIRONMENT
diff --git a/tog/tog.c b/tog/tog.c
index 9618b61..ca4cead 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -5156,7 +5156,7 @@ __dead static void
 usage_tree(void)
 {
 	endwin();
-	fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
+	fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path]\n",
 	    getprogname());
 	exit(1);
 }
@@ -5183,11 +5183,17 @@ cmd_tree(int argc, char *argv[])
 		err(1, "pledge");
 #endif
 
-	while ((ch = getopt(argc, argv, "c:")) != -1) {
+	while ((ch = getopt(argc, argv, "c:r:")) != -1) {
 		switch (ch) {
 		case 'c':
 			commit_id_arg = optarg;
 			break;
+		case 'r':
+			repo_path = realpath(optarg, NULL);
+			if (repo_path == NULL)
+				return got_error_from_errno2("realpath",
+				    optarg);
+			break;
 		default:
 			usage_tree();
 			/* NOTREACHED */
@@ -5197,7 +5203,10 @@ cmd_tree(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	if (argc == 0) {
+	if (argc != 0)
+		usage_tree();
+
+	if (repo_path == NULL) {
 		struct got_worktree *worktree;
 		char *cwd = getcwd(NULL, 0);
 		if (cwd == NULL)
@@ -5205,6 +5214,8 @@ cmd_tree(int argc, char *argv[])
 		error = got_worktree_open(&worktree, cwd);
 		if (error && error->code != GOT_ERR_NOT_WORKTREE)
 			goto done;
+		else
+			error = NULL;
 		if (worktree) {
 			free(cwd);
 			repo_path =
@@ -5216,12 +5227,7 @@ cmd_tree(int argc, char *argv[])
 			error = got_error_from_errno("strdup");
 			goto done;
 		}
-	} else if (argc == 1) {
-		repo_path = realpath(argv[0], NULL);
-		if (repo_path == NULL)
-			return got_error_from_errno2("realpath", argv[0]);
-	} else
-		usage_tree();
+	}
 
 	init_curses();