Skip to content

Instantly share code, notes, and snippets.

@itochan
Last active January 16, 2016 07:40
Show Gist options
  • Save itochan/e8a9552a2d42bae929e0 to your computer and use it in GitHub Desktop.
Save itochan/e8a9552a2d42bae929e0 to your computer and use it in GitHub Desktop.
syspro_final

dvn: Date version control

日付によるバージョン管理システム

バージョン管理といえばプログラムの開発に必要不可欠なシステムであるが、バージョン管理システムを使いこなすのは難しい。そこで、古くからバージョン管理の手法として用いられてきた日付によるディレクトリ構造でバージョン管理を行うツールを開発した。

機能

dvn init
dvn commit [message] [file]
dvn log

コミットを行うと、はじめにコミット時点の日付と時刻でディレクトリを作る。対象のファイルをそのディレクトリにコピーをするというシンプルなものだ。コミットメッセージは日付ディレクトリの .dvn.metadata というファイルに保存される。

dvn log というコマンドでコミット履歴を参照することができる。表示は、コミット日時とコミットメッセージのみの単純なものだ。ファイルの比較は diff コマンドを用いて行う。極力シンプルなツールを目指したため本体に実装していない。

これでバージョン管理をしたい人はこのツールでバージョン管理すればよく、バージョン管理システムがわからない人はこのディレクトリを開けばその時点のファイルを開けるようになる。

なお、このシステムの開発は Git を用いてバージョン管理されている。プログラム名の dvn は、QWERTY 配列上ではキーの配置の関係で Subversion の svn コマンドと間違われやすいため、あえてこの名前をつけた。

The MIT License (MIT)
Copyright (c) 2016 Kazunori Jo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#define VCS_DIR ".dvn"
#define METADATA ".dvn.metadata"
void usage();
void cmd_init();
void cmd_commit(char *message, int files_count, char *files[]);
void cmd_log();
int copy(char *source_name, char *destination_name);
int main (int argc, char *argv[]) {
if (argc == 1) {
usage();
return 0;
}
const char *action = argv[1];
if (strcmp(action, "init") == 0) {
cmd_init();
} else if (strcmp(action, "commit") == 0 && argc >= 4) {
int file_argc = argc - 3;
char *files[file_argc];
int j = 3;
for (int i = 0; i < file_argc; i++) {
files[i] = argv[j];
j++;
}
cmd_commit(argv[2], file_argc, files);
} else if (strcmp(action, "log") == 0) {
cmd_log();
} else {
usage();
}
return 0;
}
void usage() {
printf("dvn - Date version control\n\n");
printf("Usage:\n");
printf("\tdvn init\n");
printf("\tdvn commit [message] [file]\n");
printf("\tdvn log\n");
}
void cmd_init() {
if (mkdir(VCS_DIR, 0755) == 0) {
printf("Initialized dvn repository.\n");
printf("Recommend: Initial commit `dvn commit 'Initial' *`\n");
} else {
perror(VCS_DIR);
}
}
void cmd_commit(char *message, int files_count, char *files[]) {
const int DATE_LENGTH = 25;
char date[DATE_LENGTH];
time_t now = time(NULL);
strftime(date, DATE_LENGTH, "%FT%T%z", localtime(&now));
char commit_dir[PATH_MAX] = VCS_DIR;
sprintf(commit_dir, "%s/%s", VCS_DIR, date);
int error = mkdir(commit_dir, 0755);
if (error != 0) {
perror(commit_dir);
exit(EXIT_FAILURE);
}
for (int i = 0; i < files_count; i++) {
char commit_file[PATH_MAX];
char *file = files[i];
sprintf(commit_file, "%s/%s", commit_dir, file);
error = copy(file, commit_file);
if (error != 0) {
rmdir(commit_dir);
perror(commit_dir);
exit(EXIT_FAILURE);
}
}
char metadata_path[PATH_MAX];
sprintf(metadata_path, "%s/%s", commit_dir, METADATA);
FILE *metadata = fopen(metadata_path, "w");
fputs(message, metadata);
fclose(metadata);
printf("Commit %s: %s\n", date, message);
}
void cmd_log() {
DIR *dir;
struct dirent *entry;
if ((dir = opendir(VCS_DIR)) == NULL) {
perror("Commit dir");
exit(EXIT_FAILURE);
}
for (entry = readdir(dir); entry != NULL; entry = readdir(dir)){
char *name = entry->d_name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
}
char metadata_path[PATH_MAX];
char commit_msg[256];
sprintf(metadata_path, "%s/%s/%s", VCS_DIR, name, METADATA);
FILE *metadata = fopen(metadata_path, "r");
fgets(commit_msg, sizeof(commit_msg), metadata);
fclose(metadata);
printf("%s: %s\n", name, commit_msg);
}
closedir(dir);
}
int copy(char *source_name, char *destination_name) {
struct stat path_stat;
stat(source_name, &path_stat);
if (S_ISDIR(path_stat.st_mode)) {
DIR *dir = opendir(source_name);
struct dirent *entry;
mkdir(destination_name, 0755);
for (entry = readdir(dir); entry != NULL; entry = readdir(dir)) {
char *name = entry->d_name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
}
char source[PATH_MAX];
char destination[PATH_MAX];
sprintf(source, "%s/%s", source_name, name);
sprintf(destination, "%s/%s", destination_name, name);
copy(source, destination);
}
closedir(dir);
return 0;
}
FILE *source = fopen(source_name, "rb");
if (source == NULL) {
fclose(source);
return 1;
}
FILE *destination = fopen(destination_name, "wb");
if (destination == NULL) {
fclose(destination);
return 1;
}
int i;
for (i = getc(source); i != EOF; i = getc(source)) {
putc(i, destination);
}
fclose(destination);
fclose(source);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment