All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] coccinelle: add and apply branch_get() rules
@ 2023-04-06 20:34 Rubén Justo
  2023-04-07 15:55 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Rubén Justo @ 2023-04-06 20:34 UTC (permalink / raw)
  To: Git List

There are three supported ways to obtain a "struct branch *" for the
currently checked out branch, in the current worktree, using the API
branch_get(): branch_get(NULL), branch_get("") and branch_get("HEAD").

The first one is the recommended [1][2] and optimal usage.  Let's add
two coccinelle rules to convert the latter two into the first one.

  1. f019d08ea6 (API documentation for remote.h, 2008-02-19)

  2. d27eb356bf (remote: move doc to remote.h and refspec.h, 2019-11-17)

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 builtin/fetch.c                     |  2 +-
 builtin/pull.c                      |  8 ++++----
 contrib/coccinelle/branch_get.cocci | 10 ++++++++++
 3 files changed, 15 insertions(+), 5 deletions(-)
 create mode 100644 contrib/coccinelle/branch_get.cocci

diff --git a/builtin/fetch.c b/builtin/fetch.c
index 7221e57f35..45d81c8e02 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1738,7 +1738,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 56f679d94a..fbb1cbea0a 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -332,7 +332,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) {
@@ -437,7 +437,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) {
@@ -710,7 +710,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;
 
@@ -774,7 +774,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;
 
diff --git a/contrib/coccinelle/branch_get.cocci b/contrib/coccinelle/branch_get.cocci
new file mode 100644
index 0000000000..3ec5b59723
--- /dev/null
+++ b/contrib/coccinelle/branch_get.cocci
@@ -0,0 +1,10 @@
+@@
+@@
+- branch_get("HEAD")
++ branch_get(NULL)
+
+@@
+@@
+- branch_get("")
++ branch_get(NULL)
+
-- 
2.34.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-04-22 22:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-06 20:34 [PATCH] coccinelle: add and apply branch_get() rules Rubén Justo
2023-04-07 15:55 ` Junio C Hamano
2023-04-07 16:05   ` Junio C Hamano
2023-04-07 19:09   ` Rubén Justo
2023-04-08 22:45     ` Junio C Hamano
2023-04-09  7:43       ` Rubén Justo
2023-04-16 13:56 ` Ævar Arnfjörð Bjarmason
2023-04-16 23:26   ` Rubén Justo
2023-04-22 22:27 ` [PATCH v2] follow usage recommendations for branch_get() Rubén Justo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.