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

[PATCH v2] follow usage recommendations for branch_get()

From
Rubén Justo <rjusto@gmail.com>
Date
Apr 22, 2023, 22:27 UTC
Message-ID
<456b2a6f-eedb-a75a-2299-06ee6e7f3a47@gmail.com>
In-Reply-To
<4cb4b69c-bd14-dfbd-6d06-59a7cd7e8c94@gmail.com>

Our recommendation is to use branch_get(NULL) to obtain the 'struct branch*' for the currently checked out branch in the current worktree. While branch_get("HEAD") produces the same result, it does not follow the recommended usage and may cause confusion.

Let's change some calls to branch_get() we currently have in our codebase that do not follow the recommendation, applying the following semantic patch:

    @@
    @@
    - branch_get("HEAD")
    + branch_get(NULL)
Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 builtin/fetch.c | 2 +-
 builtin/pull.c  | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index ab623f41b4..3c1806aae9 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1755,7 +1755,7 @@ static int do_fetch(struct transport *transport,
 	commit_fetch_head(&fetch_head);
 
 	if (set_upstream) {
-		struct branch *branch = branch_get("HEAD");
+		struct branch *branch = branch_get(NULL);
 		struct ref *rm;
 		struct ref *source_ref = NULL;
 
diff --git a/builtin/pull.c b/builtin/pull.c
index 967368ebc6..f93e8610e0 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -335,7 +335,7 @@ static const char *config_get_ff(void)
  */
 static enum rebase_type config_get_rebase(int *rebase_unspecified)
 {
-	struct branch *curr_branch = branch_get("HEAD");
+	struct branch *curr_branch = branch_get(NULL);
 	const char *value;
 
 	if (curr_branch) {
@@ -440,7 +440,7 @@ static int get_only_remote(struct remote *remote, void *cb_data)
  */
 static void NORETURN die_no_merge_candidates(const char *repo, const char **refspecs)
 {
-	struct branch *curr_branch = branch_get("HEAD");
+	struct branch *curr_branch = branch_get(NULL);
 	const char *remote = curr_branch ? curr_branch->remote_name : NULL;
 
 	if (*refspecs) {
@@ -713,7 +713,7 @@ static const char *get_upstream_branch(const char *remote)
 	if (!rm)
 		return NULL;
 
-	curr_branch = branch_get("HEAD");
+	curr_branch = branch_get(NULL);
 	if (!curr_branch)
 		return NULL;
 
@@ -777,7 +777,7 @@ static int get_rebase_fork_point(struct object_id *fork_point, const char *repo,
 	struct child_process cp = CHILD_PROCESS_INIT;
 	struct strbuf sb = STRBUF_INIT;
 
-	curr_branch = branch_get("HEAD");
+	curr_branch = branch_get(NULL);
 	if (!curr_branch)
 		return -1;
 
-- 
2.34.1
Previous: Rubén Justo
Message 9 of 9 in “coccinelle: add and apply branch_get() rules”
  1. coccinelle: add and apply branch_get() rulesRubén Justo, Apr 6, 2023
  2. Junio C HamanoApr 7, 2023
  3. Junio C HamanoApr 7, 2023
  4. Rubén JustoApr 7, 2023
  5. Junio C HamanoApr 8, 2023
  6. Rubén JustoApr 9, 2023
  7. Ævar Arnfjörð BjarmasonApr 16, 2023
  8. Rubén JustoApr 16, 2023
  9. follow usage recommendations for branch_get()Rubén Justo, Apr 22, 2023

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.