Esempio n. 1
0
// Commit commits the current changes to the repo
func Commit() {
	repo, err := meta.Open()
	if err != nil {
		fmt.Println(err)
		return
	}
	commit := new(meta.Commit)
	err = fillCommit(commit, repo)
	if err != nil {
		fmt.Println(err)
		return
	}
	branch, _ := repo.Find(repo.Current)
	branch.Commit = append(branch.Commit, *commit)
	err = repo.Write()
	if err != nil {
		panic(err)
	}
	err = stashFiles(branch.Title, commit)
	if err != nil {
		branch.DeleteLastCommit()
		fmt.Println(err)
		return
	}
	fmt.Println(commit.Timestamp)
	fmt.Println(commit.Title + "\n")
	fmt.Println(commit.Message)
}
Esempio n. 2
0
// Display displays a list of all branches
func Display() {
	repo, err := meta.Open()
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, branch := range repo.Branch {
		if branch.Title == repo.Current {
			fmt.Print("*")
		} else {
			fmt.Print(" ")
		}
		fmt.Println(branch.Title)
	}
}
Esempio n. 3
0
// GetFileStatus maps file names to their status.
func GetFileStatus() (map[string]int, error) {
	// Read metadata
	repo, err := meta.Open()
	if err != nil {
		return nil, err
	}

	branch, _ := repo.Find(repo.Current)
	if branch == nil {
		panic(fmt.Errorf("Repo is corrupt"))
	}

	// Check if initial commit
	commit := (*meta.Commit)(nil)
	numCommits := len(branch.Commit)
	if numCommits > 0 {
		commit = &branch.Commit[numCommits-1]
	}
	return CompareStatusToCommit(commit)
}
Esempio n. 4
0
// newBranch creates a new branch and returns a pointer to it
func newBranch(title string) (*meta.Branch, error) {
	repo, err := meta.Open()
	if err != nil {
		return nil, err
	}
	if repo.SetCurrent(title) == nil {
		err = repo.Write()
		if err != nil {
			return nil, err
		}
		fmt.Println("Switched to branch: \x1b[32;1m" + title + "\x1b[0m")
		revert.Revert(0)
		return nil, nil
	}
	current, _ := repo.Find(repo.Current)
	if current == nil {
		panic(fmt.Errorf("Repo is corrupt"))
	}
	branch := current.Copy()
	branch.Title = title
	branch.Parent = current.Title
	current.Child = append(current.Child, branch.Title)
	err = repo.AddBranch(branch)
	if err != nil {
		return nil, err
	}
	err = repo.SetCurrent(branch.Title)
	if err != nil {
		return nil, err
	}
	err = repo.Write()
	if err != nil {
		return nil, err
	}
	err = copyStash(current.Title, branch.Title)
	if err != nil {
		return nil, err
	}
	fmt.Println("Created new branch: \x1b[32;1m" + title + "\x1b[0m")
	return branch, nil
}
Esempio n. 5
0
// Revert reverts the state of the working directory to that
// of n commits ago.
func Revert(n int) {
	repo, err := meta.Open()
	if err != nil {
		fmt.Println(err)
		return
	}
	branch, _ := repo.Find(repo.Current)
	if len(branch.Commit) < n {
		fmt.Printf("Only %d commits exist!\n", len(branch.Commit))
		return
	}
	var commit *meta.Commit
	if len(branch.Commit) == n {
		commit = nil
	} else {
		commit = &branch.Commit[(len(branch.Commit)-1)-n]
	}
	files, err := status.CompareStatusToCommit(commit)
	if err != nil {
		fmt.Println(err)
		return
	}
	repoDir, err := dirutils.OpenRepo()
	if err != nil {
		fmt.Println(err)
		return
	}
	// Might have to restore some files if not at initial commit
	if commit != nil {
		err = stash.Init(branch.Title, commit.Timestamp.String())
		if err != nil {
			fmt.Println(err)
			return
		}
	}
	for file, stat := range files {
		if stat == status.NEW {
			fileWithPath := repoDir.Name() + "/" + file
			err = os.Remove(fileWithPath)
			if err != nil {
				fmt.Println(err)
				fmt.Println("Repo may be corrupt.")
				return
			}
			err = dirutils.DeleteIfEmpty(fileWithPath[:strings.LastIndex(fileWithPath, "/")])
			if err != nil {
				fmt.Println(err)
				fmt.Println("Repo may be corrupt.")
				return
			}
		} else if stat == status.REMOVED || stat == status.MODIFIED {
			err = stash.Restore(file)
			if err != nil {
				fmt.Println(err)
				fmt.Println("Repo may be corrupt.")
				return
			}
		}
	}
	err = cleanup(branch, n)
	if err != nil {
		fmt.Println(err)
		fmt.Println("Repo may be corrupt.")
		return
	}
	err = repo.Write()
	if err != nil {
		fmt.Println(err)
		fmt.Println("Repo may be corrupt.")
		return
	}
	if len(branch.Commit) == 0 {
		fmt.Println("Repo reset to intial state")
	} else {
		fmt.Println("Repo reset to commit:")
		fmt.Println("\x1b[32;1m" + branch.Commit[len(branch.Commit)-1].Title + "\x1b[0m")
		fmt.Printf("%v\n", branch.Commit[len(branch.Commit)-1].Timestamp)
	}
}