Commit 19e70ad6fff7c3b54cf7f69f86c86c5299037c62

Stefan Sperling 2019-05-14T16:33:38

make 'tog log' default to the current branch in a work tree

diff --git a/tog/tog.1 b/tog/tog.1
index 08647d7..0ebf42b 100644
--- a/tog/tog.1
+++ b/tog/tog.1
@@ -115,6 +115,8 @@ Start traversing history at the specified
 .Ar commit .
 The expected argument is the name of a branch or a SHA1 hash which corresponds
 to a commit object.
+If this option is not specified, default to the work tree's current branch
+if invoked in a work tree, or to the repository's HEAD reference.
 .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
diff --git a/tog/tog.c b/tog/tog.c
index a3ca552..f6e6728 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1106,14 +1106,15 @@ queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
 }
 
 static const struct got_error *
-get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
+get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
+    struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	struct got_reference *head_ref;
 
 	*head_id = NULL;
 
-	err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
+	err = got_ref_open(&head_ref, repo, branch_name, 0);
 	if (err)
 		return err;
 
@@ -1901,7 +1902,9 @@ cmd_log(int argc, char *argv[])
 		goto done;
 
 	if (start_commit == NULL)
-		error = get_head_commit_id(&start_id, repo);
+		error = get_head_commit_id(&start_id, worktree ?
+		    got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
+		    repo);
 	else
 		error = got_object_resolve_id_str(&start_id, repo,
 		    start_commit);
@@ -3909,7 +3912,7 @@ cmd_tree(int argc, char *argv[])
 		goto done;
 
 	if (commit_id_arg == NULL)
-		error = get_head_commit_id(&commit_id, repo);
+		error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
 	else
 		error = got_object_resolve_id_str(&commit_id, repo,
 		    commit_id_arg);