From de5b97db09c5b46291e3f8eb51012b229610628f Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 5 Jun 2015 11:53:32 -0300 Subject: [PATCH 01/18] WIP: First spike at a go version. --- gb.go | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 gb.go diff --git a/gb.go b/gb.go new file mode 100644 index 0000000..91cd1cc --- /dev/null +++ b/gb.go @@ -0,0 +1,183 @@ +package main + +import( + "time" + "os" + "fmt" + "github.com/libgit2/git2go" +) + +const ( + Red string = "\x1b[0;31m" + Yellow string = "\x1b[0;33m" + Green string = "\x1b[0;32m" + + BaseBranch string = "master" +) + + +func GetRepo() (*git.Repository) { + repo, err := git.OpenRepository(".") + if err != nil { + // @todo improve message + fmt.Printf("Could not open repository at '.'\n") + os.Exit(1) + } + return repo +} + +func GetBranchIterator(repo *git.Repository) (*git.BranchIterator) { + i, err := repo.NewBranchIterator(git.BranchLocal) + if err != nil { + // @todo improve message + fmt.Printf("Can't list branches\n") + os.Exit(1) + } + return i +} + +func GetBaseOid(repo *git.Repository) (*git.Oid) { + base_branch, err := repo.LookupBranch(BaseBranch, git.BranchLocal) + if err != nil { + fmt.Printf("Error looking up %s\n", BaseBranch) + os.Exit(1) + } + + return base_branch.Target() +} + + +type Comparison struct { + Repo *git.Repository + BaseOid *git.Oid + Branch *git.Branch + Oid *git.Oid + + _ahead int + _behind int +} + +func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) *Comparison{ + c := new(Comparison) + + c.Repo = repo + c.BaseOid = base_oid + + c.Branch = branch + c.Oid = branch.Target() + + c._ahead = -1 + c._behind = -1 + + return c +} + +func (c Comparison) Name() string { + name, err := c.Branch.Name() + if err != nil { + fmt.Printf("Can't get branch name\n") + os.Exit(1) + } + return name +} + +func (c Comparison) IsHead() bool { + head, err := c.Branch.IsHead() + if err != nil { + fmt.Printf("Can't get IsHead\n") + os.Exit(1) + } + return head +} + + +func (c Comparison) IsMerged() bool { + if c.Oid.String() == c.BaseOid.String() { + return true + } else { + merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid) + if err != nil { + fmt.Printf("Could not get descendant of '%s' and '%s'.\n", c.BaseOid, c.Oid) + os.Exit(1) + } + return merged + } +} + +func (c Comparison) Commit() *git.Commit { + commit, err := c.Repo.LookupCommit(c.Oid) + if err != nil { + fmt.Printf("Could not lookup commit '%s'.\n", c.Oid) + os.Exit(1) + } + return commit +} + +// @todo red for old commits +func (c Comparison) Color() string { + if c.IsHead() { + return Green + } else { + return Yellow + } +} + +func (c Comparison) When() time.Time { + sig := c.Commit().Committer() + return sig.When +} + +func (c *Comparison) Ahead() int { + c.ComputeAheadBehind() + return c._ahead +} + +func (c *Comparison) Behind() int { + c.ComputeAheadBehind() + return c._behind +} + +func (c *Comparison) ComputeAheadBehind() { + if (c._ahead > -1 && c._behind > -1) { return } + + var err error + c._ahead, c._behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid) + if err != nil { + fmt.Printf("Error getting ahead/behind\n", c.BaseOid) + os.Exit(1) + } +} + + +func main() { + repo := GetRepo() + branch_iterator := GetBranchIterator(repo) + base_oid := GetBaseOid(repo) + + // comparisons := [...]Comparison + + + // type BranchIteratorFunc func(*Branch, BranchType) error + branch_iterator.ForEach( func(branch *git.Branch, btype git.BranchType) error { + comp := NewComparison(repo, base_oid, branch) + + merged_string := "" + if comp.IsMerged() { + merged_string = "(merged)" + } + + fmt.Printf( + "%s%s | %-30s | behind: %4d | ahead: %4d %s\n", + comp.Color(), + comp.When().Format("2006-01-02 15:04"), + comp.Name(), + comp.Behind(), + comp.Ahead(), + merged_string) + + return nil + }) + + // @todo store all comparisons in a list that can be sorted before printing. + // @todo filter them things +} From 333cf51a06007712baf499f48716f810bfbde618 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 5 Jun 2015 16:43:46 -0300 Subject: [PATCH 02/18] WIP: go version --- gb.go => src/github.com/vroy/gb/gb.go | 84 +++--- src/main.c | 384 -------------------------- 2 files changed, 41 insertions(+), 427 deletions(-) rename gb.go => src/github.com/vroy/gb/gb.go (68%) delete mode 100644 src/main.c diff --git a/gb.go b/src/github.com/vroy/gb/gb.go similarity index 68% rename from gb.go rename to src/github.com/vroy/gb/gb.go index 91cd1cc..e1c82d8 100644 --- a/gb.go +++ b/src/github.com/vroy/gb/gb.go @@ -1,22 +1,24 @@ package main -import( - "time" - "os" +import ( "fmt" - "github.com/libgit2/git2go" + "os" + "time" + + git "github.com/libgit2/git2go" ) +type ColorType string + const ( - Red string = "\x1b[0;31m" - Yellow string = "\x1b[0;33m" - Green string = "\x1b[0;32m" + Red ColorType = "\x1b[0;31m" + Yellow = "\x1b[0;33m" + Green = "\x1b[0;32m" BaseBranch string = "master" ) - -func GetRepo() (*git.Repository) { +func NewRepo() *git.Repository { repo, err := git.OpenRepository(".") if err != nil { // @todo improve message @@ -26,7 +28,7 @@ func GetRepo() (*git.Repository) { return repo } -func GetBranchIterator(repo *git.Repository) (*git.BranchIterator) { +func NewBranchIterator(repo *git.Repository) *git.BranchIterator { i, err := repo.NewBranchIterator(git.BranchLocal) if err != nil { // @todo improve message @@ -36,7 +38,7 @@ func GetBranchIterator(repo *git.Repository) (*git.BranchIterator) { return i } -func GetBaseOid(repo *git.Repository) (*git.Oid) { +func LookupBaseOid(repo *git.Repository) *git.Oid { base_branch, err := repo.LookupBranch(BaseBranch, git.BranchLocal) if err != nil { fmt.Printf("Error looking up %s\n", BaseBranch) @@ -46,18 +48,17 @@ func GetBaseOid(repo *git.Repository) (*git.Oid) { return base_branch.Target() } - type Comparison struct { - Repo *git.Repository + Repo *git.Repository BaseOid *git.Oid - Branch *git.Branch - Oid *git.Oid + Branch *git.Branch + Oid *git.Oid - _ahead int - _behind int + ahead int + behind int } -func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) *Comparison{ +func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) *Comparison { c := new(Comparison) c.Repo = repo @@ -66,8 +67,8 @@ func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) c.Branch = branch c.Oid = branch.Target() - c._ahead = -1 - c._behind = -1 + c.ahead = -1 + c.behind = -1 return c } @@ -90,18 +91,17 @@ func (c Comparison) IsHead() bool { return head } - func (c Comparison) IsMerged() bool { - if c.Oid.String() == c.BaseOid.String() { - return true - } else { - merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid) - if err != nil { - fmt.Printf("Could not get descendant of '%s' and '%s'.\n", c.BaseOid, c.Oid) - os.Exit(1) - } - return merged + if c.Oid.String() == c.BaseOid.String() { + return true + } else { + merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid) + if err != nil { + fmt.Printf("Could not get descendant of '%s' and '%s'.\n", c.BaseOid, c.Oid) + os.Exit(1) } + return merged + } } func (c Comparison) Commit() *git.Commit { @@ -114,7 +114,7 @@ func (c Comparison) Commit() *git.Commit { } // @todo red for old commits -func (c Comparison) Color() string { +func (c Comparison) Color() ColorType { if c.IsHead() { return Green } else { @@ -129,36 +129,34 @@ func (c Comparison) When() time.Time { func (c *Comparison) Ahead() int { c.ComputeAheadBehind() - return c._ahead + return c.ahead } func (c *Comparison) Behind() int { c.ComputeAheadBehind() - return c._behind + return c.behind } func (c *Comparison) ComputeAheadBehind() { - if (c._ahead > -1 && c._behind > -1) { return } + if c.ahead > -1 && c.behind > -1 { + return + } var err error - c._ahead, c._behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid) + c.ahead, c.behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid) if err != nil { fmt.Printf("Error getting ahead/behind\n", c.BaseOid) os.Exit(1) } } - func main() { - repo := GetRepo() - branch_iterator := GetBranchIterator(repo) - base_oid := GetBaseOid(repo) - - // comparisons := [...]Comparison - + repo := NewRepo() + branch_iterator := NewBranchIterator(repo) + base_oid := LookupBaseOid(repo) // type BranchIteratorFunc func(*Branch, BranchType) error - branch_iterator.ForEach( func(branch *git.Branch, btype git.BranchType) error { + branch_iterator.ForEach(func(branch *git.Branch, btype git.BranchType) error { comp := NewComparison(repo, base_oid, branch) merged_string := "" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index a4daa7d..0000000 --- a/src/main.c +++ /dev/null @@ -1,384 +0,0 @@ -// IO stuff, output, reading files, etc. -#include - -// atoi for arguments parsing, qsort, malloc, and more? -#include - -// String utilities like strcmp, strcpy, etc -#include - -// git bindings -#include - -// JSON library -#include - -// POSIX API. For gwtcwd. -#include - -// To have a reference to MAXPATHLEN -#include - -// Parse them options -#include - - -// Uncomment to compile with debugging output turned on. -// #define DEBUG 1 - -//Define bool type -typedef int bool; -enum { false, true }; - - -typedef struct gb_options { - int ahead_filter; - int behind_filter; - int help_flag; - int merged_flag; - int no_merged_flag; - int clear_cache_flag; -} gb_options; - -typedef struct gb_comparison { - char tip[41]; - char master_tip[41]; - git_oid tip_oid; - git_oid master_oid; - char name[200]; - char reference_name[200]; - long timestamp; - size_t ahead; - size_t behind; - int is_head; - size_t is_merged; -} gb_comparison; - - -// Globals are evil, but this is a short-live program that will never change -// context while it's running. -git_repository *gb_repo; -json_t *gb_json; -char *gb_cache_path; -gb_options *options; - - -void gb_options_init(int argc, char **argv) { - options = malloc(sizeof(gb_options)); - - // Set defaults - options->ahead_filter = -1; - options->behind_filter = -1; - options->help_flag = 0; - options->merged_flag = 0; - options->no_merged_flag = 0; - options->clear_cache_flag = 0; - - - int opt; - - while (1) { - struct option long_options[] = { - { "merged", no_argument, &options->merged_flag, 1 }, - { "no-merged", no_argument, &options->no_merged_flag, 1 }, - { "clear-cache", no_argument, &options->clear_cache_flag, 1 }, - { "help", no_argument, &options->help_flag, 1 } - }; - - int option_index = 0; - opt = getopt_long(argc, argv, "a:b:h", long_options, &option_index); - - switch(opt) { - case 'a': - options->ahead_filter = atoi(optarg); - break; - case 'b': - options->behind_filter = atoi(optarg); - break; - case 'h': - case '?': - options->help_flag = 1; - break; - } - - if (opt == -1) break; - } -} - - - -char *RED = "\e[0;31m"; -char *YELLOW = "\e[0;33m"; -char *GREEN = "\e[0;32m"; - -void gb_git_check_return(int rc, char *msg) { - if (rc != 0) { - fprintf(stderr, "%s. Code: %d\n", msg, rc); - exit(1); - } -} - - - - - -void gb_comparison_new(git_reference *ref, gb_comparison *comp) { - int rc; - - memset(comp->tip, '\0', 41); - memset(comp->master_tip, '\0', 41); - - // Find branch name. - const char *name; - git_branch_name(&name, ref); - memset(comp->name, '\0', 200); - strcpy(comp->name, name); - - // Assign full reference name. - memset(comp->reference_name, '\0', 200); - strcat(comp->reference_name, "refs/heads/"); - strcat(comp->reference_name, comp->name); - - // Choose color of output. - comp->is_head = git_branch_is_head(ref); - - // Find tip oid. - rc = git_reference_name_to_id(&comp->tip_oid, gb_repo, comp->reference_name); - gb_git_check_return(rc, "Can't find branch tip id."); - git_oid_tostr(comp->tip, 41, &comp->tip_oid); - - // Keep reference to master_tip that we're comparing to. - rc = git_reference_name_to_id(&comp->master_oid, gb_repo, "refs/heads/master"); - gb_git_check_return(rc, "Can't find branch tip id."); - git_oid_tostr(comp->master_tip, 41, &comp->master_oid); - - // Find commit based on tip oid. - git_commit *commit; - git_commit_lookup(&commit, gb_repo, &(comp->tip_oid)); - - // Assign timestamp. - comp->timestamp = git_commit_time(commit); - - comp->ahead = 0; - comp->behind = 0; - comp->is_merged = 0; -} - - -int gb_comparison_asc_timestamp_sort(const void *a, const void *b) { - gb_comparison *x = *(gb_comparison **) a; - gb_comparison *y = *(gb_comparison **) b; - - if (x->timestamp < y->timestamp) return 1; - if (x->timestamp > y->timestamp) return -1; - - return 0; -} - -int gb_comparison_desc_timestamp_sort(const void *a, const void *b) { - gb_comparison *x = *(gb_comparison **) a; - gb_comparison *y = *(gb_comparison **) b; - - if (x->timestamp > y->timestamp) return 1; - if (x->timestamp < y->timestamp) return -1; - - return 0; -} - - -void gb_comparison_execute(gb_comparison *comp) { - char *range = malloc( (strlen(comp->tip) + strlen(comp->master_tip) + 3) * sizeof(char)); - sprintf(range, "%s..%s", comp->tip, comp->master_tip); - - json_t *object = json_object_get(gb_json, range); - if (json_is_object(object)) { - json_unpack(object, "{sIsIsI}", - "ahead", &comp->ahead, - "behind", &comp->behind, - "is_merged", &comp->is_merged); - - } else { - // Same behaviour for is_merged as `git branch --merged` - comp->is_merged = git_graph_descendant_of(gb_repo, &comp->master_oid, &comp->tip_oid) || git_oid_equal(&comp->master_oid, &comp->tip_oid); - - git_graph_ahead_behind(&comp->ahead, &comp->behind, gb_repo, &comp->tip_oid, &comp->master_oid); - - json_object_set(gb_json, range, json_pack("{sIsIsI}", "ahead", comp->ahead, "behind", comp->behind, "is_merged", comp->is_merged)); - } -} - - -// Returns a pointer to the color constant based on some very basic rules. -char* gb_output_color(gb_comparison *comp) { - if ( !isatty(STDOUT_FILENO) ) return ""; - - time_t rawtime = comp->timestamp; - time_t now = time(0); - int one_week = (14 * 24 * 60 * 60); - - if (comp->is_head) { - return GREEN; - } else if ( rawtime > (now - one_week) ) { - return YELLOW; - } else { - return RED; - } -} - -void gb_comparison_print(gb_comparison *comp) { - char formatted_time[80]; - time_t rawtime = comp->timestamp; - struct tm * timeinfo = localtime(&rawtime); - strftime(formatted_time, 80, "%F %H:%M%p", timeinfo); - - char merge_status[10]; - memset(merge_status, '\0', 9); - - if (comp->is_merged) { - strcpy(merge_status, " (merged)"); - } - - printf("%s%s | %-40.40s | behind: %4lu | ahead: %4lu %s\n", - gb_output_color(comp), - formatted_time, - comp->name, - comp->behind, - comp->ahead, - merge_status); -} - -bool gb_branch_is_filtered(gb_comparison *comp) { - if (options->ahead_filter > -1 && comp->ahead != options->ahead_filter) { - return true; - } - - if (options->behind_filter > -1 && comp->behind != options->behind_filter) { - return true; - } - - if (options->merged_flag && !comp->is_merged) { - return true; - } - - if (options->no_merged_flag && comp->is_merged) { - return true; - } - - return false; -} - - - -void print_last_branches() { - gb_comparison **comps = malloc( sizeof(gb_comparison*) ); - - int branch_count = 0; - - git_branch_iterator *iter; - int rc; - - rc = git_branch_iterator_new(&iter, gb_repo, GIT_BRANCH_LOCAL); - gb_git_check_return(rc, "Can't iterate over branches."); - - git_reference *ref = NULL; - git_branch_t type; - - while (!(rc = git_branch_next(&ref, &type, iter))) { - comps = (gb_comparison**) realloc(comps, (branch_count+1) * sizeof(gb_comparison*)); - gb_comparison *comp = malloc(sizeof(gb_comparison)); - gb_comparison_new(ref, comp); - comps[branch_count] = comp; - branch_count++; - } - - qsort(comps, branch_count, sizeof(*comps), gb_comparison_desc_timestamp_sort); - - for (int i = 0; i < branch_count; i++) { - gb_comparison_execute(comps[i]); - if (!gb_branch_is_filtered(comps[i])) { - gb_comparison_print(comps[i]); - } - } - - git_branch_iterator_free(iter); - -} - -void gb_cache_load() { - json_error_t error; - - if (options->clear_cache_flag) { - gb_json = json_object(); - return; - } - - gb_json = json_load_file(gb_cache_path, 0, &error); - - if (!gb_json) { - #ifdef DEBUG - if (error.line > 0) { - fprintf(stderr, "error: on line %d: %s\n", error.line, error.text); - } else { - fprintf(stderr, "error: %s\n", error.text); - } - #endif - - // If JSON load failed (file does not exist, syntax error, etc), simply - // proceed forward with an empty json_object. - gb_json = json_object(); - } -} - -void gb_cache_dump() { - json_dump_file(gb_json, gb_cache_path, 0); -} - - -git_repository* gb_git_repo_new() { - git_repository *repo; - char cwd[MAXPATHLEN]; - - if (getcwd(cwd, sizeof(cwd)) == NULL) { - fprintf(stderr, "fatal: Could not get current working directory.\n"); - exit(1); - } - - int rc = git_repository_open_ext(&repo, cwd, 0, NULL); - if (rc == GIT_ENOTFOUND) { - fprintf(stderr, "fatal: Not a git repository (or any of the parent directories): .git\n"); - exit(1); - } - - gb_git_check_return(rc, "opening repository"); - - return repo; -} - - - - -int main(int argc, char **argv) { - git_libgit2_init(); - - gb_options_init(argc, argv); - - if (options->help_flag) { - system("man gb"); - return 0; - } - - // First thing we do is init/load the globals. - gb_repo = gb_git_repo_new(); - - gb_cache_path = malloc(MAXPATHLEN * sizeof(char)); - sprintf(gb_cache_path, "%sgb_cache.json", git_repository_path(gb_repo)); - - gb_cache_load(); - - // Program run. - print_last_branches(); - - gb_cache_dump(); - - return 0; -} From a801452fbe4e5b424f31193aef81b7c7be77c60b Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 5 Jun 2015 16:50:01 -0300 Subject: [PATCH 03/18] Move things around. --- src/github.com/vroy/gb/gb.go => gb.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/github.com/vroy/gb/gb.go => gb.go (100%) diff --git a/src/github.com/vroy/gb/gb.go b/gb.go similarity index 100% rename from src/github.com/vroy/gb/gb.go rename to gb.go From 10fd83da835a3bdedcc5616f460309fe9fa18bba Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sat, 6 Jun 2015 11:58:34 -0300 Subject: [PATCH 04/18] Use pointer receiver everywhere. Use helper exit function. --- gb.go | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/gb.go b/gb.go index e1c82d8..fe14715 100644 --- a/gb.go +++ b/gb.go @@ -18,12 +18,16 @@ const ( BaseBranch string = "master" ) +func exit(msg string, args ...string) { + fmt.Printf(msg, args) + os.Exit(1) +} + func NewRepo() *git.Repository { repo, err := git.OpenRepository(".") if err != nil { // @todo improve message - fmt.Printf("Could not open repository at '.'\n") - os.Exit(1) + exit("Could not open repository at '.'\n") } return repo } @@ -32,8 +36,7 @@ func NewBranchIterator(repo *git.Repository) *git.BranchIterator { i, err := repo.NewBranchIterator(git.BranchLocal) if err != nil { // @todo improve message - fmt.Printf("Can't list branches\n") - os.Exit(1) + exit("Can't list branches\n") } return i } @@ -41,8 +44,7 @@ func NewBranchIterator(repo *git.Repository) *git.BranchIterator { func LookupBaseOid(repo *git.Repository) *git.Oid { base_branch, err := repo.LookupBranch(BaseBranch, git.BranchLocal) if err != nil { - fmt.Printf("Error looking up %s\n", BaseBranch) - os.Exit(1) + exit("Error looking up %s\n", BaseBranch) } return base_branch.Target() @@ -73,48 +75,44 @@ func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) return c } -func (c Comparison) Name() string { +func (c *Comparison) Name() string { name, err := c.Branch.Name() if err != nil { - fmt.Printf("Can't get branch name\n") - os.Exit(1) + exit("Can't get branch name\n") } return name } -func (c Comparison) IsHead() bool { +func (c *Comparison) IsHead() bool { head, err := c.Branch.IsHead() if err != nil { - fmt.Printf("Can't get IsHead\n") - os.Exit(1) + exit("Can't get IsHead\n") } return head } -func (c Comparison) IsMerged() bool { +func (c *Comparison) IsMerged() bool { if c.Oid.String() == c.BaseOid.String() { return true } else { merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid) if err != nil { - fmt.Printf("Could not get descendant of '%s' and '%s'.\n", c.BaseOid, c.Oid) - os.Exit(1) + exit("Could not get descendant of '%s' and '%s'.\n", c.BaseOid.String(), c.Oid.String()) } return merged } } -func (c Comparison) Commit() *git.Commit { +func (c *Comparison) Commit() *git.Commit { commit, err := c.Repo.LookupCommit(c.Oid) if err != nil { - fmt.Printf("Could not lookup commit '%s'.\n", c.Oid) - os.Exit(1) + exit("Could not lookup commit '%s'.\n", c.Oid.String()) } return commit } // @todo red for old commits -func (c Comparison) Color() ColorType { +func (c *Comparison) Color() ColorType { if c.IsHead() { return Green } else { @@ -122,7 +120,7 @@ func (c Comparison) Color() ColorType { } } -func (c Comparison) When() time.Time { +func (c *Comparison) When() time.Time { sig := c.Commit().Committer() return sig.When } @@ -145,8 +143,7 @@ func (c *Comparison) ComputeAheadBehind() { var err error c.ahead, c.behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid) if err != nil { - fmt.Printf("Error getting ahead/behind\n", c.BaseOid) - os.Exit(1) + exit("Error getting ahead/behind\n", c.BaseOid.String()) } } From bdace8bacf25c12384b3f7085ec466a082672c7e Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sun, 7 Jun 2015 11:28:44 -0300 Subject: [PATCH 05/18] Add sorting. --- gb.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/gb.go b/gb.go index fe14715..54c3135 100644 --- a/gb.go +++ b/gb.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "sort" "time" git "github.com/libgit2/git2go" @@ -125,6 +126,10 @@ func (c *Comparison) When() time.Time { return sig.When } +func (c *Comparison) FormattedWhen() string { + return c.When().Format("2006-01-02 15:04") +} + func (c *Comparison) Ahead() int { c.ComputeAheadBehind() return c.ahead @@ -147,15 +152,31 @@ func (c *Comparison) ComputeAheadBehind() { } } +type ComparisonWhenAsc []*Comparison + +func (a ComparisonWhenAsc) Len() int { return len(a) } +func (a ComparisonWhenAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ComparisonWhenAsc) Less(i, j int) bool { + return a[i].When().Unix() < a[j].When().Unix() +} + func main() { repo := NewRepo() branch_iterator := NewBranchIterator(repo) base_oid := LookupBaseOid(repo) + comparisons := make([]*Comparison, 0) + // type BranchIteratorFunc func(*Branch, BranchType) error branch_iterator.ForEach(func(branch *git.Branch, btype git.BranchType) error { comp := NewComparison(repo, base_oid, branch) + comparisons = append(comparisons, comp) + return nil + }) + + sort.Sort(ComparisonWhenAsc(comparisons)) + for _, comp := range comparisons { merged_string := "" if comp.IsMerged() { merged_string = "(merged)" @@ -164,14 +185,12 @@ func main() { fmt.Printf( "%s%s | %-30s | behind: %4d | ahead: %4d %s\n", comp.Color(), - comp.When().Format("2006-01-02 15:04"), + comp.FormattedWhen(), comp.Name(), comp.Behind(), comp.Ahead(), merged_string) - - return nil - }) + } // @todo store all comparisons in a list that can be sorted before printing. // @todo filter them things From dab77e2d882b1644b61364788a970e38f3ccecf8 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sun, 7 Jun 2015 11:30:00 -0300 Subject: [PATCH 06/18] Add .projectile to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 880ac59..559ecca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ .DS_Store +.projectile From 5f5b254f13493bf2bbda78bdcb09a3dc277b78f9 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sun, 7 Jun 2015 11:31:06 -0300 Subject: [PATCH 07/18] Add built binary and my TODO list. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 559ecca..f9d62d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ build/ .DS_Store .projectile +gb +TODO \ No newline at end of file From 1a59391a4bf4edae65db4575002dea172d624504 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sun, 7 Jun 2015 17:15:05 -0300 Subject: [PATCH 08/18] Add options and filtering. --- gb.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/gb.go b/gb.go index 54c3135..4167b98 100644 --- a/gb.go +++ b/gb.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "os" "sort" @@ -160,7 +161,31 @@ func (a ComparisonWhenAsc) Less(i, j int) bool { return a[i].When().Unix() < a[j].When().Unix() } +type Options struct { + Ahead int + Behind int + Merged bool + NoMerged bool +} + +func NewOptions() *Options { + o := new(Options) + + flag.IntVar(&o.Ahead, "ahead", -1, "help message for ahead") + flag.IntVar(&o.Behind, "behind", -1, "help message for behind") + flag.BoolVar(&o.Merged, "merged", false, "help message for merged") + flag.BoolVar(&o.NoMerged, "no-merged", false, "help message for no-merged") + + flag.Parse() + + return o +} + func main() { + opts := NewOptions() + + fmt.Printf("%s\n", opts) + repo := NewRepo() branch_iterator := NewBranchIterator(repo) base_oid := LookupBaseOid(repo) @@ -182,6 +207,23 @@ func main() { merged_string = "(merged)" } + if opts.Ahead != -1 && opts.Ahead != comp.Ahead() { + continue + } + + if opts.Behind != -1 && opts.Behind != comp.Behind() { + continue + } + + if opts.Merged && !comp.IsMerged() { + continue + } + + if opts.NoMerged && comp.IsMerged() { + continue + } + + // continue fmt.Printf( "%s%s | %-30s | behind: %4d | ahead: %4d %s\n", comp.Color(), From 065de7acc95b8022816407850e2ef16e59a74e44 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sun, 7 Jun 2015 17:30:42 -0300 Subject: [PATCH 09/18] Comparisons type. Update to ComparisonsByWhen type for sort. --- gb.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/gb.go b/gb.go index 4167b98..29788eb 100644 --- a/gb.go +++ b/gb.go @@ -153,11 +153,19 @@ func (c *Comparison) ComputeAheadBehind() { } } -type ComparisonWhenAsc []*Comparison +type Comparisons []*Comparison -func (a ComparisonWhenAsc) Len() int { return len(a) } -func (a ComparisonWhenAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ComparisonWhenAsc) Less(i, j int) bool { +type ComparisonsByWhen Comparisons + +func (a ComparisonsByWhen) Len() int { + return len(a) +} + +func (a ComparisonsByWhen) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} + +func (a ComparisonsByWhen) Less(i, j int) bool { return a[i].When().Unix() < a[j].When().Unix() } @@ -190,7 +198,7 @@ func main() { branch_iterator := NewBranchIterator(repo) base_oid := LookupBaseOid(repo) - comparisons := make([]*Comparison, 0) + comparisons := make(Comparisons, 0) // type BranchIteratorFunc func(*Branch, BranchType) error branch_iterator.ForEach(func(branch *git.Branch, btype git.BranchType) error { @@ -199,7 +207,7 @@ func main() { return nil }) - sort.Sort(ComparisonWhenAsc(comparisons)) + sort.Sort(ComparisonsByWhen(comparisons)) for _, comp := range comparisons { merged_string := "" From e0477ba5556a177d3dcee7341edf8db30462b854 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sun, 7 Jun 2015 17:44:58 -0300 Subject: [PATCH 10/18] Update flag help and manual. --- gb.1 | 4 ++-- gb.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gb.1 b/gb.1 index da53f3b..a75e217 100644 --- a/gb.1 +++ b/gb.1 @@ -7,12 +7,12 @@ gb \- List git branches with additional information about them. .SH SYNOPSIS -gb [--merged] [--no-merged] [-a ] [-b ] +gb [-merged] [-no-merged] [-ahead ] [-behind ] .SH OPTIONS .IP "-a " -only show branches that are commits ahead." +only show branches that are commits ahead. .IP "-b " only show branches that are commits behind. diff --git a/gb.go b/gb.go index 29788eb..082a397 100644 --- a/gb.go +++ b/gb.go @@ -179,10 +179,10 @@ type Options struct { func NewOptions() *Options { o := new(Options) - flag.IntVar(&o.Ahead, "ahead", -1, "help message for ahead") - flag.IntVar(&o.Behind, "behind", -1, "help message for behind") - flag.BoolVar(&o.Merged, "merged", false, "help message for merged") - flag.BoolVar(&o.NoMerged, "no-merged", false, "help message for no-merged") + flag.IntVar(&o.Ahead, "ahead", -1, "only show branches that are commits ahead.") + flag.IntVar(&o.Behind, "behind", -1, "only show branches that are commits behind.") + flag.BoolVar(&o.Merged, "merged", false, "only show branches that are merged.") + flag.BoolVar(&o.NoMerged, "no-merged", false, "only show branches that are not merged.") flag.Parse() From b9c03d3b29013db892b9f32e40375f0a139a75cf Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sun, 7 Jun 2015 17:47:42 -0300 Subject: [PATCH 11/18] Cleanup --- gb.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/gb.go b/gb.go index 082a397..da26b40 100644 --- a/gb.go +++ b/gb.go @@ -192,8 +192,6 @@ func NewOptions() *Options { func main() { opts := NewOptions() - fmt.Printf("%s\n", opts) - repo := NewRepo() branch_iterator := NewBranchIterator(repo) base_oid := LookupBaseOid(repo) From f898c4db5d21faf60cbc85a142223341aa491f21 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 12 Jun 2015 00:46:13 -0300 Subject: [PATCH 12/18] Add JSON caching. --- gb.go | 123 +++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 40 deletions(-) diff --git a/gb.go b/gb.go index da26b40..7ee46d5 100644 --- a/gb.go +++ b/gb.go @@ -1,12 +1,16 @@ package main import ( + "encoding/json" "flag" "fmt" "os" "sort" + "strings" "time" + ioutil "io/ioutil" + git "github.com/libgit2/git2go" ) @@ -20,6 +24,7 @@ const ( BaseBranch string = "master" ) +// @todo Always append \n to msg. func exit(msg string, args ...string) { fmt.Printf(msg, args) os.Exit(1) @@ -58,11 +63,12 @@ type Comparison struct { Branch *git.Branch Oid *git.Oid - ahead int - behind int + IsMerged bool + Ahead int + Behind int } -func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) *Comparison { +func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch, store CacheStore) *Comparison { c := new(Comparison) c.Repo = repo @@ -71,8 +77,17 @@ func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) c.Branch = branch c.Oid = branch.Target() - c.ahead = -1 - c.behind = -1 + cache := store[c.CacheKey()] + + if cache != nil { + c.Ahead = cache.Ahead + c.Behind = cache.Behind + c.IsMerged = cache.IsMerged + } else { + c.IsMerged = false + c.Ahead = -1 + c.Behind = -1 + } return c } @@ -93,18 +108,6 @@ func (c *Comparison) IsHead() bool { return head } -func (c *Comparison) IsMerged() bool { - if c.Oid.String() == c.BaseOid.String() { - return true - } else { - merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid) - if err != nil { - exit("Could not get descendant of '%s' and '%s'.\n", c.BaseOid.String(), c.Oid.String()) - } - return merged - } -} - func (c *Comparison) Commit() *git.Commit { commit, err := c.Repo.LookupCommit(c.Oid) if err != nil { @@ -128,31 +131,43 @@ func (c *Comparison) When() time.Time { } func (c *Comparison) FormattedWhen() string { - return c.When().Format("2006-01-02 15:04") -} - -func (c *Comparison) Ahead() int { - c.ComputeAheadBehind() - return c.ahead + return c.When().Format("2006-01-02 15:04PM") } -func (c *Comparison) Behind() int { - c.ComputeAheadBehind() - return c.behind +func (c *Comparison) CacheKey() string { + strs := []string{c.BaseOid.String(), c.Oid.String()} + return strings.Join(strs, "..") } -func (c *Comparison) ComputeAheadBehind() { - if c.ahead > -1 && c.behind > -1 { - return +func (c *Comparison) SetIsMerged() { + if c.Oid.String() == c.BaseOid.String() { + c.IsMerged = true + } else { + merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid) + if err != nil { + exit("Could not get descendant of '%s' and '%s'.\n", c.BaseOid.String(), c.Oid.String()) + } + c.IsMerged = merged } +} +func (c *Comparison) SetAheadBehind() { var err error - c.ahead, c.behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid) + c.Ahead, c.Behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid) if err != nil { exit("Error getting ahead/behind\n", c.BaseOid.String()) } } +func (c *Comparison) Execute() { + if c.Ahead > -1 && c.Behind > -1 { + return + } + + c.SetIsMerged() + c.SetAheadBehind() +} + type Comparisons []*Comparison type ComparisonsByWhen Comparisons @@ -189,7 +204,31 @@ func NewOptions() *Options { return o } +type CacheStore map[string]*Comparison + +func NewCacheStore() CacheStore { + bits, err := ioutil.ReadFile(".git/go_gb_cache.json") + if err != nil { + // no-op: `cache.json` will be written on exit. + } + + y := make(CacheStore) + json.Unmarshal(bits, &y) + return y +} + +func (store *CacheStore) WriteToFile() error { + b, err := json.Marshal(store) + if err != nil { + fmt.Printf("Could not save cache to file.\n") + } + ioutil.WriteFile(".git/go_gb_cache.json", b, 0644) + return nil +} + func main() { + store := NewCacheStore() + opts := NewOptions() repo := NewRepo() @@ -200,7 +239,7 @@ func main() { // type BranchIteratorFunc func(*Branch, BranchType) error branch_iterator.ForEach(func(branch *git.Branch, btype git.BranchType) error { - comp := NewComparison(repo, base_oid, branch) + comp := NewComparison(repo, base_oid, branch, store) comparisons = append(comparisons, comp) return nil }) @@ -208,24 +247,26 @@ func main() { sort.Sort(ComparisonsByWhen(comparisons)) for _, comp := range comparisons { + comp.Execute() + merged_string := "" - if comp.IsMerged() { + if comp.IsMerged { merged_string = "(merged)" } - if opts.Ahead != -1 && opts.Ahead != comp.Ahead() { + if opts.Ahead != -1 && opts.Ahead != comp.Ahead { continue } - if opts.Behind != -1 && opts.Behind != comp.Behind() { + if opts.Behind != -1 && opts.Behind != comp.Behind { continue } - if opts.Merged && !comp.IsMerged() { + if opts.Merged && !comp.IsMerged { continue } - if opts.NoMerged && comp.IsMerged() { + if opts.NoMerged && comp.IsMerged { continue } @@ -235,11 +276,13 @@ func main() { comp.Color(), comp.FormattedWhen(), comp.Name(), - comp.Behind(), - comp.Ahead(), + comp.Behind, + comp.Ahead, merged_string) + + store[comp.CacheKey()] = comp } - // @todo store all comparisons in a list that can be sorted before printing. - // @todo filter them things + store.WriteToFile() + } From 3863f66a16c13be208a513733df9ed5ab453e22a Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 12 Jun 2015 00:50:43 -0300 Subject: [PATCH 13/18] Add -clear-cache option --- gb.go | 18 ++++++++++++------ gb_test | 11 +++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 gb_test diff --git a/gb.go b/gb.go index 7ee46d5..9eb3a0e 100644 --- a/gb.go +++ b/gb.go @@ -185,10 +185,11 @@ func (a ComparisonsByWhen) Less(i, j int) bool { } type Options struct { - Ahead int - Behind int - Merged bool - NoMerged bool + Ahead int + Behind int + Merged bool + NoMerged bool + ClearCache bool } func NewOptions() *Options { @@ -198,6 +199,7 @@ func NewOptions() *Options { flag.IntVar(&o.Behind, "behind", -1, "only show branches that are commits behind.") flag.BoolVar(&o.Merged, "merged", false, "only show branches that are merged.") flag.BoolVar(&o.NoMerged, "no-merged", false, "only show branches that are not merged.") + flag.BoolVar(&o.ClearCache, "clear-cache", false, "clear cache of comparisons.") flag.Parse() @@ -227,10 +229,14 @@ func (store *CacheStore) WriteToFile() error { } func main() { - store := NewCacheStore() - opts := NewOptions() + if opts.ClearCache { + os.Remove(".git/go_gb_cache.json") + } + + store := NewCacheStore() + repo := NewRepo() branch_iterator := NewBranchIterator(repo) base_oid := LookupBaseOid(repo) diff --git a/gb_test b/gb_test new file mode 100644 index 0000000..a33758c --- /dev/null +++ b/gb_test @@ -0,0 +1,11 @@ +package main + +import ( + "testing" + + assert "github.com/stretchr/testify/assert" +) + +func TestMain(t *testing.T) { + assert.Equal(t, 0, 1) +} From 812081423073c1e23d8b3599d9fc157ec74c1f23 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 12 Jun 2015 00:52:12 -0300 Subject: [PATCH 14/18] Move cache path to constant. --- gb.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/gb.go b/gb.go index 9eb3a0e..458e37e 100644 --- a/gb.go +++ b/gb.go @@ -22,6 +22,8 @@ const ( Green = "\x1b[0;32m" BaseBranch string = "master" + + CachePath = ".git/go_gb_cache.json" ) // @todo Always append \n to msg. @@ -209,7 +211,7 @@ func NewOptions() *Options { type CacheStore map[string]*Comparison func NewCacheStore() CacheStore { - bits, err := ioutil.ReadFile(".git/go_gb_cache.json") + bits, err := ioutil.ReadFile(CachePath) if err != nil { // no-op: `cache.json` will be written on exit. } @@ -224,7 +226,7 @@ func (store *CacheStore) WriteToFile() error { if err != nil { fmt.Printf("Could not save cache to file.\n") } - ioutil.WriteFile(".git/go_gb_cache.json", b, 0644) + ioutil.WriteFile(CachePath, b, 0644) return nil } @@ -232,7 +234,7 @@ func main() { opts := NewOptions() if opts.ClearCache { - os.Remove(".git/go_gb_cache.json") + os.Remove(CachePath) } store := NewCacheStore() From f6bd208ed5ef18dae00f8c818fa297f5db25695c Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 19 Jun 2015 17:05:00 -0300 Subject: [PATCH 15/18] Add red color output. --- gb.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/gb.go b/gb.go index 458e37e..7bfb41e 100644 --- a/gb.go +++ b/gb.go @@ -12,15 +12,16 @@ import ( ioutil "io/ioutil" git "github.com/libgit2/git2go" + "github.com/mgutz/ansi" ) -type ColorType string +var ( + Red string = ansi.ColorCode("red") + Yellow = ansi.ColorCode("yellow") + Green = ansi.ColorCode("green") +) const ( - Red ColorType = "\x1b[0;31m" - Yellow = "\x1b[0;33m" - Green = "\x1b[0;32m" - BaseBranch string = "master" CachePath = ".git/go_gb_cache.json" @@ -113,15 +114,19 @@ func (c *Comparison) IsHead() bool { func (c *Comparison) Commit() *git.Commit { commit, err := c.Repo.LookupCommit(c.Oid) if err != nil { - exit("Could not lookup commit '%s'.\n", c.Oid.String()) + exit("Could not lookup commit '%s'.", c.Oid.String()) } return commit } -// @todo red for old commits -func (c *Comparison) Color() ColorType { +func (c *Comparison) ColorCode() string { + hours, _ := time.ParseDuration("336h") // two weeks + two_weeks := time.Now().Add(-hours) + if c.IsHead() { return Green + } else if c.When().Before(two_weeks) { + return Red } else { return Yellow } @@ -278,10 +283,9 @@ func main() { continue } - // continue fmt.Printf( - "%s%s | %-30s | behind: %4d | ahead: %4d %s\n", - comp.Color(), + "%s%s | %-30s | behind: %4d | ahead: %4d %s", + comp.ColorCode(), comp.FormattedWhen(), comp.Name(), comp.Behind, From a45e3460d20f28fb8037c49d84b6c6fadf95b31d Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 19 Jun 2015 17:16:30 -0300 Subject: [PATCH 16/18] Improve error messages. --- gb.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/gb.go b/gb.go index 7bfb41e..83babdd 100644 --- a/gb.go +++ b/gb.go @@ -27,17 +27,17 @@ const ( CachePath = ".git/go_gb_cache.json" ) -// @todo Always append \n to msg. -func exit(msg string, args ...string) { - fmt.Printf(msg, args) +func exit(msg string, args ...interface{}) { + msg = fmt.Sprintf(msg, args...) + fmt.Println(msg) os.Exit(1) } func NewRepo() *git.Repository { repo, err := git.OpenRepository(".") if err != nil { - // @todo improve message - exit("Could not open repository at '.'\n") + wd, _ := os.Getwd() + exit("Could not open repository at '%s'", wd) } return repo } @@ -45,8 +45,8 @@ func NewRepo() *git.Repository { func NewBranchIterator(repo *git.Repository) *git.BranchIterator { i, err := repo.NewBranchIterator(git.BranchLocal) if err != nil { - // @todo improve message - exit("Can't list branches\n") + wd, _ := os.Getwd() + exit("Failed to list branches for '%s'", wd) } return i } @@ -54,7 +54,7 @@ func NewBranchIterator(repo *git.Repository) *git.BranchIterator { func LookupBaseOid(repo *git.Repository) *git.Oid { base_branch, err := repo.LookupBranch(BaseBranch, git.BranchLocal) if err != nil { - exit("Error looking up %s\n", BaseBranch) + exit("Error looking up '%s'", BaseBranch) } return base_branch.Target() @@ -98,7 +98,7 @@ func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch, func (c *Comparison) Name() string { name, err := c.Branch.Name() if err != nil { - exit("Can't get branch name\n") + exit("Can't get branch name for '%s'", c.Oid) } return name } @@ -106,7 +106,7 @@ func (c *Comparison) Name() string { func (c *Comparison) IsHead() bool { head, err := c.Branch.IsHead() if err != nil { - exit("Can't get IsHead\n") + exit("Can't get IsHead for '%s'", c.Name()) } return head } @@ -152,7 +152,7 @@ func (c *Comparison) SetIsMerged() { } else { merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid) if err != nil { - exit("Could not get descendant of '%s' and '%s'.\n", c.BaseOid.String(), c.Oid.String()) + exit("Could not get descendant of '%s' and '%s'.", c.BaseOid.String(), c.Oid.String()) } c.IsMerged = merged } @@ -162,7 +162,7 @@ func (c *Comparison) SetAheadBehind() { var err error c.Ahead, c.Behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid) if err != nil { - exit("Error getting ahead/behind\n", c.BaseOid.String()) + exit("Error getting ahead/behind", c.BaseOid.String()) } } @@ -222,14 +222,15 @@ func NewCacheStore() CacheStore { } y := make(CacheStore) - json.Unmarshal(bits, &y) + _ = json.Unmarshal(bits, &y) + return y } func (store *CacheStore) WriteToFile() error { b, err := json.Marshal(store) if err != nil { - fmt.Printf("Could not save cache to file.\n") + fmt.Printf("Could not save cache to file.") } ioutil.WriteFile(CachePath, b, 0644) return nil From 866eaec712ef88436837dea1dbe8b01f2381f985 Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Fri, 19 Jun 2015 17:16:36 -0300 Subject: [PATCH 17/18] Fix missing newline. --- gb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gb.go b/gb.go index 83babdd..28e6f7f 100644 --- a/gb.go +++ b/gb.go @@ -285,7 +285,7 @@ func main() { } fmt.Printf( - "%s%s | %-30s | behind: %4d | ahead: %4d %s", + "%s%s | %-30s | behind: %4d | ahead: %4d %s\n", comp.ColorCode(), comp.FormattedWhen(), comp.Name(), From 46b1a6966051f5c38e09d5c010e0110b491aa8ba Mon Sep 17 00:00:00 2001 From: Vincent Roy Date: Sat, 20 Jun 2015 20:58:49 -0300 Subject: [PATCH 18/18] Cleanup. --- Makefile | 79 ------------------------------------------------------- README.md | 14 +++------- gb.1 | 34 ------------------------ gb_test | 11 -------- install | 29 -------------------- 5 files changed, 3 insertions(+), 164 deletions(-) delete mode 100644 Makefile delete mode 100644 gb.1 delete mode 100644 gb_test delete mode 100755 install diff --git a/Makefile b/Makefile deleted file mode 100644 index 6ce766d..0000000 --- a/Makefile +++ /dev/null @@ -1,79 +0,0 @@ -UNAME := $(shell uname -s) - -CC=gcc - -CFLAGS := -std=c99 -Wall -CFLAGS += $(shell pkg-config --libs libgit2 jansson) -CFLAGS += $(shell pkg-config --cflags libgit2 jansson) - -# Add -rpath option so that the dynamic linker knows where to find shared -# libraries and avoid having to set LD_LIBRARY_PATH. -# -# See http://stackoverflow.com/a/695684 for background on this. -ifeq ($(UNAME), Linux) -CFLAGS += -Wl,-rpath /usr/local/lib -endif - -TARGET=build/gb -SRC=src/main.c - -INSTALL_DIR=/usr/local/bin/gb - -.PHONY: install build - -all: build - -build: src/main.c - mkdir -p build/ - $(CC) -o $(TARGET) $(SRC) $(CFLAGS) - -install: man - cp $(TARGET) $(INSTALL_DIR) - -force: - touch $(SRC) - -# Mainly for use when developing -run: clean force build install - gb - -clean: - @rm -rf build/ - -man: - install -g 0 -o 0 -m 0644 gb.1 /usr/share/man/man1/gb.1 - rm -f /usr/share/man/man1/gb.1.gz - gzip -f /usr/share/man/man1/gb.1 - -deps: clean - @if [ `pkg-config --modversion jansson` == "2.7" ]; then \ - echo "jansson was found - skipping installation"; \ - else \ - echo "installing jansson" && \ - mkdir -p build && \ - cd build && \ - wget http://www.digip.org/jansson/releases/jansson-2.7.tar.gz && \ - tar xzf jansson-2.7.tar.gz && \ - cd jansson-2.7 && \ - ./configure && \ - make && \ - make check && \ - echo "sudo password required for 'sudo make install' in jansson" && \ - sudo make install; \ - fi; \ - - - @if [ `pkg-config --modversion libgit2` == "0.22.1" ]; then \ - echo "libgit2 was found - skipping installation"; \ - else \ - echo "installing libgit2" && \ - mkdir -p build && \ - cd build && \ - wget https://github.com/libgit2/libgit2/archive/v0.22.1.tar.gz && \ - tar xzf v0.22.1.tar.gz && \ - cd libgit2-0.22.1 && \ - mkdir build && \ - cd build && \ - cmake .. && \ - cmake --build . --target install; \ - fi diff --git a/README.md b/README.md index 0ef839d..85583a7 100644 --- a/README.md +++ b/README.md @@ -16,14 +16,6 @@ The output is sorted in chronological order - your last modified branches appear ## Installation -The install script will do its best to install dependencies before compiling. - - curl -sSL https://raw.githubusercontent.com/vroy/gb/master/install | bash - -Or alternatively, after making sure that [cmake](http://www.cmake.org/) is installed: - - git clone git@github.com:vroy/gb.git - cd gb - make deps - make - sudo make install + brew install go + brew install libgit2 + go get github.com/vroy/gb diff --git a/gb.1 b/gb.1 deleted file mode 100644 index a75e217..0000000 --- a/gb.1 +++ /dev/null @@ -1,34 +0,0 @@ -.\" Manpage for gb. -.\" Contact vincentroy8@gmail.com to correct errors or typos. - -.TH gb 1 "14 March 2015" "0.0.2" "gb man page" - -.SH NAME -gb \- List git branches with additional information about them. - -.SH SYNOPSIS -gb [-merged] [-no-merged] [-ahead ] [-behind ] - -.SH OPTIONS - -.IP "-a " -only show branches that are commits ahead. - -.IP "-b " -only show branches that are commits behind. - -.IP "--merged" -only show branches that are merged. - -.IP "--no-merged" -only show branches that are not merged. - -.SH BUGS -No known bugs. - -.SH SEE ALSO - -https://github.com/vroy/gb - -.SH AUTHOR -Vincent Roy (vincentroy8@gmail.com) diff --git a/gb_test b/gb_test deleted file mode 100644 index a33758c..0000000 --- a/gb_test +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - "testing" - - assert "github.com/stretchr/testify/assert" -) - -func TestMain(t *testing.T) { - assert.Equal(t, 0, 1) -} diff --git a/install b/install deleted file mode 100755 index dbde1a7..0000000 --- a/install +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env bash - -if which cmake >/dev/null -then - echo 'cmake is installed.' - -elif which apt-get >/dev/null -then - echo 'installing cmake with apt-get' - sudo apt-get install cmake - -elif which brew >/dev/null -then - echo 'installing cmake with brew' - brew install cmake - -else - echo 'Could not install cmake. Make sure that cmake is installed and in your $PATH.' - exit 1 - -fi - -wget https://github.com/vroy/gb/archive/v0.0.4.tar.gz -tar xzf v0.0.4.tar.gz -cd gb-0.0.4 - -sudo make deps -make -sudo make install