Exemplo n.º 1
0
void
cvs_annotate_local(struct cvs_file *cf)
{
	int i;
	char date[10], rnum[13], *p;
	RCSNUM *bnum, *rev;
	struct rcs_line *line;
	struct rcs_line **alines;

	cvs_log(LP_TRACE, "cvs_annotate_local(%s)", cf->file_path);

	cvs_file_classify(cf, cvs_directory_tag);

	if (cf->file_rcs == NULL || cf->file_rcs->rf_head == NULL)
		return;

	if (cvs_specified_tag != NULL) {
		if ((rev = rcs_translate_tag(cvs_specified_tag,
		    cf->file_rcs)) == NULL) {
			if (!force_head)
				/* Stick at weird GNU cvs, ignore error. */
				return;

			/* -f is not allowed for unknown symbols */
			rev = rcsnum_parse(cvs_specified_tag);
			if (rev == NULL)
				fatal("no such tag %s", cvs_specified_tag);
                        rcsnum_free(rev);
			rev = rcsnum_alloc();
			rcsnum_cpy(cf->file_rcs->rf_head, rev, 0);
		}

		/*
		 * If this is a revision in a branch, we have to go first
		 * from HEAD to branch, then down to 1.1. After that, take
		 * annotated branch and go up to branch revision. This must
		 * be done this way due to different handling of "a" and
		 * "d" in rcs file for annotation.
		 */
		if (!RCSNUM_ISBRANCHREV(rev)) {
			bnum = rev;
		} else {
			bnum = rcsnum_alloc();
			rcsnum_cpy(rev, bnum, 2);
		}

		rcs_rev_getlines(cf->file_rcs, bnum, &alines);

		/*
		 * Go into branch and receive annotations for branch revision,
		 * with inverted "a" and "d" meaning.
		 */
		if (bnum != rev) {
			rcs_annotate_getlines(cf->file_rcs, rev, &alines);
			rcsnum_free(bnum);
		}
		rcsnum_free(rev);
	} else {
		rcs_rev_getlines(cf->file_rcs, (cvs_specified_date != -1 ||
		    cvs_directory_date != -1) ? cf->file_rcsrev :
		    cf->file_rcs->rf_head, &alines);
	}

	/* Stick at weird GNU cvs, ignore error. */
	if (alines == NULL)
		return;

	cvs_log(LP_RCS, "Annotations for %s", cf->file_path);
	cvs_log(LP_RCS, "***************");

	for (i = 0; alines[i] != NULL; i++) {
		line = alines[i];

		rcsnum_tostr(line->l_delta->rd_num, rnum, sizeof(rnum));
		strftime(date, sizeof(date), "%d-%b-%y",
		    &(line->l_delta->rd_date));
		if (line->l_len && line->l_line[line->l_len - 1] == '\n')
			line->l_line[line->l_len - 1] = '\0';
		else {
			p = xmalloc(line->l_len + 1);
			memcpy(p, line->l_line, line->l_len);
			p[line->l_len] = '\0';

			if (line->l_needsfree)
				xfree(line->l_line);
			line->l_line = p;
			line->l_len++;
			line->l_needsfree = 1;
		}
		cvs_printf("%-12.12s (%-8.8s %s): %s\n", rnum,
		    line->l_delta->rd_author, date, line->l_line);

		if (line->l_needsfree)
			xfree(line->l_line);
		xfree(line);
	}

	xfree(alines);
}
Exemplo n.º 2
0
static void
import_update(struct cvs_file *cf)
{
	int ret;
	BUF *b1, *b2, *d;
	char branch[CVS_REV_BUFSZ];
	RCSNUM *newrev, *rev, *brev;

	cvs_log(LP_TRACE, "import_update(%s)", cf->file_path);

	if (cf->file_rcs->rf_head == NULL)
		fatal("no head revision in RCS file for `%s'", cf->file_path);

	if ((rev = rcs_translate_tag(import_branch, cf->file_rcs)) == NULL)
		fatal("import_update: could not translate tag `%s'",
		    import_branch);

	if ((brev = rcsnum_parse(import_branch)) == NULL)
		fatal("import_update: rcsnum_parse failed");

	b1 = rcs_rev_getbuf(cf->file_rcs, rev, RCS_KWEXP_NONE);
	b2 = buf_load_fd(cf->fd);

	ret = buf_differ(b1, b2);
	buf_free(b1);
	buf_free(b2);
	if (ret == 0) {
		import_tag(cf, brev, rev);
		rcsnum_free(brev);
		if (cvs_noexec != 1)
			rcs_write(cf->file_rcs);
		import_printf("U %s/%s\n", import_repository, cf->file_path);
		return;
	}

	if (cf->file_rcs->rf_branch != NULL)
		rcsnum_tostr(cf->file_rcs->rf_branch, branch, sizeof(branch));

	if (cf->file_rcs->rf_branch == NULL || cf->in_attic == 1 ||
	    strcmp(branch, import_branch)) {
		import_conflicts++;
		import_printf("C %s/%s\n", import_repository, cf->file_path);
	} else {
		import_printf("U %s/%s\n", import_repository, cf->file_path);
	}

	if (cvs_noexec == 1)
		return;

	d = import_get_rcsdiff(cf, rev);
	newrev = rcsnum_inc(rev);

	if (rcs_rev_add(cf->file_rcs, newrev, logmsg, -1, NULL) == -1)
		fatal("import_update: failed to add new revision");

	if (rcs_deltatext_set(cf->file_rcs, newrev, d) == -1)
		fatal("import_update: failed to set deltatext");

	import_tag(cf, brev, newrev);

	if (kflag)
		rcs_kwexp_set(cf->file_rcs, kflag);

	rcsnum_free(brev);
	rcs_write(cf->file_rcs);
}
Exemplo n.º 3
0
void
cvs_diff_local(struct cvs_file *cf)
{
	BUF *b1;
	int fd1, fd2;
	struct stat st;
	struct timespec tv[2], tv2[2];
	struct tm datetm;
	char rbuf[CVS_REV_BUFSZ], tbuf[CVS_TIME_BUFSZ], *p1, *p2;

	b1 = NULL;
	fd1 = fd2 = -1;
	p1 = p2 = NULL;

	cvs_log(LP_TRACE, "cvs_diff_local(%s)", cf->file_path);

	if (cf->file_type == CVS_DIR) {
		if (verbosity > 1)
			cvs_log(LP_ERR, "Diffing inside %s", cf->file_path);
		return;
	}

	cvs_file_classify(cf, cvs_directory_tag);

	if (cvs_cmdop == CVS_OP_DIFF) {
		if (cf->file_ent == NULL) {
			cvs_log(LP_ERR, "I know nothing about %s",
			    cf->file_path);
			return;
		}

		switch (cf->file_ent->ce_status) {
		case CVS_ENT_ADDED:
			if (Nflag == 0) {
				cvs_log(LP_ERR, "%s is a new entry, no "
				    "comparison available", cf->file_path);
				return;
			}
			if (!(cf->file_flags & FILE_ON_DISK)) {
				cvs_log(LP_ERR, "cannot find %s",
				    cf->file_path);
				return;
			}
			break;
		case CVS_ENT_REMOVED:
			if (Nflag == 0) {
				cvs_log(LP_ERR, "%s was removed, no "
				    "comparison available", cf->file_path);
				return;
			}
			if (cf->file_rcs == NULL) {
				cvs_log(LP_ERR, "cannot find RCS file for %s",
				    cf->file_path);
				return;
			}
			break;
		default:
			if (!(cf->file_flags & FILE_ON_DISK)) {
				cvs_printf("? %s\n", cf->file_path);
				return;
			}

			if (cf->file_rcs == NULL) {
				cvs_log(LP_ERR, "cannot find RCS file for %s",
				    cf->file_path);
				return;
			}
			break;
		}
	}

	if (cf->file_status == FILE_UPTODATE && rev1 == NULL && rev2 == NULL &&
	    date1 == -1 && date2 == -1)
		return;

	if (cf->file_rcs != NULL && cf->file_rcs->rf_head == NULL) {
		cvs_log(LP_ERR, "no head revision in RCS file for %s\n",
		    cf->file_path);
		return;
	}

	if (kflag && cf->file_rcs != NULL)
		rcs_kwexp_set(cf->file_rcs, kflag);

	if (cf->file_rcs == NULL)
		diff_rev1 = NULL;
	else if (rev1 != NULL || date1 != -1) {
		cvs_specified_date = date1;
		diff_rev1 = rcs_translate_tag(rev1, cf->file_rcs);
		if (diff_rev1 == NULL && cvs_cmdop == CVS_OP_DIFF) {
			if (rev1 != NULL) {
				cvs_log(LP_ERR, "tag %s not in file %s", rev1,
				    cf->file_path);
				goto cleanup;
			} else if (Nflag) {
				diff_rev1 = NULL;
			} else {
				gmtime_r(&cvs_specified_date, &datetm);
				strftime(tbuf, sizeof(tbuf),
				    "%Y.%m.%d.%H.%M.%S", &datetm);
				cvs_log(LP_ERR, "no revision for date %s in "
				    "file %s", tbuf, cf->file_path);
				goto cleanup;
			}
		} else if (diff_rev1 == NULL && cvs_cmdop == CVS_OP_RDIFF &&
		    force_head) {
			/* -f is not allowed for unknown symbols */
			if ((diff_rev1 = rcsnum_parse(rev1)) == NULL)
				fatal("no such tag %s", rev1);
			rcsnum_free(diff_rev1);

			diff_rev1 = cf->file_rcs->rf_head;
		}
		cvs_specified_date = -1;
	} else if (cvs_cmdop == CVS_OP_DIFF) {
		if (cf->file_ent->ce_status == CVS_ENT_ADDED)
			diff_rev1 = NULL;
		else
			diff_rev1 = cf->file_ent->ce_rev;
	}

	if (cf->file_rcs == NULL)
		diff_rev2 = NULL;
	else if (rev2 != NULL || date2 != -1) {
		cvs_specified_date = date2;
		diff_rev2 = rcs_translate_tag(rev2, cf->file_rcs);
		if (diff_rev2 == NULL && cvs_cmdop == CVS_OP_DIFF) {
			if (rev2 != NULL) {
				cvs_log(LP_ERR, "tag %s not in file %s", rev2,
				    cf->file_path);
				goto cleanup;
			} else if (Nflag) {
				diff_rev2 = NULL;
			} else {
				gmtime_r(&cvs_specified_date, &datetm);
				strftime(tbuf, sizeof(tbuf),
				    "%Y.%m.%d.%H.%M.%S", &datetm);
				cvs_log(LP_ERR, "no revision for date %s in "
				    "file %s", tbuf, cf->file_path);
				goto cleanup;
			}
		} else if (diff_rev2 == NULL && cvs_cmdop == CVS_OP_RDIFF &&
		    force_head) {
			/* -f is not allowed for unknown symbols */
			if ((diff_rev2 = rcsnum_parse(rev2)) == NULL)
				fatal("no such tag %s", rev2);
			rcsnum_free(diff_rev2);

			diff_rev2 = cf->file_rcs->rf_head;
		}
		cvs_specified_date = -1;
	} else if (cvs_cmdop == CVS_OP_RDIFF)
		diff_rev2 = cf->file_rcs->rf_head;
	else if (cf->file_ent->ce_status == CVS_ENT_REMOVED)
		diff_rev2 = NULL;

	if (diff_rev1 != NULL && diff_rev2 != NULL &&
	    rcsnum_cmp(diff_rev1, diff_rev2, 0) == 0)
		goto cleanup;

	switch (cvs_cmdop) {
	case CVS_OP_DIFF:
		if (cf->file_status == FILE_UPTODATE) {
			if (diff_rev2 == NULL &&
			    !rcsnum_cmp(diff_rev1, cf->file_rcsrev, 0))
				goto cleanup;
		}
		break;
	case CVS_OP_RDIFF:
		if (diff_rev1 == NULL && diff_rev2 == NULL)
			goto cleanup;
		break;
	}

	cvs_printf("Index: %s\n", cf->file_path);
	if (cvs_cmdop == CVS_OP_DIFF)
		cvs_printf("%s\nRCS file: %s\n", RCS_DIFF_DIV,
		    cf->file_rcs != NULL ? cf->file_rpath : cf->file_path);

	if (diff_rev1 != NULL) {
		if (cvs_cmdop == CVS_OP_DIFF && diff_rev1 != NULL) {
			(void)rcsnum_tostr(diff_rev1, rbuf, sizeof(rbuf));
			cvs_printf("retrieving revision %s\n", rbuf);
		}

		tv[0].tv_sec = rcs_rev_getdate(cf->file_rcs, diff_rev1);
		tv[0].tv_nsec = 0;
		tv[1] = tv[0];

		(void)xasprintf(&p1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
		fd1 = rcs_rev_write_stmp(cf->file_rcs, diff_rev1, p1, 0);
		if (futimens(fd1, tv) == -1)
			fatal("cvs_diff_local: futimens failed");
	}

	if (diff_rev2 != NULL) {
		if (cvs_cmdop == CVS_OP_DIFF && rev2 != NULL) {
			(void)rcsnum_tostr(diff_rev2, rbuf, sizeof(rbuf));
			cvs_printf("retrieving revision %s\n", rbuf);
		}

		tv2[0].tv_sec = rcs_rev_getdate(cf->file_rcs, diff_rev2);
		tv2[0].tv_nsec = 0;
		tv2[1] = tv2[0];

		(void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
		fd2 = rcs_rev_write_stmp(cf->file_rcs, diff_rev2, p2, 0);
		if (futimens(fd2, tv2) == -1)
			fatal("cvs_diff_local: futimens failed");
	} else if (cvs_cmdop == CVS_OP_DIFF &&
	    (cf->file_flags & FILE_ON_DISK) &&
	    cf->file_ent->ce_status != CVS_ENT_REMOVED) {
		(void)xasprintf(&p2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
		if (cvs_server_active == 1 && cf->fd == -1) {
			tv2[0].tv_sec = rcs_rev_getdate(cf->file_rcs,
			    cf->file_ent->ce_rev);
			tv2[0].tv_nsec = 0;
			tv2[1] = tv2[0];

			fd2 = rcs_rev_write_stmp(cf->file_rcs,
			    cf->file_ent->ce_rev, p2, 0);
			if (futimens(fd2, tv2) == -1)
				fatal("cvs_diff_local: futimens failed");
		} else {
			if (fstat(cf->fd, &st) == -1)
				fatal("fstat failed %s", strerror(errno));
			b1 = buf_load_fd(cf->fd);

			tv2[0].tv_sec = st.st_mtime;
			tv2[0].tv_nsec = 0;
			tv2[1] = tv2[0];

			fd2 = buf_write_stmp(b1, p2, tv2);
			buf_free(b1);
		}
	}

	switch (cvs_cmdop) {
	case CVS_OP_DIFF:
		cvs_printf("%s", diffargs);

		if (rev1 != NULL && diff_rev1 != NULL) {
			(void)rcsnum_tostr(diff_rev1, rbuf, sizeof(rbuf));
			cvs_printf(" -r%s", rbuf);

			if (rev2 != NULL && diff_rev2 != NULL) {
				(void)rcsnum_tostr(diff_rev2, rbuf,
				    sizeof(rbuf));
				cvs_printf(" -r%s", rbuf);
			}
		}

		if (diff_rev2 == NULL)
			cvs_printf(" %s", cf->file_path);
		cvs_printf("\n");
		break;
	case CVS_OP_RDIFF:
		cvs_printf("diff ");
		switch (diff_format) {
		case D_CONTEXT:
			cvs_printf("-c ");
			break;
		case D_RCSDIFF:
			cvs_printf("-n ");
			break;
		case D_UNIFIED:
			cvs_printf("-u ");
			break;
		default:
			break;
		}
		if (diff_rev1 == NULL) {
			cvs_printf("%s ", CVS_PATH_DEVNULL);
		} else {
			(void)rcsnum_tostr(diff_rev1, rbuf, sizeof(rbuf));
			cvs_printf("%s:%s ", cf->file_path, rbuf);
		}

		if (diff_rev2 == NULL) {
			cvs_printf("%s:removed\n", cf->file_path);
		} else {
			(void)rcsnum_tostr(diff_rev2 != NULL ? diff_rev2 :
			    cf->file_rcs->rf_head, rbuf, sizeof(rbuf));
			cvs_printf("%s:%s\n", cf->file_path, rbuf);
		}
		break;
	}

	if (fd1 == -1) {
		if ((fd1 = open(CVS_PATH_DEVNULL, O_RDONLY, 0)) == -1)
			fatal("cannot open %s", CVS_PATH_DEVNULL);
	}
	if (fd2 == -1) {
		if ((fd2 = open(CVS_PATH_DEVNULL, O_RDONLY, 0)) == -1)
			fatal("cannot open %s", CVS_PATH_DEVNULL);
	}

	if (diffreg(p1 != NULL ? cf->file_path : CVS_PATH_DEVNULL,
	    p2 != NULL ? cf->file_path : CVS_PATH_DEVNULL, fd1, fd2, NULL,
	    dflags) == D_ERROR)
		fatal("cvs_diff_local: failed to get RCS patch");

	close(fd1);
	close(fd2);

	worklist_run(&temp_files, worklist_unlink);

	if (p1 != NULL)
		xfree(p1);
	if (p2 != NULL)
		xfree(p2);

cleanup:
	if (diff_rev1 != NULL &&
	    (cf->file_rcs == NULL || diff_rev1 != cf->file_rcs->rf_head) &&
	    (cf->file_ent == NULL || diff_rev1 != cf->file_ent->ce_rev))
		xfree(diff_rev1);
	diff_rev1 = NULL;

	if (diff_rev2 != NULL &&
	    (cf->file_rcs == NULL || diff_rev2 != cf->file_rcs->rf_head))
		xfree(diff_rev2);
	diff_rev2 = NULL;
}