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

Re: [PATCH] branch as a builtin (again)

From
DRDavid Rientjes <rientjes@google.com>
Date
Aug 21, 2006, 07:49 UTC
Message-ID
<Pine.LNX.4.63.0608210045450.17336@chino.corp.google.com>
In-Reply-To
<59ad55d30608201422h4a6d40f7y7782212637380438@mail.gmail.com>
On Sun, 20 Aug 2006, Kristian Høgsberg wrote:
Show 23 quoted lines
> diff --git a/builtin-branch.c b/builtin-branch.c
> new file mode 100644
> index 0000000..25c6313
> --- /dev/null
> +++ b/builtin-branch.c
> @@ -0,0 +1,170 @@
> +/*
> + * Builtin "git branch"
> + *
> + * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
> + * Based on git-branch.sh by Junio C Hamano.
> + */
> +
> +#include "cache.h"
> +#include "refs.h"
> +#include "commit.h"
> +#include "builtin.h"
> +
> +static const char builtin_branch_usage[] =
> +    "git-branch [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r";
> +
> +
> +static int remote_only = 0;
Unnecessary initialization
Show 19 quoted lines
> +static const char *head;
> +static unsigned char head_sha1[20];
> +
> +static int in_merge_bases(const unsigned char *sha1,
> +			  struct commit *rev1,
> +			  struct commit *rev2)
> +{
> +	struct commit_list *bases, *b;
> +
> +	bases = get_merge_bases(rev1, rev2, 1);
> +	for (b = bases; b != NULL; b = b->next) {
> +		if (!hashcmp(sha1, b->item->object.sha1)) {
> +			free_commit_list(bases);
> +			return 1;
> +		}
> +	}
> +
> +	free_commit_list(bases);
> +	return 0;
Make it cleaner for the future:
{
	int ret = 0;
	...
	for (b = bases; b; b = b->next) {
		if (!hashcmp(sha1, b->item->object.sha1)) {
			ret = 1;
			break;
		}
	}
	free_commit_list(bases);
	return ret;
}
Show 17 quoted lines
> +}
> +
> +static void delete_branches(int argc, const char **argv, int force)
> +{
> +	struct commit *rev1, *rev2;
> +	unsigned char sha1[20];
> +	const char *p, *name;
> +	int i;
> +
> +	for (i = 0; i < argc; i++) {
> +		if (!strcmp(head, argv[i]))
> +			die("Cannot delete the branch you are currently on.");
> +
> +		name = git_path("refs/heads/%s", argv[i]);
> +		p = resolve_ref(name, sha1, 1);
> +		if (p == NULL)
> +			die("Branch '%s' not found.", argv[i]);
p is unnecessary:
	if (!resolve_ref(name, sha1, 1))
		die(...);
> +
> +		rev1 = lookup_commit_reference(sha1);
> +		rev2 = lookup_commit_reference(head_sha1);
Are these both needed inside every iteration?
Show 29 quoted lines
> +		if (!rev1 || !rev2)
> +			die("Couldn't look up commit objects.");
> +
> +		/* This checks wether the merge bases of branch and
> +		 * HEAD contains branch -- which means that the HEAD
> +		 * contains everything in both.
> +		 */
> +
> +		if (!force &&
> +		    !in_merge_bases(sha1, rev1, rev2)) {
> +			fprintf(stderr,
> +				"The branch '%s' is not a strict subset of your current HEAD.\n"
> +				"If you are sure you want to delete it, run 'git branch -D %s'.\n",
> +				argv[i], argv[i]);
> +			exit(1);
> +		}
> +
> +		unlink(name);
> +		printf("Deleted branch %s.\n", argv[i]);
> +	}
> +}
> +
> +static int show_reference(const char *refname, const unsigned char *sha1)
> +{
> +	int is_head = !strcmp(refname, head);
> +
> +	printf("%c %s\n", (is_head ? '*' : ' '), refname);
> +
> +	return 0;
Unnecessary variable
> +}
> +
> +static void create_branch (const char *name, const char *start, int force)
No space
		David
Previous: Johannes SchindelinNext: Shawn Pearce
Message 3 of 13 in “branch as a builtin (again)”
  1. branch as a builtin (again)Kristian Høgsberg, Aug 20, 2006
  2. Johannes SchindelinAug 20, 2006
  3. David RientjesAug 21, 2006
  4. Shawn PearceAug 21, 2006
  5. Jonas FonsecaAug 21, 2006
  6. Kristian HøgsbergAug 21, 2006
  7. David RientjesAug 21, 2006
  8. Kristian HøgsbergAug 21, 2006
  9. Junio C HamanoAug 22, 2006
  10. Johannes SchindelinAug 21, 2006
  11. Kristian HøgsbergAug 21, 2006
  12. Shawn PearceAug 21, 2006
  13. Johannes SchindelinAug 21, 2006

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.