Skip to content

Commit

Permalink
Merge branch 'jc/request-pull-show-head-4'
Browse files Browse the repository at this point in the history
* jc/request-pull-show-head-4:
  request-pull: use the annotated tag contents
  fmt-merge-msg.c: Fix an "dubious one-bit signed bitfield" sparse error
  environment.c: Fix an sparse "symbol not declared" warning
  builtin/log.c: Fix an "Using plain integer as NULL pointer" warning
  fmt-merge-msg: use branch.$name.description
  request-pull: use the branch description
  request-pull: state what commit to expect
  request-pull: modernize style
  branch: teach --edit-description option
  format-patch: use branch description in cover letter
  branch: add read_branch_desc() helper function

Conflicts:
	builtin/branch.c
  • Loading branch information
gitster committed Dec 9, 2011
2 parents 1ee740e + d050464 commit a4043ae
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 59 deletions.
5 changes: 5 additions & 0 deletions Documentation/git-branch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SYNOPSIS
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
'git branch' (-m | -M) [<oldbranch>] <newbranch>
'git branch' (-d | -D) [-r] <branchname>...
'git branch' --edit-description [<branchname>]

DESCRIPTION
-----------
Expand Down Expand Up @@ -158,6 +159,10 @@ start-point is either a local or remote-tracking branch.
like '--track' would when creating the branch, except that where
branch points to is not changed.

--edit-description::
Open an editor and edit the text to explain what the branch is
for, to be used by various other commands (e.g. `request-pull`).

--contains <commit>::
Only list branches which contain the specified commit.

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ LIB_H += diffcore.h
LIB_H += diff.h
LIB_H += dir.h
LIB_H += exec_cmd.h
LIB_H += fmt-merge-msg.h
LIB_H += fsck.h
LIB_H += gettext.h
LIB_H += git-compat-util.h
Expand Down
31 changes: 31 additions & 0 deletions branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,37 @@ static int setup_tracking(const char *new_ref, const char *orig_ref,
return 0;
}

struct branch_desc_cb {
const char *config_name;
const char *value;
};

static int read_branch_desc_cb(const char *var, const char *value, void *cb)
{
struct branch_desc_cb *desc = cb;
if (strcmp(desc->config_name, var))
return 0;
free((char *)desc->value);
return git_config_string(&desc->value, var, value);
}

int read_branch_desc(struct strbuf *buf, const char *branch_name)
{
struct branch_desc_cb cb;
struct strbuf name = STRBUF_INIT;
strbuf_addf(&name, "branch.%s.description", branch_name);
cb.config_name = name.buf;
cb.value = NULL;
if (git_config(read_branch_desc_cb, &cb) < 0) {
strbuf_release(&name);
return -1;
}
if (cb.value)
strbuf_addstr(buf, cb.value);
strbuf_release(&name);
return 0;
}

int validate_new_branchname(const char *name, struct strbuf *ref,
int force, int attr_only)
{
Expand Down
5 changes: 5 additions & 0 deletions branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ void remove_branch_state(void);
#define BRANCH_CONFIG_VERBOSE 01
extern void install_branch_config(int flag, const char *local, const char *origin, const char *remote);

/*
* Read branch description
*/
extern int read_branch_desc(struct strbuf *, const char *branch_name);

#endif
58 changes: 55 additions & 3 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,49 @@ static int opt_parse_merge_filter(const struct option *opt, const char *arg, int
return 0;
}

static const char edit_description[] = "BRANCH_DESCRIPTION";

static int edit_branch_description(const char *branch_name)
{
FILE *fp;
int status;
struct strbuf buf = STRBUF_INIT;
struct strbuf name = STRBUF_INIT;

read_branch_desc(&buf, branch_name);
if (!buf.len || buf.buf[buf.len-1] != '\n')
strbuf_addch(&buf, '\n');
strbuf_addf(&buf,
"# Please edit the description for the branch\n"
"# %s\n"
"# Lines starting with '#' will be stripped.\n",
branch_name);
fp = fopen(git_path(edit_description), "w");
if ((fwrite(buf.buf, 1, buf.len, fp) < buf.len) || fclose(fp)) {
strbuf_release(&buf);
return error(_("could not write branch description template: %s\n"),
strerror(errno));
}
strbuf_reset(&buf);
if (launch_editor(git_path(edit_description), &buf, NULL)) {
strbuf_release(&buf);
return -1;
}
stripspace(&buf, 1);

strbuf_addf(&name, "branch.%s.description", branch_name);
status = git_config_set(name.buf, buf.buf);
strbuf_release(&name);
strbuf_release(&buf);

return status;
}

int cmd_branch(int argc, const char **argv, const char *prefix)
{
int delete = 0, rename = 0, force_create = 0, list = 0;
int verbose = 0, abbrev = -1, detached = 0;
int reflog = 0;
int reflog = 0, edit_description = 0;
enum branch_track track;
int kinds = REF_LOCAL_BRANCH;
struct commit_list *with_commit = NULL;
Expand Down Expand Up @@ -666,6 +704,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
OPT_BOOLEAN(0, "list", &list, "list branch names"),
OPT_BOOLEAN('l', "create-reflog", &reflog, "create the branch's reflog"),
OPT_BOOLEAN(0, "edit-description", &edit_description,
"edit the description for the branch"),
OPT__FORCE(&force_create, "force creation (when already exists)"),
{
OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
Expand Down Expand Up @@ -705,7 +745,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
0);

if (!delete && !rename && argc == 0)
if (!delete && !rename && !edit_description && argc == 0)
list = 1;

if (!!delete + !!rename + !!force_create + !!list > 1)
Expand All @@ -719,7 +759,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
else if (list)
return print_ref_list(kinds, detached, verbose, abbrev,
with_commit, argv);
else if (rename) {
else if (edit_description) {
const char *branch_name;
if (detached)
die("Cannot give description to detached HEAD");
if (!argc)
branch_name = head;
else if (argc == 1)
branch_name = argv[0];
else
usage_with_options(builtin_branch_usage, options);
if (edit_branch_description(branch_name))
return 1;
} else if (rename) {
if (argc == 1)
rename_branch(head, argv[0], rename > 1);
else if (argc == 2)
Expand Down
75 changes: 59 additions & 16 deletions builtin/fmt-merge-msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@
#include "revision.h"
#include "tag.h"
#include "string-list.h"
#include "branch.h"
#include "fmt-merge-msg.h"

static const char * const fmt_merge_msg_usage[] = {
"git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
NULL
};

static int shortlog_len;
static int use_branch_desc;

static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
int fmt_merge_msg_config(const char *key, const char *value, void *cb)
{
if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
int is_bool;
shortlog_len = git_config_bool_or_int(key, value, &is_bool);
if (!is_bool && shortlog_len < 0)
merge_log_config = git_config_bool_or_int(key, value, &is_bool);
if (!is_bool && merge_log_config < 0)
return error("%s: negative length %s", key, value);
if (is_bool && shortlog_len)
shortlog_len = DEFAULT_MERGE_LOG_LEN;
if (is_bool && merge_log_config)
merge_log_config = DEFAULT_MERGE_LOG_LEN;
} else if (!strcmp(key, "merge.branchdesc")) {
use_branch_desc = git_config_bool(key, value);
}
return 0;
}
Expand All @@ -31,6 +35,11 @@ struct src_data {
int head_status;
};

struct origin_data {
unsigned char sha1[20];
unsigned is_local_branch:1;
};

static void init_src_data(struct src_data *data)
{
data->branch.strdup_strings = 1;
Expand All @@ -45,7 +54,7 @@ static struct string_list origins = STRING_LIST_INIT_DUP;
static int handle_line(char *line)
{
int i, len = strlen(line);
unsigned char *sha1;
struct origin_data *origin_data;
char *src, *origin;
struct src_data *src_data;
struct string_list_item *item;
Expand All @@ -61,11 +70,13 @@ static int handle_line(char *line)
return 2;

line[40] = 0;
sha1 = xmalloc(20);
i = get_sha1(line, sha1);
origin_data = xcalloc(1, sizeof(struct origin_data));
i = get_sha1(line, origin_data->sha1);
line[40] = '\t';
if (i)
if (i) {
free(origin_data);
return 3;
}

if (line[len - 1] == '\n')
line[len - 1] = 0;
Expand Down Expand Up @@ -93,6 +104,7 @@ static int handle_line(char *line)
origin = src;
src_data->head_status |= 1;
} else if (!prefixcmp(line, "branch ")) {
origin_data->is_local_branch = 1;
origin = line + 7;
string_list_append(&src_data->branch, origin);
src_data->head_status |= 2;
Expand All @@ -119,7 +131,9 @@ static int handle_line(char *line)
sprintf(new_origin, "%s of %s", origin, src);
origin = new_origin;
}
string_list_append(&origins, origin)->util = sha1;
if (strcmp(".", src))
origin_data->is_local_branch = 0;
string_list_append(&origins, origin)->util = origin_data;
return 0;
}

Expand All @@ -140,16 +154,38 @@ static void print_joined(const char *singular, const char *plural,
}
}

static void shortlog(const char *name, unsigned char *sha1,
struct commit *head, struct rev_info *rev, int limit,
struct strbuf *out)
static void add_branch_desc(struct strbuf *out, const char *name)
{
struct strbuf desc = STRBUF_INIT;

if (!read_branch_desc(&desc, name)) {
const char *bp = desc.buf;
while (*bp) {
const char *ep = strchrnul(bp, '\n');
if (*ep)
ep++;
strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
bp = ep;
}
if (out->buf[out->len - 1] != '\n')
strbuf_addch(out, '\n');
}
strbuf_release(&desc);
}

static void shortlog(const char *name,
struct origin_data *origin_data,
struct commit *head,
struct rev_info *rev, int limit,
struct strbuf *out)
{
int i, count = 0;
struct commit *commit;
struct object *branch;
struct string_list subjects = STRING_LIST_INIT_DUP;
int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
struct strbuf sb = STRBUF_INIT;
const unsigned char *sha1 = origin_data->sha1;

branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
if (!branch || branch->type != OBJ_COMMIT)
Expand Down Expand Up @@ -188,6 +224,9 @@ static void shortlog(const char *name, unsigned char *sha1,
else
strbuf_addf(out, "\n* %s:\n", name);

if (origin_data->is_local_branch && use_branch_desc)
add_branch_desc(out, name);

for (i = 0; i < subjects.nr; i++)
if (i >= limit)
strbuf_addf(out, " ...\n");
Expand Down Expand Up @@ -303,8 +342,9 @@ static int do_fmt_merge_msg(int merge_title, struct strbuf *in,
strbuf_addch(out, '\n');

for (i = 0; i < origins.nr; i++)
shortlog(origins.items[i].string, origins.items[i].util,
head, &rev, shortlog_len, out);
shortlog(origins.items[i].string,
origins.items[i].util,
head, &rev, shortlog_len, out);
}
return 0;
}
Expand All @@ -318,6 +358,7 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
{
const char *inpath = NULL;
const char *message = NULL;
int shortlog_len = -1;
struct option options[] = {
{ OPTION_INTEGER, 0, "log", &shortlog_len, "n",
"populate log with at most <n> entries from shortlog",
Expand All @@ -341,6 +382,8 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
0);
if (argc > 0)
usage_with_options(fmt_merge_msg_usage, options);
if (shortlog_len < 0)
shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
if (message && !shortlog_len) {
char nl = '\n';
write_in_full(STDOUT_FILENO, message, strlen(message));
Expand Down
Loading

0 comments on commit a4043ae

Please sign in to comment.